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

Side by Side Diff: tools/json_schema_compiler/cpp_util.py

Issue 437883002: Make the root_namespace argument to json_schema_compiler.gypi a string (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: un-escape %% for windows Created 6 years, 4 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 | Annotate | Revision Log
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Utilies and constants specific to Chromium C++ code. 5 """Utilies and constants specific to Chromium C++ code.
6 """ 6 """
7 7
8 from code import Code 8 from code import Code
9 from datetime import datetime 9 from datetime import datetime
10 from model import PropertyType 10 from model import PropertyType
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 PropertyType.STRING): 83 PropertyType.STRING):
84 arg = 'const %(type)s& %(name)s' 84 arg = 'const %(type)s& %(name)s'
85 else: 85 else:
86 arg = '%(type)s %(name)s' 86 arg = '%(type)s %(name)s'
87 return arg % { 87 return arg % {
88 'type': type_, 88 'type': type_,
89 'name': param.unix_name, 89 'name': param.unix_name,
90 } 90 }
91 91
92 92
93 def GenerateIfndefName(path, filename): 93 def GenerateIfndefName(file_path):
94 """Formats a path and filename as a #define name. 94 """Formats |file_path| as a #define name. Presumably |file_path| is a header
95 file, or there's little point in generating a #define for it.
95 96
96 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__. 97 e.g chrome/extensions/gen/file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__.
97 """ 98 """
98 return (('%s_%s_H__' % (path, filename)) 99 return (('%s__' % file_path).upper()
99 .upper().replace(os.sep, '_').replace('/', '_')) 100 .replace('\\', '_')
101 .replace('/', '_')
102 .replace('.', '_'))
100 103
101 104
102 def PadForGenerics(var): 105 def PadForGenerics(var):
103 """Appends a space to |var| if it ends with a >, so that it can be compiled 106 """Appends a space to |var| if it ends with a >, so that it can be compiled
104 within generic types. 107 within generic types.
105 """ 108 """
106 return ('%s ' % var) if var.endswith('>') else var 109 return ('%s ' % var) if var.endswith('>') else var
107 110
108 111
109 def OpenNamespace(namespace): 112
113 def OpenNamespace(cpp_namespace):
110 """Get opening root namespace declarations. 114 """Get opening root namespace declarations.
111 """ 115 """
112 c = Code() 116 c = Code()
113 # In lieu of GYP supporting None for the namespace variable the '' namespace 117 for component in cpp_namespace.split('::'):
114 # implies there is no root namespace.
115 if namespace == '':
116 return c
117 for component in namespace.split('::'):
118 c.Append('namespace %s {' % component) 118 c.Append('namespace %s {' % component)
119 return c 119 return c
120 120
121 121
122 def CloseNamespace(namespace): 122 def CloseNamespace(cpp_namespace):
123 """Get closing root namespace declarations. 123 """Get closing root namespace declarations.
124 """ 124 """
125 c = Code() 125 c = Code()
126 # In lieu of GYP supporting None for the namespace variable the '' namespace 126 for component in reversed(cpp_namespace.split('::')):
127 # implies there is no root namespace.
128 if namespace == '':
129 return c
130 for component in reversed(namespace.split('::')):
131 c.Append('} // namespace %s' % component) 127 c.Append('} // namespace %s' % component)
132 return c 128 return c
133 129
134 130
135 def ConstantName(feature_name): 131 def ConstantName(feature_name):
136 """Returns a kName for a feature's name. 132 """Returns a kName for a feature's name.
137 """ 133 """
138 return ('k' + ''.join(word[0].upper() + word[1:] 134 return ('k' + ''.join(word[0].upper() + word[1:]
139 for word in feature_name.replace('.', ' ').split())) 135 for word in feature_name.replace('.', ' ').split()))
140 136
141 137
142 def CamelCase(unix_name): 138 def CamelCase(unix_name):
143 return ''.join(word.capitalize() for word in unix_name.split('_')) 139 return ''.join(word.capitalize() for word in unix_name.split('_'))
144 140
145 141
146 def ClassName(filepath): 142 def ClassName(filepath):
147 return CamelCase(os.path.split(filepath)[1]) 143 return CamelCase(os.path.split(filepath)[1])
144
145
146 def GetCppNamespace(pattern, namespace):
147 '''Returns the C++ namespace given |pattern| which includes a %(namespace)s
148 substitution, and the |namespace| to substitute. It is expected that |pattern|
149 has been passed as a flag to compiler.py from GYP/GN.
150 '''
151 # For some reason Windows builds escape the % characters, so unescape them.
152 # This means that %% can never appear legitimately within a pattern, but
153 # that's ok. It should never happen.
154 cpp_namespace = pattern.replace('%%', '%') % { 'namespace': namespace }
not at google - send to devlin 2014/08/05 04:37:59 after some debugging, turns out I needed to do thi
155 assert '%' not in cpp_namespace, \
156 ('Did not manage to fully substitute namespace "%s" into pattern "%s"'
157 % (namespace, pattern))
158 return cpp_namespace
OLDNEW
« no previous file with comments | « tools/json_schema_compiler/cpp_type_generator_test.py ('k') | tools/json_schema_compiler/cpp_util_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698