-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
- 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
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
MoritzWeber0
Author
Member
|
||
"`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.
Sorry, something went wrong. |
||
"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__": | ||
|
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: