Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: third_party/WebKit/Source/bindings/scripts/name_style_converter.py

Issue 2684773003: [WebAgent API]: Implement name style handling. (Closed)
Patch Set: Feedback addressed. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2017 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
5 # pylint: disable=import-error,print-statement,relative-import
6
7 import re
8
9 SPECIAL_PREFIXES = [
10 'WebGL',
11 'SVG',
12 ]
13
14 MATCHING_EXPRESSION = '((?:[A-Z][a-z]+)|[0-9]D?$)'
15
16
17 class SmartTokenizer(object):
18 """Detects special cases that are not easily discernible without additional
19 knowledge, such as recognizing that in SVGSVGElement, the first two SVGs
20 are separate tokens, but WebGL is one token."""
21
22 def __init__(self, name):
23 self.remaining = name
24
25 def detect_special_prefix(self):
26 for prefix in SPECIAL_PREFIXES:
27 if self.remaining.startswith(prefix):
28 result = self.remaining[:len(prefix)]
29 self.remaining = self.remaining[len(prefix):]
30 return result
31 return None
32
33 def tokenize(self):
34 prefix = self.detect_special_prefix()
35 return filter(None,
36 [prefix] + re.split(MATCHING_EXPRESSION, self.remaining))
37
38
39 class NameStyleConverter(object):
40 """Converts names from camelCase and other styles to various other styles.
41 """
42
43 def __init__(self, name):
44 self.tokens = self.tokenize(name)
45
46 def tokenize(self, name):
47 tokenizer = SmartTokenizer(name)
48 return tokenizer.tokenize()
49
50 def to_snake_case(self):
51 """Snake case is the file and variable name style per Google C++ Style
52 Guide:
53 https://google.github.io/styleguide/cppguide.html#Variable_Names
54
55 Also known as the hacker case.
56 https://en.wikipedia.org/wiki/Snake_case
57 """
58 return '_'.join([token.lower() for token in self.tokens])
59
60 def to_upper_camel_case(self):
61 """Upper-camel case is the class and function name style per
62 Google C++ Style Guide:
63 https://google.github.io/styleguide/cppguide.html#Function_Names
64
65 Also known as the PascalCase.
66 https://en.wikipedia.org/wiki/Camel_case.
67 """
68 return ''.join([token[0].upper() + token[1:] for token in self.tokens])
69
70 def to_macro_case(self):
71 """Macro case is the macro name style per Google C++ Style Guide:
72 https://google.github.io/styleguide/cppguide.html#Macro_Names
73 """
74 return '_'.join([token.upper() for token in self.tokens])
75
76 def to_all_cases(self):
77 return {
78 'snake_case': self.to_snake_case(),
79 'upper_camel_case': self.to_upper_camel_case(),
80 'macro_case': self.to_macro_case(),
81 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698