<s> """<STR_LIT>""" <EOL> import os <EOL> import win32api <EOL> import win32con <EOL> import win32event <EOL> import win32service <EOL> import win32serviceutil <EOL> from cherrypy . process import wspbus , plugins <EOL> class ConsoleCtrlHandler ( plugins . SimplePlugin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , bus ) : <EOL> self . is_set = False <EOL> plugins . SimplePlugin . __init__ ( self , bus ) <EOL> def start ( self ) : <EOL> if self . is_set : <EOL> self . bus . log ( '<STR_LIT>' , level = <NUM_LIT> ) <EOL> return <EOL> result = win32api . SetConsoleCtrlHandler ( self . handle , <NUM_LIT:1> ) <EOL> if result == <NUM_LIT:0> : <EOL> self . bus . log ( '<STR_LIT>' % <EOL> win32api . GetLastError ( ) , level = <NUM_LIT> ) <EOL> else : <EOL> self . bus . log ( '<STR_LIT>' , level = <NUM_LIT> ) <EOL> self . is_set = True <EOL> def stop ( self ) : <EOL> if not self . is_set : <EOL> self . bus . log ( '<STR_LIT>' , level = <NUM_LIT> ) <EOL> return <EOL> try : <EOL> result = win32api . SetConsoleCtrlHandler ( self . handle , <NUM_LIT:0> ) <EOL> except ValueError : <EOL> result = <NUM_LIT:1> <EOL> if result == <NUM_LIT:0> : <EOL> self . bus . log ( '<STR_LIT>' % <EOL> win32api . GetLastError ( ) , level = <NUM_LIT> ) <EOL> else : <EOL> self . bus . log ( '<STR_LIT>' , level = <NUM_LIT> ) <EOL> self . is_set = False <EOL> def handle ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if event in ( win32con . CTRL_C_EVENT , win32con . CTRL_LOGOFF_EVENT , <EOL> win32con . CTRL_BREAK_EVENT , win32con . CTRL_SHUTDOWN_EVENT , <EOL> win32con . CTRL_CLOSE_EVENT ) : <EOL> self . bus . log ( '<STR_LIT>' % event ) <EOL> try : <EOL> self . stop ( ) <EOL> except ValueError : <EOL> pass <EOL> self . bus . exit ( ) <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> class Win32Bus ( wspbus . Bus ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . events = { } <EOL> wspbus . Bus . __init__ ( self ) <EOL> def _get_state_event ( self , state ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . events [ state ] <EOL> except KeyError : <EOL> event = win32event . CreateEvent ( None , <NUM_LIT:0> , <NUM_LIT:0> , <EOL> "<STR_LIT>" % <EOL> ( state . name , os . getpid ( ) ) ) <EOL> self . events [ state ] = event <EOL> return event <EOL> def _get_state ( self ) : <EOL> return self . _state <EOL> def _set_state ( self , value ) : <EOL> self . _state = value <EOL> event = self . _get_state_event ( value ) <EOL> win32event . PulseEvent ( event ) <EOL> state = property ( _get_state , _set_state ) <EOL> def wait ( self , state , interval = <NUM_LIT:0.1> , channel = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( state , ( tuple , list ) ) : <EOL> if self . state not in state : <EOL> events = tuple ( [ self . _get_state_event ( s ) for s in state ] ) <EOL> win32event . WaitForMultipleObjects ( events , <NUM_LIT:0> , win32event . INFINITE ) <EOL> else : <EOL> if self . state != state : <EOL> event = self . _get_state_event ( state ) <EOL> win32event . WaitForSingleObject ( event , win32event . INFINITE ) <EOL> class _ControlCodes ( dict ) : <EOL> """<STR_LIT>""" <EOL> def key_for ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> for key , val in self . items ( ) : <EOL> if val is obj : <EOL> return key <EOL> raise ValueError ( "<STR_LIT>" % obj ) <EOL> control_codes = _ControlCodes ( { '<STR_LIT>' : <NUM_LIT> } ) <EOL> def signal_child ( service , command ) : <EOL> if command == '<STR_LIT>' : <EOL> win32serviceutil . StopService ( service ) <EOL> elif command == '<STR_LIT>' : <EOL> win32serviceutil . RestartService ( service ) <EOL> else : <EOL> win32serviceutil . ControlService ( service , control_codes [ command ] ) <EOL> class PyWebService ( win32serviceutil . ServiceFramework ) : <EOL> """<STR_LIT>""" <EOL> _svc_name_ = "<STR_LIT>" <EOL> _svc_display_name_ = "<STR_LIT>" <EOL> _svc_deps_ = None <EOL> _exe_name_ = "<STR_LIT>" <EOL> _exe_args_ = None <EOL> _svc_description_ = "<STR_LIT>" <EOL> def SvcDoRun ( self ) : <EOL> from cherrypy import process <EOL> process . bus . start ( ) <EOL> process . bus . block ( ) <EOL> def SvcStop ( self ) : <EOL> from cherrypy import process <EOL> self . ReportServiceStatus ( win32service . SERVICE_STOP_PENDING ) <EOL> process . bus . exit ( ) <EOL> def SvcOther ( self , control ) : <EOL> process . bus . publish ( control_codes . key_for ( control ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> win32serviceutil . HandleCommandLine ( PyWebService ) </s>
