import os from string import strip from shutil import copyfile from urllib2 import urlopen from time import localtime today = str(localtime()[0]) + '-' + str(localtime()[1]) + '-' + str(localtime()[2]) class Storage: def __init__(self): self.path = os.path.expanduser('~/Notes/Date') self.tagPath = os.path.expanduser('~/Notes/Tag') def getDates(self): ''' Returns list of all dates ''' list = os.listdir(self.path) return list def getDatesNumber(self): ''' Returns total dates number ''' return len(self.getDates()) def getTags(self): ''' Returns list of all tags ''' list = os.listdir(self.tagPath) return list def getTagsNumber(self): ''' Returns total tags number ''' return len(self.getTags()) def getNotes(self): notes = [] for date in self.getDates(): notes += Date(date).getNotes() return notes def getNotesNumber(self): ''' Returns total notes number ''' return len(self.getNotes()) class Note: def __init__(self, name): self.name = name def exists(self): ''' Returns True if note exists and False if not''' if self.getPath() is None: return False else: return True def getDate(self): ''' Returns creation date of selected note ''' for date in Storage().getDates(): if Date(date).hasNote(self.name): return date def getPath(self): ''' Returns file path for selected note ''' path = None if self.getDate() != None: path = os.path.join(Storage().path, self.getDate(), self.name) return path def getText(self): text = None ''' Returns full text for selected note ''' if self.exists(): file = open(self.getPath(), 'r') text = ''.join(file.readlines()) return text def getTags(self): ''' Returns tags applied to selected note ''' tags = [] if self.exists(): for tag in Storage().getTags(): if Tag(tag).hasNote(self.name): tags.append(tag) return tags def getKeywords(self): ''' Returns list containing 5 most commonly used words in text ''' keywords = [] if self.exists(): words = {} for line in open(self.getPath(), 'r'): line = strip(line, " \n") for word in line.split(" "): try: words[word] += 1 except KeyError: words[word] = 1 pairs = words.items() pairs.sort(lambda a, b: b[1]-a[1]) for p in pairs[:5]: keywords.append(p[0]) return keywords def remove(self): ''' Removes note and date/tag dirs if empty ''' if self.exists(): date = Date(self.getDate()) tags = self.getTags() os.remove(self.getPath()) date.remove() for tag in tags: os.unlink(os.path.join(Storage().tagPath, tag, self.name)) Tag(tag).remove() def write(self, text): ''' Writes some text to note ''' if not self.exists(): Date(today).create() file = open(os.path.join(Storage().path, today, self.name), 'w') file.write(text) file.close() else: file = open(self.getPath(), 'w') file.write(text) file.close() def tag(self, tags): ''' Applies user given tags to note ''' if self.exists(): src = self.getTags() for tag in tags: if tag not in src: Tag(tag).create() os.symlink(self.getPath(), os.path.join(Storage().tagPath, tag, self.name)) for tag in src: if tag not in tags: os.unlink(os.path.join(Storage().tagPath, tag, self.name)) Tag(tag).remove() def importFile(self, filePath): ''' Imports specified local file as a new note ''' if not self.exists(): if os.path.exists(filePath) and os.path.isfile(filePath): Date(today).create() copyfile(filePath, os.path.join(Storage().path, today, self.name)) def importUrl(self, url): ''' Saves text from specified web page as new note ''' if not self.exists(): try: page = urlopen(url) text = ''.join(page.readlines()) self.write(text) except: pass class Date: def __init__(self, date): self.value = date def exists(self): ''' Returns True if date exists and False if not ''' if self.value in Storage().getDates(): return True else: return False def getPath(self): path = None ''' Returns path for selected date ''' if self.exists(): path = os.path.join(Storage().path, self.value) return path def getNotes(self): ''' Returns list that contains notes created at selected date ''' notes = [] if self.exists(): notes = os.listdir(self.getPath()) return notes def getNumber(self): ''' Returns number of notes created at selected date ''' return len(self.getNotes()) def remove(self): ''' Removes selected date if there are no notes created at it ''' if self.exists() and self.getNumber() == 0: os.rmdir(self.getPath()) def create(self): ''' Creates specified date ''' dir = os.path.join(Storage().path, self.value) if not self.exists(): os.mkdir(dir) def hasNote(self, name): if name in self.getNotes(): return True else: return False class Tag: def __init__(self, tag): self.value = tag def exists(self): ''' Returns True if tag exists and False if not ''' if self.value in Storage().getTags(): return True else: return False def getPath(self): ''' Returns path for selected tag ''' path = None if self.exists(): path = os.path.join(Storage().tagPath, self.value) return path def getNotes(self): ''' Returns list that contains notes tagged with selected tag ''' notes = [] if self.exists(): notes = os.listdir(self.getPath()) return notes def getNumber(self): ''' Returns number of notes tagged with selected tag ''' return len(self.getNotes()) def remove(self): ''' Removes selected tag if there are now notes tagged with it ''' if self.exists() and self.getNumber() == 0: os.rmdir(self.getPath()) def create(self): ''' Creates specified tag ''' dir = os.path.join(Storage().tagPath, self.value) if not self.exists(): os.mkdir(dir) def hasNote(self, name): if name in self.getNotes(): return True else: return False class Text: def __init__(self, text): self.value = text def getNotes(self): ''' Returns notes that contains specified text fragment ''' notes = [] if self.value != None: for note in Storage().getNotes(): if self.value in Note(note).getText(): notes.append(note) return notes def getNumber(self): ''' Returns number of notes that contain specified text fragment ''' return len(self.getNotes()) class Search: def __init__(self, items): self.items = items def getNotes(self): ''' Returns list that contains notes matching all search items ''' full_list = [] for item in self.items: notes = Date(item).getNotes() + Tag(item).getNotes() + Text(item).getNotes() if Note(item).exists(): if item not in notes: notes.append(item) full_list.append(notes) list_intersection = lambda list: set(list[0]) if len(list) == 1 else set(list[0]).intersection(list_intersection(list[1::])) results = list(list_intersection(full_list)) return results def getNumber(self): ''' Returns number of notes matching all search items ''' return len(self.getNotes())