Skip to content

Commit

Permalink
refactor: Restructure semantic browser auto-refresh disabling
Browse files Browse the repository at this point in the history
- Replace hardcoded workspace directory with env variable
- Use functions for better readibility
- Use context manager and `enumerate` for file iteration
- Use `replace` statement to replace old with new
  • Loading branch information
MoritzWeber0 committed Oct 8, 2024
1 parent 3772d25 commit c4c9b69
Showing 1 changed file with 66 additions and 54 deletions.
120 changes: 66 additions & 54 deletions capella/setup/disable_semantic_browser_auto_refresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,85 +18,97 @@
"application": "http://www.eclipse.org/ui/2010/UIModel/application",
"menu": "http://www.eclipse.org/ui/2010/UIModel/application/ui/menu",
}
XPATH_EXPR = (
"//persistedState[@key='memento' and"
" contains(@value, 'listeningToWorkbenchPageSelectionEvents')]"
)
WS = "/home/techuser/workspace"
XPATH_EXPR = "//persistedState[@key='memento' and contains(@value, 'listeningToWorkbenchPageSelectionEvents')]"
WORKSPACE_DIR = os.getenv("WORKSPACE_DIR", "/workspace")

logging.basicConfig(level=os.getenv("LOG_LEVEL", "INFO"))
logger = logging.getLogger(__file__)

# we do something if it is configured like that to keep with defaults
# and act only, if deviating from defaults has been configured
if os.getenv("CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH", "0") != "1":
sys.exit(0)
logger.debug(
"Identified that the environment variable"
" `CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH` is set to `1`."
" We will disable the auto-refresh of the semantic browser."
)
logger.debug("Expecting the workspace to be at: `%s`", WS)
FILE_PATH = pathlib.Path(
f"{WS}/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi"
)
if not FILE_PATH.is_file():

def check_environment_variable() -> None:
if os.getenv(
"CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH", "0"
).lower() in (
"1",
"true",
"t",
):
logger.info("The semantic browser auto refresh will not be disabled.")
sys.exit(0)

logger.debug(
"File not found: `%s`."
" Cannot disable auto-refresh of semantic browser.",
FILE_PATH,
"Identified that the environment variable "

This comment has been minimized.

Copy link
@jamilraichouni

jamilraichouni Oct 8, 2024

Contributor

Used to do long strings like shown by you and moved to putting the blank at the beginning of "next" string lines. this makes sense, because when you want to remove a line with line_no > 1, you can just remove the line and must not also remove a blank at the end of the previous line.

better:

    logger.debug(
        "Identified that the environment variable"
        " `CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH` is set to `1`."
        " We will disable the auto-refresh of the semantic browser."
    )

This comment has been minimized.

Copy link
@MoritzWeber0

MoritzWeber0 Oct 9, 2024

Author Member

With this argumentation we could also argue that after removing the first line the user has to remove the leading space of the next line. So it's either for all lines with line_no > 1 in your case or for all line numbers line_no > len() - 1 currently, so not really a difference. And even if it would be difference, how often would that actually happen and does a trailing whitespace hurt at all? It's better than a forgotten leading whitespace.

Looking at the code proposal with leading whitespace hurts my eyes since the lines aren't aligned, but I assume that's just personal preference and there is no official style for it.

"`CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH` is set to `1`. "
"We will disable the auto-refresh of the semantic browser."
)
sys.exit(0)


def main() -> None:
tree = etree.parse(FILE_PATH)
root = tree.getroot()
def check_for_file_existence(file_path: pathlib.Path) -> None:
if not file_path.is_file():
logger.debug(
"File not found: `%s`. "

This comment has been minimized.

Copy link
@jamilraichouni

jamilraichouni Oct 8, 2024

Contributor

Same comment here, like in line 40-42

"Cannot disable auto-refresh of semantic browser.",
file_path,
)
sys.exit(0)


def identify_semantic_browser_line_no(file_path: pathlib.Path) -> int:
root = etree.parse(file_path).getroot()
logger.debug(
"Searching for XPath expression: `%s` in the file `%s`",
XPATH_EXPR,
FILE_PATH,
file_path,
)
hit_elements = root.xpath(XPATH_EXPR, namespaces=NS_MAP)
if not hit_elements:
logger.debug("No elements found. Exiting.")
sys.exit(0)
# runtime lands here, when we found a setting that controls if the semantic

# Runtime lands here, when we found a setting that controls if the semantic
# browser should auto-refresh or not
persisted_state = hit_elements[0]
parent_element = persisted_state.getparent()
if parent_element is None:
sys.exit(0)
if "elementId" not in parent_element.attrib:
sys.exit(0)
if "semanticbrowser" not in parent_element.attrib["elementId"].lower():
if (
parent_element is None
or "elementId" not in parent_element.attrib
or "semanticbrowser" not in parent_element.attrib["elementId"].lower()
):
sys.exit(0)
# get line number of the element we want to modify

browser_autorefresh_line_no = persisted_state.sourceline
logger.debug(
"Found element at line number: `%d`", browser_autorefresh_line_no
)
line_no = 0
old = new = None
for line in fileinput.input(FILE_PATH, inplace=True):
line_no += 1
if line_no == browser_autorefresh_line_no:
old = "listeningToWorkbenchPageSelectionEvents="1""
new = "listeningToWorkbenchPageSelectionEvents="0""
if old in line:
return browser_autorefresh_line_no


def replace_content_of_line(
file_path: pathlib.Path, line_no: int, old: str, new: str
) -> None:
with fileinput.input(file_path, inplace=True, backup=".bak") as file:
for cur_line_no, line in enumerate(file, start=1):
if cur_line_no == line_no:
line = line.replace(old, new)
else:
old = new = None
sys.stdout.write(line)
fileinput.close()
if old is not None:
logger.debug(
"Replaced `%s` with `%s` in file `%s` at line number %d.",
old,
new,
FILE_PATH,
browser_autorefresh_line_no,
)
sys.stdout.write(line)


def main() -> None:
check_environment_variable()
logger.debug("Expecting the workspace to be at: `%s`", WORKSPACE_DIR)
file_path = pathlib.Path(
f"{WORKSPACE_DIR}/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi"
)
check_for_file_existence(file_path)

browser_autorefresh_line_no = identify_semantic_browser_line_no(file_path)

replace_content_of_line(
file_path,
browser_autorefresh_line_no,
"listeningToWorkbenchPageSelectionEvents="1"",
"listeningToWorkbenchPageSelectionEvents="0"",
)


if __name__ == "__main__":
Expand Down

0 comments on commit c4c9b69

Please sign in to comment.