otupy.core.encoder.Encoder
- class Encoder
Bases:
objectBase Encoder
The Base Encoder provides a common interface that must be implemented by all implementations of Encoding mechanisms. Each new Encoder should be derived from this class.
The Base Encoder translates otupy data types and structures into dictionaries. This function can be used by derived class to have an intermediary representation which strictly follows the OpenC2 formatting rules. The intermediary representation must then be translated into the specific syntax used by the derived Encoder (e.g., json, xml, yaml).
This class is designed to encode into both text and binary formats. For encoders that produce a bytestream, the member is_binary must be set to True. Also verify that the Transfer Protocol supports both formats.
Methods
Decode into OpenC2 object
Encode an OpenC2 object
Create an object from dictionary
Encoder name
Convert object to dictionary
Attributes
encoder_typeis_binary- static decode(msgtype, msg)
Decode into OpenC2 object
This method decodes a text representation into an otupy object. The method throws an Exception in case of unknown elements.
This method requires to specify the otupy class that implements the OpenC2 object described by the text. This will commonly be either Message, Command, or Response.
- Parameters:
msgtype – The class of an otupy object.
msg – Text-based representation of the OpenC2 object.
- Returns:
An instance of an otupy object.
- static encode(obj)
Encode an OpenC2 object
This method encodes an otupy object (namely, a data structure derived from BaseType). It MUST be implemented by each derived class. It could be applied to any otupy object, but the most common use is for Message, Command, or Response.
- Parameters:
obj – An OpenC2 object derived from a BaseType.
- Returns:
A string that contains the encoded object.
- static fromdict(clstype, dic)
Create an object from dictionary
This is an internal method to create an otupy object from a dictionary. The dictionary must be compliant with the OpenC2 syntax rules. Derived classes are expected to create this intermediate representation and use this method in the encode method. It is necessary to provide the class definition of the otupy object to be instantiated.
- Parameters:
clstype – The class definition that must be used to instantiate the object.
dic – The dictionary with the OpenC2 description.
- Returns:
An instance of clstype initialized with the data in the dic.
- classmethod getName()
Encoder name
This method MUST be implemented to return the name of the encoding format. The name should be highly representative, possible using official terminology (e.g.: json, xml).
- Returns:
The class name.
- static todict(obj)
Convert object to dictionary
This is an internal method to convert an otupy object into a dictionary. The dictionary is structured according to OpenC2 syntax.
This method should only be invoked by derived classes to get the intermediary representation of otupy objects. It will likely be used in the implementation of the decode method. :param obj: The otupy object to convert into a dictionary. :return: A dictionary compliant with the OpenC2 syntax rules.