示例#1
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()
示例#2
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()