From 9043e7a012c0b01a993e04831180005ff36730d6 Mon Sep 17 00:00:00 2001 From: Elrond Date: Sun, 23 Oct 2011 12:47:25 +0200 Subject: [PATCH] Refactor gmg's cf option into a function Many (all?) gmg subcommands take a -cf option to change the used config file. This options used to be created in each subcommand's parse_setup. Add a helper function and use it around. --- mediagoblin/gmg_commands/import_export.py | 5 ++--- mediagoblin/gmg_commands/migrate.py | 5 ++--- mediagoblin/gmg_commands/shell.py | 5 ++--- mediagoblin/gmg_commands/users.py | 13 ++++--------- mediagoblin/gmg_commands/util.py | 9 +++++++++ 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/mediagoblin/gmg_commands/import_export.py b/mediagoblin/gmg_commands/import_export.py index 05edbfc8..5d39304a 100644 --- a/mediagoblin/gmg_commands/import_export.py +++ b/mediagoblin/gmg_commands/import_export.py @@ -19,6 +19,7 @@ from mediagoblin.db.open import setup_connection_and_db_from_config from mediagoblin.init.config import read_mediagoblin_config from mediagoblin.storage.filestorage import BasicFileStorage from mediagoblin.init import setup_storage, setup_global_and_app_config +from mediagoblin.gmg_commands.util import option_add_conffile import shutil import tarfile @@ -39,9 +40,7 @@ def import_export_parse_setup(subparser): # TODO: Add default subparser.add_argument( 'tar_file') - subparser.add_argument( - '-cf', '--conf_file', default='mediagoblin.ini', - help='Config file used to set up environment') + option_add_conffile(subparser) subparser.add_argument( '--mongodump_path', default='mongodump', help='mongodump binary') diff --git a/mediagoblin/gmg_commands/migrate.py b/mediagoblin/gmg_commands/migrate.py index 1a597188..0871a171 100644 --- a/mediagoblin/gmg_commands/migrate.py +++ b/mediagoblin/gmg_commands/migrate.py @@ -16,6 +16,7 @@ import sys +from mediagoblin.gmg_commands.util import option_add_conffile from mediagoblin.db import util as db_util from mediagoblin.db.open import setup_connection_and_db_from_config from mediagoblin.init.config import read_mediagoblin_config @@ -25,9 +26,7 @@ from mediagoblin.db import migrations def migrate_parser_setup(subparser): - subparser.add_argument( - '-cf', '--conf_file', default='mediagoblin.ini', - help="Config file used to set up environment") + option_add_conffile(subparser) def _print_started_migration(migration_number, migration_func): diff --git a/mediagoblin/gmg_commands/shell.py b/mediagoblin/gmg_commands/shell.py index 2a380c7b..408028d0 100644 --- a/mediagoblin/gmg_commands/shell.py +++ b/mediagoblin/gmg_commands/shell.py @@ -19,12 +19,11 @@ import code from mediagoblin import mg_globals from mediagoblin.gmg_commands import util as commands_util +from mediagoblin.gmg_commands.util import option_add_conffile def shell_parser_setup(subparser): - subparser.add_argument( - '-cf', '--conf_file', default='mediagoblin.ini', - help="Config file used to set up environment") + option_add_conffile(subparser) SHELL_BANNER = """\ diff --git a/mediagoblin/gmg_commands/users.py b/mediagoblin/gmg_commands/users.py index 5421907d..f6b03bf1 100644 --- a/mediagoblin/gmg_commands/users.py +++ b/mediagoblin/gmg_commands/users.py @@ -15,6 +15,7 @@ # along with this program. If not, see . from mediagoblin.gmg_commands import util as commands_util +from mediagoblin.gmg_commands.util import option_add_conffile from mediagoblin.auth import lib as auth_lib from mediagoblin import mg_globals @@ -29,9 +30,7 @@ def adduser_parser_setup(subparser): subparser.add_argument( 'email', help="Email to recieve notifications") - subparser.add_argument( - '-cf', '--conf_file', default='mediagoblin.ini', - help="Config file used to set up environment") + option_add_conffile(subparser) def adduser(args): @@ -64,9 +63,7 @@ def makeadmin_parser_setup(subparser): subparser.add_argument( 'username', help="Username to give admin level") - subparser.add_argument( - '-cf', '--conf_file', default='mediagoblin.ini', - help="Config file used to set up environment") + option_add_conffile(subparser) def makeadmin(args): @@ -90,9 +87,7 @@ def changepw_parser_setup(subparser): subparser.add_argument( 'password', help="Your NEW supersecret word to login") - subparser.add_argument( - '-cf', '--conf_file', default='mediagoblin.ini', - help="Config file used to set up environment") + option_add_conffile(subparser) def changepw(args): diff --git a/mediagoblin/gmg_commands/util.py b/mediagoblin/gmg_commands/util.py index 168a0760..02febd2c 100644 --- a/mediagoblin/gmg_commands/util.py +++ b/mediagoblin/gmg_commands/util.py @@ -25,3 +25,12 @@ def setup_app(args): mgoblin_app = app.MediaGoblinApp(args.conf_file) return mgoblin_app + + +def option_add_conffile(subparser): + """ + Add the -cf option to a subparser + """ + subparser.add_argument( + '-cf', '--conf_file', default='mediagoblin.ini', + help="Config file used to set up environment")