psa: Full width articles
[feedingit] / psa_harmattan / feedingit / deb_dist / feedingit-0.1.0 / pysrc / wc.py
index c28d1d7..d9a2efd 100644 (file)
@@ -32,7 +32,7 @@ except ImportError, exception:
         % traceback.format_exc ())
     woodchuck_imported = False
     class PyWoodchuck (object):
-        def available(self):
+        def available(self, *args, **kwargs):
             return False
     woodchuck = None
 
@@ -40,7 +40,7 @@ except ImportError, exception:
 refresh_interval = 6 * 60 * 60
 
 class mywoodchuck (PyWoodchuck):
-    def __init__(self, listing, human_readable_name, identifier,
+    def __init__(self, config, listing, human_readable_name, identifier,
                  request_feedback):
         try:
             PyWoodchuck.__init__ (self, human_readable_name, identifier,
@@ -52,9 +52,22 @@ class mywoodchuck (PyWoodchuck):
             self.available = self.not_available
             return
 
+        self.config = config
         self.listing = listing
 
-    def not_available(self):
+        try:
+            self.enabled = config.getWoodchuckEnabled()
+        except Exception:
+            logging.exception("Setting enabled")
+
+    def available(self, check_config=True):
+        if not PyWoodchuck.available(self):
+            return False
+        if check_config:
+            return self.config.getWoodchuckEnabled()
+        return True
+
+    def not_available(self, *args, **kwargs):
         return False
 
     # Woodchuck upcalls.
@@ -85,12 +98,12 @@ class mywoodchuck (PyWoodchuck):
                            str(e)))
 
 _w = None
-def wc_init(listing, request_feedback=False):
+def wc_init(config, listing, request_feedback=False):
     """Connect to the woodchuck server and initialize any state."""
     global _w
     assert _w is None
     
-    _w = mywoodchuck (listing, "FeedingIt", "org.marcoz.feedingit",
+    _w = mywoodchuck (config, listing, "FeedingIt", "org.marcoz.feedingit",
                       request_feedback)
 
     if not woodchuck_imported or not _w.available ():
@@ -98,6 +111,27 @@ def wc_init(listing, request_feedback=False):
     else:
         logger.debug("Woodchuck appears to be available.")
 
+def wc_disable_set(disable=True):
+    """Disable Woodchuck."""
+    if disable:
+        logger.info("Disabling Woodchuck")
+    else:
+        logger.info("Enabling Woodchuck")
+
+    global _w
+    if _w is None:
+        logging.info("Woodchuck not loaded.  Not doing anything.")
+        return
+
+    if not _w.available(check_config=False):
+        logging.info("Woodchuck not available.  Not doing anything.")
+        return
+
+    try:
+        _w.enabled = not disable
+    except Exception:
+        logger.exception("Disabling Woodchuck")
+
 def wc():
     """Return the Woodchuck singleton."""
     global _w