Django

Code

Changeset 6528

Show
Ignore:
Timestamp:
10/18/07 20:26:09 (1 year ago)
Author:
gwilson
Message:

Django coding style fixes.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/management/__init__.py

    r6456 r6528  
    1 import django 
    2 from django.core.management.base import BaseCommand, CommandError, handle_default_options  
    3 from optparse import OptionParser 
    41import os 
    52import sys 
     3from optparse import OptionParser 
    64from imp import find_module 
     5 
     6import django 
     7from django.core.management.base import BaseCommand, CommandError, handle_default_options 
    78 
    89# For backwards compatibility: get_version() used to be in this module. 
    910get_version = django.get_version 
    1011 
    11 # A cache of loaded commands, so that call_command  
     12# A cache of loaded commands, so that call_command 
    1213# doesn't have to reload every time it is called 
    1314_commands = None 
     
    1516def find_commands(management_dir): 
    1617    """ 
    17     Given a path to a management directory, return a list of all the command names  
    18     that are available. Returns an empty list if no commands are defined. 
    19     """ 
    20     command_dir = os.path.join(management_dir,'commands') 
     18    Given a path to a management directory, returns a list of all the command 
     19    names that are available. 
     20 
     21    Returns an empty list if no commands are defined. 
     22    """ 
     23    command_dir = os.path.join(management_dir, 'commands') 
    2124    try: 
    22         return [f[:-3] for f in os.listdir(command_dir) if not f.startswith('_') and f.endswith('.py')] 
     25        return [f[:-3] for f in os.listdir(command_dir) 
     26                if not f.startswith('_') and f.endswith('.py')] 
    2327    except OSError: 
    2428        return [] 
     
    2630def find_management_module(app_name): 
    2731    """ 
    28     Determine the path to the management module for the application named, 
     32    Determines the path to the management module for the application named, 
    2933    without acutally importing the application or the management module. 
    3034 
     
    3741    while parts: 
    3842        part = parts.pop() 
    39         f,path,descr = find_module(part, path and [path] or None) 
     43        f, path, descr = find_module(part, path and [path] or None) 
    4044    return path 
    41      
     45 
    4246def load_command_class(app_name, name): 
    4347    """ 
    44     Given a command name and an application name, returns the Command  
     48    Given a command name and an application name, returns the Command 
    4549    class instance. All errors raised by the importation process 
    4650    (ImportError, AttributeError) are allowed to propagate. 
    4751    """ 
    48     return getattr(__import__('%s.management.commands.%s' % (app_name, name),  
     52    return getattr(__import__('%s.management.commands.%s' % (app_name, name), 
    4953                   {}, {}, ['Command']), 'Command')() 
    5054 
     
    5256    """ 
    5357    Returns a dictionary of commands against the application in which 
    54     those commands can be found. This works by looking for a  
    55     management.commands package in django.core, and in each installed  
     58    those commands can be found. This works by looking for a 
     59    management.commands package in django.core, and in each installed 
    5660    application -- if a commands package exists, all commands in that 
    5761    package are registered. 
     
    6367 
    6468    The dictionary is in the format {command_name: app_name}. Key-value 
    65     pairs from this dictionary can then be used in calls to  
     69    pairs from this dictionary can then be used in calls to 
    6670    load_command_class(app_name, command_name) 
    67      
     71 
    6872    If a specific version of a command must be loaded (e.g., with the 
    6973    startapp command), the instantiated module can be placed in the 
    7074    dictionary in place of the application name. 
    71      
     75 
    7276    The dictionary is cached on the first call, and reused on subsequent 
    7377    calls. 
     
    7579    global _commands 
    7680    if _commands is None: 
    77         _commands = dict([(name, 'django.core')  
     81        _commands = dict([(name, 'django.core') 
    7882                          for name in find_commands(__path__[0])]) 
    7983        if load_user_commands: 
    80             # Get commands from all installed apps 
     84            # Get commands from all installed apps. 
    8185            from django.conf import settings 
    8286            for app_name in settings.INSTALLED_APPS: 
    8387                try: 
    8488                    path = find_management_module(app_name) 
    85                     _commands.update(dict([(name, app_name)  
     89                    _commands.update(dict([(name, app_name) 
    8690                                           for name in find_commands(path)])) 
    8791                except ImportError: 
    8892                    pass # No management module - ignore this app 
    89                      
     93 
    9094        if project_directory: 
    9195            # Remove the "startproject" command from self.commands, because 
     
    9498 
    9599            # Override the startapp command so that it always uses the 
    96             # project_directory, not the current working directory  
     100            # project_directory, not the current working directory 
    97101            # (which is default). 
    98102            from django.core.management.commands.startapp import ProjectCommand 
     
    114118    try: 
    115119        app_name = get_commands()[name] 
    116         if isinstance(app_name, BaseCommand):  
     120        if isinstance(app_name, BaseCommand): 
    117121            # If the command is already loaded, use it directly. 
    118122            klass = app_name 
     
    122126        raise CommandError, "Unknown command: %r" % name 
    123127    return klass.execute(*args, **options) 
    124      
    125 class LaxOptionParser(OptionParser):  
     128 
     129class LaxOptionParser(OptionParser): 
    126130    """ 
    127131    An option parser that doesn't raise any errors on unknown options. 
    128      
     132 
    129133    This is needed because the --settings and --pythonpath options affect 
    130     the commands (and thus the options) that are available to the user.  
    131     """ 
    132     def error(self, msg):  
    133            pass 
     134    the commands (and thus the options) that are available to the user. 
     135    """ 
     136    def error(self, msg): 
     137        pass 
    134138 
    135139class ManagementUtility(object): 
     
    145149        self.project_directory = None 
    146150        self.user_commands = False 
    147          
     151 
    148152    def main_help_text(self): 
    149153        """ 
     
    151155        """ 
    152156        usage = ['%s <subcommand> [options] [args]' % self.prog_name] 
    153         usage.append('Django command line tool, version %s' % django.get_version()) 
    154         usage.append("Type '%s help <subcommand>' for help on a specific subcommand." % self.prog_name) 
     157        usage.append('Django command line tool,' 
     158                     ' version %s' % django.get_version()) 
     159        usage.append("Type '%s help <subcommand>' for help on a specific" 
     160                     " subcommand." % self.prog_name) 
    155161        usage.append('Available subcommands:') 
    156         commands = get_commands(self.user_commands, self.project_directory).keys() 
     162        commands = get_commands(self.user_commands, 
     163                                self.project_directory).keys() 
    157164        commands.sort() 
    158165        for cmd in commands: 
     
    167174        """ 
    168175        try: 
    169             app_name = get_commands(self.user_commands, self.project_directory)[subcommand] 
    170             if isinstance(app_name, BaseCommand):  
     176            app_name = get_commands(self.user_commands, 
     177                                    self.project_directory)[subcommand] 
     178            if isinstance(app_name, BaseCommand): 
    171179                # If the command is already loaded, use it directly. 
    172180                klass = app_name 
     
    174182                klass = load_command_class(app_name, subcommand) 
    175183        except KeyError: 
    176             sys.stderr.write("Unknown command: %r\nType '%s help' for usage.\n" % (subcommand, self.prog_name)) 
     184            sys.stderr.write("Unknown command: %r\nType '%s help' for" 
     185                             " usage.\n" % (subcommand, self.prog_name)) 
    177186            sys.exit(1) 
    178187        return klass 
    179          
     188 
    180189    def execute(self): 
    181190        """ 
     
    183192        being run, creates a parser appropriate to that command, and runs it. 
    184193        """ 
    185         # Preprocess options to extract --settings and --pythonpath. These options 
    186         # could affect the commands that are available, so they must be processed 
    187         # early 
    188         parser = LaxOptionParser(version=get_version(),  
    189                                  option_list=BaseCommand.option_list)  
     194        # Preprocess options to extract --settings and --pythonpath. 
     195        # These options could affect the commands that are available, so they 
     196        # must be processed early. 
     197        parser = LaxOptionParser(version=get_version(), 
     198                                 option_list=BaseCommand.option_list) 
    190199        try: 
    191             options, args = parser.parse_args(self.argv)  
     200            options, args = parser.parse_args(self.argv) 
    192201            handle_default_options(options) 
    193         except:  
     202        except: 
    194203            pass # Ignore any option errors at this point. 
    195           
     204 
    196205        try: 
    197206            subcommand = self.argv[1] 
     
    228237        self.project_directory = project_directory 
    229238        self.user_commands = True 
    230                  
     239 
    231240def setup_environ(settings_mod): 
    232241    """ 
    233     Configure the runtime environment. This can also be used by external 
     242    Configures the runtime environment. This can also be used by external 
    234243    scripts wanting to set up a similar environment to manage.py. 
    235244    """ 
     
    245254 
    246255    # Set DJANGO_SETTINGS_MODULE appropriately. 
    247     os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name) 
     256    os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, 
     257                                                      settings_name) 
    248258    return project_directory 
    249259 
  • django/trunk/django/utils/datastructures.py

    r6506 r6528  
    4343                return True 
    4444        return False 
    45      
     45 
    4646    __contains__ = has_key 
    4747 
    4848    def copy(self): 
    49         """ returns a copy of this object""" 
     49        """Returns a copy of this object.""" 
    5050        return self.__copy__() 
    5151 
    5252class SortedDict(dict): 
    53     "A dictionary that keeps its keys in the order in which they're inserted." 
     53    """ 
     54    A dictionary that keeps its keys in the order in which they're inserted. 
     55    """ 
    5456    def __init__(self, data=None): 
    55         if data is None: data = {} 
     57        if data is None: 
     58            data = {} 
    5659        dict.__init__(self, data) 
    5760        if isinstance(data, dict): 
    5861            self.keyOrder = data.keys() 
    5962        else: 
    60             self.keyOrder=[key for key, value in data] 
     63            self.keyOrder = [key for key, value in data] 
    6164 
    6265    def __setitem__(self, key, value): 
     
    103106 
    104107    def value_for_index(self, index): 
    105         "Returns the value of the item at the given zero-based index.
     108        """Returns the value of the item at the given zero-based index.""
    106109        return self[self.keyOrder[index]] 
    107110 
    108111    def insert(self, index, key, value): 
    109         "Inserts the key, value pair before the item with the given index.
     112        """Inserts the key, value pair before the item with the given index.""
    110113        if key in self.keyOrder: 
    111114            n = self.keyOrder.index(key) 
    112115            del self.keyOrder[n] 
    113             if n < index: index -= 1 
     116            if n < index: 
     117                index -= 1 
    114118        self.keyOrder.insert(index, key) 
    115119        dict.__setitem__(self, key, value) 
    116120 
    117121    def copy(self): 
    118         "Returns a copy of this object.
     122        """Returns a copy of this object.""
    119123        # This way of initializing the copy means it works for subclasses, too. 
    120124        obj = self.__class__(self) 
     
    134138class MultiValueDict(dict): 
    135139    """ 
    136     A subclass of dictionary customized to handle multiple values for the same key. 
     140    A subclass of dictionary customized to handle multiple values for the 
     141    same key. 
    137142 
    138143    >>> d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']}) 
     
    177182    def __deepcopy__(self, memo=None): 
    178183        import copy 
    179         if memo is None: memo = {} 
     184        if memo is None: 
     185            memo = {} 
    180186        result = self.__class__() 
    181187        memo[id(self)] = result 
    182188        for key, value in dict.items(self): 
    183             dict.__setitem__(result, copy.deepcopy(key, memo), copy.deepcopy(value, memo)) 
     189            dict.__setitem__(result, copy.deepcopy(key, memo), 
     190                             copy.deepcopy(value, memo)) 
    184191        return result 
    185192 
    186193    def get(self, key, default=None): 
    187         "Returns the default value if the requested data doesn't exist
     194        """Returns the default value if the requested data doesn't exist.""
    188195        try: 
    189196            val = self[key] 
     
    195202 
    196203    def getlist(self, key): 
    197         "Returns an empty list if the requested data doesn't exist
     204        """Returns an empty list if the requested data doesn't exist.""
    198205        try: 
    199206            return dict.__getitem__(self, key) 
     
    215222 
    216223    def appendlist(self, key, value): 
    217         "Appends an item to the internal list associated with key
     224        """Appends an item to the internal list associated with key.""
    218225        self.setlistdefault(key, []) 
    219226        dict.__setitem__(self, key, self.getlist(key) + [value]) 
     
    227234 
    228235    def lists(self): 
    229         "Returns a list of (key, list) pairs.
     236        """Returns a list of (key, list) pairs.""
    230237        return dict.items(self) 
    231238 
    232239    def values(self): 
    233         "Returns a list of the last value on every key list.
     240        """Returns a list of the last value on every key list.""
    234241        return [self[key] for key in self.keys()] 
    235242 
    236243    def copy(self): 
    237         "Returns a copy of this object.
     244        """Returns a copy of this object.""
    238245        return self.__deepcopy__() 
    239246 
    240247    def update(self, *args, **kwargs): 
    241         "update() extends rather than replaces existing key lists. Also accepts keyword args." 
     248        """ 
     249        update() extends rather than replaces existing key lists. 
     250        Also accepts keyword args. 
     251        """ 
    242252        if len(args) > 1: 
    243253            raise TypeError, "update expected at most 1 arguments, got %d" % len(args) 
     
    300310            return dict.__repr__(d) 
    301311        return dict.__repr__(self) 
    302