1# Copyright 2019 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4"""
5A collection of functions that format a variety of names (class name, function
6name, variable name, etc.)
7
8The functions are grouped into two.
9
10xxx(*args):
11    The name is made by concatenating the arguments.
12
13xxx_f(format_string, *args, **kwargs):
14    The name is formatted with the given format string and arguments.
15"""
16
17from blinkbuild import name_style_converter
18
19
20def api_func(*args):
21    """Applies the style of Blink implementation function names for Web API."""
22    return _concat(raw.lower_camel_case, args)
23
24
25def api_func_f(format_string, *args, **kwargs):
26    """Applies the style of Blink implementation function names for Web API."""
27    return raw.lower_camel_case(
28        _format(raw.upper_camel_case, format_string, *args, **kwargs))
29
30
31def arg(*args):
32    """Applies the style of argument variable names."""
33    return _concat(raw.snake_case, args)
34
35
36def arg_f(format_string, *args, **kwargs):
37    """Applies the style of argument variable names."""
38    return _format(raw.snake_case, format_string, *args, **kwargs)
39
40
41def class_(*args):
42    """Applies the style of class names."""
43    return _concat(raw.upper_camel_case, args)
44
45
46def class_f(format_string, *args, **kwargs):
47    """Applies the style of class names."""
48    return _format(raw.upper_camel_case, format_string, *args, **kwargs)
49
50
51def constant(*args):
52    """Applies the style of constant names."""
53    return "k" + _concat(raw.upper_camel_case, args)
54
55
56def constant_f(format_string, *args, **kwargs):
57    """Applies the style of constant names."""
58    return "k" + raw.upper_camel_case(
59        _format(raw.upper_camel_case, format_string, *args, **kwargs))
60
61
62def file(*args):
63    """Applies the style of filenames."""
64    return _concat(raw.snake_case, args)
65
66
67def file_f(format_string, *args, **kwargs):
68    """Applies the style of filenames."""
69    return _format(raw.snake_case, format_string, *args, **kwargs)
70
71
72def func(*args):
73    """Applies the style of general Blink function names."""
74    return _concat(raw.upper_camel_case, args)
75
76
77def func_f(format_string, *args, **kwargs):
78    """Applies the style of general Blink function names."""
79    return _format(raw.upper_camel_case, format_string, *args, **kwargs)
80
81
82def header_guard(*args):
83    """Applies the style of header guard names."""
84    return _concat(raw.macro_case, args) + "_"
85
86
87def header_guard_f(format_string, *args, **kwargs):
88    """Applies the style of header guard names."""
89    return _format(raw.macro_case, format_string, *args, **kwargs) + "_"
90
91
92def local_var(*args):
93    """Applies the style of function local variable names."""
94    return _concat(raw.snake_case, args)
95
96
97def local_var_f(format_string, *args, **kwargs):
98    """Applies the style of function local variable names."""
99    return _format(raw.snake_case, format_string, *args, **kwargs)
100
101
102def macro(*args):
103    """Applies the style of macro names."""
104    return _concat(raw.macro_case, args)
105
106
107def macro_f(format_string, *args, **kwargs):
108    """Applies the style of macro names."""
109    return _format(raw.macro_case, format_string, *args, **kwargs)
110
111
112def member_var(*args):
113    """Applies the style of member variable names."""
114    return _concat(raw.snake_case, args) + "_"
115
116
117def member_var_f(format_string, *args, **kwargs):
118    """Applies the style of member variable names."""
119    return _format(raw.snake_case, format_string, *args, **kwargs) + "_"
120
121
122def namespace(*args):
123    """Applies the style of namespace names."""
124    return _concat(raw.snake_case, args)
125
126
127def namespace_f(format_string, *args, **kwargs):
128    """Applies the style of namespace names."""
129    return _format(raw.snake_case, format_string, *args, **kwargs)
130
131
132def _concat(style_func, args):
133    assert callable(style_func)
134
135    return style_func(" ".join(map(_tokenize, args)))
136
137
138def _format(style_func, format_string, *args, **kwargs):
139    assert callable(style_func)
140    assert isinstance(format_string, str)
141
142    args = map(style_func, map(_tokenize, args))
143    for key, value in kwargs.items():
144        kwargs[key] = style_func(_tokenize(value))
145    return format_string.format(*args, **kwargs)
146
147
148def _tokenize(s):
149    s = str(s)
150    if "_" in s and s.isupper():
151        # NameStyleConverter doesn't treat "ABC_DEF" as two tokens of "abc" and
152        # "def" while treating "abc_def" as "abc" and "def".  Help
153        # NameStyleConverter by lowering the string.
154        return s.lower()
155    return s
156
157
158class raw(object):
159    """
160    Namespace to provide (unrecommended) raw controls on case conversions.
161
162    This class is pretending to be a module.
163    """
164
165    _NameStyleConverter = name_style_converter.NameStyleConverter
166
167    def __init__(self):
168        assert False
169
170    @staticmethod
171    def tokenize(name):
172        return name_style_converter.tokenize_name(name)
173
174    @staticmethod
175    def snake_case(name):
176        return raw._NameStyleConverter(name).to_snake_case()
177
178    @staticmethod
179    def upper_camel_case(name):
180        return raw._NameStyleConverter(name).to_upper_camel_case()
181
182    @staticmethod
183    def lower_camel_case(name):
184        return raw._NameStyleConverter(name).to_lower_camel_case()
185
186    @staticmethod
187    def macro_case(name):
188        return raw._NameStyleConverter(name).to_macro_case()
189