<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import abc <EOL> import logging <EOL> from multiprocessing . connection import Listener , Client <EOL> import os <EOL> import socket <EOL> from threading import Thread <EOL> from chalmers import errors <EOL> from chalmers import config <EOL> import sys <EOL> log = logging . getLogger ( __name__ ) <EOL> try : <EOL> WindowsError <EOL> except NameError : <EOL> WindowsError = SystemError <EOL> try : <EOL> basestring <EOL> except NameError : <EOL> basestring = str <EOL> def get_addr ( name ) : <EOL> """<STR_LIT>""" <EOL> if os . name == '<STR_LIT>' : <EOL> return r'<STR_LIT>' % name <EOL> else : <EOL> socdir = os . path . join ( config . dirs . user_data_dir , '<STR_LIT>' ) <EOL> if not os . path . isdir ( socdir ) : <EOL> os . makedirs ( socdir ) <EOL> sock_path = os . path . join ( socdir , '<STR_LIT:%s>' % name ) <EOL> if sys . version_info . major < <NUM_LIT:3> : <EOL> sock_path = sock_path . encode ( ) <EOL> return sock_path <EOL> class EventDispatcher ( object ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = abc . ABCMeta <EOL> FAMILY = '<STR_LIT>' if os . name == '<STR_LIT>' else '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> self . _running = False <EOL> self . _listener = None <EOL> @ property <EOL> def listener ( self ) : <EOL> "<STR_LIT>" <EOL> if self . _listener is None : <EOL> if self . FAMILY == '<STR_LIT>' and os . path . exists ( self . addr ) : <EOL> os . unlink ( self . addr ) <EOL> try : <EOL> log . debug ( "<STR_LIT>" % self . addr ) <EOL> self . _listener = Listener ( self . addr , family = self . FAMILY ) <EOL> except socket . error as err : <EOL> if err . errno == <NUM_LIT> : <EOL> msg = "<STR_LIT>" <EOL> raise errors . ChalmersError ( msg % self . addr ) <EOL> else : <EOL> raise <EOL> return self . _listener <EOL> def start_listener ( self ) : <EOL> "<STR_LIT>" <EOL> self . _running = True <EOL> self . listener <EOL> self . _listener_thread = Thread ( target = self . listen ) <EOL> self . _listener_thread . start ( ) <EOL> @ abc . abstractproperty <EOL> def name ( self ) : <EOL> return '<STR_LIT>' <EOL> @ property <EOL> def addr ( self ) : <EOL> return get_addr ( self . name ) <EOL> def dispatch_exitloop ( self ) : <EOL> '<STR_LIT>' <EOL> self . _running = False <EOL> def dispatch_ping ( self ) : <EOL> '<STR_LIT>' <EOL> return os . getpid ( ) <EOL> @ property <EOL> def is_listening ( self ) : <EOL> return self . _running <EOL> def listen ( self ) : <EOL> """<STR_LIT>""" <EOL> l = self . listener <EOL> self . _running = True <EOL> try : <EOL> while self . _running : <EOL> log . debug ( "<STR_LIT>" ) <EOL> c = l . accept ( ) <EOL> try : <EOL> action = c . recv ( ) <EOL> except EOFError : <EOL> c . close ( ) <EOL> continue <EOL> if isinstance ( action , basestring ) : <EOL> args = ( ) <EOL> kwargs = { } <EOL> else : <EOL> args = action . get ( '<STR_LIT:args>' , ( ) ) <EOL> kwargs = action . get ( '<STR_LIT>' ) or { } <EOL> action = action . get ( '<STR_LIT:action>' ) <EOL> log . info ( '<STR_LIT>' % action ) <EOL> method = getattr ( self , '<STR_LIT>' % action , None ) <EOL> if method : <EOL> try : <EOL> result = method ( * args , ** kwargs ) <EOL> except Exception as err : <EOL> log . exception ( err ) <EOL> c . send ( { '<STR_LIT:error>' : True , '<STR_LIT:message>' : '<STR_LIT>' % ( action , err ) } ) <EOL> else : <EOL> c . send ( { '<STR_LIT:error>' : False , '<STR_LIT:message>' : '<STR_LIT>' , '<STR_LIT:result>' : result } ) <EOL> else : <EOL> log . warn ( '<STR_LIT>' % action ) <EOL> c . send ( { '<STR_LIT:error>' : True , '<STR_LIT:message>' : '<STR_LIT>' % action } ) <EOL> c . close ( ) <EOL> finally : <EOL> self . _listener = None <EOL> l . close ( ) <EOL> log . info ( "<STR_LIT>" ) <EOL> def send ( self , action , * args , ** kwargs ) : <EOL> '<STR_LIT>' <EOL> return send_action ( self . name , action , * args , ** kwargs ) <EOL> def send_action ( name , action , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> addr = get_addr ( name ) <EOL> try : <EOL> c = Client ( addr , family = EventDispatcher . FAMILY ) <EOL> except ( socket . error , WindowsError ) : <EOL> raise errors . ConnectionError ( "<STR_LIT>" % name ) <EOL> try : <EOL> c . send ( { '<STR_LIT:action>' : action , '<STR_LIT:args>' : args , '<STR_LIT>' : kwargs } ) <EOL> res = c . recv ( ) <EOL> if res . get ( '<STR_LIT:error>' ) : <EOL> raise errors . ChalmersError ( res . get ( '<STR_LIT:message>' , '<STR_LIT>' ) ) <EOL> return res . get ( '<STR_LIT:result>' ) <EOL> finally : <EOL> c . close ( ) </s>
