Skip to content

Commit ef6cd2a

Browse files
committed
Small refactoring
1 parent 72be3c8 commit ef6cd2a

3 files changed

Lines changed: 20 additions & 6 deletions

File tree

pylasu/model/model.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
from .position import Position, Source
77
from .reflection import Multiplicity, PropertyDescription
8-
from ..reflection import getannotations
9-
from ..reflection.reflection import is_sequence_type, get_type_arguments
8+
from ..reflection import getannotations, get_type_arguments, is_sequence_type
109

1110

1211
class internal_property(property):
@@ -177,3 +176,17 @@ def _fields(self):
177176
@internal_property
178177
def node_type(self):
179178
return type(self)
179+
180+
181+
def concept_of(node):
182+
properties = dir(node)
183+
if "__concept__" in properties:
184+
node_type = node.__concept__
185+
elif "node_type" in properties:
186+
node_type = node.node_type
187+
else:
188+
node_type = type(node)
189+
if isinstance(node_type, Concept):
190+
return node_type
191+
else:
192+
raise Exception(f"Not a concept: {node_type} of {node}")

pylasu/reflection/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .reflection import getannotations
1+
from .reflection import getannotations, get_type_arguments, is_sequence_type

pylasu/transformation/transformation.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from pylasu.model import Node, Origin
77
from pylasu.model.errors import GenericErrorNode
8+
from pylasu.model.model import concept_of
89
from pylasu.model.reflection import PropertyDescription
910
from pylasu.transformation.generic_nodes import GenericNode
1011
from pylasu.validation import Issue, IssueSeverity
@@ -32,7 +33,7 @@ def set(self, node: Node, value):
3233
class NodeFactory(Generic[Source, Output]):
3334
constructor: node_factory_constructor_type
3435
children: Dict[str, "ChildNodeFactory[Source, Any, Any]"] = field(default_factory=dict)
35-
finalizer: Callable[[Source], None] = lambda _: None
36+
finalizer: Callable[[Source], None] = field(default=lambda _: None)
3637

3738
def with_child(
3839
self,
@@ -104,7 +105,7 @@ def transform_into_nodes(self, source: Optional[Any], parent: Optional[Node] = N
104105
if factory:
105106
nodes = self.make_nodes(factory, source)
106107
for node in nodes:
107-
for pd in type(node).node_properties:
108+
for pd in concept_of(node).node_properties:
108109
self.process_child(source, node, pd, factory)
109110
factory.finalizer(node)
110111
node.parent = parent
@@ -134,7 +135,7 @@ def process_child(self, source, node, pd, factory):
134135
if child_node_factory != NO_CHILD_NODE:
135136
self.set_child(child_node_factory, source, node, pd)
136137
else:
137-
# TODO should we support @Mapped?
138+
# TODO should we support @Mapped / dot-notation?
138139
factory.children[child_key] = NO_CHILD_NODE
139140

140141
def as_origin(self, source: Any) -> Optional[Origin]:

0 commit comments

Comments
 (0)