Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parser import by python path #3544

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ This version drop support for Ubuntu Bionic 18.04 debian package. Please update
- Pre-deprecate support of Ubuntu 20.04. Unit tests will continue but end to end tests will be stopped. Update will be available but install script will be discontinued.
- Deprecate support of Ubuntu 18.04 bionic.

**Bug fixes**

- Fix: Parser loading with import command (#3538)


2.113.1 (2025-02-17)
----------------------------
Expand Down
30 changes: 15 additions & 15 deletions geotrek/common/management/commands/import.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from django.utils.module_loading import import_string

from geotrek.common.parsers import ImportError

Expand All @@ -23,24 +24,23 @@ def handle(self, *args, **options):

if '.' in options['parser']:
# Python import syntax
module_name, class_name = options['parser'].rsplit('.', 1)
module_path = module_name.replace('.', '/') + '.py'
try:
Parser = import_string(options['parser'])
except Exception:
raise CommandError("Failed to import parser class '{0}'".format(options['parser']))
else:
# just a class name
module_path = join(settings.VAR_DIR, 'conf/parsers.py')
module_name = 'parsers'
class_name = options['parser']
try:
module_path = join(settings.VAR_DIR, 'conf/parsers.py')
module_name = 'parsers'
class_name = options['parser']
spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
Parser = getattr(module, class_name)
except FileNotFoundError:
raise CommandError("Failed to import parser file '{0}'".format(module_path))

spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(spec)
try:
spec.loader.exec_module(module)
except FileNotFoundError:
raise CommandError("Failed to import parser file '{0}'".format(module_path))
try:
Parser = getattr(module, class_name)
except AttributeError:
raise CommandError("Failed to import parser class '{0}'".format(class_name))
if not Parser.filename and not Parser.url and not options['filename']:
raise CommandError("File path missing")

Expand Down
6 changes: 3 additions & 3 deletions geotrek/common/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ def filter_attachments(self, src, val):

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

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

def test_no_filename_no_url(self):
with self.assertRaisesRegex(CommandError, "File path missing"):
Expand Down
Loading