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

Source Code for Module conduit.modules.TomboyModule

  1  import dbus 
  2  import dbus.glib 
  3  import logging 
  4  import string 
  5  log = logging.getLogger("modules.Tomboy") 
  6   
  7  import conduit 
  8  import conduit.TypeConverter as TypeConverter 
  9  import conduit.dataproviders.DataProvider as DataProvider 
 10  import conduit.dataproviders.AutoSync as AutoSync 
 11  import conduit.Exceptions as Exceptions 
 12  import conduit.datatypes.Note as Note 
 13  import conduit.datatypes.File as File 
 14  import conduit.utils as Utils 
 15   
 16  MODULES = { 
 17          "TomboyNoteTwoWay" :        { "type": "dataprovider"    }, 
 18          "TomboyNoteConverter" :     { "type": "converter"       } 
 19  } 
 20   
21 -class TomboyNote(Note.Note):
22 """ 23 Stores both the text and xml representations of the note 24 """
25 - def __init__(self, title, contents, xml):
26 Note.Note.__init__(self, title, contents) 27 self.xml = xml
28
29 - def get_xml(self):
30 return self.xml
31
32 - def __getstate__(self):
33 data = Note.Note.__getstate__(self) 34 data["xml"] = self.xml 35 return data
36
37 - def __setstate__(self, data):
38 self.xml = data["xml"] 39 Note.Note.__setstate__(self, data)
40
41 -class TomboyNoteConverter(TypeConverter.Converter):
42 NOTE_EXTENSION = ".xml" 43 ILLEGAL_TITLE_CHARS = " /"
44 - def __init__(self):
45 self.conversions = { 46 "note,note/tomboy" : self.note_to_tomboy_note, 47 "note/tomboy,file" : self.tomboy_note_to_file, 48 "file,note/tomboy" : self.file_to_tomboy_note, 49 }
50
51 - def note_to_tomboy_note(self, note, **kwargs):
52 n = TomboyNote( 53 title=note.get_title(), 54 contents=note.get_contents(), 55 xml=None 56 ) 57 return n
58
59 - def tomboy_note_to_file(self, note, **kwargs):
60 content = note.get_xml() 61 #Old tomboy made this note, fallback to plain text 62 if content == None: 63 content = note.get_contents() 64 65 #replace the following characters in the note 66 #title with an underscore 67 filename = note.get_title().translate( 68 string.maketrans( 69 self.ILLEGAL_TITLE_CHARS, 70 "_"*len(self.ILLEGAL_TITLE_CHARS) 71 ) 72 ) 73 f = File.TempFile(content) 74 f.force_new_filename(filename) 75 f.force_new_file_extension(self.NOTE_EXTENSION) 76 return f
77
78 - def file_to_tomboy_note(self, f, **kwargs):
79 title,ext = f.get_filename_and_extension() 80 text = f.get_contents_as_text() 81 #A tomboy formatted XML file 82 if text.startswith('<?xml version="1.0" encoding="utf-8"?>') and text.find('xmlns="http://beatniksoftware.com/tomboy">') > 0: 83 note = TomboyNote( 84 title=Utils.xml_extract_value_from_tag("title", text), 85 contents=None, 86 xml=text 87 ) 88 #A bog standard text file 89 else: 90 note = TomboyNote( 91 title=title, 92 contents=text, 93 xml=None 94 ) 95 return note
96
97 -class TomboyNoteTwoWay(DataProvider.TwoWay, AutoSync.AutoSync):
98 """ 99 LUID is the tomboy uid string 100 """ 101 _name_ = "Tomboy Notes" 102 _description_ = "Sync your Tomboy notes" 103 _category_ = conduit.dataproviders.CATEGORY_NOTES 104 _module_type_ = "twoway" 105 _in_type_ = "note/tomboy" 106 _out_type_ = "note/tomboy" 107