Esempio n. 1
0
    def test_returns_false_if_items_not_match(self):
        file_path_1 = self.tempdir.write(fake.config_file(), b'')
        file_path_2 = self.tempdir.write(fake.config_file(), b'')
        file_ = open(file_path_1, 'w+')

        config = ConfigReader(filename=file_path_2, file_object=file_)
        config.remove_section('main')
        config.set('start', 'True', section='defaults')
        config.set('value', '45', section='defaults')
        items = config.get_items('main')

        with self.subTest(0):
            self.assertIsNone(items)

        items = config.get_items('defaults')

        with self.subTest(1):
            self.assertIsInstance(items, OrderedDict)

        with self.subTest(2):
            expected = OrderedDict([('start', True), ('value', 45)])
            self.assertEqual(items, expected)
        config.close()

        with self.subTest('Test returns false if file object still writable'):
            self.assertRaises(ValueError, file_.write, '')
Esempio n. 2
0
    def test_returns_false_if_load_fails_windows(self):
        file_path = self.tempdir.write(fake.config_file(), b'')
        config = ConfigReader(file_path, case_sensitive=True)
        config.set('states', '35', section='country')
        config.set('counties', 'None', section='country')

        environment = os.environ.copy()
        user = '******'
        environment['USER'] = user
        environment['COUNTER'] = 'never'

        config.to_env(environment)
        config.load_env(environment)

        items = config.get_items('main')
        with self.subTest(0):
            self.assertEqual(items['APPDATA'], os.environ['APPDATA'])

        with self.subTest(1):
            self.assertEqual(items['USER'], user)

        with self.subTest(2):
            self.assertEqual(items['ALLUSERSPROFILE'],
                             os.environ['ALLUSERSPROFILE'])

        with self.subTest(3):
            self.assertRaises(KeyError, lambda: items['home'])

        with self.subTest(4):
            self.assertEqual(items['COUNTER'], 'never')
Esempio n. 3
0
    def test_returns_false_if_set_many_fails(self):
        file_path = self._get_config_file()
        config = ConfigReader(file_path)
        config.set_many({'name': 'one', 'number': '2'})

        with self.subTest('Test returns false if data not in default section'):
            self.assertListEqual(
                [config.get('name'), config.get('number')], ['one', 2])

        config.reload()
        with self.subTest(), self.assertRaises(MissingOptionError):
            config.get('number')

        data = OrderedDict([('people', 'true'), ('count', 30)])
        config.set_many(data, section='demo')
        with self.subTest(
                'Test returns false if data not in specified section'):
            self.assertDictEqual(config.get_items('demo'), data)

        config.close(save=True)
        with ConfigReader(file_path) as d, self.subTest():
            self.assertEqual(d.get('people', section='demo'), 'true')
Esempio n. 4
0
class Sample:
    def __init__(self, file_, retain_case):
        self.retain_case = retain_case
        self.config = ConfigReader(file_, case_sensitive=self.retain_case)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()

    def create(self, filename=None):
        """Create a sample file

        :param str filename: The name of the sample.
            Default to the name of the original file suffixed with `.sample`
        """
        if filename is None:
            filename = f'{self.config.filename}.sample'

        try:
            os.remove(filename)
        except FileNotFoundError:
            pass

        with ConfigReader(filename, case_sensitive=self.retain_case) as sample:
            for section in self.config.sections:
                items = self.config.get_items(section)
                for k, v in items.items():
                    value = get_type(v)
                    sample.set(k, value, section=section)

            sample.remove_section('main')
            sample.save()

    def close(self):
        """Close the original file handler"""
        self.config.close()