~hudson-openstack/burrow/cactus

« back to all changes in this revision

Viewing changes to burrow/client.py

  • Committer: Tarmac
  • Author(s): Eric Day
  • Date: 2011-04-20 07:32:27 UTC
  • mfrom: (7.1.3 dev)
  • Revision ID: tarmac-20110420073227-jv4kmex73efsf7b1
Pylint cleanup, reorganized code, added interactive shell.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
 
15
15
'''Client module for burrow.'''
16
16
 
17
 
import ConfigParser
18
 
import logging
19
 
import logging.config
20
 
 
21
 
import burrow
 
17
import burrow.common
22
18
import burrow.config
23
19
 
24
20
# Default configuration values for this module.
28
24
class Client(object):
29
25
    '''Client class for burrow.'''
30
26
 
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()
47
 
 
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:
52
 
            return
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()
58
40
 
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)