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 os | 9 import os |
10 | 10 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 PropertyType.STRING, PropertyType.ANY): | 68 PropertyType.STRING, PropertyType.ANY): |
69 arg = '%(type)s& %(name)s' | 69 arg = '%(type)s& %(name)s' |
70 else: | 70 else: |
71 arg = '%(type)s %(name)s' | 71 arg = '%(type)s %(name)s' |
72 return arg % { | 72 return arg % { |
73 'type': type_, | 73 'type': type_, |
74 'name': param.unix_name, | 74 'name': param.unix_name, |
75 } | 75 } |
76 | 76 |
77 def GenerateIfndefName(path, filename): | 77 def GenerateIfndefName(path, filename): |
78 """Formats a path and filename as a #define name. | 78 """Formats a path and filename as a #define name. |
79 | 79 |
80 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__. | 80 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__. |
81 """ | 81 """ |
82 return (('%s_%s_H__' % (path, filename)) | 82 return (('%s_%s_H__' % (path, filename)) |
83 .upper().replace(os.sep, '_').replace('/', '_')) | 83 .upper().replace(os.sep, '_').replace('/', '_')) |
| 84 |
| 85 def GenerateTypeToCompiledTypeConversion(prop, from_, to): |
| 86 try: |
| 87 return _GenerateTypeConversionHelper(prop.type_, prop.compiled_type, from_, |
| 88 to) |
| 89 except KeyError: |
| 90 raise NotImplementedError('Conversion from %s to %s in %s not supported' % |
| 91 (prop.type_, prop.compiled_type, prop.name)) |
| 92 |
| 93 def GenerateCompiledTypeToTypeConversion(prop, from_, to): |
| 94 try: |
| 95 return _GenerateTypeConversionHelper(prop.compiled_type, prop.type_, from_, |
| 96 to) |
| 97 except KeyError: |
| 98 raise NotImplementedError('Conversion from %s to %s in %s not supported' % |
| 99 (prop.compiled_type, prop.type_, prop.name)) |
| 100 |
| 101 def _GenerateTypeConversionHelper(from_type, to_type, from_, to): |
| 102 """Converts from PropertyType from_type to PropertyType to_type. |
| 103 |
| 104 from_type: The PropertyType to be converted from. |
| 105 to_type: The PropertyType to be converted to. |
| 106 from_: The variable name of the type to be converted from. |
| 107 to: The variable name of the type to be converted to. |
| 108 """ |
| 109 # TODO(mwrosen): Add support for more from/to combinations as necessary. |
| 110 return { |
| 111 PropertyType.STRING: { |
| 112 PropertyType.INTEGER: 'base::StringToInt(%(from)s, &%(to)s)', |
| 113 PropertyType.INT64: 'base::StringToInt64(%(from)s, &%(to)s)', |
| 114 }, |
| 115 PropertyType.INTEGER: { |
| 116 PropertyType.STRING: '%(to)s = base::IntToString(%(from)s)', |
| 117 }, |
| 118 PropertyType.INT64: { |
| 119 PropertyType.STRING: '%(to)s = base::Int64ToString(%(from)s)', |
| 120 } |
| 121 }[from_type][to_type] % {'from': from_, 'to': to} |
OLD | NEW |