fireworkUtils.py
The fireworkUtils.py
file provides utility functions to assist in managing the operations of the pyrotechnic network. These utilities are organized into two classes: ParseUtils
and PrintUtils
, and a color class: Colors
.
- The
Colors
class offers constants for colorizing console output for a more visually engaging and understandable user experience. - The
ParseUtils
class includes static methods that parse user commands and return structured data. This is essential for interpreting and executing user commands accurately and efficiently. - The
PrintUtils
class provides static methods for presenting data to the user in a clear, colored, and formatted manner. It aids in diagnosing errors, understanding the state of the system, and interacting with the user interface.
These utilities work together to streamline user interactions, data parsing, and user feedback, making the management of large-scale firework displays more user-friendly and efficient.
ParseUtils
parse_fire_command
@staticmethod
def parse_fire_command(command):
pattern = r'-m\s+(\w+)\s+-d\s+(\d+)\s+-i\s+(\d+)'
matches = re.match(pattern, command)
if matches:
moduleName = matches.group(1)
moduleId = int(matches.group(2))
igniteNumber = int(matches.group(3))
return moduleName, moduleId, igniteNumber
return None
Parameter(s):
Parameter | Expected Type | Description |
---|---|---|
command | String | The command string to be parsed. |
Response:
Output | Type | Description |
---|---|---|
moduleName | String | The name of the module extracted from the command. |
moduleId | int | The id of the module extracted from the command. |
igniteNumber | int | The ignition number extracted from the command. |
The parse_fire_command
method is a static method of the ParseUtils
class. This method is used to parse the fire command provided in the format -m <module name> -d <module id> -i <ignition number>
.
It uses regex to match and extract the module name, module id, and the ignition number from the command.
If the pattern is matched, it returns a tuple containing the module name, module id, and ignition number. Otherwise, it returns None
.
parse_generate_sequence
@staticmethod
def parse_generate_sequence(command):
pattern = r'(generate sequence)\s+(-f)\s+(RANDOM|SEQUENTIAL)'
matches = re.match(pattern, command)
if matches:
command = matches.group(1)
modifier = matches.group(2)
seq_format = matches.group(3)
return command, modifier, seq_format
return None
Parameter(s):
Parameter | Type | Description |
---|---|---|
command | String | The command string to be parsed. Expected format: 'generate sequence -f RANDOM' or 'generate sequence -f SEQUENTIAL' |
Response:
Output | Type | Description |
---|---|---|
command | String | The command string 'generate sequence' |
modifier | String | The modifier '-f' |
seq_format | String | The sequence format 'RANDOM' or 'SEQUENTIAL' |
The parse_generate_sequence
static method takes a command string as input and tries to match it with a certain pattern. This pattern expects a command string starting with 'generate sequence' followed by a '-f' and then either 'RANDOM' or 'SEQUENTIAL'. If the command matches this pattern, the method will return the command 'generate sequence', the modifier '-f', and the sequence format (either 'RANDOM' or 'SEQUENTIAL'). If the command does not match this pattern, the method will return None. This method is used to parse and extract important information from the command string.
parse_sequence_swap
@staticmethod
def parse_sequence_swap(command):
pattern = r'sequence swap (\d+) (\d+)'
matches = re.match(pattern, command)
if matches:
index1 = int(matches.group(1))
index2 = int(matches.group(2))
return index1, index2
return None
Parameter(s):
Parameter | Type | Description |
---|---|---|
command | String | The input string command to be parsed. |
Response:
Output | Type | Description |
---|---|---|
index1 | int | The first index specified in the command string. |
index2 | int | The second index specified in the command string. |
The parse_sequence_swap
method in the ParseUtils
class is used to parse a command string that follows the format 'sequence swap n m', where 'n' and 'm' are integers.
This method employs regular expressions to match and extract these integer values from the command string. The extracted integers represent the indices of two items in a sequence that are to be swapped.
If the command string matches the expected format, the method returns a tuple of the two integer indices. If the command string does not match the expected format, the method returns None
.
PrintUtils
host
@staticmethod
def host(message):
pattern = r"(\d+\.\d+\.\d+\.\d+)\s+-\s+(.*?)\s+(\d+)/(\d+)\s+-\s+Ignitions:\s+(.*)"
matches = re.search(pattern, message)
ip, moduleName, moduleId, modules, ignitions = matches.groups()
formatted_ip = f"{Colors.LIGHT_BLUE}{ip}{Colors.RESET}"
formatted_dash = f"{Colors.WHITE}-"
formatted_moduleName = f"{Colors.DARK_BLUE}{moduleName}{Colors.RESET}"
formatted_moduleId = f"{Colors.YELLOW}{moduleId}{Colors.RESET}"
formatted_slash = f"{Colors.WHITE}/"
formatted_modules = f"{Colors.PURPLE}{modules}{Colors.RESET}"
formatted_ignitions = " ".join(
[
f"{Colors.GREEN}[{val}]{Colors.RESET}" if val.isdigit() else f"{Colors.RED}[{val}]{Colors.RESET}"
for val in re.findall(r"\[(.*?)\]", ignitions)
]
)
print(f"{formatted_ip} {formatted_dash} {formatted_moduleName} {formatted_moduleId}{formatted_slash}{formatted_modules} - Ignitions: {formatted_ignitions}")
Parameter(s):
Parameter | Type | Description |
---|---|---|
message | String | A string containing host information. |
The host
method is a utility function in the PrintUtils
class. It is designed to display the host information in a formatted, colored output. The method parses the input string to extract the IP address, module name, module ID, total modules, and ignition information. Each part of the output is colored differently for easy readability.
sequence
@staticmethod
def sequence(message):
pattern = r"(\d+\.\d+\.\d+\.\d+)\s+-\s+(.*?)\s+(\d+)/(\d+)\s+-\s+Ignitions:\s+(.*)"
matches = re.search(pattern, message)
ip, moduleName, moduleId, modules, ignitions = matches.groups()
formatted_ip = f"{Colors.BRIGHT_GREEN}{ip}{Colors.RESET}"
formatted_dash = f"{Colors.WHITE}-"
formatted_moduleName = f"{Colors.GREEN}{moduleName}{Colors.RESET}"
formatted_moduleId = f"{Colors.CYAN}{moduleId}{Colors.RESET}"
formatted_slash = f"{Colors.WHITE}/"
formatted_modules = f"{Colors.MAGENTA}{modules}{Colors.RESET}"
formatted_ignitions = " ".join(
[
f"{Colors.DARK_BLUE}[{val}]{Colors.RESET}" if val.isdigit() else f"{Colors.RED}[{val}]{Colors.RESET}"
for val in re.findall(r"\[(.*?)\]", ignitions)
]
)
print(f"{formatted_ip} {formatted_dash} {formatted_moduleName} {formatted_moduleId}{formatted_slash}{formatted_modules} - Ignitions: {formatted_ignitions}")
Parameter(s):
Parameter | Type | Description |
---|---|---|
message | String | A string containing sequence information. |
The sequence
method, which is part of the PrintUtils
class, is designed to display the sequence information in a formatted, colorized output. The method parses the input string to extract the IP address, module name, module ID, total modules, and ignition information. Each part of the output is colored differently to enhance readability and distinguish between different parts of the output.
param_error
@staticmethod
def param_error(message):
parts = message.split('"')
formatted_parts = []
for i, part in enumerate(parts):
if i % 2 == 0:
# White color for non-quoted parts
formatted_parts.append(Colors.WHITE + part)
else:
# Green color for quoted parts
formatted_parts.append(Colors.BRIGHT_GREEN + part)
formatted_string = '"'.join(formatted_parts)
# Dark blue color for dashes and the following character
formatted_string = re.sub(r"-(\w)", Colors.DARK_BLUE + r"-\1" + Colors.WHITE, formatted_string)
# Yellow color for values inside <>
formatted_string = re.sub(r"<([^<>]+)>", Colors.BRIGHT_YELLOW + r"<\1>" + Colors.WHITE, formatted_string)
# Reset color to default
formatted_string += Colors.RESET
print(Colors.RED + "[ERROR] - " + Colors.RESET + formatted_string)
Parameter(s):
Parameter | Type | Description |
---|---|---|
message | String | A string containing parameterized error information. |
The param_error
method, which is a part of the PrintUtils
class, is designed to handle parameter-related error messages. The method takes in an error message string, and provides a formatted output with specific colors assigned to different parts of the error message, making it easier to read and understand. The method specifically highlights quoted parts, dashes and the following character, and values inside angle brackets. An [ERROR] -
prefix is added to the output message.
error
@staticmethod
def error(message):
print(Colors.RED + "[ERROR] - " + Colors.RESET + message )
Parameter(s):
Parameter | Type | Description |
---|---|---|
message | String | A string containing error information. |
The error
method, which is a part of the PrintUtils
class, is a method for handling general error messages. It accepts an error message string as an input and outputs a formatted error message, with the message text appearing in red. The method prefixes the output message with an [ERROR] -
string.
log
@staticmethod
def log(message):
print(Colors.WHITE + message + Colors.RESET)
Parameter(s):
Parameter | Type | Description |
---|---|---|
message | String | A string containing log information. |
The log
method, a part of the PrintUtils
class, is designed for handling log messages. It takes in a message string and outputs it with default (white) coloring. This method is typically used for outputting standard log information.
other
@staticmethod
def other(message):
print(Colors.BRIGHT_BLUE + message + Colors.RESET)
Parameter(s):
Parameter | Type | Description |
---|---|---|
message | String | A string containing loggable information. |
The other
method, which belongs to the PrintUtils
class, is meant for handling messages that do not fall under the other specific categories. It takes a message string and outputs it with a bright blue color, making it distinct from regular log messages and error messages. This method is useful for any kind of output that needs to be distinguishable from regular logs and errors.