Skip to content

Commit

Permalink
Add reconvert command to merge PVs into an existing Device
Browse files Browse the repository at this point in the history
  • Loading branch information
GDYendell committed Jul 11, 2024
1 parent 77c364f commit b6671be
Show file tree
Hide file tree
Showing 6 changed files with 1,113 additions and 10 deletions.
19 changes: 19 additions & 0 deletions src/pvi/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,25 @@ def regroup(
device.serialize(device_path)


@app.command()
def reconvert(
device_path: Annotated[
Path, typer.Argument(..., help="Path to the device.yaml file to add to")
],
templates: Annotated[
list[Path],
typer.Option(..., "--template", help="Paths of templates to add PVs from"),
],
):
"""Add PVs to an existing device.yaml file based on extra / updated templates."""
device = Device.deserialize(device_path)
template_components = TemplateConverter(templates).convert()

device.merge_components(template_components)

device.serialize(device_path)


# test with: pipenv run python -m pvi
if __name__ == "__main__":
app()
32 changes: 22 additions & 10 deletions src/pvi/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ class Component(Named):
def get_label(self):
return self.label or to_title_case(self.name)

def __eq__(self, other: object) -> bool:
return isinstance(other, Component) and self.name == other.name


class Signal(Component, AccessModeMixin):
"""Base signal type representing one or two PVs of a `Device`."""
Expand Down Expand Up @@ -492,23 +495,32 @@ def deserialize_parents(self, yaml_paths: list[Path]):
if self.parent is None or self.parent == "asynPortDriver":
return

parent_parameters = find_components(self.parent, yaml_paths)
for node in parent_parameters:
parent_components = find_components(self.parent, yaml_paths)
self.merge_components(parent_components)

Check warning on line 499 in src/pvi/device.py

View check run for this annotation

Codecov / codecov/patch

src/pvi/device.py#L498-L499

Added lines #L498 - L499 were not covered by tests

def merge_components(self, components: Tree) -> None:
existing_components = list(walk(self.children))
for node in components:
if isinstance(node, Group):
for param_group in self.children:
if not isinstance(param_group, Group):
for group in self.children:
if not isinstance(group, Group):
continue
elif param_group.name == node.name:
param_group.children = list(node.children) + list(
param_group.children
)
break # Groups merged - skip to next parent group
elif group.name == node.name:
group.children = list(node.children) + list(group.children)
break # Groups merged - skip to next group

Check warning on line 510 in src/pvi/device.py

View check run for this annotation

Codecov / codecov/patch

src/pvi/device.py#L509-L510

Added lines #L509 - L510 were not covered by tests

else: # No break - Did not find the Group
# Remove any signals already in device
new_components: list[ComponentUnion] = []
for component in walk([node]):
if component not in existing_components:
new_components.append(component)
node.children = new_components

# Inherit as a new Group
self.children = list(self.children) + [node]
continue # Skip to next parent group

continue # Skip to next group
else:
# Node is an individual AsynParameter - just append it
self.children = list(self.children) + [node]
Expand Down
1 change: 1 addition & 0 deletions tests/reconvert/input/simDetector.pvi.device.yaml
Loading

0 comments on commit b6671be

Please sign in to comment.