19
19
import _jsonnet
20
20
21
21
22
- # Returns content if worked, None if file not found, or throws an exception
23
- def try_path (dir , rel , do_encode ):
22
+ # Returns (full_path, contents) if the file was successfully retrieved,
23
+ # (full_path, None) if file not found, or throws an exception when the path
24
+ # is invalid or an IO error occured.
25
+ # It caches both hits and misses in the `cache` dict. Exceptions
26
+ # do not need to be cached, because they abort the computation anyway.
27
+ # if do_encode is True, then the file is returned as bytes
28
+ # instead of as a string (this does not make a difference in Python2).
29
+ def try_path_cached (cache , dir , rel , do_encode ):
24
30
if not rel :
25
31
raise RuntimeError ('Got invalid filename (empty string).' )
26
32
if rel [0 ] == '/' :
@@ -29,24 +35,27 @@ def try_path(dir, rel, do_encode):
29
35
full_path = dir + rel
30
36
if full_path [- 1 ] == '/' :
31
37
raise RuntimeError ('Attempted to import a directory' )
32
-
33
- if not os .path .isfile (full_path ):
34
- return full_path , None
35
- with open (full_path ) as f :
36
- if do_encode :
37
- return full_path , f .read ().encode ()
38
+ if full_path not in cache :
39
+ if not os .path .isfile (full_path ):
40
+ cache [full_path ] = None
38
41
else :
39
- return full_path , f .read ()
40
-
42
+ with open (full_path ) as f :
43
+ if do_encode :
44
+ cache [full_path ] = f .read ().encode ()
45
+ else :
46
+ cache [full_path ] = f .read ()
47
+ return full_path , cache [full_path ]
41
48
42
49
def import_callback_encode (dir , rel ):
43
- full_path , content = try_path (dir , rel , do_encode = True )
50
+ cache = {}
51
+ full_path , content = try_path_cached (cache , dir , rel , do_encode = True )
44
52
if content :
45
53
return full_path , content
46
54
raise RuntimeError ('File not found' )
47
55
48
56
def import_callback_no_encode (dir , rel ):
49
- full_path , content = try_path (dir , rel , do_encode = False )
57
+ cache = {}
58
+ full_path , content = try_path_cached (cache , dir , rel , do_encode = False )
50
59
if content :
51
60
return full_path , content
52
61
raise RuntimeError ('File not found' )
@@ -268,6 +277,5 @@ def test_double_import(self):
268
277
)
269
278
self .assertEqual (json_str , "84\n " )
270
279
271
-
272
280
if __name__ == '__main__' :
273
281
unittest .main ()
0 commit comments