File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change 1818from os import mkdir
1919from os import path as osp
2020
21+ # Simple !include constructor for YAML to allow reusing fragments across files.
22+ # Usage examples:
23+ # - !include path/to/file.yaml -> includes full file content
24+ # - !include path/to/file.yaml::doc_mapping -> includes the 'doc_mapping' key
25+ # - !include path/to/file.yaml::a.b.c -> includes nested key a -> b -> c
26+ def _yaml_include (loader , node ):
27+ value = loader .construct_scalar (node )
28+ if "::" in value :
29+ filepath , subpath = value .split ("::" , 1 )
30+ else :
31+ filepath , subpath = value , None
32+ with open (filepath , "r" ) as f :
33+ included = yaml .load (f , Loader = yaml .Loader )
34+ if subpath :
35+ cur = included
36+ for seg in filter (None , subpath .split ("." )):
37+ if not isinstance (cur , dict ) or seg not in cur :
38+ raise KeyError (f"!include path '{ subpath } ' not found in { filepath } " )
39+ cur = cur [seg ]
40+ return cur
41+ return included
42+
43+ # Register the constructor on the default Loader used by this script.
44+ yaml .Loader .add_constructor ("!include" , _yaml_include )
45+
2146def debug_http ():
2247 old_send = http .client .HTTPConnection .send
2348 def new_send (self , data ):
You can’t perform that action at this time.
0 commit comments