-
Notifications
You must be signed in to change notification settings - Fork 3
Ticket3396 show global macros #450
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
base: master
Are you sure you want to change the base?
Changes from all commits
d0964b1
ae34053
9a64ced
2fb2aa5
964a846
f7246bd
5cc6e1a
71873df
17776be
f4aa705
615ff30
7aa2e58
58a35af
e01cebb
6521e05
7d6e2af
6e3aa66
7b09e37
cd5551d
4d471c7
06ce22e
38771f7
3f33243
60c3e35
60e0e91
5da0e16
9c5963e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| # This file is part of the ISIS IBEX application. | ||
| # Copyright (C) 2012-2025 Science & Technology Facilities Council. | ||
| # All rights reserved. | ||
| # | ||
| # This program is distributed in the hope that it will be useful. | ||
| # This program and the accompanying materials are made available under the | ||
| # terms of the Eclipse Public License v1.0 which accompanies this distribution. | ||
| # EXCEPT AS EXPRESSLY SET FORTH IN THE ECLIPSE PUBLIC LICENSE V1.0, THE PROGRAM | ||
| # AND ACCOMPANYING MATERIALS ARE PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES | ||
| # OR CONDITIONS OF ANY KIND. See the Eclipse Public License v1.0 for more details. | ||
| # | ||
| # You should have received a copy of the Eclipse Public License v1.0 | ||
| # along with this program; if not, you can obtain a copy from | ||
| # https://www.eclipse.org/org/documents/epl-v10.php or | ||
| # http://opensource.org/licenses/eclipse-1.0.php | ||
|
|
||
| import copy | ||
| from collections import OrderedDict | ||
| from typing import Any | ||
|
|
||
| from server_common.utilities import print_and_log | ||
|
|
||
|
|
||
| class Globalmacro: | ||
| """Represents an IOC with its global macros. | ||
|
|
||
| Attributes: | ||
| name (string): The name of the IOC | ||
| macros (dict): The IOC's macros | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| name: str, | ||
| macros: dict[str, str] | None, | ||
| ) -> None: | ||
| """Constructor. | ||
|
|
||
| Args: | ||
| name: The name of the IOC | ||
| macros: The IOC's macros | ||
| """ | ||
| self.name = name | ||
|
|
||
| if macros is None: | ||
| self.macros = {} | ||
| else: | ||
| self.macros = macros | ||
|
|
||
| @staticmethod | ||
| def _dict_to_list(in_dict: dict[str, Any]) -> list[Any]: | ||
| """Converts into a format better for the GUI to parse, namely a list. | ||
|
|
||
| Args: | ||
| in_dict: The dictionary to be converted | ||
|
|
||
| Returns: | ||
| The newly created list | ||
| """ | ||
| out_list = [] | ||
| if in_dict: | ||
| for k, v in in_dict.items(): | ||
| # Take a copy as we do not want to modify the original | ||
| c = copy.deepcopy(v) | ||
| c["name"] = k | ||
| out_list.append(c) | ||
| return out_list | ||
|
|
||
| def __str__(self) -> str: | ||
| return f"{self.__class__.__name__}(name={self.name})" | ||
|
|
||
| def to_dict(self) -> dict[str, str | dict[str, str]]: | ||
| """Puts the IOC-globalmacro's details into a dictionary. | ||
|
|
||
| Returns: | ||
| The IOC-Global Macros' details | ||
| """ | ||
| return { | ||
| "name": self.name, | ||
| "macros": self.macros, | ||
| } | ||
|
|
||
|
|
||
| class GlobalmacroHelper: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: does it need to be a class with one method at all - can it just be a free function?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a helper class, now with two methods |
||
| """Converts global macro data to Globalmacro Object.""" | ||
|
|
||
| globalmacros = OrderedDict() | ||
|
|
||
| @staticmethod | ||
| def row_to_globalmacro(globalmacros: dict, row: str) -> None: | ||
| """converts a row from the globals file to globalmacro data. | ||
|
|
||
| Args: | ||
| globalmacros: The current list of global macros | ||
| row: The IOC's (or All IOCs) global macro record | ||
| """ | ||
| ioc_separator = "__" | ||
| equal_to = "=" | ||
| all_iocs = "<ALL IOCS>" | ||
| # Each record is of the form IOC__MACRO=VALUE | ||
| # Where there is no __ the Macro is applicable for all IOCs | ||
| if equal_to in row: | ||
| ioc_macro, value = row.rsplit(equal_to, maxsplit=1) | ||
| to_add_ioc = {} | ||
| if ioc_separator in ioc_macro: | ||
| ioc, macro = ioc_macro.split(ioc_separator, maxsplit=1) | ||
| else: | ||
| ioc = all_iocs | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should: We seem to have a "special" key of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changes done |
||
| macro = ioc_macro | ||
|
|
||
| if ioc in globalmacros: | ||
| to_add_ioc = globalmacros[ioc] | ||
| to_add_ioc[macro] = value.strip() | ||
| globalmacros[ioc] = to_add_ioc | ||
|
|
||
| @staticmethod | ||
| def add_globalmacro(name: str, macros: dict) -> None: | ||
| """Add an IOC with its global macros to the configuration. | ||
|
|
||
| Args: | ||
| name (string): The name of the IOC to add | ||
| macros: The macro sets relating to the IOC | ||
|
|
||
| """ | ||
| # Only add it if it has not been added before | ||
| if name.upper() in GlobalmacroHelper.globalmacros.keys(): | ||
| print_and_log( | ||
| f"Warning: IOC '{name}' is already part of the configuration. Not adding it again." | ||
| ) | ||
| else: | ||
| GlobalmacroHelper.globalmacros[name.upper()] = Globalmacro(name, macros) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should:
macrosnot type-hinted as being able to be set to None.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes done