def AnnotateApi(self, the_api): """Annotate a Api with C++ specific elements.""" super(CppGenerator, self).AnnotateApi(the_api) if the_api.values.get('version') and self._options.get('version_package'): the_api.module.SetPath('%s_%s' % ( the_api.module.package_path, the_api.values.get('versionNoDots'))) for schema in the_api.TopLevelModelClasses(): filename = utilities.UnCamelCase(schema.class_name) schema.SetTemplateValue('filename', filename) schema.SetTemplateValue( 'include_path', self._HeaderFileName(schema.module.path, schema.values['filename'])) include_guard = '%s_%s_H_' % (the_api.module.name, filename) schema.SetTemplateValue('include_guard', include_guard.upper()) the_api.SetTemplateValue('filename', utilities.UnCamelCase(the_api.class_name)) the_api.SetTemplateValue( 'include_path', self._HeaderFileName(the_api.module.path, the_api.values['filename'])) monolithic_source_name = the_api.GetTemplateValue('monolithicSourceName') if monolithic_source_name: the_api.SetTemplateValue('kitchensink', monolithic_source_name) else: plain_api_filename = utilities.UnCamelCase(the_api.values['name']) the_api.SetTemplateValue('kitchensink', '%s_api' % plain_api_filename) authscopes = the_api.values.get('authscopes') if authscopes: for scope in authscopes: self.AnnotateDocumentation(scope) self.AnnotateDocumentation(the_api)
def ApplyCaseTransform(self, s, policy): """Applies a Policy's case transforms to a string. Args: s: (str) A string to transform. policy: (NamingPolicy) The naming policy to use for the transform. Returns: Case transformed string. """ # Pick the right transformer method if policy.case_transform == LOWER_CASE: transform = lambda s, _: ''.join(s).lower() elif policy.case_transform == UPPER_CASE: transform = lambda s, _: ''.join(s).upper() elif policy.case_transform == UPPER_CAMEL_CASE: transform = lambda s, _: ''.join([s[0].upper()] + s[1:]) elif policy.case_transform == LOWER_CAMEL_CASE: # pylint: disable=g-long-lambda transform = (lambda s, first_word: ''.join( [s[0].lower() if first_word else s[0].upper()] + s[1:])) elif policy.case_transform == LOWER_UNCAMEL_CASE: transform = lambda s, _: utilities.UnCamelCase(''.join(s)) elif policy.case_transform == UPPER_UNCAMEL_CASE: transform = lambda s, _: utilities.UnCamelCase(''.join(s)).upper() elif policy.case_transform == CAP_FIRST: # pylint: disable=g-long-lambda transform = (lambda s, first_word: ''.join( [s[0].upper() if first_word else s[0]] + s[1:])) else: transform = lambda s, _: ''.join(s) if policy.atsign_policy == ATSIGN_STRIP: s = s.replace('@', '') if policy.atsign_policy == ATSIGN_BREAK: # chr(1) is always going to not be in allowed_characters. s = s.replace('@', policy.separator or chr(1)) # Split into words at characters which can not be part of an identifier. parts = [] curpart = [] for c in s: # Collect valid characters for an identifier. if not c.isalnum() and c not in self.allowed_characters: if curpart: parts.append(transform(curpart, not parts)) curpart = [] else: # end the word when we have a bad one. curpart.append(c) if curpart: parts.append(transform(curpart, not parts)) join_char = policy.separator or '' return join_char.join(parts)
def testUnCamelCase(self): """Basic CamelCase functionality.""" # standard case self.assertEquals('hello_world', utilities.UnCamelCase('helloWorld')) self.assertEquals('hello_world', utilities.UnCamelCase('Hello_world')) self.assertEquals('hello_world', utilities.UnCamelCase('helloWorld')) self.assertEquals('hello_world', utilities.UnCamelCase('HELLO_WORLD')) self.assertEquals('hello_world', utilities.UnCamelCase('HELLOworld')) self.assertEquals('hello_world', utilities.UnCamelCase('helloWORLD')) self.assertEquals('hello2_world', utilities.UnCamelCase('Hello2World')) # keep existing separators self.assertEquals('hello_world', utilities.UnCamelCase('hello_world')) self.assertEquals('_hello_world', utilities.UnCamelCase('_hello_world')) self.assertEquals('_hello_world', utilities.UnCamelCase('_HelloWorld')) self.assertEquals('hello__world', utilities.UnCamelCase('Hello__World')) # embedded acronym self.assertEquals('hello_xw_orld', utilities.UnCamelCase('HelloXWorld')) # minimal input self.assertEquals('h', utilities.UnCamelCase('H')) self.assertEquals('', utilities.UnCamelCase('')) # Other cases involving expanded alphabet. self.assertEquals('_', utilities.UnCamelCase('_')) self.assertEquals('hello-world', utilities.UnCamelCase('hello-world')) self.assertEquals('hello.world', utilities.UnCamelCase('hello.world')) self.assertEquals('hello/world', utilities.UnCamelCase('hello/world')) self.assertEquals('hello world', utilities.UnCamelCase('Hello World')) self.assertEquals(' ', utilities.UnCamelCase(' '))