From aaf548258f2c15089345afa77184399993f836eb Mon Sep 17 00:00:00 2001 From: bobslee Date: Sat, 23 Sep 2023 11:22:45 +0200 Subject: [PATCH] Fix get_component_object (Builder) method to handle ModuleNotFoundError. Therefor implemented the get_component_class method to determine the class with a fallback to the base `Component` class. --- CHANGELOG.md | 5 +++++ formiodata/builder.py | 35 ++++++++++++++++++++--------------- pyproject.toml | 2 +- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ca298a..502f79e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 1.2.1 + +Fix `get_component_object` (Builder) method to handle `ModuleNotFoundError`.\ +Therefor implemented the `get_component_class` method to determine the class with a fallback to the base `Component` class. + ## 1.2.0 New "component class mapping feature" for the Builder instantiation:\ diff --git a/formiodata/builder.py b/formiodata/builder.py index 3d8da6c..fda3a95 100644 --- a/formiodata/builder.py +++ b/formiodata/builder.py @@ -84,6 +84,24 @@ def _load_components(self, components, parent=None): component_obj.load(component_owner=self, parent=None, data=None) self.components[component_obj.key] = component_obj + def get_component_class(self, component): + component_type = component.get('type') + try: + mapping_value = self.component_class_mapping[component_type] + if isinstance(mapping_value, str): + cls_name = '%sComponent' % mapping_value + import_path = 'formiodata.components.%s' % mapping_value + module = __import__(import_path, fromlist=[cls_name]) + cls = getattr(module, cls_name) + else: + cls = self.component_class_mapping[component_type] + except KeyError: + cls_name = '%sComponent' % component_type + import_path = 'formiodata.components.%s' % component_type + module = __import__(import_path, fromlist=[cls_name]) + cls = getattr(module, cls_name) + return cls + def get_component_object(self, component): """ @param component @@ -91,23 +109,10 @@ def get_component_object(self, component): component_type = component.get('type') if component_type: try: - try: - mapping_value = self.component_class_mapping[component_type] - if isinstance(mapping_value, str): - cls_name = '%sComponent' % mapping_value - import_path = 'formiodata.components.%s' % mapping_value - module = __import__(import_path, fromlist=[cls_name]) - cls = getattr(module, cls_name) - else: - cls = self.component_class_mapping[component_type] - except KeyError: - cls_name = '%sComponent' % component_type - import_path = 'formiodata.components.%s' % component_type - module = __import__(import_path, fromlist=[cls_name]) - cls = getattr(module, cls_name) + cls = self.get_component_class(component) component_obj = cls(component, self, language=self.language, i18n=self.i18n, resources=self.resources) return component_obj - except AttributeError as e: + except (AttributeError, ModuleNotFoundError) as e: # TODO try to find/load first from self._component_cls else # re-raise exception or silence (log error and return False) logging.error(e) diff --git a/pyproject.toml b/pyproject.toml index 5a093a5..1b6c376 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "formio-data" -version = "1.2.0" +version = "1.2.1" homepage = "https://github.com/novacode-nl/python-formio-data" description = "formio.js JSON-data API" readme = "README.md"