diff options
| author | 2010-03-17 10:27:44 -0500 | |
|---|---|---|
| committer | 2012-03-20 23:25:54 +0100 | |
| commit | 68c63cc1382326e7f0cb4e6bd863427f9069ca05 (patch) | |
| tree | d4094cb7f98cdb081e614c519f6cf8d0f9080c18 | |
| parent | cfe6244f8954efce4ef12b9b4338cc0d41a9ff40 (diff) | |
| download | libimobiledevice-68c63cc1382326e7f0cb4e6bd863427f9069ca05.tar.gz libimobiledevice-68c63cc1382326e7f0cb4e6bd863427f9069ca05.tar.bz2 | |
Added base class for more efficient error handling.
| -rw-r--r-- | cython/Makefile.am | 8 | ||||
| -rw-r--r-- | cython/imobiledevice.pxd | 8 | ||||
| -rw-r--r-- | cython/imobiledevice.pyx | 80 | ||||
| -rw-r--r-- | cython/mobilebackup.pxi | 28 | ||||
| -rw-r--r-- | cython/mobilesync.pxi | 28 | ||||
| -rw-r--r-- | cython/property_list_client.pxi | 13 | ||||
| -rw-r--r-- | cython/property_list_service.pxi | 15 |
7 files changed, 113 insertions, 67 deletions
diff --git a/cython/Makefile.am b/cython/Makefile.am index f5f01ef..1a71fbe 100644 --- a/cython/Makefile.am +++ b/cython/Makefile.am | |||
| @@ -7,7 +7,13 @@ if HAVE_CYTHON | |||
| 7 | 7 | ||
| 8 | BUILT_SOURCES = imobiledevice.c | 8 | BUILT_SOURCES = imobiledevice.c |
| 9 | PXDINCLUDES = imobiledevice.pxd stdint.pxi $(CYTHON_PLIST_INCLUDE_DIR)/plist.pxd | 9 | PXDINCLUDES = imobiledevice.pxd stdint.pxi $(CYTHON_PLIST_INCLUDE_DIR)/plist.pxd |
| 10 | PXIINCLUDES = stdint.pxi mobilesync.pxi notification_proxy.pxi sbservices.pxi mobilebackup.pxi | 10 | PXIINCLUDES = \ |
| 11 | stdint.pxi \ | ||
| 12 | mobilesync.pxi \ | ||
| 13 | notification_proxy.pxi \ | ||
| 14 | sbservices.pxi \ | ||
| 15 | mobilebackup.pxi \ | ||
| 16 | property_list_client.pxi | ||
| 11 | 17 | ||
| 12 | CLEANFILES = \ | 18 | CLEANFILES = \ |
| 13 | *.pyc \ | 19 | *.pyc \ |
diff --git a/cython/imobiledevice.pxd b/cython/imobiledevice.pxd index bdebe33..0557d37 100644 --- a/cython/imobiledevice.pxd +++ b/cython/imobiledevice.pxd | |||
| @@ -10,6 +10,10 @@ cdef class BaseError(Exception): | |||
| 10 | cdef dict _lookup_table | 10 | cdef dict _lookup_table |
| 11 | cdef int16_t _c_errcode | 11 | cdef int16_t _c_errcode |
| 12 | 12 | ||
| 13 | cdef class Base: | ||
| 14 | cdef inline int handle_error(self, int16_t ret) except -1 | ||
| 15 | cdef inline BaseError _error(self, int16_t ret) | ||
| 16 | |||
| 13 | cdef class iDeviceError(BaseError): pass | 17 | cdef class iDeviceError(BaseError): pass |
| 14 | 18 | ||
| 15 | cdef extern from "libimobiledevice/libimobiledevice.h": | 19 | cdef extern from "libimobiledevice/libimobiledevice.h": |
| @@ -29,7 +33,7 @@ cdef extern from "libimobiledevice/libimobiledevice.h": | |||
| 29 | cdef class iDeviceEvent: | 33 | cdef class iDeviceEvent: |
| 30 | cdef const_idevice_event_t _c_event | 34 | cdef const_idevice_event_t _c_event |
| 31 | 35 | ||
| 32 | cdef class iDevice: | 36 | cdef class iDevice(Base): |
| 33 | cdef idevice_t _c_dev | 37 | cdef idevice_t _c_dev |
| 34 | 38 | ||
| 35 | cdef class LockdownError(BaseError): pass | 39 | cdef class LockdownError(BaseError): pass |
| @@ -39,7 +43,7 @@ cdef extern from "libimobiledevice/lockdown.h": | |||
| 39 | pass | 43 | pass |
| 40 | ctypedef lockdownd_client_int *lockdownd_client_t | 44 | ctypedef lockdownd_client_int *lockdownd_client_t |
| 41 | 45 | ||
| 42 | cdef class LockdownClient: | 46 | cdef class LockdownClient(Base): |
| 43 | cdef lockdownd_client_t _c_client | 47 | cdef lockdownd_client_t _c_client |
| 44 | cpdef int start_service(self, service) | 48 | cpdef int start_service(self, service) |
| 45 | cpdef goodbye(self) | 49 | cpdef goodbye(self) |
diff --git a/cython/imobiledevice.pyx b/cython/imobiledevice.pyx index bbec7b8..c6a96dd 100644 --- a/cython/imobiledevice.pyx +++ b/cython/imobiledevice.pyx | |||
| @@ -19,6 +19,16 @@ cdef class BaseError(Exception): | |||
| 19 | def __repr__(self): | 19 | def __repr__(self): |
| 20 | return self.__str__() | 20 | return self.__str__() |
| 21 | 21 | ||
| 22 | cdef class Base: | ||
| 23 | cdef inline int handle_error(self, int16_t ret) except -1: | ||
| 24 | if ret == 0: | ||
| 25 | return 0 | ||
| 26 | cdef BaseError err = self._error(ret) | ||
| 27 | raise err | ||
| 28 | return -1 | ||
| 29 | |||
| 30 | cdef inline BaseError _error(self, int16_t ret): pass | ||
| 31 | |||
| 22 | cdef extern from "libimobiledevice/libimobiledevice.h": | 32 | cdef extern from "libimobiledevice/libimobiledevice.h": |
| 23 | int16_t IDEVICE_E_SUCCESS | 33 | int16_t IDEVICE_E_SUCCESS |
| 24 | int16_t IDEVICE_E_INVALID_ARG | 34 | int16_t IDEVICE_E_INVALID_ARG |
| @@ -70,44 +80,53 @@ cpdef event_unsubscribe(): | |||
| 70 | if err: raise err | 80 | if err: raise err |
| 71 | 81 | ||
| 72 | cpdef get_device_list(): | 82 | cpdef get_device_list(): |
| 73 | cdef char** devices | 83 | cdef: |
| 74 | cdef int count | 84 | char** devices |
| 75 | cdef list result | 85 | int count |
| 76 | cdef bytes device | 86 | list result |
| 77 | cdef iDeviceError err = iDeviceError(idevice_get_device_list(&devices, &count)) | 87 | bytes device |
| 88 | iDeviceError err = iDeviceError(idevice_get_device_list(&devices, &count)) | ||
| 89 | |||
| 90 | if err: raise err | ||
| 78 | 91 | ||
| 79 | result = [] | 92 | result = [] |
| 80 | for i from 0 <= i < count: | 93 | for i from 0 <= i < count: |
| 81 | device = devices[i] | 94 | device = devices[i] |
| 82 | result.append(device) | 95 | result.append(device) |
| 83 | 96 | ||
| 84 | idevice_device_list_free(devices) | 97 | err = iDeviceError(idevice_device_list_free(devices)) |
| 98 | if err: raise err | ||
| 85 | return result | 99 | return result |
| 86 | 100 | ||
| 87 | cdef class iDevice: | 101 | cdef class iDevice(Base): |
| 88 | def __cinit__(self, uuid=None, *args, **kwargs): | 102 | def __cinit__(self, uuid=None, *args, **kwargs): |
| 89 | cdef char* c_uuid = NULL | 103 | cdef: |
| 104 | char* c_uuid = NULL | ||
| 105 | idevice_error_t err | ||
| 90 | if uuid is not None: | 106 | if uuid is not None: |
| 91 | c_uuid = uuid | 107 | c_uuid = uuid |
| 92 | err = iDeviceError(idevice_new(&(self._c_dev), c_uuid)) | 108 | err = idevice_new(&self._c_dev, c_uuid) |
| 93 | if err: raise err | 109 | self.handle_error(err) |
| 94 | 110 | ||
| 95 | def __dealloc__(self): | 111 | def __dealloc__(self): |
| 96 | if self._c_dev is not NULL: | 112 | if self._c_dev is not NULL: |
| 97 | err = iDeviceError(idevice_free(self._c_dev)) | 113 | self.handle_error(idevice_free(self._c_dev)) |
| 98 | if err: raise err | 114 | |
| 115 | cdef inline BaseError _error(self, int16_t ret): | ||
| 116 | return iDeviceError(ret) | ||
| 99 | 117 | ||
| 100 | property uuid: | 118 | property uuid: |
| 101 | def __get__(self): | 119 | def __get__(self): |
| 102 | cdef char* uuid | 120 | cdef: |
| 103 | err = iDeviceError(idevice_get_uuid(self._c_dev, &uuid)) | 121 | char* uuid |
| 104 | if err: raise err | 122 | idevice_error_t err |
| 123 | err = idevice_get_uuid(self._c_dev, &uuid) | ||
| 124 | self.handle_error(err) | ||
| 105 | return uuid | 125 | return uuid |
| 106 | property handle: | 126 | property handle: |
| 107 | def __get__(self): | 127 | def __get__(self): |
| 108 | cdef uint32_t handle | 128 | cdef uint32_t handle |
| 109 | err = iDeviceError(idevice_get_handle(self._c_dev, &handle)) | 129 | self.handle_error(idevice_get_handle(self._c_dev, &handle)) |
| 110 | if err: raise err | ||
| 111 | return handle | 130 | return handle |
| 112 | 131 | ||
| 113 | cdef extern from "libimobiledevice/lockdown.h": | 132 | cdef extern from "libimobiledevice/lockdown.h": |
| @@ -166,27 +185,34 @@ cdef class LockdownError(BaseError): | |||
| 166 | } | 185 | } |
| 167 | BaseError.__init__(self, *args, **kwargs) | 186 | BaseError.__init__(self, *args, **kwargs) |
| 168 | 187 | ||
| 169 | cdef class LockdownClient: | 188 | cdef class LockdownClient(Base): |
| 170 | def __cinit__(self, iDevice device not None, char *label=NULL, *args, **kwargs): | 189 | def __cinit__(self, iDevice device not None, char *label=NULL, *args, **kwargs): |
| 171 | cdef iDevice dev = device | 190 | cdef: |
| 172 | err = LockdownError(lockdownd_client_new_with_handshake(dev._c_dev, &(self._c_client), label)) | 191 | iDevice dev = device |
| 173 | if err: raise err | 192 | lockdownd_error_t err = lockdownd_client_new_with_handshake(dev._c_dev, &(self._c_client), label) |
| 193 | self.handle_error(err) | ||
| 174 | 194 | ||
| 175 | def __dealloc__(self): | 195 | def __dealloc__(self): |
| 196 | cdef lockdownd_error_t err | ||
| 176 | if self._c_client is not NULL: | 197 | if self._c_client is not NULL: |
| 177 | err = LockdownError(lockdownd_client_free(self._c_client)) | 198 | err = lockdownd_client_free(self._c_client) |
| 178 | if err: raise err | 199 | self.handle_error(err) |
| 200 | |||
| 201 | cdef inline BaseError _error(self, int16_t ret): | ||
| 202 | return LockdownError(ret) | ||
| 179 | 203 | ||
| 180 | cpdef int start_service(self, service): | 204 | cpdef int start_service(self, service): |
| 181 | cdef uint16_t port | 205 | cdef: |
| 182 | err = LockdownError(lockdownd_start_service(self._c_client, service, &port)) | 206 | uint16_t port |
| 183 | if err: raise err | 207 | lockdownd_error_t err |
| 208 | err = lockdownd_start_service(self._c_client, service, &port) | ||
| 209 | self.handle_error(err) | ||
| 184 | return port | 210 | return port |
| 185 | 211 | ||
| 186 | cpdef goodbye(self): | 212 | cpdef goodbye(self): |
| 187 | pass | 213 | pass |
| 188 | 214 | ||
| 189 | include "property_list_service.pxi" | 215 | include "property_list_client.pxi" |
| 190 | include "mobilesync.pxi" | 216 | include "mobilesync.pxi" |
| 191 | include "notification_proxy.pxi" | 217 | include "notification_proxy.pxi" |
| 192 | include "sbservices.pxi" | 218 | include "sbservices.pxi" |
diff --git a/cython/mobilebackup.pxi b/cython/mobilebackup.pxi index 2bbaebe..f6b13a9 100644 --- a/cython/mobilebackup.pxi +++ b/cython/mobilebackup.pxi | |||
| @@ -28,27 +28,33 @@ cdef class MobileBackupError(BaseError): | |||
| 28 | } | 28 | } |
| 29 | BaseError.__init__(self, *args, **kwargs) | 29 | BaseError.__init__(self, *args, **kwargs) |
| 30 | 30 | ||
| 31 | cdef class MobileBackupClient(PropertyListService): | 31 | cdef class MobileBackupClient(PropertyListClient): |
| 32 | cdef mobilebackup_client_t _c_client | 32 | cdef mobilebackup_client_t _c_client |
| 33 | 33 | ||
| 34 | def __cinit__(self, iDevice device not None, LockdownClient lockdown=None, *args, **kwargs): | 34 | def __cinit__(self, iDevice device not None, LockdownClient lockdown=None, *args, **kwargs): |
| 35 | cdef iDevice dev = device | 35 | cdef: |
| 36 | cdef LockdownClient lckd | 36 | iDevice dev = device |
| 37 | LockdownClient lckd | ||
| 38 | mobilebackup_error_t err | ||
| 37 | if lockdown is None: | 39 | if lockdown is None: |
| 38 | lckd = LockdownClient(dev) | 40 | lckd = LockdownClient(dev) |
| 39 | else: | 41 | else: |
| 40 | lckd = lockdown | 42 | lckd = lockdown |
| 41 | port = lckd.start_service("com.apple.mobilebackup") | 43 | port = lckd.start_service("com.apple.mobilebackup") |
| 42 | err = MobileBackupError(mobilebackup_client_new(dev._c_dev, port, &self._c_client)) | 44 | err = mobilebackup_client_new(dev._c_dev, port, &self._c_client) |
| 43 | if err: raise err | 45 | self.handle_error(err) |
| 44 | 46 | ||
| 45 | def __dealloc__(self): | 47 | def __dealloc__(self): |
| 48 | cdef mobilebackup_error_t err | ||
| 46 | if self._c_client is not NULL: | 49 | if self._c_client is not NULL: |
| 47 | err = MobileBackupError(mobilebackup_client_free(self._c_client)) | 50 | err = mobilebackup_client_free(self._c_client) |
| 48 | if err: raise err | 51 | self.handle_error(err) |
| 49 | 52 | ||
| 50 | cdef _send(self, plist.plist_t node): | 53 | cdef inline int16_t _send(self, plist.plist_t node): |
| 51 | return MobileBackupError(mobilebackup_send(self._c_client, node)) | 54 | return mobilebackup_send(self._c_client, node) |
| 52 | 55 | ||
| 53 | cdef _receive(self, plist.plist_t* node): | 56 | cdef inline int16_t _receive(self, plist.plist_t* node): |
| 54 | return MobileBackupError(mobilebackup_receive(self._c_client, node)) | 57 | return mobilebackup_receive(self._c_client, node) |
| 58 | |||
| 59 | cdef inline BaseError _error(self, int16_t ret): | ||
| 60 | return MobileBackupError(ret) | ||
diff --git a/cython/mobilesync.pxi b/cython/mobilesync.pxi index b9cf951..abd56b4 100644 --- a/cython/mobilesync.pxi +++ b/cython/mobilesync.pxi | |||
| @@ -28,27 +28,33 @@ cdef class MobileSyncError(BaseError): | |||
| 28 | } | 28 | } |
| 29 | BaseError.__init__(self, *args, **kwargs) | 29 | BaseError.__init__(self, *args, **kwargs) |
| 30 | 30 | ||
| 31 | cdef class MobileSyncClient(PropertyListService): | 31 | cdef class MobileSyncClient(PropertyListClient): |
| 32 | cdef mobilesync_client_t _c_client | 32 | cdef mobilesync_client_t _c_client |
| 33 | 33 | ||
| 34 | def __cinit__(self, iDevice device not None, LockdownClient lockdown=None, *args, **kwargs): | 34 | def __cinit__(self, iDevice device not None, LockdownClient lockdown=None, *args, **kwargs): |
| 35 | cdef iDevice dev = device | 35 | cdef: |
| 36 | cdef LockdownClient lckd | 36 | iDevice dev = device |
| 37 | LockdownClient lckd | ||
| 38 | mobilesync_error_t err | ||
| 37 | if lockdown is None: | 39 | if lockdown is None: |
| 38 | lckd = LockdownClient(dev) | 40 | lckd = LockdownClient(dev) |
| 39 | else: | 41 | else: |
| 40 | lckd = lockdown | 42 | lckd = lockdown |
| 41 | port = lckd.start_service("com.apple.mobilesync") | 43 | port = lckd.start_service("com.apple.mobilesync") |
| 42 | err = MobileSyncError(mobilesync_client_new(dev._c_dev, port, &(self._c_client))) | 44 | err = mobilesync_client_new(dev._c_dev, port, &(self._c_client)) |
| 43 | if err: raise err | 45 | self.handle_error(err) |
| 44 | 46 | ||
| 45 | def __dealloc__(self): | 47 | def __dealloc__(self): |
| 48 | cdef mobilesync_error_t err | ||
| 46 | if self._c_client is not NULL: | 49 | if self._c_client is not NULL: |
| 47 | err = MobileSyncError(mobilesync_client_free(self._c_client)) | 50 | err = mobilesync_client_free(self._c_client) |
| 48 | if err: raise err | 51 | self.handle_error(err) |
| 49 | 52 | ||
| 50 | cdef _send(self, plist.plist_t node): | 53 | cdef inline int16_t _send(self, plist.plist_t node): |
| 51 | return MobileSyncError(mobilesync_send(self._c_client, node)) | 54 | return mobilesync_send(self._c_client, node) |
| 52 | 55 | ||
| 53 | cdef _receive(self, plist.plist_t* node): | 56 | cdef inline int16_t _receive(self, plist.plist_t* node): |
| 54 | return MobileSyncError(mobilesync_receive(self._c_client, node)) | 57 | return mobilesync_receive(self._c_client, node) |
| 58 | |||
| 59 | cdef inline BaseError _error(self, int16_t ret): | ||
| 60 | return MobileSyncError(ret) | ||
diff --git a/cython/property_list_client.pxi b/cython/property_list_client.pxi new file mode 100644 index 0000000..2689658 --- /dev/null +++ b/cython/property_list_client.pxi | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | cdef class PropertyListClient(Base): | ||
| 2 | cpdef send(self, plist.Node node): | ||
| 3 | cdef plist.Node n = node | ||
| 4 | self.handle_error(self._send(n._c_node)) | ||
| 5 | |||
| 6 | cpdef plist.Node receive(self): | ||
| 7 | cdef plist.plist_t c_node = NULL | ||
| 8 | self.handle_error(self._receive(&c_node)) | ||
| 9 | |||
| 10 | return plist.plist_t_to_node(c_node) | ||
| 11 | |||
| 12 | cdef inline int16_t _send(self, plist.plist_t node): pass | ||
| 13 | cdef inline int16_t _receive(self, plist.plist_t* c_node): pass | ||
diff --git a/cython/property_list_service.pxi b/cython/property_list_service.pxi deleted file mode 100644 index 9b3f694..0000000 --- a/cython/property_list_service.pxi +++ /dev/null | |||
| @@ -1,15 +0,0 @@ | |||
| 1 | cdef class PropertyListService: | ||
| 2 | cpdef send(self, plist.Node node): | ||
| 3 | cdef plist.Node n = node | ||
| 4 | cdef BaseError err = self._send(n._c_node) | ||
| 5 | if err: raise err | ||
| 6 | |||
| 7 | cpdef plist.Node receive(self): | ||
| 8 | cdef plist.plist_t c_node = NULL | ||
| 9 | cdef BaseError err = self._receive(&c_node) | ||
| 10 | if err: raise err | ||
| 11 | |||
| 12 | return plist.plist_t_to_node(c_node) | ||
| 13 | |||
| 14 | cdef _send(self, plist.plist_t node): pass | ||
| 15 | cdef _receive(self, plist.plist_t* c_node): pass | ||
