Core - Lib Module
anvil.lib.lib
A collection of useful functions and classes used throughout the program.
CopyFiles(old_dir, new_dir, target_file, rename=None)
Copies a file from one directory to another.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
old_dir
|
str
|
The path to the source directory. |
required |
new_dir
|
str
|
The path to the destination directory. |
required |
target_file
|
str
|
The name of the file to be copied. |
required |
rename
|
str
|
The new name for the copied file. Defaults to None. |
None
|
CopyFolder(old_dir, new_dir)
Copies a folder and all its contents to a new location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
old_dir
|
str
|
The path to the source directory. |
required |
new_dir
|
str
|
The path to the destination directory. |
required |
CreateDirectory(path)
Creates a new directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The path to the new directory. |
required |
File(name, content, directory, mode, skip_tag=False, **parameters)
Create or modify a file with the given content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the file. |
required |
content
|
str | dict
|
The content of the file. |
required |
directory
|
str
|
The directory path where the file should be created or modified. |
required |
mode
|
str
|
The file mode, either "w" (write) or "a" (append). |
required |
skip_tag
|
bool
|
Whether to skip adding the file metadata tag. Defaults to False. |
False
|
*Parameters
|
Additional StrEnum. |
required |
Note
The file content is converted to the appropriate format based on the file extension.
FileExists(path)
Checks if a file exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
_type_
|
The path to the file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the file exists, False otherwise. |
MoveFolder(old_dir, new_dir)
Moves a folder and all its contents to a new location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
old_dir
|
str
|
The path to the source directory. |
required |
new_dir
|
str
|
The path to the destination directory. |
required |
RemoveDirectory(path)
Removes a directory and all its contents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The path to the directory to be removed. |
required |
RemoveFile(path)
Removes a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The path to the file to be removed. |
required |
clamp(value, _min, _max)
Clamps a value between a minimum and maximum limit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
The value to be clamped. |
required |
_min
|
float
|
The lower limit. |
required |
_max
|
float
|
The upper limit. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The clamped value. |
convert_color(color, target=None)
Converts a color from any supported format to the target format.
Supported input formats: - Hex string: "#RRGGBB", "#RRGGBBAA", "#RGB", or "#RGBA" (short format) - RGB/RGBA (0-1): tuple of 3 or 4 floats in range [0, 1] - RGB255/RGBA255 (0-255): tuple of 3 or 4 floats in range [0, 255]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color
|
Color
|
The input color in any supported format. |
required |
target
|
RGB | RGBA | RGB255 | RGBA255 | HexRGB | HexRGBA | None
|
The target color type (RGB, RGBA, RGB255, RGBA255, HexRGB, or HexRGBA). If None, returns normalized version of input format. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Color |
RGB | RGBA | RGB255 | RGBA255 | HexRGB | HexRGBA
|
The color converted to the target format. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the color format is invalid or cannot be determined. |
TypeError
|
If the color type is not supported. |
Examples:
>>> convert_color("#FF0000", RGB)
(1.0, 0.0, 0.0)
>>> convert_color("#F00", RGB)
(1.0, 0.0, 0.0)
>>> convert_color("#F00") # No target, normalizes to full hex
"#ff0000"
>>> convert_color((1.0, 0.0, 0.0), HexRGB)
"#ff0000"
>>> convert_color((1.0, 0.0, 0.0), HexRGBA)
"#ff0000ff"
>>> convert_color((255, 0, 0), RGB)
(1.0, 0.0, 0.0)
>>> convert_color((0.5, 0.5, 0.5), RGBA)
(0.5, 0.5, 0.5, 1.0)
>>> convert_color((128, 64, 32)) # No target, normalizes to RGB255
(128.0, 64.0, 32.0)
frange(start, stop, num=1)
Generate a list of interpolated float values between start and stop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
int
|
The starting value. |
required |
stop
|
int
|
The ending value. |
required |
num
|
float
|
The number of values to generate. Defaults to 1. |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
A list of interpolated values between start and stop. |
normalize_180(angle)
Normalizes an angle between -180 and 180.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
angle
|
float
|
The angle to be normalized. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The normalized angle. |
process_subcommand(command, error_handle='Error')
Executes a subprocess command with error handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
The command to execute. |
required |
error_handle
|
str
|
Error message prefix. Defaults to "Error". |
'Error'
|
salt_from_str(s)
Generates a hash value from a string for use as a random seed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
The input string to hash. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
A hash value derived from the input string. |
validate_namespace_project_name(namespace, project_name, is_addon=False)
Validates namespace and project name according to Minecraft addon requirements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
str
|
The namespace to validate. |
required |
project_name
|
str
|
The project name to validate. |
required |
is_addon
|
bool
|
Whether this is for an addon. Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If validation fails for any requirement. |
zipit(zip_name, dir_list)
Create a ZIP archive containing multiple directories and files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
zip_name
|
The name of the ZIP archive. |
required | |
dir_list
|
dict
|
A dictionary where the keys are source directories and the values are target directories. |
required |
Note
The target directories represent the structure inside the ZIP archive.