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

Source Code for Module advene.model.view.builtin

  1  """I am the content handler for mimetype application/x-advene-builtin-view. 
  2  """ 
  3   
  4  from inspect import getargspec 
  5   
  6  from advene.model.consts import _RAISE 
  7  from advene.model.core.element import \ 
  8      MEDIA, ANNOTATION, RELATION, TAG, LIST, IMPORT, QUERY, VIEW, RESOURCE 
  9  from advene.model.exceptions import ContentHandlingError 
10 11 # general handler interface 12 13 -def claims_for_handle(mimetype):
14 """Is this view_handler likely to handle a view with that mimetype. 15 16 Return an int between 00 and 99, indicating the likelyhood of this handler 17 to handle correctly the given mimetype. 70 is used as a standard value when 18 the hanlder is pretty sure it can handle the mimetype. 19 """ 20 if mimetype == "application/x-advene-builtin-view": 21 return 99 22 else: 23 return 0
24
25 -def get_output_mimetype(view):
26 """Return the mimetype of the content produced by that view. 27 28 Note that the output mimetype may depend on the mimetype of the view, as 29 well as the content of the view itself, but should not depend on the 30 element the view is applied to. 31 """ 32 global _methods 33 params = view.content_parsed 34 return _methods[params["method"]].info["output_mimetype"]
35
36 -def apply_to(view, obj):
37 global _methods 38 params = view.content_parsed 39 method = params.pop("method") 40 m = _methods.get(method, None) 41 if m is None: 42 raise ContentHandlingError("unknown builtin view method: %s" % method) 43 try: 44 return _methods[method](obj, **params) 45 except TypeError, e: 46 raise ContentHandlingError(*e.args)
47
48 49 # specific to this handler 50 51 -def iter_methods():
52 global _methods 53 return _methods.iterkeys()
54
55 -def get_method_info(method, default=_RAISE):
56 global _methods 57 r = _methods.get(method, _RAISE) 58 if r is None: 59 if default is _RAISE: 60 raise KeyError(method) 61 else: 62 r = default 63 else: 64 r = r.info.copy() 65 return r
66
67 # private implementation 68 69 -def _wrap_method(**info):
70 def wrapper(f, info=info): 71 global _methods 72 _methods[f.__name__] = f 73 f.info = info 74 info["params"] = getargspec(f)[0][1:] 75 return f
76 return wrapper 77 78 _methods = {} 79 80 81 @_wrap_method( 82 output_mimetype = "text/plain", 83 )
84 -def hello_world(obj):
85 return "hello world!"
86 87 88 89 @_wrap_method( 90 output_mimetype = "text/plain", 91 type = "one of the following values:\n" \ 92 " * ANNOTATION\n * IMPORT\n * LIST\n * MEDIA\n * PACKAGE\n * QUERY\n" \ 93 " * RELATION\n * RESOURCE\n * TAG\n * VIEW\n""" 94 )
95 -def has_type(obj, type):
96 d = { "ANNOTATION": ANNOTATION, 97 "IMPORT": IMPORT, 98 "LIST": LIST, 99 "MEDIA": MEDIA, 100 "PACKAGE": "Package", 101 "QUERY": QUERY, 102 "RELATION": RELATION, 103 "RESOURCE": RESOURCE, 104 "TAG": TAG, 105 "VIEW": VIEW, 106 } 107 type = d[type] 108 ref = getattr(obj, "ADVENE_TYPE", None) 109 if ref is None: 110 if obj.__class__.__name__ == "Package": 111 ref = "Package" 112 # TODO are the following heuristics really the good solution? 113 elif isinstance(basestring): 114 ref = RESOURCE 115 else: 116 try: 117 iter(obj) 118 except TypeError: 119 ref = None 120 else: 121 ref = LIST 122 if type == ref: 123 return "true" 124 else: 125 return ""
126 127 128 @_wrap_method( 129 output_mimetype = "text/plain", 130 mimetype = "a mimetype that the element's content mimetype, if any, must "\ 131 "match" 132 )
133 -def basic_element_constraint(obj, mimetype=None):
134 if mimetype is not None: 135 m = getattr(obj, "content_mimetype", None) 136 if m is not None: 137 if m != mimetype: 138 # TODO manage generic mimetypes (with '*') 139 return "" 140 return "true"
141 142 # 143