# Path: smart/lib/utils.py
# def url_request(url,  method, headers, data=None):
#     req = url_request_build(url,  method, headers, data)
#     return url_request_execute(req)
# 
# class URLFetchException(Exception):
#     def __init__(self, status, body=""):
#         self.status = status
#         self.body = body
# 
# Path: smart/models/ontology_url_patterns.py
# class CallMapper(object):
#     __mapper_registry = set()
# 
#     @classmethod
#     def map_call(cls, call):
#         potential_maps = {}
#         for m_class in cls.__mapper_registry:
#             m = m_class(call)  # instantiate the mapper
#             precedence = m.map_score
#             if precedence > 0:
#                 potential_maps[m] = precedence
# 
#         in_order = sorted(
#             potential_maps.keys(),
#             key=lambda x: potential_maps[x]
#         )
#         #print call.client_method_name, in_order
#         return in_order[-1]
# 
#     @classmethod
#     def register(cls, *new_registrant, **options):
#         if new_registrant:
#             cls.__mapper_registry.add(new_registrant[0])
#             return new_registrant
# 
#         http_method = options.pop('method', None)
#         category = options.pop('category', None)
#         cardinality = options.pop('cardinality', None)
#         client_method_name = options.pop('client_method_name', None)
#         target = options.pop('target', None)
#         filter_func = options.pop('filter_func', None)
#         path = options.pop('path', None)
# 
#         assert http_method or category or cardinality or client_method_name or target or path, "Can't define a call mapping with no parameters"
# 
#         def ret(single_func):
#             class SingleMethodMatcher(BasicCallMapper):
#                 @property
#                 def maps_p(self):
#                     return  ((not http_method or str(self.call.htt_method) == http_method) and
#                              (not category or str(self.call.category) == category) and
#                              (not target or str(self.call.target) == target) and
#                              (not path or str(self.call.path) == path) and
#                              (not client_method_name or str(self.call.client_method_name) == client_method_name) and
#                              (not filter_func or filter_func(self.call)))
#                 maps_to = staticmethod(single_func)
#             cls.__mapper_registry.add(SingleMethodMatcher)
#             return single_func
#         return ret
# 
# class BasicCallMapper(object):
#     arguments = {}
# 
#     def __init__(self, call):
#         self.call = call
# 
#     @property
#     def map_score(self):
#         if self.maps_p:
#             return 100
#         return 0


from django.conf import settings
from smart.common.rdf_tools.util import remap_node, parse_rdf, LookupType
from smart.common.rdf_tools.rdf_ontology import api_types, api_calls, ontology
from smart.lib.utils import url_request, URLFetchException
from smart.models.rdf_rest_operations import *
from smart.models.ontology_url_patterns import CallMapper, BasicCallMapper
from email.mime.text import MIMEText
import smtplib

def proxy_get(request, *args, **kwargs):
    logmsg = " ".join("proxying request", request.path, args, kwargs)
    print logmsg
    url = settings.PROXY_BASE + request.path    
    try:
        ret = url_request(url, "GET", {})
        return rdf_response(ret)
    except URLFetchException as e:
    
        if (settings.PROXY_ERROR_NOTIFICATION):

# Path: smart/lib/utils.py
# def url_request(url,  method, headers, data=None):
#     req = url_request_build(url,  method, headers, data)
#     return url_request_execute(req)
# 
# class URLFetchException(Exception):
#     def __init__(self, status, body=""):
#         self.status = status
#         self.body = body
# 
# Path: smart/models/ontology_url_patterns.py
# class CallMapper(object):
#     __mapper_registry = set()
# 
#     @classmethod
#     def map_call(cls, call):
#         potential_maps = {}
#         for m_class in cls.__mapper_registry:
#             m = m_class(call)  # instantiate the mapper
#             precedence = m.map_score
#             if precedence > 0:
#                 potential_maps[m] = precedence
# 
#         in_order = sorted(
#             potential_maps.keys(),
#             key=lambda x: potential_maps[x]
#         )
#         #print call.client_method_name, in_order
#         return in_order[-1]
# 
#     @classmethod
#     def register(cls, *new_registrant, **options):
#         if new_registrant:
#             cls.__mapper_registry.add(new_registrant[0])
#             return new_registrant
# 
#         http_method = options.pop('method', None)
#         category = options.pop('category', None)
#         cardinality = options.pop('cardinality', None)
#         client_method_name = options.pop('client_method_name', None)
#         target = options.pop('target', None)
#         filter_func = options.pop('filter_func', None)
#         path = options.pop('path', None)
# 
#         assert http_method or category or cardinality or client_method_name or target or path, "Can't define a call mapping with no parameters"
# 
#         def ret(single_func):
#             class SingleMethodMatcher(BasicCallMapper):
#                 @property
#                 def maps_p(self):
#                     return  ((not http_method or str(self.call.htt_method) == http_method) and
#                              (not category or str(self.call.category) == category) and
#                              (not target or str(self.call.target) == target) and
#                              (not path or str(self.call.path) == path) and
#                              (not client_method_name or str(self.call.client_method_name) == client_method_name) and
#                              (not filter_func or filter_func(self.call)))
#                 maps_to = staticmethod(single_func)
#             cls.__mapper_registry.add(SingleMethodMatcher)
#             return single_func
#         return ret
# 
# class BasicCallMapper(object):
#     arguments = {}
# 
#     def __init__(self, call):
#         self.call = call
# 
#     @property
#     def map_score(self):
#         if self.maps_p:
#             return 100
#         return 0


# Path: smart/plugins/record_proxy_backend.py
from django.conf import settings
from smart.common.rdf_tools.util import remap_node, parse_rdf, LookupType
from smart.common.rdf_tools.rdf_ontology import api_types, api_calls, ontology
from smart.lib.utils import url_request, URLFetchException
from smart.models.rdf_rest_operations import *
from smart.models.ontology_url_patterns import CallMapper, BasicCallMapper
from email.mime.text import MIMEText
import smtplib


def proxy_get(request, *args, **kwargs):
    logmsg = " ".join("proxying request", request.path, args, kwargs)
    print logmsg
    url = settings.PROXY_BASE + request.path    
    try:
        ret = url_request(url, "GET", {})
        return rdf_response(ret)
    except URLFetchException as e:
    
        if (settings.PROXY_ERROR_NOTIFICATION):

            me = settings.PROXY_NOTIFICATION_FROM