Skip to content

Commit

Permalink
Merge pull request #37 from bobslee/master
Browse files Browse the repository at this point in the history
Fix get_component_object (Builder) method to handle ModuleNotFoundError.
  • Loading branch information
bobslee authored Sep 23, 2023
2 parents 0a3c95d + aaf5482 commit 20c7939
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:\
Expand Down
35 changes: 20 additions & 15 deletions formiodata/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,35 @@ 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
"""
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)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 20c7939

Please sign in to comment.