|
| 1 | +# Copyright (c) 2012-2013 The CEF Python authors. All rights reserved. |
| 2 | +# License: New BSD License. |
| 3 | +# Website: http://code.google.com/p/cefpython/ |
| 4 | + |
| 5 | +cdef list CefListValueToPyList( |
| 6 | + CefRefPtr[CefListValue] cefListValue, |
| 7 | + int nestingLevel=0): |
| 8 | + assert cefListValue.IsValid(), "cefListValue is invalid" |
| 9 | + if nestingLevel > 8: |
| 10 | + raise Exception("CefListValueToPyList(): max nesting level (8)" |
| 11 | + " exceeded") |
| 12 | + cdef int index |
| 13 | + cdef int size = cefListValue.GetSize() |
| 14 | + cdef cef_types.cef_value_type_t valueType |
| 15 | + cdef list ret |
| 16 | + for index in range(0, size): |
| 17 | + valueType = cefListValue.GetType(index) |
| 18 | + if valueType == cef_types.VTYPE_NULL: |
| 19 | + ret.append(None) |
| 20 | + elif valueType == cef_types.VTYPE_BOOL: |
| 21 | + ret.append(bool(cefListValue.GetBool(index))) |
| 22 | + elif valueType == cef_types.VTYPE_INT: |
| 23 | + ret.append(cefListValue.GetInt(index)) |
| 24 | + elif valueType == cef_types.VTYPE_DOUBLE: |
| 25 | + ret.append(cefListValue.GetDouble(index)) |
| 26 | + elif valueType == cef_types.VTYPE_STRING: |
| 27 | + ret.append(CefToPyString(cefListValue.GetString(index))) |
| 28 | + elif valueType == cef_types.VTYPE_BINARY: |
| 29 | + raise Exception("VTYPE_BINARY not supported") |
| 30 | + elif valueType == cef_types.VTYPE_DICTIONARY: |
| 31 | + ret.append(CefDictionaryValueToPyDict( |
| 32 | + cefListValue.GetDictionary(index), |
| 33 | + nestingLevel + 1)) |
| 34 | + elif valueType == cef_types.VTYPE_LIST: |
| 35 | + ret.append(CefListValueToPyList( |
| 36 | + cefListValue.GetList(index), |
| 37 | + nestingLevel + 1)) |
| 38 | + else: |
| 39 | + raise Exception("Unknown value type = %s" % valueType) |
| 40 | + return ret |
| 41 | + |
| 42 | +cdef dict CefDictionaryValueToPyDict( |
| 43 | + CefRefPtr[CefDictionaryValue] cefDictionaryValue, |
| 44 | + int nestingLevel=0): |
| 45 | + assert cefDictionaryValue.IsValid(), "cefDictionaryValue is invalid" |
| 46 | + if nestingLevel > 8: |
| 47 | + raise Exception("CefDictionaryValueToPyDict(): max nesting level (8)" |
| 48 | + " exceeded") |
| 49 | + cdef cpp_vector[CefString] keyList |
| 50 | + cefDictionaryValue.GetKeys(keyList) |
| 51 | + cdef cef_types.cef_value_type_t valueType |
| 52 | + cdef dict ret |
| 53 | + cdef cpp_vector[CefString].iterator iterator = keyList.begin() |
| 54 | + cdef CefString cefKey |
| 55 | + cdef py_string pyKey |
| 56 | + while iterator != keyList.end(): |
| 57 | + cefKey = deref(iterator) |
| 58 | + pyKey = CefToPyString(cefKey) |
| 59 | + preinc(iterator) |
| 60 | + valueType = cefDictionaryValue.GetType(cefKey) |
| 61 | + if valueType == cef_types.VTYPE_NULL: |
| 62 | + ret[pyKey] = None |
| 63 | + elif valueType == cef_types.VTYPE_BOOL: |
| 64 | + ret[pyKey] = bool(cefDictionaryValue.GetBool(cefKey)) |
| 65 | + elif valueType == cef_types.VTYPE_INT: |
| 66 | + ret[pyKey] = cefDictionaryValue.GetInt(cefKey) |
| 67 | + elif valueType == cef_types.VTYPE_DOUBLE: |
| 68 | + ret[pyKey] = cefDictionaryValue.GetDouble(cefKey) |
| 69 | + elif valueType == cef_types.VTYPE_STRING: |
| 70 | + ret[pyKey] = CefToPyString(cefDictionaryValue.GetString(cefKey)) |
| 71 | + elif valueType == cef_types.VTYPE_BINARY: |
| 72 | + raise Exception("VTYPE_BINARY not supported") |
| 73 | + elif valueType == cef_types.VTYPE_DICTIONARY: |
| 74 | + ret[pyKey] = CefDictionaryValueToPyDict( |
| 75 | + cefDictionaryValue.GetDictionary(cefKey), |
| 76 | + nestingLevel + 1) |
| 77 | + elif valueType == cef_types.VTYPE_LIST: |
| 78 | + ret[pyKey] = CefListValueToPyList( |
| 79 | + cefDictionaryValue.GetList(cefKey), |
| 80 | + nestingLevel + 1) |
| 81 | + else: |
| 82 | + raise Exception("Unknown value type = %s" % valueType) |
| 83 | + return ret |
| 84 | + |
| 85 | +cdef py_void PyListToCefListValue( |
| 86 | + list pyList, |
| 87 | + CefRefPtr[CefListValue] cefListValue, |
| 88 | + int nestingLevel=0): |
| 89 | + if nestingLevel > 8: |
| 90 | + raise Exception("PyListToCefListValue(): max nesting level (8)" |
| 91 | + " exceeded") |
| 92 | + cdef type valueType |
| 93 | + cdef CefRefPtr[CefListValue] newCefListValue |
| 94 | + for index, value in enumerate(pyList): |
| 95 | + valueType = type(value) |
| 96 | + if valueType == type(None): |
| 97 | + cefListValue.SetNull(index) |
| 98 | + elif valueType == bool: |
| 99 | + cefListValue.SetBool(index, bool(value)) |
| 100 | + elif valueType == int: |
| 101 | + cefListValue.SetInt(index, int(value)) |
| 102 | + elif valueType == long: |
| 103 | + # Int32 range is -2147483648..2147483647, we've increased the |
| 104 | + # minimum size by one as Cython was throwing a warning: |
| 105 | + # "unary minus operator applied to unsigned type, result still |
| 106 | + # unsigned". |
| 107 | + if value <= 2147483647 and value >= -2147483647: |
| 108 | + cefListValue.SetInt(index, int(value)) |
| 109 | + else: |
| 110 | + # Long values become strings. |
| 111 | + cefListValue.SetString(index, PyToCefStringValue(str(value))) |
| 112 | + elif valueType == float: |
| 113 | + cefListValue.SetDouble(index, float(value)) |
| 114 | + elif valueType == bytes or valueType == unicode: |
| 115 | + cefListValue.SetString(index, PyToCefStringValue(str(value))) |
| 116 | + elif valueType == dict: |
| 117 | + cefListValue.SetDictionary(index, PyDictToCefDictionaryValue( |
| 118 | + value, nestingLevel + 1)) |
| 119 | + elif valueType == list: |
| 120 | + newCefListValue = CefListValue_Create() |
| 121 | + PyListToCefListValue(value, newCefListValue, nestingLevel + 1) |
| 122 | + cefListValue.SetList(index, newCefListValue) |
| 123 | + elif valueType == type: |
| 124 | + cefListValue.SetString(index, PyToCefStringValue(str(value))) |
| 125 | + else: |
| 126 | + # Raising an exception probably not a good idea, why |
| 127 | + # terminate application when we can cast it to string, |
| 128 | + # the data may contain some non-standard object that is |
| 129 | + # probably redundant, but casting to string will do no harm. |
| 130 | + cefListValue.SetString(index, PyToCefStringValue(str(value))) |
| 131 | + return None |
| 132 | + |
| 133 | +cdef CefRefPtr[CefDictionaryValue] PyDictToCefDictionaryValue( |
| 134 | + dict pyDict, |
| 135 | + int nestingLevel=0): |
| 136 | + if nestingLevel > 8: |
| 137 | + raise Exception("PyDictToCefDictionaryValue(): max nesting level (8)" |
| 138 | + " exceeded") |
| 139 | + cdef type valueType |
| 140 | + cdef CefRefPtr[CefListValue] newCefListValue |
| 141 | + cdef CefRefPtr[CefDictionaryValue] ret = CefDictionaryValue_Create() |
| 142 | + cdef CefString cefKey |
| 143 | + for pyKey in pyDict: |
| 144 | + valueType = type(value) |
| 145 | + value = pyDict[pyKey] |
| 146 | + PyToCefString(pyKey, cefKey) |
| 147 | + if valueType == type(None): |
| 148 | + ret.SetNull(cefKey) |
| 149 | + elif valueType == bool: |
| 150 | + ret.SetBool(cefKey, bool(value)) |
| 151 | + elif valueType == int: |
| 152 | + ret.SetInt(cefKey, int(value)) |
| 153 | + elif valueType == long: |
| 154 | + # Int32 range is -2147483648..2147483647, we've increased the |
| 155 | + # minimum size by one as Cython was throwing a warning: |
| 156 | + # "unary minus operator applied to unsigned type, result still |
| 157 | + # unsigned". |
| 158 | + if value <= 2147483647 and value >= -2147483647: |
| 159 | + ret.SetInt(cefKey, int(value)) |
| 160 | + else: |
| 161 | + # Long values become strings. |
| 162 | + ret.SetString(cefKey, PyToCefStringValue(str(value))) |
| 163 | + elif valueType == float: |
| 164 | + ret.SetDouble(cefKey, float(value)) |
| 165 | + elif valueType == bytes or valueType == unicode: |
| 166 | + ret.SetString(cefKey, PyToCefStringValue(str(value))) |
| 167 | + elif valueType == dict: |
| 168 | + ret.SetDictionary(cefKey, PyDictToCefDictionaryValue( |
| 169 | + value, nestingLevel + 1)) |
| 170 | + elif valueType == list: |
| 171 | + newCefListValue = CefListValue_Create() |
| 172 | + PyListToCefListValue(value, newCefListValue, nestingLevel + 1) |
| 173 | + ret.SetList(cefKey, newCefListValue) |
| 174 | + elif valueType == type: |
| 175 | + ret.SetString(cefKey, PyToCefStringValue(str(value))) |
| 176 | + else: |
| 177 | + # Raising an exception probably not a good idea, why |
| 178 | + # terminate application when we can cast it to string, |
| 179 | + # the data may contain some non-standard object that is |
| 180 | + # probably redundant, but casting to string will do no harm. |
| 181 | + ret.SetString(cefKey, PyToCefStringValue(str(value))) |
| 182 | + return ret |
0 commit comments