33
44from django .conf import settings
55from django .core .management .base import BaseCommand , CommandError
6+ from django .utils .module_loading import import_string
67
78from geotrek .common .parsers import ImportError
89
@@ -23,24 +24,20 @@ def handle(self, *args, **options):
2324
2425 if '.' in options ['parser' ]:
2526 # Python import syntax
26- module_name , class_name = options ['parser' ].rsplit ('.' , 1 )
27- module_path = module_name .replace ('.' , '/' ) + '.py'
27+ Parser = import_string (options ['parser' ])
2828 else :
2929 # just a class name
3030 module_path = join (settings .VAR_DIR , 'conf/parsers.py' )
3131 module_name = 'parsers'
3232 class_name = options ['parser' ]
33-
34- spec = importlib .util .spec_from_file_location (module_name , module_path )
35- module = importlib .util .module_from_spec (spec )
36- try :
37- spec .loader .exec_module (module )
38- except FileNotFoundError :
39- raise CommandError ("Failed to import parser file '{0}'" .format (module_path ))
40- try :
33+ spec = importlib .util .spec_from_file_location (module_name , module_path )
34+ module = importlib .util .module_from_spec (spec )
4135 Parser = getattr (module , class_name )
42- except AttributeError :
43- raise CommandError ("Failed to import parser class '{0}'" .format (class_name ))
36+ try :
37+ spec .loader .exec_module (module )
38+ except FileNotFoundError :
39+ raise CommandError ("Failed to import parser file '{0}'" .format (module_path ))
40+
4441 if not Parser .filename and not Parser .url and not options ['filename' ]:
4542 raise CommandError ("File path missing" )
4643
0 commit comments