1#!/usr/bin/env python 2 3# 4# Copyright (C) 2024 The Android Open Source Project 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19"""Validators commonly used.""" 20 21 22def check_str_or_none(d, key: str) -> str | None: 23 value = d.get(key) 24 assert value is None or isinstance(value, str), ( 25 "%s type must be str or None." % key 26 ) 27 return value 28 29 30def check_str(d, key: str) -> str: 31 value = d.get(key) 32 assert isinstance(value, str), "%s type must be str." % key 33 return value 34 35 36def check_int_or_none(d, key: str) -> int | None: 37 """Chcek if the given value of key in dict is int or None.""" 38 value = d.get(key) 39 if value is None: 40 return None 41 elif isinstance(value, int): 42 return value 43 elif isinstance(value, str): 44 try: 45 return int(value) 46 except ValueError as e: 47 raise AssertionError() from e 48 else: 49 raise AssertionError("%s type must be int or str or None." % key) 50 51 52def check_float(d, key: str) -> float: 53 """Chcek if the given value of key in dict is float.""" 54 value = d.get(key) 55 if isinstance(value, float): 56 return value 57 elif isinstance(value, int): 58 return float(value) 59 elif isinstance(value, str): 60 try: 61 return float(value) 62 except ValueError as e: 63 raise AssertionError() from e 64 else: 65 raise AssertionError("Float value is expeted but it is %s" % key) 66 67 68def check_weight_or_none(d, key: str) -> int | None: 69 value = check_int_or_none(d, key) 70 71 assert value is None or ( 72 value >= 0 and value <= 1000 73 ), "weight must be larger than 0 and lower than 1000." 74 return value 75 76 77def check_priority_or_none(d, key: str) -> int | None: 78 value = check_int_or_none(d, key) 79 80 assert value is None or ( 81 value >= -100 and value <= 100 82 ), "priority must be between -100 (highest) to 100 (lowest)" 83 return value 84 85 86def check_enum_or_none(d, key: str, enum: [str]) -> str | None: 87 value = check_str_or_none(d, key) 88 89 assert value is None or value in enum, "%s must be None or one of %s" % ( 90 key, 91 enum, 92 ) 93 return value 94 95 96def check_tag(value) -> str: 97 if len(value) != 4 or not value.isascii(): 98 raise AssertionError("OpenType tag must be 4 ASCII letters: %s" % value) 99 return value 100