def __start__(self, name): self._maskfile = None self._resultfiles = {} self._masknames = tuple(maskname for maskname in self.masks.keys()) basename = _os.path.splitext(name)[0] ensure_directory(self.maskdir) ensure_directory(self.resultdir) for roiname, roi in get_items(self.ROIs): if roi.is_empty(): continue resultpath = _os.path.join( self.resultdir, "Results_{0}_{1}.csv".format(basename, roiname)) # TODO: create mask file try: self._resultfiles[roiname] = open(resultpath, 'w') self._resultfiles[roiname].write('Slice,' + ','.join( _mask_entries(m) for m in self._masknames)) self._resultfiles[roiname].write('\n') except: _print_exc() print_status("*** could not open: {0}".format(resultpath)) if len(self._resultfiles) == 0: raise RuntimeError("nothing to output for {0}".format(name)) # maskpath = _os.path.join(self.maskdir, "MASK_{0}.avi".format(basename, roiname)) maskpath = _os.path.join(self.maskdir, "MASK_{0}.mp4".format(basename, roiname)) try: self._maskfile = _vio.FFmpegWriter(maskpath, outputdict=OUTPUT_SPECS) except: _print_exc() print_status("*** could not open: {0}".format(maskpath))
def run(self): """runs its EventSource object.""" status = self.source.setup() self.handler.initialized(status) status = None try: for evt in self.source: self.handler.handle(evt) except OSError as e: if DETAILED_ERROR == True: print("***error in reading from {self.source}:") _print_exc() else: print(f"***failed to read from source for {self.source}: {e}") status = e self.__running = False finally: try: self.source.finalize() self.__running = False except Exception as e: status = e self.__running = False finally: self.handler.done(status)
def signal(self): self._timing.lock() try: self._continue = False except: _print_exc() finally: self._timing.unlock()
def __bootstrap(self): try: if _trace_hook: _sys.settrace(_trace_hook) if _profile_hook: _sys.setprofile(_profile_hook) try: self.run() except SystemExit: pass except InterruptedException: # Quiet InterruptedExceptions if they're caused by # _systemrestart if not jython.shouldRestart: raise except: # If sys.stderr is no more (most likely from interpreter # shutdown) use self.__stderr. Otherwise still use sys (as in # _sys) in case sys.stderr was redefined. if _sys: _sys.stderr.write("Exception in thread %s:" % self.getName()) _print_exc(file=_sys.stderr) else: # Do the best job possible w/o a huge amt. of code to # approx. a traceback stack trace exc_type, exc_value, exc_tb = self.__exc_info() try: print >> self.__stderr, ( "Exception in thread " + self.getName() + " (most likely raised during interpreter shutdown):" ) print >> self.__stderr, ( "Traceback (most recent call last):") while exc_tb: print >> self.__stderr, ( ' File "%s", line %s, in %s' % (exc_tb.tb_frame.f_code.co_filename, exc_tb.tb_lineno, exc_tb.tb_frame.f_code.co_name)) exc_tb = exc_tb.tb_next print >> self.__stderr, ("%s: %s" % (exc_type, exc_value)) # Make sure that exc_tb gets deleted since it is a memory # hog; deleting everything else is just for thoroughness finally: del exc_type, exc_value, exc_tb finally: self.__stop() try: self.__delete() except: pass
def __procedure(self): """Function of the fork process""" _current_process().name = "Process%d" % (self.__id) _current_thread().name = "Worker%d" % (self.__id) self.debug("Fork build") while not self.__events.waitStart(self.checkPeriod): self.debug("Waiting start...") try: self.info("Fork starts %s after the event trigger" % (_datetime.now()-self.__events.whenStarted())) except Exception as e: self.warning("Start event received but not propagated when " "it was triggered") while not self._endProcedure(): try: if self.__events.isPaused(): self.info("paused") if self._procedureHas2End(): break while not self.__events.waitResume(self.checkPeriod): pass self.info("resume") else: self.__currentArgin = self.__input.get() self.debug("argin: %s" % (self.__currentArgin)) if self.__preHook is not None: self.debug("call preHook") self.__preHook(self.__currentArgin, **self.__preExtraArgs) if self._procedureHas2End(): break t_0 = _datetime.now() self.__currentArgout = self.__target(self.__currentArgin) t_diff = _datetime.now()-t_0 t_diff = t_diff.total_seconds() self.__computationTime.value += t_diff self.debug("argout: %s (%f seconds)" % (self.__currentArgout, t_diff)) self.__ctr.value += 1 self.__output.put([self.__currentArgin, self.__currentArgout]) if self.__postHook is not None: self.debug("call postHook") self.__postHook(self.__currentArgin, self.__currentArgout, **self.__postExtraArgs) self.__events.step() except Exception as e: self.error("exception: %s" % (e)) _print_exc() # process has finish, lets wake up the monitor self.__internalEvent.set() self.debug("Internal event emitted to report end of the procedure")
def __bootstrap(self): try: if _trace_hook: _sys.settrace(_trace_hook) if _profile_hook: _sys.setprofile(_profile_hook) try: self.run() except SystemExit: pass except InterruptedException: # Quiet InterruptedExceptions if they're caused by # _systemrestart if not jython.shouldRestart: raise except: # If sys.stderr is no more (most likely from interpreter # shutdown) use self.__stderr. Otherwise still use sys (as in # _sys) in case sys.stderr was redefined. if _sys: _sys.stderr.write("Exception in thread %s:" % self.getName()) _print_exc(file=_sys.stderr) else: # Do the best job possible w/o a huge amt. of code to # approx. a traceback stack trace exc_type, exc_value, exc_tb = self.__exc_info() try: print>>self.__stderr, ( "Exception in thread " + self.getName() + " (most likely raised during interpreter shutdown):") print>>self.__stderr, ( "Traceback (most recent call last):") while exc_tb: print>>self.__stderr, ( ' File "%s", line %s, in %s' % (exc_tb.tb_frame.f_code.co_filename, exc_tb.tb_lineno, exc_tb.tb_frame.f_code.co_name)) exc_tb = exc_tb.tb_next print>>self.__stderr, ("%s: %s" % (exc_type, exc_value)) # Make sure that exc_tb gets deleted since it is a memory # hog; deleting everything else is just for thoroughness finally: del exc_type, exc_value, exc_tb finally: self.__stop() try: self.__delete() except: pass
def pause(self, book=False): """ With this method the requester will ask emit the pause event. It can book the request to allow only itself to release the pause. """ try: self.__storeRequester(book) self.__emitPause() return True except Exception as e: self.error("PAUSE request failed due to: %s" % (e)) _print_exc() return False
def run(*args, **kwargs): try: ret = func(*args, **kwargs) if isinstance(ret, _Response): return ret return json_response({"data": ret}) except AppException as e: return json_response({"error": f"{e}"}) except Exception as e: _print_exc() err = "An unknown error occured" return json_response({"error": err, "tb": f"{e}"})
def __processMonitoring(self): self.debug("Start monitoring") while not self._endProcedure(): try: self.__doWait(self.__internalEvent, [[self.__events.waitPause, self.__doPause], [self.__events.waitResume, self.__doResume], ]) except Exception as e: self.error("Monitor exception: %s" % (e)) _print_exc() self.__doJoin()
def __start__(self, name): self._resultfile = None basename = _os.path.splitext(name)[0] ensure_directory(self.resultdir) resultpath = _os.path.join(self.resultdir, "Profile_{}.csv".format(basename)) try: self._resultfile = open(resultpath, 'w') self._resultfile.write('Slice,' + ','.join(self.ROIs.keys())) self._resultfile.write('\n') except: _print_exc() raise RuntimeError("could not open: {0}".format(resultpath))
def main(): line = _input(prompt).split() while len(line) > 0: if line[0] != 'help' and line[0] in _registered: try: _registered[line[0]](line) except Exception as e: _print_exc(file=_sys.stdout) elif line[0] == 'help' and len(line) > 1: for cmd in line[1:]: _help(cmd) else: print('Available commands:', ' '.join(_registered.keys())) _help() line = _input(prompt).split()
def run(input_files=[], target=None, force_conversion=True, verbose=True): if target is None: target = DEFAULT_TARGET for pathpat in input_files: for path in _glob(pathpat): try: process_file(path, target, force_conversion=force_conversion, verbose=verbose) except ProcessingError as e: print(f"***{e}", file=_sys.stderr, flush=True) continue except: _print_exc() print(f"***failed to process: {path}", file=_sys.stderr, flush=True) continue
def resume(self): """ With this method the requester will ask to resume the process. When itself is is the list of requesters, it will be removed from there, or will remove one of the non-book requesters. Only when the list is empty will the pause event be clear and resume emitted. """ try: if self.__popRequester(): return self.__emitResume() except Exception as e: self.error("RESUME request failed due to: %s" % (e)) _print_exc() return False
def process_file(path, target, verbose=True, force_conversion=True): path = _Path(path) if not path.is_file(): raise ProcessingError(f"not a file: {path}") try: total_count, per_file, nfiles = estimate_frame_per_file(path, target, verbose=verbose) except: _print_exc() raise ProcessingError(f"running of ffprobe failed") if per_file < 10: raise ProcessingError("target file size seems to be too small") elif per_file >= total_count: if force_conversion == True: return convert_file(path, nframes=total_count, verbose=verbose) else: raise ProcessingError(f"no need to split {path}") # split the video outputpath = path.with_suffix(".split") if outputpath.exists(): if not outputpath.is_dir(): raise ProcessingError(f"file exists in place of the output directory: {outputpath}") else: pass # overwrite existing .split directory else: outputpath.mkdir(parents=True) source = iter(_VReader(str(path)).nextFrame()) try: frame = next(source) # always read the next frame before # so that we don't have to create an empty fragment for j, fragment in enumerate(FragmentWriter.iterate(nfiles, basename=path.stem, outputdir=outputpath, verbose=verbose)): with fragment as out: for i in range(per_file): out.write(frame) frame = next(source) except StopIteration: pass if verbose == True: print(f">>> split into {j+1} files.")
def __bootstrap(self): try: self.__started = True _active_limbo_lock.acquire() _active[_get_ident()] = self del _limbo[self] _active_limbo_lock.release() if __debug__: self._note("%s.__bootstrap(): thread started", self) if _trace_hook: self._note("%s.__bootstrap(): registering trace hook", self) _sys.settrace(_trace_hook) if _profile_hook: self._note("%s.__bootstrap(): registering profile hook", self) _sys.setprofile(_profile_hook) try: self.run() except SystemExit: if __debug__: self._note("%s.__bootstrap(): raised SystemExit", self) except: if __debug__: self._note("%s.__bootstrap(): unhandled exception", self) s = _StringIO() _print_exc(file=s) _sys.stderr.write("Exception in thread %s:\n%s\n" % (self.getName(), s.getvalue())) else: if __debug__: self._note("%s.__bootstrap(): normal return", self) finally: self.__stop() try: self.__delete() except: pass
def run(self): self._continue = True try: while True: now = _now() while now < self._target: self._timing.lock() if self._continue == False: # _debug(f"ending timer") self._timing.unlock() break self._timing.unlock() now = _now() self._timing.lock() if self._continue == False: # _debug(f"ending timer") self._timing.unlock() break self._timing.unlock() self._waitAcq() self._target = now + self._interval except: _print_exc()
def _shutdownSocket(self, sock): try: sock.shutdown(_socket.SHUT_RDWR) except Exception as e: _print_exc()
def __exit__(self, t, v, tb): if tb: _print_exc() return True
from .ogg import * from .library_loader import ExternalLibrary, ExternalLibraryError __here = os.getcwd() libvorbis = None try: libvorbis = ExternalLibrary.load( "vorbis", tests=[lambda lib: hasattr(lib, "vorbis_info_init")]) except ExternalLibraryError: pass except: _print_exc() libvorbisfile = None try: libvorbisfile = ExternalLibrary.load( "vorbisfile", tests=[lambda lib: hasattr(lib, "ov_clear")]) except ExternalLibraryError: pass except: _print_exc() libvorbisenc = None try: libvorbisenc = ExternalLibrary.load(
def wrapper(*args, **kwargs): try: return function(*args, **kwargs) except: _print_exc() raise
def print_exc(e): return _print_exc()
def __bootstrap(self): try: self.__started = True _active_limbo_lock.acquire() _active[_get_ident()] = self del _limbo[self] _active_limbo_lock.release() if __debug__: self._note("%s.__bootstrap(): thread started", self) if _trace_hook: self._note("%s.__bootstrap(): registering trace hook", self) _sys.settrace(_trace_hook) if _profile_hook: self._note("%s.__bootstrap(): registering profile hook", self) _sys.setprofile(_profile_hook) try: self.run() except SystemExit: if __debug__: self._note("%s.__bootstrap(): raised SystemExit", self) except: if __debug__: self._note("%s.__bootstrap(): unhandled exception", self) # If sys.stderr is no more (most likely from interpreter # shutdown) use self.__stderr. Otherwise still use sys (as in # _sys) in case sys.stderr was redefined. if _sys: _sys.stderr.write("Exception in thread %s:" % self.getName()) _print_exc(file=_sys.stderr) else: # Do the best job possible w/o a huge amt. of code to # approx. a traceback stack trace exc_type, exc_value, exc_tb = self.__exc_info() try: print>>self.__stderr, ( "Exception in thread " + self.getName() + " (most likely raised during interpreter shutdown):") print>>self.__stderr, ( "Traceback (most recent call last):") while exc_tb: print>>self.__stderr, ( ' File "%s", line %s, in %s' % (exc_tb.tb_frame.f_code.co_filename, exc_tb.tb_lineno, exc_tb.tb_frame.f_code.co_name)) exc_tb = exc_tb.tb_next print>>self.__stderr, ("%s: %s" % (exc_type, exc_value)) # Make sure that exc_tb gets deleted since it is a memory # hog; deleting everything else is just for thoroughness finally: del exc_type, exc_value, exc_tb else: if __debug__: self._note("%s.__bootstrap(): normal return", self) finally: self.__stop() try: self.__delete() except: pass
def __bootstrap(self): try: self.__started = True _active_limbo_lock.acquire() _active[_get_ident()] = self del _limbo[self] _active_limbo_lock.release() if __debug__: self._note("%s.__bootstrap(): thread started", self) if _trace_hook: self._note("%s.__bootstrap(): registering trace hook", self) _sys.settrace(_trace_hook) if _profile_hook: self._note("%s.__bootstrap(): registering profile hook", self) _sys.setprofile(_profile_hook) try: self.run() except SystemExit: if __debug__: self._note("%s.__bootstrap(): raised SystemExit", self) except: if __debug__: self._note("%s.__bootstrap(): unhandled exception", self) # If sys.stderr is no more (most likely from interpreter # shutdown) use self.__stderr. Otherwise still use sys (as in # _sys) in case sys.stderr was redefined. if _sys: _sys.stderr.write("Exception in thread %s:" % self.getName()) _print_exc(file=_sys.stderr) else: # Do the best job possible w/o a huge amt. of code to # approx. a traceback stack trace exc_type, exc_value, exc_tb = self.__exc_info() try: print >> self.__stderr, ( "Exception in thread " + self.getName() + " (most likely raised during interpreter shutdown):" ) print >> self.__stderr, ( "Traceback (most recent call last):") while exc_tb: print >> self.__stderr, ( ' File "%s", line %s, in %s' % (exc_tb.tb_frame.f_code.co_filename, exc_tb.tb_lineno, exc_tb.tb_frame.f_code.co_name)) exc_tb = exc_tb.tb_next print >> self.__stderr, ("%s: %s" % (exc_type, exc_value)) # Make sure that exc_tb gets deleted since it is a memory # hog; deleting everything else is just for thoroughness finally: del exc_type, exc_value, exc_tb else: if __debug__: self._note("%s.__bootstrap(): normal return", self) finally: self.__stop() try: self.__delete() except: pass