Exemplo n.º 1
0
    def test_flatten_lowercase_keys_dict(self):
        input_dict = {"x": 1, "y": 2}
        result_dict = Parser.flatten_lowercase_keys_dict([{
            'x': 2
        }, input_dict])
        self.assertEqual(input_dict, result_dict)
        input_dict = {"X": 1, "y": 2}
        result_dict = Parser.flatten_lowercase_keys_dict([{
            'x': 2
        }, input_dict])
        self.assertEqual({'x': 1, 'y': 2}, result_dict)

        input_dict = {"X": 1, "y": 2}
        result_dict = Parser.flatten_lowercase_keys_dict(input_dict)
        self.assertEqual({'x': 1, 'y': 2}, result_dict)

        input_dict = 22  # unexpected
        result_dict = Parser.flatten_lowercase_keys_dict(input_dict)
        self.assertEqual(22, result_dict)
Exemplo n.º 2
0
    def parse_content(node):
        """ Parse content from input node and returns ContentHandler object
        it'll look like:

            - template:
                - file:
                    - temple: path

            or something

        """

        # Tread carefully, this one is a bit narly because of nesting
        output = ContentHandler()
        is_template_path = False
        is_template_content = False
        is_file = False
        is_done = False

        if not isinstance(node, (str, dict, list)):
            raise TypeError(
                "Content must be a string, dictionary, or list of dictionaries"
            )

        while node and not is_done:  # Dive through the configuration tree
            # Finally we've found the value!
            if isinstance(node, str):
                output.content = node
                output.setup(node,
                             is_file=is_file,
                             is_template_path=is_template_path,
                             is_template_content=is_template_content)
                return output

            is_done = True

            # Dictionary or list of dictionaries
            flat_dict = Parser.flatten_lowercase_keys_dict(node)
            for key, value in flat_dict.items():
                if key == 'template':
                    if isinstance(value, str):
                        if is_file:
                            value = os.path.abspath(value)
                        output.content = value
                        is_template_content = is_template_content or not is_file
                        output.is_template_content = is_template_content
                        output.is_template_path = is_file
                        output.is_file = is_file
                        return output
                    else:
                        is_template_content = True
                        node = value
                        is_done = False
                        break

                elif key == 'file':
                    if isinstance(value, str):
                        output.content = os.path.abspath(value)
                        output.is_file = True
                        output.is_template_content = is_template_content
                        return output
                    else:
                        is_file = True
                        node = value
                        is_done = False
                        break

        raise Exception("Invalid configuration for content.")