28
24
class Client(object):
29
25
'''Client class for burrow.'''
31
def __init__(self, url=None, config_files=[],
27
def __init__(self, url=None, config_files=None,
32
28
add_default_log_handler=True):
33
29
'''Initialize a client using the URL and config files from the
34
30
given list. This is passed directly to ConfigParser.read(),
35
31
so files should be in ConfigParser format. This will load
36
32
all the backend class from the configuration.'''
37
if len(config_files) > 0:
38
logging.config.fileConfig(config_files)
39
self._config = ConfigParser.ConfigParser()
40
self._config.read(config_files)
33
self._config = burrow.config.load_config_files(config_files)
41
34
# TODO: Parse URL if given and overwrite any values in self._config.
42
35
self.config = burrow.config.Config(self._config, 'burrow.client')
43
self.log = burrow.get_logger(self.config)
44
if add_default_log_handler:
45
self._add_default_log_handler()
46
self._import_backend()
48
def _add_default_log_handler(self):
49
'''Add a default log handler it one has not been set.'''
50
root_log = logging.getLogger()
51
if len(root_log.handlers) > 0 or len(self.log.handlers) > 0:
53
handler = logging.StreamHandler()
54
handler.setLevel(logging.ERROR)
55
log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
56
handler.setFormatter(logging.Formatter(log_format))
57
root_log.addHandler(handler)
36
self.log = burrow.common.get_logger(self.config)
37
if len(self.log.handlers) == 0 and add_default_log_handler:
38
burrow.common.add_default_log_handler()
39
self.backend = self._import_backend()
59
41
def _import_backend(self):
60
42
'''Load backend given in the 'backend' option.'''
61
43
backend = self.config.get('backend', DEFAULT_BACKEND)
62
44
config = (self._config, backend)
63
self.backend = burrow.import_class(backend, 'Backend')(config)
45
return burrow.common.import_class(backend, 'Backend')(config)