diff options
Diffstat (limited to 'jt')
-rwxr-xr-x | jt | 64 |
1 files changed, 39 insertions, 25 deletions
@@ -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') |