Package advene :: Package model :: Package view :: Module type_constraint
[hide private]
[frames] | no frames]

Source Code for Module advene.model.view.type_constraint

 1  """I am the content handler for mimetype application/x-advene-type-constraint. 
 2  """ 
 3   
 4  from advene.model.exceptions import ContentHandlingError 
 5   
 6  from gettext import gettext as _ 
 7   
 8  # general handler interface 
 9   
10 -def claims_for_handle(mimetype):
11 """Is this view_handler likely to handle a view with that mimetype. 12 13 Return an int between 00 and 99, indicating the likelyhood of this handler 14 to handle correctly the given mimetype. 70 is used as a standard value when 15 the hanlder is pretty sure it can handle the mimetype. 16 """ 17 if mimetype == "application/x-advene-type-constraint": 18 return 99 19 else: 20 return 0
21
22 -def get_output_mimetype(view):
23 """Return the mimetype of the content produced by that view. 24 25 Note that the output mimetype may depend on the mimetype of the view, as 26 well as the content of the view itself, but should not depend on the 27 element the view is applied to. 28 """ 29 return "application/x-advene-diagnosis"
30 # note that application/x-advene-diagnosis is represented by the class 31 # Diagnosis below. 32 # An empty diagnosis means 'True', while a non-empty diagnosis means 'False' 33 # diagnosis can be added (as well as added to an empty string). 34
35 -def apply_to(view, obj):
36 params = view.content_parsed 37 r = Diagnosis() 38 for k,v in params.iteritems(): 39 if k == "mimetype": 40 mimetype = getattr(obj, "content_mimetype") 41 if mimetype is not None and not check_mimetype(v, mimetype): 42 r.append( 43 "%(uri)s: mimetype %(mimetype)s does not match %(expected)s", 44 uri = obj.uriref, expected = v, 45 mimetype = obj.content_mimetype, 46 ) 47 else: 48 raise ContentHandlingError( 49 "unknown type-constraint parameter: %s" % k 50 ) 51 return r
52
53 -def check_mimetype(expected, actual):
54 e1, e2 = expected.split("/") 55 a1, a2 = actual.split("/") 56 return (e1 == "*" or a1 == e1) and (e2 == "*" or a2 == e2)
57
58 -class Diagnosis(object):
59 - def __init__(self, value=None):
60 assert value is None or isinstance(value, list) 61 if value is None: 62 value = [] 63 self._v = value
64
65 - def __nonzero__(self):
66 return len(self._v) == 0
67
68 - def __iter__(self):
69 for template, args in self._v: 70 yield _(template) % args
71
72 - def __str__(self):
73 return "\n".join(self)
74
75 - def __repr__(self):
76 return "Diagnosis(%r)" % self._v
77
78 - def __and__(self, rho):
79 if isinstance(rho, Diagnosis): 80 return Diagnosis(self._v + rho._v) 81 elif not rho: 82 return rho 83 else: 84 return self
85
86 - def __rand__(self, lho):
87 if isinstance(lho, Diagnosis): 88 return Diagnosis(lho + self._v) 89 elif lho: 90 return self 91 else: 92 return lho
93
94 - def append(self, template, **args):
95 self._v.append((template, args))
96 97 # 98