| OLD | NEW |
| 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 """Utilies and constants specific to Chromium C++ code. | 4 """Utilies and constants specific to Chromium C++ code. |
| 5 """ | 5 """ |
| 6 | 6 |
| 7 from datetime import datetime | 7 from datetime import datetime |
| 8 from model import PropertyType | 8 from model import PropertyType |
| 9 import re |
| 9 | 10 |
| 10 CHROMIUM_LICENSE = ( | 11 CHROMIUM_LICENSE = ( |
| 11 """// Copyright (c) %d The Chromium Authors. All rights reserved. | 12 """// Copyright (c) %d The Chromium Authors. All rights reserved. |
| 12 // Use of this source code is governed by a BSD-style license that can be | 13 // Use of this source code is governed by a BSD-style license that can be |
| 13 // found in the LICENSE file.""" % datetime.now().year | 14 // found in the LICENSE file.""" % datetime.now().year |
| 14 ) | 15 ) |
| 15 GENERATED_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITION IN | 16 GENERATED_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITION IN |
| 16 // %s | 17 // %s |
| 17 // DO NOT EDIT. | 18 // DO NOT EDIT. |
| 18 """ | 19 """ |
| 19 | 20 |
| 20 | 21 def Classname(s): |
| 21 def CppName(s): | |
| 22 """Translates a namespace name or function name into something more | 22 """Translates a namespace name or function name into something more |
| 23 suited to C++. | 23 suited to C++. |
| 24 | 24 |
| 25 eg experimental.downloads -> Experimental_Downloads | 25 eg experimental.downloads -> Experimental_Downloads |
| 26 updateAll -> UpdateAll. | 26 updateAll -> UpdateAll. |
| 27 """ | 27 """ |
| 28 return '_'.join([x[0].upper() + x[1:] for x in s.split('.')]) | 28 return '_'.join([x[0].upper() + x[1:] for x in s.split('.')]) |
| 29 | 29 |
| 30 def CreateFundamentalValue(prop, var): | 30 def CreateFundamentalValue(prop, var): |
| 31 """Returns the C++ code for creating a value of the given property type | 31 """Returns the C++ code for creating a value of the given property type |
| 32 using the given variable. | 32 using the given variable. |
| 33 |
| 34 var: raw value |
| 33 """ | 35 """ |
| 36 if prop.optional: |
| 37 var = '*' + var |
| 34 return { | 38 return { |
| 35 PropertyType.STRING: 'Value::CreateStringValue(%s)', | 39 PropertyType.STRING: 'Value::CreateStringValue(%s)', |
| 36 PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)', | 40 PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)', |
| 37 PropertyType.INTEGER: 'Value::CreateIntegerValue(%s)', | 41 PropertyType.INTEGER: 'Value::CreateIntegerValue(%s)', |
| 38 PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)', | 42 PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)', |
| 39 }[prop.type_] % var | 43 }[prop.type_] % var |
| 40 | 44 |
| 45 def GetFundamentalValue(prop, src, name, dst): |
| 46 """Returns the C++ code for retrieving a fundamental type from a |
| 47 DictionaryValue into a variable. |
| 41 | 48 |
| 42 def GetFundamentalValue(prop, var): | 49 src: DictionaryValue* |
| 43 """Returns the C++ code for retrieving a fundamental type from a Value | 50 name: key |
| 44 into a variable. | 51 dst: Property* |
| 45 """ | 52 """ |
| 46 return { | 53 return { |
| 47 PropertyType.STRING: 'GetAsString(%s)', | 54 PropertyType.STRING: '%s->GetString("%s", %s)', |
| 48 PropertyType.BOOLEAN: 'GetAsBoolean(%s)', | 55 PropertyType.BOOLEAN: '%s->GetBoolean("%s", %s)', |
| 49 PropertyType.INTEGER: 'GetAsInteger(%s)', | 56 PropertyType.INTEGER: '%s->GetInteger("%s", %s)', |
| 50 PropertyType.DOUBLE: 'GetAsDouble(%s)', | 57 PropertyType.DOUBLE: '%s->GetDouble("%s", %s)', |
| 51 }[prop.type_] % var | 58 }[prop.type_] % (src, name, dst) |
| 59 |
| 60 def CreateValueFromSingleProperty(prop, var): |
| 61 """Creates a Value given a single property. Use for everything except |
| 62 PropertyType.ARRAY. |
| 63 |
| 64 var: raw value |
| 65 """ |
| 66 if prop.type_ == PropertyType.REF or prop.type_ == PropertyType.OBJECT: |
| 67 # TODO(calamity): is this wrong? |
| 68 return '%s.ToValue()' % var |
| 69 elif prop.type_.is_fundamental: |
| 70 return CreateFundamentalValue(prop, var) |
| 71 else: |
| 72 raise NotImplementedError('%s conversion to Value not implemented' % |
| 73 repr(prop.type_)) |
| 74 |
| 75 def GetValueFromList(prop, src, index, dst): |
| 76 """Returns the C++ code for retrieving a fundamental type from a |
| 77 DictionaryValue into a variable. |
| 78 |
| 79 src: ListValue& |
| 80 index: int |
| 81 dst: Property* |
| 82 """ |
| 83 return { |
| 84 PropertyType.REF: '%s.GetDictionary(%d, %s)', |
| 85 PropertyType.STRING: '%s.GetString(%d, %s)', |
| 86 PropertyType.BOOLEAN: '%s.GetBoolean(%d, %s)', |
| 87 PropertyType.INTEGER: '%s.GetInteger(%d, %s)', |
| 88 PropertyType.DOUBLE: '%s.GetDouble(%d, %s)', |
| 89 }[prop.type_] % (src, index, dst) |
| OLD | NEW |