File: main.py

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (94 lines) | stat: -rwxr-xr-x 3,523 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env python3
# Copyright 2019 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 sys
import os

sys.path += [os.path.dirname(os.path.dirname(__file__))]

from style_variable_generator.css_generator import CSSStyleGenerator
from style_variable_generator.ts_generator import TSStyleGenerator
from style_variable_generator.proto_generator import ProtoStyleGenerator, ProtoJSONStyleGenerator
from style_variable_generator.json_generator import JSONStyleGenerator
from style_variable_generator.views_generator import ViewsCCStyleGenerator, ViewsHStyleGenerator
from style_variable_generator.base_generator import Modes
from style_variable_generator.color_mappings_generator import ColorMappingsCCStyleGenerator, ColorMappingsHStyleGenerator


def parseGeneratorOptionList(options):
    result = {}
    if options is None:
        return result
    for key_value_pair in options:
        key, value = key_value_pair.split('=', 1)
        key = key.strip()
        result[key] = value
    return result


def main():
    parser = argparse.ArgumentParser(
        description='Generate style variables from JSON5 color file.')

    generators = [
        CSSStyleGenerator, ViewsCCStyleGenerator, ViewsHStyleGenerator,
        ProtoStyleGenerator, ProtoJSONStyleGenerator, TSStyleGenerator,
        JSONStyleGenerator, ColorMappingsCCStyleGenerator,
        ColorMappingsHStyleGenerator
    ]

    parser.add_argument(
        '--generator',
        choices=[g.GetName() for g in generators],
        required=True,
        help='type of file to generate, provide this option multiple '
        'times to use multiple generators',
        action='append')
    parser.add_argument('--generate-single-mode',
                        choices=Modes.ALL,
                        help='generates output for a single mode')
    parser.add_argument(
        '--out-file',
        help='file to write output to, the number of out-files '
        'must match the number of generators if specified',
        action='append')
    parser.add_argument('--generator-option',
                        metavar='KEY=VALUE',
                        action='append',
                        help='Set a option specific to the selected generator '
                        'via a key value pair. See the README.md for a '
                        'full list of generator specific options.')
    parser.add_argument('targets', nargs='+', help='source json5 color files')

    args = parser.parse_args()

    if args.out_file and len(args.generator) != len(args.out_file):
        raise ValueError(
            'number of --out-files must match number of --generators')

    for i, g in enumerate(args.generator):
        gen_constructor = next(x for x in generators if g == x.GetName())
        style_generator = gen_constructor()

        style_generator.AddJSONFilesToModel(args.targets)

        style_generator.generate_single_mode = args.generate_single_mode
        style_generator.generator_options = parseGeneratorOptionList(
            args.generator_option)

        if args.out_file:
            style_generator.out_file_path = args.out_file[i]
            with open(args.out_file[i], 'w') as f:
                f.write(style_generator.Render())
        else:
            print('=========', style_generator.GetName(), '=========')
            print(style_generator.Render())

    return 0


if __name__ == '__main__':
    sys.exit(main())