Adding wip version for meego harmattan
[feedingit] / psa_harmattan / feedingit / deb_dist / feedingit-0.1.0 / debian / feedingit / usr / share / feedingit / wc.py
diff --git a/psa_harmattan/feedingit/deb_dist/feedingit-0.1.0/debian/feedingit/usr/share/feedingit/wc.py b/psa_harmattan/feedingit/deb_dist/feedingit-0.1.0/debian/feedingit/usr/share/feedingit/wc.py
new file mode 100644 (file)
index 0000000..c8fd987
--- /dev/null
@@ -0,0 +1,97 @@
+# Copyright (c) 2011 Neal H. Walfield
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import logging
+logger = logging.getLogger(__name__)
+import traceback
+
+# Don't fail if the Woodchuck modules are not available.  Just disable
+# Woodchuck's functionality.
+
+# Whether we imported the woodchuck modules successfully.
+woodchuck_imported = True
+try:
+    import pywoodchuck
+    from pywoodchuck import PyWoodchuck
+    from pywoodchuck import woodchuck
+except ImportError, exception:
+    logger.info(
+        "Unable to load Woodchuck modules: disabling Woodchuck support: %s"
+        % traceback.format_exc ())
+    woodchuck_imported = False
+    class PyWoodchuck (object):
+        def available(self):
+            return False
+    woodchuck = None
+
+# The default channel refresh interval: 6 hours.
+refresh_interval = 6 * 60 * 60
+
+class mywoodchuck (PyWoodchuck):
+    def __init__(self, listing, human_readable_name, identifier,
+                 request_feedback):
+        try:
+            PyWoodchuck.__init__ (self, human_readable_name, identifier,
+                                  request_feedback)
+        except Exception, e:
+            logger.error(
+                "Failed to establish a connection to the Woodchuck server: %s"
+                % (str(e),))
+            self.available = self.not_available
+            return
+
+        self.listing = listing
+
+    def not_available(self):
+        return False
+
+    # Woodchuck upcalls.
+    def stream_update_cb(self, stream):
+        logger.debug("stream update called on %s (%s)"
+                     % (stream.human_readable_name, stream.identifier,))
+
+        # Make sure no one else is concurrently updating this
+        # feed.
+        try:
+            self.listing.updateFeed(stream.identifier)
+        except:
+            logger.debug("Updating %s: %s"
+                         % (stream.identifier, traceback.format_exc ()))
+
+    def object_transfer_cb(self, stream, object,
+                           version, filename, quality):
+        log ("object transfer called on %s (%s) in stream %s (%s)"
+             % (object.human_readable_name, object.identifier,
+                stream.human_readable_name, stream.identifier))
+
+_w = None
+def wc_init(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",
+                      request_feedback)
+
+    if not woodchuck_imported or not _w.available ():
+        logger.info("Unable to contact Woodchuck server.")
+    else:
+        logger.debug("Woodchuck appears to be available.")
+
+def wc():
+    """Return the Woodchuck singleton."""
+    global _w
+    assert _w is not None
+    return _w