From 7b4ba0c385deb01dae857126666706389dfa08c5 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 3 Oct 2015 09:25:20 +0300 Subject: Move DraftsDirectory into jtlib --- jt | 71 +++++-------------------------------------------- jtlib/__init__.py | 3 +++ jtlib/draftsdir.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 65 deletions(-) create mode 100644 jtlib/draftsdir.py diff --git a/jt b/jt index b9a0580..4b64c87 100755 --- a/jt +++ b/jt @@ -17,7 +17,6 @@ import cliapp import os -import re import shutil import string import time @@ -25,64 +24,6 @@ import time import jtlib -class DraftsDirectory(object): - - def __init__(self, dirname): - self.dirname = dirname - - def create_if_missing(self): - if not os.path.exists(self.dirname): - os.mkdir(self.dirname) - - def get_draft_pathname(self, draft_id): - return os.path.join(self.dirname, '%s.mdwn' % draft_id) - - def get_draft_attachments_dirname(self, draft_id): - return os.path.join(self.dirname, '%s' % draft_id) - - def create_draft(self, content): - draft_id = self._pick_available_draft_id() - pathname = self.get_draft_pathname(draft_id) - with open(pathname, 'w') as f: - f.write(content) - return draft_id - - def _pick_available_draft_id(self): - for i in range(1000): - pathname = self.get_draft_pathname(i) - if not os.path.exists(pathname): - return i - raise cliapp.AppException('ERROR: too many existing drafts') - - def get_drafts(self): - for basename in os.listdir(self.dirname): - # .# is what Emacs autosave files start with. - if basename.endswith('.mdwn') and not basename.startswith('.#'): - suffixless = basename[:-len('.mdwn')] - pathname = os.path.join(self.dirname, basename) - yield suffixless, pathname - - def remove_draft(self, draft_id): - filename = self.get_draft_pathname(draft_id) - os.remove(filename) - - dirname = self.get_draft_attachments_dirname(draft_id) - if os.path.exists(dirname): - shutil.rmtree(dirname) - - def get_draft_title(self, draft_id): - pathname = self.get_draft_pathname(draft_id) - with open(pathname) as f: - return self._get_title_from_open_file(f) - - def _get_title_from_open_file(self, f): - for line in f: - m = re.match(r'\[\[!meta title="(?P.*)("\]\])$', line) - if m: - return m.group('title') - return None - - def commit_to_git(source_dir, pathnames): cliapp.runcmd(['git', 'add'] + pathnames, cwd=source_dir) cliapp.runcmd(['git', 'commit', '-m', 'Publish log entry'], cwd=source_dir) @@ -121,7 +62,7 @@ class NewCommand(Command): 'topiclink': self._get_topic_link(topic), } - drafts_dir = DraftsDirectory(self._app.drafts_dir()) + drafts_dir = jtlib.DraftsDirectory(self._app.drafts_dir()) drafts_dir.create_if_missing() draft_id = drafts_dir.create_draft( self._get_new_note_template() % values) @@ -158,7 +99,7 @@ class NewCommand(Command): class ListCommand(Command): def run(self, args): - drafts_dir = DraftsDirectory(self._app.drafts_dir()) + drafts_dir = jtlib.DraftsDirectory(self._app.drafts_dir()) for draft_id, _ in drafts_dir.get_drafts(): print draft_id, drafts_dir.get_draft_title(draft_id) @@ -168,7 +109,7 @@ class EditCommand(Command): def run(self, args): if len(args) > 1: raise cliapp.AppException('Must be given at most one draft ID') - drafts_dir = DraftsDirectory(self._app.drafts_dir()) + drafts_dir = jtlib.DraftsDirectory(self._app.drafts_dir()) _, pathname = self._app.choose_draft(drafts_dir, args) self._app.edit_file(pathname) @@ -179,7 +120,7 @@ class AttachCommand(Command): if len(args) < 2: raise cliapp.AppException('Usage: journal-note attach ID file...') - drafts_dir = DraftsDirectory(self._app.drafts_dir()) + drafts_dir = jtlib.DraftsDirectory(self._app.drafts_dir()) dirname = drafts_dir.get_draft_attachments_dirname(args[0]) if not os.path.exists(dirname): os.mkdir(dirname) @@ -192,14 +133,14 @@ class RemoveCommand(Command): def run(self, args): if not args: raise cliapp.AppException('Usage: journal-note remove ID') - drafts_dir = DraftsDirectory(self._app.drafts_dir()) + drafts_dir = jtlib.DraftsDirectory(self._app.drafts_dir()) drafts_dir.remove_draft(args[0]) class FinishCommand(Command): def run(self, args): - drafts_dir = DraftsDirectory(self._app.drafts_dir()) + drafts_dir = jtlib.DraftsDirectory(self._app.drafts_dir()) draft_id, draft_mdwn = self._app.choose_draft(drafts_dir, args) draft_attch = drafts_dir.get_draft_attachments_dirname(draft_id) diff --git a/jtlib/__init__.py b/jtlib/__init__.py index a09c65f..7c83767 100644 --- a/jtlib/__init__.py +++ b/jtlib/__init__.py @@ -17,3 +17,6 @@ from .version import __version__ +from .draftsdir import DraftsDirectory + +__all__ = locals() diff --git a/jtlib/draftsdir.py b/jtlib/draftsdir.py new file mode 100644 index 0000000..59eb47d --- /dev/null +++ b/jtlib/draftsdir.py @@ -0,0 +1,78 @@ +# 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/>. +# +# =*= License: GPL-3+ =*= + + +import os +import re + + +class DraftsDirectory(object): + + def __init__(self, dirname): + self.dirname = dirname + + def create_if_missing(self): + if not os.path.exists(self.dirname): + os.mkdir(self.dirname) + + def get_draft_pathname(self, draft_id): + return os.path.join(self.dirname, '%s.mdwn' % draft_id) + + def get_draft_attachments_dirname(self, draft_id): + return os.path.join(self.dirname, '%s' % draft_id) + + def create_draft(self, content): + draft_id = self._pick_available_draft_id() + pathname = self.get_draft_pathname(draft_id) + with open(pathname, 'w') as f: + f.write(content) + return draft_id + + def _pick_available_draft_id(self): + for i in range(1000): + pathname = self.get_draft_pathname(i) + if not os.path.exists(pathname): + return i + raise cliapp.AppException('ERROR: too many existing drafts') + + def get_drafts(self): + for basename in os.listdir(self.dirname): + # .# is what Emacs autosave files start with. + if basename.endswith('.mdwn') and not basename.startswith('.#'): + suffixless = basename[:-len('.mdwn')] + pathname = os.path.join(self.dirname, basename) + yield suffixless, pathname + + def remove_draft(self, draft_id): + filename = self.get_draft_pathname(draft_id) + os.remove(filename) + + dirname = self.get_draft_attachments_dirname(draft_id) + if os.path.exists(dirname): + shutil.rmtree(dirname) + + def get_draft_title(self, draft_id): + pathname = self.get_draft_pathname(draft_id) + with open(pathname) as f: + return self._get_title_from_open_file(f) + + def _get_title_from_open_file(self, f): + for line in f: + m = re.match(r'\[\[!meta title="(?P<title>.*)("\]\])$', line) + if m: + return m.group('title') + return None -- cgit v1.2.1