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

Source Code for Module conduit.modules.iPodModule.iPodModule

  1  """ 
  2  Provides a number of dataproviders which are associated with 
  3  removable devices such as USB keys. 
  4   
  5  It also includes classes specific to the ipod.  
  6  This file is not dynamically loaded at runtime in the same 
  7  way as the other dataproviders as it needs to be loaded all the time in 
  8  order to listen to HAL events 
  9   
 10  Copyright: John Stowers, 2006 
 11  License: GPLv2 
 12  """ 
 13  import os 
 14  import pickle 
 15  import logging 
 16  log = logging.getLogger("modules.iPod") 
 17   
 18  import conduit 
 19  import conduit.dataproviders.DataProvider as DataProvider 
 20  import conduit.dataproviders.DataProviderCategory as DataProviderCategory 
 21  import conduit.dataproviders.VolumeFactory as VolumeFactory 
 22  import conduit.utils as Utils 
 23  import conduit.datatypes.Note as Note 
 24  import conduit.datatypes.Contact as Contact 
 25  import conduit.datatypes.Event as Event 
 26  import conduit.datatypes.File as File 
 27   
 28  MODULES = { 
 29          "iPodFactory" :         { "type":   "dataprovider-factory"  } 
 30  } 
 31   
 32  try: 
 33      import gpod 
 34      LIBGPOD_PHOTOS = gpod.version_info >= (0,6,0) 
 35      log.info("Module Information: %s" % Utils.get_module_information(gpod, 'version_info')) 
 36  except ImportError: 
 37      LIBGPOD_PHOTOS = False 
 38      log.info("iPod photo support disabled") 
 39   
40 -def _string_to_unqiue_file(txt, base_uri, prefix, postfix=''):
41 for i in range(1, 10000): 42 filename = prefix + str(i) + postfix 43 uri = os.path.join(base_uri, filename) 44 f = File.File(uri) 45 if not f.exists(): 46 break 47 48 temp = Utils.new_tempfile(txt) 49 temp.transfer(uri, True) 50 temp.set_UID(filename) 51 return temp.get_rid()
52
53 -class iPodFactory(VolumeFactory.VolumeFactory):
54 - def is_interesting(self, udi, props):
55 if props.has_key("info.parent") and props.has_key("info.parent")!="": 56 prop2 = self._get_properties(props["info.parent"]) 57 if prop2.has_key("storage.model") and prop2["storage.model"]=="iPod": 58 return True 59 return False
60
61 - def get_category(self, udi, **kwargs):
62 return DataProviderCategory.DataProviderCategory( 63 kwargs['label'], 64 "multimedia-player-ipod-video-white", 65 kwargs['mount'])
66
67 - def get_dataproviders(self, udi, **kwargs):
68 if LIBGPOD_PHOTOS: 69 #Read information about the ipod, like if it supports 70 #photos or not 71 d = gpod.itdb_device_new() 72 gpod.itdb_device_set_mountpoint(d,kwargs['mount']) 73 supportsPhotos = gpod.itdb_device_supports_photo(d) 74 gpod.itdb_device_free(d) 75 if supportsPhotos: 76 return [IPodNoteTwoWay, IPodContactsTwoWay, IPodCalendarTwoWay, IPodPhotoSink] 77 78 return [IPodNoteTwoWay, IPodContactsTwoWay, IPodCalendarTwoWay]
79 80
81 -class IPodBase(DataProvider.TwoWay):
82 - def __init__(self, *args):
83 DataProvider.TwoWay.__init__(self) 84 self.mountPoint = args[0] 85 self.uid = args[1] 86 self.objects = None
87
88 - def refresh(self):
89 DataProvider.TwoWay.refresh(self) 90 self.objects = [] 91 92 #Also checks directory exists 93 if not os.path.exists(self.dataDir): 94 os.mkdir(self.dataDir) 95 96 #When acting as a source, only notes in the Notes dir are 97 #considered 98 for f in os.listdir(self.dataDir): 99 fullpath = os.path.join(self.dataDir, f) 100 if os.path.isfile(fullpath): 101 self.objects.append(f)
102
103 - def get_all(self):
104 DataProvider.TwoWay.get_all(self) 105 return self.objects
106
107 - def delete(self, LUID):
108 obj = File.File(URI=os.path.join(self.dataDir, LUID)) 109 if obj.exists(): 110 obj.delete()
111
112 - def finish(self, aborted, error, conflict):
113 DataProvider.TwoWay.finish(self) 114 self.objects = None
115
116 - def get_UID(self):
117 return self.uid
118
119 - def _get_unique_filename(self, directory):
120 """ 121 Returns the name of a non-existant file on the 122 ipod within directory 123 124 @param directory: Name of the directory within the device root to make 125 the random file in 126 """ 127 done = False 128 while not done: 129 f = os.path.join(self.mountPoint,directory,Utils.random_string()) 130 if not os.path.exists(f): 131 done = True 132 return f
133
134 -class IPodNoteTwoWay(IPodBase):
135 """ 136 Stores Notes on the iPod. 137 Rather than requiring a perfect transform to and from notes to the 138 ipod note format I also store the original note data in a 139 .conduit directory in the root of the iPod. 140 141 Notes are saved as title.txt and a copy of the raw note is saved as 142 title.note 143 144 LUID is the note title 145 """ 146 147 _name_ = "Notes" 148 _description_ = "Sync your iPod notes" 149