"""Provides classes for representing the results of executing a plugin."""fromenumimportEnum,auto,uniquefromattrimportattrib,attrs,validators,FactoryfromosimportlinesepfromtypingimportOptionalfromaac.in_out.files.aac_fileimportAaCFilefromaac.context.source_locationimportSourceLocation
[docs]@uniqueclassExecutionStatus(Enum):"""An enumeration that represents status codes for AaC commands to return."""SUCCESS=0CONSTRAINT_FAILURE=auto()CONSTRAINT_WARNING=auto()PARSER_FAILURE=auto()PLUGIN_FAILURE=auto()OPERATION_CANCELLED=auto()GENERAL_FAILURE=auto()
[docs]classMessageLevel(Enum):"""An enumeration that represents the level of a message."""DEBUG=auto()INFO=auto()WARNING=auto()ERROR=auto()
[docs]@attrs(slots=True,auto_attribs=True)classExecutionMessage:"""Provides a message for the user. Attributes: message (str): The textual content of the message. level (MessageLevel): value of MessageLevel.DEBUG, .INFO, .WARNING, or .ERROR source: (AaCFile): The file from whence the message came. location: (SourceLocation): The col, row info within the source file. """message:str=attrib(validator=validators.instance_of(str))level:MessageLevel=attrib(validator=validators.instance_of(MessageLevel))source:Optional[AaCFile]=attrib(validator=validators.optional(validators.instance_of(AaCFile)))location:Optional[SourceLocation]=attrib(validator=validators.optional(validators.instance_of(SourceLocation)))
[docs]@attrs(slots=True,auto_attribs=True)classExecutionResult:"""Provides information regarding the results of the execution of a plugin. Attributes: plugin_name (str): The name of the plugin whose results are included. plugin_command_name (str): The name of the command which contributed to these results. status_code (ExecutionStatus): A status code for the plugin execution. messages (list[str]): A list of messages for the user. """plugin_name:str=attrib(validator=validators.instance_of(str))plugin_command_name:str=attrib(validator=validators.instance_of(str))status_code:ExecutionStatus=attrib(validator=validators.instance_of(ExecutionStatus))messages:list[ExecutionMessage]=attrib(default=Factory(list),validator=validators.instance_of(list))
[docs]defadd_message(self,message:ExecutionMessage)->None:"""Add a message to the list of messages."""iftype(message)isnotExecutionMessage:raiseTypeError("Message should be of class ExecutionMessage")self.messages.append(message)
[docs]defadd_messages(self,messages:list[ExecutionMessage])->None:"""Add messages to the list of messages."""formessageinmessages:iftype(message)isnotExecutionMessage:raiseTypeError("Message should be of class ExecutionMessage")self.messages.extend(messages)
[docs]defis_success(self)->bool:"""Return True if the command succeeded; False, otherwise."""returnself.status_code==ExecutionStatus.SUCCESS
[docs]defget_messages_as_string(self)->str:"""Return the output messages as a combined string."""result=""formessageinself.messages:result+=message.message+linesepifmessage.sourceisnotNone:result+=f" Source: {message.source.uri}"ifmessage.locationisnotNone:result+=f" (Ln {message.location.line}: Col {message.location.column}: Pos {message.location.position}: Spn {message.location.span})"result+=linesepreturnresult
[docs]@attrs(slots=True)classExecutionError(Exception):"""A base class representing a plugin error condition. Attributes: message (str): a textual description of the reason for the exception. """message:str=attrib(validator=validators.instance_of(str))
[docs]@attrs(slots=True)classOperationCancelled(Exception):"""A base class representing an cancelled plugin operation condition. Attributes: message (str): a textual description of the reason for cancellation. """message:str=attrib(validator=validators.instance_of(str))