File tree 2 files changed +35
-1
lines changed
2 files changed +35
-1
lines changed Original file line number Diff line number Diff line change 1
1
from pathlib import Path
2
2
from typing import Dict , List , Optional
3
3
4
+ import json
4
5
import yaml
6
+ import mimetypes
5
7
from pydantic import BaseModel
6
8
7
9
@@ -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
1
import pathlib
2
+ import pytest
2
3
3
4
from openapi_python_client .config import Config
5
+ import yaml
6
+ import json
4
7
5
8
6
9
def test_load_from_path (mocker ):
@@ -28,3 +31,28 @@ def test_load_from_path(mocker):
28
31
assert config .project_name_override == "project-name"
29
32
assert config .package_name_override == "package_name"
30
33
assert config .package_version_override == "package_version"
34
+
35
+
36
+ DATA = {"class_overrides" : {"Class1" : {"class_name" : "ExampleClass" , "module_name" : "example_module" }}}
37
+
38
+
39
+ def json_with_tabs (d ):
40
+ return json .dumps (d , indent = 4 ).replace (" " , "\t " )
41
+
42
+
43
+ @pytest .mark .parametrize (
44
+ "filename,dump" ,
45
+ [
46
+ ("example.yml" , yaml .dump ),
47
+ ("example.json" , json .dumps ),
48
+ ("example.yaml" , yaml .dump ),
49
+ ("example.json" , json_with_tabs ),
50
+ ],
51
+ )
52
+ def test_load_filenames (tmp_path , filename , dump ):
53
+ yml_file = tmp_path .joinpath (filename )
54
+ with open (yml_file , "w" ) as f :
55
+ f .write (dump (DATA ))
56
+
57
+ config = Config .load_from_path (yml_file )
58
+ assert config .class_overrides == DATA ["class_overrides" ]
You can’t perform that action at this time.
0 commit comments