<s> import sys <EOL> import logging <EOL> import logging . config <EOL> import os <EOL> import socket <EOL> from os . path import expanduser , dirname <EOL> import tornado . ioloop <EOL> from tornado . httpserver import HTTPServer <EOL> from thumbor . console import get_server_parameters <EOL> from thumbor . config import Config <EOL> from thumbor . importer import Importer <EOL> from thumbor . context import Context <EOL> from thumbor . utils import which <EOL> def get_as_integer ( value ) : <EOL> try : <EOL> return int ( value ) <EOL> except ( ValueError , TypeError ) : <EOL> return None <EOL> def get_config ( config_path ) : <EOL> lookup_paths = [ os . curdir , <EOL> expanduser ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> dirname ( __file__ ) ] <EOL> return Config . load ( config_path , conf_name = '<STR_LIT>' , lookup_paths = lookup_paths ) <EOL> def configure_log ( config , log_level ) : <EOL> if ( config . THUMBOR_LOG_CONFIG and config . THUMBOR_LOG_CONFIG != '<STR_LIT>' ) : <EOL> logging . config . dictConfig ( config . THUMBOR_LOG_CONFIG ) <EOL> else : <EOL> logging . basicConfig ( <EOL> level = getattr ( logging , log_level ) , <EOL> format = config . THUMBOR_LOG_FORMAT , <EOL> datefmt = config . THUMBOR_LOG_DATE_FORMAT <EOL> ) <EOL> def get_importer ( config ) : <EOL> importer = Importer ( config ) <EOL> importer . import_modules ( ) <EOL> if importer . error_handler_class is not None : <EOL> importer . error_handler = importer . error_handler_class ( config ) <EOL> return importer <EOL> def validate_config ( config , server_parameters ) : <EOL> if server_parameters . security_key is None : <EOL> server_parameters . security_key = config . SECURITY_KEY <EOL> if not isinstance ( server_parameters . security_key , basestring ) : <EOL> raise RuntimeError ( <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> if config . USE_GIFSICLE_ENGINE : <EOL> server_parameters . gifsicle_path = which ( '<STR_LIT>' ) <EOL> if server_parameters . gifsicle_path is None : <EOL> raise RuntimeError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> def get_context ( server_parameters , config , importer ) : <EOL> return Context ( <EOL> server = server_parameters , <EOL> config = config , <EOL> importer = importer <EOL> ) <EOL> def get_application ( context ) : <EOL> return context . modules . importer . import_class ( context . server . app_class ) ( context ) <EOL> def run_server ( application , context ) : <EOL> server = HTTPServer ( application ) <EOL> if context . server . fd is not None : <EOL> fd_number = get_as_integer ( context . server . fd ) <EOL> if fd_number is None : <EOL> with open ( context . server . fd , '<STR_LIT:r>' ) as sock : <EOL> fd_number = sock . fileno ( ) <EOL> sock = socket . fromfd ( fd_number , <EOL> socket . AF_INET | socket . AF_INET6 , <EOL> socket . SOCK_STREAM ) <EOL> server . add_socket ( sock ) <EOL> else : <EOL> server . bind ( context . server . port , context . server . ip ) <EOL> server . start ( <NUM_LIT:1> ) <EOL> def main ( arguments = None ) : <EOL> '''<STR_LIT>''' <EOL> if arguments is None : <EOL> arguments = sys . argv [ <NUM_LIT:1> : ] <EOL> server_parameters = get_server_parameters ( arguments ) <EOL> config = get_config ( server_parameters . config_path ) <EOL> configure_log ( config , server_parameters . log_level . upper ( ) ) <EOL> importer = get_importer ( config ) <EOL> validate_config ( config , server_parameters ) <EOL> context = get_context ( server_parameters , config , importer ) <EOL> application = get_application ( context ) <EOL> run_server ( application , context ) <EOL> try : <EOL> logging . debug ( '<STR_LIT>' % ( context . server . ip , context . server . port ) ) <EOL> tornado . ioloop . IOLoop . instance ( ) . start ( ) <EOL> except KeyboardInterrupt : <EOL> sys . stdout . write ( '<STR_LIT:\n>' ) <EOL> sys . stdout . write ( "<STR_LIT>" ) <EOL> finally : <EOL> context . thread_pool . cleanup ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( sys . argv ) </s>
