示例#1
0
    def load(self, filepath, initialize=True):
        if initialize:
            self._stack = []
            self._includes = set()

        try:
            pre_open = self._options['plug']['pre_open']

            filename, basedir = os.path.basename(filepath), os.path.dirname(
                filepath)

            process, filename, basedir = pre_open(filename, basedir)

            filepath = os.path.join(basedir, filename) if basedir else filename

            if not process:
                return {}

        except KeyError:
            pass

        if filepath in self._includes and not self._options.get(
                'includeagain'):
            return {}

        self._includes.add(filepath)

        try:
            with self._reader.open(filepath) as f:
                return self.loads(f.read(), source=filepath)

        except IOError as ex:
            raise error.ConfigFileReadError('File %s can\'t be open: %s' %
                                            (filepath, ex))
示例#2
0
    def g_include(self, ast):
        filepath = ast[0]

        options = self._options

        if os.path.isabs(filepath):
            configpath = [os.path.dirname(filepath)]
            filename = os.path.basename(filepath)

        else:
            configpath = options.get('configpath', [])

            if 'configroot' in options and options.get('includerelative'):
                configpath.insert(0, options['configroot'])

            if 'programpath' in options:
                configpath.append(options['programpath'])
            else:
                configpath.append('.')

            if self._reader.isdir(filepath):
                configpath.insert(0, filepath)
                filename = '.'
            else:
                filename = filepath

        for configdir in configpath:

            filepath = os.path.join(configdir, filename)

            if self._reader.isdir(filepath):
                if options.get('includedirectories'):
                    contents = {}

                    for include_file in sorted(self._reader.listdir(filepath)):
                        items = self.load(os.path.join(filepath, include_file),
                                          initialize=False)
                        self._merge_contents(contents, items)

                    return contents

            elif options.get('includeglob'):
                contents = {}

                for include_file in sorted(glob.glob(filepath)):
                    items = self.load(include_file, initialize=False)
                    self._merge_contents(contents, items)

                return contents

            elif self._reader.exists(filepath):
                return self.load(filepath, initialize=False)

        else:
            raise error.ConfigFileReadError(
                'Config file "%s" not found in search path %s' %
                (filename, ':'.join(configpath)))
示例#3
0
    def load(self, filepath, initialize=True):
        """Loads config file into a dictionary object.

        Args:
            filepath (Text): path of config file to load. Expects UTF-8
                encoding.

        Returns:
            dict containing configuration information loaded from file.
        """
        if initialize:
            self._stack = []
            self._includes = set()
            self._ast_cache = {}

        try:
            pre_open = self._options['plug']['pre_open']

            filename, basedir = os.path.basename(
                filepath), os.path.dirname(filepath)

            process, filename, basedir = pre_open(filename, basedir)

            filepath = os.path.join(
                basedir, filename) if basedir else filename

            if not process:
                return {}

        except KeyError:
            pass

        if filepath in self._includes and not self._options.get(
                'includeagain'):
            return {}

        self._includes.add(filepath)

        if filepath in self._ast_cache:
            return self._ast_cache[filepath]

        try:
            with self._reader.open(filepath) as f:
                return self.loads(f.read(), source=filepath)

        except IOError as ex:
            raise error.ConfigFileReadError('File %s can\'t be open: %s'
                                            % (filepath, ex))

        finally:
            if initialize:
                self._ast_cache = {}