Beispiel #1
0
    def _to_dict(self, reader):
        data_dict = {}
        for row in reader:
            if row:
                key = row[0].strip()
                # Ignore comment lines.
                if not key.strip().startswith(self._comment_markers):
                    # NOTE(zhaochao): a list object is expected for
                    # trove_utils.unpack_singleton, however in python3
                    # map objects won't be treated as lists, so we
                    # convert the result of StringConverter.to_objects
                    # to a list explicitly.
                    items = list(self._string_converter.to_objects(
                        [v if v else None for v in
                         map(self._strip_comments, row[1:])]))
                    current = data_dict.get(key)
                    if current is not None:
                        current.append(trove_utils.unpack_singleton(items)
                                       if self._unpack_singletons else items)
                    else:
                        data_dict.update({key: [items]})

        if self._unpack_singletons:
            # Unpack singleton values.
            # NOTE(zhaochao): In Python 3.x, dict.items() returns a view
            # object, which will reflect the changes of the dict itself:
            # https://docs.python.org/3/library/stdtypes.html#dict-views
            # This means as we're changing the dict, dict.items() cannot
            # guarantee we're safely iterating all entries in the dict.
            # Manually converting the result of dict.items() to a list will
            # fix.
            for k, v in list(data_dict.items()):
                data_dict.update({k: trove_utils.unpack_singleton(v)})

        return data_dict
Beispiel #2
0
    def _to_dict(self, reader):
        data_dict = {}
        for row in reader:
            if row:
                key = row[0].strip()
                # Ignore comment lines.
                if not key.strip().startswith(self._comment_markers):
                    items = self._string_converter.to_objects([
                        v if v else None
                        for v in map(self._strip_comments, row[1:])
                    ])
                    current = data_dict.get(key)
                    if current is not None:
                        current.append(
                            trove_utils.unpack_singleton(items) if self.
                            _unpack_singletons else items)
                    else:
                        data_dict.update({key: [items]})

        if self._unpack_singletons:
            # Unpack singleton values.
            for k, v in data_dict.items():
                data_dict.update({k: trove_utils.unpack_singleton(v)})

        return data_dict
Beispiel #3
0
 def get_configuration_property(self, name, default=None):
     """Return the value of a Redis configuration property.
     Returns a single value for single-argument properties or
     a list otherwise.
     """
     return utils.unpack_singleton(
         self.configuration_manager.get_value(name, default))
Beispiel #4
0
 def get_configuration_property(self, name, default=None):
     """Return the value of a Redis configuration property.
     Returns a single value for single-argument properties or
     a list otherwise.
     """
     return utils.unpack_singleton(
         self.configuration_manager.get_value(name, default))
Beispiel #5
0
    def _to_dict(self, reader):
        data_dict = {}
        for row in reader:
            # Ignore comment lines.
            if row and not row[0].startswith(self._comment_markers):
                items = self._string_converter.to_objects([v if v else None for v in row[1:]])
                current = data_dict.get(row[0])
                if current is not None:
                    current.append(trove_utils.unpack_singleton(items) if self._unpack_singletons else items)
                else:
                    data_dict.update({row[0]: [items]})

        if self._unpack_singletons:
            # Unpack singleton values.
            for k, v in data_dict.items():
                data_dict.update({k: trove_utils.unpack_singleton(v)})

        return data_dict
Beispiel #6
0
 def test_unpack_singleton(self):
     self.assertEqual([1, 2, 3], utils.unpack_singleton([1, 2, 3]))
     self.assertEqual(0, utils.unpack_singleton([0]))
     self.assertEqual('test', utils.unpack_singleton('test'))
     self.assertEqual('test', utils.unpack_singleton(['test']))
     self.assertEqual([], utils.unpack_singleton([]))
     self.assertIsNone(utils.unpack_singleton(None))
     self.assertEqual([None, None], utils.unpack_singleton([None, None]))
     self.assertEqual('test', utils.unpack_singleton([['test']]))
     self.assertEqual([1, 2, 3], utils.unpack_singleton([[1, 2, 3]]))
     self.assertEqual(1, utils.unpack_singleton([[[1]]]))
     self.assertEqual([[1], [2]], utils.unpack_singleton([[1], [2]]))
     self.assertEqual(['a', 'b'], utils.unpack_singleton(['a', 'b']))
Beispiel #7
0
 def test_unpack_singleton(self):
     self.assertEqual([1, 2, 3], utils.unpack_singleton([1, 2, 3]))
     self.assertEqual(0, utils.unpack_singleton([0]))
     self.assertEqual('test', utils.unpack_singleton('test'))
     self.assertEqual('test', utils.unpack_singleton(['test']))
     self.assertEqual([], utils.unpack_singleton([]))
     self.assertEqual(None, utils.unpack_singleton(None))
     self.assertEqual([None, None], utils.unpack_singleton([None, None]))
     self.assertEqual('test', utils.unpack_singleton([['test']]))
     self.assertEqual([1, 2, 3], utils.unpack_singleton([[1, 2, 3]]))
     self.assertEqual(1, utils.unpack_singleton([[[1]]]))
     self.assertEqual([[1], [2]], utils.unpack_singleton([[1], [2]]))
     self.assertEqual(['a', 'b'], utils.unpack_singleton(['a', 'b']))