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 Property, PropertyType, Type
     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 
     28 def Classname(s):
     29   """Translates a namespace name or function name into something more
     30   suited to C++.
     31 
     32   eg experimental.downloads -> Experimental_Downloads
     33   updateAll -> UpdateAll.
     34   """
     35   return '_'.join([x[0].upper() + x[1:] for x in re.split('\W', s)])
     36 
     37 def GetAsFundamentalValue(type_, src, dst):
     38   """Returns the C++ code for retrieving a fundamental type from a
     39   Value into a variable.
     40 
     41   src: Value*
     42   dst: Property*
     43   """
     44   return {
     45       PropertyType.BOOLEAN: '%s->GetAsBoolean(%s)',
     46       PropertyType.DOUBLE: '%s->GetAsDouble(%s)',
     47       PropertyType.INTEGER: '%s->GetAsInteger(%s)',
     48       PropertyType.STRING: '%s->GetAsString(%s)',
     49   }[type_.property_type] % (src, dst)
     50 
     51 def GetValueType(type_):
     52   """Returns the Value::Type corresponding to the model.Type.
     53   """
     54   return {
     55       PropertyType.ARRAY: 'base::Value::TYPE_LIST',
     56       PropertyType.BINARY: 'base::Value::TYPE_BINARY',
     57       PropertyType.BOOLEAN: 'base::Value::TYPE_BOOLEAN',
     58       # PropertyType.CHOICES can be any combination of types.
     59       PropertyType.DOUBLE: 'base::Value::TYPE_DOUBLE',
     60       PropertyType.ENUM: 'base::Value::TYPE_STRING',
     61       PropertyType.FUNCTION: 'base::Value::TYPE_DICTIONARY',
     62       PropertyType.INTEGER: 'base::Value::TYPE_INTEGER',
     63       PropertyType.OBJECT: 'base::Value::TYPE_DICTIONARY',
     64       PropertyType.STRING: 'base::Value::TYPE_STRING',
     65   }[type_.property_type]
     66 
     67 def GetParameterDeclaration(param, type_):
     68   """Gets a parameter declaration of a given model.Property and its C++
     69   type.
     70   """
     71   if param.type_.property_type in (PropertyType.ANY,
     72                                    PropertyType.ARRAY,
     73                                    PropertyType.CHOICES,
     74                                    PropertyType.OBJECT,
     75                                    PropertyType.REF,
     76                                    PropertyType.STRING):
     77     arg = 'const %(type)s& %(name)s'
     78   else:
     79     arg = '%(type)s %(name)s'
     80   return arg % {
     81     'type': type_,
     82     'name': param.unix_name,
     83   }
     84 
     85 def GenerateIfndefName(path, filename):
     86   """Formats a path and filename as a #define name.
     87 
     88   e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__.
     89   """
     90   return (('%s_%s_H__' % (path, filename))
     91           .upper().replace(os.sep, '_').replace('/', '_'))
     92 
     93 def PadForGenerics(var):
     94   """Appends a space to |var| if it ends with a >, so that it can be compiled
     95   within generic types.
     96   """
     97   return ('%s ' % var) if var.endswith('>') else var
     98 
     99 def OpenNamespace(namespace):
    100   """Get opening root namespace declarations.
    101   """
    102   c = Code()
    103   for component in namespace.split('::'):
    104     c.Append('namespace %s {' % component)
    105   return c
    106 
    107 def CloseNamespace(namespace):
    108   """Get closing root namespace declarations.
    109   """
    110   c = Code()
    111   for component in reversed(namespace.split('::')):
    112     c.Append('}  // namespace %s' % component)
    113   return c
    114