def _test(self, raw_arg_spec, arg_data, expected): from Products.ZSPARQLMethod.Method import map_arg_values, parse_arg_spec missing, result = map_arg_values(parse_arg_spec(raw_arg_spec), arg_data) self.assertEqual(missing, []) self.assertEqual(result, expected) self.assertEqual(map(type, result.values()), map(type, expected.values()))
def test_query(self): """test query""" arg_string = ' '.join([arg['name'] for arg in self.context.arg_spec]) arg_spec = parse_arg_spec(arg_string) missing, arg_values = map_arg_values(arg_spec, self.request.form) error = None if missing: # missing argument data = None dt = 0 else: t0 = time() res, error = {}, None try: res = self.context.execute(**arg_values) except Exception, err: import traceback logger.exception(err) error = traceback.format_exc() data = res.get('result') error = error or res.get('exception') dt = time() - t0
def preview(self): """preview""" tmp_query = self.request.get("sparql_query", "") tmp_query = "\n".join(x for x in tmp_query.splitlines() if not x.strip().startswith("#")) tmp_arg_spec = self.request.get("arg_spec", "") tmp_endpoint = self.request.get("endpoint", "") tmp_timeout = int(self.request.get("timeout", "0")) arg_spec = parse_arg_spec(tmp_arg_spec) missing, arg_values = map_arg_values(arg_spec, self.request.form) error = None if missing: error = "" for missing_arg in missing: error = error + "<div>Argument '%s' missing</div>" % missing_arg else: result = {} data = [] error = None try: m = re.search(r"limit\s(\d+)", tmp_query, re.IGNORECASE) if m: tmp_query = tmp_query[:m.start(1)]+'10'+tmp_query[m.end(1):] else: tmp_query = tmp_query + " LIMIT 5" cooked_query = interpolate_query(tmp_query, arg_values) args = (tmp_endpoint, cooked_query) result, error = {}, None result = run_with_timeout(tmp_timeout, query_and_get_result, *args) data = result.get('result') error = error or result.get('exception') except Exception: import traceback error = traceback.format_exc() if error: return "<blockquote class='sparql-error'> %s </blockquote>" % error result = [] result.append(u"<table class='sparql-results'>") result.append(u"<thead>") result.append(u"<tr>") for var_name in data.get('var_names', []): result.append(u"<th> %s </th>" %var_name) result.append(u"</tr>") result.append(u"</thead>") result.append(u"<tbody>") for row in data.get('rows', []): result.append(u"<tr class='row_0'>") for value in row: try: result.append(u"<td> %s </td>" %cgi.escape(value.n3())) except Exception, err: logger.debug(err) result.append(u"<td> %s </td>" %value) result.append(u"</tr>")
def preview(self): """preview""" tmp_query = self.request.get("sparql_query", "") tmp_query = "\n".join(x for x in tmp_query.splitlines() if not x.strip().startswith("#")) tmp_arg_spec = self.request.get("arg_spec", "") tmp_endpoint = self.request.get("endpoint", "") tmp_timeout = int(self.request.get("timeout", "0")) arg_spec = parse_arg_spec(tmp_arg_spec) missing, arg_values = map_arg_values(arg_spec, self.request.form) error = None if missing: error = "" for missing_arg in missing: error = error + "<div>Argument '%s' missing</div>" % missing_arg else: result = [] data = [] error = None try: m = re.search(r"limit\s(\d+)", tmp_query, re.IGNORECASE) if m: tmp_query = tmp_query[:m.start(1)] + '10' + tmp_query[ m.end(1):] else: tmp_query = tmp_query + " LIMIT 5" cooked_query = interpolate_query(tmp_query, arg_values) args = (tmp_endpoint, cooked_query) result, error = {}, None result = run_with_timeout(tmp_timeout, query_and_get_result, *args) data = result.get('result', {}) error = error or result.get('exception') except Exception: import traceback error = traceback.format_exc() if error: return "<code class='sparql-error'><pre> %s </pre></code>" % error result = [u"<table class='sparql-results'>", u"<thead>", u"<tr>"] for var_name in data.get('var_names', []): result.append(u"<th> %s </th>" % var_name) result.append(u"</tr>") result.append(u"</thead>") result.append(u"<tbody>") for row in data.get('rows', []): result.append(u"<tr class='row_0'>") for value in row: try: result.append(u"<td> %s </td>" % cgi.escape(value.n3())) except Exception, err: logger.debug(err) result.append(u"<td> %s </td>" % value) result.append(u"</tr>")
def map_arguments(self, **arg_values): """ overides map_arguments to match the name:type - query data model """ arg_string = ' '.join([arg['name'] for arg in self.arg_spec]) arg_spec = parse_arg_spec(arg_string) missing, arg_values = map_arg_values(arg_spec, arg_values) if missing: raise KeyError("Missing arguments: %r" % missing) else: return arg_values
def loadSparql(): """ Load data from mock http """ sparql = Sparql(0) sparql.endpoint_url = "http://localhost:" + str( PORT) + "/sparql-results.xml" sparql.sparql_query = mock_sparql_query sparql.timeout = None sparql.arg_spec = "" args = "" arg_spec = parse_arg_spec(sparql.arg_spec) arg_values = map_arg_values(arg_spec, args)[1] cooked_query = interpolate_query(sparql.query, arg_values) query_args = (sparql.endpoint_url, cooked_query) data = run_with_timeout(10, query_and_get_result, *query_args) return data
def loadSparql(): """ Load data from mock http """ sparql = Sparql(0) sparql.endpoint_url = "http://localhost:"+str(PORT)+"/sparql-results.xml" sparql.sparql_query = mock_sparql_query sparql.timeout = None sparql.arg_spec = "" args = "" arg_spec = parse_arg_spec(sparql.arg_spec) arg_values = map_arg_values(arg_spec, args)[1] cooked_query = interpolate_query(sparql.query, arg_values) query_args = (sparql.endpoint_url, cooked_query) data = run_with_timeout( 10, query_and_get_result, *query_args) return data
def test_query(self): """test query""" arg_string = ' '.join([arg['name'] for arg in self.context.arg_spec]) arg_spec = parse_arg_spec(arg_string) missing, arg_values = map_arg_values(arg_spec, self.request.form) error = None if missing: # missing argument data = None dt = 0 else: t0 = time() res, error = {}, None try: res = self.context.execute(**arg_values) except Exception: import traceback error = traceback.format_exc() data = res.get('result') error = error or res.get('exception') dt = time() - t0 options = { 'query': interpolate_query_html(self.context.query, arg_values), 'query_with_comments': interpolate_query_html( self.context.query_with_comments, arg_values), 'data': data, 'duration': dt, 'arg_spec': arg_spec, 'error': error, } return options
def execute_query(self, args=None): """execute query""" arg_string = ' '.join([arg['name'] for arg in self.arg_spec]) arg_spec = parse_arg_spec(arg_string) arg_values = map_arg_values(arg_spec, args)[1] return self.execute(**self.map_arguments(**arg_values))