Skip to content

Commit

Permalink
Add subcommand extract (qgis2qdt)
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed May 26, 2023
1 parent b17b7ac commit 57cb293
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
19 changes: 18 additions & 1 deletion qgis_deployment_toolbelt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
__uri_homepage__,
__version__,
)
from qgis_deployment_toolbelt.commands import parser_main_deployment, parser_upgrade
from qgis_deployment_toolbelt.commands import (
parser_extract_from_profile,
parser_main_deployment,
parser_upgrade,
)
from qgis_deployment_toolbelt.utils.journalizer import configure_logger

# #############################################################################
Expand Down Expand Up @@ -154,6 +158,19 @@ def main(in_args: list[str] = None):
add_common_arguments(subcmd_deployment)
parser_main_deployment(subcmd_deployment)

# 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(
"upgrade",
Expand Down
1 change: 1 addition & 0 deletions qgis_deployment_toolbelt/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

# submodules
from .deployment import parser_main_deployment # noqa: F401
from .extract import parser_extract_from_profile # noqa: F401
from .upgrade import parser_upgrade # noqa: F401
99 changes: 99 additions & 0 deletions qgis_deployment_toolbelt/commands/extract.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 57cb293

Please sign in to comment.