Skip to content

Commit 8d38150

Browse files
author
Jonathan, Lutterbeck
committed
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.
1 parent 5485293 commit 8d38150

File tree

1 file changed

+13
-33
lines changed

1 file changed

+13
-33
lines changed

examples/configloader.py

+13-33
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import sys
33
import os.path
44
import yaml
5+
import pathlib
6+
57

68

79
def which_path():
@@ -34,12 +36,12 @@ def which_path():
3436

3537
return path
3638

39+
3740
def create_config(path):
3841
"""
3942
this will copy the currently used config file in the standard folder
4043
"""
41-
cwd = os.getcwd()
42-
path = os.path.join(cwd, path)
44+
syspath = which_path() + "/config.yaml"
4345
shutil.copyfile(path, syspath)
4446
print(f"New config file created, edit config file at: {syspath}")
4547

@@ -49,37 +51,15 @@ def load_config(path):
4951
"""
5052
First checking "path" to match minimum length and other requirements.
5153
52-
Then it opens the specified config file and returns all keys ehich include token and UserId.
54+
Then it opens the specified config file and returns all keys which include token and UserId.
5355
"""
54-
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)
72-
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
56+
# opens specified file to retrieve config tokens
57+
if isinstance(path, str) or isinstance(path, pathlib.Path):
58+
assert path
59+
with open(f"{path}", 'r') as stream:
60+
data = yaml.safe_load(stream)
61+
# return list of dictionaries for all projects
62+
for value in data.values():
63+
return (value)
8164
else:
82-
print("Not a valid file name!")
8365
raise AssertionError
84-
85-
syspath = which_path() + "/config.yaml"

0 commit comments

Comments
 (0)