Skip to content
Snippets Groups Projects
config.py 975 B
Newer Older
  • Learn to ignore specific revisions
  • Benoit Favre's avatar
    Benoit Favre committed
    import sys, re
    
    def read(filename):
        config = {}
        with open(filename) as fp:
            for line in fp:
                line = line.strip()
                line = re.sub(r'#.*$', '', line) # remove comments
                found = re.match(r'^([a-zA-Z0-9-_]*)\s*=\s*(.*)$', line)
                if found:
                    name = found.group(1)
                    value = found.group(2).strip()
                    for other, replacement in config.items():
                        value = re.sub(r'\$%s\b' % other, str(replacement), value)
                    if value in ['True', 'False']:
                        value = bool(value)
                    else:
                        try:
                            value = int(value)
                        except:
                            try: 
                                value = float(value)
                            except:
                                pass
                    config[name] = value
        return config
    
    if __name__ == '__main__':
        print read(sys.argv[1])