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. |
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_color(Color, add_alpha=False)
Processes color values from different formats.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
Color
|
Color
|
The color to process, can be tuple or hex string. |
required |
add_alpha
|
bool
|
Whether to add alpha channel if missing. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
str | list[float]
|
str | list[float]: Processed color in the appropriate format. |
Raises:
Type | Description |
---|---|
ValueError
|
If color format is invalid. |
TypeError
|
If color type is not supported. |
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.