bwConfig.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/python3
  2. # bwConfig.py by Bill Weinman <http://bw.org/contact/>
  3. # Copyright (c) 2010 The BearHeart Group, LLC
  4. #
  5. __version__ = '1.0.0'
  6. class configFile:
  7. ''' simple config file support '''
  8. _recs = {}
  9. def __init__(self, fn):
  10. self._fh = open(fn, 'rt')
  11. self.parse(self._fh)
  12. def parseline(self, line):
  13. if line[0] == '#': return
  14. if '#' in line: line = line.split('#', 2)[0]
  15. if '=' not in line: return
  16. ( lhs, rhs ) = line.split('=', 2)
  17. self._recs[lhs.strip()] = rhs.strip()
  18. def parse(self, fh):
  19. for line in fh.readlines():
  20. self.parseline(line)
  21. def recs(self):
  22. return self._recs
  23. def test():
  24. import sys
  25. fn = sys.argv[1] if len(sys.argv) > 1 else 'test.conf'
  26. try:
  27. conf = configFile(fn)
  28. except IOError as e:
  29. print('could not open {},'.format(fn), e)
  30. else:
  31. recs = conf.recs()
  32. for k in sorted(recs):
  33. print('{} is [{}]'.format(k, recs[k]))
  34. if __name__ == "__main__": test()