@@ -63,36 +63,44 @@ def read_from_url(url: str):
6363 )
6464
6565
66- def read_from_source (source : str ):
66+ def read_from_source (source : str , cwd : Path = None ):
6767 """
6868 Tries to read a JSON or YAML file from a given source.
6969 The source can be a path to a file, or a URL.
70+
71+ Attempts to take from a relative path if `cwd` is provided.
7072 """
71- source_path = Path (source )
73+ potential_paths = [Path (source )]
74+
75+ if cwd :
76+ potential_paths .append (cwd / source )
7277
73- if source_path .exists ():
74- if not source_path .is_file ():
75- raise ValueError ("The given path is not a file path." )
78+ for source_path in potential_paths :
79+ if source_path .exists ():
80+ if not source_path .is_file ():
81+ raise ValueError ("The given path is not a file path." )
7682
77- logger .debug ("Reading from file %s" , source )
83+ logger .debug ("Reading from file %s" , source )
7884
79- file_path = source .lower ()
85+ file_path = source .lower ()
8086
81- if file_path .endswith (".json" ):
82- return read_from_json_file (source_path )
87+ if file_path .endswith (".json" ):
88+ return read_from_json_file (source_path )
8389
84- if file_path .endswith (".yaml" ) or file_path .endswith (".yml" ):
85- return read_from_yaml_file (source_path )
90+ if file_path .endswith (".yaml" ) or file_path .endswith (".yml" ):
91+ return read_from_yaml_file (source_path )
92+ else :
93+ raise ValueError ("Unsupported source file." )
94+ else :
95+ logger .debug ("Path %s does not exist, trying next." , source )
8696
87- raise ValueError ("Unsupported source file." )
97+ source_lower = source .lower ()
98+
99+ if source_lower .startswith ("http://" ) or source_lower .startswith ("https://" ):
100+ # fetch with a web request, read - ensure that it's JSON or YAML!
101+ return read_from_url (source )
88102 else :
89- source_lower = source .lower ()
90-
91- if source_lower .startswith ("http://" ) or source_lower .startswith ("https://" ):
92- # fetch with a web request, read - ensure that it's JSON or YAML!
93- return read_from_url (source )
94- else :
95- raise ValueError (
96- "Invalid source: it must be either a path to a "
97- ".json or .yaml file, or a valid URL."
98- )
103+ raise ValueError (
104+ "Invalid source: it must be either a path to a "
105+ ".json or .yaml file, or a valid URL."
106+ )
0 commit comments