file_utils
trestle.common.file_utils
¤
Trestle file system utils.
Attributes¤
logger = logging.getLogger(__name__)
module-attribute
¤
Classes¤
Functions¤
check_oscal_directories(root_path)
¤
Identify the state of the trestle workspace.
Traverses trestle workspace and looks for unexpected files or directories. Additional files are allowed in the Trestle root but not inside the model folders.
Source code in trestle/common/file_utils.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
|
extract_project_model_path(path)
¤
Get the base path of the trestle model project.
Source code in trestle/common/file_utils.py
201 202 203 204 205 206 207 208 |
|
extract_trestle_project_root(path)
¤
Get the trestle workspace root folder in the path.
Source code in trestle/common/file_utils.py
180 181 182 183 184 185 186 |
|
get_contextual_file_type(path)
¤
Return the file content type for files in the given directory, if it's a trestle workspace.
Source code in trestle/common/file_utils.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
|
insert_text_in_file(file_path, tag, text)
¤
Insert text lines after line containing tag.
Return True on success, False tag not found. Text is a string with appropriate \n line endings. If tag is none just add at end of file. This will only open file once if tag is not found.
Source code in trestle/common/file_utils.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
|
is_directory_name_allowed(name)
¤
Determine whether a directory name, which is a 'non-core-OSCAL activity/directory is allowed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
the name which is assumed may take the form of a relative path for task/subtasks. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the name is allowed or not allowed (interferes with assumed project directories such as catalogs). |
Source code in trestle/common/file_utils.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
|
is_hidden(file_path)
¤
Determine whether a file is hidden based on the appropriate os attributes.
This function will only work for the current file path only (e.g. not if a parent is hidden).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
Path
|
The file path for which we are testing whether the file / directory is hidden. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether or not the file is file/directory is hidden. |
Source code in trestle/common/file_utils.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
is_local_and_visible(file_path)
¤
Is the file or dir local (not a symlink) and not hidden.
Source code in trestle/common/file_utils.py
89 90 91 |
|
is_symlink(file_path)
¤
Is the file path a symlink.
Source code in trestle/common/file_utils.py
82 83 84 85 86 |
|
is_valid_project_root(path)
¤
Check if the path is a valid trestle workspace root.
Source code in trestle/common/file_utils.py
174 175 176 177 |
|
is_windows()
¤
Check if current operating system is Windows.
Source code in trestle/common/file_utils.py
40 41 42 |
|
iterdir_without_hidden_files(directory_path)
¤
Get iterator over all paths in the given directory_path excluding hidden files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory_path
|
Path
|
The directory to iterate through. |
required |
Returns:
Type | Description |
---|---|
Iterable[Path]
|
Iterator over the files in the directory excluding hidden files. |
Source code in trestle/common/file_utils.py
45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
load_file(file_path)
¤
Load JSON or YAML file content into a dict.
This is not intended to be the default load mechanism. It should only be used if a OSCAL object type is unknown but the context a user is in.
Source code in trestle/common/file_utils.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
|
make_hidden_file(file_path)
¤
Make hidden file.
Source code in trestle/common/file_utils.py
124 125 126 127 128 129 130 131 132 |
|
prune_empty_dirs(file_path, pattern)
¤
Remove directories with no subdirs and with no files matching pattern.
Source code in trestle/common/file_utils.py
299 300 301 302 303 304 305 306 307 |
|
relative_resolve(candidate, cwd)
¤
Resolve a candidate file path relative to a provided cwd.
This is to circumvent bad behaviour for resolve on windows platforms where the path must exist.
If a relative dir is passed it presumes the directory is relative to the PROVIDED cwd. If relative expansions exist (e.g. ../) the final result must still be within the cwd.
If an absolute path is provided it tests whether the path is within the cwd or not.
Source code in trestle/common/file_utils.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
|
handler: python