diff options
author | Lars Wirzenius <liw@liw.fi> | 2017-11-22 15:19:01 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2017-11-22 15:19:01 +0200 |
commit | 5d694ac92020f247a2b76550e628639fcb36c183 (patch) | |
tree | 46b899c0aa8e07ff379daea14eafcada49f45ddb | |
parent | af8c346af93e153e1a553d1edc10ac3dc444be09 (diff) | |
parent | 542c215ac8ca1b50488baefae133c72b86f2a0b5 (diff) | |
download | jt-5d694ac92020f247a2b76550e628639fcb36c183.tar.gz |
Merge: add --layout
-rw-r--r-- | NEWS | 1 | ||||
-rw-r--r-- | jtlib/__init__.py | 1 | ||||
-rw-r--r-- | jtlib/app.py | 18 | ||||
-rw-r--r-- | jtlib/layout.py | 70 | ||||
-rw-r--r-- | jtlib/plugins/finish_plugin.py | 21 |
5 files changed, 106 insertions, 5 deletions
@@ -4,6 +4,7 @@ NEWS for jt Version 0.19+git, not yet released ---------------------------------- +* The `--layout` setting is back. Version 0.19, released 2017-05-13 ---------------------------------- diff --git a/jtlib/__init__.py b/jtlib/__init__.py index 42df5bb..de66966 100644 --- a/jtlib/__init__.py +++ b/jtlib/__init__.py @@ -19,6 +19,7 @@ from .version import __version__ from .draftsdir import DraftsDirectory from .git import commit_to_git, pull_rebase_git, push_git +from .layout import PkbLayout, CtLayout from .app import JournalTool __all__ = locals() diff --git a/jtlib/app.py b/jtlib/app.py index 6ea2e74..760b9a4 100644 --- a/jtlib/app.py +++ b/jtlib/app.py @@ -32,6 +32,12 @@ class JournalTool(cliapp.Application): 'in addition to [config]', metavar='PROFILE') + self.settings.choice( + ['layout'], + ['pkb', 'ct'], + 'use journal layout (one of pkb, ct)', + metavar='LAYOUT') + self.settings.string( ['source'], 'use journal source tree in DIR', @@ -85,8 +91,20 @@ class JournalTool(cliapp.Application): self.settings['pretend-time'], '%Y-%m-%d %H:%M:%S') else: self.now_tuple = time.localtime() + self.layout = self.create_layout() cliapp.Application.process_args(self, args) + def create_layout(self): + layouts = { + 'pkb': jtlib.PkbLayout(), + 'ct': jtlib.CtLayout(), + } + + layout = layouts[self.settings['layout']] + layout.set_settings(self.settings) + layout.set_time_tuple(self.now_tuple) + return layout + def merge_profile(self): profile = self.settings['profile'] if profile: diff --git a/jtlib/layout.py b/jtlib/layout.py new file mode 100644 index 0000000..4eccee0 --- /dev/null +++ b/jtlib/layout.py @@ -0,0 +1,70 @@ +# Copyright 2017 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 os +import string +import time + + +class LayoutBase(object): + + def __init__(self): + self._settigs = None + self._time = None + + def set_settings(self, settings): + self._settings = settings + + def set_time_tuple(self, tuple): + self._time = tuple + + def get_filename(self, title): + return os.path.join( + self._settings['source'], + self._settings['notes-dir'], + self.subdir(), + self.basename(title)) + + def subdir(self): + raise NotImplementedError() + + def basename(self, title): + raise NotImplementedError() + + +class PkbLayout(LayoutBase): + + def subdir(self): + return time.strftime('%Y/%m/%d', self._time) + + def basename(self, title): + basename = '' + acceptable = set(string.ascii_letters + string.digits + '-_') + for c in title.lower(): + if c in acceptable: + basename += c + elif not basename.endswith('_'): + basename += '_' + return basename.rstrip('_') or 'untitled' + + +class CtLayout(LayoutBase): + + def subdir(self): + return '%d' % self.app.now_tuple.tm_year, + + def basename(self, title): + return time.strftime('%Y-%m-%d-%H:%M:%S', self.app.now_tuple), diff --git a/jtlib/plugins/finish_plugin.py b/jtlib/plugins/finish_plugin.py index e98f59a..1336ea0 100644 --- a/jtlib/plugins/finish_plugin.py +++ b/jtlib/plugins/finish_plugin.py @@ -40,10 +40,8 @@ class FinishCommand(cliapp.Plugin): if not title: raise Exception("%s has no title" % draft_mdwn) - pub_attch = os.path.join( - self._published_dir(), - self._summarise_title(title)) - pub_mdwn = pub_attch + '.mdwn' + pub_attch = self.app.layout.get_filename(title) + pub_mdwn = self.app.layout.get_filename(title) + '.mdwn' if os.path.exists(pub_mdwn): raise cliapp.AppException('%s already exists' % pub_mdwn) @@ -59,12 +57,25 @@ class FinishCommand(cliapp.Plugin): self._push_git() def _published_dir(self): - subdir = time.strftime('%Y/%m/%d', self.app.now_tuple) + subdirs = { + 'ct': '%d' % self.app.now_tuple.tm_year, + 'pkb': time.strftime('%Y/%m/%d', self.app.now_tuple), + } + + subdir = subdirs[self.app.settings['layout']] return os.path.join( self.app.settings['source'], self.app.settings['notes-dir'], subdir) + def _published_basename(self, title, draft_mdwn): + layout = self.app.settings['layout'] + basenames = { + 'ct': time.strftime('%Y-%m-%d-%H:%M:%S', self.app.now_tuple), + 'pkb': self._summarise_title(title), + } + return basenames[layout] + def _summarise_title(self, title): basename = '' acceptable = set(string.ascii_letters + string.digits + '-_') |