Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow changing values of metadata labels #157

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 39 additions & 20 deletions src/pandablocks_ioc/ioc.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,34 @@

values, all_values_dict = _create_dicts_from_changes(changes, block_dict)

panda_dict = {}
for (block_name, block_info), field_info in zip(
block_dict.items(), field_infos, strict=False
):
panda_dict[block_name] = _BlockAndFieldInfo(
block_info=block_info, fields=field_info, values=values[block_name]
panda_dict = {
block_name: _BlockAndFieldInfo(block_info, field_info, values[block_name])
for (block_name, block_info), field_info in zip(
block_dict.items(), field_infos, strict=False
)
}

return (panda_dict, all_values_dict)


def extract_label_from_metadata(block_name_number, field_name: str):
# Parse *METADATA.LABEL_<block><num> into "<block>" key and
# "<block><num>:LABEL" value
if block_name_number.startswith("*METADATA") and field_name.startswith("LABEL_"):
_, block_name_number = field_name.split("_", maxsplit=1)

# The block is fixed with metadata, it should end with a number
# "*METADATA.LABEL_SEQ2": "NewSeqMetadataLabel",
if not block_name_number[-1].isdigit():
raise ValueError(
f"Recieved metadata for a block name {block_name_number} that "
"didn't contain a number"
)

return block_name_number
return None


def _create_dicts_from_changes(
changes: Changes, block_info_dict: dict[str, BlockInfo]
) -> tuple[dict[str, dict[EpicsName, RecordValue]], dict[EpicsName, RecordValue]]:
Expand Down Expand Up @@ -298,22 +315,12 @@

block_name_number, field_name = block_and_field_name.split(".", maxsplit=1)

# Parse *METADATA.LABEL_<block><num> into "<block>" key and
# "<block><num>:LABEL" value
if block_name_number.startswith("*METADATA") and field_name.startswith(
"LABEL_"
if label_block_name_number := extract_label_from_metadata(
block_name_number, field_name
):
_, block_name_number = field_name.split("_", maxsplit=1)

# The block is fixed with metadata, it should end with a number
# "*METADATA.LABEL_SEQ2": "NewSeqMetadataLabel",
if not block_name_number[-1].isdigit():
raise ValueError(
f"Recieved metadata for a block name {block_name_number} that "
"didn't contain a number"
)
block_name_number = label_block_name_number
block_name_no_number = re.sub(r"\d*$", "", label_block_name_number)

block_name_no_number = re.sub(r"\d*$", "", block_name_number)
number_of_blocks = block_info_dict[block_name_no_number].number

if number_of_blocks == 1:
Expand Down Expand Up @@ -2095,6 +2102,18 @@
field = PandAName(field)
field = panda_to_epics_name(field)

if block_label := extract_label_from_metadata(
*field.split(":", maxsplit=1)
):
block_label_no_number = re.sub(r"\d*$", "", block_label)
block_label = f"{block_label}:LABEL"
block_label_no_number = f"{block_label_no_number}:LABEL"

if block_label_no_number in all_records:
field = block_label_no_number

Check warning on line 2113 in src/pandablocks_ioc/ioc.py

View check run for this annotation

Codecov / codecov/patch

src/pandablocks_ioc/ioc.py#L2113

Added line #L2113 was not covered by tests
elif block_label in all_records:
field = block_label

Check warning on line 2115 in src/pandablocks_ioc/ioc.py

View check run for this annotation

Codecov / codecov/patch

src/pandablocks_ioc/ioc.py#L2115

Added line #L2115 was not covered by tests

if field not in all_records:
logging.error(
f"Unknown field {field} returned from GetChanges values"
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/mocked_panda.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ def standard_responses(table_field_info, table_data_1, table_data_2):
values={
"PCAP.TRIG_EDGE": "Either",
"PULSE.DELAY.UNITS": "s",
"*METADATA.LABEL_PULSE1": "ANewLabel",
evalott100 marked this conversation as resolved.
Show resolved Hide resolved
},
multiline_values={"SEQ.TABLE": table_data_2},
),
Expand Down
Loading