<s> import venusian <EOL> from flowy . config import Restart <EOL> from flowy . result import SuspendTask <EOL> from flowy . result import TaskError <EOL> from flowy . utils import logger <EOL> from flowy . utils import caller_package <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class Worker ( object ) : <EOL> """<STR_LIT>""" <EOL> categories = [ ] <EOL> def __init__ ( self ) : <EOL> self . registry = { } <EOL> def register ( self , config , func , key = None ) : <EOL> """<STR_LIT>""" <EOL> config . register ( self , key , func ) <EOL> def register_task ( self , key , wrapped_func ) : <EOL> """<STR_LIT>""" <EOL> if key in self . registry : <EOL> raise ValueError ( '<STR_LIT>' % ( key , ) ) <EOL> self . registry [ key ] = wrapped_func <EOL> def __call__ ( self , key , input_data , decision , * extra_args ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> wrapped_func = self . registry [ key ] <EOL> except KeyError : <EOL> logger . error ( "<STR_LIT>" , ( key , ) ) <EOL> return <EOL> try : <EOL> serialized_result = wrapped_func ( input_data , * extra_args ) <EOL> except SuspendTask : <EOL> decision . flush ( ) <EOL> except TaskError as e : <EOL> logger . exception ( '<STR_LIT>' ) <EOL> decision . fail ( e ) <EOL> except Restart as e : <EOL> decision . restart ( e . input_data ) <EOL> except Exception as e : <EOL> logger . exception ( '<STR_LIT>' ) <EOL> decision . fail ( e ) <EOL> else : <EOL> decision . finish ( serialized_result ) <EOL> def scan ( self , categories = None , package = None , ignore = None , level = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if categories is None : <EOL> categories = self . categories <EOL> scanner = self . make_scanner ( ) <EOL> if package is None : <EOL> package = caller_package ( level = <NUM_LIT:2> + level ) <EOL> scanner . scan ( package , categories = categories , ignore = ignore ) <EOL> def make_scanner ( self ) : <EOL> return venusian . Scanner ( register_task = self . register_task ) <EOL> def __repr__ ( self ) : <EOL> klass = self . __class__ . __name__ <EOL> max_entries = <NUM_LIT:5> <EOL> entries = sorted ( self . registry . values ( ) ) <EOL> more_entries = len ( entries ) - max_entries <EOL> if more_entries > <NUM_LIT:0> : <EOL> entries = entries [ : max_entries ] <EOL> return '<STR_LIT>' % ( klass , entries , more_entries ) <EOL> return '<STR_LIT>' % ( klass , entries ) </s>
