-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathsettings.py
More file actions
73 lines (57 loc) · 2.03 KB
/
settings.py
File metadata and controls
73 lines (57 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import appdirs
from scanapi.config_loader import load_config_file
GLOBAL_CONFIG_PATH = os.path.join(
appdirs.site_config_dir("scanapi"),
"scanapi.conf",
)
LOCAL_CONFIG_PATH = "./scanapi.conf"
DEFAULT_SETTINGS = {
"spec_path": "scanapi.yaml",
"output_path": None,
"template": None,
"no_report": False,
"open_browser": False,
"input_path": None,
"base_url": "${BASE_URL}",
}
class Settings(dict):
"""Class for generating Settings dictionary."""
def __init__(self):
"""
Constructs a Settings object with default values for all possible preferences.
"""
super().__init__()
self.update(DEFAULT_SETTINGS)
def save_config_file_preferences(self, config_path=None):
"""Saves the Settings object config file preferences."""
path = None
if config_path:
path = config_path
elif self.has_local_config_file:
path = LOCAL_CONFIG_PATH
elif self.has_global_config_file:
path = GLOBAL_CONFIG_PATH
if path:
self["config_path"] = path
self.update(**load_config_file(path))
def save_click_preferences(self, **preferences):
"""Saves all preference items to the Settings object."""
cleaned_preferences = {
k: v for k, v in preferences.items() if v is not None
}
self.update(**cleaned_preferences)
def save_preferences(self, **click_preferences):
"""Caller function that begins the saving of Setting preferences."""
config_path = click_preferences.get("config_path")
self.save_config_file_preferences(config_path)
self.save_click_preferences(**click_preferences)
@property
def has_global_config_file(self):
"""Checks if there is a global config file."""
return os.path.isfile(GLOBAL_CONFIG_PATH)
@property
def has_local_config_file(self):
"""Checks if there is a local config file."""
return os.path.isfile(LOCAL_CONFIG_PATH)
settings = Settings()