Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. import ConfigParser
  2.  
  3. class Section(object):
  4. def __init__(self,conf,section_name):
  5. self.conf = conf
  6. self.section = section_name
  7.  
  8. def __getattr__(self,attr):
  9. print "getting the %s of the %s" % (attr,self.section)
  10. return self.conf.get(self.section,attr)
  11.  
  12. class Configuration(object):
  13. def __init__(self,config_file):
  14. self.config_file = config_file
  15. self.parser = ConfigParser.ConfigParser()
  16. self.parser.read(self.config_file)
  17.  
  18. def __getattr__(self,attr):
  19. print "getting the %s section" % (attr)
  20. return Section(self,attr)
  21.  
  22.  
  23. class App(object):
  24. def __init__(self):
  25. self.config = Configuration("/etc/infomaniak.conf")
  26.  
  27. def start(self):
  28. bottle.run(host='',port=self.config.port)
  29.  
  30. conf = app.Configuration("../../etc/server.conf")
  31. print conf.SERVER.PORT
  32.  
  33. # ychaouche@ychaouche-PC ~/REPOS/INFOMANIAK-CLASSES/infomaniak/tests $ python test_config.py
  34. # getting the SERVER section
  35. # getting the PORT of the SERVER
  36. # getting the get section
  37. # Traceback (most recent call last):
  38. # File "test_config.py", line 5, in <module>
  39. # print conf.SERVER.PORT
  40. # File "../app.py", line 10, in __getattr__
  41. # return self.conf.get(self.section,attr)
  42. # TypeError: 'Section' object is not callable
  43. # ychaouche@ychaouche-PC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement