1414
1515import io
1616import os
17+ import pathlib
1718import platform
1819import shlex
1920import shutil
@@ -49,6 +50,25 @@ def _isdir_without_all_perms(dir_path):
4950 )
5051
5152
53+ def get_default_samples_dir ():
54+ """Get default samples directory
55+
56+ The default samples directory is computed according to the following priorities:
57+ - all systems: ``KHIOPS_SAMPLES_DIR/khiops_data/samples`` if set
58+ - Windows:
59+ - ``%PUBLIC%\\ khiops_data\\ samples`` if ``%PUBLIC%`` is defined
60+ - ``%USERPROFILE%\\ khiops_data\\ samples`` otherwise
61+ - Linux/macOS: ``$HOME/khiops_data/samples``
62+ """
63+ if "KHIOPS_SAMPLES_DIR" in os .environ and os .environ ["KHIOPS_SAMPLES_DIR" ]:
64+ samples_dir = os .environ ["KHIOPS_SAMPLES_DIR" ]
65+ elif platform .system () == "Windows" and "PUBLIC" in os .environ :
66+ samples_dir = os .path .join (os .environ ["PUBLIC" ], "khiops_data" , "samples" )
67+ else :
68+ samples_dir = str (pathlib .Path .home () / "khiops_data" / "samples" )
69+ return samples_dir
70+
71+
5272def _get_dir_status (a_dir ):
5373 """Returns the status of a local or remote directory
5474
@@ -59,11 +79,9 @@ def _get_dir_status(a_dir):
5979 # Remove initial slash on windows systems
6080 # urllib's url2pathname does not work properly
6181 a_dir_res = fs .create_resource (os .path .normpath (a_dir ))
62- a_dir_path = a_dir_res .uri_info .path
63- if platform .system () == "Windows" :
64- if a_dir_path .startswith ("/" ):
65- a_dir_path = a_dir_path [1 :]
6682
83+ # a_dir_res is a LocalFilesystemResource already
84+ a_dir_path = a_dir_res .path
6785 if not os .path .exists (a_dir_path ):
6886 status = "non-existent"
6987 elif not os .path .isdir (a_dir_path ):
@@ -98,31 +116,6 @@ def _check_samples_dir(samples_dir):
98116 )
99117
100118
101- def _extract_path_from_uri (uri ):
102- res = fs .create_resource (uri )
103- if platform .system () == "Windows" :
104- # Case of file:///<LETTER>:/<REST_OF_PATH>:
105- # Eliminate first slash ("/") from path if the first component
106- if (
107- res .uri_info .scheme == ""
108- and res .uri_info .path [0 ] == "/"
109- and res .uri_info .path [1 ].isalpha ()
110- and res .uri_info .path [2 ] == ":"
111- ):
112- path = res .uri_info .path [1 :]
113- # Case of C:/<REST_OF_PATH>:
114- # Just use the original path
115- elif len (res .uri_info .scheme ) == 1 :
116- path = uri
117- # Otherwise return URI path as-is
118- else :
119- path = res .uri_info .path
120-
121- else :
122- path = res .uri_info .path
123- return path
124-
125-
126119def _khiops_env_file_exists (env_dir ):
127120 """Check ``khiops_env`` exists relative to the specified environment dir"""
128121 khiops_env_path = os .path .join (env_dir , "khiops_env" )
@@ -399,7 +392,7 @@ def root_temp_dir(self):
399392 def root_temp_dir (self , dir_path ):
400393 # Check existence, directory status and permissions for local paths
401394 if fs .is_local_resource (dir_path ):
402- real_dir_path = _extract_path_from_uri (dir_path )
395+ real_dir_path = fs . create_resource (dir_path ). path
403396 if os .path .exists (real_dir_path ):
404397 if os .path .isfile (real_dir_path ):
405398 raise KhiopsEnvironmentError (
@@ -439,7 +432,7 @@ def create_temp_file(self, prefix, suffix):
439432 # Local resource: Effectively create the file with the python file API
440433 if fs .is_local_resource (self .root_temp_dir ):
441434 # Extract the path from the potential URI
442- root_temp_dir_path = _extract_path_from_uri (self .root_temp_dir )
435+ root_temp_dir_path = fs . create_resource (self .root_temp_dir ). path
443436
444437 # Create the temporary file
445438 tmp_file_fd , tmp_file_path = tempfile .mkstemp (
@@ -470,7 +463,7 @@ def create_temp_dir(self, prefix):
470463 """
471464 # Local resource: Effectively create the directory with the python file API
472465 if fs .is_local_resource (self .root_temp_dir ):
473- root_temp_dir_path = _extract_path_from_uri (self .root_temp_dir )
466+ root_temp_dir_path = fs . create_resource (self .root_temp_dir ). path
474467 temp_dir = tempfile .mkdtemp (prefix = prefix , dir = root_temp_dir_path )
475468 # Remote resource: Just return a highly probable unique path
476469 else :
@@ -919,7 +912,7 @@ class KhiopsLocalRunner(KhiopsRunner):
919912
920913 - Windows:
921914
922- - ``%PUBLIC%\khiops_data\samples%`` if it exists and is a directory
915+ - ``%PUBLIC%\khiops_data\samples%`` if ``%PUBLIC%`` is defined
923916 - ``%USERPROFILE%\khiops_data\samples%`` otherwise
924917
925918 - Linux and macOS:
@@ -1029,39 +1022,9 @@ def _initialize_khiops_environment(self):
10291022
10301023 def _initialize_default_samples_dir (self ):
10311024 """See class docstring"""
1032- # Set the fallback value for the samples directory
1033- home_samples_dir = Path .home () / "khiops_data" / "samples"
1034-
1035- # Take the value of an environment variable in priority, if set to
1036- # non-empty string
1037- if "KHIOPS_SAMPLES_DIR" in os .environ and os .environ ["KHIOPS_SAMPLES_DIR" ]:
1038- self ._samples_dir = os .environ ["KHIOPS_SAMPLES_DIR" ]
1039-
1040- # The samples location of Windows systems is:
1041- # - %PUBLIC%\khiops_data\samples if %PUBLIC% exists
1042- # - %USERPROFILE%\khiops_data\samples otherwise
1043- elif platform .system () == "Windows" :
1044- if "PUBLIC" in os .environ :
1045- public_samples_dir = os .path .join (
1046- os .environ ["PUBLIC" ], "khiops_data" , "samples"
1047- )
1048- else :
1049- public_samples_dir = None
1050-
1051- ok_statuses = ["ok" , "remote" ]
1052- if (
1053- public_samples_dir is not None
1054- and _get_dir_status (public_samples_dir ) in ok_statuses
1055- ):
1056- self ._samples_dir = public_samples_dir
1057- else :
1058- self ._samples_dir = str (home_samples_dir )
1059-
1060- # The default samples location on Unix systems is:
1061- # $HOME/khiops/samples on Linux and Mac OS
1062- else :
1063- self ._samples_dir = str (home_samples_dir )
1064-
1025+ samples_dir = get_default_samples_dir ()
1026+ _check_samples_dir (samples_dir )
1027+ self ._samples_dir = samples_dir
10651028 assert self ._samples_dir is not None
10661029
10671030 def _check_tools (self ):
0 commit comments