11import os
2+ import platform
3+ from pathlib import Path
24
35ROOT = os .path .abspath (os .path .dirname (__file__ ))
46
@@ -7,6 +9,22 @@ class WorkspaceError(Exception):
79 pass
810
911
12+ def find_platform_location (dir_type ):
13+ """
14+ Attempt to find and create operating system data folders
15+ """
16+ if platform .system () == "Linux" :
17+ home_dir = os .path .expanduser ("~" )
18+ if dir_type == "config" :
19+ xdg_dir = os .environ .get ("XDG_CONFIG_HOME" ) or os .path .join (home_dir , ".config" )
20+ elif dir_type == "data" :
21+ xdg_dir = os .environ .get ("XDG_DATA_HOME" ) or os .path .join (home_dir , ".local/share" )
22+ ev3sim_dir = os .path .join (xdg_dir , "ev3sim" )
23+ Path (ev3sim_dir ).mkdir (parents = True , exist_ok = True )
24+ return ev3sim_dir
25+ return None
26+
27+
1028def find_abs (filepath , allowed_areas = None ):
1129 """
1230 Attempt to find a reference file, from this list of appropriate places specified.
@@ -51,6 +69,17 @@ def find_abs(filepath, allowed_areas=None):
5169 if not WORKSPACE :
5270 continue
5371 path = os .path .join (WORKSPACE , area [10 :], filepath )
72+ elif area .startswith ("platform" ):
73+ if area .startswith ("platform/config" ):
74+ pl = find_platform_location ("config" )
75+ if pl is None :
76+ continue
77+ path = os .path .join (pl , area [16 :], filepath )
78+ elif area .startswith ("platform/data" ):
79+ pl = find_platform_location ("data" )
80+ if pl is None :
81+ continue
82+ path = os .path .join (pl , area [14 :], filepath )
5483 elif area == "local" :
5584 path = filepath
5685 elif area .startswith ("local" ):
@@ -91,12 +120,16 @@ def fix():
91120
92121def find_abs_directory (dirpath , create = True ):
93122 try :
94- return find_abs ("" , allowed_areas = [dirpath ])
123+ if not isinstance (dirpath , list ):
124+ dirpath = [dirpath ]
125+ return find_abs ("" , allowed_areas = dirpath )
95126 except ValueError as e :
96127 if not create :
97128 raise e
98129 else :
99130 # Remove one part of the directory, then try again.
131+ if isinstance (dirpath , list ):
132+ dirpath = dirpath [0 ]
100133 rest , single = os .path .split (dirpath .rstrip ("/" ))
101134 if rest == dirpath :
102135 raise ValueError (f"Find abs dir failed with input { dirpath } " )
0 commit comments