Skip to content

Commit 557fdb4

Browse files
committed
fix parser import by python path
1 parent 2c2cc40 commit 557fdb4

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
**Bug fixes**
99

1010
- Fix: Remove user group creation in Outdoor fixture (#3524)
11+
- Fix: Parser loading with import command (#3538)
1112

1213

1314
2.98.0 (2023-03-27)

geotrek/common/management/commands/import.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from django.conf import settings
55
from django.core.management.base import BaseCommand, CommandError
6+
from django.utils.module_loading import import_string
67

78
from 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

Comments
 (0)