<s> import HydrusExceptions <EOL> import Queue <EOL> import threading <EOL> import time <EOL> import traceback <EOL> import HydrusData <EOL> import HydrusGlobals <EOL> import HydrusThreading <EOL> import os <EOL> class JobKey ( object ) : <EOL> def __init__ ( self , pausable = False , cancellable = False , only_when_idle = False , only_start_if_unbusy = False , stop_time = None ) : <EOL> self . _key = HydrusData . GenerateKey ( ) <EOL> self . _pausable = pausable <EOL> self . _cancellable = cancellable <EOL> self . _only_when_idle = only_when_idle <EOL> self . _only_start_if_unbusy = only_start_if_unbusy <EOL> self . _stop_time = stop_time <EOL> self . _deleted = threading . Event ( ) <EOL> self . _begun = threading . Event ( ) <EOL> self . _done = threading . Event ( ) <EOL> self . _cancelled = threading . Event ( ) <EOL> self . _paused = threading . Event ( ) <EOL> self . _yield_pause_period = <NUM_LIT:10> <EOL> self . _next_yield_pause = HydrusData . GetNow ( ) + self . _yield_pause_period <EOL> self . _bigger_pause_period = <NUM_LIT:100> <EOL> self . _next_bigger_pause = HydrusData . GetNow ( ) + self . _bigger_pause_period <EOL> self . _longer_pause_period = <NUM_LIT:1000> <EOL> self . _next_longer_pause = HydrusData . GetNow ( ) + self . _longer_pause_period <EOL> self . _variable_lock = threading . Lock ( ) <EOL> self . _variables = dict ( ) <EOL> def __eq__ ( self , other ) : return self . __hash__ ( ) == other . __hash__ ( ) <EOL> def __hash__ ( self ) : return self . _key . __hash__ ( ) <EOL> def __ne__ ( self , other ) : return self . __hash__ ( ) != other . __hash__ ( ) <EOL> def _CheckCancelTests ( self ) : <EOL> if not self . _cancelled . is_set ( ) : <EOL> should_cancel = False <EOL> if HydrusThreading . IsThreadShuttingDown ( ) : <EOL> should_cancel = True <EOL> if self . _only_when_idle and not HydrusGlobals . client_controller . CurrentlyIdle ( ) : <EOL> should_cancel = True <EOL> if self . _stop_time is not None : <EOL> if HydrusData . TimeHasPassed ( self . _stop_time ) : <EOL> should_cancel = True <EOL> if should_cancel : <EOL> self . Cancel ( ) <EOL> def Begin ( self ) : self . _begun . set ( ) <EOL> def CanBegin ( self ) : <EOL> if self . IsCancelled ( ) : <EOL> return False <EOL> if self . _only_start_if_unbusy and HydrusGlobals . client_controller . SystemBusy ( ) : <EOL> return False <EOL> return True <EOL> def Cancel ( self ) : <EOL> self . _cancelled . set ( ) <EOL> self . Finish ( ) <EOL> def Delete ( self ) : <EOL> self . Finish ( ) <EOL> self . _deleted . set ( ) <EOL> def DeleteVariable ( self , name ) : <EOL> with self . _variable_lock : <EOL> if name in self . _variables : del self . _variables [ name ] <EOL> time . sleep ( <NUM_LIT> ) <EOL> def Finish ( self ) : self . _done . set ( ) <EOL> def GetKey ( self ) : return self . _key <EOL> def GetVariable ( self , name ) : <EOL> with self . _variable_lock : return self . _variables [ name ] <EOL> def HasVariable ( self , name ) : <EOL> with self . _variable_lock : return name in self . _variables <EOL> def IsBegun ( self ) : <EOL> return self . _begun . is_set ( ) <EOL> def IsCancellable ( self ) : <EOL> return self . _cancellable and not self . IsDone ( ) <EOL> def IsCancelled ( self ) : <EOL> self . _CheckCancelTests ( ) <EOL> return HydrusThreading . IsThreadShuttingDown ( ) or self . _cancelled . is_set ( ) <EOL> def IsDeleted ( self ) : <EOL> self . _CheckCancelTests ( ) <EOL> return HydrusThreading . IsThreadShuttingDown ( ) or self . _deleted . is_set ( ) <EOL> def IsDone ( self ) : <EOL> return HydrusThreading . IsThreadShuttingDown ( ) or self . _done . is_set ( ) <EOL> def IsPausable ( self ) : return self . _pausable and not self . IsDone ( ) <EOL> def IsPaused ( self ) : return self . _paused . is_set ( ) and not self . IsDone ( ) <EOL> def IsWorking ( self ) : return self . IsBegun ( ) and not self . IsDone ( ) <EOL> def PausePlay ( self ) : <EOL> if self . _paused . is_set ( ) : self . _paused . clear ( ) <EOL> else : self . _paused . set ( ) <EOL> def SetCancellable ( self , value ) : self . _cancellable = value <EOL> def SetPausable ( self , value ) : self . _pausable = value <EOL> def SetVariable ( self , name , value ) : <EOL> with self . _variable_lock : self . _variables [ name ] = value <EOL> time . sleep ( <NUM_LIT> ) <EOL> def ToString ( self ) : <EOL> stuff_to_print = [ ] <EOL> with self . _variable_lock : <EOL> if '<STR_LIT>' in self . _variables : stuff_to_print . append ( self . _variables [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in self . _variables : stuff_to_print . append ( self . _variables [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in self . _variables : stuff_to_print . append ( self . _variables [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in self . _variables : stuff_to_print . append ( self . _variables [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in self . _variables : stuff_to_print . append ( self . _variables [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in self . _variables : stuff_to_print . append ( self . _variables [ '<STR_LIT>' ] ) <EOL> stuff_to_print = [ HydrusData . ToUnicode ( s ) for s in stuff_to_print ] <EOL> try : <EOL> return os . linesep . join ( stuff_to_print ) <EOL> except : <EOL> return repr ( stuff_to_print ) <EOL> def WaitIfNeeded ( self ) : <EOL> if HydrusData . TimeHasPassed ( self . _next_yield_pause ) : <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> self . _next_yield_pause = HydrusData . GetNow ( ) + self . _yield_pause_period <EOL> if HydrusData . TimeHasPassed ( self . _next_bigger_pause ) : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> self . _next_bigger_pause = HydrusData . GetNow ( ) + self . _bigger_pause_period <EOL> if HydrusData . TimeHasPassed ( self . _longer_pause_period ) : <EOL> time . sleep ( <NUM_LIT:10> ) <EOL> self . _next_longer_pause = HydrusData . GetNow ( ) + self . _longer_pause_period <EOL> i_paused = False <EOL> should_quit = False <EOL> while self . IsPaused ( ) : <EOL> i_paused = True <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> if self . IsDone ( ) : <EOL> break <EOL> if self . IsCancelled ( ) : <EOL> should_quit = True <EOL> return ( i_paused , should_quit ) </s>
