Cotton code
41 lines · 1.8 KB · PythonRaw
"""A painted tile's words as its painter set them, for the tile import scripts.    from tile_words import words_of    words = words_of(layers)  # None where the layout names no type`layout.json` beside the layers says how the phone sets the tile's live words(`type`): their colour, the name's first baseline and the count's under aname of one line and of two, in pixels on the 474 x 696 stock. The tilecarries them as `fan_tile.words` (`crates/cotton-core/src/definitions/fan_tile.rs`, `TileWords`), so a re-import never loses them; a key the layoutdoes not name is left out, and the phone keeps the type's own."""import jsonimport pathlibimport sysdef words_of(layers):    path = pathlib.Path(layers) / 'layout.json'    if not path.exists():        return None    kind = json.loads(path.read_text()).get('type')    if not isinstance(kind, dict):        return None    name, count = kind.get('name') or {}, kind.get('count') or {}    named = {        'ink': kind.get('colour'),        'name_baseline': name.get('baseline'),        'count_baseline_one': count.get('baselineForOneLine'),        'count_baseline_two': count.get('baselineForTwoLines'),    }    words = {key: value for key, value in named.items() if value is not None}    for key, value in words.items():        if key == 'ink':            if not (isinstance(value, str) and len(value) == 7 and value.startswith('#') and all(c in '0123456789abcdefABCDEF' for c in value[1:])):                sys.exit(f'{path}: type.colour {value!r} is no #rrggbb')            words[key] = value.lower()        elif not (isinstance(value, int) and 0 < value <= 696):            sys.exit(f'{path}: {key} {value!r} is no pixel row of the 696-high stock')    # In the order the core writes them (`TileWords`'s field order).    return {key: words[key] for key in named if key in words} or None