Package conduit :: Package dataproviders :: Module Image
[hide private]

Source Code for Module conduit.dataproviders.Image

  1  import logging 
  2  log = logging.getLogger("dataproviders.Image") 
  3   
  4  import conduit 
  5  import conduit.Exceptions as Exceptions 
  6  import conduit.datatypes.File as File 
  7  import conduit.dataproviders.DataProvider as DataProvider 
  8   
9 -class UploadInfo:
10 """ 11 Upload information container, this way we can add info 12 and keep the _upload_info method on the ImageSink retain 13 its api 14 """
15 - def __init__ (self, url, mimeType, name="", tags=(), caption=""):
16 self.url = url 17 self.mimeType = mimeType 18 self.name = name 19 self.tags = tags 20 self.caption = caption
21
22 -class ImageSink(DataProvider.DataSink):
23 """ 24 Abstract Base class for Image DataSinks 25 """ 26 _category_ = conduit.dataproviders.CATEGORY_PHOTOS 27 _module_type_ = "sink" 28 _in_type_ = "file/photo" 29 _out_type_ = "file/photo" 30 31 IMAGE_SIZES = ["640x480", "800x600", "1024x768"] 32 NO_RESIZE = "None" 33
34 - def __init__(self, *args):
36
37 - def initialize(self):
38 return True
39
40 - def _resize_combobox_build(self, combobox, selected):
41 import gtk 42 store = gtk.ListStore(str) 43 cell = gtk.CellRendererText() 44 combobox.pack_start(cell, True) 45 combobox.add_attribute(cell, 'text', 0) 46 combobox.set_model(store) 47 48 for s in [self.NO_RESIZE] + self.IMAGE_SIZES: 49 rowref = store.append( (s,) ) 50 if s == selected: 51 combobox.set_active_iter(rowref)
52
53 - def _resize_combobox_get_active(self, combobox):
54 model = combobox.get_model() 55 active = combobox.get_active() 56 if active < 0: 57 return self.NO_RESIZE 58 59 size = model[active][0] 60 if size not in self.IMAGE_SIZES: 61 return self.NO_RESIZE 62 63 return size
64
65 - def _get_photo_info(self, photoID):
66 """ 67 This should return the info for a given photo id, 68 If this returns anything different from None, it will be 69 passed onto _get_raw_photo_url 70 """ 71 return None
72
73 - def _get_raw_photo_url(self, photoInfo):
74 """ 75 This should return the url of the online photo 76 """ 77 return None
78
79 - def _upload_photo (self, uploadInfo):
80 """ 81 Upload a photo 82 """ 83 return None
84
85 - def _replace_photo (self, id, uploadInfo):
86 """ 87 Replace a photo with a new version 88 """ 89 return id
90
91 - def _get_photo_formats (self):
92 """ 93 This should return the allowed photo mimetypes 94 """ 95 return ("image/jpeg", "image/png")
96
97 - def _get_default_format (self):
98 """ 99 This should return the preferred format of images the sink accepts 100 """ 101 return "image/jpeg"
102
103 - def _get_photo_size (self):
104 """ 105 Return the preferred photo size string for rescaling, or None 106 """ 107 return None
108
109 - def put(self, photo, overwrite, LUID=None):
110 """ 111 Accepts a vfs file. Must be made local. 112 I also store a md5 of the photos uri to check for duplicates 113 """ 114 DataProvider.DataSink.put(self, photo, overwrite, LUID) 115 116 originalName = photo.get_filename() 117 #Gets the local URI (/foo/bar). If this is a remote file then 118 #it is first transferred to the local filesystem 119 photoURI = photo.get_local_uri() 120 mimeType = photo.get_mimetype() 121 tags = photo.get_tags () 122 caption = photo.get_caption() 123 124 uploadInfo = UploadInfo(photoURI, mimeType, originalName, tags, caption) 125 126 #Check if we have already uploaded the photo 127 if LUID != None: 128 info = self._get_photo_info(LUID) 129 #check if a photo exists at that UID 130 if info != None: 131 if overwrite == True: 132 #replace the photo 133 return self._replace_photo(LUID, uploadInfo) 134 else: 135 #Only upload the photo if it is newer than the Remote one 136 url = self._get_raw_photo_url(info) 137 remoteFile = File.File(url) 138 139 #this is a limited test for equality type comparison 140 comp = photo.compare(remoteFile,True) 141 log.debug("Compared %s with %s to check if they are the same (size). Result = %s" % 142 (photo.get_filename(),remoteFile.get_filename(),comp)) 143 if comp != conduit.datatypes.COMPARISON_EQUAL: 144 raise Exceptions.SynchronizeConflictError(comp, photo, remoteFile) 145 else: 146 return conduit.datatypes.Rid(uid=LUID) 147 148 log.debug("Uploading Photo URI = %s, Mimetype = %s, Original Name = %s" % (photoURI, mimeType, originalName)) 149 150 #upload the file 151 return self._upload_photo (uploadInfo)
152
153 - def delete(self, LUID):
154 pass
155
157 args = { 158 "formats" : ','.join(self._get_photo_formats()), 159 "default-format" : self._get_default_format(), 160 "size" : self._get_photo_size(), 161 } 162 return args
163
164 -class ImageTwoWay(DataProvider.DataSource, ImageSink):
165 """ 166 Abstract Base Class for ImageTwoWay dataproviders 167 """ 168 169