def test_section_set(self, sec1, sec2):
        """ OptionModel:section:set change case of value should work """

        p = OptionModel('KEY', sec1, 0, int)

        p.section = sec2
        self.assertEqual(p.section, sec2)
    def test_section_set_raise(self, sec1, sec2):
        """ OptionModel:section:set change section should raise """

        p = OptionModel('KEY', sec1, 0, int)

        with self.assertRaisesRegexp(ValueError, r'already set'):
            p.section = sec2
    def test_init(self):
        """ OptionModel: __init__ """
        p = OptionModel('KEY', 'GeneralSection', 0, int)
        st = {}
        p.bindToConfig(lambda: st)

        self.assertIsNotNone(p)
        self.assertIsInstance(p, OptionModel)
    def test_get_default_with_conv(self, deflt, conv, expval, exptp):
        """ OptionModel: test get of default value vith type conversion """
        p = OptionModel('KEY', 'GeneralSection', deflt, conv)
        st = {}
        p.bindToConfig(lambda: st)

        act = p.get()
        self.assertIsInstance(act, exptp)
        self.assertEqual(act, expval)
    def test_get_default(self, deflt, tp):
        """ OptionModel: test get of default value """
        p = OptionModel('KEY', 'GeneralSection', deflt, tp)
        st = {}
        p.bindToConfig(lambda: st)

        act = p.get()
        self.assertIsInstance(act, tp)
        self.assertEqual(act, deflt)
    def test_section_set_none_to_any(self, new_sec_name):
        """ OptionModel:section:set set value from None to any """

        section_name = None
        p = OptionModel('KEY', section_name, 0, int)

        p.section = new_sec_name

        self.assertEqual(p.section, new_sec_name)
    def test_get_wrong_type_falls_back_to_default(self, deflt, conv, confval):
        """ OptionModel: get with wrong type in config SHOULD fallback to default """
        sec = 'AnySection'
        key = 'ANYKEY'
        inikey = key.lower()
        p = OptionModel(key, sec, deflt, conv)
        st = {
            sec: {
                inikey: confval
            }
        }
        p.bindToConfig(lambda: st)

        act = p.get()
        self.assertEqual(act, deflt)
Beispiel #8
0
    def __init__(self, appkey, section, default=None, options=None, initype=str):
        """Initialization

        Args:
            appkey - unique identifier for an option
            section - name of section in CONFIG FILE
            default - default value
            initype - function to convert data from internal form to config
            options - list of suboptions
        """
        super(OptionBase, self).__init__()

        self.model = OptionModel(appkey=appkey, section=section, default=default, initype=initype)

        self._options = options or []  # should not be None
        self._initype = initype

        self._uinames = ['hp_ui_' + self.model.appkey.lower()]

        self.visible = True
        self.readonly = False
    def test_get_without_binding_raises(self, deflt, tp):
        """ OptionModel:get without bindToConfig SHOULD raise """
        p = OptionModel('ANYKEY', 'AnySection', deflt, tp)

        with self.assertRaisesRegexp(ConfigError, r'ANYKEY.*not\sbinded'):
            p.get()
Beispiel #10
0
class OptionBase(Renderable):
    """ Base, abstract parent for all other options """

    def __init__(self, appkey, section, default=None, options=None, initype=str):
        """Initialization

        Args:
            appkey - unique identifier for an option
            section - name of section in CONFIG FILE
            default - default value
            initype - function to convert data from internal form to config
            options - list of suboptions
        """
        super(OptionBase, self).__init__()

        self.model = OptionModel(appkey=appkey, section=section, default=default, initype=initype)

        self._options = options or []  # should not be None
        self._initype = initype

        self._uinames = ['hp_ui_' + self.model.appkey.lower()]

        self.visible = True
        self.readonly = False

    def __repr__(self):
        return "<{0} appkey={1}>".format(
            self.__class__.__name__,
            self.model.appkey
        )

    def __iter__(self):
        """ Iterates over visible suboptions """
        return _get_iterator_over_visible(self._options)

    def __len__(self):
        return len(self._options)

    def render(self, parent=None):
        return super(OptionBase, self).render(me=self, parent=parent)

    @property
    def appkey(self):
        return self.model.appkey

    @property
    def value(self):
        """ raw, runtime value of the option """
        return self.model.get()

    def uiValue(self):
        """ UI-friendly value of the option """
        v = self.model.get()
        if v is None:
            v = ''
        return v

    def uiValue2DataValue(self, valuedict):
        """
        Parses the value of an options, received from UI.
        Returned value MUST BE compatible with datamodel's value

        Args:

            @valuedict
                dict. Keys are from self.uiNamesList(), values - from submitted form (from UI).

        """
        value = valuedict[self.uiName()]
        return self._initype(value)

    def uiName(self):
        """This is a main identifier of the option in the UI.

        This identifier will be used:
        1. for generating identifiers (and names) for UI-input-elements
        2. for handling results, received from UI (on submit form)

        Also, extended options, with more than one input box (like `OptionExtra`), uses this
        method during generating names for child-options.
        """
        return self._uinames[0]

    def uiNamesList(self):
        """ MUST return list with all NAMEs, used in UI """
        return self._uinames