From c53a50f3a219d3667af0a8636d180c1efc3cd265 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 22 Nov 2014 18:20:56 +0200 Subject: Move command finish to a separate class --- jt | 65 +++++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/jt b/jt index ad02c62..3e78621 100755 --- a/jt +++ b/jt @@ -123,6 +123,40 @@ class RemoveCommand(Command): shutil.rmtree(dirname) +class FinishCommand(Command): + + def run(self, args): + draft_mdwn = self._app.choose_draft(args) + draft_attch, ext = os.path.splitext(draft_mdwn) + assert ext == '.mdwn' + + pub_attch = os.path.join( + self._app.published_dir(), self._app.published_basename(draft_mdwn)) + 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._app.published_dir()): + os.makedirs(self._app.published_dir()) + os.rename(draft_mdwn, pub_mdwn) + if os.path.exists(draft_attch): + os.rename(draft_attch, pub_attch) + + if self._app.settings['git']: + argv = ['git', 'add', pub_mdwn] + if os.path.exists(pub_attch): + argv.append(pub_attch) + cliapp.runcmd(argv, cwd=self._app.settings['source']) + cliapp.runcmd( + ['git', 'commit', '-m', 'Publish log entry'], + cwd=self._app.settings['source']) + if self.settings['push']: + cliapp.runcmd( + ['git', 'push', 'origin', 'HEAD'], + cwd=self._app.settings['source']) + + class JournalTool(cliapp.Application): cmd_synopsis = { @@ -235,36 +269,7 @@ class JournalTool(cliapp.Application): def cmd_finish(self, args): '''Publish a draft journal entry.''' - - draft_mdwn = self.choose_draft(args) - draft_attch, ext = os.path.splitext(draft_mdwn) - assert ext == '.mdwn' - - pub_attch = os.path.join( - self.published_dir(), self.published_basename(draft_mdwn)) - 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) - - if self.settings['git']: - argv = ['git', 'add', pub_mdwn] - if os.path.exists(pub_attch): - argv.append(pub_attch) - cliapp.runcmd(argv, cwd=self.settings['source']) - cliapp.runcmd( - ['git', 'commit', '-m', 'Publish log entry'], - cwd=self.settings['source']) - if self.settings['push']: - cliapp.runcmd( - ['git', 'push', 'origin', 'HEAD'], - cwd=self.settings['source']) + FinishCommand(self).run(args) def published_dir(self): subdirs = { -- cgit v1.2.1