Пример #1
0
    def validate_recdic(self, recdic):
        if recdic.has_key("image") and not recdic.has_key("thumb"):
            # if we have an image but no thumbnail, we want to create the thumbnail.
            try:
                img = ImageExtras.get_image_from_string(recdic["image"])
                thumb = ImageExtras.resize_image(img, 40, 40)
                ofi = StringIO.StringIO()
                thumb.save(ofi, "JPEG")
                recdic["thumb"] = ofi.getvalue()
                ofi.close()
            except:
                del recdic["image"]
                print """Warning: gourmet couldn't recognize the image.

                Proceding anyway, but here's the traceback should you
                wish to investigate.
                """
                import traceback

                traceback.print_stack()
        for k, v in recdic.items():
            try:
                recdic[k] = v.strip()
            except:
                pass
Пример #2
0
 def write_image(self, data):
     fn = ImageExtras.write_image_tempfile(data)
     i = platypus.Image(fn)
     self.scale_image(i)
     factor = 1
     MAX_WIDTH = self.doc.frame_width * 0.35
     MAX_HEIGHT = self.doc.frame_height * 0.5
     if i.drawWidth > MAX_WIDTH:
         factor = MAX_WIDTH / i.drawWidth
     if i.drawHeight > MAX_HEIGHT:
         f = MAX_HEIGHT / i.drawHeight
         if f < factor: factor = f
     if factor < 1.0:
         self.scale_image(i, factor)
     self.image = i
Пример #3
0
 def write_image (self, data):
     fn = ImageExtras.write_image_tempfile(data)
     i = platypus.Image(fn)
     self.scale_image(i)
     factor = 1
     MAX_WIDTH = self.doc.frame_width * 0.35
     MAX_HEIGHT = self.doc.frame_height * 0.5
     if i.drawWidth > MAX_WIDTH:
         factor = MAX_WIDTH/i.drawWidth
     if i.drawHeight > MAX_HEIGHT:
         f = MAX_HEIGHT/i.drawHeight
         if f < factor: factor = f
     if factor < 1.0:
         self.scale_image(i,factor)
     self.image = i
Пример #4
0
    def validate_recdic(self, recdic):
        if recdic.has_key('image') and not recdic.has_key('thumb'):
            # if we have an image but no thumbnail, we want to create the thumbnail.
            try:
                img = ImageExtras.get_image_from_string(recdic['image'])
                thumb = ImageExtras.resize_image(img, 40, 40)
                ofi = StringIO.StringIO()
                thumb.save(ofi, 'JPEG')
                recdic['thumb'] = ofi.getvalue()
                ofi.close()
            except:
                del recdic['image']
                print """Warning: gourmet couldn't recognize the image.

                Proceding anyway, but here's the traceback should you
                wish to investigate.
                """
                import traceback
                traceback.print_stack()
        for k, v in recdic.items():
            try:
                recdic[k] = v.strip()
            except:
                pass
Пример #5
0
    def commit_changes(self):
        def mark_sorter(a, b):
            a = self.tb.get_iter_at_mark(a[0]).get_offset()
            b = self.tb.get_iter_at_mark(b[0]).get_offset()
            return cmp(a, b)

        self.labelled.sort(mark_sorter)
        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:
                    ifi = file(imageBrowser.get_image_file(ibd.ret), 'r')
                    image_str = ifi.read()
                    ifi.close()
                    image = ImageExtras.get_image_from_string(image_str)
                    # Adding image!
                    thumb = ImageExtras.resize_image(image, 40, 40)
                    self.rd.modify_rec(
                        rec, {
                            'image': ImageExtras.get_string_from_image(image),
                            'thumb': ImageExtras.get_string_from_image(thumb),
                        })
        if self.modal:
            self.w.hide()
            gtk.main_quit()
Пример #6
0
 def commit_rec (self):
     timeaction = TimeAction('importer.commit_rec',10)
     for key in ['cuisine','category','title']:
         if self.rec.has_key(key):
             self.rec[key]=unicode(re.sub('\s+',' ',self.rec[key]).strip())
     # if yields/servings can't be recognized as a number, add them
     # to the instructions.
     if self.rec.has_key('yields'):
         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 self.rec.has_key('servings'):
         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 self.rec.has_key(t) and type(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 self.rec.has_key(k): 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 self.rec.has_key('rating') and type(self.rec['rating']) not in [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 = ImageExtras.get_image_from_string(self.rec['image'])
             if img:
                 thumb = ImageExtras.resize_image(img,40,40)
                 self.rec['thumb'] = ImageExtras.get_string_from_image(thumb)
                 # Make sure our image is properly formatted...
                 self.rec['image'] = ImageExtras.get_string_from_image(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 self.rec.has_key('id'):
         id_to_convert = self.rec['id']            
     else:
         id_to_convert = None
     if id_to_convert:
         if self.id_converter.has_key(self.rec['id']):
             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 i.has_key('id'):
             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)
             )
Пример #7
0
 def commit_changes (self):
     def mark_sorter (a,b):
         a = self.tb.get_iter_at_mark(a[0]).get_offset()
         b = self.tb.get_iter_at_mark(b[0]).get_offset()
         return cmp(a,b)
     self.labelled.sort(mark_sorter)
     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:
                 ifi = file(imageBrowser.get_image_file(ibd.ret),'r')
                 image_str = ifi.read(); ifi.close()
                 image = ImageExtras.get_image_from_string(image_str)
                 # Adding image!
                 thumb = ImageExtras.resize_image(image,40,40)
                 self.rd.modify_rec(rec,{'image':ImageExtras.get_string_from_image(image),
                                         'thumb':ImageExtras.get_string_from_image(thumb),
                                         })
     if self.modal:
         self.w.hide()
         gtk.main_quit()
Пример #8
0
 def commit_rec (self):
     timeaction = TimeAction('importer.commit_rec',10)
     for key in ['cuisine','category','title']:
         if self.rec.has_key(key):
             self.rec[key]=unicode(re.sub('\s+',' ',self.rec[key]).strip())
     # if yields/servings can't be recognized as a number, add them
     # to the instructions.
     if self.rec.has_key('yields'):
         try:
             self.rec['yields'] = float(self.rec['yields'])
         except:
             yields = self.convert_str_to_num(self.rec['yields'])
             if not yields:
                 print 'Moving yields to instructions!'
                 self._move_to_instructions(self.rec,'yields')
             else:
                 self.rec['yields'] = yields
     if self.rec.has_key('servings'):
         servs=self.convert_str_to_num(self.rec['servings'])
         if servs != None:
             #self.rec['servings'] = str(servs)
             self.rec['yields'] = float(servs)
             self.rec['yield_unit'] = gettext.ngettext('serving',
                                                       'servings',
                                                       servs)
         else:
             self._move_to_instructions(self.rec,'servings')
     # Check preptime and cooktime
     for t in ['preptime','cooktime']:
         if self.rec.has_key(t) and type(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 self.rec.has_key(k): 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 self.rec.has_key('rating') and type(self.rec['rating']) not in [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 = ImageExtras.get_image_from_string(self.rec['image'])
             if img:
                 thumb = ImageExtras.resize_image(img,40,40)
                 self.rec['thumb'] = ImageExtras.get_string_from_image(thumb)
                 # Make sure our image is properly formatted...
                 self.rec['image'] = ImageExtras.get_string_from_image(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 self.rec.has_key('id'):
         id_to_convert = self.rec['id']            
     else:
         id_to_convert = None
     if id_to_convert:
         if self.id_converter.has_key(self.rec['id']):
             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 i.has_key('id'):
             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)
             )