Skip to content

Commit 5edf6b8

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

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

docs/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ CHANGELOG
99

1010
- Add field ``access`` to Signage and Infrastructure models (#3605)
1111

12+
**Bug fixes**
13+
14+
- Fix: Parser loading with import command (#3538)
15+
16+
1217
2.98.1 (2023-05-30)
1318
-----------------------
1419

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)