Package advene :: Package model :: Package tal :: Module global_methods
[hide private]
[frames] | no frames]

Source Code for Module advene.model.tal.global_methods

  1  # 
  2  # Advene: Annotate Digital Videos, Exchange on the NEt 
  3  # Copyright (C) 2008 Olivier Aubert <olivier.aubert@liris.cnrs.fr> 
  4  # 
  5  # Advene is free software; you can redistribute it and/or modify 
  6  # it under the terms of the GNU General Public License as published by 
  7  # the Free Software Foundation; either version 2 of the License, or 
  8  # (at your option) any later version. 
  9  # 
 10  # Advene is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU General Public License for more details. 
 14  # 
 15  # You should have received a copy of the GNU General Public License 
 16  # along with Advene; if not, write to the Free Software 
 17  # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 18  # 
 19  """ 
 20  This module contains all the global methods to be automatically added to a new 
 21  AdveneContext. 
 22  Note that those method must import every module they need _inside_ their body in 
 23  order to prevent cyclic references. 
 24   
 25  If called on an invalid target, the method should return None. 
 26  """ 
27 -def absolute_url(target, context):
28 """Return the absolute URL of the element. 29 """ 30 31 import advene.model.annotation 32 import advene.model.content 33 import advene.model.fragment 34 import advene.model.package 35 import advene.model.query 36 import advene.model.schema 37 import advene.model.view 38 import advene.model.resources 39 40 def _abs_url(target): 41 if isinstance(target, advene.model.annotation.Annotation): 42 return '/annotations/%s' % target.getId() 43 elif isinstance(target, advene.model.annotation.Relation): 44 return '/relations/%s' % target.getId() 45 elif isinstance(target, advene.model.package.Package): 46 return '' 47 elif isinstance(target, advene.model.query.Query): 48 return '/queries/%s' % target.getId() 49 elif isinstance(target, advene.model.schema.Schema): 50 return '/schemas/%s' % target.getId() 51 elif isinstance(target, advene.model.schema.AnnotationType): 52 return '/schemas/%s/annotationTypes/%s' % \ 53 (target.getSchema().getId(), target.getId()) 54 elif isinstance(target, advene.model.schema.RelationType): 55 return '/schemas/%s/relationTypes/%s' % \ 56 (target.getSchema().getId(), target.getId()) 57 elif isinstance(target, advene.model.view.View): 58 return '/views/%s' % target.getId() 59 elif isinstance(target, (advene.model.resources.ResourceData, 60 advene.model.resources.Resources) ): 61 return '/resources/%s' % target.resourcepath 62 else: 63 return None
64 65 path = _abs_url(target) 66 67 if path is None: 68 if context is None: 69 return None 70 resolved_stack = context.locals['__resolved_stack'] 71 if resolved_stack is None or len (resolved_stack) == 0: 72 return None 73 suffix = [resolved_stack[0][0]] 74 for i in resolved_stack[1:]: 75 name, obj = i 76 path = _abs_url (obj) 77 if path is not None: 78 path = "%s/%s" % (path, "/".join (suffix)) 79 break 80 else: 81 suffix.insert (0, name) 82 #print "Generated %s" % path 83 84 if path is not None and context is not None: 85 options = context.globals['options'] 86 if options.has_key('package_url'): 87 path = '%s%s' % (options['package_url'], path) 88 return path 89
90 -def isa (target, context):
91 """Check the type of an element. 92 93 Return an object such that target/isa/[viewable_class], 94 target/isa/[viewable_type] and target/isa/[viewable_class]/[viewble_type] 95 are true for the correct values of viewable_class and viewable_type. 96 Note that for annotations and relations, viewable_type must be the QName 97 for of the type URI. 98 99 Note that for contents, viewable_type can be a two part path, corresponding 100 to the usual mime-type writing. The star ('*') character, however, is not 101 supported. For example, if c1 has type 'text/*' and c2 has type 102 'text/plain', the following will evaluate to True: c1/isa/text, 103 c2/isa/text, c1/isa/text/html; the following will of course evaluate to 104 False: c2/isa/text/html. 105 """ 106 class my_dict (dict): 107 def __init__ (self, values=None, default=False): 108 dict.__init__(self) 109 self.__default = default 110 if values: 111 for k, v in values.iteritems(): 112 self[k]=v
113 114 def has_key (self, key): 115 return True 116 117 def __getitem__ (self, key): 118 if dict.has_key (self, key): 119 return dict.__getitem__ (self, key) 120 else: 121 return self.__default 122 123 def merge (self, dico): 124 for k, v in dico.iteritems(): 125 self[k]=v 126 127 try: 128 viewable_class = target.getViewableClass() 129 except AttributeError: 130 return my_dict({'unknown':True}) 131 132 r = my_dict ({viewable_class:True}) 133 if viewable_class == 'content': 134 t1, t2 = target.getMimetype ().split ('/') 135 vt1 = my_dict ({t2:True}) 136 mimetype_dict = my_dict ({t1:vt1}) 137 r[viewable_class] = mimetype_dict 138 r.merge (mimetype_dict) 139 elif viewable_class in ('annotation', 'relation'): 140 viewable_type = target.getType ().getId () 141 d1 = my_dict ({viewable_type:True}) 142 r[viewable_class] = d1 143 r.merge (d1) 144 elif viewable_class == 'list': 145 viewable_type = target.getViewableType () 146 list_dict = ({viewable_type:True}) 147 r[viewable_class] = list_dict 148 r.merge (list_dict) 149 150 return r 151
152 -def meta(target, context):
153 """Access to meta attributes. 154 155 Function to be used as a TALES global method, in order to give access 156 to meta attributes. 157 158 This function assumes that the 'options' of the TALES context have a 159 dictionnary named 'namespace_prefix', whose keys are prefices and whose 160 values are corresponding namespace URIs. 161 162 The use of this function is (assuming that here is a Metaed object): 163 here/meta/dc/version 164 for example (where prefix 'dc' has been mapped to the Dublin Core 165 namespace URI in 'namespace_prefix'. 166 """ 167 168 import advene.model._impl 169 170 class MetaNameWrapper(object): 171 def __init__(self, target, namespace_uri): 172 self.__target = target 173 self.__namespace_uri = namespace_uri
174 175 def has_key(self, key): 176 return (self[key] is not None) 177 178 def __getitem__(self, key): 179 return self.__target.getMetaData(self.__namespace_uri, key) 180 181 class MetaNSWrapper(object): 182 def __init__(self, target, context): 183 self.__target = target 184 options = context.globals['options'] 185 self.__ns_dict = options.get('namespace_prefix', {}) 186 187 def has_key(self, key): 188 return key in self.__ns_dict 189 190 def __getitem__(self, key): 191 if self.has_key(key): 192 return MetaNameWrapper(self.__target, self.__ns_dict[key]) 193 else: 194 return None 195 196 def keys(self): 197 return self.__ns_dict.keys() 198 199