Skip to content

Commit 5c360ae

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.
1 parent 05c5279 commit 5c360ae

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

examples/configloader.py

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
import os.path
44
import yaml
55

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

97
def which_path():
8+
"""
9+
10+
this checks the operation system of the user.
11+
this is used to determine the standard save location for the global gridscale config file.
12+
13+
"""
1014
#check if os is linux
1115
if(sys.platform in ("linux", "linux2")):
1216
path = "~/.config/gridscale"
@@ -31,23 +35,51 @@ def which_path():
3135
return path
3236

3337
def create_config(path):
38+
"""
39+
this will copy the currently used config file in the standard folder
40+
"""
3441
cwd = os.getcwd()
42+
path = os.path.join(cwd, path)
3543
shutil.copyfile(path, syspath)
36-
print(f"New config file created, edit config file at: {path}")
44+
print(f"New config file created, edit config file at: {syspath}")
45+
3746

3847
def load_config(path):
3948

40-
if not os.path.exists(syspath):
41-
create_config(path)
49+
"""
50+
First checking "path" to match minimum length and other requirements.
51+
52+
Then it opens the specified config file and returns all keys ehich include token and UserId.
53+
"""
4254

43-
with open(f"{syspath}", 'r') as stream:
44-
try:
45-
data = yaml.safe_load(stream)
46-
#return list of dictionaries for all projects
47-
for value in data.values():
48-
return(value)
55+
#converts path into string to avoid errors if variable is not supported
56+
path = str(path)
57+
if(len(path)>2):
58+
if (".yaml" in path):
59+
#if standard directory is not available run create_config
60+
if not os.path.exists(syspath):
61+
print("creating new config")
62+
create_config(path)
63+
#opens specified file to retrieve config tokens
64+
with open(f"{path}", 'r') as stream:
65+
try:
66+
data = yaml.safe_load(stream)
67+
# return list of dictionaries for all projects
68+
for value in data.values():
69+
return (value)
70+
except yaml.YAMLError as exc:
71+
print(exc)
4972

50-
except yaml.YAMLError as exc:
51-
print(exc)
73+
except FileNotFoundError:
74+
print("Configuration file is not found")
75+
76+
#it's not a yaml file. yaml error is raised.
77+
else:
78+
raise yaml.YAMLError
79+
80+
#filename is too short
81+
else:
82+
print("Not a valid file name!")
83+
raise AssertionError
5284

5385
syspath = which_path() + "/config.yaml"

examples/examples.py

Lines changed: 2 additions & 5 deletions
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)