No Description

interface.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import os
  4. import threading
  5. try:
  6. import Queue as queue
  7. except:
  8. import queue
  9. import time
  10. import piwigotools.progressbar as progressbar
  11. class Step(threading.Thread):
  12. def __init__(self, qin, qout, qerr):
  13. threading.Thread.__init__(self)
  14. self.qin = qin
  15. self.qout = qout
  16. self.qerr = qerr
  17. def run(self):
  18. while not self.qin.empty():
  19. try:
  20. call, arg, kw = self.qin.get_nowait()
  21. try:
  22. call(*arg, **kw)
  23. except Exception as e:
  24. self.qerr.put([call, arg, kw, e])
  25. self.qout.put([call, arg, kw])
  26. except queue.Empty:
  27. pass
  28. class Run:
  29. def __init__(self, name, cnt=1):
  30. self._name = name
  31. self._qin = queue.Queue()
  32. self._qout = queue.Queue()
  33. self._qerr = queue.Queue()
  34. self._threads = [ Step(self._qin, self._qout, self._qerr) for i in range(cnt)]
  35. @property
  36. def error(self):
  37. """
  38. return true if _qerr.qsize() > 0
  39. """
  40. if self._qerr.qsize() > 0:
  41. return True
  42. return False
  43. @property
  44. def strerror(self):
  45. ret = ""
  46. while not self._qerr.empty():
  47. call, arg, kw, e = self._qerr.get_nowait()
  48. ret = "%s%s\n" % (ret, e)
  49. return ret
  50. def add(self, call, arg, kw):
  51. self._qin.put([call, arg, kw])
  52. def start(self):
  53. self._qout.maxsize = self._qin.qsize()
  54. pbar = progressbar.ProgressBar(widgets=['%s ' % self._name,
  55. progressbar.Counter() ,
  56. ' on %s ' % self._qin.qsize(),
  57. progressbar.Bar(),
  58. ' ',
  59. progressbar.Timer()],
  60. maxval=self._qin.qsize()).start()
  61. for thread in self._threads:
  62. thread.start()
  63. while not self._qout.full():
  64. time.sleep(0.1) # sleep 0.1s
  65. pbar.update(self._qout.qsize())
  66. pbar.finish()
  67. return self._qerr
  68. class StepAnalyse(threading.Thread):
  69. def __init__(self, pbar):
  70. threading.Thread.__init__(self)
  71. self._pbar = pbar
  72. self._stopevent = threading.Event()
  73. def run(self):
  74. self._pbar.start()
  75. i = 0
  76. while not self._stopevent.isSet():
  77. try:
  78. self._pbar.update(i)
  79. i = i + 1
  80. except:
  81. pass
  82. time.sleep(0.1)
  83. def stop(self):
  84. self._stopevent.set()
  85. self._pbar.finish()
  86. class Analyse:
  87. def __init__(self, name):
  88. self._name = name
  89. pbar = progressbar.ProgressBar(widgets=['%s: ' % name,
  90. progressbar.AnimatedMarker(),
  91. ' | ',
  92. progressbar.Timer()]
  93. )
  94. self._thread = StepAnalyse(pbar)
  95. def start(self):
  96. self._thread.start()
  97. def stop(self):
  98. self._thread.stop()
  99. def purge_kw(kw, notkw):
  100. return {k : kw[k] for k in kw if k not in notkw}