OSDN Git Service

Multithreads are abandoned. Alternatly, The asyncore substitutes.(#16776)
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / misc.py
index ca852b0..9bf7d98 100644 (file)
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
+import pygtk
+pygtk.require('2.0')
+import gtk
+import gobject
 import os.path
 import re
-import threading
 from datetime import tzinfo, timedelta, datetime
 import itertools
+import traceback
 
 import config
 from BbsType import bbs_type_exception
@@ -142,19 +146,6 @@ def httpdate_to_secs(httpdate):
         raise ValueError
 
 
-class ThreadInvoker(threading.Thread):
-    def __init__(self, on_end, *methods):
-        super(ThreadInvoker, self).__init__()
-        self.on_end = on_end
-        self.methods = methods
-    def run(self):
-        try:
-            for m in self.methods:
-                m()
-        finally:
-            self.on_end()
-
-
 class FileWrap:
     def __init__(self, path, mode="a+"):
         self.__file = None
@@ -205,3 +196,26 @@ def tabbed_to_dict_generator(tabbed, sep="\t"):
 def tabbed_to_dict(tabbed, sep="\t"):
     """Creates a dict from key equal value pairs seperated with tab"""
     return dict([pair for pair in tabbed_to_dict_generator(tabbed, sep)])
+
+
+class StopChainException: pass
+
+def _do_chain(function, on_end, iterable):
+    try:
+        value = iterable.next()
+        function(value)
+    except StopIteration:
+        # normal end.
+        on_end()
+    except StopChainException:
+        # normal end.
+        on_end()
+    except:
+        # an error is occurred.
+        on_end()
+        traceback.print_exc()
+    else:
+        gobject.idle_add(_do_chain, function, on_end, iterable)
+
+def chain(function, on_end, iterable):
+    gobject.idle_add(_do_chain, function, on_end, iterable)