############################################################################## # # Copyright (c) 2004-1005 Michel Pelletier # All Rights Reserved. # # This software is subject to the provisions of the Zope Public License, # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS # FOR A PARTICULAR PURPOSE. # ############################################################################## __rdf_description__ = '''\ ''' Any = None from zope.i18nmessageid import MessageIDFactory from zope.app import zapi from rdflib.URIRef import URIRef from rdflib.BNode import BNode from rdflib.Literal import Literal from rdflib.Identifier import Identifier from utils import ZemanticToDotAdapter from query import Query import popen2 import urllib, os from tempfile import NamedTemporaryFile from StringIO import StringIO from resources import FileResource, DirectoryResource from zontology import Ontology from result import ResultSet _ = MessageIDFactory('zemantic') class ZemanticView: def shorty(self, s): """ dumb ui method """ if len(s) > 30: return s[:15]+'...'+s[-15:] return s def n3(self, obj): return obj.n3() def uniquePredicatesView(self): r = [] for i in self.context.uniquePredicates(): r.append(i) return r def clear(self): self.context.clear() def query(self, subject, predicate, object): q = Query(subject, predicate, object) return self.context.query(q) def queryRDF(self, subject, predicate, object): if subject in ('Any', 'None'): subject = Any if predicate in ('Any', 'None'): predicate = Any if object in ('Any', 'None'): object = Any q = Query(subject, predicate, object) self.request.response.setHeader('content-type', 'text/xml') return ResultSet(self.context.query(q)).rdfxml() def quote(self, str): return urllib.quote(str) def genSVG(self): x = ZemanticToDotAdapter(self.context, "RDF Graph") d = x.toDot() i = NamedTemporaryFile() i.write(d) i.flush() self.request.response.setHeader('content-type', 'image/svg+xml') return getoutput('dot -Tsvg %s' % i.name) def genJPG(self): x = ZemanticToDotAdapter(self.context, "RDF Graph") d = x.toDot() i = NamedTemporaryFile() o = NamedTemporaryFile() i.write(d) i.flush() getoutput('dot -Tjpg %s -o %s' % (i.name, o.name)) f = open(o.name) self.request.response.setHeader('content-type', 'image/jpg') return f.read() def genDOT(self): x = ZemanticToDotAdapter(self.context, "RDF Graph") return x.toDot() def addURL(self): url = self.request.form['add_url'] f = urllib.urlopen(url) self.context.parse(f) self.request.response.redirect('zemantic.html') def clear(self): self.context.clear() self.request.response.redirect('zemantic.html') class ZontologyView: def loadstuff(self): dir = DirectoryResource('data') for name in dir.list(): if name.endswith('.rdf'): try: f = FileResource(os.path.join(dir.path(), name)).open() z = Ontology() z.parse(f) self.context[name[:-4]] = z get_transaction().commit(1) except: print "could not load %s" % name self.request.response.redirect('contents.html') class OntologyView: def getClassNames(self): return self.context.classes().keys() def getPropertyNames(self): return self.context.properties().keys() # Get the output from a shell command into a string. # The exit status is ignored; a trailing newline is stripped. # Assume the command will work with '{ ... ; } 2>&1' around it.. # def getoutput(cmd): """Return output (stdout or stderr) of executing cmd in a shell.""" return getstatusoutput(cmd)[1] # Ditto but preserving the exit status. # Returns a pair (sts, output) # def getstatusoutput(cmd): """Return (status, output) of executing cmd in a shell.""" import os sout = os.popen(cmd, 'r') text = sout.read() sts = sout.close() if sts is None: sts = 0 if text[-1:] == '\n': text = text[:-1] return sts, text