diff options
author | Lars Wirzenius <liw@liw.fi> | 2014-11-22 19:48:46 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2014-11-22 19:48:46 +0200 |
commit | de3b41b647d6bafb92d0f368660c6ca92056d7f4 (patch) | |
tree | a439d32bebe62187995fc0fbd7f37fa585f5defd | |
parent | db41106352188f30ed2b814f22442a6fe63bbb60 (diff) | |
parent | 275f7ebd71e6f0335772496e5904c4e1b52a4e65 (diff) | |
download | jt-de3b41b647d6bafb92d0f368660c6ca92056d7f4.tar.gz |
Merge in more refactoring
-rwxr-xr-x | jt | 149 | ||||
-rw-r--r-- | yarns/yarn.sh | 1 |
2 files changed, 67 insertions, 83 deletions
@@ -83,6 +83,21 @@ class DraftsDirectory(object): 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: + for line in f: + m = re.match( + '\[\[!meta title="(?P<title>.*)("\]\])$', + line) + if m: + title = m.group('title') + break + else: + title = None + return title + return '' + class Command(object): @@ -100,7 +115,6 @@ class NewCommand(Command): raise cliapp.AppException('Usage: journal-note new TITLE') self._app.settings.require('source') - self._app.settings.require('layout') values = { 'title': args[0], @@ -118,7 +132,7 @@ class ListCommand(Command): def run(self, args): drafts_dir = DraftsDirectory(self._app.drafts_dir()) for draft_id, filename in drafts_dir.get_drafts(): - print draft_id, self._app.get_draft_title(filename) or "" + print draft_id, drafts_dir.get_draft_title(draft_id) class EditCommand(Command): @@ -158,21 +172,23 @@ class FinishCommand(Command): def run(self, args): drafts_dir = 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) + title = drafts_dir.get_draft_title(draft_id) + if not title: + raise Exception("%s has no title" % draft_mdwn) + pub_attch = os.path.join( - self._published_dir(), self._published_basename(draft_mdwn)) + self._published_dir(), + self._summarise_title(title)) pub_mdwn = pub_attch + '.mdwn' if os.path.exists(pub_mdwn): raise cliapp.AppException('%s already exists' % pub_mdwn) - if not os.path.exists(self._published_dir()): - os.makedirs(self._published_dir()) - os.rename(draft_mdwn, pub_mdwn) - if os.path.exists(draft_attch): - os.rename(draft_attch, pub_attch) + self._publish_draft(draft_mdwn, draft_attch, pub_mdwn, pub_attch) if self._app.settings['git']: if os.path.exists(pub_attch): @@ -183,29 +199,27 @@ class FinishCommand(Command): self.push_git() def _published_dir(self): - subdirs = { - 'liw': 'notes', - 'ct': 'log/%d' % time.localtime().tm_year, - 'pkb': time.strftime('notes/%Y/%m/%d'), - } - - subdir = subdirs[self._app.settings['layout']] + subdir = time.strftime('notes/%Y/%m/%d') return os.path.join(self._app.settings['source'], subdir) - def _published_basename(self, draft_mdwn): - if self._app.settings['layout'] in ('liw', 'ct'): - basename = time.strftime('%Y-%m-%d-%H:%M:%S') - elif self._app.settings['layout'] == 'pkb': - title = self._app.get_draft_title(draft_mdwn) - if not title: - raise Exception("%s has no title" % draft_mdwn) - basename = self._app.summarise_title(title) - else: - raise Exception( - 'Setting --layout=%s is unknown' % self._app.settings['layout']) - + def _summarise_title(self, title): + basename = '' + acceptable = set(string.ascii_letters + string.digits + '-_') + for c in title.lower(): + if c in acceptable: + basename += c + elif not basename.endswith('_'): + basename += '_' return basename + def _publish_draft(self, draft_mdwn, draft_attch, pub_mdwn, pub_attch): + parent_dir = os.path.dirname(pub_mdwn) + if not os.path.exists(parent_dir): + os.makedirs(parent_dir) + os.rename(draft_mdwn, pub_mdwn) + if os.path.exists(draft_attch): + os.rename(draft_attch, pub_attch) + def _commit_to_git(self, pathnames): cliapp.runcmd( ['git', 'add'] + pathname, @@ -271,12 +285,6 @@ class JournalTool(cliapp.Application): 'use journal source tree in DIR', metavar='DIR') - self.settings.choice( - ['layout'], - ['ct', 'liw', 'pkb'], - 'use journal layout (one of liw, ct, pkb)', - metavar='LAYOUT') - self.settings.boolean( ['git'], 'add entries to git automatically', @@ -301,53 +309,14 @@ class JournalTool(cliapp.Application): '''Create a new journal entry draft.''' NewCommand(self).run(args) - def drafts_dir(self): - return os.path.join(self.settings['source'], 'drafts') - - def edit_file(self, pathname): - safe_pathname = cliapp.shell_quote(pathname) - cmdline = ['sh', '-c', self.settings['editor'] % pathname] - self.runcmd(cmdline, stdin=None, stdout=None, stderr=None) - def cmd_list(self, args): '''List journal entry drafts.''' ListCommand(self).run(args) - def get_draft_title(self, filename): - with open(filename) as f: - for line in f: - m = re.match( - '\[\[!meta title="(?P<title>.*)("\]\])$', - line) - if m: - title = m.group('title') - break - else: - title = None - return title - def cmd_edit(self, args): '''Edit a draft journal entry.''' EditCommand(self).run(args) - def choose_draft(self, drafts_dir, args): - if len(args) == 0: - drafts = list(drafts_dir.get_drafts()) - if len(drafts) == 1: - draft_id, filename = drafts[0] - return draft_id, filename - elif len(drafts) == 0: - raise cliapp.AppException('No drafts to choose from') - else: - raise cliapp.AppException('Cannot choose entry draft automatically') - elif len(args) == 1: - pathname = drafts_dir.get_draft_pathname(args[0]) - if not os.path.exists(pathname): - raise cliapp.AppException('draft %s does not exist' % args[0]) - return args[0], pathname - elif len(args) > 1: - raise cliapp.AppException('Must give at most one draft number') - def cmd_attach(self, args): '''Attach files to a journal entry draft.''' AttachCommand(self).run(args) @@ -360,16 +329,6 @@ class JournalTool(cliapp.Application): '''Publish a draft journal entry.''' FinishCommand(self).run(args) - def summarise_title(self, title): - basename = '' - acceptable = set(string.ascii_letters + string.digits + '-_') - for c in title.lower(): - if c in acceptable: - basename += c - elif not basename.endswith('_'): - basename += '_' - return basename - def cmd_new_person(self, args): '''Create a page to list all notes referring to a person. @@ -379,5 +338,31 @@ class JournalTool(cliapp.Application): NewPersonCommand(self).run(args) + def drafts_dir(self): + return os.path.join(self.settings['source'], 'drafts') + + def edit_file(self, pathname): + safe_pathname = cliapp.shell_quote(pathname) + cmdline = ['sh', '-c', self.settings['editor'] % pathname] + self.runcmd(cmdline, stdin=None, stdout=None, stderr=None) + + def choose_draft(self, drafts_dir, args): + if len(args) == 0: + drafts = list(drafts_dir.get_drafts()) + if len(drafts) == 1: + draft_id, filename = drafts[0] + return draft_id, filename + elif len(drafts) == 0: + raise cliapp.AppException('No drafts to choose from') + else: + raise cliapp.AppException('Cannot choose entry draft automatically') + elif len(args) == 1: + pathname = drafts_dir.get_draft_pathname(args[0]) + if not os.path.exists(pathname): + raise cliapp.AppException('draft %s does not exist' % args[0]) + return args[0], pathname + elif len(args) > 1: + raise cliapp.AppException('Must give at most one draft number') + JournalTool(version=__version__).run() diff --git a/yarns/yarn.sh b/yarns/yarn.sh index 5f3e59b..1f02369 100644 --- a/yarns/yarn.sh +++ b/yarns/yarn.sh @@ -28,7 +28,6 @@ run_jt() "$SRCDIR/jt" --no-default-config \ --config "$DATADIR/source.conf" \ --config "$DATADIR/time.conf" \ - --layout=pkb \ --no-git \ "$@" exit_code="$?" |