Skip to content

Commit

Permalink
全局配置添加类型约束
Browse files Browse the repository at this point in the history
  • Loading branch information
TakWolf committed Jul 5, 2024
1 parent c0f8efe commit 21afb99
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
19 changes: 13 additions & 6 deletions tools/configs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from typing import Literal, get_args

font_version = '2024.05.12'

font_sizes = [8, 10, 12]
type FontSize = Literal[8, 10, 12]
font_sizes = list[FontSize](get_args(FontSize.__value__))

width_modes = [
type WidthMode = Literal[
'monospaced',
'proportional',
]
width_modes = list[WidthMode](get_args(WidthMode.__value__))

language_file_flavors = [
type LanguageFileFlavors = Literal[
'latin',
'zh_cn',
'zh_hk',
Expand All @@ -17,18 +20,22 @@
'ja',
'ko',
]
language_file_flavors = list[LanguageFileFlavors](get_args(LanguageFileFlavors.__value__))

language_flavors = [
type LanguageFlavor = Literal[
'latin',
'zh_hans',
'zh_hant',
'ja',
'ko',
]
language_flavors = list[LanguageFlavor](get_args(LanguageFlavor.__value__))

font_formats = ['otf', 'woff2', 'ttf', 'bdf', 'pcf']
type FontFormat = Literal['otf', 'woff2', 'ttf', 'bdf', 'pcf']
font_formats = list[FontFormat](get_args(FontFormat.__value__))

font_collection_formats = ['otc', 'ttc']
type FontCollectionFormat = Literal['otc', 'ttc']
font_collection_formats = list[FontCollectionFormat](get_args(FontCollectionFormat.__value__))

license_configs = {
8: [
Expand Down
8 changes: 4 additions & 4 deletions tools/configs/dump.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from pathlib import Path

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


class DumpConfig:
@staticmethod
def load() -> dict[int, list['DumpConfig']]:
def load() -> dict[FontSize, list['DumpConfig']]:
configs_data = fs_util.read_yaml(path_define.assets_dir.joinpath('dump-configs.yml'))
dump_configs = {font_size: [] for font_size in configs.font_sizes}
for name, items_data in configs_data.items():
Expand All @@ -32,7 +32,7 @@ def load() -> dict[int, list['DumpConfig']]:

name: str
font_file_path: Path
font_size: int
font_size: FontSize
dump_dir: Path
rasterize_size: int
rasterize_offset_x: int
Expand All @@ -42,7 +42,7 @@ def __init__(
self,
name: str,
font_file_path: Path,
font_size: int,
font_size: FontSize,
dump_dir: Path,
rasterize_size: int,
rasterize_offset_x: int,
Expand Down
12 changes: 6 additions & 6 deletions tools/configs/fallback.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from pathlib import Path

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


class FallbackConfig:
@staticmethod
def load() -> dict[int, list['FallbackConfig']]:
def load() -> dict[FontSize, list['FallbackConfig']]:
configs_data = fs_util.read_yaml(path_define.assets_dir.joinpath('fallback-configs.yml'))
fallback_configs = {font_size: [] for font_size in configs.font_sizes}
for config_data in configs_data:
Expand All @@ -23,17 +23,17 @@ def load() -> dict[int, list['FallbackConfig']]:
))
return fallback_configs

font_size: int
font_size: FontSize
dir_from: Path
dir_to: Path
flavor: str | None
flavor: LanguageFileFlavors | None

def __init__(
self,
font_size: int,
font_size: FontSize,
dir_from: Path,
dir_to: Path,
flavor: str | None,
flavor: LanguageFileFlavors | None,
):
self.font_size = font_size
self.dir_from = dir_from
Expand Down
10 changes: 5 additions & 5 deletions tools/configs/font.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from tools.configs import path_define
from tools.configs import path_define, FontSize, WidthMode
from tools.utils import fs_util


Expand All @@ -21,7 +21,7 @@ def line_height(self) -> int:

class FontConfig:
@staticmethod
def load(font_size: int) -> 'FontConfig':
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)

Expand All @@ -36,10 +36,10 @@ def load(font_size: int) -> 'FontConfig':

return FontConfig(font_size, layout_params)

font_size: int
layout_params: dict[str, LayoutParam]
font_size: FontSize
layout_params: dict[WidthMode, LayoutParam]

def __init__(self, font_size: int, layout_params: dict[str, LayoutParam]):
def __init__(self, font_size: FontSize, layout_params: dict[WidthMode, LayoutParam]):
self.font_size = font_size
self.layout_params = layout_params

Expand Down

0 comments on commit 21afb99

Please sign in to comment.