[docs]classTuoniResult:""" A class that provides data for a command result. Attributes: status (str): The status of the result. error_message (str): The error message, if any, for the result. received (str): The time, in string format, indicating when the result was received. parts (list[TuoniResultPart]): A list of parts that make up the result. """def__init__(self,conf,c2):""" Constructor for the result class. Args: conf (dict): Data from the server. c2 (TuoniC2): The related server object that manages communication. """self.c2=c2self._load_conf(conf)def_load_conf(self,conf):self.status=conf["status"]self.error_message=conf["errorMessage"]self.received=conf["received"]self.parts=[]forpart_confinconf["childResults"]:part_obj=TuoniResultPart(part_conf,self.c2)self.parts.append(part_obj)
[docs]defis_text(self):""" Check if the result contains typical text output (result part where name is "stdout"). Returns: bool: True if the result is a text output, False otherwise. """forpartinself.parts:ifpart.type=="text"andpart.name.lower()=="stdout":returnTruereturnFalse
[docs]defis_json(self):""" Check if the result contains JSON output (result part where name "json"). Returns: bool: True if the result is a JSON output, False otherwise. """forpartinself.parts:ifpart.type=="text"andpart.name.lower()=="json":returnTruereturnFalse
[docs]defis_file(self):""" Check if the result contains file output (result part where type is "file"). Returns: bool: True if the result is a file output, False otherwise. """forpartinself.parts:ifpart.type=="file":returnTruereturnFalse
[docs]defget_text(self):""" Retrieve the text output from the result parts. Returns: str: The text output if available, otherwise None. """forpartinself.parts:ifpart.type=="text"andpart.name.lower()=="stdout":returnpart.get_as_text()returnNone
[docs]defget_json(self):""" Retrieve the JSON output from the result parts. Returns: dict: The JSON output if available, otherwise None. """forpartinself.parts:ifpart.type=="text"andpart.name.lower()=="json":returnpart.get_as_json()returnNone
[docs]defget_files(self,download=False):""" Retrieve the file outputs from the result parts. Returns: dict: A dictionary where keys are filenames and values are the file contents if download is True, otherwise a ResultPart object. """files={}forpartinself.parts:ifpart.type=="file":ifdownload:files[part.filename]=part.get_as_bytes()else:files[part.filename]=partreturnfiles