3
3
import os .path
4
4
import yaml
5
5
6
- #TODO: change active project
7
- project = "somthing-else"
8
6
9
7
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
+ """
10
14
#check if os is linux
11
15
if (sys .platform in ("linux" , "linux2" )):
12
16
path = "~/.config/gridscale"
@@ -31,23 +35,51 @@ def which_path():
31
35
return path
32
36
33
37
def create_config (path ):
38
+ """
39
+ this will copy the currently used config file in the standard folder
40
+ """
34
41
cwd = os .getcwd ()
42
+ path = os .path .join (cwd , path )
35
43
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
+
37
46
38
47
def load_config (path ):
39
48
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
+ """
42
54
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 )
49
72
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
52
84
53
85
syspath = which_path () + "/config.yaml"
0 commit comments