Changeset 857

Show
Ignore:
Timestamp:
03/03/06 11:51:01 (3 years ago)
Author:
abauer
Message:

Updates to the python wrapper (thanks to andrew)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/wrapper/opensync.i

    r522 r857  
    11%module opensync 
     2%include "cdata.i" 
    23 
    34%{ 
     
    1314typedef struct {} OSyncChange; 
    1415typedef struct {} OSyncMember; 
     16typedef struct {} OSyncHashTable; 
    1517 
    1618%extend OSyncMember { 
     
    4446        } 
    4547         
    46         void set_data(char *data, int datasize, osync_bool has_data) { 
    47                 osync_change_set_data(self, data, datasize, has_data); 
    48         } 
    49          
    50         const char *data_get() { 
     48        void data_set(char *data, int datasize, osync_bool has_data) { 
     49                /* take a copy of the data, so python does not try to reclaim it 
     50                 * this memory should be freed by opensync after the change is written 
     51                 * FIXME: if the change is not handed over to opensync, this will leak 
     52                 */ 
     53                char *copy = memcpy(malloc(datasize), data, datasize); 
     54                osync_change_set_data(self, copy, datasize, has_data); 
     55        } 
     56         
     57        void *data_get() { 
    5158                return osync_change_get_data(self); 
    5259        } 
    53          
    54         %pythoncode %{ 
    55         def get_data(self): 
    56                 return self.data_get() 
    57         data = property(get_data) 
    58         %} 
    5960         
    6061        int datasize_get() { 
     
    6364         
    6465        %pythoncode %{ 
    65         def get_datasize(self): 
    66                 return self.datasize_get() 
    67         datasize = property(get_datasize) 
     66        def get_data(self): 
     67                data = cdata(self.data_get(), self.datasize_get()) 
     68                # FIXME: despite passing the size around, sometimes the data 
     69                # seems also to be null-terminated; remove this. 
     70                if data[-1] == '\0': 
     71                        data = data[:-1] 
     72                return data 
     73        def set_data(self, data, has_data=True): 
     74                return self.data_set(data, len(data), has_data) 
     75        data = property(get_data,set_data) 
    6876        %} 
    6977         
     
    141149                self.changetype_set(changetype) 
    142150        changetype = property(get_changetype, set_changetype) 
     151        %} 
     152         
     153        void hash_set(const char *hash) { 
     154                osync_change_set_hash(self, hash); 
     155        } 
     156         
     157        const char *hash_get() { 
     158                return osync_change_get_hash(self); 
     159        } 
     160         
     161        %pythoncode %{ 
     162        def get_hash(self): 
     163                return self.hash_get() 
     164        def set_hash(self, hash): 
     165                self.hash_set(hash) 
     166        hash = property(get_hash, set_hash) 
    143167        %} 
    144168}; 
     
    244268        } 
    245269}; 
     270 
     271%extend OSyncHashTable { 
     272        OSyncHashTable() { 
     273                return osync_hashtable_new(); 
     274        } 
     275 
     276        ~OSyncHashTable() { 
     277                osync_hashtable_free(self); 
     278        } 
     279 
     280        void forget() { 
     281                osync_hashtable_forget(self); 
     282        } 
     283 
     284        osync_bool load(OSyncMember *member) { 
     285                OSyncError *error; 
     286                /* FIXME: do something with the error! */ 
     287                return osync_hashtable_load(self, member, &error); 
     288        } 
     289 
     290        void close() { 
     291                osync_hashtable_close(self); 
     292        } 
     293 
     294        void update_hash(OSyncChange *change) { 
     295                osync_hashtable_update_hash(self, change); 
     296        } 
     297 
     298        void report(const char *uid) { 
     299                osync_hashtable_report(self, uid); 
     300        } 
     301 
     302        void report_deleted(OSyncContext *context, const char *objtype) { 
     303                osync_hashtable_report_deleted(self, context, objtype); 
     304        } 
     305 
     306        OSyncChangeType get_changetype(const char *uid, const char *hash) { 
     307                return osync_hashtable_get_changetype(self, uid, hash); 
     308        } 
     309 
     310        osync_bool detect_change(OSyncChange *change) { 
     311                return osync_hashtable_detect_change(self, change); 
     312        } 
     313 
     314        void set_slow_sync(const char *objtype) { 
     315                osync_hashtable_set_slow_sync(self, objtype); 
     316        } 
     317}; 
     318 
     319/* pull in constants from opensync_error.h without all the functions */ 
     320%constant int NO_ERROR = OSYNC_NO_ERROR; 
     321%constant int ERROR_GENERIC = OSYNC_ERROR_GENERIC; 
     322%constant int ERROR_IO_ERROR = OSYNC_ERROR_IO_ERROR; 
     323%constant int ERROR_NOT_SUPPORTED = OSYNC_ERROR_NOT_SUPPORTED; 
     324%constant int ERROR_TIMEOUT = OSYNC_ERROR_TIMEOUT; 
     325%constant int ERROR_DISCONNECTED = OSYNC_ERROR_DISCONNECTED; 
     326%constant int ERROR_FILE_NOT_FOUND = OSYNC_ERROR_FILE_NOT_FOUND; 
     327%constant int ERROR_EXISTS = OSYNC_ERROR_EXISTS; 
     328%constant int ERROR_CONVERT = OSYNC_ERROR_CONVERT; 
     329%constant int ERROR_MISCONFIGURATION = OSYNC_ERROR_MISCONFIGURATION; 
     330%constant int ERROR_INITIALIZATION = OSYNC_ERROR_INITIALIZATION; 
     331%constant int ERROR_PARAMETER = OSYNC_ERROR_PARAMETER; 
     332%constant int ERROR_EXPECTED = OSYNC_ERROR_EXPECTED; 
     333%constant int ERROR_NO_CONNECTION = OSYNC_ERROR_NO_CONNECTION; 
     334%constant int ERROR_TEMPORARY = OSYNC_ERROR_TEMPORARY; 
     335%constant int ERROR_LOCKED = OSYNC_ERROR_LOCKED; 
     336%constant int ERROR_PLUGIN_NOT_FOUND = OSYNC_ERROR_PLUGIN_NOT_FOUND;