Debian Codegraph @

gavo/svcs/vanity.py

raw
1 """
2 Parsing and maintaining URL shortcuts.
3 """
4
5 #c Copyright 2008-2025, the GAVO project <[email protected]>
6 #c
7 #c This program is free software, covered by the GNU GPL. See the
8 #c COPYING file in the source distribution.
9
10
11 from gavo import base
12 from gavo import utils
13
14
15 class VanityLineError(base.Error):
16 """parse error in vanity file.
17 """
18 def __init__(self, msg, lineNo, src):
19 base.Error.__init__(msg)
20 self.msg, self.lineNo, self.src = msg, lineNo, src
21
22 def __str__(self):
23 return "Mapping file %s, line %d: %s"%(
24 repr(self.src), self.msg, self.lineNo)
25
26
27 BUILTIN_VANITY = """
28 __system__/products/p/get getproduct
29 __system__/products/p/dlasync datalinkuws
30 __system__/services/registry/pubreg.xml oai.xml
31 __system__/services/overview/external odoc
32 __system__/dc_tables/show/tablenote tablenote
33 __system__/dc_tables/show/tableinfo tableinfo
34 __system__/services/overview/admin seffe
35 __system__/services/overview/rdinfo browse
36 __system__/tap/run tap
37 __system__/adql/query/form adql !redirect
38 __system__/run/genrd genrd
39 """
40
41
42 class _VanityMap(object):
43 """a map of short resource paths to longer resource paths.
44
45 This is only used as singleton through getVanityMap.
46
47 There are two mappings: shortToLong, going from vanityName to
48 (fullPath,flags), and longToShort, mapping fullPath -> vanityName.
49
50 flags, in both cases, is a frozenset of flags. The only one defined
51 at this point is "!redirect".
52 """
53 knownVanityOptions = set(["!redirect"])
54
55 def __init__(self):
56 self.shortToLong = {}
57 self.longToShort = {}
58
59 def _parseVanityLines(self, src):
60 lineNo = 0
61 for ln in src:
62 lineNo += 1
63 ln = ln.strip()
64 if not ln or ln.startswith("#"):
65 continue
66
67 parts = ln.split()
68 if not 1<len(parts)<4:
69 raise VanityLineError("Wrong number of words in '%s'"%ln, lineNo, src)
70
71 options = []
72 if len(parts)>2:
73 options.append(parts.pop())
74 if options[-1] not in self.knownVanityOptions:
75 raise VanityLineError("Bad option '%s'"%options[-1], lineNo, src)
76 dest, src = parts
77 yield src, dest, frozenset(options)
78
79 def _buildDicts(self, triples):
80 """builds the child mappings from triples as yielded by _parseVanityLines.
81 """
82 for short, int, flags in triples:
83 self.shortToLong[short] = (int, flags)
84 self.longToShort[int] = short
85
86 def addFromString(self, s):
87 """adds vanity mappings from a string literal.
88 """
89 self._buildDicts(
90 self._parseVanityLines(s.split("\n")))
91
92 def addFromFile(self, f):
93 """adds vanity mappings from an open file.
94 """
95 self._buildDicts(
96 self._parseVanityLines(f))
97
98
99 def _loadVanityMap():
100 """helps getVanityMap.
101 """
102 vm = _VanityMap()
103 vm.addFromString(BUILTIN_VANITY)
104
105 fSrc = base.getConfig("configDir") / "vanitynames.txt"
106 if fSrc.exists():
107 with open(fSrc) as f:
108 vm.addFromFile(f)
109
110 return vm
111
112
113 def getVanityMap():
114 """returns "the" vanity map on this data center.
115
116 It consists of built-in vanity (without which things like product
117 delivery would actually break) and things read from etc/vanitynames.txt.
118
119 The input file format is documented in the DaCHS tutorial (The Vanity Map).
120 """
121 try:
122 anchorRD = base.resolveCrossId("//services")
123 except (AttributeError, base.ReportableError):
124 # We're somewhere where we don't already have rscdesc loaded,
125 # or perhaps the database is down entirely.
126 # Let's assume that means the VanityMap will not need to be reloaded
127 # during the runtime, and it's ok if we anchor on the function.
128 anchorRD = getVanityMap
129
130 return utils.memoizeOn(
131 anchorRD,
132 _VanityMap,
133 _loadVanityMap)