-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add semantic browser auto-refresh setting
Make it possible to disable the auto-refresh of the semantic browser by setting the environment variable `CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH` to `1`.
- Loading branch information
1 parent
358eca8
commit 5021640
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import fileinput | ||
import logging | ||
import os | ||
import pathlib | ||
import sys | ||
|
||
from lxml import etree | ||
|
||
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." | ||
) | ||
WS = "/home/techuser/workspace" | ||
logger.debug(f"Expecting the workspace to be at: `{WS}`") | ||
file_path = pathlib.Path( | ||
f"{WS}/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi" | ||
) | ||
if not file_path.is_file(): | ||
logger.debug( | ||
f"File not found: `{file_path}`." | ||
" Cannot disable auto-refresh of semantic browser." | ||
) | ||
sys.exit(0) | ||
tree = etree.parse(file_path) | ||
encoding = tree.docinfo.encoding | ||
root = tree.getroot() | ||
nsmap = { | ||
"xmi": "http://www.omg.org/XMI", | ||
"xsi": "http://www.w3.org/2001/XMLSchema-instance", | ||
"basic": "http://www.eclipse.org/ui/2010/UIModel/application/ui/basic", | ||
"advanced": ( | ||
"http://www.eclipse.org/ui/2010/UIModel/application/ui/advanced" | ||
), | ||
"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')]" | ||
) | ||
logger.debug( | ||
f"Searching for XPath expression: `{xpath_expr}` in the file `{file_path}`" | ||
) | ||
hit_elements = root.xpath(xpath_expr, namespaces=nsmap) | ||
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 | ||
# 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(): | ||
sys.exit(0) | ||
# get line number of the element we want to modify | ||
browser_autorefresh_line_no = persisted_state.sourceline | ||
logger.debug(f"Found element at line number: `{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: | ||
line = line.replace(old, new) | ||
else: | ||
old = new = None | ||
sys.stdout.write(line) | ||
fileinput.close() | ||
if old is not None: | ||
logger.debug( | ||
f"Replaced `{old}` with `{new}` in file `{file_path}` at line" | ||
f" number `{browser_autorefresh_line_no}`." | ||
) |