# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr## This file is part of CubicWeb.## CubicWeb is free software: you can redistribute it and/or modify it under the# terms of the GNU Lesser General Public License as published by the Free# Software Foundation, either version 2.1 of the License, or (at your option)# any later version.## CubicWeb is distributed in the hope that it will be useful, but WITHOUT# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more# details.## You should have received a copy of the GNU Lesser General Public License along# with CubicWeb. If not, see <http://www.gnu.org/licenses/>."""base xml and rss views"""__docformat__="restructuredtext en"_=unicodefromyamsimportxyfromcubicweb.schemaimportVIRTUAL_RTYPESfromcubicweb.viewimportEntityViewfromcubicweb.web.views.xmlrssimportSERIALIZERStry:importrdflibexceptImportError:rdflib=NoneifrdflibisnotNone:RDF=rdflib.Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')CW=rdflib.Namespace('http://ns.cubicweb.org/cubicweb/0.0/')fromrdflibimportLiteral,URIRef,Namespacedefurijoin(item):base,ext=itemreturnURIRef(Namespace(base)[ext])SKIP_RTYPES=VIRTUAL_RTYPES|set(['cwuri','is','is_instance_of'])classRDFView(EntityView):"""rdf view for entities"""__regid__='rdf'title=_('rdf export')templatable=Falsecontent_type='text/xml'# +rdfdefcall(self):graph=rdflib.Graph()graph.bind('cw',CW)forprefix,xmlnsinxy.XY.prefixes.items():graph.bind(prefix,rdflib.Namespace(xmlns))foriinxrange(self.cw_rset.rowcount):entity=self.cw_rset.complete_entity(i,0)self.entity2graph(graph,entity)self.w(graph.serialize().decode('utf-8'))defentity_call(self,entity):self.call()defentity2graph(self,graph,entity):cwuri=URIRef(entity.cwuri)add=graph.addadd((cwuri,RDF.type,CW[entity.e_schema.type]))try:foriteminxy.xeq(entity.e_schema.type):add((cwuri,RDF.type,urijoin(item)))exceptxy.UnsupportedVocabulary:passforrschema,eschemas,roleinentity.e_schema.relation_definitions('relation'):rtype=rschema.typeifrtypeinSKIP_RTYPESorrtype.endswith('_permission'):continueforeschemaineschemas:ifeschema.final:try:value=entity.cw_attr_cache[rtype]exceptKeyError:continue# assuming rtype is BytesifvalueisnotNone:add((cwuri,CW[rtype],Literal(value)))try:foriteminxy.xeq('%s%s'%(entity.e_schema.type,rtype)):add((cwuri,urijoin(item[1]),Literal(value)))exceptxy.UnsupportedVocabulary:passelse:forrelatedinentity.related(rtype,role,entities=True,safe=True):ifrole=='subject':add((cwuri,CW[rtype],URIRef(related.cwuri)))try:foriteminxy.xeq('%s%s'%(entity.e_schema.type,rtype)):add((cwuri,urijoin(item),URIRef(related.cwuri)))exceptxy.UnsupportedVocabulary:passelse:add((URIRef(related.cwuri),CW[rtype],cwuri))