From 311d94dd491d58d0de3f89caa431d13cc19d00be Mon Sep 17 00:00:00 2001 From: Julien M Date: Fri, 26 May 2023 11:08:24 +0200 Subject: [PATCH 1/3] Add class method to create QdtProfile from folder --- .../profiles/qdt_profile.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/qgis_deployment_toolbelt/profiles/qdt_profile.py b/qgis_deployment_toolbelt/profiles/qdt_profile.py index f0295508..b872df8a 100644 --- a/qgis_deployment_toolbelt/profiles/qdt_profile.py +++ b/qgis_deployment_toolbelt/profiles/qdt_profile.py @@ -188,6 +188,39 @@ def from_json( **profile_data, ) + @classmethod + def from_profile_folder(cls, input_profile_folder: Path) -> Self: + """Create object from a QGIS profile folder. Must contain a QGIS/QGIS3.ini file. + + Args: + input_profile_folder (Path): path to the folder containgin a QGIS plugin + + Returns: + Self: instanciated object + """ + # check that input path is a folder + check_path( + input_path=input_profile_folder, + must_exists=True, + must_be_a_folder=True, + must_be_a_file=False, + must_be_readable=True, + ) + # check if the folder contains a QGIS3.ini file + profile_qgis_ini = input_profile_folder / "QGIS/QGIS3.ini" + check_path( + input_path=profile_qgis_ini, + must_be_a_file=True, + must_be_readable=True, + must_exists=True, + ) + + # return new instance with loaded object + return cls( + folder=input_profile_folder.name, + loaded_from_json=False, + ) + @property def alias(self) -> str: """Returns the profile's alias. From 035d2823705d2023d4e56eb739d0397729b6e139 Mon Sep 17 00:00:00 2001 From: Julien M Date: Fri, 26 May 2023 11:09:02 +0200 Subject: [PATCH 2/3] Add subcommand extract (qgis2qdt) --- qgis_deployment_toolbelt/cli.py | 18 +++- qgis_deployment_toolbelt/commands/extract.py | 99 ++++++++++++++++++++ 2 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 qgis_deployment_toolbelt/commands/extract.py diff --git a/qgis_deployment_toolbelt/cli.py b/qgis_deployment_toolbelt/cli.py index c22caae4..1c12cd97 100644 --- a/qgis_deployment_toolbelt/cli.py +++ b/qgis_deployment_toolbelt/cli.py @@ -25,11 +25,10 @@ __uri_homepage__, __version__, ) -from qgis_deployment_toolbelt.commands.cmd_rules_context import ( - parser_rules_context_export, -) +from qgis_deployment_toolbelt.commands.cmd_rules_context import parser_rules_context_export from qgis_deployment_toolbelt.commands.deployment import parser_main_deployment from qgis_deployment_toolbelt.commands.upgrade import parser_upgrade +from qgis_deployment_toolbelt.commands.extract import parser_extract_from_profile from qgis_deployment_toolbelt.utils.journalizer import configure_logger # ############################################################################# @@ -180,6 +179,19 @@ def main(in_args: list[str] = None): ) add_common_arguments(subcmd_rules_context) parser_rules_context_export(subcmd_rules_context) + + # Extractor + subcmd_extract = subparsers.add_parser( + "extract", + aliases=[ + "qgis2qdt", + ], + help="Generate profile ready for QDT from an existing QGIS profile.", + formatter_class=main_parser.formatter_class, + prog="upgrade", + ) + add_common_arguments(subcmd_extract) + parser_extract_from_profile(subcmd_extract) # Upgrader subcmd_upgrade = subparsers.add_parser( diff --git a/qgis_deployment_toolbelt/commands/extract.py b/qgis_deployment_toolbelt/commands/extract.py new file mode 100644 index 00000000..a8bb9473 --- /dev/null +++ b/qgis_deployment_toolbelt/commands/extract.py @@ -0,0 +1,99 @@ +#! python3 # noqa: E265 + +""" + Sub-command in charge of running the main logic. + + Author: Julien M. (https://github.com/guts) +""" + +# ############################################################################# +# ########## Libraries ############# +# ################################## + +# Standard library +import argparse +import logging +from pathlib import Path + +# submodules +from qgis_deployment_toolbelt.profiles.qdt_profile import QdtProfile +from qgis_deployment_toolbelt.utils.check_path import check_path + +# ############################################################################# +# ########## Globals ############### +# ################################## + +logger = logging.getLogger(__name__) + + +# ############################################################################ +# ########## CLI ################# +# ################################ + + +def parser_extract_from_profile( + subparser: argparse.ArgumentParser, +) -> argparse.ArgumentParser: + """Set the argument parser for deployment subcommand. + + Args: + subparser (argparse.ArgumentParser): parser to set up + + Returns: + argparse.ArgumentParser: parser ready to use + """ + subparser.add_argument( + "-f", + "--from", + "--from-profile-path", + dest="input_profile_path", + help="Path to the QGIS profile to extract.", + type=str, + ) + + subparser.add_argument( + "-t", + "--to", + "--to-profile-path", + dest="output_profile_path", + help="Path where to store the QDT profile.", + type=str, + ) + + subparser.set_defaults(func=run) + + return subparser + + +# ############################################################################ +# ########## MAIN ################ +# ################################ + + +def run(args: argparse.Namespace): + """Run the main logic. + + Args: + args (argparse.Namespace): arguments passed to the subcommand + """ + logger.debug(f"Running {args.command} with {args}") + + # check input profile exists + check_path( + input_path=args.input_profile_path, + must_be_a_file=False, + must_be_a_folder=True, + must_be_readable=True, + raise_error=True, + ) + input_qgis_profile_path = Path(args.input_profile_path) + + # make sure output profile folder exists + output_qdt_profile_path = Path(args.output_profile_path) + output_qdt_profile_path.mkdir(parents=True, exist_ok=True) + + src_profile: QdtProfile = QdtProfile.from_profile_folder( + input_profile_folder=input_qgis_profile_path + ) + + print(src_profile) From 50682efca0cc7879943eccf952c1baad2c06dc31 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Jun 2024 07:36:23 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- qgis_deployment_toolbelt/cli.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/qgis_deployment_toolbelt/cli.py b/qgis_deployment_toolbelt/cli.py index 1c12cd97..4fc822e1 100644 --- a/qgis_deployment_toolbelt/cli.py +++ b/qgis_deployment_toolbelt/cli.py @@ -25,10 +25,12 @@ __uri_homepage__, __version__, ) -from qgis_deployment_toolbelt.commands.cmd_rules_context import parser_rules_context_export +from qgis_deployment_toolbelt.commands.cmd_rules_context import ( + parser_rules_context_export, +) from qgis_deployment_toolbelt.commands.deployment import parser_main_deployment -from qgis_deployment_toolbelt.commands.upgrade import parser_upgrade from qgis_deployment_toolbelt.commands.extract import parser_extract_from_profile +from qgis_deployment_toolbelt.commands.upgrade import parser_upgrade from qgis_deployment_toolbelt.utils.journalizer import configure_logger # ############################################################################# @@ -179,7 +181,7 @@ def main(in_args: list[str] = None): ) add_common_arguments(subcmd_rules_context) parser_rules_context_export(subcmd_rules_context) - + # Extractor subcmd_extract = subparsers.add_parser( "extract",