diff options
| author | 2013-03-19 06:06:01 +0100 | |
|---|---|---|
| committer | 2013-03-19 06:06:01 +0100 | |
| commit | 927c81a7d6cc0a42d6a3b6487f661626af193f34 (patch) | |
| tree | 8be1637e6815180d18430b936cf5632a1e2f1935 /cython/plist.pyx | |
| parent | 1eb804e3a2a7e39771deb4b50f38e21d500f9423 (diff) | |
| download | libplist-927c81a7d6cc0a42d6a3b6487f661626af193f34.tar.gz libplist-927c81a7d6cc0a42d6a3b6487f661626af193f34.tar.bz2 | |
cython: fixed missing class definition for PLIST_KEY type
Diffstat (limited to 'cython/plist.pyx')
| -rw-r--r-- | cython/plist.pyx | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/cython/plist.pyx b/cython/plist.pyx index 9b7eec9..746db4f 100644 --- a/cython/plist.pyx +++ b/cython/plist.pyx | |||
| @@ -41,6 +41,10 @@ cdef extern from *: | |||
| 41 | void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec) | 41 | void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec) |
| 42 | void plist_set_date_val(plist_t node, int32_t sec, int32_t usec) | 42 | void plist_set_date_val(plist_t node, int32_t sec, int32_t usec) |
| 43 | 43 | ||
| 44 | plist_t plist_new_key(char *val) | ||
| 45 | void plist_get_key_val(plist_t node, char **val) | ||
| 46 | void plist_set_key_val(plist_t node, char *val) | ||
| 47 | |||
| 44 | plist_t plist_new_string(char *val) | 48 | plist_t plist_new_string(char *val) |
| 45 | void plist_get_string_val(plist_t node, char **val) | 49 | void plist_get_string_val(plist_t node, char **val) |
| 46 | void plist_set_string_val(plist_t node, char *val) | 50 | void plist_set_string_val(plist_t node, char *val) |
| @@ -272,6 +276,77 @@ cdef Real Real_factory(plist_t c_node, bint managed=True): | |||
| 272 | 276 | ||
| 273 | from cpython cimport PY_MAJOR_VERSION | 277 | from cpython cimport PY_MAJOR_VERSION |
| 274 | 278 | ||
| 279 | cdef class Key(Node): | ||
| 280 | def __cinit__(self, object value=None, *args, **kwargs): | ||
| 281 | cdef: | ||
| 282 | char* c_utf8_data = NULL | ||
| 283 | bytes utf8_data | ||
| 284 | if value is None: | ||
| 285 | raise ValueError("Requires a value") | ||
| 286 | else: | ||
| 287 | if isinstance(value, unicode): | ||
| 288 | utf8_data = value.encode('utf-8') | ||
| 289 | elif (PY_MAJOR_VERSION < 3) and isinstance(value, str): | ||
| 290 | value.encode('ascii') # trial decode | ||
| 291 | utf8_data = value.encode('ascii') | ||
| 292 | else: | ||
| 293 | raise ValueError("Requires unicode input, got %s" % type(value)) | ||
| 294 | c_utf8_data = utf8_data | ||
| 295 | self._c_node = plist_new_string("") | ||
| 296 | plist_set_type(self._c_node, PLIST_KEY) | ||
| 297 | #plist_set_key_val(self._c_node, c_utf8_data) | ||
| 298 | |||
| 299 | def __repr__(self): | ||
| 300 | s = self.get_value() | ||
| 301 | return '<Key: %s>' % s.encode('utf-8') | ||
| 302 | |||
| 303 | def __richcmp__(self, other, op): | ||
| 304 | cdef unicode s = self.get_value() | ||
| 305 | if op == 0: | ||
| 306 | return s < other | ||
| 307 | if op == 1: | ||
| 308 | return s <= other | ||
| 309 | if op == 2: | ||
| 310 | return s == other | ||
| 311 | if op == 3: | ||
| 312 | return s != other | ||
| 313 | if op == 4: | ||
| 314 | return s > other | ||
| 315 | if op == 5: | ||
| 316 | return s >= other | ||
| 317 | |||
| 318 | cpdef set_value(self, object value): | ||
| 319 | cdef: | ||
| 320 | char* c_utf8_data = NULL | ||
| 321 | bytes utf8_data | ||
| 322 | if value is None: | ||
| 323 | plist_set_key_val(self._c_node, c_utf8_data) | ||
| 324 | else: | ||
| 325 | if isinstance(value, unicode): | ||
| 326 | utf8_data = value.encode('utf-8') | ||
| 327 | elif (PY_MAJOR_VERSION < 3) and isinstance(value, str): | ||
| 328 | value.encode('ascii') # trial decode | ||
| 329 | utf8_data = value.encode('ascii') | ||
| 330 | else: | ||
| 331 | raise ValueError("Requires unicode input, got %s" % type(value)) | ||
| 332 | c_utf8_data = utf8_data | ||
| 333 | plist_set_key_val(self._c_node, c_utf8_data) | ||
| 334 | |||
| 335 | cpdef unicode get_value(self): | ||
| 336 | cdef: | ||
| 337 | char* c_value = NULL | ||
| 338 | plist_get_key_val(self._c_node, &c_value) | ||
| 339 | try: | ||
| 340 | return cpython.PyUnicode_DecodeUTF8(c_value, len(c_value), 'strict') | ||
| 341 | finally: | ||
| 342 | libc.stdlib.free(c_value) | ||
| 343 | |||
| 344 | cdef Key Key_factory(plist_t c_node, bint managed=True): | ||
| 345 | cdef Key instance = Key.__new__(Key) | ||
| 346 | instance._c_managed = managed | ||
| 347 | instance._c_node = c_node | ||
| 348 | return instance | ||
| 349 | |||
| 275 | cdef class String(Node): | 350 | cdef class String(Node): |
| 276 | def __cinit__(self, object value=None, *args, **kwargs): | 351 | def __cinit__(self, object value=None, *args, **kwargs): |
| 277 | cdef: | 352 | cdef: |
