Home | History | Annotate | Download | only in json_schema_compiler
      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
      3 # found in the LICENSE file.
      4 
      5 """Utilies and constants specific to Chromium C++ code.
      6 """
      7 
      8 from code import Code
      9 from datetime import datetime
     10 from model import PropertyType
     11 import os
     12 import re
     13 
     14 CHROMIUM_LICENSE = (
     15 """// Copyright (c) %d The Chromium Authors. All rights reserved.
     16 // Use of this source code is governed by a BSD-style license that can be
     17 // found in the LICENSE file.""" % datetime.now().year
     18 )
     19 GENERATED_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITION IN
     20 //   %s
     21 // DO NOT EDIT.
     22 """
     23 GENERATED_BUNDLE_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITIONS IN
     24 //   %s
     25 // DO NOT EDIT.
     26 """
     27 GENERATED_FEATURE_MESSAGE = """// GENERATED FROM THE FEATURE DEFINITIONS IN
     28 //   %s
     29 // DO NOT EDIT.
     30 """
     31 
     32 def Classname(s):
     33   """Translates a namespace name or function name into something more
     34   suited to C++.
     35 
     36   eg experimental.downloads -> Experimental_Downloads
     37   updateAll -> UpdateAll.
     38   """
     39   return '_'.join([x[0].upper() + x[1:] for x in re.split('\W', s)])
     40 
     41 
     42 def GetAsFundamentalValue(type_, src, dst):
     43   """Returns the C++ code for retrieving a fundamental type from a
     44   Value into a variable.
     45 
     46   src: Value*
     47   dst: Property*
     48   """
     49   return {
     50       PropertyType.BOOLEAN: '%s->GetAsBoolean(%s)',
     51       PropertyType.DOUBLE: '%s->GetAsDouble(%s)',
     52       PropertyType.INTEGER: '%s->GetAsInteger(%s)',
     53       PropertyType.STRING: '%s->GetAsString(%s)',
     54   }[type_.property_type] % (src, dst)
     55 
     56 
     57 def GetValueType(type_):
     58   """Returns the Value::Type corresponding to the model.Type.
     59   """
     60   return {
     61       PropertyType.ARRAY: 'base::Value::TYPE_LIST',
     62       PropertyType.BINARY: 'base::Value::TYPE_BINARY',
     63       PropertyType.BOOLEAN: 'base::Value::TYPE_BOOLEAN',
     64       # PropertyType.CHOICES can be any combination of types.
     65       PropertyType.DOUBLE: 'base::Value::TYPE_DOUBLE',
     66       PropertyType.ENUM: 'base::Value::TYPE_STRING',
     67       PropertyType.FUNCTION: 'base::Value::TYPE_DICTIONARY',
     68       PropertyType.INTEGER: 'base::Value::TYPE_INTEGER',
     69       PropertyType.OBJECT: 'base::Value::TYPE_DICTIONARY',
     70       PropertyType.STRING: 'base::Value::TYPE_STRING',
     71   }[type_.property_type]
     72 
     73 
     74 def GetParameterDeclaration(param, type_):
     75   """Gets a parameter declaration of a given model.Property and its C++
     76   type.
     77   """
     78   if param.type_.property_type in (PropertyType.ANY,
     79                                    PropertyType.ARRAY,
     80                                    PropertyType.CHOICES,
     81                                    PropertyType.OBJECT,
     82                                    PropertyType.REF,
     83                                    PropertyType.STRING):
     84     arg = 'const %(type)s& %(name)s'
     85   else:
     86     arg = '%(type)s %(name)s'
     87   return arg % {
     88     'type': type_,
     89     'name': param.unix_name,
     90   }
     91 
     92 
     93 def GenerateIfndefName(path, filename):
     94   """Formats a path and filename as a #define name.
     95 
     96   e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__.
     97   """
     98   return (('%s_%s_H__' % (path, filename))
     99           .upper().replace(os.sep, '_').replace('/', '_'))
    100 
    101 
    102 def PadForGenerics(var):
    103   """Appends a space to |var| if it ends with a >, so that it can be compiled
    104   within generic types.
    105   """
    106   return ('%s ' % var) if var.endswith('>') else var
    107 
    108 
    109 def OpenNamespace(namespace):
    110   """Get opening root namespace declarations.
    111   """
    112   c = Code()
    113   for component in namespace.split('::'):
    114     c.Append('namespace %s {' % component)
    115   return c
    116 
    117 
    118 def CloseNamespace(namespace):
    119   """Get closing root namespace declarations.
    120   """
    121   c = Code()
    122   for component in reversed(namespace.split('::')):
    123     c.Append('}  // namespace %s' % component)
    124   return c
    125 
    126 
    127 def ConstantName(feature_name):
    128   """Returns a kName for a feature's name.
    129   """
    130   return ('k' + ''.join(word[0].upper() + word[1:]
    131       for word in feature_name.replace('.', ' ').split()))
    132 
    133 
    134 def CamelCase(unix_name):
    135   return ''.join(word.capitalize() for word in unix_name.split('_'))
    136 
    137 
    138 def ClassName(filepath):
    139   return CamelCase(os.path.split(filepath)[1])
    140