Пример #1
0
    def __get_prefs(self):
        '''
        Processes each file in self.prefs_list
        Returns-->OrderedDict
        '''
        ret = OrderedDict()
        for f in self.prefs_list:
            try:
                parser = CfgParser(f=f, undef=False)
                if not parser.contents:
                    continue
                d = parser.prefs_dict
                ntd = parser.ntd
                for (k, v) in d.items():
                    if k in ret and ret[k] != v:
                        msg = 'Redefinition of %s in %s old(%s) new(%s)' % (
                            k,
                            f,
                            str(ret[k]),
                            str(v),
                        )
                        debug(msg)
                        if k in ntd:
                            msg = '%s previously defined in %s (%d)' % (
                                k, ntd[k].f, ntd[k].num)
                            debug(msg)
                    ret[k] = v
            except Exception as e:
                debug(format_exc(e, msg='CfgParser(%s)' % (f, )))
                continue

        return ret
Пример #2
0
 def __contents(self):
     '''Returns-->str'''
     try:
         (contents, _) = file_contents(self.f, debug=DEBUG_SINGLETON)
         if not contents.endswith('\n'):
             contents += '\n'
         return contents
     except Exception as e:
         debug(format_exc(e))
         return ''
Пример #3
0
    def update(self, d, silent=False):
        '''
        d-->OrderedDict
        silent-->bool: If True, does not produce any output
        (Over)writes cfg updating / adding key-value pairs from d
        Returns-->bool: success
        '''
        try:
            to_change = self.compare(d=d)
            out_lines = []
            unchanged = dict.fromkeys([LINE_EMPTY, LINE_COMMENT])
            if not to_change:
                if not silent:
                    print('All your config prefs are already set')
                return True

            if not silent:
                print('Setting following kernel prefs:')
            if self.prefs_dict:
                for nt in self.ntl:
                    if nt.lt == LINE_INVALID:
                        continue  # ignore invalid lines
                    if nt.lt in unchanged:
                        out_lines.append(nt.raw)
                        continue
                    # LINE_SET_KV or LINE_UNDEF_COMMENT
                    (k, v) = (nt.k, nt.v)
                    if k in to_change:
                        v = to_change[k][0]
                        if not silent:
                            print('    %s=%s' % (k, v))
                        del to_change[k]
                    out_lines.append('%s%s=%s' % (_CONFIG, k, v))

                # Now write what remains in to_change (completely missing in cfg)
                for (k, (v1, v2)) in to_change.items():
                    if not silent:
                        print('    %s=%s' % (k, v1))
                    out_lines.append('%s%s=%s' % (_CONFIG, k, v1))

            with open(self.config, mode='w', encoding=self.encoding) as f:
                for l in out_lines:
                    f.write(l + '\n')
                f.flush()
                f.close()
                self.init()  # re-read
            return True
        except Exception as e:
            print(format_exc(e))
            return False
Пример #4
0
    def init(self):
        '''
        Sets instance vars
        '''
        self.config = os.path.join(BUILD_DIR, '.config')
        f = os.path.join(BUILD_DIR, '.config')
        try:
            parser = CfgParser(f=f, undef=True)
        except Exception as e:
            debug(format_exc(e, msg='CfgParser(%s)' % (f, )))
            raise

        self.contents = parser.contents
        self.lines = parser.lines
        self.ntl = parser.ntl
        self.prefs_dict = parser.prefs_dict