Skip to content

Commit 5fe3df0

Browse files
author
Jonathan Lutterbeck
committed
improvements to check validity of config file
Checks have been added to verify file name lenght and to check if it's really a .yaml file. All tests are now green. Fixed error Moved the syspath cariable to be globally available Fix error path variable was named wrong Try to fix github action error Changed file name for config file. Fixed Test Fixed copyfile for test case Check if config is valid load_config now gives correct error messages and does not catch any exceptions. The functions are now also properly seperated. removed print statements Print statements have been removed and replaced them with RuntimeErrors where applicable. which_path was renamed to default_config_path. Improved code I merged the two isInstance calls to improve readability.
1 parent a658543 commit 5fe3df0

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

examples/configloader.py

+29-19
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import shutil
22
import sys
33
import os.path
4+
import pathlib
45
import yaml
56

6-
#TODO: change active project
7-
project = "somthing-else"
87

9-
def which_path():
8+
def default_config_path():
9+
"""
10+
11+
this checks the operation system of the user.
12+
this is used to determine the standard save location for the global gridscale config file.
13+
14+
"""
1015
#check if os is linux
1116
if(sys.platform in ("linux", "linux2")):
1217
path = "~/.config/gridscale"
@@ -26,27 +31,32 @@ def which_path():
2631
if not os.path.exists(path):
2732
os.makedirs(path)
2833
else:
29-
print("Operating System not supported")
34+
raise RuntimeError("Operating system not supported")
3035

3136
return path
3237

33-
def create_config(path):
34-
cwd = os.getcwd()
35-
shutil.copyfile(f"{cwd}/config.yaml", syspath)
36-
print(f"New config file created, edit config file at: {syspath}")
3738

38-
def load_config(path):
39-
syspath = which_path() + "/config.yaml"
39+
def create_config(path):
40+
"""
41+
this will copy the currently used config file in the standard folder
42+
"""
43+
syspath = default_config_path() + "/config.yaml"
44+
shutil.copyfile(path, syspath)
4045

41-
if not os.path.exists(syspath):
42-
create_config(syspath)
4346

44-
with open(f"{syspath}", 'r') as stream:
45-
try:
47+
def load_config(path):
48+
"""
49+
First checking "path" to match minimum length and other requirements.
50+
51+
Then it opens the specified config file and returns all keys which include token and UserId.
52+
"""
53+
# opens specified file to retrieve config tokens
54+
if isinstance(path, (pathlib.Path, str)):
55+
assert path
56+
with open(f"{path}", 'r') as stream:
4657
data = yaml.safe_load(stream)
47-
#return list of dictionaries for all projects
58+
# return list of dictionaries for all projects
4859
for value in data.values():
49-
return(value)
50-
51-
except yaml.YAMLError as exc:
52-
print(exc)
60+
return (value)
61+
else:
62+
raise AssertionError

examples/examples.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
from gs_api_client import Configuration
1111

1212

13-
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
14-
1513
if __name__ == '__main__':
1614

1715
# run `pip3 install index_by` before executing this file
@@ -20,9 +18,8 @@
2018
# api_config.debug = True
2119
api_config.host = 'https://api.gridscale.io'
2220

23-
#TODO: Change project
24-
example_config = os.path.join(CURRENT_DIR, "config.yaml")
25-
configfile = load_config(example_config)
21+
#TODO: Change filename
22+
configfile = load_config("config.yaml")
2623
api_config.api_key['X-Auth-Token'] = configfile[0].get("token")
2724
api_config.api_key['X-Auth-UserId'] = configfile[0].get("userId")
2825
api_config.debug = True

0 commit comments

Comments
 (0)