Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Break most of the configuration logic out into configure_app function

  • Loading branch information...
commit bf9a90ea823bd5319870b2141f02c44567044bfb 1 parent 79a4b79
David Cramer authored
Showing with 48 additions and 33 deletions.
  1. +48 −33 logan/runner.py
81 logan/runner.py
View
@@ -46,9 +46,9 @@ def parse_args(args):
return (args[:index], args[index], args[(index + 1):])
-def run_app(project=None, default_config_path=None, default_settings=None,
+def configure_app(config_path=None, project=None, default_config_path=None, default_settings=None,
settings_initializer=None, settings_envvar=None, initializer=None,
- allow_extras=True, config_module_name=None):
+ allow_extras=True, config_module_name=None, runner_name=None):
"""
:param project: should represent the canonical name for the project, generally
the same name it assigned in distutils.
@@ -60,19 +60,6 @@ def run_app(project=None, default_config_path=None, default_settings=None,
is executed. It is passed a dictionary of various configuration attributes.
"""
- sys_args = sys.argv
-
- # The established command for running this program
- runner_name = os.path.basename(sys_args[0])
-
- args, command, command_args = parse_args(sys_args[1:])
-
- if not command:
- print "usage: %s [--config=/path/to/settings.py] [command] [options]" % runner_name
- sys.exit(1)
-
- parser = OptionParser()
-
project_filename = sanitize_name(project)
if default_config_path is None:
@@ -90,10 +77,54 @@ def run_app(project=None, default_config_path=None, default_settings=None,
else:
default_config_path = os.path.normpath(os.path.abspath(os.path.expanduser(default_config_path)))
+ if not config_path:
+ config_path = default_config_path
+
+ config_path = os.path.expanduser(config_path)
+
+ if not os.path.exists(config_path):
+ if runner_name:
+ raise ValueError("Configuration file does not exist. Use '%s init' to initialize the file." % (runner_name,))
+ raise ValueError("Configuration file does not exist at %r" % (config_path,))
+
+ os.environ['DJANGO_SETTINGS_MODULE'] = config_module_name
+
+ def settings_callback(settings):
+ if initializer is None:
+ return
+
+ initializer({
+ 'project': project,
+ 'config_path': config_path,
+ 'settings': settings,
+ })
+
+ importer.install(config_module_name, config_path, default_settings,
+ allow_extras=allow_extras, callback=settings_callback)
+
+
+def run_app(*args, **kwargs):
+ sys_args = sys.argv
+
+ # The established command for running this program
+ runner_name = os.path.basename(sys_args[0])
+
+ args, command, command_args = parse_args(sys_args[1:])
+
+ if not command:
+ print "usage: %s [--config=/path/to/settings.py] [command] [options]" % runner_name
+ sys.exit(1)
+
+ default_config_path = kwargs.get('default_config_path')
+
+ parser = OptionParser()
+
# The ``init`` command is reserved for initializing configuration
if command == 'init':
(options, opt_args) = parser.parse_args()
+ settings_initializer = kwargs.get('settings_initializer')
+
config_path = ' '.join(opt_args[1:]) or default_config_path
if os.path.exists(config_path):
@@ -117,25 +148,9 @@ def run_app(project=None, default_config_path=None, default_settings=None,
(options, logan_args) = parser.parse_args(args)
- config_path = os.path.expanduser(options.config)
+ config_path = options.config
- if not os.path.exists(config_path):
- raise ValueError("Configuration file does not exist. Use '%s init' to initialize the file." % runner_name)
-
- os.environ['DJANGO_SETTINGS_MODULE'] = config_module_name
-
- def settings_callback(settings):
- if initializer is None:
- return
-
- initializer({
- 'project': project,
- 'config_path': config_path,
- 'settings': settings,
- })
-
- importer.install(config_module_name, config_path, default_settings,
- allow_extras=allow_extras, callback=settings_callback)
+ configure_app(config_path, *args, **kwargs)
management.execute_from_command_line([runner_name, command] + command_args)
Please sign in to comment.
Something went wrong with that request. Please try again.