Public feed
#887282
#!/usr/bin/python# -*- coding: utf-8 -*-# This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version. # This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.# # You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,# MA 02110-1301, USA.#Importing modulesimport osimport sysimport getoptfrom shutil import copyfrom time import localtime#Setting Variablespath = os.path.expanduser('~/Notes/All')tag_path = os.path.expanduser('~/Notes/Tag')today = str(localtime()[0]) + '-' + str(localtime()[1]) + '-' + str(localtime()[2])#Defining functionsdef dircheck(): if not os.path.exists(os.path.join(path, today)): try: os.mkdir(os.path.join(path, today)) except OSError: sys.exit('Disk is full or you have no write permissions')def create(name): dircheck() if not os.path.exists(os.path.join(path, today, name)): text = raw_input(':: Enter text\n') try: note_path = os.path.join(path, today, name) open(note_path, 'w').write(text) tag(note_path) except OSError: sys.exit('Disk is full or you have no write permissions') else: print 'Note with such name already exists'def add(note): dircheck() if not os.path.exists(os.path.join(path, today, os.path.split(note)[1])): try: note_path = os.path.join(path,today,os.path.split(note)[1]) copy(note, note_path) tag(note_path) except: sys.exit('Disk is full or you have no write permissions') tag(note_path) else: print 'Note with such name already exists' def edit(note_path): full_path = os.path.join(path,note_path) if os.path.exists(full_path): try: os.system('nano %s' % (full_path)) except: print 'GNU Nano is not installed' else: print 'No such note'def tag(note_path): tag = raw_input(':: Enter tags\n') taglist = [] while tag != '': taglist.append(tag) tag=raw_input() if taglist != []: for eachtag in taglist: if not os.path.exists(os.path.join(tag_path, eachtag)): try: os.mkdir(os.path.join(tag_path, eachtag)) except: pass try: os.symlink(note_path, os.path.join(tag_path, eachtag, os.path.split(note_path)[1])) except OSError: sys.exit('Disk is full or you have no write permissions') else: print 'No tags given'def view(notename): for eachdir in os.listdir(path): if notename in os.listdir(os.path.join(path, eachdir)): note = open(os.path.join(path, eachdir, notename), 'r') print 'Date: ',eachdir taglist = [] for eachtagdir in os.listdir(tag_path): if notename in os.listdir(os.path.join(tag_path, eachtagdir)) and \ os.readlink(os.path.join(tag_path, eachtagdir, notename)) == os.path.join(path, eachdir, notename): taglist.append(eachtagdir) print 'Tags: ',taglist print 'Text: ','\n'.join(note.readlines()) def search(): date_query = raw_input(':: Enter date:\n') tag_query = raw_input(':: Enter tag:\n') if date_query == '' and tag_query == '': print 'No queries to search' elif tag_query == '': if os.path.exists(os.path.join(path, date_query)): for eachnote in os.listdir(os.path.join(path, date_query)): print 'Name: ', eachnote note = open(os.path.join(path, date_query, eachnote), 'r') print '\n'.join(note.readlines()) else: print 'No notes dated by %s' % (str(date_query)) elif date_query == '': if os.path.exists(os.path.join(tag_path, tag_query)): for eachnote in os.listdir(os.path.join(tag_path, tag_query)): print 'Name: ', eachnote note = open(os.readlink(os.path.join(tag_path, tag_query, eachnote)),'r') print '\n'.join(note.readlines()) else: print 'No notes tagged with %s' % (tag_query) else: if os.path.exists(os.path.join(path,date_query)) and os.path.exists(os.path.join(path, date_query)): for eachnote in os.listdir(os.path.join(path, date_query)): if eachnote in os.listdir(os.path.join(tag_path, tag_query)) and \ os.readlink(os.path.join(tag_path, tag_query, eachnote))==os.path.join(path, date_query, eachnote): print 'Name: ', eachnote note=open(os.path.join(path, date_query, eachnote), 'r') print '\n'.join(note.readlines()) else: print 'No notes dated by %s and tagged with %s' % (str(date_query),str(tag_query))def summary(): for eachdate in os.listdir(path): if len(os.listdir(os.path.join(path, eachdate)))!=0: print eachdate, ' - ', len(os.listdir(os.path.join(path, eachdate))) for eachtag in os.listdir(tag_path): if len(os.listdir(os.path.join(tag_path, eachtag)))!=0: print eachtag, ' - ', len(os.listdir(os.path.join(tag_path, eachtag)))def main(): help = 'USAGE: nofi [OPTION] [ARGUMENT]\n-n, --new [NAME]\n create new note\n-i, --import [PATH]\n add text file to db\n-e, --edit [DATE/NAME]\n edit note\n-v, --view [NAME]\n view note and its properties (e.g.: name, date, tags)\n-f, --find\n search notes by date or/and tag\n-s, --summary\n view summary\n-h, --help\n view this help message\n-a, --about\n about this software' about = 'NoteFinder 0.5 Devel\nEasy-to-use console notes manager\nCopyright (c) 2008 GFORGX <gforgx@gmail.com>\nArtwork (c) 2008 Melfina <melfina2106@mail.ru>' try: opts, args = getopt.getopt(sys.argv[1:], "ae:fhi:n:p:sv:", ["about","edit=","find","help","import=","new=","prop=","summary","view="]) except getopt.GetoptError: print help sys.exit('\nIncorrect option') for o, a in opts: if o in ('-f', '--find'): search() elif o in ('-i', '--import'): note = a add(note) elif o in ('-n', '--new'): name = a create(name) elif o in ('e, --edit'): note_path=a edit(note_path) elif o in ('-s', '--summary'): summary() elif o in ('-v', '--view'): notename = a view(notename) elif o in ('-h', '--help'): print help elif o in ('-a', '--about'): print about#Executing scriptif __name__ == "__main__": main()