<s> from __future__ import with_statement <EOL> from os import sep , path <EOL> from optparse import OptionParser <EOL> import sys , logging , logging . handlers , traceback <EOL> from static import Cling <EOL> from daemon import DaemonContext <EOL> from gevent import wsgi <EOL> sys . path . append ( sep . join ( path . dirname ( path . abspath ( __file__ ) ) . split ( sep ) [ : - <NUM_LIT:1> ] ) ) <EOL> from shaveet import config , api , gc <EOL> from shaveet . utils import maxfd , setprocname <EOL> BASE_PATH = path . dirname ( path . abspath ( __file__ ) ) <EOL> logger_wsgi = logging . getLogger ( "<STR_LIT>" ) <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> static_app = Cling ( path . dirname ( path . abspath ( __file__ ) ) ) <EOL> def wsgi_main ( env , start_response ) : <EOL> try : <EOL> if env [ '<STR_LIT>' ] == '<STR_LIT:/>' : <EOL> return api . handle ( env , start_response ) <EOL> elif env [ '<STR_LIT>' ] . startswith ( '<STR_LIT>' ) : <EOL> return [ api . message_updates ( env , start_response ) ] <EOL> elif env [ '<STR_LIT>' ] . startswith ( '<STR_LIT>' ) : <EOL> return static_app ( env , start_response ) <EOL> else : <EOL> start_response ( '<STR_LIT>' , [ ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) ] ) <EOL> return '<STR_LIT>' <EOL> except GeneratorExit : <EOL> pass <EOL> except Exception , e : <EOL> start_response ( '<STR_LIT>' , [ ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) ] ) <EOL> logger . error ( traceback . format_exc ( ) ) <EOL> return '<STR_LIT>' <EOL> def main ( options ) : <EOL> server = wsgi . WSGIServer ( ( '<STR_LIT>' , options . port or config . PORT ) , wsgi_main ) <EOL> client_gc = gc . ClientGC ( ) <EOL> try : <EOL> client_gc . start ( ) <EOL> server . serve_forever ( ) <EOL> except KeyboardInterrupt : <EOL> server . stop ( ) <EOL> def cli_start ( ) : <EOL> setprocname ( "<STR_LIT>" ) <EOL> parser = OptionParser ( ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , help = "<STR_LIT>" , action = "<STR_LIT:store_true>" , default = False ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT:port>" , help = "<STR_LIT>" , type = "<STR_LIT:int>" , default = <NUM_LIT:0> ) <EOL> ( options , args ) = parser . parse_args ( ) <EOL> formatter = logging . Formatter ( "<STR_LIT>" ) <EOL> fh = logging . handlers . RotatingFileHandler ( config . LOG_PATH , maxBytes = <NUM_LIT> * <NUM_LIT> , backupCount = <NUM_LIT:3> ) <EOL> ch = logging . StreamHandler ( ) <EOL> fh . setFormatter ( formatter ) <EOL> ch . setFormatter ( formatter ) <EOL> logger_wsgi . setLevel ( logging . INFO ) <EOL> logger . setLevel ( logging . INFO ) <EOL> if options . daemon : <EOL> with DaemonContext ( files_preserve = range ( maxfd ( ) + <NUM_LIT:2> ) ) : <EOL> logger . addHandler ( fh ) <EOL> logger_wsgi . addHandler ( fh ) <EOL> logger . info ( "<STR_LIT>" ) <EOL> main ( options ) <EOL> else : <EOL> logger . setLevel ( logging . DEBUG ) <EOL> logger_wsgi . setLevel ( logging . DEBUG ) <EOL> logger . addHandler ( ch ) <EOL> logger_wsgi . addHandler ( ch ) <EOL> main ( options ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> cli_start ( ) </s>
