summaryrefslogtreecommitdiffstats
path: root/cython
diff options
context:
space:
mode:
Diffstat (limited to 'cython')
-rw-r--r--cython/plist.pxd2
-rw-r--r--cython/plist.pyx75
2 files changed, 76 insertions, 1 deletions
diff --git a/cython/plist.pxd b/cython/plist.pxd
index 05f3e16..d7c70bd 100644
--- a/cython/plist.pxd
+++ b/cython/plist.pxd
@@ -21,7 +21,7 @@ cdef class Integer(Node):
21 21
22cdef class Key(Node): 22cdef class Key(Node):
23 cpdef set_value(self, object value) 23 cpdef set_value(self, object value)
24 cpdef int get_value(self) 24 cpdef unicode get_value(self)
25 25
26cdef class Real(Node): 26cdef class Real(Node):
27 cpdef set_value(self, object value) 27 cpdef set_value(self, object value)
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
273from cpython cimport PY_MAJOR_VERSION 277from cpython cimport PY_MAJOR_VERSION
274 278
279cdef 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
344cdef 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
275cdef class String(Node): 350cdef 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: