示例#1
0
    def load(self, fname, convert=None, recurse_key='', incpath='.', **kw):
        """Load a configuration file, return the resulting Struct.

        Call: load_config(fname,convert=None,conflict=None,recurse_key='')

         - fname: file to load from.
         - convert: dictionary of type conversions (see read_dict())
         - recurse_key: keyword in dictionary to trigger recursive file inclusions.
         """

        if self.recdepth > self.reclimit:
            raise ConfigLoaderError, 'maximum recursive inclusion of rcfiles '+\
                  'exceeded: ' + `self.recdepth` + \
                  '.\nMaybe you have a circular chain of inclusions?'
        self.recdepth += 1
        fname = filefind(fname, incpath)
        data = Struct()
        # avoid including the same file more than once
        if fname in self.included:
            return data
        Xinfo = ultraTB.AutoFormattedTB()
        if convert == None and recurse_key: convert = {qwflat: recurse_key}
        # for production, change warn to 0:
        data.merge(
            read_dict(fname,
                      convert,
                      fs=self.field_sep,
                      strip=1,
                      warn=0,
                      no_empty=1,
                      **kw))
        # keep track of successfully loaded files
        self.included.append(fname)
        if recurse_key in data.keys():
            for incfilename in data[recurse_key]:
                found = 0
                try:
                    incfile = filefind(incfilename, incpath)
                except IOError:
                    if os.name in ['nt', 'dos']:
                        try:
                            # Try again with '.ini' extension
                            incfilename += '.ini'
                            incfile = filefind(incfilename, incpath)
                        except IOError:
                            found = 0
                        else:
                            found = 1
                    else:
                        found = 0
                else:
                    found = 1
                if found:
                    try:
                        data.merge(
                            self.load(incfile, convert, recurse_key, incpath,
                                      **kw), self.conflict)
                    except:
                        Xinfo()
                        warn('Problem loading included file: ' +
                             ` incfilename ` + '. Ignoring it...')
                else:
                    warn('File `%s` not found. Included by %s' %
                         (incfilename, fname))

        return data