From 1e30a6899e883dd458ddc1480fc97808d3f40eb6 Mon Sep 17 00:00:00 2001 From: jgrizou Date: Thu, 6 Jun 2013 11:31:43 +0200 Subject: [PATCH] when init -> update config file with papers-directory --- papers/commands/init_cmd.py | 13 +++++++------ papers/configs.py | 20 ++++++++++++++++---- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/papers/commands/init_cmd.py b/papers/commands/init_cmd.py index dfe2a71..1d1b717 100644 --- a/papers/commands/init_cmd.py +++ b/papers/commands/init_cmd.py @@ -8,21 +8,22 @@ from .. import configs def parser(subparsers, config): parser = subparsers.add_parser('init', - help="initialize the papers directory") + help="initialize the papers directory") parser.add_argument('-p', '--path', default=None, - help='path to papers directory (if none, ~/.papers is used)') + help='path to papers directory (if none, ~/.papers is used)') parser.add_argument('-d', '--doc-dir', default=None, - help=('path to document directory ' - '(if none, documents are stored in the same directory)')) + help=('path to document directory ' + '(if none, documents are stored in the same directory)')) return parser def command(config, ui, path, doc_dir): """Create a .papers directory""" if path is None: - papersdir = configs.DEFAULT_PAPER_PATH + papersdir = configs.get('papers', 'papers-directory') else: papersdir = os.path.join(os.getcwd(), path) + configs.add_and_write_option('papers', 'papers-directory', papersdir) if not os.path.exists(papersdir): ui.print_('Initializing papers in {}.'.format( ui.colored(papersdir, 'filepath'))) @@ -31,5 +32,5 @@ def command(config, ui, path, doc_dir): repo.save() # Saves empty repository description else: ui.error('papers already present in {}.'.format( - ui.colored(papersdir, 'filepath'))) + ui.colored(papersdir, 'filepath'))) exit(-1) diff --git a/papers/configs.py b/papers/configs.py index 29d67f2..5ff2663 100644 --- a/papers/configs.py +++ b/papers/configs.py @@ -1,7 +1,7 @@ import os import ConfigParser - +CONFIG_PATH = os.path.expanduser('~/.papersrc') DEFAULT_PAPERS_DIRECTORY = os.path.expanduser('~/.papers') DEFAULT_OPEN_CMD = 'open' try: @@ -20,16 +20,28 @@ CONFIG = ConfigParser.SafeConfigParser({ 'edit-cmd': DEFAULT_EDIT_CMD, 'import-copy': DEFAULT_IMPORT_COPY, 'import-move': DEFAULT_IMPORT_MOVE, - 'color': DEFAULT_COLOR, - }) + 'color': DEFAULT_COLOR}) CONFIG.add_section('papers') def read_config(): - CONFIG.read(os.path.expanduser('~/.papersrc')) + CONFIG.read(CONFIG_PATH) return CONFIG +def add_and_write_option(section, option, value): + cfg = ConfigParser.ConfigParser() + cfg.read(CONFIG_PATH) + if not cfg.has_section(section): + cfg.add_section(section) + + cfg.set(section, option, value) + + f = open(CONFIG_PATH, 'w') + cfg.write(f) + f.close() + + def get_boolean(value, default): value = str(value).lower() if value in ('yes', 'true', 't', 'y', '1'):