diff options
author | Lars Wirzenius <liw@liw.fi> | 2010-12-07 10:20:41 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2010-12-07 10:20:41 +0000 |
commit | 6b9738fb93cedcf44056466792876f3fd5746d05 (patch) | |
tree | 2a97cde2d90ecdc6827181b5d189f6ff5f0510be | |
parent | 792946d92e579ff73f585b997ff5686c822859a7 (diff) | |
download | liw-automation-6b9738fb93cedcf44056466792876f3fd5746d05.tar.gz |
Rewrite in Python for hackability.
-rwxr-xr-x | scripts/journal-note | 161 |
1 files 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 <http://www.gnu.org/licenses/>. -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) + |