diff options
author | Lars Wirzenius <liw@liw.fi> | 2014-11-22 18:15:31 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2014-11-22 18:15:31 +0200 |
commit | a6eb8b13949dcdb85cb176e354f711de9600d400 (patch) | |
tree | ffe53924fc665a88f1ee9f4f5276f4503558bfb5 | |
parent | fe0a02e3eccc93eb5547154bd98089f823429642 (diff) | |
download | jt-a6eb8b13949dcdb85cb176e354f711de9600d400.tar.gz |
Move command new to a separate class
-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') |