diff options
| author | 2009-10-18 21:55:25 +0200 | |
|---|---|---|
| committer | 2009-10-18 21:55:25 +0200 | |
| commit | f85fa802a4fb23c0a9fa09b6dd8e052fa05a40d0 (patch) | |
| tree | 36214b4e6cebe8e1ce6159a0a9700c3165fcc8cf | |
| parent | c17b420a7f65573a84d8acd8a6a92f9d4e0ae696 (diff) | |
| download | libplist-f85fa802a4fb23c0a9fa09b6dd8e052fa05a40d0.tar.gz libplist-f85fa802a4fb23c0a9fa09b6dd8e052fa05a40d0.tar.bz2 | |
Conform to python naming. Make Dictionary behave like python's dicts.
| -rw-r--r-- | swig/plist.i | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/swig/plist.i b/swig/plist.i index 7aa4f3d..1d343b0 100644 --- a/swig/plist.i +++ b/swig/plist.i | |||
| @@ -128,9 +128,28 @@ static swig_type_info *Node_dynamic(void **ptr) | |||
| 128 | DYNAMIC_CAST(SWIGTYPE_p_PList__Node, Node_dynamic); | 128 | DYNAMIC_CAST(SWIGTYPE_p_PList__Node, Node_dynamic); |
| 129 | DYNAMIC_CAST(SWIGTYPE_p_PList__Structure, Node_dynamic); | 129 | DYNAMIC_CAST(SWIGTYPE_p_PList__Structure, Node_dynamic); |
| 130 | 130 | ||
| 131 | %include "std_map.i" | ||
| 132 | // Instantiate templates used by example | ||
| 133 | namespace std { | ||
| 134 | %template(PairStringNodePtr) std::pair<string, PList::Node*>; | ||
| 135 | %template(MapStringNodePtr) map<string,PList::Node*>; | ||
| 136 | } | ||
| 131 | 137 | ||
| 132 | %rename(__assign__) *::operator=; | 138 | %rename(__assign__) *::operator=; |
| 133 | %rename(__getitem__) *::operator[]; | 139 | %rename(__getitem__) *::operator[]; |
| 140 | %rename(__delitem__) *::Remove; | ||
| 141 | %rename(__setitem__) PList::Dictionary::Insert; | ||
| 142 | %rename(__deepcopy__) *::Clone; | ||
| 143 | %rename(__len__) *::GetSize; | ||
| 144 | %rename(get_type) *::GetType; | ||
| 145 | %rename(set_value) *::SetValue; | ||
| 146 | %rename(get_value) *::GetValue; | ||
| 147 | %rename(to_xml) *::ToXml; | ||
| 148 | %rename(to_bin) *::ToBin; | ||
| 149 | %rename(from_xml) *::FromXml; | ||
| 150 | %rename(from_bin) *::FromBin; | ||
| 151 | %rename(append) *::Append; | ||
| 152 | %rename(insert) PList::Array::Insert; | ||
| 134 | 153 | ||
| 135 | %ignore GetPlist(); | 154 | %ignore GetPlist(); |
| 136 | %ignore Boolean(plist_t); | 155 | %ignore Boolean(plist_t); |
| @@ -141,6 +160,9 @@ DYNAMIC_CAST(SWIGTYPE_p_PList__Structure, Node_dynamic); | |||
| 141 | %ignore Date(plist_t); | 160 | %ignore Date(plist_t); |
| 142 | %ignore Array(plist_t); | 161 | %ignore Array(plist_t); |
| 143 | %ignore Dictionary(plist_t); | 162 | %ignore Dictionary(plist_t); |
| 163 | %ignore Begin(); | ||
| 164 | %ignore End(); | ||
| 165 | %ignore Find(); | ||
| 144 | 166 | ||
| 145 | %include <plist/Node.h> | 167 | %include <plist/Node.h> |
| 146 | %include <plist/Boolean.h> | 168 | %include <plist/Boolean.h> |
| @@ -154,6 +176,96 @@ DYNAMIC_CAST(SWIGTYPE_p_PList__Structure, Node_dynamic); | |||
| 154 | %include <plist/Dictionary.h> | 176 | %include <plist/Dictionary.h> |
| 155 | %include <plist/Utils.h> | 177 | %include <plist/Utils.h> |
| 156 | 178 | ||
| 179 | |||
| 180 | %extend PList::Dictionary { | ||
| 181 | |||
| 182 | %newobject key_iterator(PyObject **PYTHON_SELF); | ||
| 183 | swig::PySwigIterator* key_iterator(PyObject **PYTHON_SELF) { | ||
| 184 | return swig::make_output_key_iterator(self->Begin(), self->Begin(), self->End(), *PYTHON_SELF); | ||
| 185 | } | ||
| 186 | |||
| 187 | %newobject value_iterator(PyObject **PYTHON_SELF); | ||
| 188 | swig::PySwigIterator* value_iterator(PyObject **PYTHON_SELF) { | ||
| 189 | return swig::make_output_value_iterator(self->Begin(), self->Begin(), self->End(), *PYTHON_SELF); | ||
| 190 | } | ||
| 191 | |||
| 192 | iterator iteritems() | ||
| 193 | { | ||
| 194 | return self->Begin(); | ||
| 195 | } | ||
| 196 | |||
| 197 | bool has_key(const std::string& key) const { | ||
| 198 | PList::Dictionary* dict = const_cast<PList::Dictionary*>(self); | ||
| 199 | PList::Dictionary::iterator i = dict->Find(key); | ||
| 200 | return i != dict->End(); | ||
| 201 | } | ||
| 202 | |||
| 203 | PyObject* keys() { | ||
| 204 | uint32_t size = self->GetSize(); | ||
| 205 | int pysize = (size <= (uint32_t) INT_MAX) ? (int) size : -1; | ||
| 206 | if (pysize < 0) { | ||
| 207 | SWIG_PYTHON_THREAD_BEGIN_BLOCK; | ||
| 208 | PyErr_SetString(PyExc_OverflowError, | ||
| 209 | "map size not valid in python"); | ||
| 210 | SWIG_PYTHON_THREAD_END_BLOCK; | ||
| 211 | return NULL; | ||
| 212 | } | ||
| 213 | PyObject* keyList = PyList_New(pysize); | ||
| 214 | PList::Dictionary::iterator i = self->Begin(); | ||
| 215 | for (int j = 0; j < pysize; ++i, ++j) { | ||
| 216 | PyList_SET_ITEM(keyList, j, swig::from(i->first)); | ||
| 217 | } | ||
| 218 | return keyList; | ||
| 219 | } | ||
| 220 | |||
| 221 | PyObject* values() { | ||
| 222 | uint32_t size = self->GetSize(); | ||
| 223 | int pysize = (size <= (uint32_t) INT_MAX) ? (int) size : -1; | ||
| 224 | if (pysize < 0) { | ||
| 225 | SWIG_PYTHON_THREAD_BEGIN_BLOCK; | ||
| 226 | PyErr_SetString(PyExc_OverflowError, | ||
| 227 | "map size not valid in python"); | ||
| 228 | SWIG_PYTHON_THREAD_END_BLOCK; | ||
| 229 | return NULL; | ||
| 230 | } | ||
| 231 | PyObject* valList = PyList_New(pysize); | ||
| 232 | PList::Dictionary::iterator i = self->Begin(); | ||
| 233 | for (int j = 0; j < pysize; ++i, ++j) { | ||
| 234 | PList::Node *second = i->second; | ||
| 235 | PyObject *down = SWIG_NewPointerObj(SWIG_as_voidptr(second), SWIG_TypeDynamicCast(SWIGTYPE_p_PList__Node, SWIG_as_voidptrptr(&second)), 0 | 0 ); | ||
| 236 | PyList_SET_ITEM(valList, j, down); | ||
| 237 | } | ||
| 238 | return valList; | ||
| 239 | } | ||
| 240 | |||
| 241 | PyObject* items() { | ||
| 242 | uint32_t size = self->GetSize(); | ||
| 243 | int pysize = (size <= (uint32_t) INT_MAX) ? (int) size : -1; | ||
| 244 | if (pysize < 0) { | ||
| 245 | SWIG_PYTHON_THREAD_BEGIN_BLOCK; | ||
| 246 | PyErr_SetString(PyExc_OverflowError, | ||
| 247 | "map size not valid in python"); | ||
| 248 | SWIG_PYTHON_THREAD_END_BLOCK; | ||
| 249 | return NULL; | ||
| 250 | } | ||
| 251 | PyObject* itemList = PyList_New(pysize); | ||
| 252 | PList::Dictionary::iterator i = self->Begin(); | ||
| 253 | for (int j = 0; j < pysize; ++i, ++j) { | ||
| 254 | PyObject *item = PyTuple_New(2); | ||
| 255 | PList::Node *second = i->second; | ||
| 256 | PyObject *down = SWIG_NewPointerObj(SWIG_as_voidptr(second), SWIG_TypeDynamicCast(SWIGTYPE_p_PList__Node, SWIG_as_voidptrptr(&second)), 0 | 0 ); | ||
| 257 | PyTuple_SetItem(item, 0, swig::from(i->first)); | ||
| 258 | PyTuple_SetItem(item, 1, down); | ||
| 259 | PyList_SET_ITEM(itemList, j, item); | ||
| 260 | } | ||
| 261 | return itemList; | ||
| 262 | } | ||
| 263 | |||
| 264 | %pythoncode {def __iter__(self): return self.key_iterator()} | ||
| 265 | %pythoncode {def iterkeys(self): return self.key_iterator()} | ||
| 266 | %pythoncode {def itervalues(self): return self.value_iterator()} | ||
| 267 | } | ||
| 268 | |||
| 157 | //deprecated wrapper below | 269 | //deprecated wrapper below |
| 158 | 270 | ||
| 159 | %include "stdint.i" | 271 | %include "stdint.i" |
