From: W. Trevor King Date: Sat, 26 Jun 2010 02:04:28 +0000 (-0400) Subject: Break sitecore.setup_logger() out to sitecore.get_logger(). X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=115504972de3f620f0b4fd1d5356df33f49e1731;p=sitecorepy.git Break sitecore.setup_logger() out to sitecore.get_logger(). This lets us use the same logger in other submodules. Also: make selenium import optional in sitecore/__init__.py, so we can run related but non-SiteCore submodules. --- diff --git a/sitecore/__init__.py b/sitecore/__init__.py index e6e0ac1..72829f8 100755 --- a/sitecore/__init__.py +++ b/sitecore/__init__.py @@ -32,38 +32,44 @@ import getpass import logging import time -from selenium.firefox.webdriver import WebDriver +try: + from selenium.firefox.webdriver import WebDriver +except ImportError, e: + WebDriver = e # allow some functionality without selenium + +def get_logger(verbose=0) + verbosities = [ # in order of decreasing severity + logging.CRITICAL, + logging.ERROR, + logging.WARN, + logging.INFO, + logging.DEBUG, + ] + logger = logging.getLogger('sitecore') + if verbose >= len(verbosities): + verbose = -1 + logger.setLevel(verbosities[verbose]) + # create console handler and set the same level + ch = logging.StreamHandler() + ch.setLevel(verbosities[verbose]) + # create formatter + formatter = logging.Formatter( + '%(asctime)s - %(name)s - %(levelname)s - %(message)s') + # add formatter to ch + ch.setFormatter(formatter) + # add ch to logger + logger.addHandler(ch) + return logger class SiteCoreConnection (object): - def __init__(self, url, verbose): + def __init__(self, url, verbose=0): + if isinstance(WebDriver, ImportError): + raise WebDriver # *now* require sitecore self.url = url - self.setup_logger(verbose) + self.logger = get_logger(verbose) self.wait_time = 5 - def setup_logger(self, verbose): - verbosities = [ # in order of decreasing severity - logging.CRITICAL, - logging.ERROR, - logging.WARN, - logging.INFO, - logging.DEBUG, - ] - self.logger = logging.getLogger('sitecore') - if verbose >= len(verbosities): - verbose = -1 - self.logger.setLevel(verbosities[verbose]) - # create console handler and set the same level - ch = logging.StreamHandler() - ch.setLevel(verbosities[verbose]) - # create formatter - formatter = logging.Formatter( - '%(asctime)s - %(name)s - %(levelname)s - %(message)s') - # add formatter to ch - ch.setFormatter(formatter) - # add ch to logger - self.logger.addHandler(ch) - def start(self): 'Start a single Firefox instance' self.logger.info('starting Firefox')