From 41dfcee46c550d01673d1d3fcba898e49d459852 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Tue, 6 Jan 2015 21:16:14 +0100 Subject: [PATCH] Backported LaxOptionParser from Django 1.7 to make it work on 1.8. --- configurations/importer.py | 3 +- configurations/utils.py | 62 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/configurations/importer.py b/configurations/importer.py index cc4ad57..a76e773 100644 --- a/configurations/importer.py +++ b/configurations/importer.py @@ -5,10 +5,9 @@ import sys from optparse import make_option from django.core.exceptions import ImproperlyConfigured -from django.core.management import LaxOptionParser from django.conf import ENVIRONMENT_VARIABLE as SETTINGS_ENVIRONMENT_VARIABLE -from .utils import uppercase_attributes, reraise +from .utils import uppercase_attributes, reraise, LaxOptionParser from .values import Value, setup_value installed = False diff --git a/configurations/utils.py b/configurations/utils.py index 59c51db..f864b48 100644 --- a/configurations/utils.py +++ b/configurations/utils.py @@ -60,3 +60,65 @@ def reraise(exc, prefix=None, suffix=None): suffix = '(' + suffix + ')' exc.args = ('{0} {1} {2}'.format(prefix, exc.args[0], suffix),) + args[1:] raise + +try: + from django.core.management import LaxOptionParser +except ImportError: + from optparse import OptionParser + + class LaxOptionParser(OptionParser): + """ + An option parser that doesn't raise any errors on unknown options. + + This is needed because the --settings and --pythonpath options affect + the commands (and thus the options) that are available to the user. + + Backported from Django 1.7.x + + """ + def error(self, msg): + pass + + def print_help(self): + """Output nothing. + + The lax options are included in the normal option parser, so under + normal usage, we don't need to print the lax options. + """ + pass + + def print_lax_help(self): + """Output the basic options available to every command. + + This just redirects to the default print_help() behavior. + """ + OptionParser.print_help(self) + + def _process_args(self, largs, rargs, values): + """ + Overrides OptionParser._process_args to exclusively handle default + options and ignore args and other options. + + This overrides the behavior of the super class, which stop parsing + at the first unrecognized option. + """ + while rargs: + arg = rargs[0] + try: + if arg[0:2] == "--" and len(arg) > 2: + # process a single long option (possibly with value(s)) + # the superclass code pops the arg off rargs + self._process_long_opt(rargs, values) + elif arg[:1] == "-" and len(arg) > 1: + # process a cluster of short options (possibly with + # value(s) for the last one only) + # the superclass code pops the arg off rargs + self._process_short_opts(rargs, values) + else: + # it's either a non-default option or an arg + # either way, add it to the args list so we can keep + # dealing with options + del rargs[0] + raise Exception + except: # Needed because we might need to catch a SystemExit + largs.append(arg)