diff options
author | Lars Wirzenius <liw@liw.fi> | 2012-02-24 17:39:57 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2012-02-24 17:39:57 +0000 |
commit | 6bc01616ad2fd338c94b80a2cd517998fd7c121b (patch) | |
tree | 0bea48288b8d8e22e957c1678bc15002791ae83e | |
parent | 675a4f1db7e73a18cbf73aa77a77dc230592a3cb (diff) | |
download | liw-automation-6bc01616ad2fd338c94b80a2cd517998fd7c121b.tar.gz |
Choose entry automatically when possible
-rwxr-xr-x | scripts/journal-note | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/scripts/journal-note b/scripts/journal-note index d6de160..ebbb57e 100755 --- a/scripts/journal-note +++ b/scripts/journal-note @@ -96,10 +96,15 @@ class App(object): f.close() self.gedit_file(name) - def list_entries(self, args, opts): + def find_drafts(self, opts): drafts = self.drafts(opts) for name in os.listdir(drafts): if name.endswith('.mdwn'): + yield name[:-len('.mdwn')], name + + def list_entries(self, args, opts): + for draft_id, name in self.find_drafts(opts): + if name.endswith('.mdwn'): f = open(os.path.join(drafts, name)) for line in f: m = re.match('\[\[!meta title="(?P<title>.*)("\]\])$', @@ -110,12 +115,24 @@ class App(object): else: title = 'unknown title' f.close() - print name[:-len('.mdwn')], title + print draft_id, title + + def choose_entry(self, args, opts): + if len(args) == 0: + drafts = list(self.find_drafts(opts)) + if len(drafts) == 1: + draft_id, filename = drafts[0] + return draft_id + else: + raise AppException('Cannot choose entry automatically') + elif len(args) == 1: + return args[0] + elif len(args) > 1: + raise AppException('Must give only one entry') def edit_entry(self, args, opts): - if not args: - raise AppException('Usage: journal-note edit ID') - pathname = self.draftname(opts, args[0]) + draft_id = self.choose_entry(args, opts) + pathname = self.draftname(opts, draft_id) if not os.path.exists(pathname): raise AppException('draft %s does not exist' % args[0]) self.gedit_file(pathname) @@ -139,9 +156,8 @@ class App(object): os.remove(pathname) def finish_entry(self, args, opts): - if not args: - raise AppException('Usage: journal-note finish ID') - draft = self.draftname(opts, args[0]) + draft_id = self.choose_entry(args, opts) + draft = self.draftname(opts, draft_id) if not os.path.exists(draft): raise AppException('draft %s does not exist' % args[0]) basename = time.strftime('%Y-%m-%d-%H:%M.mdwn') |