Ejemplo n.º 1
0
    def get_nutritional_info_as_text_blob(self, rec):
        if not Prefs.instance()().get('include_nutritional_info_in_export',
                                      True):
            return None
        txt = ''
        footnotes = ''
        rd = default_rec_manager()
        nd = rd.nd
        nutinfo = nd.get_nutinfo_for_inglist(rd.get_ings(rec), rd)
        ings = rd.get_ings(rec)
        vapor = nutinfo._get_vapor()
        if len(vapor) == len(ings): return None
        if len(vapor) >= 1 and not Prefs.instance()().get(
                'include_partial_nutritional_info', False):
            return None
        if rec.yields and rec.yield_unit:
            singular_unit = gourmet.defaults.get_pluralized_form(
                rec.yield_unit, 1)
            txt += '<i>%s</i>' % (
                (rec.yields
                 and _('Nutritional information reflects amount per %s.' %
                       singular_unit)) or
                _('Nutritional information reflects amounts for entire recipe')
            )

        if vapor:
            txt = txt + '*'
            footnotes = '\n*' + _(
                'Nutritional information is missing for %s ingredients: %s'
            ) % (len(vapor), ', '.join(
                [escape(nv.__ingobject__.item) for nv in vapor]))
        for itm in MAIN_NUT_LAYOUT:
            if itm == SEP:
                # We don't have any nice way of outputting separator
                # lines in our export
                continue
            else:
                label, typ, name, properties, show_percent, unit = itm
                if typ == MAJOR:
                    itm_text = '<b>' + label + '</b>'
                else:
                    itm_text = label
                if unit:
                    itm_text += ' (%s)' % unit
                if isinstance(properties, list):
                    amts = [getattr(nutinfo, att) for att in properties]
                    amt = sum(amts)
                else:
                    amt = getattr(nutinfo, properties)
                if rec.yields:
                    amt = amt / rec.yields
                itm_text += ' %d' % round(amt)
            txt += '\n' + itm_text
        return '\n'.join([txt, footnotes])
Ejemplo n.º 2
0
    def __init__(self, default_value=inch):
        Gtk.HBox.__init__(self)  # do we really inherit from HBox?
        self.__quiet__ = False
        self.unit_combo = Gtk.ComboBoxText()
        for key in self.units:
            self.unit_combo.append_text(key)
        unit = Prefs.instance().get('default_margin_unit', _('cm'))
        if unit not in self.units: unit = _('cm')
        self.last_unit = self.units[unit]
        cb_extras.setup_typeahead(self.unit_combo)
        cb_extras.cb_set_active_text(self.unit_combo, unit)
        self.unit_combo.connect('changed', self.unit_changed_cb)
        self.value_adjustment = Gtk.Adjustment(
            value=self.adjust_to_unit(default_value),
            lower=self.min_val / self.last_unit,
            upper=self.max_val / self.last_unit,
            step_incr=self.adjustments[self.last_unit][0],
            page_incr=self.adjustments[self.last_unit][1],
        )

        def emit_changed(*args):
            self.emit('changed')

        self.value_adjustment.connect('changed', emit_changed)
        self.value_widget = Gtk.SpinButton()
        self.value_widget.set_adjustment(self.value_adjustment)
        self.value_widget.set_digits(2)
        self.value_widget.connect('changed', emit_changed)
        self.value_widget.show()
        self.unit_combo.show()
        self.pack_start(self.value_widget, True, True, 0)
        self.pack_start(self.unit_combo, True, True, 0)
Ejemplo n.º 3
0
 def __init__ (self, url):
     self.url = url
     self.prefs = Prefs.instance()
     self.logged_in = True
     gourmet.threadManager.SuspendableThread.__init__(
         self,
         name=_('Downloading %s'%url)
         )
Ejemplo n.º 4
0
 def get_args_from_opts(self, opts):
     args = {}
     if 'PDF_EXP' not in Prefs.instance()():
         Prefs.instance()()['PDF_EXP'] = {}
     prefs = Prefs.instance()()['PDF_EXP']
     args['pagesize'] = self.page_sizes[opts[self.OPT_PS][1]]  # PAGE SIZE
     prefs['page_size'] = self.page_sizes_r[args['pagesize']]
     args['pagemode'] = self.page_modes[opts[self.OPT_PO][1]]  # PAGE MODE
     prefs['orientation'] = self.page_modes_r[args['pagemode']]
     prefs['font_size'] = args['base_font_size'] = opts[self.OPT_FS][
         1]  # FONT SIZE
     args['mode'] = self.layouts[opts[self.OPT_PL][1]]  # LAYOUT/MODE
     prefs['page_layout'] = self.layouts_r[args['mode']]
     prefs['left_margin'] = args['left_margin'] = opts[self.OPT_LM][1]
     prefs['right_margin'] = args['right_margin'] = opts[self.OPT_RM][1]
     prefs['top_margin'] = args['top_margin'] = opts[self.OPT_TM][1]
     prefs['bottom_margin'] = args['bottom_margin'] = opts[self.OPT_BM][1]
     return args
Ejemplo n.º 5
0
    def __init__(self):
        self.prefs = Prefs.instance()
        defaults = self.prefs.get('PDF_EXP', PDF_PREF_DEFAULT)
        self.size_strings = list(self.page_sizes.keys())
        self.size_strings.sort()
        for n in range(2, 5):
            self.layouts[ngettext('%s Column', '%s Columns', n) %
                         n] = ('column', n)
        self.make_reverse_dicts()
        self.layout_strings = list(self.layouts.keys())
        self.layout_strings.sort()
        margin_widgets = [
            CustomUnitOption(defaults.get(pref, PDF_PREF_DEFAULT[pref]))
            for pref in
            ['left_margin', 'right_margin', 'top_margin', 'bottom_margin']
        ]
        # Make unit changes to one widget affect all the others!
        for m in margin_widgets:
            for mm in margin_widgets:
                if mm is not m:
                    m.sync_to_other_cuo(mm)

        self.opts = [
            [
                _('Paper _Size') + ':',
                (defaults.get('page_size', PDF_PREF_DEFAULT['page_size']),
                 self.size_strings)
            ],
            [
                _('_Orientation') + ':',
                (defaults.get('orientation', PDF_PREF_DEFAULT['orientation']),
                 list(self.page_modes.keys()))
            ],
            [
                _('_Font Size') + ':',
                int(defaults.get('font_size', PDF_PREF_DEFAULT['font_size']))
            ],
            [
                _('Page _Layout'),
                (defaults.get('page_layout', PDF_PREF_DEFAULT['page_layout']),
                 self.layout_strings)
            ],
            [_('Left Margin') + ':', margin_widgets[0]],
            [_('Right Margin') + ':', margin_widgets[1]],
            [_('Top Margin') + ':', margin_widgets[2]],
            [_('Bottom Margin') + ':', margin_widgets[3]],
        ]

        self.page_drawer = PdfPageDrawer(yalign=0.0)
        self.in_ccb = False
        self.setup_widgets()
        self.table.connect('changed', self.change_cb)
        self.table.emit('changed')
        self.page_drawer.set_size_request(200, 100)
        self.page_drawer.show()
Ejemplo n.º 6
0
 def unit_changed_cb (self, widget):
     new_unit = self.units[self.unit_combo.get_active_text()]
     Prefs.instance()['default_margin_unit'] = self.unit_combo.get_active_text()
     old_val = self.value_adjustment.get_value() * self.last_unit
     self.last_unit = self.units[self.unit_combo.get_active_text()]
     new_val = self.adjust_to_unit(old_val)
     self.value_adjustment.set_upper(self.max_val / self.last_unit)
     self.value_adjustment.set_lower(self.min_val / self.last_unit)
     self.value_adjustment.set_step_increment(self.adjustments[self.last_unit][0])
     self.value_adjustment.set_page_increment(self.adjustments[self.last_unit][1])
     self.value_adjustment.set_value(new_val)
     if not self.__quiet__:
         self.emit('changed')
Ejemplo n.º 7
0
def test_get_sets_default():
    """Test that using get with a default value adds it to the dictionnary"""
    prefs = Prefs.instance()

    val = prefs.get('key', 'value')
    assert val == val

    assert prefs['key'] == val  # The value was inserted

    val = prefs.get('anotherkey')
    assert val is None

    with pytest.raises(KeyError):
        prefs['anotherkey']
Ejemplo n.º 8
0
 def create_nutrition_window (self):
     self.nutrition_window = Gtk.Dialog(_('Nutritional Information'),
                         self.pluggable.w,
                         buttons=(Gtk.STOCK_CLOSE,Gtk.ResponseType.CLOSE)
                         )
     self.nutrition_window.set_default_size(400,550)
     self.nutrition_window.set_icon(
         self.nutrition_window.render_icon('nutritional-info',
                                           Gtk.IconSize.MENU)
         )
     self.nl = NutritionLabel(Prefs.instance())
     self.sw = Gtk.ScrolledWindow(); self.sw.set_policy(Gtk.PolicyType.NEVER,Gtk.PolicyType.AUTOMATIC)
     self.sw.add_with_viewport(self.nl); self.sw.show()
     self.nutrition_window.vbox.pack_start(self.sw, True, True, 0)
     self.nutrition_window.connect('response',self.response_cb)
     self.nutrition_window.connect('close',self.response_cb)
     self.nl.yieldLabel.set_markup('<b>'+_('Amount for Shopping List')+'</b>')
     self.nl.show()
Ejemplo n.º 9
0
 def __init__(self, *args, **kwargs):
     # Create main widget
     self.widget = Gtk.VBox()
     self.prefs = Prefs.instance()
     label = Gtk.Label(label='Hello world')
     self.include_tb = Gtk.CheckButton(
         'Include nutritional information in print-outs and exports')
     self.partial_tb = Gtk.CheckButton(
         'Include partial nutritional information in print-outs and exports?'
     )
     self.include_tb.set_active(self.prefs.get(includep, True))
     self.partial_tb.set_active(self.prefs.get(partialp, False))
     self.include_tb.connect('toggled', self.toggle_cb)
     self.partial_tb.connect('toggled', self.toggle_cb)
     self.widget.pack_start(self.include_tb, expand=False, fill=False)
     self.widget.pack_start(self.partial_tb, expand=False, fill=False)
     self.widget.set_border_width(12)
     self.widget.set_spacing(6)
     self.widget.show_all()
Ejemplo n.º 10
0
    def load_active_plugins(self):
        """Enable plugins that were previously saved to the preferences"""
        prefs = Prefs.instance()
        self.active_plugin_sets = prefs.get('plugins',
                                           self.default_active_plugin_sets[:])
        self.active_plugins = []
        self.instantiated_plugins = {}

        for p in self.active_plugin_sets:
            if p in self.available_plugin_sets:
                try:
                    self.active_plugins.extend(self.available_plugin_sets[p].plugins)
                except:
                    import logging
                    import traceback
                    print('WARNING: Failed to load plugin %s'%p)
                    self.errors[p] = traceback.format_exc()
                    logging.exception('')
            else:
                print('Plugin ',p,'not found')
Ejemplo n.º 11
0
 def __init__(self, reccards):
     self.reccards = reccards
     self.prefs = Prefs.instance()
Ejemplo n.º 12
0
 def activate(self, pluggable):
     Prefs.instance()['utf-16'] = True
Ejemplo n.º 13
0
 def remove(self):
     Prefs.instance()['utf-16'] = False
Ejemplo n.º 14
0
 def save_active_plugins(self):
     prefs = Prefs.instance()
     prefs['plugins'] = self.active_plugin_sets
     prefs.save()
Ejemplo n.º 15
0
def test_singleton():
    prefs = Prefs.instance()
    pprefs = Prefs.instance()
    assert prefs == pprefs
Ejemplo n.º 16
0
 def get_amount_and_unit_hook (self, db, *args, **kwargs):
     kwargs['preferred_unit_groups'] = Prefs.instance()().get('preferred_unit_groups',[])
     return args,kwargs
Ejemplo n.º 17
0
 def __init__(self):
     BaseExporterPlugin.__init__(self)
     if Prefs.instance()().get('include_nutritional_info_in_export', True):
         self.add_field('Nutritional Information',
                        self.get_nutritional_info_as_text_blob, self.TEXT)