<s> import os <EOL> import peewee <EOL> import logging <EOL> import datetime <EOL> from importlib import import_module <EOL> from . parameter import Param <EOL> from . utility import set_logging , class_name_to_api_name , get_cls_with_path , iterate_package , rst_to_html <EOL> from . web_ui import setup_web_ui <EOL> from . web_ui . log_ui import LogUI <EOL> from . web_ui . test_ui import TestsStandaloneUI <EOL> log = logging . getLogger ( __name__ ) <EOL> def http_api_setup ( app , config , log_ui_class = LogUI , test_ui_class = TestsStandaloneUI ) : <EOL> workspace = config [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> if os . path . exists ( workspace ) : <EOL> workspace = os . path . realpath ( workspace ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' % os . path . realpath ( workspace ) ) <EOL> if config . get ( '<STR_LIT>' ) : <EOL> set_logging ( config [ '<STR_LIT>' ] , workspace ) <EOL> if config . get ( '<STR_LIT>' ) : <EOL> setup_database ( config [ '<STR_LIT>' ] ) <EOL> setup_app ( app , config ) <EOL> parameter_types ( app , config [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> setup_view ( app , config [ '<STR_LIT>' ] . get ( '<STR_LIT>' , ( ) ) ) <EOL> setup_web_ui ( app , config [ '<STR_LIT>' ] [ '<STR_LIT>' ] , workspace , log_ui_class , test_ui_class ) <EOL> return workspace <EOL> db_classes = { <EOL> '<STR_LIT>' : peewee . MySQLDatabase , <EOL> '<STR_LIT>' : peewee . PostgresqlDatabase , <EOL> '<STR_LIT>' : peewee . SqliteDatabase <EOL> } <EOL> db = { } <EOL> def setup_database ( config ) : <EOL> """<STR_LIT>""" <EOL> global db <EOL> for database in config : <EOL> db [ database [ '<STR_LIT:key>' ] ] = db_classes [ database [ '<STR_LIT:type>' ] ] ( ** config [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> peewee . Model . _meta . database = db . get ( '<STR_LIT:default>' ) or db [ <NUM_LIT:0> ] <EOL> def setup_app ( app , config ) : <EOL> """<STR_LIT>""" <EOL> flask_config = config . get ( '<STR_LIT>' ) or { } <EOL> for key , value in flask_config . items ( ) : <EOL> if key == '<STR_LIT>' : <EOL> app . config [ key ] = datetime . timedelta ( days = int ( value ) ) <EOL> else : <EOL> app . config [ key ] = value <EOL> app_config = config . get ( '<STR_LIT>' ) or { } <EOL> for key , value in app_config . items ( ) : <EOL> app . config [ key ] = value <EOL> app . config [ '<STR_LIT>' ] = app . config . get ( '<STR_LIT>' ) or { } <EOL> for name , config in app . config [ '<STR_LIT>' ] . items ( ) : <EOL> plugin_class = get_cls_with_path ( config . pop ( '<STR_LIT:class>' ) ) <EOL> plugin = plugin_class ( config ) <EOL> app . config [ '<STR_LIT>' ] [ name ] = plugin <EOL> default_class_sign = '<STR_LIT>' <EOL> app . config [ '<STR_LIT>' ] = app . config . get ( '<STR_LIT>' ) or { } <EOL> for name , class_path in app . config [ '<STR_LIT>' ] . items ( ) : <EOL> if name == '<STR_LIT:default>' : <EOL> default_class_sign = class_path <EOL> else : <EOL> response_class = get_cls_with_path ( class_path ) <EOL> app . config [ '<STR_LIT>' ] [ name ] = response_class <EOL> app . config [ '<STR_LIT>' ] [ '<STR_LIT:default>' ] = app . config [ '<STR_LIT>' ] [ default_class_sign ] <EOL> def parameter_types ( app , config ) : <EOL> types = { } <EOL> for package_name in config : <EOL> exec ( '<STR_LIT>' % package_name ) <EOL> parameter_package = locals ( ) [ '<STR_LIT>' ] <EOL> for item in dir ( parameter_package ) : <EOL> if item in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> continue <EOL> parameter = getattr ( parameter_package , item ) <EOL> try : <EOL> if issubclass ( parameter , Param ) : <EOL> if parameter . __name__ not in types : <EOL> types [ parameter . __name__ ] = parameter <EOL> except TypeError : <EOL> pass <EOL> app . config [ '<STR_LIT>' ] = types <EOL> def setup_view ( app , config ) : <EOL> """<STR_LIT>""" <EOL> from . view import CView <EOL> app . jinja_env . globals . update ( rst_to_html = rst_to_html ) <EOL> for package_path in config : <EOL> iterate_package ( import_module ( package_path ) ) <EOL> for view in CView . __subclasses__ ( ) : <EOL> name = class_name_to_api_name ( view . __name__ ) <EOL> uri = '<STR_LIT>' % ( view . __module__ . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] , name ) <EOL> endpoint = '<STR_LIT>' % ( view . __module__ . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] , name ) <EOL> app . add_url_rule ( uri , view_func = view . as_view ( endpoint , uri , app ) ) </s>
