Repository navigation
Expand file tree
/
Copy pathconfig_handler.py
More file actions
107 lines (82 loc) · 3.13 KB
/
Copy pathconfig_handler.py
File metadata and controls
107 lines (82 loc) · 3.13 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
import yaml
PATH = os.path.join(os.path.dirname(__file__), "config")
class ConfigHandler(dict):
def __init__(
self,
username=None,
password=None,
country_code=None,
store=False,
*arg,
**kwargs
):
super().__init__(*arg, **kwargs)
self._check_default_config()
self._check_app_config()
self._check_defaults()
if username and password:
self["username"] = username
self["password"] = password
if store:
self.set_username(username)
self.set_password(password)
if country_code:
self["country_code"] = country_code
if store:
self.set_country_code(country_code)
if "username" not in self.keys() or "password" not in self.keys():
raise Exception("No username and/or password provided")
def set_username(self, username):
self["username"] = username
with open(PATH + "/app.yaml", "r") as stream:
config = yaml.safe_load(stream)
config["username"] = username
with open(PATH + "/app.yaml", "w") as stream:
yaml.dump(config, stream)
def set_password(self, password):
self["password"] = password
with open(PATH + "/app.yaml", "r") as stream:
config = yaml.safe_load(stream)
config["password"] = password
with open(PATH + "/app.yaml", "w") as stream:
yaml.dump(config, stream)
def set_country_code(self, country_code):
self["country_code"] = country_code
with open(PATH + "/app.yaml", "r") as stream:
config = yaml.safe_load(stream)
config["country_code"] = country_code
with open(PATH + "/app.yaml", "w") as stream:
yaml.dump(config, stream)
def _check_defaults(self):
if self["username"] == "YOUR_USERNAME_HERE":
del self["username"]
if self["password"] == "YOUR_PASSWORD_HERE":
del self["password"]
def _check_default_config(self):
if os.path.isfile(PATH + "/default.yaml"):
with open(PATH + "/default.yaml", "r") as stream:
self.update(yaml.safe_load(stream))
else:
self.update(self._generate_default_config())
def _generate_default_config(self):
config = dict(
base_url="https://storefront-prod.{}.picnicinternational.com/api/",
api_version="15",
country_code="NL",
)
with open(PATH + "/default.yaml", "w") as stream:
yaml.dump(config, stream)
return config
def _check_app_config(self):
if os.path.isfile(PATH + "/app.yaml"):
with open(PATH + "/app.yaml", "r") as stream:
self.update(yaml.safe_load(stream))
else:
self.update(self._generate_app_config())
def _generate_app_config(self):
config = dict(username="YOUR_USERNAME_HERE", password="YOUR_PASSWORD_HERE",)
with open(PATH + "/app.yaml", "w") as stream:
yaml.dump(config, stream)
return config
__all__ = ["ConfigHandler"]