Skip to content

Commit

Permalink
Feature: improve splash screen manager logic by using ini helper inte…
Browse files Browse the repository at this point in the history
…nsively (#384)
  • Loading branch information
Guts authored Dec 29, 2023
2 parents ee6def5 + c8d8aea commit ded95c3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 113 deletions.
72 changes: 25 additions & 47 deletions qgis_deployment_toolbelt/jobs/job_splash_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from qgis_deployment_toolbelt.profiles.qdt_profile import QdtProfile
from qgis_deployment_toolbelt.profiles.qgis_ini_handler import QgisIniHelper
from qgis_deployment_toolbelt.utils.check_image_size import check_image_dimensions
from qgis_deployment_toolbelt.utils.check_path import check_path

# #############################################################################
# ########## Globals ###############
Expand Down Expand Up @@ -74,43 +73,37 @@ def run(self) -> None:
"""Execute job logic."""
# check of there are some profiles folders within the downloaded folder
downloaded_profiles = self.list_downloaded_profiles()
if downloaded_profiles is None:
logger.error("No QGIS profile found in the downloaded folder.")
if downloaded_profiles is None or not len(downloaded_profiles):
logger.warning(
f"Job {self.ID} ran successfully but that's because no QGIS profile has "
"been found in the QDT downloaded folder. Was the expected behavior?"
)
return

li_installed_profiles_path = [
d
for d in self.qgis_profiles_path.iterdir()
if d.is_dir() and not d.name.startswith(".")
]

if self.options.get("action") in ("create", "create_or_restore"):
for profile_downloaded in downloaded_profiles:
# default absolute splash screen path
installed_splash_screen_filepath = (
profile_downloaded.path_in_qgis / self.DEFAULT_SPLASH_FILEPATH
)
# target QGIS configuration files
# iterate over downloaded profiles
for profile_downloaded in downloaded_profiles:
logger.debug(
f"Applying splash screen for downloaded profile '{profile_downloaded.name}'"
)
profile_installed = profile_downloaded.installed_profile
# target QGIS configuration files
if isinstance(profile_installed, QdtProfile):
qini_helper_installed = profile_installed.get_qgis3ini_helper()
else:
qini_helper_installed = QgisIniHelper(
ini_filepath=profile_downloaded.path_in_qgis / "QGIS/QGIS3.ini",
ini_type="profile_qgis3",
)

if self.options.get("action") == "remove":
qini_helper_installed.set_splash_screen(switch=False)
elif self.options.get("action") in ("create", "create_or_restore"):
# default absolute splash screen path
installed_splash_screen_filepath = (
profile_downloaded.path_in_qgis / self.DEFAULT_SPLASH_FILEPATH
)
# check if a profile.json exists
if check_path(
input_path=profile_downloaded.path_in_qgis.joinpath("profile.json"),
must_be_a_file=True,
must_be_readable=True,
must_exists=True,
raise_error=False,
):
profile_installed: QdtProfile = QdtProfile.from_json(
profile_json_path=profile_downloaded.path_in_qgis.joinpath(
"profile.json"
),
profile_folder=profile_downloaded.path_in_qgis,
)

if isinstance(profile_installed, QdtProfile):
# if the splash image referenced into the profile.json exists, make
# sure it complies QGIS splash screen naming rules
if (
Expand Down Expand Up @@ -187,24 +180,9 @@ def run(self) -> None:
f"Profile {profile_installed.name}: splash screen set "
f"in {qini_helper_installed.profile_customization_path}"
)
elif self.options.get("action") == "remove":
for profile_dir in li_installed_profiles_path:
# default absolute splash screen path
installed_splash_screen_filepath = (
profile_dir / self.DEFAULT_SPLASH_FILEPATH
)

# target QGIS configuration files
cfg_qgis_custom = profile_dir / "QGIS/QGISCUSTOMIZATION3.ini"

# set the splash screen into the customization file
qini_helper_installed.set_splash_screen(
ini_file=cfg_qgis_custom,
splash_screen_filepath=installed_splash_screen_filepath.resolve(),
switch=False,
)
else:
raise NotImplementedError
else:
raise NotImplementedError

logger.debug(f"Job {self.ID} ran successfully.")

Expand Down
71 changes: 5 additions & 66 deletions qgis_deployment_toolbelt/profiles/qgis_ini_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ def is_splash_screen_set(
)
return False
else:
logger.debug(
f"{ini_file.initial_file_path} has a splash screen set: {splash_path}"
)
return True
else:
logger.debug(
Expand All @@ -227,7 +230,7 @@ def is_splash_screen_set(
cfg_parser = self.cfg_parser()
cfg_parser.read(ini_file, encoding="UTF8")
logger.debug(
f"{ini_file} is an existing file, has been parsed. Let's check if a "
f"{ini_file} is an existing file and has been parsed. Let's check if a "
"splash path is set."
)
return self.is_splash_screen_set(cfg_parser)
Expand Down Expand Up @@ -436,10 +439,6 @@ def set_splash_screen(
pass

# FROM NOW: isinstance(ini_file, CustomConfigParser) is True
assert isinstance(ini_file, CustomConfigParser)
assert isinstance(ini_file.get_initial_file_path(), Path)
assert ini_file.get_initial_file_path().exists()

qgiscustomization3ini_filepath = ini_file.get_initial_file_path()
option = "splashpath"
section = "Customization"
Expand Down Expand Up @@ -530,64 +529,4 @@ def set_splash_screen(

if __name__ == "__main__":
"""Standalone execution."""
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s||%(levelname)s||%(module)s||%(lineno)d||%(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)

new_config_file = Path("tests/fixtures/qgis_ini/default_no_customization/QGIS3.ini")
assert new_config_file.exists()
ini_config = QgisIniHelper(ini_filepath=new_config_file)

print(ini_config.is_ui_customization_enabled())
assert ini_config.is_ui_customization_enabled() is False
ini_config.set_ui_customization_enabled()
assert ini_config.is_ui_customization_enabled() is True
ini_config.set_ui_customization_enabled(switch=False)
assert ini_config.is_ui_customization_enabled() is False

qini_helper = QgisIniHelper(
ini_filepath=Path(
"/home/jmo/Git/Oslandia/QGIS/qgis-deployment-cli/tests/fixtures/tmp/customization_with_splashpath.ini"
),
ini_type="profile_qgis3customization",
)
qini_helper.ini_type = "profile_qgis3customization"
print(qini_helper.is_splash_screen_set())

fake_config = "[Customization]\nsplashpath="

tmp_ini_customization = Path(
"tests/fixtures/tmp/customization_with_splashpath_empty.ini"
)
tmp_ini_customization.parent.mkdir(parents=True, exist_ok=True)
tmp_ini_customization.write_text(fake_config)

qini_helper = QgisIniHelper(
ini_filepath=tmp_ini_customization, ini_type="profile_qgis3customization"
)
print(qini_helper.is_splash_screen_set())
assert qini_helper.is_splash_screen_set() is False
tmp_ini_customization.unlink()

# ENABLE/DISABLE SPLASH SCREEN
fake_config = "[Customization]\nsplashpath="

tmp_ini_customization = Path(
"tests/fixtures/tmp/customization_with_splashpath_empty.ini"
)
tmp_ini_customization.parent.mkdir(parents=True, exist_ok=True)
tmp_ini_customization.write_text(fake_config)

qini_helper = QgisIniHelper(
ini_filepath=tmp_ini_customization, ini_type="profile_qgis3customization"
)
qini_helper.set_splash_screen(switch=False) is False
tmp_ini_customization.unlink()

not_existing_ini = Path("no_existing_file.ini")
qini_helper = QgisIniHelper(
ini_filepath=not_existing_ini, ini_type="profile_qgis3customization"
)
qini_helper.set_splash_screen(switch=False) is False
pass

0 comments on commit ded95c3

Please sign in to comment.