14
14
from pathlib import Path
15
15
from typing import Dict , Sequence , Tuple , Union
16
16
17
- from monai .bundle .reference_resolver import ReferenceResolver
18
17
from monai .config import PathLike
19
18
from monai .utils import ensure_tuple , look_up_option , optional_import
20
19
@@ -37,8 +36,10 @@ class ConfigReader:
37
36
"""
38
37
39
38
suffixes = ("json" , "yaml" , "yml" )
40
- path_match = re .compile (rf"(.*\.({ '|' .join (suffixes )} )$)" , re .IGNORECASE )
39
+ suffix_match = rf"\.({ '|' .join (suffixes )} )"
40
+ path_match = rf"(.*{ suffix_match } $)"
41
41
meta_key = "<meta>" # field key to save metadata
42
+ sep = "#" # separator for file path and the id of content in the file
42
43
43
44
def __init__ (self ):
44
45
self .config : Dict = {self .meta_key : {}}
@@ -54,7 +55,7 @@ def load_config_file(cls, filepath: PathLike, **kwargs):
54
55
55
56
"""
56
57
_filepath : str = str (Path (filepath ))
57
- if not cls .path_match .findall (_filepath ):
58
+ if not re . compile ( cls .path_match , re . IGNORECASE ) .findall (_filepath ):
58
59
raise ValueError (f'unknown file input: "{ filepath } "' )
59
60
with open (_filepath ) as f :
60
61
if _filepath .lower ().endswith (cls .suffixes [0 ]):
@@ -105,17 +106,18 @@ def split_path_id(cls, src: str) -> Tuple[str, str]:
105
106
"""
106
107
Split `src` string into two parts: a config file path and component id.
107
108
The file path should end with `(json|yaml|yml)`. The component id should be separated by `#` if it exists.
109
+ If no path or no id, return "".
108
110
109
111
Args:
110
112
src: source string to split.
111
113
112
114
"""
113
- path , * ids = src .rsplit (ReferenceResolver .sep , 1 )
114
- path_name = cls .path_match .findall (path )
115
- if not path_name :
115
+ result = re .compile (cls .suffix_match , re .IGNORECASE ).findall (src )
116
+ if len (result ) != 1 :
116
117
return "" , src # the src is a pure id
117
- ids_string : str = ids [0 ] if ids else ""
118
- return path_name [0 ][0 ], ids_string
118
+ items = src .split (result [0 ])
119
+ # return file path and the
120
+ return items [0 ] + result [0 ], items [1 ][len (cls .sep ) :] if items [1 ] != "" else ""
119
121
120
122
def read_meta (self , f : Union [PathLike , Sequence [PathLike ], Dict ], ** kwargs ):
121
123
"""
0 commit comments