def test_option_constructor_more_complex_default_converters(self):
        data = {
            'default': '2011-12-31',
            'doc': "lucy's bday",
            'from_string_converter': dtu.date_from_ISO_string,
        }
        o = Option('now', **data)
        self.assertEqual(o.name, 'now')
        self.assertEqual(o.default, '2011-12-31')
        self.assertEqual(o.doc, "lucy's bday")
        self.assertEqual(o.from_string_converter, dtu.date_from_ISO_string)
        o.set_value()
        self.assertEqual(o.value, datetime.date(2011, 12, 31))

        data = {
            'default': '2011-12-31',
            'doc': "lucy's bday",
            'from_string_converter':
            'configman.datetime_util.date_from_ISO_string',
        }
        o = Option('now', **data)
        self.assertEqual(o.name, 'now')
        self.assertEqual(o.default, '2011-12-31')
        self.assertEqual(o.doc, "lucy's bday")
        self.assertEqual(o.from_string_converter, dtu.date_from_ISO_string)
        self.assertEqual(o.value, '2011-12-31')
        o.set_value()
        self.assertEqual(o.value, datetime.date(2011, 12, 31))
    def test_option_comparison(self):
        o1 = Option('name')
        o2 = Option('name')
        self.assertEqual(o1, o2)

        o1 = Option('name', 'Peter')
        o2 = Option('name', u'Peter')
        self.assertEqual(o1, o2)

        o1 = Option('name', 'Peter')
        o2 = Option('name', 'Ashley')
        self.assertNotEqual(o1, o2)

        o1 = Option('name', doc='Aaa')
        o2 = Option('name', doc='Bee')
        self.assertNotEqual(o1, o2)

        o1 = Option('name', doc='Aaa')
        o2 = Option('name', doc='Aaa')
        self.assertEqual(o1, o2)

        o1 = Option('name', doc='Aaa', short_form='n')
        o2 = Option('name', doc='Aaa', short_form='N')
        self.assertNotEqual(o1, o2)

        o1 = Option('name')
        o1.set_value('Peter')
        o2 = Option('name')
        self.assertNotEqual(o1, o2)
Beispiel #3
0
    def test_option_comparison(self):
        o1 = Option('name')
        o2 = Option('name')
        self.assertEqual(o1, o2)

        o1 = Option('name', 'Peter')
        o2 = Option('name', u'Peter')
        self.assertEqual(o1, o2)

        o1 = Option('name', 'Peter')
        o2 = Option('name', 'Ashley')
        self.assertNotEqual(o1, o2)

        o1 = Option('name', doc='Aaa')
        o2 = Option('name', doc='Bee')
        self.assertNotEqual(o1, o2)

        o1 = Option('name', doc='Aaa')
        o2 = Option('name', doc='Aaa')
        self.assertEqual(o1, o2)

        o1 = Option('name', doc='Aaa', short_form='n')
        o2 = Option('name', doc='Aaa', short_form='N')
        self.assertNotEqual(o1, o2)

        o1 = Option('name')
        o1.set_value('Peter')
        o2 = Option('name')
        self.assertNotEqual(o1, o2)
Beispiel #4
0
    def test_option_constructor_more_complex_default_converters(self):
        data = {
            'default': '2011-12-31',
            'doc': "lucy's bday",
            'from_string_converter': date_from_ISO_string,
        }
        o = Option('now', **data)
        self.assertEqual(o.name, 'now')
        self.assertEqual(o.default, '2011-12-31')
        self.assertEqual(o.doc, "lucy's bday")
        self.assertEqual(o.from_string_converter, date_from_ISO_string)
        o.set_value()
        self.assertEqual(o.value, datetime.date(2011, 12, 31))

        data = {
            'default':
            '2011-12-31',
            'doc':
            "lucy's bday",
            'from_string_converter':
            'configman.datetime_util.date_from_ISO_string',
        }
        o = Option('now', **data)
        self.assertEqual(o.name, 'now')
        self.assertEqual(o.default, '2011-12-31')
        self.assertEqual(o.doc, "lucy's bday")
        self.assertEqual(o.from_string_converter, date_from_ISO_string)
        self.assertEqual(o.value, '2011-12-31')
        o.set_value()
        self.assertEqual(o.value, datetime.date(2011, 12, 31))
Beispiel #5
0
    def test_list_converter_inOption(self):
        some_list = ['some', 'values', 'here']
        opt = Option('some name', default=some_list)
        self.assertEqual(opt.default, some_list)
        self.assertEqual(opt.from_string_converter, list_converter)

        opt.set_value('list, of, things')
        self.assertEqual(opt.value, ['list', 'of', 'things'])
Beispiel #6
0
    def test_set_value_from_mapping(self):
        o1 = Option('name')
        val = {'default': u'Peter'}
        o1.set_value(val)
        self.assertEqual(o1.value, 'Peter')

        val = {'justanother': 'dict!'}
        o1.set_value(val)
        self.assertEqual(o1.value, val)
    def test_list_converter_inOption(self):
        some_list = ['some', 'values', 'here']
        opt = Option('some name', default=some_list)
        self.assertEqual(opt.default, some_list)
        self.assertEqual(opt.from_string_converter,
                         conv.list_converter)

        opt.set_value('list, of, things')
        self.assertEqual(opt.value, ['list', 'of', 'things'])
Beispiel #8
0
    def test_regexp_converter_inOption(self):
        regex_str = '\w+'
        sample_regex = re.compile(regex_str)
        opt = Option('name', default=sample_regex)
        self.assertEqual(opt.default, sample_regex)
        self.assertEqual(opt.from_string_converter, regex_converter)

        opt.set_value(regex_str)
        self.assertEqual(opt.value.pattern, sample_regex.pattern)
    def test_set_value_from_mapping(self):
        o1 = Option('name')
        val = {'default': u'Peter'}
        o1.set_value(val)
        self.assertEqual(o1.value, 'Peter')

        val = {'justanother': 'dict!'}
        o1.set_value(val)
        self.assertEqual(o1.value, val)
    def test_regexp_converter_inOption(self):
        regex_str = '\w+'
        sample_regex = re.compile(regex_str)
        opt = Option('name', default=sample_regex)
        self.assertEqual(opt.default, sample_regex)
        self.assertEqual(opt.from_string_converter,
                         conv.regex_converter)

        opt.set_value(regex_str)
        self.assertEqual(opt.value.pattern, sample_regex.pattern)
Beispiel #11
0
 def set_value(self, name, value, strict=True):
     name_parts = name.split('.', 1)
     prefix = name_parts[0]
     try:
         candidate = getattr(self, prefix)
     except KeyError:
         if strict:
             raise
         candidate = Option(name)
         setattr(self, prefix, candidate)
     candidate_type = type(candidate)
     if candidate_type == Namespace:
         candidate.set_value(name_parts[1], value, strict)
     else:
         candidate.set_value(value)
Beispiel #12
0
 def set_value(self, name, value, strict=True):
     name_parts = name.split('.', 1)
     prefix = name_parts[0]
     try:
         candidate = getattr(self, prefix)
     except KeyError:
         if strict:
             raise
         candidate = Option(name)
         setattr(self, prefix, candidate)
     candidate_type = type(candidate)
     if candidate_type == Namespace:
         candidate.set_value(name_parts[1], value, strict)
     else:
         candidate.set_value(value)
    def test_set_value_from_other_option(self):
        o1 = Option('name')
        o1.set_value('Peter')
        o2 = Option('name')
        o2.set_value(o1)
        self.assertEqual(o2.value, None)

        o1 = Option('name', default='Your name here')
        o1.set_value('Peter')
        o2 = Option('name')
        o2.set_value(o1)
        self.assertEqual(o2.value, 'Your name here')
Beispiel #14
0
    def test_set_value_from_other_option(self):
        o1 = Option('name')
        o1.set_value('Peter')
        o2 = Option('name')
        o2.set_value(o1)
        self.assertEqual(o2.value, None)

        o1 = Option('name', default='Your name here')
        o1.set_value('Peter')
        o2 = Option('name')
        o2.set_value(o1)
        self.assertEqual(o2.value, 'Your name here')
Beispiel #15
0
    def test_timedelta_converter_inOption(self):
        one_day = datetime.timedelta(days=1)
        opt = Option('some name', default=one_day)
        self.assertEqual(opt.default, one_day)
        self.assertEqual(opt.from_string_converter, timedelta_converter)

        two_days = datetime.timedelta(days=2)
        timedelta_as_string = timedelta_to_str(two_days)
        assert isinstance(timedelta_as_string, basestring)
        opt.set_value(timedelta_as_string)
        self.assertEqual(opt.value, two_days)

        opt.set_value(unicode(timedelta_as_string))
        self.assertEqual(opt.value, two_days)

        opt.set_value(two_days)
        self.assertEqual(opt.value, two_days)

        self.assertRaises(CannotConvertError, opt.set_value, 'JUNK')

        self.assertRaises(CannotConvertError, opt.set_value, '0:x:0:0')
    def test_timedelta_converter_inOption(self):
        one_day = datetime.timedelta(days=1)
        opt = Option('some name', default=one_day)
        self.assertEqual(opt.default, one_day)
        self.assertEqual(opt.from_string_converter,
                         conv.timedelta_converter)

        two_days = datetime.timedelta(days=2)
        timedelta_as_string = dtu.timedelta_to_str(two_days)
        assert isinstance(timedelta_as_string, basestring)
        opt.set_value(timedelta_as_string)
        self.assertEqual(opt.value, two_days)

        opt.set_value(unicode(timedelta_as_string))
        self.assertEqual(opt.value, two_days)

        opt.set_value(two_days)
        self.assertEqual(opt.value, two_days)

        self.assertRaises(CannotConvertError,
                          opt.set_value, 'JUNK')

        self.assertRaises(CannotConvertError,
                          opt.set_value, '0:x:0:0')
    def test_boolean_converter_inOption(self):
        opt = Option('name', default=False)
        self.assertEqual(opt.default, False)
        self.assertEqual(opt.from_string_converter,
                         conv.boolean_converter)

        opt.set_value('true')
        self.assertEqual(opt.value, True)

        opt.set_value('false')
        self.assertEqual(opt.value, False)

        opt.set_value('1')
        self.assertEqual(opt.value, True)

        opt.set_value('t')
        self.assertEqual(opt.value, True)

        opt.set_value(True)
        self.assertEqual(opt.value, True)

        opt.set_value(False)
        self.assertEqual(opt.value, False)

        opt.set_value('False')
        self.assertEqual(opt.value, False)

        opt.set_value('True')
        self.assertEqual(opt.value, True)

        opt.set_value('None')
        self.assertEqual(opt.value, False)

        opt.set_value('YES')
        self.assertEqual(opt.value, True)

        opt.set_value(u'1')
        self.assertEqual(opt.value, True)

        opt.set_value(u'y')
        self.assertEqual(opt.value, True)

        opt.set_value(u't')
        self.assertEqual(opt.value, True)
    def test_option_constructor_basics(self):
        o = Option('name')
        self.assertEqual(o.name, 'name')
        self.assertEqual(o.default, None)
        self.assertEqual(o.doc, None)
        self.assertEqual(o.from_string_converter, None)
        self.assertEqual(o.value, None)

        o = Option('lucy')
        self.assertEqual(o.name, 'lucy')
        self.assertEqual(o.default, None)
        self.assertEqual(o.doc, None)
        self.assertEqual(o.from_string_converter, None)
        self.assertEqual(o.value, None)

        o = Option(u'spa\xa0e')
        self.assertEqual(o.name, u'spa\xa0e')
        self.assertEqual(o.default, None)
        self.assertEqual(o.doc, None)
        self.assertEqual(o.from_string_converter, None)
        self.assertEqual(o.value, None)

        data = {
            'name': 'lucy',
            'default': 1,
            'doc': "lucy's integer"
        }
        o = Option(**data)
        self.assertEqual(o.name, 'lucy')
        self.assertEqual(o.default, 1)
        self.assertEqual(o.doc, "lucy's integer")
        self.assertEqual(o.from_string_converter, int)
        self.assertEqual(o.value, 1)

        data = {
            'name': 'lucy',
            'default': 1,
            'doc': "lucy's integer",
            'value': '1'
        }
        o = Option(**data)
        self.assertEqual(o.name, 'lucy')
        self.assertEqual(o.default, 1)
        self.assertEqual(o.doc, "lucy's integer")
        self.assertEqual(o.from_string_converter, int)
        self.assertEqual(o.value, '1')

        data = {
            'name': 'lucy',
            'default': '1',
            'doc': "lucy's integer",
            'from_string_converter': int
        }
        o = Option(**data)
        self.assertEqual(o.name, 'lucy')
        self.assertEqual(o.default, '1')
        self.assertEqual(o.doc, "lucy's integer")
        self.assertEqual(o.from_string_converter, int)
        self.assertEqual(o.value, '1')
        o.set_value()
        self.assertEqual(o.default, '1')
        self.assertEqual(o.value, 1)

        data = {
            'name': 'lucy',
            'default': '1',
            'doc': "lucy's integer",
            'from_string_converter': int,
        }
        o = Option(**data)
        self.assertEqual(o.name, 'lucy')
        self.assertEqual(o.default, '1')
        self.assertEqual(o.doc, "lucy's integer")
        self.assertEqual(o.from_string_converter, int)
        self.assertEqual(o.value, '1')

        data = {
            'default': '1',
            'doc': "lucy's integer",
            'from_string_converter': int,
        }
        o = Option('now', **data)
        self.assertEqual(o.name, 'now')
        self.assertEqual(o.default, '1')
        self.assertEqual(o.doc, "lucy's integer")
        self.assertEqual(o.from_string_converter, int)
        self.assertEqual(o.value, '1')

        d = datetime.datetime.now()
        o = Option('now', default=d)
        self.assertEqual(o.name, 'now')
        self.assertEqual(o.default, d)
        self.assertEqual(o.doc, None)
        self.assertEqual(o.from_string_converter,
                         dtu.datetime_from_ISO_string)
        self.assertEqual(o.value, d)

        data = {
            'default': '1.0',
            'doc': "lucy's height",
            'from_string_converter': float,
        }
        o = Option('now', **data)
        self.assertEqual(o.name, 'now')
        self.assertEqual(o.default, '1.0')
        self.assertEqual(o.doc, "lucy's height")
        self.assertEqual(o.from_string_converter, float)
        self.assertEqual(o.value, '1.0')
        o.set_value()
        self.assertEqual(o.default, '1.0')
        self.assertEqual(o.value, 1.0)
Beispiel #19
0
    def test_option_constructor_basics(self):
        o = Option('name')
        self.assertEqual(o.name, 'name')
        self.assertEqual(o.default, None)
        self.assertEqual(o.doc, None)
        self.assertEqual(o.from_string_converter, None)
        self.assertEqual(o.value, None)

        o = Option('lucy')
        self.assertEqual(o.name, 'lucy')
        self.assertEqual(o.default, None)
        self.assertEqual(o.doc, None)
        self.assertEqual(o.from_string_converter, None)
        self.assertEqual(o.value, None)

        o = Option(u'spa\xa0e')
        self.assertEqual(o.name, u'spa\xa0e')
        self.assertEqual(o.default, None)
        self.assertEqual(o.doc, None)
        self.assertEqual(o.from_string_converter, None)
        self.assertEqual(o.value, None)

        data = {'name': 'lucy', 'default': 1, 'doc': "lucy's integer"}
        o = Option(**data)
        self.assertEqual(o.name, 'lucy')
        self.assertEqual(o.default, 1)
        self.assertEqual(o.doc, "lucy's integer")
        self.assertEqual(o.from_string_converter, int)
        self.assertEqual(o.value, 1)

        data = {
            'name': 'lucy',
            'default': 1,
            'doc': "lucy's integer",
            'value': '1'
        }
        o = Option(**data)
        self.assertEqual(o.name, 'lucy')
        self.assertEqual(o.default, 1)
        self.assertEqual(o.doc, "lucy's integer")
        self.assertEqual(o.from_string_converter, int)
        self.assertEqual(o.value, '1')

        data = {
            'name': 'lucy',
            'default': '1',
            'doc': "lucy's integer",
            'from_string_converter': int
        }
        o = Option(**data)
        self.assertEqual(o.name, 'lucy')
        self.assertEqual(o.default, '1')
        self.assertEqual(o.doc, "lucy's integer")
        self.assertEqual(o.from_string_converter, int)
        self.assertEqual(o.value, '1')
        o.set_value()
        self.assertEqual(o.default, '1')
        self.assertEqual(o.value, 1)

        data = {
            'name': 'lucy',
            'default': '1',
            'doc': "lucy's integer",
            'from_string_converter': int,
        }
        o = Option(**data)
        self.assertEqual(o.name, 'lucy')
        self.assertEqual(o.default, '1')
        self.assertEqual(o.doc, "lucy's integer")
        self.assertEqual(o.from_string_converter, int)
        self.assertEqual(o.value, '1')

        data = {
            'default': '1',
            'doc': "lucy's integer",
            'from_string_converter': int,
        }
        o = Option('now', **data)
        self.assertEqual(o.name, 'now')
        self.assertEqual(o.default, '1')
        self.assertEqual(o.doc, "lucy's integer")
        self.assertEqual(o.from_string_converter, int)
        self.assertEqual(o.value, '1')

        d = datetime.datetime.now()
        o = Option('now', default=d)
        self.assertEqual(o.name, 'now')
        self.assertEqual(o.default, d)
        self.assertEqual(o.doc, None)
        self.assertEqual(o.from_string_converter, datetime_from_ISO_string)
        self.assertEqual(o.value, d)

        data = {
            'default': '1.0',
            'doc': "lucy's height",
            'from_string_converter': float,
        }
        o = Option('now', **data)
        self.assertEqual(o.name, 'now')
        self.assertEqual(o.default, '1.0')
        self.assertEqual(o.doc, "lucy's height")
        self.assertEqual(o.from_string_converter, float)
        self.assertEqual(o.value, '1.0')
        o.set_value()
        self.assertEqual(o.default, '1.0')
        self.assertEqual(o.value, 1.0)
Beispiel #20
0
    def test_boolean_converter_inOption(self):
        opt = Option('name', default=False)
        self.assertEqual(opt.default, False)
        self.assertEqual(opt.from_string_converter,
                         conv.boolean_converter)

        opt.set_value('true')
        self.assertEqual(opt.value, True)

        opt.set_value('false')
        self.assertEqual(opt.value, False)

        opt.set_value('1')
        self.assertEqual(opt.value, True)

        opt.set_value('t')
        self.assertEqual(opt.value, True)

        opt.set_value(True)
        self.assertEqual(opt.value, True)

        opt.set_value(False)
        self.assertEqual(opt.value, False)

        opt.set_value('False')
        self.assertEqual(opt.value, False)

        opt.set_value('True')
        self.assertEqual(opt.value, True)

        opt.set_value('None')
        self.assertEqual(opt.value, False)

        opt.set_value('YES')
        self.assertEqual(opt.value, True)

        opt.set_value(u'1')
        self.assertEqual(opt.value, True)

        opt.set_value(u'y')
        self.assertEqual(opt.value, True)

        opt.set_value(u't')
        self.assertEqual(opt.value, True)