-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransformer.py
65 lines (48 loc) · 1.69 KB
/
transformer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from config_translator import ConfigTranslator
def std_name_only(data: str) -> str:
"""
Transform a subject to its name only, example -
Given "https://whatever.url/somedirectory/somesubdirectory#the_name
Returns "the_name"
:param data:
:return:
"""
return data[data.find("#")+1:]
def std_urn_name(data: str) -> str:
"""
Transform a subject to some calculated URN, example -
Given "https://whatever.url/somedirectory/somesubdirectory#the_name
Returns "urn:ngsi-ls:somesubdirectory:the_name"
:param data:
:return:
"""
import os.path
return "urn:ngsi-ld:" + os.path.basename(data).replace("#", ":")
def std_type_name(data: str) -> str:
import os.path
return os.path.basename(data).replace("#","")
def global_transformer(f_name: str, data: str) -> str:
"""
Transforms the name into some value, depending on the function to be used (the function name is the 1st parameter).
If nothing valid is there, the same data is returned. This name can be configured in the configuration file.
:param f_name:
:param data:
:return:
"""
if f_name == "":
return data
try:
f = globals()[f_name]
return f(data)
except KeyError:
return data
def transformer(data: str) -> str:
ct = ConfigTranslator.instance
f_name = ct.get_string("urn-transform", "urn")
return global_transformer(f_name, data)
def type_transformer(data: str) -> str:
ct = ConfigTranslator.instance
f_name = ct.get_string("type-transform", "urn")
return global_transformer(f_name, data)
if __name__ == "__main__":
print(std_name_only("http://candil.namespace.com#clab-srlinux-openconfig-02-srl2"))