Ticket #18020: loaddata_refactor_compression_types.diff

File loaddata_refactor_compression_types.diff, 3.8 KB (added by hoffmaje, 12 years ago)
  • django/core/management/commands/loaddata.py

     
    1818from django.db.models import get_apps
    1919from django.utils.itercompat import product
    2020
    21 try:
    22     import bz2
    23     has_bz2 = True
    24 except ImportError:
    25     has_bz2 = False
    2621
     22def get_module(module_name):
     23    try:
     24        module = __import__(module_name)
     25    except ImportError:
     26        return None
     27    return module
     28
     29
    2730class Command(BaseCommand):
    2831    help = 'Installs the named fixture(s) in the database.'
    2932    args = "fixture [fixture ...]"
     
    3336            default=DEFAULT_DB_ALIAS, help='Nominates a specific database to load '
    3437                'fixtures into. Defaults to the "default" database.'),
    3538    )
     39 
     40    class _SingleZipReader(zipfile.ZipFile):
     41        def __init__(self, *args, **kwargs):
     42            zipfile.ZipFile.__init__(self, *args, **kwargs)
     43            if settings.DEBUG:
     44                assert len(self.namelist()) == 1, "Zip-compressed fixtures must contain only one file."
     45        def read(self):
     46            return zipfile.ZipFile.read(self, self.namelist()[0])
     47   
     48    def __init__(self, *args, **kwargs):
     49        super(self.__class__, self).__init__(*args, **kwargs)
     50        self._compression_types = {
     51            None:   open,
     52            'gz':   gzip.GzipFile,
     53            'zip':  self.__class__._SingleZipReader
     54        }
     55        bz2 = get_module('bz2')
     56        if bz2:
     57            self._compression_types['bz2'] = bz2.BZ2File
    3658
    3759    def handle(self, *fixture_labels, **options):
    3860        using = options.get('database')
     
    7799            transaction.enter_transaction_management(using=using)
    78100            transaction.managed(True, using=using)
    79101
    80         class SingleZipReader(zipfile.ZipFile):
    81             def __init__(self, *args, **kwargs):
    82                 zipfile.ZipFile.__init__(self, *args, **kwargs)
    83                 if settings.DEBUG:
    84                     assert len(self.namelist()) == 1, "Zip-compressed fixtures must contain only one file."
    85             def read(self):
    86                 return zipfile.ZipFile.read(self, self.namelist()[0])
    87 
    88         compression_types = {
    89             None:   open,
    90             'gz':   gzip.GzipFile,
    91             'zip':  SingleZipReader
    92         }
    93         if has_bz2:
    94             compression_types['bz2'] = bz2.BZ2File
    95 
    96102        app_module_paths = []
    97103        for app in get_apps():
    98104            if hasattr(app, '__path__'):
     
    110116                for fixture_label in fixture_labels:
    111117                    parts = fixture_label.split('.')
    112118
    113                     if len(parts) > 1 and parts[-1] in compression_types:
     119                    if len(parts) > 1 and parts[-1] in self._compression_types:
    114120                        compression_formats = [parts[-1]]
    115121                        parts = parts[:-1]
    116122                    else:
    117                         compression_formats = compression_types.keys()
     123                        compression_formats = self._compression_types.keys()
    118124
    119125                    if len(parts) == 1:
    120126                        fixture_name = parts[0]
     
    161167                                self.stdout.write("Trying %s for %s fixture '%s'...\n" % \
    162168                                    (humanize(fixture_dir), file_name, fixture_name))
    163169                            full_path = os.path.join(fixture_dir, file_name)
    164                             open_method = compression_types[compression_format]
     170                            open_method = self._compression_types[compression_format]
    165171                            try:
    166172                                fixture = open_method(full_path, 'r')
    167173                            except IOError:
Back to Top