Skip to content

Commit c6f3e7f

Browse files
submarcosChatewgne
authored andcommitted
fix parser import by python path
1 parent 5edf6b8 commit c6f3e7f

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

geotrek/common/management/commands/import.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,20 @@ def handle(self, *args, **options):
2424

2525
if '.' in options['parser']:
2626
# Python import syntax
27-
Parser = import_string(options['parser'])
27+
try:
28+
Parser = import_string(options['parser'])
29+
except Exception:
30+
raise CommandError("Failed to import parser class '{0}'".format(options['parser']))
2831
else:
2932
# just a class name
30-
module_path = join(settings.VAR_DIR, 'conf/parsers.py')
31-
module_name = 'parsers'
32-
class_name = options['parser']
33-
spec = importlib.util.spec_from_file_location(module_name, module_path)
34-
module = importlib.util.module_from_spec(spec)
35-
Parser = getattr(module, class_name)
3633
try:
34+
module_path = join(settings.VAR_DIR, 'conf/parsers.py')
35+
module_name = 'parsers'
36+
class_name = options['parser']
37+
spec = importlib.util.spec_from_file_location(module_name, module_path)
38+
module = importlib.util.module_from_spec(spec)
3739
spec.loader.exec_module(module)
40+
Parser = getattr(module, class_name)
3841
except FileNotFoundError:
3942
raise CommandError("Failed to import parser file '{0}'".format(module_path))
4043

geotrek/common/tests/test_parsers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ def filter_attachments(self, src, val):
9191

9292
class ParserTests(TestCase):
9393
def test_bad_parser_class(self):
94-
with self.assertRaisesRegex(CommandError, "Failed to import parser class 'DoesNotExist'"):
94+
with self.assertRaisesRegex(Exception, "Failed to import parser class 'geotrek.common.tests.test_parsers.DoesNotExist'"):
9595
call_command('import', 'geotrek.common.tests.test_parsers.DoesNotExist', '', verbosity=0)
9696

9797
def test_bad_parser_file(self):
98-
with self.assertRaisesRegex(CommandError, "Failed to import parser file 'geotrek/common.py'"):
99-
call_command('import', 'geotrek.common.DoesNotExist', '', verbosity=0)
98+
with self.assertRaisesRegex(Exception, "module 'parsers' has no attribute 'DoesNotExist'"):
99+
call_command('import', 'DoesNotExist', '', verbosity=0)
100100

101101
def test_no_filename_no_url(self):
102102
with self.assertRaisesRegex(CommandError, "File path missing"):

0 commit comments

Comments
 (0)