<s> import inspect <EOL> import logging <EOL> import os <EOL> import os . path as osp <EOL> import sys <EOL> import gevent . monkey <EOL> gevent . monkey . patch_all ( ) <EOL> import gevent . pool <EOL> from gevent_zeromq import zmq <EOL> from . log import config_logger <EOL> from . . zmq import Socket , MultiSocket , RpcServer <EOL> from config . app import Configuration <EOL> def job ( method ) : <EOL> method . job = True <EOL> return method <EOL> class BaseModule ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , context = None ) : <EOL> self . conf = Configuration ( ) <EOL> self . name = osp . basename ( osp . dirname ( osp . abspath ( sys . argv [ <NUM_LIT:0> ] ) ) ) <EOL> self . pidfile = "<STR_LIT>" % self . name <EOL> config_logger ( "<STR_LIT>" % self . name , self . conf . log_level ) <EOL> self . context = context or zmq . Context . instance ( ) <EOL> self . jobs = gevent . pool . Group ( ) <EOL> self . rpc = RpcServer ( self , "<STR_LIT>" % self . name ) <EOL> self . rpc . run ( ) <EOL> bases = self . __class__ . __mro__ <EOL> for base in bases : <EOL> for func in base . __dict__ . values ( ) : <EOL> if getattr ( func , '<STR_LIT>' , None ) : <EOL> method = func . __get__ ( self , self . __class__ ) <EOL> self . jobs . spawn ( method ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> if os . access ( self . pidfile , os . F_OK ) : <EOL> pid = open ( self . pidfile , '<STR_LIT:r>' ) . readline ( ) <EOL> if osp . exists ( '<STR_LIT>' + pid ) : <EOL> logging . warn ( "<STR_LIT>" % ( self . name , pid ) ) <EOL> return <EOL> else : <EOL> logging . warn ( "<STR_LIT>" % self . name ) <EOL> os . remove ( self . pidfile ) <EOL> with self : <EOL> try : <EOL> self . jobs . join ( ) <EOL> except KeyboardInterrupt : <EOL> self . jobs . kill ( ) <EOL> def sock ( self , name , _type = None ) : <EOL> """<STR_LIT>""" <EOL> if _type : <EOL> return Socket ( name , _type ) <EOL> else : <EOL> return Socket ( name ) <EOL> def multisock ( self , name , _type = None ) : <EOL> """<STR_LIT>""" <EOL> if _type : <EOL> return MultiSocket ( name , _type ) <EOL> else : <EOL> return MultiSocket ( name ) <EOL> def __enter__ ( self ) : <EOL> logging . status ( "<STR_LIT>" % self . name ) <EOL> open ( self . pidfile , '<STR_LIT:w>' ) . write ( str ( os . getpid ( ) ) ) <EOL> return self <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> logging . status ( "<STR_LIT>" % self . name ) <EOL> os . remove ( self . pidfile ) <EOL> return False <EOL> def help ( self ) : <EOL> methods = [ ] <EOL> for name in dir ( self ) : <EOL> obj = getattr ( self , name ) <EOL> if inspect . ismethod ( obj ) : <EOL> if getattr ( obj , '<STR_LIT>' , False ) : <EOL> spec = inspect . getargspec ( obj ) <EOL> doc = inspect . getdoc ( obj ) <EOL> if doc : doc = doc . strip ( ) <EOL> methods . append ( ( name , inspect . formatargspec ( * spec ) , doc ) ) <EOL> else : <EOL> if not name . startswith ( '<STR_LIT:_>' ) : <EOL> methods . append ( name ) <EOL> return methods <EOL> def notice ( self , msg ) : <EOL> pass <EOL> def alert ( self , msg ) : <EOL> pass <EOL> def error ( self , msg ) : <EOL> pass </s>
