Parser Helpers
apply_rewrite_rule(rewrite_rule, arg_list)
Apply a rewrite rule to an argument list. Rewrite rules often look like '$(A),$(A).SEVR', which can expand a single argument into multiple arguments by replacing placeholders.
For example
rewrite_rule = '$(A),$(A).SEVR' arg_list = ['myPV']
=> new_args = ['myPV', 'myPV.SEVR']
Parameters
rewrite_rule : str The rewrite rule, without the leading '@' (e.g. '$(A),$(A).SEVR'). arg_list : List[str] The list of arguments to which we apply the rule.
Returns
List[str] A new list of arguments produced by the rewrite rule.
Source code in pydmconverter/edm/parser_helpers.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
clean_escape_characters(expr)
Remove extra ' characters from CALC/LOC expressions.
Parameters
expression : str The expression to be cleaned.
Returns
str The new expression with \s removed.
Source code in pydmconverter/edm/parser_helpers.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
convert_color_property_to_qcolor(fillColor, color_data)
Convert the EDM 'fillColor', 'bgColor', 'fgColor' property into a tuple representing RGBA values.
Returns:
| Type | Description |
|---|---|
Optional[Tuple[int, int, int, int]]
|
Optional[Tuple[int, int, int, int]]: A tuple (red, green, blue, alpha) or None. |
Source code in pydmconverter/edm/parser_helpers.py
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 | |
get_color_by_index(color_data, index)
Retrieve the color definition from color_data using an index string like 'index 3'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color_data
|
Dict[str, Any]
|
The parsed colors.list data. |
required |
index
|
str
|
The color index string, e.g., 'index 3'. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
Optional[Dict[str, Any]]: The corresponding color dictionary (expected to have an 'rgb' key) or None if not found. |
Source code in pydmconverter/edm/parser_helpers.py
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 | |
get_color_by_rgb(colorStr)
Retrieve the color definition from color_data using an rgb string like 'rgb 0 0 0'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
colorStr
|
str
|
The color index string, e.g., 'rgb 0 0 0'. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
Optional[Dict[str, Any]]: The corresponding color dictionary (expected to have an 'rgb' key) or None if not found. |
Source code in pydmconverter/edm/parser_helpers.py
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 | |
loc_conversion(edm_string)
Convert an EDM local PV string to a PyDM local variable string, mapping types to supported PyDM types.
Supported PyDM types: - int - float - str - array (numpy.ndarray)
Enum ('e') is mapped to int for simplicity in this conversion.
Parameters
edm_string : str EDM local PV string to be converted. Expected format is: "LOC\name=type:value" (with optional scope modifiers and ignoring special functions).
Returns
pydm_string : str
Corresponding PyDM string in the format:
"loc://
Raises
ValueError If the EDM string does not start with 'LOC\' or if it lacks the proper format.
Source code in pydmconverter/edm/parser_helpers.py
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | |
parse_calc_list(calc_list_path)
Parse an EPICS-style calc.list file and return a dictionary mapping calculation names to a tuple of (rewrite_rule, expression).
The file format typically looks like
CALC1 ...
# ...
<calc_name>
[@rewrite_rule]
<expression>
# ...
Parameters
calc_list_path : str Path to the calc.list file.
Returns
Dict[str, Tuple[Optional[str], Optional[str]]] A dictionary with keys as calculation names (e.g. 'sum', 'diff') and values as a 2-tuple (rewrite_rule, expression). Both are optional: rewrite_rule may be None if not present, and expression may also be None if the file has incomplete entries.
Source code in pydmconverter/edm/parser_helpers.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
parse_calc_pv(edm_pv)
Parse an EDM-style CALC PV reference and extract the calculation name (or inline expression), list of arguments, and whether it's an inline expression (curly braces).
EDM CALC PV examples
- 'CALC\sum(pv1, pv2)'
- 'CALC\{A+B}(pv1, pv2)'
- 'CALC\{(A)}($(P)$(R)Acquire)'
Parameters
edm_pv : str The EDM-style CALC PV string to parse. For example, 'CALC\sum(pv1, pv2)'.
Returns
calc_name_or_expr : str The calculation name (e.g. 'sum') or inline expression (e.g. 'A+B'). arg_list : List[str] The list of arguments, e.g. ['pv1', 'pv2']. is_inline_expr : bool True if the EDM PV used inline curly brace syntax (e.g. '{A+B}'), False otherwise.
Raises
ValueError If the given edm_pv string doesn't match the expected CALC syntax.
Source code in pydmconverter/edm/parser_helpers.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
parse_colors_list(filepath)
Parse an EDM colors.list file into a structured Python dictionary.
Parameters
filepath : str
Path to the colors.list file.
Returns
Dict[str, Any]
A dictionary representing the parsed content of the colors.list file.
Keys
----
version : Dict[str, int]
Dictionary containing {"major", "minor", "release"}.
blinkms : int or None
The blink period in milliseconds.
columns : int or None
Number of columns in the color palette.
max : int or None
The maximum RGB component value + 1 (e.g. 256 or 0x10000).
alias : Dict[str, str]
Maps an alias name to a (static or rule-based) color name.
static : Dict[int, Dict[str, Union[str, List[int]]]]
Static color definitions keyed by their numeric index.
Each value is a dictionary containing:
- "name": str
- "rgb": List[int] (3 values) or 6 values if blinking
rules : Dict[int, Dict[str, Any]]
Rule definitions keyed by their numeric index.
Each value is a dictionary containing:
- "name": str
- "conditions": List[Dict[str, str]]
Each condition has:
- "condition": str (e.g. ">0 && <10" or "default")
- "color": str
menumap : List[str]
List of color names as displayed in the color name menu.
alarm : Dict[str, str]
Alarm color configuration. Keys are alarm states, values are color names.
Notes
- The first non-comment, non-empty line must be the version line: e.g. "4 0 0".
- The parser assumes a well-formed file. If your file structure differs, you may need to handle additional edge cases (e.g. malformed lines, trailing braces, etc.).
- A “blinking” static color has six numeric components for its two RGB states.
- A rule line has the form: rule
{ ... }. - The menumap and alarm blocks must each be enclosed in braces.
- The alias lines have the form: alias
.
Source code in pydmconverter/edm/parser_helpers.py
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 | |
parse_edm_macros(macro_string)
Parse an EDM macro string into a dictionary for PyDM widgets.
EDM macros are in the format: "KEY1=value1,KEY2=value2,KEY3=value3" This function converts them to a Python dict: {"KEY1": "value1", "KEY2": "value2", "KEY3": "value3"}
Parameters
macro_string : str The EDM macro string to parse (e.g., "P=CAMR:LI20:110,R=:ASYN")
Returns
dict A dictionary containing the parsed macro key-value pairs. Returns an empty dict if the input is empty or None.
Examples
parse_edm_macros("P=CAMR:LI20:110,R=:ASYN") {'P': 'CAMR:LI20:110', 'R': ':ASYN'} parse_edm_macros("DEVICE=IOC:SYS0:1") {'DEVICE': 'IOC:SYS0:1'} parse_edm_macros("") {}
Source code in pydmconverter/edm/parser_helpers.py
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 | |
reformat_calc_expression(exp)
Convert EPICS calc expression operators to Python equivalents.
PyDM's calc plugin evaluates the expr query as a Python expression, so
EPICS-only operators must be translated or PyDM raises a SyntaxError when the
display is opened.
Conversions:
- ^ exponentiation -> **
- # not equal -> !=
- = equality -> == (leaving <=, >=, !=, == untouched)
- && / || -> and / or
- cond ? a : b ternary -> (a if cond else b) (handles nesting)
Source code in pydmconverter/edm/parser_helpers.py
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
replace_calc_and_loc_in_edm_content(edm_content, filepath, calc_list_file=None)
Replace both CALC...(...) and LOC...=... references in the EDM file content with PyDM equivalents. The first time each unique reference appears, the replacement is the full PyDM string; subsequent appearances use the short form.
Parameters
edm_content : str The full text of the EDM file, as a single string. filepath : str path of the given edm file calc_list_file : str, optional An explicit path to a calc.list file used to resolve named CALC PVs. Overrides the default calc.list search locations.
Returns
new_content : str The EDM content after all CALC and LOC references have been replaced. encountered_calcs : Dict[str, Dict[str, str]] A dictionary of all encountered CALC references, mapping the original EDM reference to {"full": ..., "short": ...}. encountered_locs : Dict[str, Dict[str, str]] A dictionary of all encountered LOC references, similarly mapping each unique original LOC reference to "full" and "short" addresses.
Source code in pydmconverter/edm/parser_helpers.py
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 | |
search_calc_list(file_path, cli_calc_file=None)
Search for a calc.list file and return its full path, returns None if no calc.list is found.
The search checks the following locations in order:
- cli_calc_file, if provided.
- calc.list in the same directory as file_path.
- calc.list in a config subdirectory next to file_path.
- $EDMFILES/calc.list.
Parameters
file_path : str The path to a file whose directory will be searched for 'calc.list'. cli_calc_file : str, optional An explicit path to a calc.list file (e.g. from the --calc-list CLI option). If provided and valid, it overrides the other locations.
Returns
Optional[str] The full path to the found 'calc.list' file if it exists; otherwise, None.
Source code in pydmconverter/edm/parser_helpers.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
search_color_list(cli_color_file=None)
Attempt to find the EDM color file by the following priority:
- CLI argument (cli_color_file), if provided.
- EDMCOLORFILE env variable (absolute path).
- EDMFILES env variable + "colors.list".
- Default path: "/etc/edm/colors.list".
Parameters
cli_color_file : str or None, optional A file path passed via command line argument. If this is provided and valid, it overrides other checks.
Returns
str or None The path to the EDM color file if found, else None.
Source code in pydmconverter/edm/parser_helpers.py
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 | |
translate_calc_pv_to_pydm(edm_pv, calc_dict=None, default_prefix='ca://')
Translate an EDM-style CALC PV (e.g., 'CALC\sum(pv1, pv2)') into a PyDM calc plugin address, e.g.:
calc://my_variable_name?A=channel://pv1&B=channel://pv2&expr=A+B
Parameters
edm_pv : str
The CALC PV in EDM syntax. For instance, "CALC\sum(pv1, pv2)"
or "CALC{A-B}(myPv, 10.5)".
calc_dict : dict, optional
A dictionary mapping calculation names to (rewrite_rule, expression).
Typically from parse_calc_list(). This is required if the CALC PV
references a named calc (e.g. 'sum') that is defined in a calc.list file.
If the CALC PV uses an inline expression ({A+B}), this dictionary
may be omitted.
default_prefix : str, optional
A prefix to apply to each argument if it doesn't already include a protocol.
Defaults to 'channel://'.
Returns
str
A PyDM calc plugin address string in the format:
'calc://
Raises
ValueError
If the named calculation does not exist in the provided calc_dict.
Source code in pydmconverter/edm/parser_helpers.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |