Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 4.35 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # -*- coding: utf-8 -*-
  2.  
  3. from pprint import pprint
  4. from os.path import exists
  5. from os.path import join
  6.  
  7. ########################################################################
  8. class ConfigParser:
  9.     """
  10.     holds and manage the configuration
  11.    
  12.     current dict layout:
  13.    
  14.     {
  15.    
  16.     section : {
  17.      subsection : {
  18.       option : {
  19.             value:
  20.             type:
  21.             desc:
  22.       }
  23.       desc:
  24.    
  25.     }
  26.     desc:
  27.    
  28.     }
  29.    
  30.    
  31.     """
  32.  
  33.     #----------------------------------------------------------------------
  34.     def __init__(self):
  35.         """Constructor"""
  36.         self.config = {} # the config values
  37.        
  38.        
  39.         if self.needUpdate():
  40.             self.copyDefault()
  41.        
  42.         self.readConfig()
  43.        
  44.     #----------------------------------------------------------------------
  45.     def copyDefault(self):
  46.         """restore default configuration"""
  47.         raise NotImplementedError
  48.    
  49.     #----------------------------------------------------------------------
  50.     def needUpdate(self):
  51.         """determines if config need to be copied"""
  52.        
  53.         if not exists("pyload.config"):
  54.             return False
  55.        
  56.         #@TODO: testing conf file version
  57.        
  58.     #----------------------------------------------------------------------
  59.     def readConfig(self):
  60.         """reads the config file"""
  61.        
  62.         self.config = self.parseConfig(join(pypath, "config", "default.config"))
  63.        
  64.         try:
  65.  
  66.             homeconf = self.parseConfig("pyload.config")
  67.             self.updateValues(homeconf)
  68.            
  69.         except:
  70.             logger.ERROR("Error parsing config file")
  71.        
  72.        
  73.     #----------------------------------------------------------------------
  74.     def parseConfig(self, file):
  75.         """parses a given file"""
  76.         raise NotImplementedError    
  77.    
  78.    
  79.     #----------------------------------------------------------------------
  80.     def updateValues(self):
  81.         """sets the config values from a parsed config file"""
  82.         raise NotImplementedError
  83.        
  84.    
  85.     #----------------------------------------------------------------------
  86.     def __getitem__(self, section):
  87.         """provides dictonary like behaviour"""
  88.         return self.config[section]      
  89.    
  90.     #----------------------------------------------------------------------
  91.     def get(self):
  92.         """get value"""
  93.         raise NotImplementedError
  94.        
  95.     #----------------------------------------------------------------------
  96.     def set(self):
  97.         """set value"""
  98.         raise NotImplementedError
  99.        
  100.    
  101.  
  102.  
  103. f = open("config.txt")
  104.  
  105. config = f.read()
  106.  
  107. config = config.split("\n")
  108.  
  109. conf = {}
  110.  
  111. section, option, value, typ, desc, add = "","","","","",""
  112.  
  113. pprint(config)
  114.  
  115. for line in config:
  116.  
  117.     dp = ":" in line # determines if : in line
  118.     line = line.strip()
  119.  
  120.     if line == "" or line.startswith(";"):
  121.         continue
  122.     elif line.startswith("##"):
  123.         section = line.replace("##","").strip()
  124.         section = section.split("-")
  125.         conf[section[1].strip()] = { "desc" :  section[0].strip() }
  126.         section = section[1].strip()
  127.     elif line.startswith("."):
  128.         typ, desc = line[1:].split(" ")
  129.     elif line.endswith(","):
  130.  
  131.         if dp:
  132.             option, value = line.split(":")
  133.             option, value = option.strip(), value.strip()
  134.  
  135.             value = [value[:-1]]
  136.  
  137.         else:
  138.             value.append(line[:-1])
  139.  
  140.     else:
  141.         if dp:
  142.             option, value = line.split(":")
  143.             option, value = option.strip(), value.strip()
  144.  
  145.             conf[section][option] = {
  146.                 "value" : value,
  147.                 "typ" : typ,
  148.                 "desc" : desc,
  149.                 "add" : add
  150.             }
  151.  
  152.         else:
  153.             try:
  154.                 value.append(line)
  155.  
  156.                 conf[section][option] = {
  157.                     "value" : value,
  158.                     "typ" : typ,
  159.                     "desc" : desc,
  160.                     "add" : add
  161.                 }
  162.             except:
  163.                 conf[section][option] = {
  164.                     "value" : value,
  165.                     "typ" : typ,
  166.                     "desc" : desc,
  167.                     "add" : add
  168.                 }
  169.                 #invalid line