Skip to content

Commit

Permalink
Enhancement: add a module to read and write QGIS ini files (#337)
Browse files Browse the repository at this point in the history
Supersedes #270

- centralize INI files reading and writing:
  - handle interpolation with environment variables
- for now, support `QGIS3.ini` and `QGIS3CUSTOMIZATION.ini` (next:
plugin `metadata.txt`)
- for now, only features related to:
  - UI customization enable/disable
  - splash screen set/unset
  • Loading branch information
Guts authored Nov 14, 2023
2 parents 6c1b939 + 9bc7d12 commit 6cbac28
Show file tree
Hide file tree
Showing 16 changed files with 1,321 additions and 285 deletions.
2 changes: 1 addition & 1 deletion qgis_deployment_toolbelt/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@


def get_qdt_working_directory(
specific_value: PathLike = None, identifier: str = "default"
specific_value: PathLike | None = None, identifier: str = "default"
) -> Path:
"""Get QDT working directory.
Expand Down
115 changes: 0 additions & 115 deletions qgis_deployment_toolbelt/jobs/generic_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

# Standard library
import logging
from configparser import ConfigParser
from pathlib import Path
from sys import platform as opersys

Expand Down Expand Up @@ -124,120 +123,6 @@ def filter_profiles_folder(self, start_parent_folder: Path) -> tuple[QdtProfile]
logger.error("No QGIS profile found in the downloaded folder.")
return None

def set_ui_customization_enabled(
self, qgis3ini_filepath: Path, section: str, option: str, switch: bool = True
) -> bool:
"""Enable/disable UI customization in the profile QGIS3.ini file.
Args:
qgis3ini_filepath (Path): path to the QGIS3.ini configuration file
section (str): section name in ini file
option (str): option name in the section of the ini file
switch (bool, optional): True to enable, False to disable UI customization.
Defaults to True.
Returns:
bool: UI customization state. True is enabled, False is disabled.
"""
# boolean syntax for PyQt
switch_value = "false"
if switch:
switch_value = "true"

# make sure that the file exists
if not qgis3ini_filepath.exists():
logger.warning(
f"Configuration file {qgis3ini_filepath} doesn't exist. "
"It will be created but maybe it was not the expected behavior."
)
qgis3ini_filepath.touch(exist_ok=True)
qgis3ini_filepath.write_text(
data=f"[{section}]\n{option}={switch_value}", encoding="UTF8"
)
return switch

# read configuration file
ini_qgis3 = ConfigParser(strict=False)
ini_qgis3.optionxform = str
ini_qgis3.read(qgis3ini_filepath, encoding="UTF8")

# if section and option already exist
if ini_qgis3.has_option(section=section, option=option): # = section AND option
actual_state = ini_qgis3.getboolean(section=section, option=option)
# when actual UI customization is enabled
if actual_state and switch:
logger.debug(
f"UI Customization is already ENABLED in {qgis3ini_filepath}"
)
return True
elif actual_state and not switch:
ini_qgis3.set(
section=section,
option=option,
value=switch_value,
)
with qgis3ini_filepath.open("w", encoding="UTF8") as configfile:
ini_qgis3.write(configfile, space_around_delimiters=False)
logger.debug(
"UI Customization was ENABLED and has been "
f"DISABLED in {qgis3ini_filepath}"
)
return False

# when actual UI customization is disabled
elif not actual_state and switch:
ini_qgis3.set(
section=section,
option=option,
value=switch_value,
)
with qgis3ini_filepath.open("w", encoding="UTF8") as configfile:
ini_qgis3.write(configfile, space_around_delimiters=False)
logger.debug(
"UI Customization was DISABLED and has been "
f"ENABLED in {qgis3ini_filepath}"
)
return True
elif not actual_state and not switch:
logger.debug(
f"UI Customization is already DISABLED in {qgis3ini_filepath}"
)
return True
elif ini_qgis3.has_section(section=section) and switch:
# section exist but not the option, so let's add it as required
ini_qgis3.set(
section=section,
option=option,
value=switch_value,
)
with qgis3ini_filepath.open("w", encoding="UTF8") as configfile:
ini_qgis3.write(configfile, space_around_delimiters=False)
logger.debug(
f"'{option}' option was missing in {section}, it's just been added and "
f"ENABLED in {qgis3ini_filepath}"
)
return True
elif not ini_qgis3.has_section(section=section) and switch:
# even the section is missing. Let's add everything
ini_qgis3.add_section(section)
ini_qgis3.set(
section=section,
option=option,
value=switch_value,
)
with qgis3ini_filepath.open("w", encoding="UTF8") as configfile:
ini_qgis3.write(configfile, space_around_delimiters=False)
logger.debug(
f"'{option}' option was missing in {section}, it's just been added and "
f"ENABLED in {qgis3ini_filepath}"
)
return True
else:
logger.debug(
f"Section '{section}' is not present so {option} is DISABLED in {qgis3ini_filepath}"
)
return False

def validate_options(self, options: dict[dict]) -> dict[dict]:
"""Validate options.
Expand Down
Loading

0 comments on commit 6cbac28

Please sign in to comment.