File: emoticon_data.py

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (83 lines) | stat: -rw-r--r-- 2,357 bytes parent folder | download | duplicates (7)
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
# Copyright 2021 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import argparse
import json
import os
import sys

_SCRIPT_DIR = os.path.realpath(os.path.dirname(__file__))
_CHROME_SOURCE = os.path.realpath(
    os.path.join(_SCRIPT_DIR, *[os.path.pardir] * 5))
sys.path.append(os.path.join(_CHROME_SOURCE, 'build'))

import action_helpers

# Set of unicode characters that do not render with fonts available on ChromeOS
# TODO(b:267370102) add font(s) such that there are no more invalid characters
INVALID_CHARACTERS = set([
    '\u2688\u0325',
    '\ufe52\ufe20',
    '\u0644',
])


def isValidEmoticon(string):
    for symbol in INVALID_CHARACTERS:
        if symbol in string:
            return False
    return True


def process_emoticon_data(metadata):
    """Produce the emoticon data to be consumed by the emoji picker.
    Args:
        metadata (list(dict)): list of emoticon group data.

    Returns:
        list(dict): list of readily used emoticon groups.
    """
    return [{
        'group':
        group['group'],
        'emoji': [{
            'base': {
                'string': emoticon['value'],
                'name': emoticon['description'],
            },
        } for emoticon in group['emoticon']
                  if isValidEmoticon(emoticon['value'])]
    } for group in metadata]


def main(args):
    parser = argparse.ArgumentParser()
    parser.add_argument('--metadata',
                        required=True,
                        help='emoji metadata ordering file as JSON')
    parser.add_argument('--output',
                        required=True,
                        help='output JSON file path')
    options = parser.parse_args(args)

    metadata_file = options.metadata
    output_file = options.output

    # Parse emoticon ordering data.
    metadata = []
    with open(metadata_file, 'r') as file:
        metadata = json.load(file)

    emoticon_data = process_emoticon_data(metadata)

    # Write output file atomically in utf-8 format.
    with action_helpers.atomic_output(output_file) as tmp_file:
        tmp_file.write(
            json.dumps(emoticon_data,
                       separators=(',', ':'),
                       ensure_ascii=False).encode('utf-8'))


if __name__ == '__main__':
    main(sys.argv[1:])