Package conduit :: Package modules :: Module ConverterModule
[hide private]

Source Code for Module conduit.modules.ConverterModule

  1  import re 
  2  import logging 
  3  log = logging.getLogger("modules.Converter") 
  4   
  5  import conduit.utils as Utils 
  6  import conduit.TypeConverter as TypeConverter 
  7  import conduit.datatypes.Contact as Contact 
  8  import conduit.datatypes.Event as Event 
  9  import conduit.datatypes.Text as Text 
 10  import conduit.datatypes.Email as Email 
 11  import conduit.datatypes.File as File 
 12  import conduit.datatypes.Note as Note 
 13  import conduit.datatypes.Setting as Setting 
 14  import conduit.datatypes.Bookmark as Bookmark 
 15   
 16  MODULES = { 
 17          "EmailConverter" :      { "type": "converter" }, 
 18          "NoteConverter" :       { "type": "converter" }, 
 19          "ContactConverter" :    { "type": "converter" }, 
 20          "EventConverter" :      { "type": "converter" }, 
 21          "FileConverter" :       { "type": "converter" }, 
 22          "SettingConverter" :    { "type": "converter" }, 
 23          "BookmarkConverter" :   { "type": "converter" }, 
 24  } 
 25   
26 -class EmailConverter(TypeConverter.Converter):
27 - def __init__(self):
28 self.conversions = { 29 "email,text" : self.email_to_text, 30 "text,email" : self.text_to_email, 31 "email,file" : self.email_to_file, 32 "file,email" : self.file_to_email, 33 }
34 35
36 - def email_to_text(self, email, **kwargs):
37 t = Text.Text( 38 text=email.get_email_string() 39 ) 40 return t
41
42 - def text_to_email(self, text, **kwargs):
43 email = Email.Email( 44 content=text.get_string() 45 ) 46 return email
47
48 - def email_to_file(self, email, **kwargs):
49 f = File.TempFile(email.get_email_string()) 50 return f
51
52 - def file_to_email(self, thefile, **kwargs):
53 """ 54 If the file is non binary then include it as the 55 Subject of the message. Otherwise include it as an attachment 56 """ 57 mimeCategory = thefile.get_mimetype().split('/')[0] 58 if mimeCategory == "text": 59 #insert the contents into the email 60 log.debug("Inserting file contents into email") 61 email = Email.Email( 62 subject=thefile.get_filename(), 63 content=thefile.get_contents_as_text() 64 ) 65 else: 66 #binary file so send as attachment 67 log.debug("Binary file, attaching to email") 68 email = Email.Email( 69 subject=thefile.get_filename(), 70 content="Attached" 71 ) 72 email.add_attachment(thefile.get_local_uri()) 73 74 return email
75 76
77 -class NoteConverter(TypeConverter.Converter):
78 - def __init__(self):
79 self.conversions = { 80 "text,note" : self.text_to_note, 81 "note,text" : self.note_to_text, 82 "note,file" : self.note_to_file 83 }
84
85 - def text_to_note(self, text, **kwargs):
86 n = Note.Note( 87 title="Note-"+Utils.random_string(), 88 contents=text 89 ) 90 return n
91
92 - def note_to_text(self, note, **kwargs):
93 t = Text.Text( 94 text=note.get_note_string() 95 ) 96 return t
97
98 - def note_to_file(self, note, **kwargs):
99 f = File.TempFile(note.get_contents()) 100 f.force_new_filename(note.get_title()) 101 f.force_new_file_extension(".txt") 102 return f
103
104 -class ContactConverter(TypeConverter.Converter):
105 - def __init__(self):
106 self.conversions = { 107 "contact,file" : self.contact_to_file, 108 "contact,text" : self.contact_to_text, 109 "file,contact" : self.file_to_contact, 110 }
111
112 - def contact_to_file(self, contact, **kwargs):
113 #get vcard data 114 f = Utils.new_tempfile(contact.get_vcard_string()) 115 return f
116
117 - def contact_to_text(self, contact, **kwargs):
118 #get vcard data 119 t = Text.Text( 120 text=contact.get_vcard_string() 121 ) 122 return t
123
124 - def file_to_contact(self, f, **kwargs):
125 c = Contact.Contact() 126 c. set_from_vcard_string(f.get_contents_as_text()) 127 return c
128
129 -class EventConverter(TypeConverter.Converter):
130 - def __init__(self):
131 self.conversions = { 132 "event,file" : self.event_to_file, 133 "event,text" : self.event_to_text, 134 "file,event" : self.file_to_event, 135 "text,event" : self.text_to_event, 136 }
137
138 - def event_to_file(self, event, **kwargs):
139 #get ical data 140 f = Utils.new_tempfile(event.get_ical_string()) 141 return f
142
143 - def event_to_text(self, event, **kwargs):
144 t = Text.Text( 145 text=event.get_ical_string() 146 ) 147 return t
148
149 - def file_to_event(self, f, **kwargs):
150 e = Event.Event() 151 e.set_from_ical_string(f.get_contents_as_text()) 152 return e
153
154 - def text_to_event(self, text, **kwargs):
155 e = Event.Event() 156 e.set_from_ical_string(text.get_string()) 157 return e
158
159 -class FileConverter(TypeConverter.Converter):
160 - def __init__(self):
161 self.conversions = { 162 "text,file" : self.text_to_file, 163 "file,text" : self.file_to_text, 164 "file,note" : self.file_to_note 165 }
166
167 - def text_to_file(self, text, **kwargs):
168 return Utils.new_tempfile(text.get_string())
169
170 - def file_to_text(self, f, **kwargs):
171 test = None 172 if f.get_mimetype().startswith("text"): 173 text = Text.Text( 174 text=f.get_contents_as_text() 175 ) 176 return text
177
178 - def file_to_note(self, f, **kwargs):
179 note = None 180 if f.get_mimetype().startswith("text"): 181 title,ext = f.get_filename_and_extension() 182 #remove the file extension.... 183 note = Note.Note( 184 title=title, 185 contents=f.get_contents_as_text() 186 ) 187 return note
188
189 -class SettingConverter(TypeConverter.Converter):
190 - def __init__(self):
191 self.conversions = { 192 "setting,text" : self.setting_to_text, 193 "setting,file" : self.setting_to_file, 194 "text,setting" : self.text_to_setting, 195 "file,setting" : self.file_to_setting 196 } 197 #recognizes key value in text strings 198 self.regex = re.compile(r"^key:(.+)\nvalue:(.*)$")
199
200 - def _to_text(self, setting):
201 return "key:%s\nvalue:%s" % (setting.key, setting.value)
202
203 - def _to_key_value(self, txt):
204 m = self.regex.match(txt) 205 if m != None and len(m.groups()) == 2: 206 return m.group(1),m.group(2) 207 else: 208 return None,None
209
210 - def setting_to_text(self, setting):
211 t = Text.Text( 212 text=self._to_text(setting) 213 ) 214 return t
215
216 - def text_to_setting(self, text):
217 setting = None 218 k,v = self._to_key_value(text.get_string()) 219 if k != None and v != None: 220 setting = Setting.Setting( 221 key=k, 222 value=v 223 ) 224 return setting
225
226 - def setting_to_file(self, setting):
227 f = File.TempFile( 228 self._to_text(setting) 229 ) 230 f.force_new_filename(setting.key.replace("/","_")) 231 f.force_new_file_extension(".txt") 232 return f
233
234 - def file_to_setting(self, f):
235 setting = None 236 if f.get_mimetype().startswith("text"): 237 txt = f.get_contents_as_text() 238 k,v = self._to_key_value(txt) 239 if k != None and v != None: 240 setting = Setting.Setting( 241 key=k, 242 value=v 243 ) 244 return setting
245
246 -class BookmarkConverter(TypeConverter.Converter):
247 - def __init__(self):
248 self.conversions = { 249 "bookmark,text" : self.bookmark_to_text, 250 "bookmark,file" : self.bookmark_to_file, 251 "text,bookmark" : self.text_to_bookmark, 252 "file,bookmark" : self.file_to_bookmark 253 } 254 #recognizes key value in text strings 255 self.regex = re.compile(r"^title:(.+)\nuri:(.*)$")
256
257 - def _to_text(self, bookmark):
258 return "title:%s\nuri:%s" % (bookmark.title, bookmark.uri)
259
260 - def _to_key_value(self, txt):
261 m = self.regex.match(txt) 262 if m != None and len(m.groups()) == 2: 263 return m.group(1),m.group(2) 264 else: 265 return None,None
266
267 - def bookmark_to_text(self, bookmark):
268 t = Text.Text( 269 text=self._to_text(bookmark) 270 ) 271 return t
272
273 - def text_to_bookmark(self, text):
274 bookmark = None 275 k,v = self._to_key_value(text.get_string()) 276 if k != None and v != None: 277 bookmark = Bookmark.Bookmark( 278 title=k, 279 uri=v 280 ) 281 return bookmark
282
283 - def bookmark_to_file(self, bookmark):
284 f = File.TempFile( 285 self._to_text(bookmark) 286 ) 287 f.force_new_filename(bookmark.title.replace("/","_")) 288 f.force_new_file_extension(".txt") 289 return f
290
291 - def file_to_bookmark(self, f):
292 bookmark = None 293 if f.get_mimetype().startswith("text"): 294 txt = f.get_contents_as_text() 295 k,v = self._to_key_value(txt) 296 if k != None and v != None: 297 bookmark = Bookmark.Bookmark( 298 title=k, 299 uri=v 300 ) 301 return bookmark
302