class LiveviewStreamThread(threading.Thread): def __init__(self, url): # Direct class call `threading.Thread` instead of `super()` for python2 capability threading.Thread.__init__(self) self.lv_url = url self._lilo_head_pool = LifoQueue() self._lilo_jpeg_pool = LifoQueue() self.header = None self.frameinfo = [] def run(self): sess = urlopen(self.lv_url) while True: header = sess.read(8) ch = common_header(header) data = sess.read(128) payload = payload_header(data, payload_type=ch['payload_type']) if ch['payload_type'] == 1: data_img = sess.read(payload['jpeg_data_size']) assert len(data_img) == payload['jpeg_data_size'] self._lilo_head_pool.put(header) self._lilo_jpeg_pool.put(data_img) elif ch['payload_type'] == 2: self.frameinfo = [] for x in range(payload['frame_count']): data_img = sess.read(payload['frame_size']) self.frameinfo.append(payload_frameinfo(data_img)) sess.read(payload['padding_size']) def get_header(self): if not self.header: try: self.header = self._lilo_head_pool.get_nowait() except Exception as e: self.header = None return self.header def get_latest_view(self): # note this is a blocking call data_img = self._lilo_jpeg_pool.get() # retrive next header try: self.header = self._lilo_head_pool.get_nowait() except Exception as e: self.header = None return data_img def get_frameinfo(self): return self.frameinfo
class LiveviewStreamThread(threading.Thread): def __init__(self, url): # Direct class call `threading.Thread` instead of `super()` for python2 capability threading.Thread.__init__(self) self.lv_url = url self._lilo_head_pool = LifoQueue() self._lilo_jpeg_pool = LifoQueue() self.header = None self.frameinfo = [] def run(self): sess = urlopen(self.lv_url) while True: header = sess.read(8) ch = common_header(header) data = sess.read(128) payload = payload_header(data, payload_type=ch['payload_type']) if ch['payload_type'] == 1: data_img = sess.read(payload['jpeg_data_size']) assert len(data_img) == payload['jpeg_data_size'] self._lilo_head_pool.put(header) self._lilo_jpeg_pool.put(data_img) elif ch['payload_type'] == 2: self.frameinfo = [] for x in range(payload['frame_count']): data_img = sess.read(payload['frame_size']) self.frameinfo.append(payload_frameinfo(data_img)) sess.read(payload['padding_size']) def get_header(self): if not self.header: try: self.header = self._lilo_head_pool.get_nowait() except Exception as e: self.header = None return self.header def get_latest_view(self): # note this is a blocking call data_img = self._lilo_jpeg_pool.get() # retrive next header try: self.header = self._lilo_head_pool.get_nowait() except Exception as e: self.header = None return data_img def get_frameinfo(self): return self.frameinfo
def DFS(N, L, root): queue = LifoQueue() queue.put_nowait(root) while queue.qsize() != 0: arrangement = queue.get_nowait() if arrangement.L == L: return arrangement.Map else: search(N, arrangement, queue) return 0
def _searchp_with_letters(self, p, has_letters): indexes, letters = p.get_letters_at_positions() words = set() s = Stack() i = 0 s.put(self.layer[indexes[i]].children[letters[i]]) has_letters += -1 i += 1 # DFS while not s.empty(): node = s.get_nowait() logapp('DEBUG', str(node)) # need to know which level we at, so I can compare also the other indexes/letters and check for words if node.level == p.length: # get me those words son logapp('DEBUG', 'Adding words if they match: ' + str(node.words)) for word in node.words: if p.check(word): words.update([word]) elif node.lengths.has_key(p.length): if has_letters and node.level == indexes[i]: logapp('DEBUG', 'we are at next letter in pattern: ' + letters[i]) #are we at level of next letter? # put only the child that checks with the next letter # BUT only if it goes to a word of the right length if letters[i] in node.lengths[p.length]: s.put(node.children[letters[i]]) logapp('DEBUG', 'there is a path from here, adding child.') i += 1 has_letters += -1 else: # put all the childs that go to a word of the right length logapp('DEBUG', 'Adding all children.') for child in node.lengths[p.length]: s.put(node.children[child]) return words
class DepFetcher: def __init__(self, comake): self.comake = comake self.root = os.getenv('COMAKEPATH') if not path.isdir(self.root): makedirs(self.root) self.queue = Queue() self.stack = LifoQueue() self.dep_set = set() self.thread = None self.stop = False self.work_num = 4 self.dep_version = {} def set_work_num(self, num): self.work_num = num def worker(self): while True: try: dep = self.queue.get_nowait() except Empty: break else: repo = utils.GetPathFromUri(dep['uri']) if repo: self.stack.put(repo) if dep["uri"] not in self.dep_set: try: self.dep_version[dep["uri"]] = dep self.dep_set.add(dep["uri"]) deps = self.getOneRepo(dep) except Exception as e: print RedIt(e.message) else: for d in deps: if len(d["uri"]) == 0: continue self.queue.put(d) self.queue.task_done() def getRepo(self): for dep in self.comake["dependency"]: if len(dep['uri']) == 0: continue self.queue.put(dep) self.thread = Thread(target=self.worker) self.thread.start() self.thread.join() self.queue.join() duplicated_dep = set() dep_list = [] while True: try: dep = self.stack.get_nowait() except Empty: break else: if dep not in duplicated_dep: duplicated_dep.add(dep) dep_list.append(dep) with open('.comake_deps', "wb") as f: pickle.dump(dep_list, f) def getOneRepo(self, dep): repo = None if len(dep["uri"]) == 0: return {} url = urlparse(dep["uri"]) if url.scheme == u"file": #TODO use local file repo_path = path.sep.join([self.root, url.netloc, url.path]) local_path = url.path.split(os.path.sep) if path.exists(repo_path): print GreenIt("[NOTICE] find local repo success: " + repo_path) else: print RedIt("[ERROR] find local repo failed: " + dep["uri"]) else: local_path = [self.root, url.netloc] local_path.extend([x for x in url.path.split('/') if x]) if not local_path[-1].endswith('.git'): print RedIt("[error] wrong dependency uri format: {}".format( dep['uri'])) return [] local_path[-1] = local_path[-1][0:-4] repo_path = path.sep.join(local_path) if not path.isdir(repo_path): makedirs(repo_path) try: repo = Repo(repo_path) except git.exc.InvalidGitRepositoryError: repo = Repo.clone_from(dep['uri'], repo_path) finally: try: if dep['tag'] is not "master": tagRepo = repo.tags[dep['tag']] repo.head.reference = tagRepo repo.head.reset(index=True, working_tree=True) print GreenIt("[NOTICE] {0} ({1}) set success.".format( local_path[-1], dep['tag'])) except IndexError: # TODO pull master to get latest tag version print RedIt( "[NOTICE] {0} ({1}) {2} set failed as {1} is invalid.". format(local_path[-1], dep['tag'], repo_path)) # if self.comake['use_local_makefile'] == 1: # return [] comake_file = path.sep.join([repo_path, 'COMAKE']) if not path.exists(comake_file): #c_file = local_path[1:] #c_file.append('COMAKE') c_file = local_path[-1] comake_url = urljoin(REPO_URL, c_file) print "start fetching " + comake_url GetComake(comake_url, comake_file) parser = ComakeParser() ret = parser.Parse(comake_file)["dependency"] print GreenIt("[NOTICE] {0} ({1}) parsed success.".format( local_path[-1], dep['tag'])) #self.stack.append(repo_path) return ret
class DepFetcher: def __init__(self, comake): self.comake = comake self.root = os.getenv('COMAKEPATH') if not path.isdir(self.root): makedirs(self.root) self.queue = Queue() self.stack = LifoQueue() self.dep_set = set() self.thread = None self.stop = False self.work_num = 4 self.dep_version = {} def set_work_num(self, num): self.work_num = num def worker(self): while True: try: dep = self.queue.get_nowait() except Empty: break else: repo = utils.GetPathFromUri(dep['uri']) if repo: self.stack.put(repo) if dep["uri"] not in self.dep_set: try: self.dep_version[dep["uri"]] = dep self.dep_set.add(dep["uri"]) deps = self.getOneRepo(dep) except Exception as e: print RedIt(e.message) else: for d in deps: if len(d["uri"]) == 0: continue self.queue.put(d) self.queue.task_done() def getRepo(self): for dep in self.comake["dependency"]: if len(dep['uri']) == 0: continue self.queue.put(dep) self.thread = Thread(target=self.worker) self.thread.start() self.thread.join() self.queue.join() duplicated_dep = set() dep_list = [] while True: try: dep = self.stack.get_nowait() except Empty: break else: if dep not in duplicated_dep: duplicated_dep.add(dep) dep_list.append(dep) with open('.comake_deps', "wb") as f: pickle.dump(dep_list, f) def getOneRepo(self, dep): repo = None if len(dep["uri"]) == 0: return {} url = urlparse(dep["uri"]) if url.scheme == u"file": #TODO use local file repo_path = path.sep.join([self.root, url.netloc, url.path]); local_path = url.path.split(os.path.sep) if path.exists(repo_path): print GreenIt("[NOTICE] find local repo success: " + repo_path) else: print RedIt("[ERROR] find local repo failed: " + dep["uri"]) else: local_path = [self.root, url.netloc] local_path.extend([x for x in url.path.split('/') if x]) if not local_path[-1].endswith('.git'): print RedIt("[error] wrong dependency uri format: {}".format(dep['uri'])) return [] local_path[-1] = local_path[-1][0:-4] repo_path = path.sep.join(local_path) if not path.isdir(repo_path): makedirs(repo_path) try: repo = Repo(repo_path) except git.exc.InvalidGitRepositoryError: repo = Repo.clone_from(dep['uri'], repo_path) finally: try: if dep['tag'] is not "master": tagRepo = repo.tags[dep['tag']] repo.head.reference = tagRepo repo.head.reset(index=True, working_tree=True) print GreenIt("[NOTICE] {0} ({1}) set success.".format(local_path[-1], dep['tag'])) except IndexError: # TODO pull master to get latest tag version print RedIt("[NOTICE] {0} ({1}) {2} set failed as {1} is invalid.".format(local_path[-1], dep['tag'], repo_path)) # if self.comake['use_local_makefile'] == 1: # return [] comake_file = path.sep.join([repo_path, 'COMAKE']) if not path.exists(comake_file): #c_file = local_path[1:] #c_file.append('COMAKE') c_file = local_path[-1] comake_url = urljoin(REPO_URL, c_file) print "start fetching " + comake_url GetComake(comake_url, comake_file) parser = ComakeParser() ret = parser.Parse(comake_file)["dependency"] print GreenIt("[NOTICE] {0} ({1}) parsed success.".format(local_path[-1], dep['tag'])) #self.stack.append(repo_path) return ret
class ThreadPool(object): def __init__(self, threadNum, max_tasks_per_period=10, seconds_per_period=30): self.pool = [] # 线程池 self.threadNum = threadNum # 线程数 self.runningLock = Lock() # 线程锁 self.taskLock = Lock() # getTask函数的锁 self.running = 0 # 正在run的线程数 # 设置为LIFO队列:在抓取了第一个post的页面后,随后需要添加所有其后的评论页, # 使用LIFO队列可以保证尽快将第一个post的所有评论抓取到,并存储 self.taskQueue = LifoQueue() # 任务队列 # 一分钟内允许的最大访问次数 self.max_tasks_per_period = max_tasks_per_period # 定制每分钟含有的秒数 self.seconds_per_period = seconds_per_period # 当前周期内已经访问的网页数量 self.currentPeriodVisits = 0 # 将一分钟当作一个访问周期,记录当前周期的开始时间 self.periodStart = time.time() # 使用当前时间初始化 def startThreads(self): """Create a certain number of threads and started to run All Workers share the same ThreadPool """ # 开始当前的抓取周期 self.periodStart = time.time() for i in range(self.threadNum): self.pool.append(Worker(self, i)) def stopThreads(self): for thread in self.pool: thread.stop() thread.join() del self.pool[:] def putTask(self, func, *args, **kargs): self.taskQueue.put((func, args, kargs)) def getTask(self, *args, **kargs): # 进行访问控制: 判断当前周期内访问的网页数目是否大于最大数目 if self.currentPeriodVisits >= self.max_tasks_per_period - 2: timeNow = time.time() seconds = timeNow - self.periodStart if seconds < self.seconds_per_period: # 如果当前还没有过一分钟,则sleep remain = self.seconds_per_period - seconds print "ThreadPool Waiting for " + str(remain) + " seconds." time.sleep(int(remain + 1)) self.periodStart = time.time() # 重新设置开始时间 self.currentPeriodVisits = 0 try: # task = self.taskQueue.get(*args, **kargs) task = self.taskQueue.get_nowait() except Empty: return (None, None, None) self.currentPeriodVisits += 1 return task def taskJoin(self, *args, **kargs): """Queue.join: Blocks until all items in the queue have been gotten and processed. """ self.taskQueue.join() def taskDone(self, *args, **kargs): self.taskQueue.task_done() def increaseRunsNum(self): self.runningLock.acquire() self.running += 1 # 正在运行的线程数加1 self.runningLock.release() def decreaseRunsNum(self): self.runningLock.acquire() self.running -= 1 self.runningLock.release() def getTaskLeft(self): # 线程池的所有任务包括: # taskQueue中未被下载的任务, resultQueue中完成了但是还没被取出的任务, 正在运行的任务 # 因此任务总数为三者之和 return self.taskQueue.qsize() + self.running
class Processer(object): def __init__(self, parent=None): # if current_processer: # raise Exception() self.children = [] self.parent = parent if parent: parent.children.append(self) self.callStack = LifoQueue() self.callDepth = 0 self.env = Globals.Globals self.ast = None self.stackPointer = 0 self.cenv = None self.initialCallDepth = 0 self.shouldAbort = None def getContinuation(self): if self.parent: pc = self.parent.continuation else: pc = dict(callDepth=0, callStack=[], initialCallDepth=0) return dict(env=self.cenv, callDepth=self.callDepth + pc['callDepth'], callStack=deepcopy(self.callStack.queue) + pc['callStack'], initialCallDepth=self.initialCallDepth + pc['initialCallDepth'], stackPointer=self.stackPointer) def setContinuation(self, args): continuation, retval = args self.callStack.queue[:] = deepcopy(continuation['callStack']) self.callDepth = continuation['callDepth'] self.cenv = continuation['env'] self.stackPointer = continuation['stackPointer'] self.popStack(retval) continuation = property(getContinuation, setContinuation) def pushStackN(self): self.callStack.put((self.ast, self.cenv, self.stackPointer, 0)) self.callDepth += 1 def popStackN(self): self.ast, self.cenv, self.stackPointer, garbage = self.callStack.get_nowait( ) self.callDepth -= 1 def pushStack(self, ast): if debug.getDebug('pushStack'): import traceback traceback.print_stack() print 'push', self.ast, self.stackPointer self.callStack.put((self.ast, self.cenv, self.stackPointer, 1)) self.ast = ast self.cenv = Environment(self.cenv) self.stackPointer = 0 self.callDepth += 1 def popStack(self, retval, wrap=True): if debug.getDebug('popStack'): import traceback traceback.print_stack() print 'pop', self.ast, retval, self.stackPointer, if len(self.callStack.queue): print self.callStack.queue[-1][0], self.callStack.queue[-1][2] print print if isinstance(retval, Symbol) and wrap: if isinstance(retval, SyntaxSymbol): retval = MacroSymbol(retval).setObj(retval) else: retval = MacroSymbol(retval).setEnv( {retval: retval.toObject(self.cenv)}) if debug.getDebug('discardedFrames'): discarded_frames.append((self.ast, self.cenv, self.stackPointer)) try: self.ast, self.cenv, self.stackPointer, rv = self.callStack.get_nowait( ) except Empty as e: e.ret = e.retval = self.ast[-1] raise e self.callDepth -= 1 if rv: if self.stackPointer >= len(self.ast): self.popStack(retval) self.ast[self.stackPointer] = retval LOG('popStack', self.ast, self.stackPointer) def dumpStack(self): while self.callDepth > 0 and self.callStack.queue: self.popStackN() self.stackPointer = 0 self.cenv = None self.initialCallDepth = 0 self.ast = None self.callDepth = 0 tcd = 0 def doProcess(self, _ast, env=None, callDepth=None, ccc=False, quotesExpanded=False): if not ccc: self.dumpStack() try: return self.process(_ast, env, callDepth, quotesExpanded) except callCCBounce as e: print >> open('/tmp/ccc.log', 'a'), (e.continuation, e.retval) # noinspection PyUnresolvedReferences continuation = e.continuation callDepth = self.callDepth icd = self.initialCallDepth self.dumpStack() self.callStack.queue = deepcopy(continuation['callStack']) self.callDepth = continuation['callDepth'] self.initialCallDepth = 0 LOG('call/cc bounce', 109, self.ast) self.ast, self.cenv, self.stackPointer, rv = self.callStack.get_nowait( ) # self.callStack.queue.pop(0) #self.callDepth-=1 LOG('call/cc bounce', self.ast) seek = True while True: for i in xrange(len(self.ast)): if isinstance(self.ast[i], list): if isinstance(self.ast[i], list) and self.ast[i][0] is e.ccc: self.ast[i] = e.retval self.stackPointer = i + 1 #a = self.ast #self.popStackN() #self.ast=a seek = False break if not seek: break self.ast, self.cenv, self.stackPointer, rv = self.callStack.get_nowait( ) self.tcd += 1 while self.callDepth > 1: try: self.doProcess(self.ast, self.cenv, 0, True) except TypeError as e3: if not callable(self.ast[0]): try: self.popStack(self.ast[-1]) except Empty as e5: if hasattr(e5, 'ret'): return e5.ret if hasattr(e5, 'retval'): return e5.retval raise e5 #return self.doProcess(self.ast, self.cenv, 0, True) else: raise e3 print 157, self.ast, self.stackPointer, self.callStack.queue, self.callDepth except Empty as e4: if hasattr(e4, 'ret'): return e4.ret if hasattr(e4, 'retval'): return e4.retval raise e4 return self.ast[-1] except Empty as e1: if hasattr(e1, 'ret'): return e1.ret if hasattr(e1, 'retval'): self.dumpStack() return e1.retval raise e1 def process(self, _ast, env=None, callDepth=None, quotesExpanded=True): global current_processer current_processer = self if _ast == [[]]: raise SyntaxError() """ :rtype : object :param _ast: :param env: Environment :return: """ try: if callDepth is not None: self.initialCallDepth = callDepth else: self.initialCallDepth = self.callDepth if env is None: self.cenv = self.env else: self.cenv = env self.ast = _ast if not quotesExpanded: self.ast = expand_quotes(self.ast) self.stackPointer = 0 if not isinstance(self.ast, list): if isinstance(self.ast, Symbol): this = self.ast.toObject(self.cenv) else: this = self.ast if self.callDepth: self.popStack(this) else: return this if len(self.ast) == 1 and not isinstance(self.ast[0], list): if isinstance(self.ast[0], Symbol): this = self.ast[0].toObject(self.cenv) else: this = self.ast[0] if self.callDepth > self.initialCallDepth: self.popStack(this) else: return this while True: if self.shouldAbort: raise self.shouldAbort if self.stackPointer >= len( self.ast) and self.callDepth <= self.initialCallDepth: return self.ast[-1] if self.stackPointer >= len(self.ast): for idx, i in enumerate(self.ast): if isinstance(i, Symbol) and i.isBound(self.cenv): self.ast[idx] = i.toObject(self.cenv) initial_call_depth = self.initialCallDepth if isinstance(self.ast[0], Symbol): self.ast[0] = self.ast[0].toObject(self.cenv) if isinstance(self.ast[0], SimpleProcedure): this = self.ast[0] args = self.ast[1:] params = deepcopy(this.ast[0]) e = Environment(this.env) if isinstance(params, list): if '.' in params: iter_args = iter(args) for idx, item in enumerate(params[:-2]): e[item] = iter_args.next() e[params[-1]] = list(iter_args) else: if (isinstance(args, list) and len(args) != len(params)): raise TypeError( "%r expected exactly %i arguments, got %i" % (self.ast[0], len(params), len(args))) if (not isinstance(args, list) and 1 != len(params)): raise TypeError( "%r expected exactly %i arguments, got %i" % (self.ast[0], len(params), len(args))) iter_args = iter(args) for idx, item in enumerate(params): e[item] = iter_args.next() else: e[params] = args self.popStackN() self.pushStack( deepcopy([ Symbol('last'), [Symbol('list')] + this.ast[1:] ])) self.cenv = Environment(e) continue elif Procedure in providedBy(self.ast[0]): r = self.ast[0](self, self.ast[1:]) self.popStack(r) elif Macro in providedBy(self.ast[0]): r = self.ast[0](self, self.ast[1:]) if r is None: continue self.popStack(r) else: r = self.ast[0](*self.ast[1:]) self.popStack(r) self.initialCallDepth = initial_call_depth self.stackPointer += 1 continue this = self.ast[self.stackPointer] if isinstance(this, SyntaxSymbol): this = this.toObject(self.cenv) if isinstance(this, list): self.pushStack(this) continue if isinstance(this, Symbol) and this.isBound(self.cenv): t = this.toObject(self.cenv) while isinstance(t, Symbol) and t.isBound(self.cenv): t = t.toObject(self.cenv) else: t = this if self.stackPointer == 0 and Macro in providedBy(t): initial_call_depth = self.initialCallDepth r = t(self, self.ast[1:]) self.initialCallDepth = initial_call_depth if r is None: continue if isinstance(r, SyntaxSymbol): self.popStack(r) elif not isinstance(r, list): r1 = [lambda *x: r] # self.ast[:] = r1 self.popStack(r1) else: self.ast[:] = r continue if isinstance(this, Symbol) and this.isBound(self.cenv): self.ast[self.stackPointer] = this.toObject(self.cenv) self.stackPointer += 1 except Empty as e: if hasattr(e, 'ret'): return e.ret return self.ast[-1]
class SpeechSynthesizer: def __init__(self): # Queue holding the last speech utterance self._speak_queue = LifoQueue(1) self._session = Session(profile_name="mylespolly") self._polly = self._session.client("polly", region_name="eu-west-1") self._thread = Thread(target=self.run, args=()) self._thread.daemon = True self._thread.start() def request(self, text): """Clear queue (ignore it being empty) and add text, both non-blocking""" # for Python 3.4+ this could be written as: with suppress(Empty): ... assuming: from contextlib import suppress try: self._speak_queue.get_nowait() except Empty: pass self._speak_queue.put_nowait(text) def run(self): """Continuously process the queue and trigger speech outputs""" while True: text = self._speak_queue.get(True, None) print(text) try: response = self._polly.synthesize_speech(Text=text, OutputFormat="mp3", VoiceId="Salli") except (BotoCoreError, ClientError) as error: print(error) sys.exit(-1) # Access the audio stream from the response if "AudioStream" in response: # Note: Closing the stream is important as the service throttles on the # number of parallel connections. Here we are using contextlib.closing to # ensure the close method of the stream object will be called automatically # at the end of the with statement's scope. with closing(response["AudioStream"]) as stream: output = os.path.join(gettempdir(), "speech.mp3") print(output) try: # Open a file for writing the output as a binary stream with open(output, "wb") as file: file.write(stream.read()) except IOError as error: # Could not write to file, exit gracefully print(error) sys.exit(-1) else: # The response didn't contain audio data, exit gracefully print("Could not stream audio") sys.exit(-1) # Play the audio using VLC # see https://wiki.videolan.org/Python_bindings # see https://www.olivieraubert.net/vlc/python-ctypes/doc/index.html p = vlc.MediaPlayer(output) sleep(0.1) p.play() sleep(0.1) while p.is_playing(): pass
class Processer(object): pause_object=object() def __init__(self, parent=None): # if current_processer: # raise Exception() self.children = [] self.parent = parent if parent: parent.children.append(self) self.callStack = LifoQueue() self.callDepth = 0 self.env = Globals.Globals self.ast = None self.stackPointer = 0 self.cenv = None self.initialCallDepth = 0 def getContinuation(self): if self.parent: pc = self.parent.continuation else: pc = dict(callDepth=0, callStack=[], initialCallDepth=0) return dict(env=self.cenv, callDepth=self.callDepth + pc['callDepth'], callStack=deepcopy(self.callStack.queue) + pc['callStack'], initialCallDepth=self.initialCallDepth + pc['initialCallDepth'], stackPointer=self.stackPointer) def setContinuation(self, args): continuation, retval = args self.callStack.queue[:] = deepcopy(continuation['callStack']) self.callDepth = continuation['callDepth'] self.cenv = continuation['env'] self.stackPointer = continuation['stackPointer'] self.popStack(retval) continuation = property(getContinuation, setContinuation) def pushStackN(self): self.callStack.put((self.ast, self.cenv, self.stackPointer, 0)) self.callDepth += 1 def popStackN(self): self.ast, self.cenv, self.stackPointer, garbage = self.callStack.get_nowait() self.callDepth -= 1 def pushStack(self, ast): if debug.DEBUG > 1: import traceback traceback.print_stack() print 'push', self.ast, self.stackPointer self.callStack.put((self.ast, self.cenv, self.stackPointer, 1)) self.ast = ast self.cenv = Environment(self.cenv) self.stackPointer = 0 self.callDepth += 1 def popStack(self, retval, wrap=True): if debug.DEBUG > 1: import traceback traceback.print_stack() print 'pop', self.ast, retval, self.stackPointer, if len(self.callStack.queue): print self.callStack.queue[-1][0], self.callStack.queue[-1][2] print print if isinstance(retval, Symbol) and wrap: if isinstance(retval, SyntaxSymbol): retval = MacroSymbol(retval).setObj(retval) else: retval = MacroSymbol(retval).setEnv({retval: retval.toObject(self.cenv)}) if debug.DEBUG>10: discarded_frames.append((self.ast, self.cenv, self.stackPointer)) try: self.ast, self.cenv, self.stackPointer, rv = self.callStack.get_nowait() except Empty as e: e.ret = e.retval=self.ast[-1] raise e self.callDepth -= 1 if rv: if self.stackPointer>=len(self.ast): self.popStack(retval) self.ast[self.stackPointer] = retval if debug.DEBUG > 1: print self.ast, self.stackPointer def dumpStack(self): while self.callDepth > 0 and self.callStack.queue: self.popStackN() self.stackPointer = 0 self.cenv = None self.initialCallDepth = 0 self.ast = None self.callDepth = 0 tcd=0 def doProcess(self, _ast, env=None, callDepth=None, ccc=False, quotesExpanded=False): print 102 if not ccc: self.dumpStack() def LOG(*stuff): print stuff LOG(97, _ast) try: p = self.iprocess(_ast, env, callDepth, quotesExpanded) v = p.next() while v is self.pause_object: yield v v=p.next() yield v except callCCBounce as e: print >> open('/tmp/ccc.log','a'), (e.continuation, e.retval) # noinspection PyUnresolvedReferences continuation=e.continuation callDepth=self.callDepth icd = self.initialCallDepth self.dumpStack() self.callStack.queue=deepcopy(continuation['callStack']) self.callDepth=continuation['callDepth'] self.initialCallDepth=0 LOG(109, self.ast) self.ast, self.cenv, self.stackPointer, rv = self.callStack.get_nowait() #self.callStack.queue.pop(0) #self.callDepth-=1 LOG(111, self.ast) seek=True while True: for i in xrange(len(self.ast)): LOG(114, self.ast[i]) if isinstance(self.ast[i],list): LOG(116, self.ast[i][0],e.ccc) if isinstance(self.ast[i],list) and self.ast[i][0] is e.ccc: LOG(118) self.ast[i]=e.retval self.stackPointer=i+1 #a = self.ast #self.popStackN() #self.ast=a seek=False break if not seek: break self.ast, self.cenv, self.stackPointer, rv = self.callStack.get_nowait() print (135, self.ast) print 155, self.ast, self.callDepth, self.callStack.queue, continuation['initialCallDepth'], continuation['targetCallDepth'] self.tcd += 1 while self.callDepth > 1: try: print(148, self.callDepth, self.ast) p1 = self.doProcess(self.ast, self.cenv, 0, True) v=p1.next() while v is self.pause_object: yield v v=p1.next() except TypeError as e3: print(162) if not callable(self.ast[0]): try: self.popStack(self.ast[-1]) except Empty as e5: if hasattr(e5, 'ret'): yield e5.ret if hasattr(e5, 'retval'): yield e5.retval raise e5 #return self.doProcess(self.ast, self.cenv, 0, True) else: raise e3 print 157, self.ast, self.stackPointer, self.callStack.queue, self.callDepth except Empty as e4: if hasattr(e4, 'ret'): yield e4.ret if hasattr(e4, 'retval'): yield e4.retval raise e4 yield self.ast[-1] except Empty as e1: LOG(166) if hasattr(e1, 'ret'): yield e1.ret if hasattr(e1, 'retval'): self.dumpStack() yield e1.retval raise e1 def process(self, *a, **k): ip = self.iprocess(*a,**k) v=ip.next() while v is self.pause_object: v=ip.next() return v def iprocess(self, _ast, env=None, callDepth=None, quotesExpanded=True): global current_processer current_processer = self if _ast == [[]]: raise SyntaxError() """ :rtype : object :param _ast: :param env: Environment :return: """ try: if callDepth is not None: self.initialCallDepth = callDepth else: self.initialCallDepth = self.callDepth if env is None: self.cenv = self.env else: self.cenv = env self.ast = _ast if not quotesExpanded: self.ast = expand_quotes(self.ast) self.stackPointer = 0; if not isinstance(self.ast, list): if isinstance(self.ast, Symbol): this = self.ast.toObject(self.cenv) else: this = self.ast if self.callDepth: self.popStack(this) else: yield this if len(self.ast) == 1 and not isinstance(self.ast[0], list): if isinstance(self.ast[0], Symbol): this = self.ast[0].toObject(self.cenv) else: this = self.ast[0] if self.callDepth > self.initialCallDepth: self.popStack(this) else: yield this while True: if self.stackPointer >= len(self.ast) and self.callDepth <= self.initialCallDepth: yield self.ast[-1] if self.stackPointer >= len(self.ast): yield self.pause_object for idx, i in enumerate(self.ast): if isinstance(i, Symbol) and i.isBound(self.cenv): self.ast[idx] = i.toObject(self.cenv) initial_call_depth = self.initialCallDepth if isinstance(self.ast[0], Symbol): self.ast[0] = self.ast[0].toObject(self.cenv) if isinstance(self.ast[0], SimpleProcedure): this = self.ast[0] args = self.ast[1:] params = deepcopy(this.ast[0]) e = Environment(this.env) if isinstance(params, list): if '.' in params: iter_args = iter(args) for idx, item in enumerate(params[:-2]): e[item] = iter_args.next() e[params[-1]] = list(iter_args) else: if (isinstance(args, list) and len(args) != len(params)): raise TypeError("%r expected exactly %i arguments, got %i" % (self.ast[0], len(params), len(args))) if (not isinstance(args, list) and 1 != len(params)): raise TypeError("%r expected exactly %i arguments, got %i" % (self.ast[0], len(params), len(args))) iter_args = iter(args) for idx, item in enumerate(params): e[item] = iter_args.next() else: e[params] = args self.popStackN() self.pushStack(deepcopy([Symbol('last'), [Symbol('list')] + this.ast[1:]])) self.cenv = Environment(e) continue elif Procedure in providedBy(self.ast[0]): r = self.ast[0](self, self.ast[1:]) self.popStack(r) elif Macro in providedBy(self.ast[0]): r = self.ast[0](self, self.ast[1:]) if r is None: continue self.popStack(r) else: r = self.ast[0](*self.ast[1:]) self.popStack(r) self.initialCallDepth = initial_call_depth self.stackPointer += 1 continue this = self.ast[self.stackPointer] if isinstance(this, SyntaxSymbol): this = this.toObject(self.cenv) if isinstance(this, list): self.pushStack(this) continue if isinstance(this, Symbol) and this.isBound(self.cenv): t = this.toObject(self.cenv) while isinstance(t, Symbol) and t.isBound(self.cenv): t = t.toObject(self.cenv) else: t = this if self.stackPointer == 0 and Macro in providedBy(t): initial_call_depth = self.initialCallDepth r = t(self, self.ast[1:]) self.initialCallDepth = initial_call_depth if r is None: continue if isinstance(r, SyntaxSymbol): self.popStack(r) elif not isinstance(r, list): r1 = [lambda *x: r] #self.ast[:] = r1 self.popStack(r1) else: self.ast[:] = r continue if isinstance(this, Symbol) and this.isBound(self.cenv): self.ast[self.stackPointer] = this.toObject(self.cenv) self.stackPointer += 1 except Empty as e: if hasattr(e, 'ret'): yield e.ret yield self.ast[-1]
class mainframe(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.filenm=None self.streamnm = None self.pack(fill=BOTH, expand=1) self.parent = parent self.initplayer() self.player_process = None self.seekthread = None self.fstate = False self.paused = True self.trackmouse = True self.stdout_thread = None self.stream = False self.inhibit_slider_trigger = False self.q = LifoQueue() self.currtime = 0 self.endtime = -1 def initplayer(self): self.parentframe = Frame(self) self.parentframe.pack(fill=BOTH, expand=True) self.videoFrame = Frame(self.parentframe, width=800, height=480) self.videoFrame.pack(side="top", fill="both", expand=True) self.buttonframe = Frame(self.parentframe, padding="2 2 1 1") self.buttonframe.pack(side="bottom", fill="x", expand=False) self.seekbar = Scale(self.buttonframe, from_= 0, to=100, orient=HORIZONTAL) self.seekbar.grid(column=0, columnspan=4, row=0, sticky=[N, E, S, W]) self.seekbar.configure(command=self.seeked) self.selectbutton = Button(self.buttonframe, text="Select File") self.selectbutton.grid(column=0, row=1, sticky=[E,W]) self.streambutton = Button(self.buttonframe, text="Open HTTP", command=self.streamopen) self.streambutton.grid(column=1, row=1, sticky=[E,W]) self.playbutton = Button(self.buttonframe, text="Play") self.playbutton.config(command=self.playpause) self.playbutton.grid(column=2, row=1, sticky=[E,W]) self.fullscreenbutton = Button(self.buttonframe, text="Fullscreen", command=self.togglefullscreen) self.fullscreenbutton.grid(column=3, row=1, sticky=[E,W]) for child in self.buttonframe.winfo_children(): child.grid_configure(padx=5, pady=5) self.buttonframe.rowconfigure(0, weight=1) self.buttonframe.rowconfigure(1, weight=1) self.buttonframe.columnconfigure(0, weight=1) self.buttonframe.columnconfigure(1, weight=1) self.buttonframe.columnconfigure(2, weight=1) self.buttonframe.columnconfigure(3, weight=1) self.selectbutton.configure(command=self.fileopen) self.videoFrame.bind("<Button-1>",self.playpause) self.parent.bind("<F11>", self.togglefullscreen) self.parent.bind("<Motion>",self.mouseops) def mouseops(self,event=None): self.videoFrame.config(cursor="") self.videoFrame.after(5000,self.cursorhandler) if self.trackmouse: x, y = self.parent.winfo_pointerx(), self.parent.winfo_pointery() windowx, windowy = self.parent.winfo_width(), self.parent.winfo_height() if windowy - 30 <= y: if self.fstate: self.buttonframe.pack(side="bottom", fill="x", expand=False) self.trackmouse = False self.parent.after(5000, self.mousetracker) self.inhibit_slider_trigger = False elif self.fstate: self.buttonframe.pack_forget() self.inhibit_slider_trigger = True else: self.inhibit_slider_trigger = True def mousetracker(self): print 'Mouse Tracker' self.trackmouse = True self.videoFrame.after(0,self.mouseops) def cursorhandler(self): self.videoFrame.config(cursor="none") def togglefullscreen(self, event=None): self.fstate = not self.fstate self.parent.attributes("-fullscreen",self.fstate) if self.fstate: self.fullscreenbutton.config(text="Exit Fullscreen") self.buttonframe.pack_forget() self.videoFrame.config(cursor="none") else: self.fullscreenbutton.config(text="Fullscreen") self.buttonframe.pack(side="bottom", fill="x", expand=False) self.videoFrame.after(5000, self.cursorhandler) def fileopen(self): self.filenm = askopenfilename(filetypes=[("Supported Files","*.mp4;*.mkv;*.mpg;*.avi;*.mov"),("All Files","*.*")]) self.stream = False self.play() def streamopen(self): self.streamnm = Dlog(self.parent) if self.streamnm.result is not None: s = str(self.streamnm) else: return if s.startswith('http'): self.stream = True self.play() else: self.stream = False showerror("Error","Incorrect Entry") def play(self): global fifofilename if self.filenm is not None and self.filenm != "": winid = self.videoFrame.winfo_id() if self.mplayer_isrunning(): self.stop() try: self.paused = False self.playbutton.configure(text="Pause") if not self.stream: self.player_process = Popen(["mplayer","-fs","-slave","-quiet","-wid",str(winid),self.filenm],stdin=PIPE, stdout=PIPE) else: self.player_process = Popen(["mplayer","-fs","-slave","-quiet","-wid",str(winid),self.streamnm], stdin=PIPE, stdout=PIPE) self.stdout_thread = Thread(target=self.enqueue_pipe, args=(self.player_process.stdout, self.q)) self.stdout_thread.daemon = True self.stdout_thread.start() self.emptypipe() self.seekthread = Thread(target=self.seekbar_setup, args=()) self.seekthread.daemon = True self.seekthread.start() except: showerror("Error","".join(["Couldn't play video:\n",str(sys.exc_info()[:])])) def getvidtime(self): if self.mplayer_isrunning(): self.command_player("get_time_length") output = self.readpipe() while "ANS_LENGTH" not in output: output = self.readpipe() if "ANS_LENGTH" in output: return output.split('ANS_LENGTH=')[1] else: return 0 def playpause(self, event=None): if self.player_process is None: return self.paused = not self.paused if self.paused: print "VIDEO IS PAUSED /B/RO" self.playbutton.configure(text="Play") else: self.playbutton.configure(text="Pause") self.command_player("pause") def setwh(self,w,h): self.videoFrame.configure(width=w, height=h) def quit(self): print "QUIT CALLED" self.destroy() def mplayer_isrunning(self): if self.player_process is not None: return (self.player_process.poll() is None) else: return False def command_player(self, comd): global fifofilename if self.mplayer_isrunning(): try: self.player_process.stdin.flush() self.player_process.stdin.write("\r\n%s\r\n"%comd) # for _ in itertools.repeat(None,8192): # self.player_process.stdin.write("\n") self.player_process.stdin.flush() except: showerror("Error","Error passing command to mplayer\n%s"%sys.exc_info()[1]) def enqueue_pipe(self, out, q): print 'Working on reading mplayer pipe output...' for line in iter(out.readline, b''): q.put(line) out.close() def seekbar_setup(self): pos = '0' trial = 0 while float(pos)<1: trial += 1 pos = self.getvidtime() self.seekbar.config(to=int(float(pos))) self.endtime = int(float(pos)) Timer(1, self.seekbar_updater).start() def seekbar_updater(self): if not self.paused and self.inhibit_slider_trigger: self.currtime += 1 self.seekbar.set(self.currtime) else: self.currtime = self.seekbar.get() Timer(1, self.seekbar_updater).start() def seeked(self,e): pos = self.seekbar.get() print "We changed pos to :%d"% pos x, y = self.parent.winfo_pointerx(), self.parent.winfo_pointery() windowx, windowy = self.parent.winfo_width(), self.parent.winfo_height() if not self.inhibit_slider_trigger and windowy - 30 <= y: self.command_player("seek %d 2"%pos) if self.paused: self.command_player("pause") def startmousetrack(self): self.trackmouse = True def readpipe(self): line = "" try: line = self.q.get_nowait() except Empty: print "Empty PIPE" finally: return line def emptypipe(self): str = '' try: while not self.q.empty(): str += self.q.get_nowait() except Empty: print "Empty Pipe" finally: return str def stop(self): if self.mplayer_isrunning(): self.player_process.stdin.write("quit\n") self.player_process.stdin.flush() print self.emptypipe() self.player_process = None
print("Ready in 2") sleep(1.0) print("Ready in 1") sleep(1.0) print("GO!") initialGuess = findIG( Q_calcMag.get()) # Determine initial guess based on magnet's location # Start iteration while (True): startTime = time() # Start time # Data acquisition H = Q_calcMag.get_nowait() # Pool data from thread # Compute norms HNorm = [ float(norm(H1)), float(norm(H2)), # Compute L2 vector norms float(norm(H3)), float(norm(H4)) ] # ... # Solve system of equations sol = root( LHS, initialGuess, args=(K, HNorm), method='lm', # Invoke solver using the
class Transceiver(object): def __init__(self, config={}, message_handler=None): """Set up a receiver which connects to the messaging hub. :param config: This is a dict in the form:: config = dict( incoming='tcp://localhost:15566', # default outgoing='tcp://localhost:15567', idle_timeout=1000, # milliseconds: ) """ self.log = logging.getLogger("evasion.messenger.endpoint.Transceiver") self.endpoint_uuid = str(uuid.uuid4()) self.exit_time = threading.Event() self.wait_for_exit = threading.Event() self.incoming = None # configured in main(). self.incoming_uri = config.get("incoming", 'tcp://localhost:15566') self.log.info("Recieving on <%s>" % self.incoming_uri) self.outgoing_uri = config.get("outgoing", 'tcp://localhost:15567') self.log.info("Sending on <%s>" % self.outgoing_uri) self.idle_timeout = int(config.get("idle_timeout", 2000)) self.log.info("Idle Timeout (ms): %d" % self.idle_timeout) self.message_handler = message_handler self.sync_message = frames.sync_message( "endpoint-%s" % self.endpoint_uuid ) # Queue up messages to be sent in the main message loop self._out_queue = LifoQueue() def main(self): """Running the main loop sending and receiving. This will keep running until stop() is called. This sets the exit flag causing clean up and shutdown. """ self.exitTime = False context = zmq.Context() incoming = context.socket(zmq.SUB) incoming.setsockopt(zmq.SUBSCRIBE, '') incoming.connect(self.incoming_uri) outgoing = context.socket(zmq.PUSH); outgoing.connect(self.outgoing_uri); def _shutdown(): try: incoming.close() except ZMQError: self.log.exception("main: error calling incoming.close()") try: outgoing.close() except ZMQError: self.log.exception("main: error calling outgoing.close()") try: context.term() except ZMQError: self.log.exception("main: error calling context.term()") try: poller = zmq.Poller() poller.register(incoming, zmq.POLLIN) while not self.exit_time.is_set(): try: events = poller.poll(self.idle_timeout) except ZMQError as e: # 4 = 'Interrupted system call' if e.errno == 4: self.log.info("main: exit time: %s" % e) break else: self.log.info("main: <%s>" % e) break except Exception: self.log.exception("main: fatal error while polling ") break else: if (events > 0): msg = incoming.recv_multipart() self.message_in(tuple(msg)) # Now recover and queued outgoing messages: if not self._out_queue.empty(): message = self._out_queue.get_nowait() if message: try: # send sync hub followed by message. The sync # will kick the hub into life if its just # started: outgoing.send_multipart(self.sync_message) outgoing.send_multipart(message) except ZMQError as e: # 4 = 'Interrupted system call' if e.errno == 4: self.log.info(( "main: sigint or other signal interrupt" ", exit time <%s>" ) % e) break else: self.log.info("main: <%s>" % e) break except Exception: self.log.exception("main: fatal error sending ") break finally: self._out_queue.task_done() finally: self.wait_for_exit.set() _shutdown() def start(self): """Set up zmq communication and start receiving messages from the hub. """ # coverage can't seem to get to this: def _main(notused): # pragma: no cover self.exit_time.clear() self.wait_for_exit.clear() self.main() thread.start_new(_main, (0,)) def stop(self, wait=2): """Stop receiving messages from the hub and clean up. :param wait: The time in seconds to wait before giving up on a clean shutdown. """ self.log.info("stop: shutting down messaging.") self.exit_time.set() self.wait_for_exit.wait(wait) self.log.info("stop: done.") def message_out(self, message): """This sends a message to the messagehub for dispatch to all connected endpoints. :param message: A tuple or list representing a multipart ZMQ message. If the message is not a tuple or list then MessageOutError will be raised. Note: The message is actually queued here so that the main loop will send it when its ready. :returns: None. """ if isinstance(message, list) or isinstance(message, tuple): self._out_queue.put(message) else: m = "The message must be a list or tuple instead of <%s>" % type( message ) raise MessageOutError(m) def message_in(self, message): """Called on receipt of an evasion frame to determine what to do. The message_handler set in the constructer will be called if one was set. If none was set then the message will be logged at the DEBUG level. :param message: A tuple or list representing a multipart ZMQ message. :returns: None. """ if self.message_handler: try: #self.log.debug("message_in: message <%s>" % str(message)) self.message_handler(message) except: self.log.exception("message_in: Error handling message - ") else: self.log.debug("message_in: message <%s>" % str(message))
class Controller(object): def __init__(self, gui, model, *args, **kwargs): self.gui = gui self.model = model self.set_mvc() self.task = None self.subprocess = None self.queue = None self.progress = None self.ensemble = None self.movie_dialog = None self.molecule = None self._last_steps = 0 def set_mvc(self): self.gui.buttonWidgets['Save Input'].configure(command=self.saveinput) self.gui.buttonWidgets['Run'].configure(command=self.run) def run(self): if not self.saveinput(): return env = os.environ.copy() env['OMMPROTOCOL_SLAVE'] = '1' env['PYTHONIOENCODING'] = 'latin-1' self.task = Task("OMMProtocol for {}".format(self.filename), cancelCB=self._clear_cb, statusFreq=((1, ), 1)) self.subprocess = Popen( ['ommprotocol', self.filename], stdout=PIPE, stderr=PIPE, progressCB=self._progress_cb, #universal_newlines=True, bufsize=1, env=env) self.progress = SubprocessTask("OMMProtocol", self.subprocess, task=self.task, afterCB=self._after_cb) self.task.updateStatus("Running OMMProtocol") self.queue = LifoQueue() thread = Thread(target=enqueue_output, args=(self.subprocess.stdout, self.queue)) thread.daemon = True # thread dies with the program thread.start() self.ensemble = _TrajProxy() self.molecule = self.ensemble.molecule = self.gui.ui_chimera_models.getvalue( ) self.ensemble.name = 'Trajectory for {}'.format(self.molecule.name) self.ensemble.startFrame = self.ensemble.endFrame = 1 self.movie_dialog = MovieDialog(self.ensemble, externalEnsemble=True) self.gui.Close() def _clear_cb(self, *args): self.task.finished() self.task, self.subprocess, self.queue, self.progress, self.molecule = [ None ] * 5 if self.movie_dialog is not None: self.movie_dialog.Close() self.movie_dialog = None def _after_cb(self, aborted): if aborted: self._clear_cb() return if self.subprocess.returncode: last = self.subprocess.stderr.readlines()[-1] msg = "OMMProtocol calculation failed! Reason: {}".format(last) self._clear_cb() raise chimera.UserError(msg) self.task.finished() chimera.statusline.show_message('Yay! MD Done!') def _progress_cb(self, process): try: chunk = self.queue.get_nowait() except Empty: return self._last_steps / self.model.total_steps steps, positions = pickle.loads(chunk) if steps == self._last_steps: self._last_steps / self.model.total_steps self._last_steps = steps # Update positions in MD Movie Dialog coordinates = np.array(positions) * 10. coordsets_so_far = len(self.molecule.coordSets) cs = self.molecule.newCoordSet(coordsets_so_far) cs.load(coordinates) self.ensemble.endFrame = self.movie_dialog.endFrame = coordsets_so_far + 1 self.movie_dialog.moreFramesUpdate('', [], self.movie_dialog.endFrame) self.movie_dialog.plusCallback() return self._last_steps / self.model.total_steps def saveinput(self, path=None): self.model.parse() if path is None: path = asksaveasfilename(parent=self.gui.canvas, defaultextension='.yaml', filetypes=[('YAML', '*.yaml')]) if not path: return self.write(path) self.gui.status('Written to {}'.format(path), color='blue', blankAfter=4) return True def write(self, output): # Write input self.filename = output with open(self.filename, 'w') as f: f.write('# Yaml input for OpenMM MD\n\n') f.write('# input\n') yaml.dump(self.model.md_input, f, default_flow_style=False) f.write('\n') f.write('# output\n') yaml.dump(self.model.md_output, f, default_flow_style=False) if self.model.md_hardware: f.write('\n# hardware\n') yaml.dump(self.model.md_hardware, f, default_flow_style=False) f.write('\n# conditions\n') yaml.dump(self.model.md_conditions, f, default_flow_style=False) f.write('\n# OpenMM system options\n') yaml.dump(self.model.md_systemoptions, f, default_flow_style=False) f.write('\n\nstages:\n') for stage in self.model.stages: yaml.dump([stage], f, indent=8, default_flow_style=False) f.write('\n')
class SaveManager(QObject): start_save = pyqtSignal() report_error = pyqtSignal(object) save_done = pyqtSignal() def __init__(self, parent): QObject.__init__(self, parent) self.count = 0 self.last_saved = -1 self.requests = LifoQueue() t = Thread(name='save-thread', target=self.run) t.daemon = True t.start() self.status_widget = w = SaveWidget(parent) self.start_save.connect(w.start, type=Qt.QueuedConnection) self.save_done.connect(w.stop, type=Qt.QueuedConnection) def schedule(self, tdir, container): self.count += 1 self.requests.put((self.count, tdir, container)) def run(self): while True: x = self.requests.get() if x is None: self.requests.task_done() self.__empty_queue() break try: count, tdir, container = x self.process_save(count, tdir, container) except: import traceback traceback.print_exc() finally: self.requests.task_done() def __empty_queue(self): ' Only to be used during shutdown ' while True: try: self.requests.get_nowait() except Empty: break else: self.requests.task_done() def process_save(self, count, tdir, container): if count <= self.last_saved: shutil.rmtree(tdir, ignore_errors=True) return self.last_saved = count self.start_save.emit() try: self.do_save(tdir, container) except: import traceback self.report_error.emit(traceback.format_exc()) self.save_done.emit() def do_save(self, tdir, container): try: save_container(container, container.path_to_ebook) finally: shutil.rmtree(tdir, ignore_errors=True) @property def has_tasks(self): return bool(self.requests.unfinished_tasks) def wait(self, timeout=30): if timeout is None: self.requests.join() else: try: join_with_timeout(self.requests, timeout) except RuntimeError: return False return True def shutdown(self): self.requests.put(None)
class SaveManager(QObject): start_save = pyqtSignal() report_error = pyqtSignal(object) save_done = pyqtSignal() def __init__(self, parent, notify=None): QObject.__init__(self, parent) self.count = 0 self.last_saved = -1 self.requests = LifoQueue() self.notify_requests = LifoQueue() self.notify_data = notify t = Thread(name='save-thread', target=self.run) t.daemon = True t.start() t = Thread(name='notify-thread', target=self.notify_calibre) t.daemon = True t.start() self.status_widget = w = SaveWidget(parent) self.start_save.connect(w.start, type=Qt.QueuedConnection) self.save_done.connect(w.stop, type=Qt.QueuedConnection) def schedule(self, tdir, container): self.count += 1 self.requests.put((self.count, tdir, container)) def run(self): while True: x = self.requests.get() if x is None: self.requests.task_done() self.__empty_queue() break try: count, tdir, container = x self.process_save(count, tdir, container) except: import traceback traceback.print_exc() finally: self.requests.task_done() def notify_calibre(self): while True: if not self.notify_requests.get(): break send_message(self.notify_data) def clear_notify_data(self): self.notify_data = None def __empty_queue(self): ' Only to be used during shutdown ' while True: try: self.requests.get_nowait() except Empty: break else: self.requests.task_done() def process_save(self, count, tdir, container): if count <= self.last_saved: shutil.rmtree(tdir, ignore_errors=True) return self.last_saved = count self.start_save.emit() try: self.do_save(tdir, container) except: import traceback self.report_error.emit(traceback.format_exc()) self.save_done.emit() if self.notify_data: self.notify_requests.put(True) def do_save(self, tdir, container): try: save_container(container, container.path_to_ebook) finally: shutil.rmtree(tdir, ignore_errors=True) @property def has_tasks(self): return bool(self.requests.unfinished_tasks) def wait(self, timeout=30): if timeout is None: self.requests.join() else: try: join_with_timeout(self.requests, timeout) except RuntimeError: return False return True def shutdown(self): self.requests.put(None) self.notify_requests.put(None)
class SaveManager(QObject): start_save = pyqtSignal() report_error = pyqtSignal(object) save_done = pyqtSignal() def __init__(self, parent): QObject.__init__(self, parent) self.count = 0 self.last_saved = -1 self.requests = LifoQueue() t = Thread(name='save-thread', target=self.run) t.daemon = True t.start() self.status_widget = w = SaveWidget(parent) self.start_save.connect(w.start, type=Qt.QueuedConnection) self.save_done.connect(w.stop, type=Qt.QueuedConnection) def schedule(self, tdir, container): self.count += 1 self.requests.put((self.count, tdir, container)) def run(self): while True: x = self.requests.get() if x is None: self.requests.task_done() self.__empty_queue() break try: count, tdir, container = x self.process_save(count, tdir, container) except: import traceback traceback.print_exc() finally: self.requests.task_done() def __empty_queue(self): ' Only to be used during shutdown ' while True: try: self.requests.get_nowait() except Empty: break else: self.requests.task_done() def process_save(self, count, tdir, container): if count <= self.last_saved: shutil.rmtree(tdir, ignore_errors=True) return self.last_saved = count self.start_save.emit() try: self.do_save(tdir, container) except: import traceback self.report_error.emit(traceback.format_exc()) self.save_done.emit() def do_save(self, tdir, container): temp = None try: path = container.path_to_ebook temp = PersistentTemporaryFile(prefix=('_' if iswindows else '.'), suffix=os.path.splitext(path)[1], dir=os.path.dirname(path)) temp.close() temp = temp.name container.commit(temp) atomic_rename(temp, path) finally: if temp and os.path.exists(temp): os.remove(temp) shutil.rmtree(tdir, ignore_errors=True) @property def has_tasks(self): return bool(self.requests.unfinished_tasks) def wait(self, timeout=30): if timeout is None: self.requests.join() else: try: join_with_timeout(self.requests, timeout) except RuntimeError: return False return True def shutdown(self): self.requests.put(None)