From a6eb8b13949dcdb85cb176e354f711de9600d400 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 22 Nov 2014 18:15:31 +0200 Subject: Move command new to a separate class --- jt | 64 +++++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/jt b/jt index c784411..9540d92 100755 --- a/jt +++ b/jt @@ -39,6 +39,44 @@ template = '''\ ''' +class Command(object): + + def __init__(self, app): + self._app = app + + def run(self, args): + raise NotImplementedError() + + +class NewCommand(Command): + + def run(self, args): + if not args: + raise cliapp.AppException('Usage: journal-note new TITLE') + + self._app.settings.require('source') + self._app.settings.require('layout') + + if not os.path.exists(self._app.drafts_dir()): + os.mkdir(self._app.drafts_dir()) + + for i in range(1000): + name = self._app.draft_name(i) + if not os.path.exists(name): + break + else: + raise cliapp.AppException('ERROR: too many existing drafts') + + values = { + 'title': args[0], + 'date': time.strftime('%Y-%m-%d %H:%M') + } + f = open(name, 'w') + f.write(template % values) + f.close() + self._app.edit_file(name) + + class JournalTool(cliapp.Application): cmd_synopsis = { @@ -84,31 +122,7 @@ class JournalTool(cliapp.Application): def cmd_new(self, args): '''Create a new journal entry draft.''' - - if not args: - raise cliapp.AppException('Usage: journal-note new TITLE') - - self.settings.require('source') - self.settings.require('layout') - - if not os.path.exists(self.drafts_dir()): - os.mkdir(self.drafts_dir()) - - for i in range(1000): - name = self.draft_name(i) - if not os.path.exists(name): - break - else: - raise cliapp.AppException('ERROR: too many existing drafts') - - values = { - 'title': args[0], - 'date': time.strftime('%Y-%m-%d %H:%M') - } - f = open(name, 'w') - f.write(template % values) - f.close() - self.edit_file(name) + NewCommand(self).run(args) def drafts_dir(self): return os.path.join(self.settings['source'], 'drafts') -- cgit v1.2.1