File tree 2 files changed +36
-1
lines changed
2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change
1
+ import json
2
+ import mimetypes
1
3
from pathlib import Path
2
4
from typing import Dict , List , Optional
3
5
@@ -35,6 +37,10 @@ class Config(BaseModel):
35
37
@staticmethod
36
38
def load_from_path (path : Path ) -> "Config" :
37
39
"""Creates a Config from provided JSON or YAML file and sets a bunch of globals from it"""
38
- config_data = yaml .safe_load (path .read_text ())
40
+ mime = mimetypes .guess_type (path .as_uri (), strict = True )[0 ]
41
+ if mime == "application/json" :
42
+ config_data = json .loads (path .read_text ())
43
+ else :
44
+ config_data = yaml .safe_load (path .read_text ())
39
45
config = Config (** config_data )
40
46
return config
Original file line number Diff line number Diff line change
1
+ import json
1
2
import pathlib
2
3
4
+ import pytest
5
+ import yaml
6
+
3
7
from openapi_python_client .config import Config
4
8
5
9
@@ -28,3 +32,28 @@ def test_load_from_path(mocker):
28
32
assert config .project_name_override == "project-name"
29
33
assert config .package_name_override == "package_name"
30
34
assert config .package_version_override == "package_version"
35
+
36
+
37
+ DATA = {"class_overrides" : {"Class1" : {"class_name" : "ExampleClass" , "module_name" : "example_module" }}}
38
+
39
+
40
+ def json_with_tabs (d ):
41
+ return json .dumps (d , indent = 4 ).replace (" " , "\t " )
42
+
43
+
44
+ @pytest .mark .parametrize (
45
+ "filename,dump" ,
46
+ [
47
+ ("example.yml" , yaml .dump ),
48
+ ("example.json" , json .dumps ),
49
+ ("example.yaml" , yaml .dump ),
50
+ ("example.json" , json_with_tabs ),
51
+ ],
52
+ )
53
+ def test_load_filenames (tmp_path , filename , dump ):
54
+ yml_file = tmp_path .joinpath (filename )
55
+ with open (yml_file , "w" ) as f :
56
+ f .write (dump (DATA ))
57
+
58
+ config = Config .load_from_path (yml_file )
59
+ assert config .class_overrides == DATA ["class_overrides" ]
You can’t perform that action at this time.
0 commit comments