Skip to content

Commit

Permalink
Run ruff --fix
Browse files Browse the repository at this point in the history
  • Loading branch information
LowriJenkins committed Jul 12, 2024
1 parent 013496d commit b5609f1
Show file tree
Hide file tree
Showing 40 changed files with 196 additions and 302 deletions.
4 changes: 3 additions & 1 deletion check_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import upgrade, sys
import sys

import upgrade


def compare_version_number(version_to_check):
Expand Down
3 changes: 2 additions & 1 deletion run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
os.environ["MYDIRBLOCK"] = ".."
sys.path.insert(0, os.path.abspath(".."))
# Standard imports
import argparse
import unittest

import xmlrunner
import argparse

DEFAULT_DIRECTORY = os.path.join("..", "..", "..", "test-reports")

Expand Down
20 changes: 6 additions & 14 deletions src/common_upgrades/add_to_base_iocs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from xml.dom import minidom
from xml.parsers.expat import ExpatError

from src.local_logger import LocalLogger

IOC_FILENAME = "configurations\components\_base\iocs.xml"

FILE_TO_CHECK_STR = "IOC default component file"
Expand All @@ -12,8 +10,7 @@


class AddToBaseIOCs:
"""
Add the ioc autostart to _base ioc so that it autostarts
"""Add the ioc autostart to _base ioc so that it autostarts
"""

def __init__(self, ioc_to_add, add_after_ioc, xml_to_add):
Expand All @@ -22,8 +19,7 @@ def __init__(self, ioc_to_add, add_after_ioc, xml_to_add):
self._xml_to_add = xml_to_add

def perform(self, file_access, logger):
"""
Add the autostart of the given.
"""Add the autostart of the given.
Args:
file_access (FileAccess): file access.
Expand Down Expand Up @@ -60,8 +56,7 @@ def perform(self, file_access, logger):

@staticmethod
def _get_ioc_names(xml):
"""
Gets the names of all the iocs in the xml.
"""Gets the names of all the iocs in the xml.
Args:
xml: XML to check.
Expand All @@ -72,8 +67,7 @@ def _get_ioc_names(xml):
return [ioc.getAttribute("name") for ioc in xml.getElementsByTagName("ioc")]

def _check_final_file_contains_one_of_added_ioc(self, logger, xml):
"""
Check the file to make sure it now contains one and only one ioc added entry.
"""Check the file to make sure it now contains one and only one ioc added entry.
Args:
logger (Logger): Logger to write to.
Expand All @@ -92,8 +86,7 @@ def _check_final_file_contains_one_of_added_ioc(self, logger, xml):
return True

def _check_prerequistes_for_file(self, xml, logger):
"""
Check the file can be modified.
"""Check the file can be modified.
Args:
xml: XML to check
Expand All @@ -117,8 +110,7 @@ def _check_prerequistes_for_file(self, xml, logger):
return True

def _add_ioc(self, ioc_xml, logger):
"""
Add IOC entry after add after ioc specified if it exists.
"""Add IOC entry after add after ioc specified if it exists.
Args:
ioc_xml: XML to add to.
Expand Down
39 changes: 11 additions & 28 deletions src/common_upgrades/change_macro_in_globals.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import re

from src.common_upgrades.utils.constants import GLOBALS_FILENAME


class ChangeMacroInGlobals(object):
"""
An interface to replace arbitrary macros in a globals.txt file
"""An interface to replace arbitrary macros in a globals.txt file
"""

def __init__(self, file_access, logger):
"""
Initialise.
"""Initialise.
Args:
file_access: Object to allow for file access.
Expand All @@ -20,22 +19,19 @@ def __init__(self, file_access, logger):
self._loaded_file = self.load_globals_file()

def load_globals_file(self):
"""
Loads in a globals file as a list of strings.
"""Loads in a globals file as a list of strings.
Returns:
Globals file loaded as list of strings if globals file exists.
Empty list otherwise.
"""

if self._file_access.exists(GLOBALS_FILENAME):
return self._file_access.open_file(GLOBALS_FILENAME)
else:
return []

def change_macros(self, ioc_name, macros_to_change):
"""
Changes a list of macros in the globals.txt file for a specific IOC.
"""Changes a list of macros in the globals.txt file for a specific IOC.
Args:
ioc_name: Name of the IOC.
Expand All @@ -45,16 +41,14 @@ def change_macros(self, ioc_name, macros_to_change):
Returns:
None
"""

for old_macro, new_macro in macros_to_change:
for index in self._globals_filter_generator(ioc_name):
self._apply_regex_macro_change(ioc_name, old_macro, new_macro, index)

self.write_modified_globals_file()

def change_ioc_name(self, old_ioc_name, new_ioc_name):
"""
Changes the name of an IOC in a globals.txt file.
"""Changes the name of an IOC in a globals.txt file.
Args:
old_ioc_name: String, the old name of the IOC
Expand All @@ -64,15 +58,13 @@ def change_ioc_name(self, old_ioc_name, new_ioc_name):
None
"""

for index in self._globals_filter_generator(old_ioc_name):
self._change_ioc_name(old_ioc_name, new_ioc_name, index)

self.write_modified_globals_file()

def _globals_filter_generator(self, ioc_to_change):
"""
Returns lines containing specified IOCs from globals.txt
"""Returns lines containing specified IOCs from globals.txt
Generator that gives all the lines for a given IOC in globals.txt.
This will match IOCs with the same name as the root plus any that have a number
Expand All @@ -84,15 +76,13 @@ def _globals_filter_generator(self, ioc_to_change):
Yields:
Index that the ioc is on.
"""

for index, line in enumerate(self._loaded_file):
if line.startswith("{}_".format(ioc_to_change)):
self._logger.info("Found line '{}' in {}".format(line, GLOBALS_FILENAME))
yield index

def _determine_replacement_values(self, old_macro, new_macro):
"""
Determines the strings to search for and replace.
"""Determines the strings to search for and replace.
Args:
old_macro: Old Macro object with old macro name and old macro value.
Expand All @@ -101,7 +91,6 @@ def _determine_replacement_values(self, old_macro, new_macro):
Returns:
regex_changes: Dictionary of regex representations of the strings to search for/replace.
"""

if old_macro.value is None:
old_value_search = r".*"

Expand All @@ -123,16 +112,14 @@ def _determine_replacement_values(self, old_macro, new_macro):
return regex_changes

def _apply_regex_macro_change(self, ioc_name, old_macro, new_macro, line_number):
"""
Applies a regular expression to modify a macro.
"""Applies a regular expression to modify a macro.
Args:
ioc_name: Name of the IOC to
Returns:
None
"""

regex_args = self._determine_replacement_values(old_macro, new_macro)

replace_regex = re.compile(
Expand All @@ -150,8 +137,7 @@ def _apply_regex_macro_change(self, ioc_name, old_macro, new_macro, line_number)
)

def _change_ioc_name(self, ioc_name, new_ioc_name, line_number):
"""
If a new name is supplied, changes the name of the IOC
"""If a new name is supplied, changes the name of the IOC
Args:
ioc_name: String, the current name of the IOC
Expand All @@ -160,19 +146,16 @@ def _change_ioc_name(self, ioc_name, new_ioc_name, line_number):
Returns:
None
"""

if new_ioc_name is not None:
self._loaded_file[line_number] = self._loaded_file[line_number].replace(
ioc_name, new_ioc_name.upper()
)

def write_modified_globals_file(self):
"""
Writes the modified globals file if it has been loaded.
"""Writes the modified globals file if it has been loaded.
Returns:
None
"""

if self._loaded_file:
self._file_access.write_file(GLOBALS_FILENAME, self._loaded_file)
31 changes: 11 additions & 20 deletions src/common_upgrades/change_macros_in_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@


def change_macro_name(macro, old_macro_name, new_macro_name):
"""
Changes the macro name of a macro xml node.
"""Changes the macro name of a macro xml node.
Args:
macro : The macro node to change.
Expand All @@ -19,8 +18,7 @@ def change_macro_name(macro, old_macro_name, new_macro_name):


def change_macro_value(macro, old_macro_value, new_macro_value):
"""
Changes the macros in the given xml if a new macro value is given.
"""Changes the macros in the given xml if a new macro value is given.
Args:
macro : The macro xml node to change.
Expand All @@ -36,8 +34,7 @@ def change_macro_value(macro, old_macro_value, new_macro_value):


def find_macro_with_name(macros, name_to_find):
"""
Find whether macro with name attribute equal to argument name_to_find exists
"""Find whether macro with name attribute equal to argument name_to_find exists
Args:
macros: XML element containing list of macros
Expand All @@ -52,13 +49,11 @@ def find_macro_with_name(macros, name_to_find):


class ChangeMacrosInXML(object):
"""
Changes macros in XML files.
"""Changes macros in XML files.
"""

def __init__(self, file_access, logger):
"""
Initialise.
"""Initialise.
Args:
file_access: Object to allow for file access.
Expand All @@ -70,8 +65,7 @@ def __init__(self, file_access, logger):
def add_macro(
self, ioc_name, macro_to_add, pattern, description="No description", default_value=None
):
"""
Add a macro with a specified name and value to all IOCs whose name begins with ioc_name, unless a macro
"""Add a macro with a specified name and value to all IOCs whose name begins with ioc_name, unless a macro
with that name already exists
Args:
Expand All @@ -96,12 +90,12 @@ def add_macro(
self._file_access.write_xml_file(path, ioc_xml)

def change_macros(self, ioc_name, macros_to_change):
"""
Changes macros in all xml files that contain the correct macros for a specified ioc.
"""Changes macros in all xml files that contain the correct macros for a specified ioc.
Args:
ioc_name: Name of the IOC to change macros within.
macros_to_change: List of 2-tuples of old_macro and new_macro Macro classes.
Returns:
None.
"""
Expand All @@ -119,8 +113,7 @@ def change_macros(self, ioc_name, macros_to_change):
self._file_access.write_xml_file(path, ioc_xml)

def change_ioc_name(self, old_ioc_name, new_ioc_name):
"""
Replaces all instances of old_ioc_name with new_ioc_name in an XML tree
"""Replaces all instances of old_ioc_name with new_ioc_name in an XML tree
Args:
old_ioc_name: String, the old ioc prefix (without _XX number suffix)
new_ioc_name: String, The desired new IOC prefix (without _XX number suffix)
Expand All @@ -140,8 +133,7 @@ def change_ioc_name(self, old_ioc_name, new_ioc_name):
self._file_access.write_xml_file(path, ioc_xml)

def change_ioc_name_in_synoptics(self, old_ioc_name, new_ioc_name):
"""
Replaces instances of old_ioc_name with new_ioc_name
"""Replaces instances of old_ioc_name with new_ioc_name
Args:
old_ioc_name: String, the old ioc prefix (without _XX number suffix)
Expand Down Expand Up @@ -176,8 +168,7 @@ def change_ioc_name_in_synoptics(self, old_ioc_name, new_ioc_name):
self._file_access.write_xml_file(xml_path, synoptic_xml)

def ioc_tag_generator(self, path, ioc_xml, ioc_to_change):
"""
Generator giving all the IOC tags in all configurations.
"""Generator giving all the IOC tags in all configurations.
Args:
path: Path to the xml file
Expand Down
Loading

0 comments on commit b5609f1

Please sign in to comment.