Skip to content
Browse files

Refactored some small parts of core.management -- ManagementUtility.e…

…xecute() no longer takes an argument, and ManagementUtility.__init__() now takes argv

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6091 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent aacb0cf commit a291952a7d324e1675ed0ee1f82bb2c4b4b2a5eb @adrianholovaty adrianholovaty committed Sep 11, 2007
Showing with 35 additions and 35 deletions.
  1. +27 −27 django/core/management/__init__.py
  2. +8 −8 django/core/management/base.py
View
54 django/core/management/__init__.py
@@ -35,7 +35,9 @@ class ManagementUtility(object):
A ManagementUtility has a number of commands, which can be manipulated
by editing the self.commands dictionary.
"""
- def __init__(self):
+ def __init__(self, argv=None):
+ self.argv = argv or sys.argv[:]
+ self.prog_name = os.path.basename(self.argv[0])
self.commands = self.default_commands()
def default_commands(self):
@@ -51,22 +53,21 @@ def default_commands(self):
names = [f[:-3] for f in os.listdir(command_dir) if not f.startswith('_') and f.endswith('.py')]
return dict([(name, load_command_class(name)) for name in names])
- def print_help(self, prog_name):
+ def main_help_text(self):
"""
- Returns the help message, as a string.
+ Returns the script's main help text, as a string.
"""
- prog_name = os.path.basename(prog_name)
- usage = ['%s <subcommand> [options] [args]' % prog_name]
+ usage = ['%s <subcommand> [options] [args]' % self.prog_name]
usage.append('Django command line tool, version %s' % django.get_version())
- usage.append("Type '%s help <subcommand>' for help on a specific subcommand." % prog_name)
+ usage.append("Type '%s help <subcommand>' for help on a specific subcommand." % self.prog_name)
usage.append('Available subcommands:')
commands = self.commands.keys()
commands.sort()
for cmd in commands:
usage.append(' %s' % cmd)
- print '\n'.join(usage)
+ return '\n'.join(usage)
- def fetch_command(self, subcommand, prog_name):
+ def fetch_command(self, subcommand):
"""
Tries to fetch the given subcommand, printing a message with the
appropriate command called from the command line (usually
@@ -75,35 +76,34 @@ def fetch_command(self, subcommand, prog_name):
try:
return self.commands[subcommand]
except KeyError:
- sys.stderr.write("Unknown command: %r\nType '%s help' for usage.\n" % (subcommand, prog_name))
+ sys.stderr.write("Unknown command: %r\nType '%s help' for usage.\n" % (subcommand, self.prog_name))
sys.exit(1)
- def execute(self, argv=None):
+ def execute(self):
"""
Given the command-line arguments, this figures out which subcommand is
being run, creates a parser appropriate to that command, and runs it.
"""
- if argv is None:
- argv = sys.argv
try:
- subcommand = argv[1]
+ subcommand = self.argv[1]
except IndexError:
- sys.stderr.write("Type '%s help' for usage.\n" % os.path.basename(argv[0]))
+ sys.stderr.write("Type '%s help' for usage.\n" % self.prog_name)
sys.exit(1)
if subcommand == 'help':
- if len(argv) > 2:
- self.fetch_command(argv[2], argv[0]).print_help(argv[0])
+ if len(self.argv) > 2:
+ self.fetch_command(self.argv[2]).print_help(self.prog_name, self.argv[2])
else:
- self.print_help(argv[0])
+ sys.stderr.write(self.main_help_text() + '\n')
+ sys.exit(1)
# Special-cases: We want 'django-admin.py --version' and
# 'django-admin.py --help' to work, for backwards compatibility.
- elif argv[1:] == ['--version']:
+ elif self.argv[1:] == ['--version']:
print django.get_version()
- elif argv[1:] == ['--help']:
- self.print_help(argv[0])
+ elif self.argv[1:] == ['--help']:
+ self.main_help_text()
else:
- self.fetch_command(subcommand, argv[0]).run_from_argv(argv[1:])
+ self.fetch_command(subcommand).run_from_argv(self.argv)
class ProjectManagementUtility(ManagementUtility):
"""
@@ -114,8 +114,8 @@ class ProjectManagementUtility(ManagementUtility):
In practice, this class represents manage.py, whereas ManagementUtility
represents django-admin.py.
"""
- def __init__(self, project_directory):
- super(ProjectManagementUtility, self).__init__()
+ def __init__(self, argv, project_directory):
+ super(ProjectManagementUtility, self).__init__(argv)
# Remove the "startproject" command from self.commands, because
# that's a django-admin.py command, not a manage.py command.
@@ -149,14 +149,14 @@ def execute_from_command_line(argv=None):
"""
A simple method that runs a ManagementUtility.
"""
- utility = ManagementUtility()
- utility.execute(argv)
+ utility = ManagementUtility(argv)
+ utility.execute()
def execute_manager(settings_mod, argv=None):
"""
Like execute_from_command_line(), but for use by manage.py, a
project-specific django-admin.py utility.
"""
project_directory = setup_environ(settings_mod)
- utility = ProjectManagementUtility(project_directory)
- utility.execute(argv)
+ utility = ProjectManagementUtility(argv, project_directory)
+ utility.execute()
View
16 django/core/management/base.py
@@ -36,26 +36,26 @@ def get_version(self):
"""
return django.get_version()
- def usage(self):
- usage = '%prog [options] ' + self.args
+ def usage(self, subcommand):
+ usage = '%%prog %s [options] %s' % (subcommand, self.args)
if self.help:
return '%s\n\n%s' % (usage, self.help)
else:
return usage
- def create_parser(self, prog_name):
+ def create_parser(self, prog_name, subcommand):
return OptionParser(prog=prog_name,
- usage=self.usage(),
+ usage=self.usage(subcommand),
version=self.get_version(),
option_list=self.option_list)
- def print_help(self, prog_name):
- parser = self.create_parser(prog_name)
+ def print_help(self, prog_name, subcommand):
+ parser = self.create_parser(prog_name, subcommand)
parser.print_help()
def run_from_argv(self, argv):
- parser = self.create_parser(argv[0])
- options, args = parser.parse_args(argv[1:])
+ parser = self.create_parser(argv[0], argv[1])
+ options, args = parser.parse_args(argv[2:])
if options.settings:
os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
if options.pythonpath:

0 comments on commit a291952

Please sign in to comment.
Something went wrong with that request. Please try again.