diff options
Diffstat (limited to 'jt')
-rwxr-xr-x | jt | 71 |
1 files changed, 6 insertions, 65 deletions
@@ -17,7 +17,6 @@ import cliapp import os -import re import shutil import string import time @@ -25,64 +24,6 @@ import time import jtlib -class DraftsDirectory(object): - - def __init__(self, dirname): - self.dirname = dirname - - def create_if_missing(self): - if not os.path.exists(self.dirname): - os.mkdir(self.dirname) - - def get_draft_pathname(self, draft_id): - return os.path.join(self.dirname, '%s.mdwn' % draft_id) - - def get_draft_attachments_dirname(self, draft_id): - return os.path.join(self.dirname, '%s' % draft_id) - - def create_draft(self, content): - draft_id = self._pick_available_draft_id() - pathname = self.get_draft_pathname(draft_id) - with open(pathname, 'w') as f: - f.write(content) - return draft_id - - def _pick_available_draft_id(self): - for i in range(1000): - pathname = self.get_draft_pathname(i) - if not os.path.exists(pathname): - return i - raise cliapp.AppException('ERROR: too many existing drafts') - - def get_drafts(self): - for basename in os.listdir(self.dirname): - # .# is what Emacs autosave files start with. - if basename.endswith('.mdwn') and not basename.startswith('.#'): - suffixless = basename[:-len('.mdwn')] - pathname = os.path.join(self.dirname, basename) - yield suffixless, pathname - - def remove_draft(self, draft_id): - filename = self.get_draft_pathname(draft_id) - os.remove(filename) - - dirname = self.get_draft_attachments_dirname(draft_id) - if os.path.exists(dirname): - shutil.rmtree(dirname) - - def get_draft_title(self, draft_id): - pathname = self.get_draft_pathname(draft_id) - with open(pathname) as f: - return self._get_title_from_open_file(f) - - def _get_title_from_open_file(self, f): - for line in f: - m = re.match(r'\[\[!meta title="(?P<title>.*)("\]\])$', line) - if m: - return m.group('title') - return None - - def commit_to_git(source_dir, pathnames): cliapp.runcmd(['git', 'add'] + pathnames, cwd=source_dir) cliapp.runcmd(['git', 'commit', '-m', 'Publish log entry'], cwd=source_dir) @@ -121,7 +62,7 @@ class NewCommand(Command): 'topiclink': self._get_topic_link(topic), } - drafts_dir = DraftsDirectory(self._app.drafts_dir()) + drafts_dir = jtlib.DraftsDirectory(self._app.drafts_dir()) drafts_dir.create_if_missing() draft_id = drafts_dir.create_draft( self._get_new_note_template() % values) @@ -158,7 +99,7 @@ class NewCommand(Command): class ListCommand(Command): def run(self, args): - drafts_dir = DraftsDirectory(self._app.drafts_dir()) + drafts_dir = jtlib.DraftsDirectory(self._app.drafts_dir()) for draft_id, _ in drafts_dir.get_drafts(): print draft_id, drafts_dir.get_draft_title(draft_id) @@ -168,7 +109,7 @@ class EditCommand(Command): def run(self, args): if len(args) > 1: raise cliapp.AppException('Must be given at most one draft ID') - drafts_dir = DraftsDirectory(self._app.drafts_dir()) + drafts_dir = jtlib.DraftsDirectory(self._app.drafts_dir()) _, pathname = self._app.choose_draft(drafts_dir, args) self._app.edit_file(pathname) @@ -179,7 +120,7 @@ class AttachCommand(Command): if len(args) < 2: raise cliapp.AppException('Usage: journal-note attach ID file...') - drafts_dir = DraftsDirectory(self._app.drafts_dir()) + drafts_dir = jtlib.DraftsDirectory(self._app.drafts_dir()) dirname = drafts_dir.get_draft_attachments_dirname(args[0]) if not os.path.exists(dirname): os.mkdir(dirname) @@ -192,14 +133,14 @@ class RemoveCommand(Command): def run(self, args): if not args: raise cliapp.AppException('Usage: journal-note remove ID') - drafts_dir = DraftsDirectory(self._app.drafts_dir()) + drafts_dir = jtlib.DraftsDirectory(self._app.drafts_dir()) drafts_dir.remove_draft(args[0]) class FinishCommand(Command): def run(self, args): - drafts_dir = DraftsDirectory(self._app.drafts_dir()) + drafts_dir = jtlib.DraftsDirectory(self._app.drafts_dir()) draft_id, draft_mdwn = self._app.choose_draft(drafts_dir, args) draft_attch = drafts_dir.get_draft_attachments_dirname(draft_id) |