<s> import os <EOL> import re <EOL> import json <EOL> import xmlrpclib <EOL> from text import markdown <EOL> from util import read_file , slugify <EOL> from model import get_url_path <EOL> class MethodsLoader ( object ) : <EOL> def __init__ ( self , api_dir , application_data ) : <EOL> self . api_dir = api_dir <EOL> self . application_data = application_data <EOL> def load ( self ) : <EOL> pass <EOL> class ResourcesLoader ( MethodsLoader ) : <EOL> def __init__ ( self , api_dir , application_data , SUPPORTED_METHODS ) : <EOL> super ( ResourcesLoader , self ) . __init__ ( api_dir , application_data ) <EOL> self . SUPPORTED_METHODS = SUPPORTED_METHODS <EOL> def load ( self ) : <EOL> paths = [ self . _complete_path ( item ) <EOL> for item in os . walk ( self . api_dir ) <EOL> if self . _check_folder ( item ) ] <EOL> return paths <EOL> def _check_folder ( self , item ) : <EOL> if not item [ <NUM_LIT:2> ] : <EOL> return False <EOL> c = re . compile ( r"<STR_LIT>" % ( "<STR_LIT:|>" . join ( self . SUPPORTED_METHODS ) ) ) <EOL> for current_file in item [ <NUM_LIT:2> ] : <EOL> if c . search ( current_file ) : <EOL> return True <EOL> return False <EOL> def _complete_path ( self , item ) : <EOL> c = re . compile ( r"<STR_LIT>" % <EOL> ( "<STR_LIT:|>" . join ( self . SUPPORTED_METHODS ) ) ) <EOL> url_path = UrlPath ( get_url_path ( item [ <NUM_LIT:0> ] [ len ( self . api_dir ) : ] ) ) <EOL> for current_file in sorted ( item [ <NUM_LIT:2> ] ) : <EOL> m = c . match ( current_file ) <EOL> if m is None : <EOL> continue <EOL> method , status_code , format = m . groups ( ) <EOL> if method in url_path . resources : <EOL> url_path . resources [ method ] . files . append ( <EOL> ResourceFormatFile ( <EOL> status_code , <EOL> format , <EOL> read_file ( "<STR_LIT>" % ( item [ <NUM_LIT:0> ] , current_file ) ) ) ) <EOL> else : <EOL> resource = self . _create_resource ( <EOL> item [ <NUM_LIT:0> ] , method , url_path . path ) <EOL> resource . files . append ( <EOL> ResourceFormatFile ( <EOL> status_code , <EOL> format , <EOL> read_file ( "<STR_LIT>" % ( item [ <NUM_LIT:0> ] , current_file ) ) ) ) <EOL> url_path . resources [ method ] = resource <EOL> return url_path <EOL> def _create_resource ( self , file_path , method , url_path ) : <EOL> resource = Resource ( method , url_path ) <EOL> resource_description_path = os . path . join ( <EOL> file_path , "<STR_LIT>" % method ) <EOL> resource_description = read_file ( resource_description_path ) <EOL> if resource_description : <EOL> resource . description = resource_description <EOL> resource . upstream_server = self . application_data . get_upstream_server ( <EOL> "<STR_LIT>" % ( method , file_path [ len ( self . api_dir ) : ] ) ) <EOL> return resource <EOL> class UrlPath ( object ) : <EOL> def __init__ ( self , path ) : <EOL> self . path = path <EOL> self . id = slugify ( self . path ) <EOL> self . resources = { } <EOL> class Resource ( object ) : <EOL> def __init__ ( self , method , url_path ) : <EOL> self . method = method <EOL> self . url_path = url_path <EOL> self . id = "<STR_LIT>" % ( self . method , self . url_path ) <EOL> self . files = [ ] <EOL> self . upstream_server = False <EOL> self . _description = "<STR_LIT>" <EOL> self . plain_description = "<STR_LIT>" <EOL> @ property <EOL> def description ( self ) : <EOL> return self . _description <EOL> @ description . setter <EOL> def description ( self , value ) : <EOL> self . plain_description = value <EOL> self . _description = markdown ( value , protocol = "<STR_LIT>" , ref_id = self . id ) <EOL> class ResourceFormatFile ( object ) : <EOL> def __init__ ( self , status_code , format , data ) : <EOL> self . status_code = status_code <EOL> self . format = format <EOL> self . data = data <EOL> class RPCMethodsLoader ( MethodsLoader ) : <EOL> def __init__ ( self , api_dir , application_data , jsonrpc ) : <EOL> super ( RPCMethodsLoader , self ) . __init__ ( api_dir , application_data ) <EOL> self . jsonrpc = jsonrpc <EOL> def load ( self , path ) : <EOL> rpc_methods = [ ] <EOL> methods_dir = os . path . join ( self . api_dir , path ) <EOL> if not os . path . exists ( methods_dir ) : <EOL> return rpc_methods <EOL> for method in os . listdir ( methods_dir ) : <EOL> if method . startswith ( "<STR_LIT:.>" ) or method . endswith ( "<STR_LIT>" ) : <EOL> continue <EOL> data = read_file ( os . path . join ( methods_dir , method ) ) <EOL> responses = self . _load_responses ( method , data ) <EOL> description_path = os . path . join ( <EOL> methods_dir , "<STR_LIT>" % method ) <EOL> description = read_file ( description_path ) <EOL> if description : <EOL> description = markdown ( <EOL> description , protocol = "<STR_LIT>" , ref_id = method ) <EOL> upstream_server = self . application_data . get_rpc_upstream_server ( <EOL> method ) <EOL> rpc_methods . append ( { <EOL> "<STR_LIT:name>" : method , <EOL> "<STR_LIT:id>" : method . replace ( "<STR_LIT:.>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : responses , <EOL> "<STR_LIT:description>" : description , <EOL> "<STR_LIT>" : upstream_server <EOL> } ) <EOL> return rpc_methods <EOL> def _load_responses ( self , method , data ) : <EOL> try : <EOL> data = json . loads ( data ) <EOL> except ValueError : <EOL> data = data <EOL> responses = { } <EOL> responses [ "<STR_LIT>" ] = XMLRPCMethod ( method , data ) <EOL> if self . jsonrpc : <EOL> responses [ "<STR_LIT>" ] = JSONRPCMethod ( method , data ) <EOL> return responses <EOL> class RPCMethod ( object ) : <EOL> CONTENT_TYPE = "<STR_LIT>" <EOL> def __init__ ( self , method , data , method_call ) : <EOL> self . method = method <EOL> self . data = data <EOL> self . method_call = method_call <EOL> class XMLRPCMethod ( RPCMethod ) : <EOL> CONTENT_TYPE = "<STR_LIT>" <EOL> def __init__ ( self , method , data ) : <EOL> data = xmlrpclib . dumps ( ( data , ) , methodresponse = True ) <EOL> method_call = "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" % method <EOL> super ( XMLRPCMethod , self ) . __init__ ( method , data , method_call ) <EOL> class JSONRPCMethod ( RPCMethod ) : <EOL> CONTENT_TYPE = "<STR_LIT:application/json>" <EOL> def __init__ ( self , method , data ) : <EOL> import jsonrpclib <EOL> data = jsonrpclib . dumps ( data , methodresponse = True , rpcid = <NUM_LIT:1> ) <EOL> method_call = '<STR_LIT>' % method <EOL> super ( JSONRPCMethod , self ) . __init__ ( method , data , method_call ) </s>
