1313# linkdUtil:
1414# link related functions
1515#
16- import time
16+ from h5json .time_util import getNow
17+ from h5json .link_util import validateLinkName , getLinkClass , getLinkPath , getLinkFilePath
1718
1819from .. import hsds_logger as log
1920
2021
21- def validateLinkName (name ):
22- """ verify the link name is valid """
23- if not isinstance (name , str ):
24- msg = "Unexpected type for link name"
25- log .warn (msg )
26- raise ValueError (msg )
27- if name .find ("/" ) >= 0 :
28- msg = "link name contains slash"
29- log .warn (msg )
30- raise ValueError (msg )
31-
32-
33- def getLinkClass (link_json ):
34- """ verify this is a valid link
35- returns the link class """
36- log .debug (f"getLinkClass({ link_json } )" )
37- if "class" in link_json :
38- link_class = link_json ["class" ]
39- else :
40- link_class = None
41- if "h5path" in link_json and "id" in link_json :
42- msg = "link tgt_id and h5path both set"
43- log .warn (msg )
44- raise ValueError (msg )
45- if "id" in link_json :
46- tgt_id = link_json ["id" ]
47- if not isinstance (tgt_id , str ) or len (tgt_id ) < 38 :
48- msg = f"link with invalid id: { tgt_id } "
49- log .warn (msg )
50- raise ValueError (msg )
51- if tgt_id [:2 ] not in ("g-" , "t-" , "d-" ):
52- msg = "link tgt must be group, datatype or dataset uuid"
53- log .warn (msg )
54- raise ValueError (msg )
55- if link_class :
56- if link_class != "H5L_TYPE_HARD" :
57- msg = f"expected link class to be H5L_TYPE_HARD but got: { link_class } "
58- log .warn (msg )
59- raise ValueError (msg )
60- else :
61- link_class = "H5L_TYPE_HARD"
62- elif "h5path" in link_json :
63- h5path = link_json ["h5path" ]
64- log .debug (f"link path: { h5path } " )
65- if "h5domain" in link_json :
66- if link_class :
67- if link_class != "H5L_TYPE_EXTERNAL" :
68- msg = f"expected link class to be H5L_TYPE_EXTERNAL but got: { link_class } "
69- log .warn (msg )
70- raise ValueError (msg )
71- else :
72- link_class = "H5L_TYPE_EXTERNAL"
73- else :
74- if link_class :
75- if link_class != "H5L_TYPE_SOFT" :
76- msg = f"expected link class to be H5L_TYPE_SOFT but got: { link_class } "
77- log .warn (msg )
78- raise ValueError (msg )
79- else :
80- link_class = "H5L_TYPE_SOFT"
81- else :
82- msg = "link with no id or h5path"
83- log .warn (msg )
84- raise ValueError (msg )
85-
86- return link_class
87-
88-
89- def isEqualLink (link1 , link2 ):
90- """ Return True if the two links are the same """
91-
92- for obj in (link1 , link2 ):
93- if not isinstance (obj , dict ):
94- raise TypeError (f"unexpected type: { type (obj )} " )
95- if "class" not in obj :
96- raise TypeError ("expected class key for link" )
97- if link1 ["class" ] != link2 ["class" ]:
98- return False # different link types
99- link_class = link1 ["class" ]
100- if link_class == "H5L_TYPE_HARD" :
101- for obj in (link1 , link2 ):
102- if "id" not in obj :
103- raise TypeError (f"expected id key for link: { obj } " )
104- if link1 ["id" ] != link2 ["id" ]:
105- return False
106- elif link_class == "H5L_TYPE_SOFT" :
107- for obj in (link1 , link2 ):
108- if "h5path" not in obj :
109- raise TypeError (f"expected h5path key for link: { obj } " )
110- if link1 ["h5path" ] != link2 ["h5path" ]:
111- return False
112- elif link_class == "H5L_TYPE_EXTERNAL" :
113- for obj in (link1 , link2 ):
114- for k in ("h5path" , "h5domain" ):
115- if k not in obj :
116- raise TypeError (f"expected { k } key for link: { obj } " )
117- if link1 ["h5path" ] != link2 ["h5path" ]:
118- return False
119- if link1 ["h5domain" ] != link2 ["h5domain" ]:
120- return False
121- else :
122- raise TypeError (f"unexpected link class: { link_class } " )
123- return True
124-
125-
126- def h5Join (path , paths ):
127- h5path = path
128- if not paths :
129- return h5path
130- if isinstance (paths , str ):
131- paths = (paths ,)
132- for s in paths :
133- if h5path [- 1 ] != "/" :
134- h5path += "/"
135- h5path += s
136- return h5path
137-
138-
13922def getRequestLink (title , link_json , predate_max_time = 0.0 ):
14023 """ return normalized link from request json
14124 Throw value error if badly formatted """
@@ -148,16 +31,11 @@ def getRequestLink(title, link_json, predate_max_time=0.0):
14831 log .debug (f"getRequestLink title: { title } link_json: { link_json } " )
14932 link_item = {} # normalized link item to return
15033
151- now = time . time ()
34+ now = getNow ()
15235
15336 validateLinkName (title ) # will raise ValueError is invalid
15437
15538 link_class = getLinkClass (link_json )
156- if "class" in link_item :
157- if link_class != link_json ["class" ]:
158- msg = f"expected link class of: { link_class } but got { link_json } "
159- log .warn (msg )
160- raise ValueError (msg )
16139
16240 link_item = {"class" : link_class }
16341
@@ -169,17 +47,10 @@ def getRequestLink(title, link_json, predate_max_time=0.0):
16947 link_item ["id" ] = link_json ["id" ]
17048 else :
17149 if link_class in ("H5L_TYPE_SOFT" , "H5L_TYPE_EXTERNAL" ):
172- if "h5path" not in link_json :
173- msg = "expected h5path key for soft link"
174- log .warn (msg )
175- raise ValueError (msg )
176- link_item ["h5path" ] = link_json ["h5path" ]
50+ link_item ["h5path" ] = getLinkPath (link_json )
17751
17852 if link_class == "H5L_TYPE_EXTERNAL" :
179- if "h5domain" not in link_json :
180- msg = "expected h5domain key for external link"
181- log .warn (msg )
182- raise ValueError (msg )
53+ link_item ["file" ] = getLinkFilePath (link_json )
18354
18455 if "created" in link_json :
18556 created = link_json ["created" ]
0 commit comments