From 5ef6d7f42bd813523887dc3a505ac1015a7a5f94 Mon Sep 17 00:00:00 2001 From: Balazs Perlaki-Horvath Date: Thu, 11 Jan 2024 04:08:54 +0100 Subject: [PATCH] Use pathlib --- src/custom_apps.py | 10 ++++------ src/generate_and_download.py | 3 ++- src/info_parser.py | 38 ++++++++++++++++++------------------ tests/custom_apps_test.py | 19 ++++++++++-------- tests/info_parser_test.py | 11 ++++++----- 5 files changed, 42 insertions(+), 39 deletions(-) diff --git a/src/custom_apps.py b/src/custom_apps.py index 08c526e..da8c46f 100644 --- a/src/custom_apps.py +++ b/src/custom_apps.py @@ -1,4 +1,4 @@ -from glob import glob +from pathlib import Path from info_parser import InfoParser import subprocess import yaml @@ -7,11 +7,9 @@ class CustomApps: def __init__(self): - self.info_files = [] - for f in glob('./**/info.json', recursive=True): - self.info_files.append(f) + self.info_files = Path.cwd().rglob('info.json') - def create_custom_project_file(self, path="custom_project.yml"): + def create_custom_project_file(self, path=Path()/'custom_project.yml'): """Create the project file based on the main repo project.yml It will contain the targets we need for each custom app, and their build settings, pointing to their individual info.plist files @@ -33,7 +31,7 @@ def create_plists(self, custom_plist): """Generate the plist files for each brand Args: - custom_plist (string): the path to the original plist file we are basing this of, + custom_plist (Path): the path to the original plist file we are basing this of, it should be a copy from the Kiwix target """ for info in self.info_files: diff --git a/src/generate_and_download.py b/src/generate_and_download.py index 8e05c52..d883365 100644 --- a/src/generate_and_download.py +++ b/src/generate_and_download.py @@ -4,12 +4,13 @@ """ from custom_apps import CustomApps +from pathlib import Path if __name__ == "__main__": custom_apps = CustomApps() # create the plist files - custom_apps.create_plists(custom_plist="Custom.plist") + custom_apps.create_plists(custom_plist=Path()/"Custom.plist") # download the zim files custom_apps.download_zim_files() diff --git a/src/info_parser.py b/src/info_parser.py index 57075bf..da64871 100644 --- a/src/info_parser.py +++ b/src/info_parser.py @@ -1,10 +1,10 @@ from urllib.parse import urlparse import json +from pathlib import Path import os import re import shutil import plistlib -from glob import glob JSON_KEY_ZIM_URL = "zim_url" JSON_KEY_AUTH = "zim_auth" @@ -23,15 +23,15 @@ class InfoParser: def __init__(self, json_path): - with open(json_path) as file: - self.brand_name = self._brandname_from(json_path) - self.data = json.loads(file.read()) - assert (JSON_KEY_ZIM_URL in self.data) - self.zim_file_name = self._filename_from( - self.data[JSON_KEY_ZIM_URL]) + self.brand_name = self._brandname_from(json_path) + content = json_path.read_text() + self.data = json.loads(content) + assert (JSON_KEY_ZIM_URL in self.data) + self.zim_file_name = self._filename_from( + self.data[JSON_KEY_ZIM_URL]) def create_plist(self, based_on_plist_file): - with open(based_on_plist_file, "rb") as file: + with based_on_plist_file.open(mode="rb") as file: plist = plistlib.load(file) for keyValues in self._plist_key_values(): for key in keyValues: @@ -39,10 +39,9 @@ def create_plist(self, based_on_plist_file): plist[CUSTOM_ZIM_FILE_KEY] = self.zim_file_name out_path = self._info_plist_path() # create dir, if doesn't exists yet - dirname = os.path.dirname(out_path) - if not os.path.exists(dirname): - os.makedirs(dirname, exist_ok=True) - with open(out_path, "wb") as out_file: + if not out_path.parent.exists(): + out_path.parent.mkdir() + with out_path.open(mode="wb") as out_file: plistlib.dump(plist, out_file) def as_project_yml(self): @@ -82,15 +81,15 @@ def zimurl(self): return self.data[JSON_KEY_ZIM_URL] def zim_file_path(self): - url = self.zimurl() - return f"{self.brand_name}/{os.path.basename(url)}" + url = Path(self.zimurl()) + return Path()/self.brand_name/url.name def download_auth(self): auth_key = self.data[JSON_KEY_AUTH] return os.getenv(auth_key) def _info_plist_path(self): - return f"{self.brand_name}/{self.brand_name}.plist" + return Path()/self.brand_name/f"{self.brand_name}.plist" def _plist_key_values(self): for json_key in JSON_TO_PLIST_MAPPING: @@ -120,10 +119,10 @@ def _enforced_language(self): return None def _brandname_from(self, filepath): - return os.path.basename(os.path.dirname(filepath)).lower() + return filepath.parent.name.lower() def _filename_from(self, url): - return os.path.splitext(os.path.basename(urlparse(url).path))[0] + return Path(urlparse(url).path).stem def _app_version_from(self, file_name): p = re.compile('(?P\d{4})-(?P\d{1,2})') @@ -143,8 +142,9 @@ def _excluded_languages(self): return ["**/qqq.lproj"] else: # Copy the enforced lang to the custom folder - for lang_file in glob(f'../**/{enforced}.lproj', recursive=True): + for lang_file in Path().parent.rglob(f'{enforced}.lproj'): + lang_file.copy shutil.copytree( - lang_file, f"../custom/{self.brand_name}/", dirs_exist_ok=True) + lang_file, Path().parent/"custom"/self.brand_name, dirs_exist_ok=True) # exclude all other languages under Support/*.lproj return ["**/*.lproj"] diff --git a/tests/custom_apps_test.py b/tests/custom_apps_test.py index a523d05..9101f6d 100644 --- a/tests/custom_apps_test.py +++ b/tests/custom_apps_test.py @@ -1,20 +1,23 @@ import unittest from src.custom_apps import CustomApps +from pathlib import Path + class CustomAppsTest(unittest.TestCase): - + def setUp(self): self.custom = CustomApps() - + def test_custom_plist(self): - self.custom.create_plists(custom_plist="./tests/Support/Info.plist") - + self.custom.create_plists( + custom_plist=Path()/"tests"/"Support"/"Info.plist") + def test_custom_project_creation(self): - self.custom.create_custom_project_file(path="custom_project_test.yml") - + self.custom.create_custom_project_file(path=Path()/"custom_project_test.yml") + def x_test_downloads(self): self.custom.download_zim_files() - + def x_test_download_commands(self): for cmd in self.custom._curl_download_commands(): - print(cmd) \ No newline at end of file + print(cmd) diff --git a/tests/info_parser_test.py b/tests/info_parser_test.py index b156f20..d29dea9 100644 --- a/tests/info_parser_test.py +++ b/tests/info_parser_test.py @@ -1,12 +1,13 @@ import unittest from src.info_parser import InfoParser +from pathlib import Path import yaml import os class InfoParserTest(unittest.TestCase): def setUp(self): - self.parser = InfoParser("tests/test.json") + self.parser = InfoParser(Path()/"tests"/"test.json") def test_json_to_project_yml(self): project = self.parser.as_project_yml() @@ -15,7 +16,7 @@ def test_json_to_project_yml(self): def test_info_plist_path(self): custom_info = self.parser._info_plist_path() - self.assertEqual(custom_info, "tests/tests.plist") + self.assertEqual(custom_info, Path()/"tests"/"tests.plist") def test_file_name_from_url(self): url = "https://www.dwds.de/kiwix/f/dwds_de_dictionary_nopic_2023-11-20.zim" @@ -27,7 +28,7 @@ def test_file_name_from_url(self): self.assertEqual(file_name, "dwds_de_dictionary_nopic_2023-11") def test_brand_name_from_file_path(self): - filepath = "/User/some/dev/path/project/dwds/info.json" + filepath = Path().home()/"some"/"dev"/"path"/"project"/"dwds"/"info.json" brand_name = self.parser._brandname_from(filepath) self.assertEqual(brand_name, "dwds") @@ -64,7 +65,7 @@ def test_app_version(self): self.assertEqual(self.parser._app_version(), "1023.12.3") def test_as_plist(self): - self.parser.create_plist(based_on_plist_file="./tests/Support/Info.plist") + self.parser.create_plist(based_on_plist_file=Path()/"tests"/"Support"/"Info.plist") def test_zimurl(self): self.assertEqual(self.parser.zimurl( @@ -72,7 +73,7 @@ def test_zimurl(self): def test_zimfile_path(self): self.assertEqual(self.parser.zim_file_path(), - "tests/dwds_de_dictionary_nopic_2023-12-15.zim") + Path()/"tests"/"dwds_de_dictionary_nopic_2023-12-15.zim") def test_auth_value(self): self.assertEqual(self.parser.download_auth(), os.getenv("DWDS_HTTP_BASIC_ACCESS_AUTHENTICATION"))