# Path: duckpond/data/sparql.py
# class SPARQL:
# 
#    def __init__(self):
#       self.statement = io.StringIO()
# 
#    def start(self,prefixes):
#       for key in prefixes:
#          self.statement.write('prefix {}: {}\n'.format(key,uri(prefixes[key])))
#       return self
# 
#    def construct(self,names):
#       self.statement.write('construct {')
#       for name in names:
#          write_var(self.statement,name)
#          self.statement.write(' ')
#       self.statement.write('}\n')
#       return self
# 
#    def select(self,names):
#       self.statement.write('select ')
#       for name in names:
#          write_var(self.statement,name)
#          self.statement.write(' ')
#       self.statement.write('\n')
#       return self
# 
#    def fromGraphs(self,graphs):
#       for graph in graphs:
#          self.statement.write('from {0}\n'.format(uri(graph)))
#       return self
# 
#    def fromNameGraphs(self,graphs):
#       for graph in graphs:
#          self.statement.write('from named {0}\n'.format(uri(graph)))
#       return self
# 
#    def withGraph(self,graph):
#       self.statement.write("with {0}\n".format(uri(graph)))
#       return self
# 
#    def where(self,*expressions):
#       self.statement.write('where {')
#       for expression in expressions:
#          self.statement.write(str(expression))
#          self.statement.write('\n')
#       self.statement.write('}\n')
#       return self
# 
#    def group(self,*expressions):
#       self.statement.write('{')
#       for expression in expressions:
#          self.statement.write(str(expression))
#          self.statement.write('\n')
#       self.statement.write('}\n')
#       return self
# 
#    def insert(self,*expressions):
#       self.statement.write('insert data {')
#       for expression in expressions:
#          self.statement.write(str(expression))
#          self.statement.write('\n')
#       self.statement.write('}\n')
#       return self
# 
#    def delete(self,*expressions):
#       self.statement.write('delete data {')
#       for expression in expressions:
#          self.statement.write(str(expression))
#          self.statement.write('\n')
#       self.statement.write('}\n')
#       return self
# 
#    def anyGraph(self,*expressions):
#       self.statement.write('graph ?g {')
#       for expression in expressions:
#          self.statement.write(str(expression))
#          self.statement.write('\n')
#       self.statement.write('}\n')
#       return self
# 
#    def forGraph(self,graph,*expressions):
#       self.statement.write('graph {0} {'.format(uri(graph)))
#       for expression in expressions:
#          self.statement.write(str(expression))
#          self.statement.write('\n')
#       self.statement.write('}\n')
#       return self
# 
#    def newline(self):
#       self.statement.write('\n')
#       return self;
# 
#    def orderBy(self,*expressions):
#       self.statement.write('order by ')
#       first = True
#       for expression in expressions:
#          if not first:
#             self.statement.write(',')
#          self.statement.write(str(expression))
#       self.statement.write('\n')
#       return self
# 
#    def groupBy(self,expression):
#       self.statement.write('group by ')
#       self.statement.write(expression)
#       self.statement.write('\n')
#       return self
# 
#    def __str__(self):
#       return self.statement.getvalue()
# 
# class var:
#    def __init__(self,value):
#       self.name = value
#    def __str__(self):
#       return '?'+str(self.name)
# 
# class inline_expr:
#    def __init__(self,value):
#       self.expr = value
#    def __str__(self):
#       return '(' + str(self.expr) + ')'


import requests
import json,io,os
import logging
import urllib
from enum import Enum
from .sparql import SPARQL, var, inline_expr
         self.logger.debug(str(q))
      params = self.mergeParameters({'limit':1,'query':str(q)})

      req = requests.get(self.service,params=params,headers={'accept':'application/json'},auth=self.serviceAuth)

      if (req.status_code>=200 or req.status_code<300):
         #print(req.text)
         data = json.loads(req.text)
         if len(data['values']) == 0:
            return None
         subject = strip(data['values'][0][0])
         title = literal(data['values'][0][1])
         summary = literal(data['values'][0][2]) if data['values'][0][2] is not None else None
         dateTime = strip(data['values'][0][3])
         nameValue = strip(data['values'][0][4])
         genreValue = strip(data['values'][0][5])
         date,time = dateTime.split('T') # Need to check that is xsd:dateTime
         return (subject,title,summary,date,time,nameValue,genreValue)
      else:
         raise IOError('Cannot post data to uri <{}>, status={}'.format(self.service,req.status_code))

   def getEntityByName(self,value,name=None,title=None,order=None,summary=None,criteria=None,genre=None):
      nameFacet = self.facet('name',name)
      orderFacet = self.facet('order',order)
      titleFacet = self.facet('title',title)
      summaryFacet = self.facet('summary',summary)
      genreFacet = self.facet('genre',genre)

      expr = '?s {0} "{1}"; {2} ?ordering; {3} ?title; {5} ?genre; . optional {{ ?s {4} ?summary . }}'.format(nameFacet,value,orderFacet,titleFacet,summaryFacet,genreFacet)
      if criteria is not None:

# Path: duckpond/data/sparql.py
# class SPARQL:
# 
#    def __init__(self):
#       self.statement = io.StringIO()
# 
#    def start(self,prefixes):
#       for key in prefixes:
#          self.statement.write('prefix {}: {}\n'.format(key,uri(prefixes[key])))
#       return self
# 
#    def construct(self,names):
#       self.statement.write('construct {')
#       for name in names:
#          write_var(self.statement,name)
#          self.statement.write(' ')
#       self.statement.write('}\n')
#       return self
# 
#    def select(self,names):
#       self.statement.write('select ')
#       for name in names:
#          write_var(self.statement,name)
#          self.statement.write(' ')
#       self.statement.write('\n')
#       return self
# 
#    def fromGraphs(self,graphs):
#       for graph in graphs:
#          self.statement.write('from {0}\n'.format(uri(graph)))
#       return self
# 
#    def fromNameGraphs(self,graphs):
#       for graph in graphs:
#          self.statement.write('from named {0}\n'.format(uri(graph)))
#       return self
# 
#    def withGraph(self,graph):
#       self.statement.write("with {0}\n".format(uri(graph)))
#       return self
# 
#    def where(self,*expressions):
#       self.statement.write('where {')
#       for expression in expressions:
#          self.statement.write(str(expression))
#          self.statement.write('\n')
#       self.statement.write('}\n')
#       return self
# 
#    def group(self,*expressions):
#       self.statement.write('{')
#       for expression in expressions:
#          self.statement.write(str(expression))
#          self.statement.write('\n')
#       self.statement.write('}\n')
#       return self
# 
#    def insert(self,*expressions):
#       self.statement.write('insert data {')
#       for expression in expressions:
#          self.statement.write(str(expression))
#          self.statement.write('\n')
#       self.statement.write('}\n')
#       return self
# 
#    def delete(self,*expressions):
#       self.statement.write('delete data {')
#       for expression in expressions:
#          self.statement.write(str(expression))
#          self.statement.write('\n')
#       self.statement.write('}\n')
#       return self
# 
#    def anyGraph(self,*expressions):
#       self.statement.write('graph ?g {')
#       for expression in expressions:
#          self.statement.write(str(expression))
#          self.statement.write('\n')
#       self.statement.write('}\n')
#       return self
# 
#    def forGraph(self,graph,*expressions):
#       self.statement.write('graph {0} {'.format(uri(graph)))
#       for expression in expressions:
#          self.statement.write(str(expression))
#          self.statement.write('\n')
#       self.statement.write('}\n')
#       return self
# 
#    def newline(self):
#       self.statement.write('\n')
#       return self;
# 
#    def orderBy(self,*expressions):
#       self.statement.write('order by ')
#       first = True
#       for expression in expressions:
#          if not first:
#             self.statement.write(',')
#          self.statement.write(str(expression))
#       self.statement.write('\n')
#       return self
# 
#    def groupBy(self,expression):
#       self.statement.write('group by ')
#       self.statement.write(expression)
#       self.statement.write('\n')
#       return self
# 
#    def __str__(self):
#       return self.statement.getvalue()
# 
# class var:
#    def __init__(self,value):
#       self.name = value
#    def __str__(self):
#       return '?'+str(self.name)
# 
# class inline_expr:
#    def __init__(self,value):
#       self.expr = value
#    def __str__(self):
#       return '(' + str(self.expr) + ')'


# Path: duckpond/data/pond.py
import requests
import json,io,os
import logging
import urllib
from enum import Enum
from .sparql import SPARQL, var, inline_expr

         self.logger.debug(str(q))
      params = self.mergeParameters({'limit':1,'query':str(q)})

      req = requests.get(self.service,params=params,headers={'accept':'application/json'},auth=self.serviceAuth)

      if (req.status_code>=200 or req.status_code<300):
         #print(req.text)
         data = json.loads(req.text)
         if len(data['values']) == 0:
            return None
         subject = strip(data['values'][0][0])
         title = literal(data['values'][0][1])
         summary = literal(data['values'][0][2]) if data['values'][0][2] is not None else None
         dateTime = strip(data['values'][0][3])
         nameValue = strip(data['values'][0][4])
         genreValue = strip(data['values'][0][5])
         date,time = dateTime.split('T') # Need to check that is xsd:dateTime
         return (subject,title,summary,date,time,nameValue,genreValue)
      else:
         raise IOError('Cannot post data to uri <{}>, status={}'.format(self.service,req.status_code))

   def getEntityByName(self,value,name=None,title=None,order=None,summary=None,criteria=None,genre=None):
      nameFacet = self.facet('name',name)
      orderFacet = self.facet('order',order)
      titleFacet = self.facet('title',title)
      summaryFacet = self.facet('summary',summary)
      genreFacet = self.facet('genre',genre)

      expr = '?s {0} "{1}"; {2} ?ordering; {3} ?title; {5} ?genre; . optional {{ ?s {4} ?summary . }}'.format(nameFacet,value,orderFacet,titleFacet,summaryFacet,genreFacet)
      if criteria is not None:

         expr = expr + criteriaConditions(self,criteria)