def test_image_utils(): image = bytes_to_image(IMAGE) bytes_ = image_to_bytes(image) image2 = bytes_to_image(bytes_) assert ImageChops.difference(image, image2).getbbox() == (0, 0, 57, 40)
def test_image_to_pixbuf(): image = bytes_to_image(IMAGE) pixbuf = image_to_pixbuf(image) assert isinstance(pixbuf, Pixbuf)
def commit_rec (self): timeaction = TimeAction('importer.commit_rec',10) for key in ['cuisine','category','title']: if key in self.rec: self.rec[key]=str(re.sub(r'\s+',' ',self.rec[key]).strip()) # if yields/servings can't be recognized as a number, add them # to the instructions. if 'yields' in self.rec: try: self.rec['yields'] = float(self.rec['yields']) except: yields,yield_unit = self.parse_yields(self.rec['yields']) if not yields: print('Moving yields to instructions!') self._move_to_instructions(self.rec,'yields') else: self.rec['yields'] = yields self.rec['yield_unit'] = yield_unit if 'servings' in self.rec: servs=self.convert_str_to_num(self.rec['servings']) if servs != None: self.rec['yields'] = float(servs) self.rec['yield_unit'] = gettext.ngettext('serving', 'servings', servs) del self.rec['servings'] else: self._move_to_instructions(self.rec,'servings') # Check preptime and cooktime for t in ['preptime','cooktime']: if t in self.rec and not isinstance(self.rec[t], int): secs = self.conv.timestring_to_seconds(self.rec[t]) if secs != None: self.rec[t]=secs else: self._move_to_instructions(self.rec,t) # Markup instructions and mods as necessary if self.do_markup: for k in ['instructions','modifications']: if k in self.rec: self.rec[k] = xml.sax.saxutils.escape(self.rec[k]) # A little strange, this is, but for UI reasons, we want to # keep track of any ratings that are not integers so that we # can ask the user how to convert them when we're all done # with importing. remembered_rating = None if 'rating' in self.rec and not isinstance(self.rec['rating'], (int, float)): if string_to_rating(self.rec['rating']): self.rec['rating']=string_to_rating(self.rec['rating']) else: remembered_rating = self.rec['rating'] del self.rec['rating'] tt=TimeAction('importer.commit_rec - rd.add_rec',5) debug('commiting recipe %s'%self.rec,0) # Check for images without thumbnails if self.rec.get('image',None) and not self.rec.get('thumb',None): if not self.rec['image']: del self.rec['image'] else: img = image_utils.bytes_to_image(self.rec['image']) if img: thumb = img.copy() thumb.thumbnail((40, 40)) self.rec['thumb'] = image_utils.image_to_bytes(thumb) # Make sure our image is properly formatted... self.rec['image'] = image_utils.image_to_bytes(img) else: print("ODD: we got no image from ",self.rec['image'][:100]) print('Deleting "image"') del self.rec['image'] del self.rec['thumb'] ## if we have an ID, we need to remember it for the converter if 'id' in self.rec: id_to_convert = self.rec['id'] else: id_to_convert = None if id_to_convert: if self.rec['id'] in self.id_converter: self.rec['id']=self.id_converter[self.rec['id']] r = self.rd.add_rec(self.rec,accept_ids=True) # See doc on add_rec else: del self.rec['id'] r = self.rd.add_rec(self.rec) self.id_converter[id_to_convert] = r.id else: r = self.rd.add_rec(self.rec) # Add ingredients... for i in self.added_ings: if 'id' in i: print('WARNING: Ingredient has ID set -- ignoring value') del i['id'] i['recipe_id'] = r.id self.rd.add_ings(self.added_ings) self.added_ings = [] # Update hash-keys... self.rd.update_hashes(r) tt.end() self.added_recs.append(r) if remembered_rating: self.rating_converter.add(r.id,remembered_rating) self.check_for_sleep() timeaction.end() self.rec_timer.end() self.count += 1 if self.total: self.emit( 'progress', float(self.count)/self.total, _("Imported %s of %s recipes.")%(self.count,self.total) )
def commit_changes(self): self.labelled.sort( key=lambda x: self.tb.get_iter_at_mark(x[0]).get_offset()) if not self.labelled: return self.start_rec() started = False for smark, emark in self.labelled: siter = self.tb.get_iter_at_mark(smark) eiter = self.tb.get_iter_at_mark(emark) text = siter.get_text(eiter) name = smark.get_name() label = name.split('-')[0] tag = self.tags_by_label[label] if tag in gglobals.TEXT_ATTR_DIC: self.add_text(tag, text) started = True elif tag in gglobals.REC_ATTR_DIC: if text: self.add_attribute(tag, text) elif tag == 'ingredient': if text: self.add_ing_from_text(text) started = True elif tag == 'ingredients': if text: self.add_ings_from_text(text) started = True elif tag == 'inggroup': if text: self.add_ing_group(text) started = True elif tag == 'newrec': if not started: continue # Then we're starting a new recipe at this point... # Commit old recipe... self.commit_rec() started = False # Start new one... self.start_rec() elif tag == 'ignore': continue elif tag == 'servings': self.add_attribute('yields', text) self.add_attribute('yield_unit', 'servings') else: try: print('UNKNOWN TAG', tag, text, label) except UnicodeError: print('UNKNOWN TAG (unprintable)') if started: self.commit_rec() if hasattr(self, 'images') and self.images: # This is ugly -- we run the dialog once per recipe. This # should happen rarely in current use-case (I don't know # of a usecase where many recipes will come from a single # text document / website); if in fact this becomes a # common usecase, we'll need to rework the UI here. for rec in self.added_recs: ibd = imageBrowser.ImageBrowserDialog( title=_('Select recipe image'), label=_('Select image for recipe "%s"') % escape(rec.title or _('Untitled')), sublabel= _("Below are all the images found for the page you are importing. Select any images that are of the recipe, or don't select anything if you don't want any of these images." ), ) for i in self.images: ibd.add_image_from_uri(i) ibd.run() if ibd.ret: with open(imageBrowser.get_image_file(ibd.ret), 'rb') as ifi: image_str = ifi.read() image = bytes_to_image(image_str) # Adding image! thumb = image.copy() thumb.thumbnail((40, 40)) self.rd.modify_rec( rec, { 'image': image_to_bytes(image), 'thumb': image_to_bytes(thumb), }) if self.modal: self.w.hide() Gtk.main_quit()