From e8d1f422888d1ea4d703b446cf732331d0fff116 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 3 Oct 2015 09:48:00 +0300 Subject: Move app class to jtlib, commands to plugins --- jtlib/app.py | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 jtlib/app.py (limited to 'jtlib/app.py') diff --git a/jtlib/app.py b/jtlib/app.py new file mode 100644 index 0000000..a560c26 --- /dev/null +++ b/jtlib/app.py @@ -0,0 +1,95 @@ +# Copyright 2010-2015 Lars Wirzenius +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +import cliapp +import os +import time + +import jtlib + + +class JournalTool(cliapp.Application): + + def add_settings(self): + self.settings.string( + ['source'], + 'use journal source tree in DIR', + metavar='DIR') + + self.settings.boolean( + ['git'], + 'add entries to git automatically', + default=True) + + self.settings.string( + ['editor'], + 'editor to launch for journal entries. Must include %s to ' + 'indicate where the filename goes', + default='sensible-editor %s') + + self.settings.boolean( + ['push'], + 'push finished articles with git?') + + self.settings.string( + ['topic'], + 'new entry belongs to TOPIC', + metavar='TOPIC') + + self.settings.string( + ['new-note-template'], + 'use FILE as the template for new journal notes', + metavar='FILE') + + self.settings.string( + ['pretend-time'], + 'pretend that the time is NOW (form: YYYY-MM-DD HH:MM:DD form)', + metavar='NOW') + + def process_args(self, args): + if self.settings['pretend-time']: + self.now_tuple = time.strptime( + self.settings['pretend-time'], '%Y-%m-%d %H:%M:%S') + else: + self.now_tuple = time.localtime() + cliapp.Application.process_args(self, 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'] % safe_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') -- cgit v1.2.1