Example #1
0
    def parse(self, content, root=None, path_info=None):
        if not root:
            root = block.Root()
        try:
            parsed = self.parser.parse(content)
        except ParseException as e:
            error_msg = 'char {char} (line:{line}, col:{col})'.format(
                char=e.loc, line=e.lineno, col=e.col)
            if path_info:
                LOG.error('Failed to parse config "{file}": {error}'.format(
                    file=path_info, error=error_msg))
            else:
                LOG.error(
                    'Failed to parse config: {error}'.format(error=error_msg))
            raise InvalidConfiguration(error_msg)

        if len(parsed) and parsed[0].getName() == 'file_delimiter':
            #  Were parse nginx dump
            LOG.info('Switched to parse nginx configuration dump.')
            root_filename = self._prepare_dump(parsed)
            self.is_dump = True
            self.cwd = os.path.dirname(root_filename)
            parsed = self.configs[root_filename]

        self.parse_block(parsed, root)
        return root
Example #2
0
    def parse(self, file_path, root=None):
        LOG.debug("Parse file: {}".format(file_path))

        if not root:
            root = block.Root()

        try:
            parser = raw_parser.RawParser()
            parsed = parser.parse(file_path)
        except ParseException as e:
            LOG.error('Failed to parse config "{file}": {error}'.format(
                file=file_path, error=str(e)))
            return root

        if len(parsed) and parsed[0].getName() == 'file_delimiter':
            #  Were parse nginx dump
            LOG.info('Switched to parse nginx configuration dump.')
            root_filename = self._prepare_dump(parsed)
            self.is_dump = True
            self.cwd = os.path.dirname(root_filename)
            parsed = self.configs[root_filename]

        self.parse_block(parsed, root)
        return root