summaryrefslogtreecommitdiff
path: root/data/fonts/script/family_builder.py
blob: 9a6f8c5538395fccb2d2dc0138535f851995fbfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python

#
# Copyright (C) 2024 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""Build Family instance with validating JSON contents."""

import dataclasses

from custom_json import _load_json_with_comment
from font_builder import Font
from font_builder import parse_fonts
from validators import check_enum_or_none
from validators import check_priority_or_none
from validators import check_str_or_none

_FAMILY_KEYS = set([
    "id",
    "lang",
    "name",
    "variant",
    "fallbackFor",
    "fonts",
    "target",
    "priority",
])


@dataclasses.dataclass
class Family:
  id: str | None
  lang: str | None
  name: str | None
  priority: int | None
  variant: str | None
  fallback_for: str | None
  target: str | None
  fonts: [Font]


def _validate_family(family):
  assert not family.lang or not family.name, (
      "If lang attribute is specified, name attribute must not be specified: %s"
      % family
  )

  if family.fallback_for:
    assert family.target, (
        "If fallbackFor is specified, must specify target: %s" % family
    )
  if family.target:
    assert family.fallback_for, (
        "If target is specified, must specify fallbackFor: %s" % family
    )


def _parse_family(obj, for_sanitization_test=False) -> Family:
  """Create Family object from dictionary."""
  unknown_keys = obj.keys() - _FAMILY_KEYS
  assert not unknown_keys, "Unknown keys found: %s in %s" % (unknown_keys, obj)

  if for_sanitization_test:
    fonts = []
  else:
    fonts = parse_fonts(obj.get("fonts"))

  family = Family(
      id=check_str_or_none(obj, "id"),
      lang=check_str_or_none(obj, "lang"),
      name=check_str_or_none(obj, "name"),
      priority=check_priority_or_none(obj, "priority"),
      variant=check_enum_or_none(obj, "variant", ["elegant", "compact"]),
      fallback_for=check_str_or_none(obj, "fallbackFor"),
      target=check_str_or_none(obj, "target"),
      fonts=fonts,
  )

  if not for_sanitization_test:
    _validate_family(family)
  return family


def parse_family_from_json_for_sanitization_test(json_str) -> Family:
  """For testing purposes."""
  return _parse_family(
      _load_json_with_comment(json_str), for_sanitization_test=True
  )


def parse_family_from_json(json_str) -> Family:
  """For testing purposes."""
  return _parse_family(_load_json_with_comment(json_str))


def parse_families_from_json(json_str) -> [Family]:
  objs = _load_json_with_comment(json_str)
  assert isinstance(objs, list), "families must be list"
  assert objs, "families must contains at least one family"
  return [_parse_family(obj) for obj in objs]