#!/usr/bin/env python #----------------------------------------------------------------------- # scribbler.py - diary script # Copyright (C) 2001 Andrei Kulakov # Licence: GPL (see http://www.gnu.org/copyleft/gpl.html) #----------------------------------------------------------------------- """ Usage: scribbler.py edit today's entry scribbler.py -b browse entries scribbler.py -v print version scribbler.py -h this help message """ import sys, time, os, os.path, commands __version__ = '0.5a' editor = 'vim' browser = 'lynx' program_dir = os.path.expanduser('~/.scribbler') conf_file = os.path.join(program_dir, "config") last_fname = None def browse(): """Browse all entries""" if os.path.exists(m): dest = m elif os.path.exists(y): dest = y else: dest = program_dir os.system(browser + ' ' + dest) def read_config(): if os.path.exists(conf_file): f = open(conf_file) last_filename = f.read() return last_filename def write_config(last_filename): f = open(conf_file, 'w') f.write(last_filename) def edit_today_entry(): """Edit today's diary entry""" if not os.path.exists(y): os.mkdir(y) if not os.path.exists(m): os.mkdir(m) last_fname = read_config() f = d if last_fname: if last_fname.startswith(d[:-4]): f = last_fname if not os.path.exists(f): file = open(f, 'w') file.write('\t\t\t' + time.ctime() + '\n') file.close() os.system(editor + ' "' + f + '"') file = open(f, 'r') file.readline() name = file.readline().strip() file.close() if name: nf = d[:-4] + '-' + name + ".txt" os.rename(f, nf) write_config(nf) def process_time(t): min, sec = t/60, t%60 return '%2d:%02d' % (min, sec) if not os.path.exists(program_dir): os.mkdir(program_dir) if not commands.getoutput('which ' + editor): print 'Can not find editor:', editor print 'You can get vim at http://www.vim.org' print 'To use a different editor, change editor variable at the top of scribbler.py' sys.exit() if not commands.getoutput('which ' + browser): print 'Can not find browser:', browser print 'You can get lynx at http://lynx.browser.org' print 'To use a different browser, change browser variable at the top of scribbler.py' sys.exit() start = time.time() year, month_num, day, _,_,_,_,_,_ = time.localtime() month = time.ctime(time.time()).split()[1] weekday = time.ctime().split()[0] y = os.path.join(program_dir, str(year)) m = os.path.join(y, "%02d-%s" % (month_num, month)) d = os.path.join(m, "%02d-%s.txt" % (day, weekday)) if len(sys.argv) > 1: if sys.argv[1] == '-b': browse() elif sys.argv[1] == '-h': print __doc__ sys.exit() elif sys.argv[1] == '-v': print 'Scribbler v' + __version__ sys.exit() else: print 'Illegal argument! Try better next time.' sys.exit() else: edit_today_entry() time_taken = time.time() - start print process_time(time_taken)