1 """
2 Interfaces for inter-operation with opensync
3
4 Copyright: John Stowers, 2006
5 License: GPLv2
6 """
7 import md5
8 import logging
9 log = logging.getLogger("dataproviders.Opensync")
10
11 import conduit
12 import conduit.dataproviders.DataProvider as DataProvider
13 import conduit.dataproviders.DataProviderCategory as DataProviderCategory
14 from conduit.datatypes import Rid
15 import conduit.datatypes.Contact as Contact
16 import conduit.datatypes.Event as Event
17
18 try:
19 import opensync
20 except ImportError:
21 raise conduit.Exceptions.NotSupportedError
22
23
24 formats = opensync.FormatEnv()
25 formats.load_plugins()
26
27
28 plugins = opensync.PluginEnv()
29 plugins.load()
30
32 """
33 Generic dataprovider for interfacing with OpenSync plugins
34
35 Maps all Conduit sync calls onto the OpenSync code. HAL and other
36 integration work is left to the DPs that extend this base class.
37 """
38
39 _module_type_ = "twoway"
40
42 DataProvider.TwoWay.__init__(self)
43
44 self.info = None
45 self.data = None
46 self.sink = None
47 self.ctx = None
48
49 for plugin in plugins.plugins:
50 print plugin.name
51 if plugin.name == self._os_name_:
52 self.info = opensync.PluginInfo()
53 self.info.set_config(self._get_config())
54 self.info.set_format_env(formats)
55
56 self.data = plugin.initialize(self.info)
57 plugin.discover(self.data, self.info)
58
59 for sink in self.info.objtypes:
60 print sink.name
61 if sink.name == self._os_sink_:
62 self.sink = sink
63 self.info.sink = sink
64
65 assert self.info and self.sink
66
67 self.ctx = opensync.Context()
68 self.ctx.set_callback_object(Callbacks(self))
69 self.uids = None
70
75
77 self.uids = {}
78 self.sink.get_changes(self.data, self.info, self.ctx)
79 return list(self.uids.keys())
80
81 - def get(self, LUID):
84
85 - def put(self, obj, overwrite, LUID=None):
86 DataProvider.TwoWay.put(self, obj, overwrite, LUID)
87 chg = self._object_to_change(obj)
88
89 if overwrite == True and LUID != None:
90 chg.uid = LUID
91 chg.changetype = opensync.CHANGE_TYPE_MODIFIED
92 else:
93 chg.changetype = opensync.CHANGE_TYPE_ADDED
94
95 self.sink.commit_change(self.data, self.info, chg, self.ctx)
96 return Rid(uid=chg.uid, hash=self._get_hash(chg))
97
99 print type(LUID)
100 DataProvider.TwoWay.delete(self, LUID)
101 chg = opensync.Change()
102 chg.uid = LUID
103 chg.changetype = opensync.CHANGE_TYPE_DELETED
104 self.sink.commit_change(self.data, self.info, chg, self.ctx)
105
106 - def finish(self, aborted, error, conflict):
110
113
114 - def _convert(self, data, chgfrom, chgto):
115 """
116 Invoke an opensync conversion plugin
117 """
118 if chgfrom == chgto:
119 return data
120
121 formatfrom = formats.find_objformat(chgfrom)
122 formatto = formats.find_objformat(chgto)
123 converter = formats.find_converter(formatfrom, formatto)
124 return converter.invoke(data, "")
125
127 """
128 If it's valid, return an osync change hash.
129 Otherwise, generate a new one.
130 """
131 myhash = change.hash
132 if myhash == None or len(myhash) == 0:
133 return md5.md5(change.data.data).hexdigest()
134
136 """
137 Map from an opensync data object to a Conduit data object
138 """
139 raise NotImplementedError
140
142 """
143 Map from a Conduit data object to an opensync data object
144 """
145 raise NotImplementedError
146
148 """
149 Generate the config that opensync needs to use this endpoint
150 """
151 raise NotImplementedError
152
153
155 """
156 The OpenSync bindings call back into this function as changes are received
157 from the other dataprovider. As they are received, we updated the uids dict.
158
159 Would it be dirty to merge this with the dataprovider base? I think so.
160 """
161
164
167
170
173
174
204
206 """
207 Implement mappings between Conduit vevent and OS vevent
208 """
209
210 _in_type_ = "event"
211 _out_type_ = "event"
212 _icon_ = "contact-new"
213 _osync_type_ = "vevent20"
214
225
227 chg = opensync.Change()
228 chg.format = "vevent20"
229 chg.objtype = "data"
230 format = formats.find_objformat("vevent20")
231 vcard = str(obj.get_ical_string())
232 chg.data = opensync.Data(vcard, format)
233 return chg
234