Commit 75bd62b4 authored by Yuki Shiino's avatar Yuki Shiino Committed by Commit Bot

bind-gen: Introduce bind_gen/name_style.py to support name conversion

Bug: 839389
Change-Id: I6a1aa633eac896f79023c0bd64ad3f539c383538
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1897556
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Cr-Commit-Position: refs/heads/master@{#712530}
parent 996f6a35
# Copyright 2019 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
A collection of functions that format a variety of names (class name, function
name, variable name, etc.)
The functions are grouped into two.
xxx(*args):
The name is made by concatenating the arguments.
xxx_f(format_string, *args, **kwargs):
The name is formatted with the given format string and arguments.
"""
from blinkbuild.name_style_converter import NameStyleConverter
def api_func(*args):
"""Applies the style of Blink implementation function names for Web API."""
return _concat(_lower_camel_case, args)
def api_func_f(format_string, *args, **kwargs):
"""Applies the style of Blink implementation function names for Web API."""
return _lower_camel_case(
_format(_upper_camel_case, format_string, *args, **kwargs))
def arg(*args):
"""Applies the style of argument variable names."""
return _concat(_snake_case, args)
def arg_f(format_string, *args, **kwargs):
"""Applies the style of argument variable names."""
return _format(_snake_case, format_string, *args, **kwargs)
def class_(*args):
"""Applies the style of class names."""
return _concat(_upper_camel_case, args)
def class_f(format_string, *args, **kwargs):
"""Applies the style of class names."""
return _format(_upper_camel_case, format_string, *args, **kwargs)
def constant(*args):
"""Applies the style of constant names."""
return "k" + _concat(_upper_camel_case, args)
def constant_f(format_string, *args, **kwargs):
"""Applies the style of constant names."""
return "k" + _upper_camel_case(
_format(_upper_camel_case, format_string, *args, **kwargs))
def file(*args):
"""Applies the style of filenames."""
return _concat(_snake_case, args)
def file_f(format_string, *args, **kwargs):
"""Applies the style of filenames."""
return _format(_snake_case, format_string, *args, **kwargs)
def func(*args):
"""Applies the style of general Blink function names."""
return _concat(_upper_camel_case, args)
def func_f(format_string, *args, **kwargs):
"""Applies the style of general Blink function names."""
return _format(_upper_camel_case, format_string, *args, **kwargs)
def header_guard(*args):
"""Applies the style of header guard names."""
return _concat(_macro_case, args) + "_"
def header_guard_f(format_string, *args, **kwargs):
"""Applies the style of header guard names."""
return _format(_macro_case, format_string, *args, **kwargs) + "_"
def local_var(*args):
"""Applies the style of function local variable names."""
return _concat(_snake_case, args)
def local_var_f(format_string, *args, **kwargs):
"""Applies the style of function local variable names."""
return _format(_snake_case, format_string, *args, **kwargs)
def macro(*args):
"""Applies the style of macro names."""
return _concat(_macro_case, args)
def macro_f(format_string, *args, **kwargs):
"""Applies the style of macro names."""
return _format(_macro_case, format_string, *args, **kwargs)
def member_var(*args):
"""Applies the style of member variable names."""
return _concat(_snake_case, args) + "_"
def member_var_f(format_string, *args, **kwargs):
"""Applies the style of member variable names."""
return _format(_snake_case, format_string, *args, **kwargs) + "_"
def namespace(*args):
"""Applies the style of namespace names."""
return _concat(_snake_case, args)
def namespace_f(format_string, *args, **kwargs):
"""Applies the style of namespace names."""
return _format(_snake_case, format_string, *args, **kwargs)
def _concat(style_func, args):
assert callable(style_func)
return style_func(" ".join(map(str, args)))
def _format(style_func, format_string, *args, **kwargs):
assert callable(style_func)
assert isinstance(format_string, str)
args = map(style_func, map(str, args))
for key, value in kwargs.iteritems():
kwargs[key] = style_func(str(value))
return format_string.format(*args, **kwargs)
def _snake_case(name):
return NameStyleConverter(name).to_snake_case()
def _upper_camel_case(name):
return NameStyleConverter(name).to_upper_camel_case()
def _lower_camel_case(name):
return NameStyleConverter(name).to_lower_camel_case()
def _macro_case(name):
return NameStyleConverter(name).to_macro_case()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment