summaryrefslogtreecommitdiff
path: root/jtlib/app.py
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2015-10-03 09:48:00 +0300
committerLars Wirzenius <liw@liw.fi>2015-10-03 10:24:46 +0300
commite8d1f422888d1ea4d703b446cf732331d0fff116 (patch)
tree88b50a1016d5f0836fcfd32ce1cea0a2674e60fa /jtlib/app.py
parentb3855d34fcd7ac7b2819d04a400f841d3091790a (diff)
downloadjt-e8d1f422888d1ea4d703b446cf732331d0fff116.tar.gz
Move app class to jtlib, commands to plugins
Diffstat (limited to 'jtlib/app.py')
-rw-r--r--jtlib/app.py95
1 files changed, 95 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>.
+
+
+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')