1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 """
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
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
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