Skip to content

Commit

Permalink
更换日志框架
Browse files Browse the repository at this point in the history
  • Loading branch information
TakWolf committed Jul 7, 2024
1 parent ce55ba2 commit d874971
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 50 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Logo 捏他自 [《游戏王》](https://zh.wikipedia.org/wiki/%E9%81%8A%E6%88%B
- [Beautiful Soup](https://www.crummy.com/software/BeautifulSoup/)
- [Jinja](https://github.com/pallets/jinja)
- [HTTPX](https://github.com/encode/httpx)
- [Loguru](https://github.com/Delgan/loguru)

## 赞助

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ pillow==10.4.0
beautifulsoup4==4.12.3
Jinja2==3.1.4
httpx==0.27.0
loguru==0.7.2
3 changes: 0 additions & 3 deletions tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
import logging

logging.basicConfig(level=logging.INFO)
12 changes: 5 additions & 7 deletions tools/services/dump_service.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import logging
import math
import shutil

import unidata_blocks
from PIL import ImageFont, Image, ImageDraw
from fontTools.ttLib import TTFont
from loguru import logger

from tools.configs.dump import DumpConfig
from tools.configs.fallback import FallbackConfig

logger = logging.getLogger(__name__)


def dump_font(dump_config: DumpConfig):
logger.info("Dump glyphs: '%s'", dump_config.dump_dir)
logger.info("Dump glyphs: '{}'", dump_config.dump_dir)
font = TTFont(dump_config.font_file_path)
image_font = ImageFont.truetype(dump_config.font_file_path, dump_config.rasterize_size)

Expand Down Expand Up @@ -45,12 +43,12 @@ def dump_font(dump_config: DumpConfig):

glyph_file_dir.mkdir(parents=True, exist_ok=True)
image.save(glyph_file_path)
logger.debug("Dump glyph: '%s'", glyph_file_path)
logger.debug("Dump glyph: '{}'", glyph_file_path)


def apply_fallback(fallback_config: FallbackConfig):
assert fallback_config.dir_from.is_dir(), f"Dump dir not exist: '{fallback_config.dir_from}'"
logger.info("Fallback glyphs: '%s' '%s' -> '%s'", fallback_config.flavor, fallback_config.dir_from, fallback_config.dir_to)
logger.info("Fallback glyphs: '{}' '{}' -> '{}'", fallback_config.flavor, fallback_config.dir_from, fallback_config.dir_to)
for glyph_file_dir_from, _, glyph_file_names in fallback_config.dir_from.walk():
for glyph_file_name in glyph_file_names:
if not glyph_file_name.endswith('.png'):
Expand All @@ -68,4 +66,4 @@ def apply_fallback(fallback_config: FallbackConfig):
glyph_file_path_to = glyph_file_dir_to.joinpath(glyph_file_name)
glyph_file_dir_to.mkdir(parents=True, exist_ok=True)
shutil.copyfile(glyph_file_path_from, glyph_file_path_to)
logger.debug("Copy glyph file: '%s'", glyph_file_path_to)
logger.debug("Copy glyph file: '{}'", glyph_file_path_to)
10 changes: 4 additions & 6 deletions tools/services/font_service.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import datetime
import logging
import math
import re
import shutil
import unicodedata
from pathlib import Path

import unidata_blocks
from loguru import logger
from pixel_font_builder import FontBuilder, FontCollectionBuilder, WeightName, SerifStyle, SlantStyle, WidthStyle, Glyph
from pixel_font_builder.opentype import Flavor
from pixel_font_knife.mono_bitmap import MonoBitmap
Expand All @@ -16,8 +16,6 @@
from tools.configs.font import FontConfig
from tools.utils import fs_util

logger = logging.getLogger(__name__)


class GlyphFile:
@staticmethod
Expand Down Expand Up @@ -186,7 +184,7 @@ def format_glyph_files(self):
file_dir.mkdir(parents=True, exist_ok=True)
glyph_file.file_path.rename(file_path)
glyph_file.file_path = file_path
logger.info("Format glyph file path: '%s'", glyph_file.file_path)
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):
Expand Down Expand Up @@ -372,9 +370,9 @@ def make_fonts(self, font_format: str):
builder.save_otf(file_path, flavor=Flavor.WOFF2)
else:
getattr(builder, f'save_{font_format}')(file_path)
logger.info("Make font: '%s'", file_path)
logger.info("Make font: '{}'", file_path)
else:
collection_builder = self._get_collection_builder()
file_path = path_define.outputs_dir.joinpath(f'fusion-pixel-{self.design_context.font_config.font_size}px-{self.width_mode}.{font_format}')
getattr(collection_builder, f'save_{font_format}')(file_path)
logger.info("Make font collection: '%s'", file_path)
logger.info("Make font collection: '{}'", file_path)
7 changes: 2 additions & 5 deletions tools/services/image_service.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import logging

from PIL import Image, ImageFont, ImageDraw
from PIL.ImageFont import FreeTypeFont
from loguru import logger

from tools.configs import path_define
from tools.configs.font import FontConfig

logger = logging.getLogger(__name__)


def _load_font(font_config: FontConfig, width_mode: str, language_flavor: str, scale: int = 1) -> FreeTypeFont:
file_path = path_define.outputs_dir.joinpath(f'fusion-pixel-{font_config.font_size}px-{width_mode}-{language_flavor}.woff2')
Expand Down Expand Up @@ -63,4 +60,4 @@ def make_preview_image(font_config: FontConfig):
path_define.outputs_dir.mkdir(parents=True, exist_ok=True)
file_path = path_define.outputs_dir.joinpath(f'preview-{font_config.font_size}px.png')
image.save(file_path)
logger.info("Make preview image: '%s'", file_path)
logger.info("Make preview image: '{}'", file_path)
8 changes: 3 additions & 5 deletions tools/services/info_service.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import logging
from collections import defaultdict
from collections.abc import Callable
from io import StringIO
from typing import TextIO

import unidata_blocks
from character_encoding_utils import gb2312, big5, shiftjis, ksx1001
from loguru import logger
from unidata_blocks import UnicodeBlock

from tools import configs
from tools.configs import path_define
from tools.services.font_service import DesignContext

logger = logging.getLogger(__name__)


def _get_unicode_chr_count_infos(alphabet: set[str]) -> list[tuple[UnicodeBlock, int]]:
count_infos = defaultdict(int)
Expand Down Expand Up @@ -147,7 +145,7 @@ def make_font_info(design_context: DesignContext, width_mode: str):
path_define.outputs_dir.mkdir(parents=True, exist_ok=True)
file_path = path_define.outputs_dir.joinpath(f'font-info-{design_context.font_config.font_size}px-{width_mode}.md')
file_path.write_text(output.getvalue(), 'utf-8')
logger.info("Make font info: '%s'", file_path)
logger.info("Make font info: '{}'", file_path)


def make_alphabet_txt(design_context: DesignContext, width_mode: str):
Expand All @@ -156,4 +154,4 @@ def make_alphabet_txt(design_context: DesignContext, width_mode: str):
path_define.outputs_dir.mkdir(parents=True, exist_ok=True)
file_path = path_define.outputs_dir.joinpath(f'alphabet-{design_context.font_config.font_size}px-{width_mode}.txt')
file_path.write_text(''.join(alphabet), 'utf-8')
logger.info("Make alphabet txt: '%s'", file_path)
logger.info("Make alphabet txt: '{}'", file_path)
9 changes: 4 additions & 5 deletions tools/services/publish_service.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import logging
import re
import shutil
import zipfile

from loguru import logger

from tools import configs
from tools.configs import path_define

logger = logging.getLogger(__name__)


def make_release_zip(font_size: int, width_mode: str, font_format: str):
path_define.releases_dir.mkdir(parents=True, exist_ok=True)
Expand All @@ -23,7 +22,7 @@ def make_release_zip(font_size: int, width_mode: str, font_format: str):
else:
font_file_name = f'fusion-pixel-{font_size}px-{width_mode}.{font_format}'
file.write(path_define.outputs_dir.joinpath(font_file_name), font_file_name)
logger.info("Make release zip: '%s'", file_path)
logger.info("Make release zip: '{}'", file_path)


def update_docs():
Expand All @@ -35,4 +34,4 @@ def update_docs():
path_to = path_define.docs_dir.joinpath(path_from.relative_to(path_define.outputs_dir))
path_to.parent.mkdir(parents=True, exist_ok=True)
shutil.copyfile(path_from, path_to)
logger.info("Copy file: '%s' -> '%s'", path_from, path_to)
logger.info("Copy file: '{}' -> '{}'", path_from, path_to)
6 changes: 2 additions & 4 deletions tools/services/template_service.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import logging
import random

import bs4
from jinja2 import Environment, FileSystemLoader
from loguru import logger

from tools import configs
from tools.configs import path_define
from tools.configs.font import FontConfig
from tools.services.font_service import DesignContext

logger = logging.getLogger(__name__)

_environment = Environment(
trim_blocks=True,
lstrip_blocks=True,
Expand Down Expand Up @@ -39,7 +37,7 @@ def _make_html(template_name: str, file_name: str, params: dict[str, object] | N
path_define.outputs_dir.mkdir(parents=True, exist_ok=True)
file_path = path_define.outputs_dir.joinpath(file_name)
file_path.write_text(html, 'utf-8')
logger.info("Make html: '%s'", file_path)
logger.info("Make html: '{}'", file_path)


def make_alphabet_html(design_context: DesignContext, width_mode: str):
Expand Down
29 changes: 14 additions & 15 deletions tools/services/update_service.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import logging
import os
import shutil
import zipfile

from loguru import logger

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

logger = logging.getLogger(__name__)


def update_ark_pixel_glyphs_version():
repository_name = 'TakWolf/ark-pixel-font'
Expand Down Expand Up @@ -40,7 +39,7 @@ 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)
logger.info("Update version file: '%s'", file_path)
logger.info("Update version file: '{}'", file_path)


def setup_ark_pixel_glyphs():
Expand All @@ -62,18 +61,18 @@ def setup_ark_pixel_glyphs():
source_file_path = downloads_dir.joinpath(f'{sha}.zip')
if not source_file_path.exists():
asset_url = version_info['asset_url']
logger.info("Start download: '%s'", asset_url)
logger.info("Start download: '{}'", asset_url)
downloads_dir.mkdir(parents=True, exist_ok=True)
download_util.download_file(asset_url, source_file_path)
else:
logger.info("Already downloaded: '%s'", source_file_path)
logger.info("Already downloaded: '{}'", source_file_path)

source_unzip_dir = downloads_dir.joinpath(f'ark-pixel-font-{sha}')
if source_unzip_dir.exists():
shutil.rmtree(source_unzip_dir)
with zipfile.ZipFile(source_file_path) as file:
file.extractall(downloads_dir)
logger.info("Unzip: '%s'", source_unzip_dir)
logger.info("Unzip: '{}'", source_unzip_dir)

if path_define.ark_pixel_glyphs_dir.exists():
shutil.rmtree(path_define.ark_pixel_glyphs_dir)
Expand All @@ -100,15 +99,15 @@ 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)
logger.info("Update glyphs: '%s'", sha)
logger.info("Update glyphs: '{}'", sha)


def update_fonts(update_config: UpdateConfig):
if update_config.tag_name is None:
tag_name = github_api.get_releases_latest_tag_name(update_config.repository_name)
else:
tag_name = update_config.tag_name
logger.info("'%s' tag: '%s'", update_config.repository_name, tag_name)
logger.info("'{}' tag: '{}'", update_config.repository_name, tag_name)
version = tag_name.removeprefix('v')

fonts_dir = path_define.fonts_dir.joinpath(update_config.name)
Expand All @@ -117,7 +116,7 @@ def update_fonts(update_config: UpdateConfig):
version_info = fs_util.read_json(version_file_path)
if version == version_info['version']:
return
logger.info("Need update fonts: '%s'", update_config.name)
logger.info("Need update fonts: '{}'", update_config.name)

repository_url = f'https://github.com/{update_config.repository_name}'
downloads_dir = path_define.downloads_dir.joinpath(update_config.repository_name, tag_name)
Expand All @@ -136,23 +135,23 @@ def update_fonts(update_config: UpdateConfig):
asset_url = f'{repository_url}/releases/download/{tag_name}/{asset_file_name}'
asset_file_path = downloads_dir.joinpath(asset_file_name)
if not asset_file_path.exists():
logger.info("Start download: '%s'", asset_url)
logger.info("Start download: '{}'", asset_url)
download_util.download_file(asset_url, asset_file_path)
else:
logger.info("Already downloaded: '%s'", asset_file_path)
logger.info("Already downloaded: '{}'", asset_file_path)

asset_unzip_dir = asset_file_path.with_suffix('')
if asset_unzip_dir.exists():
shutil.rmtree(asset_unzip_dir)
with zipfile.ZipFile(asset_file_path) as file:
file.extractall(asset_unzip_dir)
logger.info("Unzip: '%s'", asset_unzip_dir)
logger.info("Unzip: '{}'", asset_unzip_dir)

for copy_info in asset_config.copy_list:
from_path = asset_unzip_dir.joinpath(copy_info[0].format(version=version))
to_path = fonts_dir.joinpath(copy_info[1].format(version=version))
shutil.copyfile(from_path, to_path)
logger.info("Copy from '%s' to '%s'", from_path, to_path)
logger.info("Copy from '{}' to '{}'", from_path, to_path)
if asset_unzip_dir.exists():
shutil.rmtree(asset_unzip_dir)

Expand All @@ -163,4 +162,4 @@ def update_fonts(update_config: UpdateConfig):
}
version_file_path = fonts_dir.joinpath('version.json')
fs_util.write_json(version_info, version_file_path)
logger.info("Update version file: '%s'", version_file_path)
logger.info("Update version file: '{}'", version_file_path)

0 comments on commit d874971

Please sign in to comment.