From 225ad8daf0ea7d05f557fc5e228fac4437d1d180 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 17 Oct 2015 12:22:48 +0300 Subject: Add --profile option --- NEWS | 7 +++++++ jtlib/app.py | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/NEWS b/NEWS index cc587a2..4277c00 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,13 @@ NEWS for jt =========== +Version 0.10, released UNRELEASED +-------------------------------- + +* Added the `--profile` (`-p`) option to select a configuration file + section to use, in addition to the default `[config]`. This allows + the user to have, say, personal and work journals more easily. + Version 0.9, released 2015-10-03 -------------------------------- diff --git a/jtlib/app.py b/jtlib/app.py index 6e33646..5293e91 100644 --- a/jtlib/app.py +++ b/jtlib/app.py @@ -26,6 +26,12 @@ import jtlib class JournalTool(cliapp.Application): def add_settings(self): + self.settings.string( + ['profile', 'p'], + 'also use section PROFILE in configuration files, ' + 'in addition to [config]', + metavar='PROFILE') + self.settings.string( ['source'], 'use journal source tree in DIR', @@ -67,6 +73,7 @@ class JournalTool(cliapp.Application): metavar='NOW') def process_args(self, args): + self.merge_profile() if self.settings['pretend-time']: self.now_tuple = time.strptime( self.settings['pretend-time'], '%Y-%m-%d %H:%M:%S') @@ -74,6 +81,19 @@ class JournalTool(cliapp.Application): self.now_tuple = time.localtime() cliapp.Application.process_args(self, args) + def merge_profile(self): + profile = self.settings['profile'] + if profile: + cp = self.settings.as_cp() + if profile not in cp.sections(): + raise cliapp.AppException('Unknown profile %s' % profile) + for key in cp.options(profile): + if key not in self.settings: + raise cliapp.AppException( + 'Profile %s uses unknown setting %s' % + (profile, key)) + self.settings[key] = cp.get(profile, key) + def drafts_dir(self): return os.path.join(self.settings['source'], 'drafts') -- cgit v1.2.1