Skip to content

Commit

Permalink
Automatically update unicode data once a month.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocaccamo committed Jan 12, 2023
1 parent c6220f9 commit 9d45dcf
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 43 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/update-data.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Update data

on:
workflow_dispatch:
schedule:
- cron: "0 0 1 * *" # run once a month

jobs:
resources:
name: Update resources
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.8'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-data.txt
- name: Update data
run: python scripts/update_data.py

- name: Commit data
uses: test-room-7/action-update-file@v1
with:
file-path: |
fontbro/data/unicode-blocks.json
fontbro/data/unicode-scripts.json
commit-msg: "Update `unicode-blocks.json` and `unicode-scripts.json` data."
committer-name: "Fabio Caccamo [bot]"
committer-email: "[email protected]"
github-token: ${{ secrets.WORKFLOWS_UPDATE_DATA_TOKEN }}
4 changes: 4 additions & 0 deletions requirements-data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fonttools[woff,unicode,pathops] == 4.38.0
imagehash == 4.3.1
pillow == 9.4.0
python-fsutil == 0.9.3
50 changes: 50 additions & 0 deletions scripts/update_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import fsutil
from fontTools import unicodedata

from fontbro import Font


def _write_data_json(filepath, data):
data_filepath = fsutil.join_path(__file__, filepath)
fsutil.write_file_json(data_filepath, data, indent=4, sort_keys=True)


def update_unicode_data():
blocks = []
blocks_cache = {}
scripts = []
scripts_cache = {}
for code in range(0, 0x110000):
block_name = unicodedata.block(code)
if block_name == "No_Block":
continue
block = {
"name": block_name,
}
Font._populate_unicode_items_set(blocks, blocks_cache, block)
script_tag = unicodedata.script(code)
script_name = unicodedata.script_name(script_tag)
script = {
"name": script_name,
"tag": script_tag,
}
Font._populate_unicode_items_set(scripts, scripts_cache, script)

for block in blocks:
block["characters_total"] = block["characters_count"]
block.pop("characters_count", None)

for script in scripts:
script["characters_total"] = script["characters_count"]
script.pop("characters_count", None)

_write_data_json(filepath="../fontbro/data/unicode-blocks.json", data=blocks)
_write_data_json(filepath="../fontbro/data/unicode-scripts.json", data=scripts)


def main():
update_unicode_data()


if __name__ == "__main__":
main()
45 changes: 2 additions & 43 deletions tests/test_update_unicode_data.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import json

import fsutil
from fontTools import unicodedata

from fontbro import Font
from scripts.update_data import update_unicode_data
from tests import AbstractTestCase


Expand All @@ -13,40 +8,4 @@ class UpdateUnicodeDataTestCase(AbstractTestCase):
"""

def test_update_unicode_data(self):
blocks = []
blocks_cache = {}
scripts = []
scripts_cache = {}
for code in range(0, 0x110000):
block_name = unicodedata.block(code)
if block_name == "No_Block":
continue
block = {
"name": block_name,
}
Font._populate_unicode_items_set(blocks, blocks_cache, block)
script_tag = unicodedata.script(code)
script_name = unicodedata.script_name(script_tag)
script = {
"name": script_name,
"tag": script_tag,
}
Font._populate_unicode_items_set(scripts, scripts_cache, script)

for block in blocks:
block["characters_total"] = block["characters_count"]
block.pop("characters_count", None)

for script in scripts:
script["characters_total"] = script["characters_count"]
script.pop("characters_count", None)

data_dir = fsutil.join_path(__file__, "../fontbro/data/")
fsutil.write_file(
fsutil.join_path(data_dir, "unicode-blocks.json"),
json.dumps(blocks, sort_keys=True, indent=4),
)
fsutil.write_file(
fsutil.join_path(data_dir, "unicode-scripts.json"),
json.dumps(scripts, sort_keys=True, indent=4),
)
update_unicode_data()

0 comments on commit 9d45dcf

Please sign in to comment.