corelibs-file (1.0.1)
Installation
pip install --index-url --extra-index-url https://pypi.org/simple corelibs-fileAbout this package
CoreLibs File handling support
CoreLibs Python File support
This is part of the Python CoreLibs
Overview
Support for file handling like creating CRC over a file, delete files in folders recursive, bom encoding handling
Install
uv add --index opj-pypi=https://git.egplusww.jp/api/packages/PyPI/pypi/simple/ corelibs-file
Usage
This package contains the following
- corelibs_file.file_bom_encoding
- corelibs_file.file_crc
- corelibs_file.file_handling
corelibs_file.file_bom_encoding usage
Check if a file is BOM encoded with various information
from corelibs_file.file_bom_encoding import is_bom_encoded, is_bom_encoded_info, BomEncodingInfo
is_bom_encoded
Will return true if the given file is BOM encoded
def is_bom_encoded(file_path: Path) -> bool:
Exmaple:
from pathlib import Path
from corelibs_file.file_bom_encoding import is_bom_encoded
some_file: Path = '/path/to/file.csv
if is_bom_encoded(some_file):
print("File is BOM encoded")
is_bom_encoded_info
def is_bom_encoded_info(file_path: Path) -> BomEncodingInfo:
Example:
from pathlib import Path
from corelibs_file.file_bom_encoding import is_bom_encoded_info
some_file: Path = '/path/to/file.csv
file_info = is_bom_encoded_info(some_file):
Will return a dictionary with the following information
- has_bom: True if file has a BOM header
- bom_type: Returns the BOM Type, for UTF-16 and UTF-32 this can be LE or BE, UTF-8 has no dedicated type
- encoding: the encoding like "utf-8", "utf-32-le", "utf-16-le", ...
- bom_length: lengh in bytes
- bom_pattern: the detected BOM battern in bytes
corelibs_file.file_crc
from corelibs_file.file_crc import file_crc, file_name_crc
file_crc
def file_crc(file_path: Path) -> str:
Example:
from pathlib import Path
from corelibs_file.file_crc import file_crc
some_file: Path = '/path/to/file.csv
file_crc_data = file_crc(some_file)
Calculates a crc32 hash iva zlib over the file
corelibs_file.file_handling
from corelibs_file.file_handling import remove_all_in_directory, get_file_name
remove_all_in_directory
Removes all file and folders inside a directory, the given directory itself will not be removed.
Note that ignore_files just takes file names and not path + names.
Verbose will print out progres sinformation directly to the screen with print commands.
dry run will not remove anything if set
The function will return false if the directory content cannot be deleted, eg if path is not a directory
def remove_all_in_directory(
directory: Path,
ignore_files: list[str] | None = None,
verbose: bool = False,
dry_run: bool = False
) -> bool:
Example:
from pathlib import Path
from corelibs_file.file_handling import remove_all_in_directory
to_delete_folder = '/path/to/folder/';
status = remove_all_in_directory(to_delete_folder)
get_file_name
This will return the file name or the file name with base path. This can be used with the file_crc to set a file name:file crc entry
def get_file_name(file_path: Path, add_parent_folder: bool = False) -> str:
Example
from pathlib import Path
from corelibs_file.file_handling import get_file_name
some_file: Path = '/path/to/file.csv
print(f"File Name: {get_file_name('file_csv')}")
print(f"File Name with base path: {get_file_name('file_csv'), True}")
The above will print
File Name: file.csv
File Name with base path: to/file.csv
Development
UV setup
uv must be installed
Python venv setup
After clone, run the command below to install all dependenciss
uv sync
Build and Publish
uv build
uv publish --index opj-pypi --token <gitea token>
Python tests
All python tests are the tests/ folder. They are structured by the source folder layout
run them with
uv run pytest
Get a coverate report
uv run pytest --cov=<project>
uv run pytest --cov=<project> --cov-report=term-missing
Other tests
In the test-run folder usage and run tests are located, runt them below
uv run test-run/<script>