Skip to content

Commit

Permalink
refactor; regen
Browse files Browse the repository at this point in the history
  • Loading branch information
doctrino committed Aug 14, 2024
1 parent 8d5fdfa commit d686f04
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cognite/pygen/_core/models/data_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ def is_edge_class(self) -> bool:

@property
def connections_docs_write(self) -> str:
connections = [f for f in self.fields_of_type(BaseConnectionField) if f.end_classes and f.is_write_field]
connections = [f for f in self.fields_of_type(BaseConnectionField) if f.end_classes and f.is_write_field] # type: ignore[type-abstract]
if len(connections) == 0:
raise ValueError("No connections found")
elif len(connections) == 1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def apply(
"""Add or update (upsert) connection item as.
Note: This method iterates through all nodes and timeseries linked to connection_item_a and creates them including the edges
between the nodes. For example, if any of `outwards` are set, then these
between the nodes. For example, if any of `other_direct`, `outwards` or `self_direct` are set, then these
nodes as well as any nodes linked to them, and all the edges linking these nodes will be created.
Args:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class ConnectionItemEGraphQL(GraphQLCore):
direct_reverse_single: Optional[ConnectionItemDGraphQL] = Field(
default=None, repr=False, alias="directReverseSingle"
)
inwards_single: Optional[list[ConnectionItemDGraphQL]] = Field(default=None, repr=False, alias="inwardsSingle")
inwards_single: Optional[ConnectionItemDGraphQL] = Field(default=None, repr=False, alias="inwardsSingle")
name: Optional[str] = None

@root_validator(pre=True)
Expand Down Expand Up @@ -125,7 +125,9 @@ def as_read(self) -> ConnectionItemE:
if isinstance(self.direct_reverse_single, GraphQLCore)
else self.direct_reverse_single
),
inwards_single=[inwards_single.as_read() for inwards_single in self.inwards_single or []],
inwards_single=(
self.inwards_single.as_read() if isinstance(self.inwards_single, GraphQLCore) else self.inwards_single
),
name=self.name,
)

Expand All @@ -138,7 +140,9 @@ def as_write(self) -> ConnectionItemEWrite:
external_id=self.external_id,
data_record=DataRecordWrite(existing_version=0),
direct_no_source=self.direct_no_source,
inwards_single=[inwards_single.as_write() for inwards_single in self.inwards_single or []],
inwards_single=(
self.inwards_single.as_write() if isinstance(self.inwards_single, GraphQLCore) else self.inwards_single
),
name=self.name,
)

Expand Down Expand Up @@ -166,7 +170,7 @@ class ConnectionItemE(DomainModel):
direct_no_source: Union[str, dm.NodeId, None] = Field(default=None, alias="directNoSource")
direct_reverse_multi: Optional[list[ConnectionItemD]] = Field(default=None, repr=False, alias="directReverseMulti")
direct_reverse_single: Optional[ConnectionItemD] = Field(default=None, repr=False, alias="directReverseSingle")
inwards_single: Optional[list[Union[ConnectionItemD, str, dm.NodeId]]] = Field(
inwards_single: Union[ConnectionItemD, str, dm.NodeId, None] = Field(
default=None, repr=False, alias="inwardsSingle"
)
name: Optional[str] = None
Expand All @@ -178,10 +182,9 @@ def as_write(self) -> ConnectionItemEWrite:
external_id=self.external_id,
data_record=DataRecordWrite(existing_version=self.data_record.version),
direct_no_source=self.direct_no_source,
inwards_single=[
inwards_single.as_write() if isinstance(inwards_single, DomainModel) else inwards_single
for inwards_single in self.inwards_single or []
],
inwards_single=(
self.inwards_single.as_write() if isinstance(self.inwards_single, DomainModel) else self.inwards_single
),
name=self.name,
)

Expand All @@ -205,7 +208,6 @@ def _update_connections(

for instance in instances.values():
if edges := edges_by_source_node.get(instance.as_id()):
inwards_single: list[ConnectionItemD | str | dm.NodeId] = []
for edge in edges:
value: DomainModel | DomainRelation | str | dm.NodeId
if isinstance(edge, DomainRelation):
Expand All @@ -231,9 +233,15 @@ def _update_connections(
if edge_type == dm.DirectRelationReference("pygen-models", "bidirectionalSingle") and isinstance(
value, (ConnectionItemD, str, dm.NodeId)
):
inwards_single.append(value)

instance.inwards_single = inwards_single or None
if instance.inwards_single is None:
instance.inwards_single = value
elif are_nodes_equal(value, instance.inwards_single):
instance.inwards_single = select_best_node(value, instance.inwards_single)
else:
warnings.warn(
f"Expected one edge for 'inwards_single' in {instance.as_id()}."
f"Ignoring new edge {value!s} in favor of {instance.inwards_single!s}."
)

for node in nodes_by_id.values():
if (
Expand Down Expand Up @@ -282,7 +290,7 @@ class ConnectionItemEWrite(DomainModelWrite):
space: str = DEFAULT_INSTANCE_SPACE
node_type: Union[dm.DirectRelationReference, None] = dm.DirectRelationReference("pygen-models", "ConnectionItemE")
direct_no_source: Union[str, dm.NodeId, None] = Field(default=None, alias="directNoSource")
inwards_single: Optional[list[Union[ConnectionItemDWrite, str, dm.NodeId]]] = Field(
inwards_single: Union[ConnectionItemDWrite, str, dm.NodeId, None] = Field(
default=None, repr=False, alias="inwardsSingle"
)
name: Optional[str] = None
Expand Down Expand Up @@ -328,13 +336,12 @@ def _to_instances_write(
resources.nodes.append(this_node)
cache.add(self.as_tuple_id())

edge_type = dm.DirectRelationReference("pygen-models", "bidirectionalSingle")
for inwards_single in self.inwards_single or []:
if self.inwards_single is not None:
other_resources = DomainRelationWrite.from_edge_to_resources(
cache,
start_node=inwards_single,
start_node=self.inwards_single,
end_node=self,
edge_type=edge_type,
edge_type=dm.DirectRelationReference("pygen-models", "bidirectionalSingle"),
write_none=write_none,
allow_version_increase=allow_version_increase,
)
Expand Down
2 changes: 1 addition & 1 deletion examples-pydantic-v1/windmill_pydantic_v1/_api/windmill.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def apply(
"""Add or update (upsert) windmills.
Note: This method iterates through all nodes and timeseries linked to windmill and creates them including the edges
between the nodes. For example, if any of `blades` or `metmast` are set, then these
between the nodes. For example, if any of `blades`, `metmast`, `nacelle` or `rotor` are set, then these
nodes as well as any nodes linked to them, and all the edges linking these nodes will be created.
Args:
Expand Down
2 changes: 1 addition & 1 deletion examples/omni/_api/connection_item_a.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def apply(
"""Add or update (upsert) connection item as.
Note: This method iterates through all nodes and timeseries linked to connection_item_a and creates them including the edges
between the nodes. For example, if any of `outwards` are set, then these
between the nodes. For example, if any of `other_direct`, `outwards` or `self_direct` are set, then these
nodes as well as any nodes linked to them, and all the edges linking these nodes will be created.
Args:
Expand Down
39 changes: 23 additions & 16 deletions examples/omni/data_classes/_connection_item_e.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class ConnectionItemEGraphQL(GraphQLCore):
direct_reverse_single: Optional[ConnectionItemDGraphQL] = Field(
default=None, repr=False, alias="directReverseSingle"
)
inwards_single: Optional[list[ConnectionItemDGraphQL]] = Field(default=None, repr=False, alias="inwardsSingle")
inwards_single: Optional[ConnectionItemDGraphQL] = Field(default=None, repr=False, alias="inwardsSingle")
name: Optional[str] = None

@model_validator(mode="before")
Expand Down Expand Up @@ -127,7 +127,9 @@ def as_read(self) -> ConnectionItemE:
if isinstance(self.direct_reverse_single, GraphQLCore)
else self.direct_reverse_single
),
inwards_single=[inwards_single.as_read() for inwards_single in self.inwards_single or []],
inwards_single=(
self.inwards_single.as_read() if isinstance(self.inwards_single, GraphQLCore) else self.inwards_single
),
name=self.name,
)

Expand All @@ -140,7 +142,9 @@ def as_write(self) -> ConnectionItemEWrite:
external_id=self.external_id,
data_record=DataRecordWrite(existing_version=0),
direct_no_source=self.direct_no_source,
inwards_single=[inwards_single.as_write() for inwards_single in self.inwards_single or []],
inwards_single=(
self.inwards_single.as_write() if isinstance(self.inwards_single, GraphQLCore) else self.inwards_single
),
name=self.name,
)

Expand Down Expand Up @@ -168,7 +172,7 @@ class ConnectionItemE(DomainModel):
direct_no_source: Union[str, dm.NodeId, None] = Field(default=None, alias="directNoSource")
direct_reverse_multi: Optional[list[ConnectionItemD]] = Field(default=None, repr=False, alias="directReverseMulti")
direct_reverse_single: Optional[ConnectionItemD] = Field(default=None, repr=False, alias="directReverseSingle")
inwards_single: Optional[list[Union[ConnectionItemD, str, dm.NodeId]]] = Field(
inwards_single: Union[ConnectionItemD, str, dm.NodeId, None] = Field(
default=None, repr=False, alias="inwardsSingle"
)
name: Optional[str] = None
Expand All @@ -180,10 +184,9 @@ def as_write(self) -> ConnectionItemEWrite:
external_id=self.external_id,
data_record=DataRecordWrite(existing_version=self.data_record.version),
direct_no_source=self.direct_no_source,
inwards_single=[
inwards_single.as_write() if isinstance(inwards_single, DomainModel) else inwards_single
for inwards_single in self.inwards_single or []
],
inwards_single=(
self.inwards_single.as_write() if isinstance(self.inwards_single, DomainModel) else self.inwards_single
),
name=self.name,
)

Expand All @@ -207,7 +210,6 @@ def _update_connections(

for instance in instances.values():
if edges := edges_by_source_node.get(instance.as_id()):
inwards_single: list[ConnectionItemD | str | dm.NodeId] = []
for edge in edges:
value: DomainModel | DomainRelation | str | dm.NodeId
if isinstance(edge, DomainRelation):
Expand All @@ -233,9 +235,15 @@ def _update_connections(
if edge_type == dm.DirectRelationReference("pygen-models", "bidirectionalSingle") and isinstance(
value, (ConnectionItemD, str, dm.NodeId)
):
inwards_single.append(value)

instance.inwards_single = inwards_single or None
if instance.inwards_single is None:
instance.inwards_single = value
elif are_nodes_equal(value, instance.inwards_single):
instance.inwards_single = select_best_node(value, instance.inwards_single)
else:
warnings.warn(
f"Expected one edge for 'inwards_single' in {instance.as_id()}."
f"Ignoring new edge {value!s} in favor of {instance.inwards_single!s}."
)

for node in nodes_by_id.values():
if (
Expand Down Expand Up @@ -330,13 +338,12 @@ def _to_instances_write(
resources.nodes.append(this_node)
cache.add(self.as_tuple_id())

edge_type = dm.DirectRelationReference("pygen-models", "bidirectionalSingle")
for inwards_single in self.inwards_single or []:
if self.inwards_single is not None:
other_resources = DomainRelationWrite.from_edge_to_resources(
cache,
start_node=inwards_single,
start_node=self.inwards_single,
end_node=self,
edge_type=edge_type,
edge_type=dm.DirectRelationReference("pygen-models", "bidirectionalSingle"),
write_none=write_none,
allow_version_increase=allow_version_increase,
)
Expand Down
2 changes: 1 addition & 1 deletion examples/windmill/_api/windmill.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def apply(
"""Add or update (upsert) windmills.
Note: This method iterates through all nodes and timeseries linked to windmill and creates them including the edges
between the nodes. For example, if any of `blades` or `metmast` are set, then these
between the nodes. For example, if any of `blades`, `metmast`, `nacelle` or `rotor` are set, then these
nodes as well as any nodes linked to them, and all the edges linking these nodes will be created.
Args:
Expand Down

0 comments on commit d686f04

Please sign in to comment.