config.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. '''
  2. /*#############################################################################
  3. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. ############################################################################ */
  14. '''
  15. import ConfigParser
  16. import json
  17. try:
  18. from cStringIO import StringIO
  19. except:
  20. from StringIO import StringIO
  21. from collections import deque, namedtuple
  22. from ..common.dict import _dict
  23. class ConfigGenerator:
  24. def __init__(self):
  25. pass
  26. def parseConfig(self, config, section='Default'):
  27. self.section = section
  28. self.conf = ConfigParser.ConfigParser()
  29. s = StringIO(config)
  30. self.conf.readfp(s)
  31. def sections(self):
  32. return self.conf.sections()
  33. def get(self, item, section=None):
  34. if not section:
  35. section = self.section
  36. return self.conf.get(section, item)
  37. class Config:
  38. def __init__(self, file):
  39. self.fileName = file
  40. self.configObj = self.loadConfig(self.fileName)
  41. def loadConfig(self, file):
  42. try:
  43. fp = open(file)
  44. js = json.load(fp)
  45. rC = namedtuple("Regress", js.keys())
  46. return _dict(getattr(rC(**js), "Regress"))
  47. except IOError as e:
  48. print(e)
  49. #implement writing out of a config.
  50. def writeConfig(self, file):
  51. pass