Created
March 26, 2017 05:01
-
-
Save ravisantoshgudimetla/8fe92f88f433ef2ceaee7100c5ea6b70 to your computer and use it in GitHub Desktop.
Sample for creating a dummy object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ConfigParser | |
import os | |
import ims.common.constants as constants | |
import ims.exception.config_exceptions as config_exceptions | |
from ims.common.bmi_config import parse_config | |
__config = None | |
def load(): | |
global __config | |
if __config is None: | |
try: | |
path = os.environ[ | |
constants.CONFIG_LOCATION_ENV_VARIABLE] | |
except KeyError: | |
path = constants.CONFIG_DEFAULT_LOCATION | |
__config = __BMIConfig(path) | |
__config.load_config() | |
parse_config(__config) | |
def get(): | |
global __config | |
return __config | |
class __BMIConfig: | |
# other instance variables are initialized as to not get AttributeErrors | |
def __init__(self, filename): | |
self.configfile = filename | |
self.config = None | |
def load_config(self): | |
config = ConfigParser.SafeConfigParser() | |
if not config.read(self.configfile): | |
raise IOError('cannot load ' + self.configfile) | |
self.config = config | |
def option(self, section, option, type=str, required=True): | |
try: | |
value = self.config.get(section, option) | |
section_obj = getattr(self, section, None) | |
if section_obj is None: | |
section_obj = object() | |
setattr(self, section, section_obj) | |
if type == bool: | |
setattr(section_obj, option, value.lower() == 'true') | |
else: | |
setattr(section_obj, option, type(value)) | |
except ConfigParser.Error: | |
if required: | |
raise config_exceptions.MissingOptionInConfigException(option, | |
section) | |
def section(self, section_name, required=True): | |
try: | |
section = self.config.items(section_name) | |
section_obj = getattr(self, section_name, None) | |
if section_obj is None: | |
section_obj = object() | |
setattr(self, section_name, section_obj) | |
for name, value in section: | |
setattr(section_obj, name, value) | |
except ConfigParser.Error: | |
if required: | |
raise config_exceptions.MissingSectionInConfigException( | |
section_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment