Debian Codegraph @

gavo/registry/identifiers.py

raw
1 """
2 Parsing identifiers, getting res tuples and resobs from them.
3
4 The DC-internal identifiers are, by default, formed as
5 ivo://<authority-from-config>/<sourceRD path>/<id within path>.
6
7 Thus, all renderers of a given service have the same ivo-id, which is
8 to say, they are all just capabilities on the same record.
9 """
10
11 #c Copyright 2008-2025, the GAVO project <[email protected]>
12 #c
13 #c This program is free software, covered by the GNU GPL. See the
14 #c COPYING file in the source distribution.
15
16
17 import re
18
19 from gavo import base
20 from gavo.registry import common
21 from gavo.registry import nonservice
22 from gavo.registry import servicelist
23
24
25 def computeIdentifierFromRestup(restup):
26 """returns an identifier from a res tuple.
27 """
28 return restup["ivoid"]
29
30
31 _idPattern = re.compile(r"ivo://(\w[^!;:@%$,/]+)(/[^?#]*)?")
32
33 def parseIdentifier(identifier):
34 """returns a pair of authority, resource key for identifier.
35
36 Identifier has to be an ivo URI.
37
38 In the context of the DaCHS, the resource key either starts with
39 static/ or consists of an RD id and a service ID.
40 """
41 mat = _idPattern.match(identifier)
42 if not mat:
43 raise common.IdDoesNotExist(identifier)
44 return mat.group(1), (mat.group(2) or "")[1:]
45
46
47 def getRestupFromIdentifier(identifier):
48 """returns the record for identifier in the services table.
49 """
50 matches = servicelist.queryServicesList(
51 "ivoid=%(identifier)s",
52 locals(), tableName="resources")
53 if len(matches)!=1:
54 raise common.IdDoesNotExist(identifier)
55 return matches[0]
56
57
58 def getResobFromRestup(restup):
59 """returns a resob for a res tuple.
60
61 restup at least has to contain the sourceRD and resId fields.
62
63 The item that is being returned is either a service or a
64 NonServiceResource (including DeletedResource). All of these have
65 a getMeta method and should be able to return the standard DC
66 metadata.
67 """
68 if restup["deleted"]:
69 return base.makeStruct(nonservice.DeletedResource,
70 resTuple=restup)
71 sourceRD, resId = restup["sourceRD"], restup["resId"]
72 try:
73 if resId=="WHOLE_RD": # hack: sentinel for "the whole RD was registered"
74 return base.caches.getRD(sourceRD)
75 return base.caches.getRD(sourceRD).getById(resId)
76 except KeyError:
77 raise base.ui.logOldExc(base.NotFoundError(resId, what="service",
78 within="RD %s"%sourceRD, hint="This usually happens when you"
79 " forgot to run gavopublish %s"%sourceRD))
80
81
82 def getResobFromIdentifier(identifier):
83 """returns a resob for an identifier.
84 """
85 return getResobFromRestup(getRestupFromIdentifier(identifier))