Skip to content

Commit

Permalink
Fix management command in Django 1.8.
Browse files Browse the repository at this point in the history
Close #52.

I hope this does not break earlier versions.
  • Loading branch information
miracle2k committed Apr 17, 2015
1 parent 3dcdd0c commit 97005b9
Showing 1 changed file with 60 additions and 5 deletions.
65 changes: 60 additions & 5 deletions django_assets/management/commands/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import logging
from optparse import make_option
from django.conf import settings
from django.core.management import LaxOptionParser
from django.core.management.base import BaseCommand, CommandError

from webassets.script import (CommandError as AssetCommandError,
Expand All @@ -35,6 +34,63 @@
from django_assets.loaders import get_django_template_dirs, DjangoLoader


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)


class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('--parse-templates', action='store_true',
Expand All @@ -51,10 +107,9 @@ def create_parser(self, prog_name, subcommand):
# along to the webassets command.
# Hooking into run_from_argv() would be another thing to try
# if this turns out to be problematic.
return LaxOptionParser(prog=prog_name,
usage=self.usage(subcommand),
version=self.get_version(),
option_list=self.option_list)
parser = BaseCommand.create_parser(self, prog_name, subcommand)
parser.__class__ = LaxOptionParser
return parser

def handle(self, *args, **options):
# Due to the use of LaxOptionParser ``args`` now contains all
Expand Down

0 comments on commit 97005b9

Please sign in to comment.