aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-08-02 08:49:26 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2022-08-03 10:46:43 +0200
commit22bc41605a4dd18330a39f6346143ec55a7d6074 (patch)
treee35dae6fb57f21d20ab6c6c1a204a40f110ced46 /tools
parent1b0134fd104cb710bc9d619c22fd0bacc0832c05 (diff)
snippets_translate: Add a way of overriding snippets
Prototypically fix the QInputDialog dialog snippets. Task-number: PYSIDE-1984 Fixes: PYSIDE-1952 Pick-to: 6.3 6.2 Change-Id: Iad75971b8778a6364123963d28f54f02a0c56737 Reviewed-by: Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> Reviewed-by: Christian Tismer <tismer@stackless.com> Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'tools')
-rw-r--r--tools/snippets_translate/main.py41
1 files changed, 36 insertions, 5 deletions
diff --git a/tools/snippets_translate/main.py b/tools/snippets_translate/main.py
index 2ff027701..59ec34394 100644
--- a/tools/snippets_translate/main.py
+++ b/tools/snippets_translate/main.py
@@ -39,6 +39,8 @@ SKIP_END = (".pro", ".pri", ".cmake", ".qdoc", ".yaml", ".frag", ".qsb", ".vert"
SKIP_BEGIN = ("changes-", ".")
SNIPPET_PATTERN = re.compile(r"//! ?\[([^]]+)\]")
+SOURCE_PATH = Path(__file__).parents[2] / "sources" / "pyside6" / "doc" / "snippets"
+OVERRIDDEN_SNIPPET = "# OVERRIDDEN_SNIPPET"
class FileStatus(Enum):
Exists = 0
@@ -169,10 +171,25 @@ def get_snippet_ids(line: str) -> List[str]:
return result
-def get_snippets(lines: List[str]) -> List[List[str]]:
+def get_snippet_override(start_id: str, rel_path: str) -> List[str]:
+ # Check if the snippet is overridden by a local file
+ override_name = f"{rel_path.stem}_{start_id}{rel_path.suffix}.py"
+ override_path = SOURCE_PATH / rel_path.parent / override_name
+ snippet = []
+ if override_path.is_file():
+ snippet.append(OVERRIDDEN_SNIPPET)
+ id_string = f"//! [{start_id}]"
+ snippet.append(id_string)
+ snippet.extend(override_path.read_text().splitlines())
+ snippet.append(id_string)
+ return snippet
+
+
+def get_snippets(lines: List[str], rel_path: str) -> List[List[str]]:
# Extract (potentially overlapping) snippets from a C++ file indicated by //! [1]
snippets: List[List[str]] = []
snippet: List[str]
+ done_snippets : List[str] = []
i = 0
while i < len(lines):
@@ -183,7 +200,15 @@ def get_snippets(lines: List[str]) -> List[List[str]]:
while start_ids:
# Start of a snippet
start_id = start_ids.pop(0)
- snippet = [line] # The snippet starts with his id
+ if start_id in done_snippets:
+ continue
+ done_snippets.append(start_id)
+ snippet = get_snippet_override(start_id, rel_path)
+ if snippet:
+ snippets.append(snippet)
+ continue
+
+ snippet.append(line) # The snippet starts with this id
# Find the end of the snippet
j = i
@@ -229,9 +254,11 @@ def get_license_from_file(filename):
return ""
-def translate_file(file_path, final_path, debug, write):
+def translate_file(file_path, final_path, qt_path, debug, write):
with open(str(file_path)) as f:
- snippets = get_snippets(f.read().splitlines())
+ lines = f.read().splitlines()
+ rel_path = file_path.relative_to(qt_path)
+ snippets = get_snippets(lines, rel_path)
if snippets:
# TODO: Get license header first
license_header = get_license_from_file(str(file_path))
@@ -244,6 +271,10 @@ def translate_file(file_path, final_path, debug, write):
translated_lines = []
for snippet in snippets:
+ if snippet and snippet[0] == OVERRIDDEN_SNIPPET:
+ translated_lines.extend(snippet[1:])
+ continue
+
for line in snippet:
if not line:
continue
@@ -322,7 +353,7 @@ def copy_file(file_path, qt_path, out_path, write=False, debug=False):
# Change .cpp to .py, .h to .h.py
# Translate C++ code into Python code
if final_path.name.endswith(".cpp") or final_path.name.endswith(".h"):
- translate_file(file_path, final_path, debug, write)
+ translate_file(file_path, final_path, qt_path, debug, write)
return status