Ticket #18020: loaddata_refactor_compression_types.diff
File loaddata_refactor_compression_types.diff, 3.8 KB (added by , 13 years ago) |
---|
-
django/core/management/commands/loaddata.py
18 18 from django.db.models import get_apps 19 19 from django.utils.itercompat import product 20 20 21 try:22 import bz223 has_bz2 = True24 except ImportError:25 has_bz2 = False26 21 22 def get_module(module_name): 23 try: 24 module = __import__(module_name) 25 except ImportError: 26 return None 27 return module 28 29 27 30 class Command(BaseCommand): 28 31 help = 'Installs the named fixture(s) in the database.' 29 32 args = "fixture [fixture ...]" … … 33 36 default=DEFAULT_DB_ALIAS, help='Nominates a specific database to load ' 34 37 'fixtures into. Defaults to the "default" database.'), 35 38 ) 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 36 58 37 59 def handle(self, *fixture_labels, **options): 38 60 using = options.get('database') … … 77 99 transaction.enter_transaction_management(using=using) 78 100 transaction.managed(True, using=using) 79 101 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': SingleZipReader92 }93 if has_bz2:94 compression_types['bz2'] = bz2.BZ2File95 96 102 app_module_paths = [] 97 103 for app in get_apps(): 98 104 if hasattr(app, '__path__'): … … 110 116 for fixture_label in fixture_labels: 111 117 parts = fixture_label.split('.') 112 118 113 if len(parts) > 1 and parts[-1] in compression_types:119 if len(parts) > 1 and parts[-1] in self._compression_types: 114 120 compression_formats = [parts[-1]] 115 121 parts = parts[:-1] 116 122 else: 117 compression_formats = compression_types.keys()123 compression_formats = self._compression_types.keys() 118 124 119 125 if len(parts) == 1: 120 126 fixture_name = parts[0] … … 161 167 self.stdout.write("Trying %s for %s fixture '%s'...\n" % \ 162 168 (humanize(fixture_dir), file_name, fixture_name)) 163 169 full_path = os.path.join(fixture_dir, file_name) 164 open_method = compression_types[compression_format]170 open_method = self._compression_types[compression_format] 165 171 try: 166 172 fixture = open_method(full_path, 'r') 167 173 except IOError: