Break sitecore.setup_logger() out to sitecore.get_logger().
authorW. Trevor King <wking@drexel.edu>
Sat, 26 Jun 2010 02:04:28 +0000 (22:04 -0400)
committerW. Trevor King <wking@drexel.edu>
Sat, 26 Jun 2010 02:04:28 +0000 (22:04 -0400)
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.

sitecore/__init__.py

index e6e0ac1827f10aa844d2892a0197d5be80b21457..72829f8d96c3be8fea7594dd17ed97a0451d5e69 100755 (executable)
@@ -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')