Skip to content

Commit

Permalink
Use pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
BPerlakiH committed Jan 11, 2024
1 parent ac408ad commit 5ef6d7f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 39 deletions.
10 changes: 4 additions & 6 deletions src/custom_apps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from glob import glob
from pathlib import Path
from info_parser import InfoParser
import subprocess
import yaml
Expand All @@ -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
Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion src/generate_and_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
38 changes: 19 additions & 19 deletions src/info_parser.py
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -23,26 +23,25 @@
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:
plist[key] = keyValues[key]
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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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<year>\d{4})-(?P<month>\d{1,2})')
Expand All @@ -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"]
19 changes: 11 additions & 8 deletions tests/custom_apps_test.py
Original file line number Diff line number Diff line change
@@ -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)
print(cmd)
11 changes: 6 additions & 5 deletions tests/info_parser_test.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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"
Expand All @@ -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")

Expand Down Expand Up @@ -64,15 +65,15 @@ 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(
), "https://www.dwds.de/kiwix/f/dwds_de_dictionary_nopic_2023-12-15.zim")

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"))

0 comments on commit 5ef6d7f

Please sign in to comment.