#!/usr/bin/python # # test.py - SPARQL Protocol Test Suite # import os, sys, string, getopt import RDF, SimpleRDF, Vocabulary import sparqlclient, sparqlprottests import rdfdiff import httplib import unittest from sparqlprottests import * from StringIO import StringIO RDF.debug(0) SimpleRDF.debug(0) def usage(): print """ Usage: %s [OPTIONS] Standard arguments: -d, --data Directory containing test manifest.ttl files. -s, --service Execute query at given service. -h, --help Display this help message. Examples:
def procfile(rdf_file): global rdf_format if rdf_format is None: head, tail = ntpath.splitext(rdf_file) if tail.lower() == '.rdf' or tail.lower() == '.owl': rdf_format = 'rdfxml' elif tail.lower() == '.nt': rdf_format = 'ntriples' elif tail.lower() == '.ttl': rdf_format = 'turtle' elif tail.lower() == '.rss': rdf_format = 'rss-tag-soup' else: rdf_format = 'unknown' if debug: RDF.debug(1) try: storage = RDF.Storage( storage_name='hashes', name='rdf_storage', options_string="new='yes',hash-type='memory',dir='.'") if storage is None: sys.exit('RDF.Storage creation failed>') model = RDF.Model(storage) if model is None: sys.exit('RDF.Model creation failed>') sys.stderr.write('Parsing file {} as {}\n'.format( rdf_file, rdf_format)) uri = RDF.Uri(string='file:' + rdf_file) parser = RDF.Parser(rdf_format) if parser is None: sys.exit('RDF.Parser({}) creation failed>'.format(rdf_format)) except: exc = traceback.format_exception_only(sys.exc_info()[0], sys.exc_info()[1]) lineno = sys.exc_info()[-1].tb_lineno errmsg = 'RDF creation(%d): %s' % (lineno, clean(exc[-1])) sys.exit(errmsg) try: count = 0 for s in parser.parse_as_stream(uri): if debug: sys.stderr.write( '---------------------------------------- {}\n'.format( count + 1)) sys.stderr.write(str(s.subject) + '\n') sys.stderr.write(str(s.predicate) + '\n') sys.stderr.write(str(s.object) + '\n') model.add_statement(s) count += 1 if count >= max_count: break if debug: sys.stderr.write('----------------------------------------\n') except: exc = traceback.format_exception_only(sys.exc_info()[0], sys.exc_info()[1]) lineno = sys.exc_info()[-1].tb_lineno errmsg = 'Parse stream(%d): %s' % (lineno, clean(exc[-1])) if re.search(r'RedlandError:..Adding statement failed', errmsg, flags=re.IGNORECASE): sys.stderr.write(errmsg + '\n') else: sys.exit(errmsg) sys.stderr.write('Found {} statements\n'.format(count)) ns = parser.namespaces_seen() # {str: URI} for n in defns: if n not in ns: ns[n] = defns[n] for n in ns: ns[n] = str(ns[n]) if debug: sys.stderr.write('@prefix {}: <{}> .\n'.format(n, ns[n])) print('@startuml') # print('scale max 100000') # Populate objects with their attributes objects = {} alias = {} for s in model: # Subject subj, subj_lit = get_subject(s, ns) ox = subj if ox not in objects: objects[ox] = [] alias[ox] = simplify(ox) # Predicate pred, pred_lit = get_predicate(s, ns) # Object obj, obj_lit = get_object(s, ns) if pred != 'a' and not obj_lit: ox = obj if ox not in objects: objects[ox] = [] alias[ox] = simplify(ox) # Add attributes attrs = objects[ox] if pred == 'a': val = '{} {}'.format(pred, obj) if val not in attrs: attrs.append(val) elif obj_lit: obj_lang = s.object.literal[1] obj_type = s.object.literal[2] if sel_lang is not None: if obj_lang is None or obj_lang == sel_lang: val = '{} "{}"'.format(pred, obj) if add_lit_type and obj_type is not None: val += ' [{}]'.format(replns(obj_type, ns)) if val not in attrs: attrs.append(val) else: val = '{} "{}"'.format(pred, obj) if add_lit_type and obj_type is not None: val += ' [{}]'.format(replns(obj_type, ns)) if val not in attrs: attrs.append(val) objects[ox] = attrs # Print objects and their aliases at the beginning for o in objects: # Print object's alias if needed if o != alias[o]: if o.startswith('_:'): # blank node sys.stdout.write('object " " as {}\n'.format(alias[o])) else: sys.stdout.write('object "{}" as {}\n'.format(o, alias[o])) # Print object's attributes if any if len(objects[o]): sys.stdout.write('object {} {{\n'.format(alias[o])) attrs = objects[o] for a in attrs: sys.stdout.write(' {}\n'.format(a)) sys.stdout.write('}\n') # Print connections between objects for s in model: subj, subj_lit = get_subject(s, ns) pred, pred_lit = get_predicate(s, ns) obj, obj_lit = get_object(s, ns) if pred == 'a': continue elif obj_lit: continue sys.stdout.write('{}'.format(alias[subj])) sys.stdout.write(' --> ') sys.stdout.write('{} : {}'.format(alias[obj], pred)) sys.stdout.write('\n') print('@enduml') return 0