Skip to content

Commit

Permalink
docs: Document the module in the function main
Browse files Browse the repository at this point in the history
Describe why we cannot use `lxml` to edit the file in-place and why we
use `fileinput` instead.
  • Loading branch information
jamilraichouni committed Oct 11, 2024
1 parent 62e7de6 commit b0b143a
Showing 1 changed file with 35 additions and 22 deletions.
57 changes: 35 additions & 22 deletions capella/setup/disable_semantic_browser_auto_refresh.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0
"""Module is documented in the function `main`."""

import fileinput
import logging
import os
Expand Down Expand Up @@ -29,6 +31,39 @@
logger = logging.getLogger(__file__)


def main() -> None:
"""Disable auto-refresh of semantic browser when setting exists.
This script will disable the auto-refresh of the semantic browser in
Capella. A precondition is, that the according Capella configuration
entry already exists on disk. The script identifies the setting of
interest using ``lxml`` and an XPath query. The editing of the
configuration file is done in-place using the ``fileinput`` module
instead of writing it on disk with ``lxml``. This is done to avoid
issues with the file format. Capella mixes HTML into an XML file
and `lxml` escapes some HTML entities, which would break the file.
"""
_check_environment_variable()
logger.debug("Expecting the workspace to be at: `%s`", WORKSPACE_DIR)
file_path = (
pathlib.Path(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_in_line_of_file(
file_path,
browser_autorefresh_line_no,
"listeningToWorkbenchPageSelectionEvents="1"",
"listeningToWorkbenchPageSelectionEvents="0"",
)


def _check_environment_variable() -> None:
disable_semantic_browser_auto_refresh = os.getenv(
"CAPELLA_DISABLE_SEMANTIC_BROWSER_AUTO_REFRESH", "0"
Expand Down Expand Up @@ -122,27 +157,5 @@ def _replace_content_in_line_of_file(
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(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_in_line_of_file(
file_path,
browser_autorefresh_line_no,
"listeningToWorkbenchPageSelectionEvents="1"",
"listeningToWorkbenchPageSelectionEvents="0"",
)


if __name__ == "__main__":
main()

0 comments on commit b0b143a

Please sign in to comment.