gws_core

Introduction
Getting Started
Developer guide
Release
Architecture
CLI
Technical documentations
Version

FileHelper

Class containing only classmethod to simplify file management

Functions
clean_extension @classmethod

Return the extension of a file without the '.' if it is present on first caracter

extension : str
extension of the file
Return type : str
copy_dir @classmethod

Copy a directory from source to destination

source_path : Union
source directory path
destination_path : Union
destination directory path
copy_dir_content_to_dir @classmethod

Copy the content of a directory to another directory

source_dir_path : Union
source directory path
destination_dir_path : Union
destination directory path
copy_file @classmethod

Copy a file from source to destination

source_path : Union
source file path
destination_path : Union
destination file path
copy_node @classmethod

Copy a file or a directory from source to destination

source_path : Union
source file or directory path
destination_path : Union
destination file or directory path
create_dir_if_not_exist @classmethod

Create a directory and all intermediate directories if they do not exist

dir_path : Union
path to the directory
Return type : Path
create_empty_file_if_not_exist @classmethod

Create an empty file if it does not exist. Creates intermediate directories if needed.

file_path : Union
path to the file
Return type : Path
create_file_response @classmethod

Create a FastAPI FileResponse from a file path

file_path : Union
path to the file
filename : str
name of the file
media_type : str
media type of the file
Return type : FileResponse
delete_dir @classmethod

Delete a directory and all its content

dir_path : Union
path to the directory
ignore_errors : bool - True
if True, do not raise an exception if the directory does not exist
delete_dir_content @classmethod

Delete all the content of a directory but not the directory itself.

dir_path : Union
path to the directory to empty
ignore_errors : bool - True
if True, do not raise an exception if the directory does not exist
delete_file @classmethod

Delete a file

file_path : Union
path to the file
ignore_errors : bool - True
if True, do not raise an exception if the file does not exist
delete_node @classmethod

Delete a file or a directory

node_path : Union
path to the file or directory
ignore_errors : bool - True
if True, do not raise an exception if the node does not exist
detect_file_encoding @classmethod

Detect the encoding of a file using charset-normalizer.

file_path : Union
path to the file
default_encoding : str - utf-8
default encoding to use if the encoding is not detected
Return type : str
exists_on_os @classmethod

Exist on local machine

path : Union
get_dir @classmethod

Return the parent directory of a file or a folder

path : Union
path to the file or folder
Return type : Path
get_dir_content_as_json @classmethod

Return the content of a directory as a json nested object.

path : Union
path to the directory
Return type : Any
get_dir_name @classmethod

Return the name of the folder. If the path is a file, return the name of the parent folder. If the path is a folder, return the name of the folder.

path : Union
path to the file or folder
get_extension @classmethod

Return the extension of a file without the '.'. Return None if no extension is found (like folder).

path : Union
path to the file or folder
Return type : str
get_extension_from_content_type @classmethod

Return the extension of a file from its content type

content_type : str
content type of the file
Return type : str
get_file_size_pretty_text @classmethod

Return a human readable size from a size in bytes. Ex : 1024 -> 1 KB

size : float
size in bytes
Return type : str
get_mime @classmethod
path : Union
Return type : str
get_name @classmethod

Return the name of the file without the extension or the name of the folder

path : Union
path to the file or folder
get_name_with_extension @classmethod

Return the name of the file with the extension or the name of the folder

path : Union
path to the file or folder
get_path @classmethod

Return a Path object from a string or a Path object

path : Union
path to convert
Return type : Path
get_size @classmethod

Return the size of a file or a folder. For folder, it is the sum of the size of all files in the folder (recursive).

path : Union
path to the file or folder
Return type : int
is_audio @classmethod
path : Union
Return type : bool
is_csv @classmethod
path : Union
Return type : bool
is_dir @classmethod
path : Union
Return type : bool
is_file @classmethod
path : Union
Return type : bool
is_image @classmethod
path : Union
Return type : bool
is_jpg @classmethod
path : Union
Return type : bool
is_json @classmethod
path : Union
Return type : bool
is_png @classmethod
path : Union
Return type : bool
is_txt @classmethod
path : Union
Return type : bool
move_file_or_dir @classmethod

Move a file or a directory from source to destination

source_path : Union
source file or directory path
destination_path : Union
destination file or directory path
sanitize_name @classmethod

Sanitize a file name, folder name or path in order to prevent injection when using the file name Basically, it keeps only the alphanumeric characters and -,_,,/

name : str
name to sanitize
Return type : str
generate_unique_fs_node_for_dir @staticmethod

Generate a unique fs node name for a list of node names. Append _1, _2... before the extension if the str is already in the list. Useful to avoid name collision when adding a file to a directory.

fs_node_name : str
name of the new fs node
dir_path : str
path to the directory
Return type : str
generate_unique_fs_node_for_list @staticmethod

Generate a unique fs node name for a list of node names. Append _1, _2... before the extension if the str is already in the list. Useful to avoid name collision when adding a file to a directory.

list_fs_node_names : List
list of existing fs node names
fs_node_name : str
name of the new fs node
Return type : str