diff options
author | Cristián Maureira-Fredes <cristian.maureira-fredes@qt.io> | 2022-06-29 11:52:35 +0200 |
---|---|---|
committer | Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io> | 2022-06-30 10:11:09 +0000 |
commit | f357d75b30890e7df853199f72560a3f34a290f2 (patch) | |
tree | 649ee04f0b4e2542e93640ca50ac500c9bbea1e6 /tools | |
parent | aaba428fd6cbfdd4da9714885659853f2ff634d7 (diff) |
snippet translate: add option to process directory
Including option to process the snippets inside
a directory, and refactoring the general script
to adapt this option.
Initial-Patch-by: Jaime Resano <gemailpersonal02@gmail.com>
Pick-to: 6.2 6.3
Change-Id: I629be8b7c13bc0445279ced73c3159800cd0644d
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/snippets_translate/README.md | 18 | ||||
-rw-r--r-- | tools/snippets_translate/main.py | 145 |
2 files changed, 100 insertions, 63 deletions
diff --git a/tools/snippets_translate/README.md b/tools/snippets_translate/README.md index 9e1a5a949..8d9ab86f8 100644 --- a/tools/snippets_translate/README.md +++ b/tools/snippets_translate/README.md @@ -11,7 +11,7 @@ Here's an explanation for each file: * `main.py`, main file that handle the arguments, the general process of copying/writing files into the pyside-setup/ repository. * `converter.py`, main function that translate each line depending - of the decision making process that use different handlers. + on the decision-making process that use different handlers. * `handlers.py`, functions that handle the different translation cases. * `parse_utils.py`, some useful function that help the translation process. * `tests/test_converter.py`, tests cases for the converter function. @@ -20,20 +20,26 @@ Here's an explanation for each file: ``` % python main.py -h -usage: sync_snippets [-h] --qt QT_DIR --pyside PYSIDE_DIR [-w] [-v] +usage: sync_snippets [-h] --qt QT_DIR --target PYSIDE_DIR [-f DIRECTORY] [-w] [-v] [-d] [-s SINGLE_SNIPPET] [--filter FILTER_SNIPPET] optional arguments: -h, --help show this help message and exit --qt QT_DIR Path to the Qt directory (QT_SRC_DIR) - --pyside PYSIDE_DIR Path to the pyside-setup directory + --target TARGET_DIR Directory into which to generate the snippets -w, --write Actually copy over the files to the pyside-setup directory -v, --verbose Generate more output + -d, --debug Generate even more output + -s SINGLE_SNIPPET, --single SINGLE_SNIPPET + Path to a single file to be translated + -f, --directory DIRECTORY Path to a directory containing the snippets to be translated + --filter FILTER_SNIPPET + String to filter the snippets to be translated ``` For example: ``` -python main.py --qt /home/cmaureir/dev/qt6/ --pyside /home/cmaureir/dev/pyside-setup -w +python main.py --qt /home/cmaureir/dev/qt6/ --target /home/cmaureir/dev/pyside-setup -w ``` which will create all the snippet files in the pyside repository. The `-w` @@ -79,7 +85,7 @@ goes to: ### Examples -Everything that has .../examples/*/*, for example: +Everything that has .../examples/*, for example: ``` ./qtbase/examples/widgets/dialogs/licensewizard @@ -175,5 +181,3 @@ for m in modules: _out[m] = m_classes pprint(_out) ``` - -PySide2 was used to cover more classes that are not available for Qt 6.0. diff --git a/tools/snippets_translate/main.py b/tools/snippets_translate/main.py index cf34bc6cd..664fe7aa5 100644 --- a/tools/snippets_translate/main.py +++ b/tools/snippets_translate/main.py @@ -1,11 +1,11 @@ # Copyright (C) 2022 The Qt Company Ltd. # SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only -import argparse import logging import os import re import sys +from argparse import ArgumentParser, Namespace from enum import Enum from pathlib import Path from textwrap import dedent @@ -45,9 +45,12 @@ class FileStatus(Enum): New = 1 -def get_parser(): - parser = argparse.ArgumentParser(prog="snippets_translate") - # List pyproject files +def get_parser() -> ArgumentParser: + """ + Returns a parser for the command line arguments of the script. + See README.md for more information. + """ + parser = ArgumentParser(prog="snippets_translate") parser.add_argument( "--qt", action="store", @@ -97,6 +100,14 @@ def get_parser(): ) parser.add_argument( + "-f", + "--directory", + action="store", + dest="single_directory", + help="Path to a single directory to be translated", + ) + + parser.add_argument( "--filter", action="store", dest="filter_snippet", @@ -315,72 +326,94 @@ def copy_file(file_path, qt_path, out_path, write=False, debug=False): return status -def process(options): - qt_path = Path(options.qt_dir) - out_path = Path(options.target_dir) +def single_directory(options, qt_path, out_path): + # Process all files in the directory + directory_path = Path(options.single_directory) + for file_path in directory_path.glob("**/*"): + if file_path.is_dir() or not is_valid_file(file_path): + continue + copy_file(file_path, qt_path, out_path, write=options.write_files, debug=options.debug) + + +def single_snippet(options, qt_path, out_path): + # Process a single file + file = Path(options.single_snippet) + if is_valid_file(file): + copy_file(file, qt_path, out_path, write=options.write_files, debug=options.debug) - # (new, exists) + +def all_modules_in_directory(options, qt_path, out_path): + """ + Process all Qt modules in the directory. Logs how many files were processed. + """ + # New files, already existing files valid_new, valid_exists = 0, 0 - # Creating directories in case they don't exist - if not out_path.is_dir(): - out_path.mkdir(parents=True) + for module in qt_path.iterdir(): + module_name = module.name - if options.single_snippet: - f = Path(options.single_snippet) - if is_valid_file(f): - status = copy_file(f, qt_path, out_path, - write=options.write_files, - debug=options.debug) + # Filter only Qt modules + if not module_name.startswith("qt"): + continue - else: - for i in qt_path.iterdir(): - module_name = i.name + if not opt_quiet: + log.info(f"Module {module_name}") - # Filter only Qt modules - if not module_name.startswith("qt"): + # Iterating everything + for f in module.glob("**/*.*"): + # Proceed only if the full path contain the filter string + if not is_valid_file(f): continue - if not opt_quiet: - log.info(f"Module {module_name}") - - # Iterating everything - for f in i.glob("**/*.*"): - if is_valid_file(f): - if options.filter_snippet: - # Proceed only if the full path contain the filter string - if options.filter_snippet not in str(f.absolute()): - continue - status = copy_file(f, qt_path, out_path, - write=options.write_files, - debug=options.debug) - - # Stats - if status == FileStatus.New: - valid_new += 1 - elif status == FileStatus.Exists: - valid_exists += 1 - if not opt_quiet: - log.info( - dedent( - f"""\ - Summary: - Total valid files: {valid_new + valid_exists} - New files: {valid_new} - Existing files: {valid_exists} - """ - ) + if options.filter_snippet and options.filter_snippet not in str(f.absolute()): + continue + + status = copy_file(f, qt_path, out_path, write=options.write_files, debug=options.debug) + + # Stats + if status == FileStatus.New: + valid_new += 1 + elif status == FileStatus.Exists: + valid_exists += 1 + + if not opt_quiet: + log.info( + dedent( + f"""\ + Summary: + Total valid files: {valid_new + valid_exists} + New files: {valid_new} + Existing files: {valid_exists} + """ ) + ) + + +def process_files(options: Namespace) -> None: + qt_path = Path(options.qt_dir) + out_path = Path(options.target_dir) + + # Creating directories in case they don't exist + if not out_path.is_dir(): + out_path.mkdir(parents=True) + + if options.single_directory: + single_directory(options, qt_path, out_path) + elif options.single_snippet: + single_snippet(options, qt_path, out_path) + else: + # General case: process all Qt modules in the directory + all_modules_in_directory(options, qt_path, out_path) if __name__ == "__main__": parser = get_parser() - options = parser.parse_args() - opt_quiet = False if options.verbose else True - opt_quiet = False if options.debug else opt_quiet + opt: Namespace = parser.parse_args() + opt_quiet = not (opt.verbose or opt.debug) - if not check_arguments(options): + if not check_arguments(opt): + # Error, invalid arguments parser.print_help() sys.exit(-1) - process(options) + process_files(opt) |