Skip to content

Commit

Permalink
清理 fs_util
Browse files Browse the repository at this point in the history
  • Loading branch information
TakWolf committed Jul 7, 2024
1 parent fc3a654 commit d7b25b5
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 44 deletions.
8 changes: 5 additions & 3 deletions tools/configs/dump.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import json
from pathlib import Path

import yaml

from tools import configs
from tools.configs import path_define, FontSize
from tools.utils import fs_util


class DumpConfig:
@staticmethod
def load() -> dict[FontSize, list['DumpConfig']]:
configs_data = fs_util.read_yaml(path_define.assets_dir.joinpath('dump-configs.yml'))
configs_data = yaml.safe_load(path_define.assets_dir.joinpath('dump-configs.yml').read_bytes())
dump_configs = {font_size: [] for font_size in configs.font_sizes}
for name, items_data in configs_data.items():
version = fs_util.read_json(path_define.fonts_dir.joinpath(name, 'version.json'))['version']
version = json.loads(path_define.fonts_dir.joinpath(name, 'version.json').read_bytes())['version']
for item_data in items_data:
font_file_path = path_define.fonts_dir.joinpath(name, item_data['font-file-name'].format(version=version))
font_size = item_data['font-size']
Expand Down
5 changes: 3 additions & 2 deletions tools/configs/fallback.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from pathlib import Path

import yaml

from tools import configs
from tools.configs import path_define, FontSize, LanguageFileFlavor
from tools.utils import fs_util


class FallbackConfig:
@staticmethod
def load() -> dict[FontSize, list['FallbackConfig']]:
configs_data = fs_util.read_yaml(path_define.assets_dir.joinpath('fallback-configs.yml'))
configs_data = yaml.safe_load(path_define.assets_dir.joinpath('fallback-configs.yml').read_bytes())
fallback_configs = {font_size: [] for font_size in configs.font_sizes}
for config_data in configs_data:
font_size = config_data['font-size']
Expand Down
5 changes: 3 additions & 2 deletions tools/configs/font.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import yaml

from tools.configs import path_define, FontSize, WidthMode
from tools.utils import fs_util


class LayoutParam:
Expand All @@ -23,7 +24,7 @@ class FontConfig:
@staticmethod
def load(font_size: FontSize) -> 'FontConfig':
file_path = path_define.patch_glyphs_dir.joinpath(str(font_size), 'config.yml')
config_data = fs_util.read_yaml(file_path)
config_data = yaml.safe_load(file_path.read_bytes())

layout_params = {}
for width_mode, layout_param_data in config_data.items():
Expand Down
5 changes: 3 additions & 2 deletions tools/configs/update.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import yaml

from tools.configs import path_define
from tools.utils import fs_util


class DownloadAssetConfig:
Expand All @@ -14,7 +15,7 @@ def __init__(self, file_name: str | None, copy_list: list[tuple[str, str]]):
class UpdateConfig:
@staticmethod
def load() -> list['UpdateConfig']:
configs_data = fs_util.read_yaml(path_define.assets_dir.joinpath('update-configs.yml'))
configs_data = yaml.safe_load(path_define.assets_dir.joinpath('update-configs.yml').read_bytes())
update_configs = []
for config_data in configs_data:
name = config_data['name']
Expand Down
11 changes: 9 additions & 2 deletions tools/services/font_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@
from tools import configs
from tools.configs import path_define, WidthMode, LanguageFlavor, LanguageFileFlavor, FontFormat, FontCollectionFormat
from tools.configs.font import FontConfig
from tools.utils import fs_util


def _is_empty_dir(path: Path) -> bool:
for item_path in path.iterdir():
if item_path.name == '.DS_Store':
continue
return False
return True


class GlyphFile:
Expand Down Expand Up @@ -187,7 +194,7 @@ def format_glyph_files(self):
logger.info("Format glyph file path: '{}'", glyph_file.file_path)

for file_dir, _, _ in root_dir.walk(top_down=False):
if fs_util.is_empty_dir(file_dir):
if _is_empty_dir(file_dir):
shutil.rmtree(file_dir)

def fallback(self, other: 'DesignContext'):
Expand Down
15 changes: 8 additions & 7 deletions tools/services/update_service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
import shutil
import zipfile
Expand All @@ -7,7 +8,7 @@
from tools import configs
from tools.configs import path_define
from tools.configs.update import UpdateConfig
from tools.utils import fs_util, github_api, download_util
from tools.utils import github_api, download_util


def update_ark_pixel_glyphs_version():
Expand Down Expand Up @@ -38,20 +39,20 @@ def update_ark_pixel_glyphs_version():
}
file_path = path_define.fonts_dir.joinpath('ark-pixel').joinpath('version.json')
file_path.parent.mkdir(parents=True, exist_ok=True)
fs_util.write_json(version_info, file_path)
file_path.write_text(f'{json.dumps(version_info, indent=2, ensure_ascii=False)}\n', 'utf-8')
logger.info("Update version file: '{}'", file_path)


def setup_ark_pixel_glyphs():
cache_version_file_path = path_define.ark_pixel_glyphs_dir.joinpath('version.json')
if cache_version_file_path.is_file():
cache_sha = fs_util.read_json(cache_version_file_path)['sha']
cache_sha = json.loads(cache_version_file_path.read_bytes())['sha']
else:
cache_sha = None

font_ark_pixel_dir = path_define.fonts_dir.joinpath('ark-pixel')
version_file_path = font_ark_pixel_dir.joinpath('version.json')
version_info = fs_util.read_json(version_file_path)
version_info = json.loads(version_file_path.read_bytes())
sha = version_info['sha']
if cache_sha == sha:
return
Expand Down Expand Up @@ -98,7 +99,7 @@ def setup_ark_pixel_glyphs():

if source_unzip_dir.exists():
shutil.rmtree(source_unzip_dir)
fs_util.write_json(version_info, cache_version_file_path)
cache_version_file_path.write_text(f'{json.dumps(version_info, indent=2, ensure_ascii=False)}\n', 'utf-8')
logger.info("Update glyphs: '{}'", sha)


Expand All @@ -113,7 +114,7 @@ def update_fonts(update_config: UpdateConfig):
fonts_dir = path_define.fonts_dir.joinpath(update_config.name)
version_file_path = fonts_dir.joinpath('version.json')
if version_file_path.exists():
version_info = fs_util.read_json(version_file_path)
version_info = json.loads(version_file_path.read_bytes())
if version == version_info['version']:
return
logger.info("Need update fonts: '{}'", update_config.name)
Expand Down Expand Up @@ -161,5 +162,5 @@ def update_fonts(update_config: UpdateConfig):
'version_url': f'{repository_url}/releases/tag/{tag_name}',
}
version_file_path = fonts_dir.joinpath('version.json')
fs_util.write_json(version_info, version_file_path)
version_file_path.write_text(f'{json.dumps(version_info, indent=2, ensure_ascii=False)}\n', 'utf-8')
logger.info("Update version file: '{}'", version_file_path)
26 changes: 0 additions & 26 deletions tools/utils/fs_util.py

This file was deleted.

0 comments on commit d7b25b5

Please sign in to comment.