Common Functions
Common utility functions and classes used by other classes in the sc_utility package.
check_hostname_and_type(target)
staticmethod
Return whether target is a valid IPv4, IPv6, or DNS hostname. Also returns the type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target
|
str
|
The target string to validate. |
required |
Returns:
Type | Description |
---|---|
tuple[bool, str | None]
|
A tuple containing a boolean indicating validity and a string indicating the type ('ipv4', 'ipv6', or 'hostname'). |
check_internet_connection(urls=None, timeout=3)
staticmethod
Check if the system has an active internet connection by trying to open a connection to common websites.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
urls
|
list
|
A list of URLs to check for internet connectivity. Defaults to common DNS servers and websites. |
None
|
timeout
|
int
|
The timeout in seconds for each request. |
3
|
Returns:
Type | Description |
---|---|
bool
|
True if the system is connected to the internet, False otherwise. |
get_os()
staticmethod
Return the name of the operating system.
Returns:
Type | Description |
---|---|
str
|
The name of the operating system in lowercase. |
get_process_id()
staticmethod
Return the process ID of the current process.
Returns:
Type | Description |
---|---|
int
|
The process ID of the current process. |
get_project_root()
staticmethod
Return the root folder of the Python project.
Returns:
Name | Type | Description |
---|---|---|
root_dir |
Path
|
The root folder of the Python project as a Path object. |
is_probable_path(possible_path)
staticmethod
Check if the given string or Path object is likely to be a file path.
This method checks if the string is an absolute path, contains a path separator, or has a file extension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
possible_path
|
str | Path
|
The string to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the string is likely a file path, False otherwise. |
is_valid_hostname(target)
staticmethod
Return whether target is a valid IPv4, IPv6, or DNS hostname.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target
|
str
|
The target string to validate. |
required |
Returns:
Type | Description |
---|---|
bool
|
A boolean indicating validity. |
ping_host(ip_address, timeout=1)
staticmethod
Pings an IP address and returns True if the host is responding, False otherwise.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip_address
|
str
|
The IP address to ping. |
required |
timeout
|
int
|
Timeout in seconds for the ping response. Default is 1 second. |
1
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If the IP address is invalid or the ping system call fails. |
Returns:
Name | Type | Description |
---|---|---|
result |
bool
|
True if the host responds, False otherwise. |
select_file_location(file_name)
staticmethod
Select the file location for the given file name. It resolves an absolute path for the file_name as follows.
- If file_name is an absolute path, return it as a Path object.
- If file_name is a relative path (contains parent directories), return the absolute path based on the current working directory.
- If file_name is just a file name, look for it in the current working directory first, then in the root directory.
The root directly is defined as the directory containing the main script being executed (the module containing main).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
The name of the file to locate. Can be just a file name, or a relative or absolute path. |
required |
Returns:
Name | Type | Description |
---|---|---|
file_path |
Path
|
The full path to the file as a Path object. |