From 6b9738fb93cedcf44056466792876f3fd5746d05 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 7 Dec 2010 10:20:41 +0000 Subject: Rewrite in Python for hackability. --- scripts/journal-note | 161 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 120 insertions(+), 41 deletions(-) diff --git a/scripts/journal-note b/scripts/journal-note index f91a07b..211aa72 100755 --- a/scripts/journal-note +++ b/scripts/journal-note @@ -1,46 +1,125 @@ -#!/bin/sh +#!/usr/bin/python +# Copyright 2010 Lars Wirzenius +# +# 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 3 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, see . -set -e -if [ "$#" != 1 ] -then - echo "Usage: $0 title" - exit 1 -fi +import optparse +import os +import subprocess +import sys +import tempfile +import time +import traceback -title="$1" -temp="$(mktemp --tmpdir=$HOME/Journal)" -cat << eof > "$temp" -[[!meta title="$title"]] +template = '''\ +[[!meta title="%(title)s"]] [[!tag ]] -[[!meta date="$(date +%Y-%m-%dT%H:%M)"]] - -eof - -checksum="$(mktemp)" -md5sum "$temp" > "$checksum" -vi "$temp" -if md5sum --status -c "$checksum" -then - echo "No change to note, aborting." - rm "$temp" "$checksum" - exit 1 -fi - -cat << eof >> "$temp" - -[[!meta done="$(date +%Y-%m-%dT%H:%M)"]] -eof - -cd "$HOME/Journal/src" -filename="notes/$(date +%Y-%m-%d-%H:%M.mdwn)" -if [ -e "$filename" ] -then - filename="notes/$(date +%Y-%m-%d-%H:%M:%S.mdwn)" -fi -cp "$temp" "$filename" -bzr add "$filename" -bzr commit -m "New note" "$filename" -ikiwiki --setup ../ikiwiki.setup --refresh -rm "$temp" "$checksum" +[[!meta date="%(date)s"]] + +''' + + +class AppException(Exception): + + pass + + +class App(object): + + def __init__(self): + pass + + def parse_args(self): + p = optparse.OptionParser() + + p.add_option('--base', default=os.path.expanduser('~/Journal')) + + return p.parse_args() + + def tempfile(self, dirname, content): + fd, name = tempfile.mkstemp(dir=dirname) + os.write(fd, content) + os.close(fd) + return name + + def main(self): + commands = { + 'new': self.new_entry, + 'list': self.list_entries, + 'edit': self.edit_entry, + 'finish': self.finish_entry, + } + + opts, args = self.parse_args() + if args and args[0] in commands: + commands[args[0]](args[1:], opts) + else: + raise AppException('Usage: journal-note [options] cmd args...') + + def drafts(self, opts): + return os.path.join(opts.base, 'drafts') + + def new_entry(self, args, opts): + if not args: + raise AppException('Usage: journal-note new TITLE') + values = { + 'title': args[0], + 'date': time.strftime('%Y-%m-%d %H:%M') + } + + name = self.tempfile(self.drafts(opts), template % values) + print name + + def list_entries(self, args, opts): + for name in os.listdir(self.drafts(opts)): + print name + + def edit_entry(self, args, opts): + if not args: + raise AppException('Usage: journal-note edit ID') + pathname = os.path.join(self.drafts(opts), args[0]) + subprocess.check_call(['gedit', '--new-window', pathname]) + + def finish_entry(self, args, opts): + if not args: + raise AppException('Usage: journal-note finish ID') + draft = os.path.join(self.drafts(opts), args[0]) + basename = time.strftime('%Y-%m-%d-%H:%M.mdwn') + i = 0 + while True: + finished = os.path.join(opts.base, 'src', 'notes', basename) + if not os.path.exists(finished): + break + i += 1 + basename = '%s-%d.mdwn' % (time.strftime('%Y-%m-%d-%H:%M:%S-'), i) + os.rename(draft, finished) + # FIXME: bzr add + commit, ikiwiki + + +if __name__ == '__main__': + try: + App().main() + except KeyboardInterrupt: + sys.exit(1) + except AppException, e: + sys.stderr.write('%s\n' % str(e)) + sys.exit(1) + except SystemExit, e: + sys.exit(e.code) + except BaseException, e: + sys.stderr.write(traceback.format_exc(e)) + sys.exit(1) + -- cgit v1.2.1 From 338b1caaee28d4b01a1b2c3a168b201033ebfc9f Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 7 Dec 2010 10:27:29 +0000 Subject: Put drafts in drafts folder with simple names. Show titles when listing drafts. --- scripts/journal-note | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/scripts/journal-note b/scripts/journal-note index 211aa72..60ab9c0 100755 --- a/scripts/journal-note +++ b/scripts/journal-note @@ -17,6 +17,7 @@ import optparse import os +import re import subprocess import sys import tempfile @@ -49,12 +50,6 @@ class App(object): return p.parse_args() - def tempfile(self, dirname, content): - fd, name = tempfile.mkstemp(dir=dirname) - os.write(fd, content) - os.close(fd) - return name - def main(self): commands = { 'new': self.new_entry, @@ -75,17 +70,37 @@ class App(object): def new_entry(self, args, opts): if not args: raise AppException('Usage: journal-note new TITLE') + + for i in range(1000): + name = os.path.join(opts.base, 'drafts', '%d.mdwn' % i) + if not os.path.exists(name): + break + else: + raise AppException('ERROR: too many existing drafts') + values = { 'title': args[0], 'date': time.strftime('%Y-%m-%d %H:%M') } - - name = self.tempfile(self.drafts(opts), template % values) - print name + f = open(name, 'w') + f.write(template % values) + f.close() def list_entries(self, args, opts): - for name in os.listdir(self.drafts(opts)): - print name + drafts = self.drafts(opts) + for name in os.listdir(drafts): + if name.endswith('.mdwn'): + f = open(os.path.join(drafts, name)) + for line in f: + m = re.match('\[\[!meta title="(?P.*)("\]\])$', + line) + if m: + title = m.group('title') + break + else: + title = 'unknown title' + f.close() + print name[:-len('.mdwn')], title def edit_entry(self, args, opts): if not args: -- cgit v1.2.1 From fe1f315c63a889136a8fba1cdd6b22406bb768c2 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius <liw@liw.fi> Date: Tue, 7 Dec 2010 10:29:26 +0000 Subject: Make edit and finish accept draft ids not full basenames as args. --- scripts/journal-note | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/scripts/journal-note b/scripts/journal-note index 60ab9c0..315a27c 100755 --- a/scripts/journal-note +++ b/scripts/journal-note @@ -105,13 +105,17 @@ class App(object): def edit_entry(self, args, opts): if not args: raise AppException('Usage: journal-note edit ID') - pathname = os.path.join(self.drafts(opts), args[0]) + pathname = os.path.join(self.drafts(opts), args[0] + '.mdwn') + if not os.path.exists(pathname): + raise AppException('draft %s does not exist' % args[0]) subprocess.check_call(['gedit', '--new-window', pathname]) def finish_entry(self, args, opts): if not args: raise AppException('Usage: journal-note finish ID') - draft = os.path.join(self.drafts(opts), args[0]) + draft = os.path.join(self.drafts(opts), args[0] + '.mdwn') + if not os.path.exists(draft): + raise AppException('draft %s does not exist' % args[0]) basename = time.strftime('%Y-%m-%d-%H:%M.mdwn') i = 0 while True: -- cgit v1.2.1 From 3d5e54427345f00a9ae3f8e30b697dd0e8929df3 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius <liw@liw.fi> Date: Tue, 7 Dec 2010 10:34:33 +0000 Subject: Run bzr and ikiwiki to add note to vcs and rebuild pages. --- scripts/journal-note | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/scripts/journal-note b/scripts/journal-note index 315a27c..ae1955d 100755 --- a/scripts/journal-note +++ b/scripts/journal-note @@ -125,7 +125,13 @@ class App(object): i += 1 basename = '%s-%d.mdwn' % (time.strftime('%Y-%m-%d-%H:%M:%S-'), i) os.rename(draft, finished) - # FIXME: bzr add + commit, ikiwiki + + src = os.path.join(opts.base, 'src') + subprocess.check_call(['bzr', 'add', finished], cwd=src) + subprocess.check_call(['bzr', 'commit', '-m', 'new note'], cwd=src) + subprocess.check_call(['ikiwiki', '--setup', '../ikiwiki.setup', + '--refresh'], + cwd=src) if __name__ == '__main__': -- cgit v1.2.1 From e682409d2443a8651673ed560fee81e9874f70de Mon Sep 17 00:00:00 2001 From: Lars Wirzenius <liw@liw.fi> Date: Tue, 7 Dec 2010 10:35:22 +0000 Subject: 'new' should run gedit. --- scripts/journal-note | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/scripts/journal-note b/scripts/journal-note index ae1955d..5a41e70 100755 --- a/scripts/journal-note +++ b/scripts/journal-note @@ -67,6 +67,9 @@ class App(object): def drafts(self, opts): return os.path.join(opts.base, 'drafts') + def gedit_file(self, pathname): + subprocess.check_call(['gedit', '--new-window', pathname]) + def new_entry(self, args, opts): if not args: raise AppException('Usage: journal-note new TITLE') @@ -85,6 +88,7 @@ class App(object): f = open(name, 'w') f.write(template % values) f.close() + self.gedit_file(name) def list_entries(self, args, opts): drafts = self.drafts(opts) @@ -108,7 +112,7 @@ class App(object): pathname = os.path.join(self.drafts(opts), args[0] + '.mdwn') if not os.path.exists(pathname): raise AppException('draft %s does not exist' % args[0]) - subprocess.check_call(['gedit', '--new-window', pathname]) + self.gedit_file(pathname) def finish_entry(self, args, opts): if not args: -- cgit v1.2.1 From ea559066a15c5fc67268db2b117e25e1b066b0c2 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius <liw@liw.fi> Date: Tue, 7 Dec 2010 10:36:23 +0000 Subject: Add remove commmand. --- scripts/journal-note | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/scripts/journal-note b/scripts/journal-note index 5a41e70..3cd7b78 100755 --- a/scripts/journal-note +++ b/scripts/journal-note @@ -55,6 +55,7 @@ class App(object): 'new': self.new_entry, 'list': self.list_entries, 'edit': self.edit_entry, + 'remove': self.remove_entry, 'finish': self.finish_entry, } @@ -114,6 +115,12 @@ class App(object): raise AppException('draft %s does not exist' % args[0]) self.gedit_file(pathname) + def remove_entry(self, args, opts): + if not args: + raise AppException('Usage: journal-note remove ID') + pathname = os.path.join(self.drafts(opts), args[0] + '.mdwn') + os.remove(pathname) + def finish_entry(self, args, opts): if not args: raise AppException('Usage: journal-note finish ID') -- cgit v1.2.1 From 726cb96b71b38d052eedf99b40f0aa1d48b5ba65 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius <liw@liw.fi> Date: Tue, 7 Dec 2010 10:38:29 +0000 Subject: Centralize generation of pathname for draft. --- scripts/journal-note | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/scripts/journal-note b/scripts/journal-note index 3cd7b78..ed87a79 100755 --- a/scripts/journal-note +++ b/scripts/journal-note @@ -68,6 +68,9 @@ class App(object): def drafts(self, opts): return os.path.join(opts.base, 'drafts') + def draftname(self, opts, draft_id): + return os.path.join(opts.base, 'drafts', '%s.mdwn' % draft_id) + def gedit_file(self, pathname): subprocess.check_call(['gedit', '--new-window', pathname]) @@ -76,7 +79,7 @@ class App(object): raise AppException('Usage: journal-note new TITLE') for i in range(1000): - name = os.path.join(opts.base, 'drafts', '%d.mdwn' % i) + name = self.draftname(opts, i) if not os.path.exists(name): break else: @@ -110,7 +113,7 @@ class App(object): def edit_entry(self, args, opts): if not args: raise AppException('Usage: journal-note edit ID') - pathname = os.path.join(self.drafts(opts), args[0] + '.mdwn') + pathname = self.draftname(opts, args[0]) if not os.path.exists(pathname): raise AppException('draft %s does not exist' % args[0]) self.gedit_file(pathname) @@ -118,13 +121,13 @@ class App(object): def remove_entry(self, args, opts): if not args: raise AppException('Usage: journal-note remove ID') - pathname = os.path.join(self.drafts(opts), args[0] + '.mdwn') + pathname = self.draftname(opts, args[0]) os.remove(pathname) def finish_entry(self, args, opts): if not args: raise AppException('Usage: journal-note finish ID') - draft = os.path.join(self.drafts(opts), args[0] + '.mdwn') + draft = self.draftname(opts, args[0]) if not os.path.exists(draft): raise AppException('draft %s does not exist' % args[0]) basename = time.strftime('%Y-%m-%d-%H:%M.mdwn') -- cgit v1.2.1