"""Class in charge of managing the SPARQL Endpoints"""
from SPARQLWrapper import SPARQLWrapper, JSON


class Singleton(object):
    """Base singleton class"""
    _instance = None

    def __new__(cls, *args, **kwargs):
        if not isinstance(cls._instance, cls):
            cls._instance = object.__new__(cls, *args, **kwargs)
        return cls._instance


class SparqlEndpoint(Singleton):
    """Class in charge of managing the SPARQL Endpoints"""
    endpoints = {}
    settings = {}

    def __init__(self, settings):
        self.settings = settings
        for k, e in self.settings["endpoints"].iteritems():
            self.endpoints[k] = SPARQLWrapper(e)

    def query(self, q, thisEndpoint="local"):
        sparql = self.endpoints[thisEndpoint]
        sparql.setQuery(q)
        sparql.setReturnFormat(JSON)
        try:
            return sparql.query().convert()
        except:
            print "SparqlEndpoint problem"
            return None
@@ -94,16 +94,27 @@
 r, JSON%0A
+import sys%0A
 %0A%0Aclass 
@@ -857,20 +857,23 @@
       re
-turn
+sults =
  sparql.
@@ -890,16 +890,526 @@
 nvert()%0A
+            try:%0A                if self.settings%5B%22mirrored%22%5D is True:%0A                    for row in results%5B%22results%22%5D%5B%22bindings%22%5D:%0A                        for elem in row:%0A                            if row%5Belem%5D%5B%22type%22%5D == %22uri%22:%0A                                row%5Belem%5D%5B%22value%22%5D = row%5Belem%5D%5B%22value%22%5D.replace(self.settings%5B'ns'%5D%5B'origin'%5D, self.settings%5B'ns'%5D%5B'local'%5D, 1)%0A            except:%0A                print sys.exc_info()%0A                print %22Error iterating results%22%0A            return results%0A
         

Support mirroring