33import os .path
44import yaml
55
6- #TODO: change active project
7- project = "somthing-else"
86
97def 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
3337def 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
3847def 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
5385syspath = which_path () + "/config.yaml"
0 commit comments