From 339049495dd8a82cc2d5058e805144e244bc7d72 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 22 Nov 2017 14:59:33 +0200 Subject: Add: put back --layout setting Support ct and pkb layouts now. --- jtlib/app.py | 6 ++++++ jtlib/plugins/finish_plugin.py | 17 +++++++++++++++-- jtlib/plugins/new_plugin.py | 1 + 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/jtlib/app.py b/jtlib/app.py index 6ea2e74..81fda8b 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', diff --git a/jtlib/plugins/finish_plugin.py b/jtlib/plugins/finish_plugin.py index e98f59a..c05f88e 100644 --- a/jtlib/plugins/finish_plugin.py +++ b/jtlib/plugins/finish_plugin.py @@ -42,7 +42,7 @@ class FinishCommand(cliapp.Plugin): pub_attch = os.path.join( self._published_dir(), - self._summarise_title(title)) + self._published_basename(title, draft_mdwn),) pub_mdwn = pub_attch + '.mdwn' if os.path.exists(pub_mdwn): @@ -59,12 +59,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 + '-_') diff --git a/jtlib/plugins/new_plugin.py b/jtlib/plugins/new_plugin.py index 25facf8..d704242 100644 --- a/jtlib/plugins/new_plugin.py +++ b/jtlib/plugins/new_plugin.py @@ -52,6 +52,7 @@ class NewCommand(cliapp.Plugin): raise cliapp.AppException('Usage: journal-note new TITLE') self.app.settings.require('source') + self.app.settings.require('layout') topic = self.app.settings['topic'] if topic and not self._topic_page_exists(topic): raise cliapp.AppException('Topic %s does not exist yet' % topic) -- cgit v1.2.1 From db3e6834c6c01b00a6ef7cbfc03c25edc732b695 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 22 Nov 2017 15:16:50 +0200 Subject: Refactor: move layout specific parts to their own classes --- jtlib/__init__.py | 1 + jtlib/app.py | 12 ++++++++++++ jtlib/plugins/finish_plugin.py | 6 ++---- jtlib/plugins/new_plugin.py | 1 - 4 files changed, 15 insertions(+), 5 deletions(-) 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 81fda8b..760b9a4 100644 --- a/jtlib/app.py +++ b/jtlib/app.py @@ -91,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/plugins/finish_plugin.py b/jtlib/plugins/finish_plugin.py index c05f88e..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._published_basename(title, draft_mdwn),) - 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) diff --git a/jtlib/plugins/new_plugin.py b/jtlib/plugins/new_plugin.py index d704242..25facf8 100644 --- a/jtlib/plugins/new_plugin.py +++ b/jtlib/plugins/new_plugin.py @@ -52,7 +52,6 @@ class NewCommand(cliapp.Plugin): raise cliapp.AppException('Usage: journal-note new TITLE') self.app.settings.require('source') - self.app.settings.require('layout') topic = self.app.settings['topic'] if topic and not self._topic_page_exists(topic): raise cliapp.AppException('Topic %s does not exist yet' % topic) -- cgit v1.2.1 From bcc558c425f9c62442386dd145f482dfe86f1b3f Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 22 Nov 2017 15:17:28 +0200 Subject: Add: update NEWS --- NEWS | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS b/NEWS index 82bce74..61d59ed 100644 --- a/NEWS +++ b/NEWS @@ -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 ---------------------------------- -- cgit v1.2.1 From 542c215ac8ca1b50488baefae133c72b86f2a0b5 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 22 Nov 2017 15:17:45 +0200 Subject: Add: missing layout.py --- jtlib/layout.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 jtlib/layout.py 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 . + + +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), -- cgit v1.2.1