1
2 """
3 """
4
5 import os.path
6 from rdflib import RDF, RDFS, ConjunctiveGraph
7 from genshi.output import DocType
8 from genshi.template import TemplateLoader
9 from oort.util import templating
10 from oort.util.code import contract, autosuper
11 __metaclass__ = autosuper
12
13
14
15
16
17
18
20
21 contentType = None
22
25
26 @contract.template_method
28 raise NotImplementedError
29
30
32
33
34
35
36
37 outputMethod = None
38 outputEncoding = None
39
40 templateBase = ""
41 globalQueries = None
42 aspects = []
43
45 self.typeAspects = {}
46 self.variantAspects = {}
47 cfg = cfg or {}
48 cfg['templateBase'] = self.templateBase
49 for aspect in self.aspects:
50 aspect.post_init_setup(self.globalQueries, cfg=cfg)
51 if aspect.forVariant:
52 variant = self.variantAspects.setdefault(aspect.forVariant, {})
53 typeDict = variant
54 else:
55 typeDict = self.typeAspects
56 typeDict[aspect.forType] = aspect
57
58 @contract.default_method
61
70
72 rdfType = RDFS.Resource
73 for _type in graph.objects(resource, RDF.type):
74 rdfType = _type; break
75 typeDict = self.typeAspects
76 if variant:
77 typeDict = self.variantAspects.get(variant) or self.typeAspects
78 return typeDict.get(rdfType) or typeDict[RDFS.Resource]
79
80 @contract.default_method
82 "Use for further template preparation based on current request."
83 return None
84
85
88
89
91
95
100
102 """For optimization, creating a sparse graph of "best matching"
103 for all resources and querying only that at runtime. """
104 typeAspects = self.typeAspects
105 def list_with_sub_types(rdfType):
106 yield rdfType
107 for subType in graph.subjects(RDFS.subClassOf, rdfType):
108 if subType not in typeAspects:
109 for subSub in list_with_sub_types(subType):
110 yield subSub
111
112 sparseGraph = ConjunctiveGraph()
113 for handledType in typeAspects:
114 for anySub in list_with_sub_types(handledType):
115 for resource in graph.subjects(RDF.type, anySub):
116 sparseGraph.add((resource, RDF.type, handledType))
117
118 return sparseGraph
119
120
121
122
123
124
125
126
127
128
129
130
131
133
134 - def __init__(self, forType, queries=None, forVariant=None):
135 self.forType = forType
136 self.queries = queries or {}
137 self.forVariant = forVariant
138
139 @contract.state_change
140 - def post_init_setup(self, globalQueries, cfg=None):
146
147 @contract.template_method
150
151 @contract.helper
157
158
159 @contract.template_method
161 """Returns a generator that takes the arguments 'encoding' and
162 'output'."""
163 raise NotImplementedError
164
165 - def using(self, **queries):
168
170 - def __init__(self, forType, templateName, queries=None, forVariant=None):
174
177
178 -class Aspect(TemplateAspectBase):
179
180 RELOAD_TEMPLATES = "reload_templates"
181
182
183 doctypes = dict(
184 (key.lower().replace('_', '-'), item)
185 for key, item in DocType.__dict__.items()
186 if key.isupper() and isinstance(item, tuple)
187 )
188
193
196
198 tplt = self.load_template()
199 allData = dict( self.generate_query_items(graph, resource, lang) )
200 if data: allData.update(data)
201
202
203
204 def generator(format, encoding):
205 doctype = self.doctypes.get(format)
206 format = format.split('-')[0]
207 yield tplt.generate(**allData).render(
208 format, doctype=doctype, encoding=encoding)
209 return generator
210
211
236
237
239
240 - def __init__(self, forType, queries=None):
242
250 return generator
251
253
254 - def __init__(self, forType, queries=None, subjectKey=False):
257
259 qData = dict( self.generate_query_items(graph, resource, lang) )
260 def generator(format, encoding):
261
262 yield '{\n'
263 for i, (key, result) in enumerate(qData.items()):
264 if i != 0: yield ","
265 yield "'%s':" % key
266
267 yield result.to_json(self.subjectKey).replace('}', '}\n')
268 yield '}\n'
269 return generator
270