Skip to content

Commit 415112c

Browse files
author
Jonathan Lutterbeck
committed
Improve code to run checks
The tests now are green. I just changed it to return the contents of the yaml file. In the test no path has to be passed. This is already done in the configloader. Can I remove this from the test @bkircher ?
1 parent 05c3775 commit 415112c

File tree

2 files changed

+17
-41
lines changed

2 files changed

+17
-41
lines changed

examples/configloader.py

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def which_path():
2626
if not os.path.exists(path):
2727
os.makedirs(path)
2828
else:
29-
"Operating System not supported"
29+
print("Operating System not supported")
3030

3131
return path
3232

@@ -35,45 +35,18 @@ def create_config(path):
3535
shutil.copyfile(f"{cwd}/config.yaml", path)
3636
print(f"New config file created, edit config file at: {path}")
3737

38-
def load_config(projectname, goal, path):
39-
with open(f"{path}", 'r') as stream:
40-
try:
41-
data = yaml.safe_load(stream)
42-
43-
for value in data.values():
44-
for x in range(len(value)):
45-
result = value[x]
46-
#returns userID and token for the selected project
47-
if (result.get("name") == projectname):
48-
userid = result.get("userId")
49-
token = result.get("token")
50-
except yaml.YAMLError as exc:
51-
print(exc)
52-
53-
if(goal == "id"):
54-
return userid
55-
elif(goal == "token"):
56-
return token
57-
58-
def load_token(project):
38+
def load_config(path):
5939
syspath = which_path() + "/config.yaml"
60-
goal = "token"
6140

62-
# check if config file exists
6341
if not os.path.exists(syspath):
6442
create_config(syspath)
65-
return load_config(project, goal, syspath)
66-
else:
67-
return load_config(project, goal, syspath)
68-
6943

70-
def load_userid(project):
71-
syspath = which_path() + "/config.yaml"
72-
goal = "id"
44+
with open(f"{syspath}", 'r') as stream:
45+
try:
46+
data = yaml.safe_load(stream)
47+
#return list of dictionaries for all projects
48+
for value in data.values():
49+
return(value)
7350

74-
# check if config file exists
75-
if not os.path.exists(syspath):
76-
create_config(syspath)
77-
return load_config(project, goal, syspath)
78-
else:
79-
return load_config(project, goal, syspath)
51+
except yaml.YAMLError as exc:
52+
print(exc)

examples/examples.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
from pprint import pprint
44
from uuid import uuid4
5+
import os
56
from index_by.key import index_by_key
6-
from configloader import load_token, load_userid
7+
from configloader import load_config
78

89
from gs_api_client import SyncGridscaleApiClient, GridscaleApiClient, models
910
from gs_api_client import Configuration
1011

1112

13+
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
1214

1315
if __name__ == '__main__':
1416

@@ -19,9 +21,10 @@
1921
api_config.host = 'https://api.gridscale.io'
2022

2123
#TODO: Change project
22-
project = "default"
23-
api_config.api_key['X-Auth-Token'] = load_token(project)
24-
api_config.api_key['X-Auth-UserId'] = load_userid(project)
24+
example_config = os.path.join(CURRENT_DIR, "config.yaml")
25+
configfile = load_config(example_config)
26+
api_config.api_key['X-Auth-Token'] = configfile[0].get("token")
27+
api_config.api_key['X-Auth-UserId'] = configfile[0].get("userId")
2528
api_config.debug = True
2629

2730
print('-' * 80)

0 commit comments

Comments
 (0)