- # -*- coding: utf-8 -*-
- from pprint import pprint
- from os.path import exists
- from os.path import join
- ########################################################################
- class ConfigParser:
- """
- holds and manage the configuration
- current dict layout:
- {
- section : {
- subsection : {
- option : {
- value:
- type:
- desc:
- }
- desc:
- }
- desc:
- }
- """
- #----------------------------------------------------------------------
- def __init__(self):
- """Constructor"""
- self.config = {} # the config values
- if self.needUpdate():
- self.copyDefault()
- self.readConfig()
- #----------------------------------------------------------------------
- def copyDefault(self):
- """restore default configuration"""
- raise NotImplementedError
- #----------------------------------------------------------------------
- def needUpdate(self):
- """determines if config need to be copied"""
- if not exists("pyload.config"):
- return False
- #@TODO: testing conf file version
- #----------------------------------------------------------------------
- def readConfig(self):
- """reads the config file"""
- self.config = self.parseConfig(join(pypath, "config", "default.config"))
- try:
- homeconf = self.parseConfig("pyload.config")
- self.updateValues(homeconf)
- except:
- logger.ERROR("Error parsing config file")
- #----------------------------------------------------------------------
- def parseConfig(self, file):
- """parses a given file"""
- raise NotImplementedError
- #----------------------------------------------------------------------
- def updateValues(self):
- """sets the config values from a parsed config file"""
- raise NotImplementedError
- #----------------------------------------------------------------------
- def __getitem__(self, section):
- """provides dictonary like behaviour"""
- return self.config[section]
- #----------------------------------------------------------------------
- def get(self):
- """get value"""
- raise NotImplementedError
- #----------------------------------------------------------------------
- def set(self):
- """set value"""
- raise NotImplementedError
- f = open("config.txt")
- config = f.read()
- config = config.split("\n")
- conf = {}
- section, option, value, typ, desc, add = "","","","","",""
- pprint(config)
- for line in config:
- dp = ":" in line # determines if : in line
- line = line.strip()
- if line == "" or line.startswith(";"):
- continue
- elif line.startswith("##"):
- section = line.replace("##","").strip()
- section = section.split("-")
- conf[section[1].strip()] = { "desc" : section[0].strip() }
- section = section[1].strip()
- elif line.startswith("."):
- typ, desc = line[1:].split(" ")
- elif line.endswith(","):
- if dp:
- option, value = line.split(":")
- option, value = option.strip(), value.strip()
- value = [value[:-1]]
- else:
- value.append(line[:-1])
- else:
- if dp:
- option, value = line.split(":")
- option, value = option.strip(), value.strip()
- conf[section][option] = {
- "value" : value,
- "typ" : typ,
- "desc" : desc,
- "add" : add
- }
- else:
- try:
- value.append(line)
- conf[section][option] = {
- "value" : value,
- "typ" : typ,
- "desc" : desc,
- "add" : add
- }
- except:
- conf[section][option] = {
- "value" : value,
- "typ" : typ,
- "desc" : desc,
- "add" : add
- }
- #invalid line