openc2lib.types.base.choice
1from openc2lib.types.base.openc2_type import Openc2Type 2 3class Choice(Openc2Type): 4 """ OpenC2 Choice 5 Implements the OpenC2 Choice: 6 >One field selected from a set of named fields. The API value has a name and a type. 7 8 It expect all allowed values to be provided in a `Register` class, which must be defined 9 as class attribute `register` in all derived classes (see `Target` and `Actuator` as examples). 10 """ 11 register = None 12 """ List of registered name/class options available """ 13 14 def __init__(self, obj): 15 """ Initialize the `Choice` object 16 17 Objects used as `Choice` must be registered in advance in the `register` dictionary. 18 19 :arg obj: An object among those defined in the `register`. 20 """ 21 self.choice: str = self.register.getName(obj.__class__) 22 """ Selected name for the `Choice` """ 23 self.obj = obj 24 """ Class corresponding to the `choice` """ 25 26 def getObj(self): 27 """ Returns the objet instance embedded in the `register`.""" 28 return self.obj 29 30 def getName(self): 31 """Returns the name of the choice 32 33 Returns the name of object, which is the selector carried by the `Choice` element. 34 This does not include the object itself. 35 """ 36 return self.choice 37 38 @classmethod 39 def getClass(cls, choice): 40 """ Get the class corresponding to the current `choice` 41 42 It may be implemented by any derived class, if a different logic than the `Register` class 43 is followed to store the name/class bindings. 44 :param choice: The name of the alternative that is being looked for. 45 :return: The class corresponding to the provided `choice`. 46 """ 47 return cls.register.get(choice) 48 49 def __str__(self): 50 return self.choice 51 52 def __repr__(self): 53 return str(self.obj) 54 55 def todict(self, e): 56 """ Converts to dictionary 57 58 It is used to convert this object to an intermediary representation during 59 serialization. It takes an `Encoder` argument that is used to recursively 60 serialize inner data and structures (the `Encoder` provides standard methods 61 for converting base types to dictionaries).. 62 63 :param e: The `Encoder` that is being used. 64 :return: A dictionary compliants to the Language Specification's serialization 65 rules. 66 """ 67 # In case of Choice, the specific choice may be the implementation of an additional type, 68 # which affects its representation. So, first of all, get the representation of the inner 69 # data type 70 dic = {} 71 dic[self.choice] = e.todict(self.obj) 72 return dic 73 74 @classmethod 75 def fromdict(cls, dic, e): 76 """ Builds instance from dictionary 77 78 It is used during deserialization to create an openc2lib instance from the text message. 79 It takes an `Encoder` instance that is used to recursively build instances of the inner 80 objects (the `Encoder` provides standard methods to create instances of base objects like 81 strings, integers, boolean). 82 83 :param dic: The intermediary dictionary representation from which the object is built. 84 :param e: The `Encoder that is being used. 85 :return: An instance of this class initialized from the dictionary values. 86 """ 87 if not len(dic) == 1: 88 raise ValueError("Unexpected dict: ", dic) 89 90 for k, v in dic.items(): 91 # Expected to run one time only! 92 objtype = cls.getClass(k) 93 return cls(e.fromdict(objtype, v))
4class Choice(Openc2Type): 5 """ OpenC2 Choice 6 Implements the OpenC2 Choice: 7 >One field selected from a set of named fields. The API value has a name and a type. 8 9 It expect all allowed values to be provided in a `Register` class, which must be defined 10 as class attribute `register` in all derived classes (see `Target` and `Actuator` as examples). 11 """ 12 register = None 13 """ List of registered name/class options available """ 14 15 def __init__(self, obj): 16 """ Initialize the `Choice` object 17 18 Objects used as `Choice` must be registered in advance in the `register` dictionary. 19 20 :arg obj: An object among those defined in the `register`. 21 """ 22 self.choice: str = self.register.getName(obj.__class__) 23 """ Selected name for the `Choice` """ 24 self.obj = obj 25 """ Class corresponding to the `choice` """ 26 27 def getObj(self): 28 """ Returns the objet instance embedded in the `register`.""" 29 return self.obj 30 31 def getName(self): 32 """Returns the name of the choice 33 34 Returns the name of object, which is the selector carried by the `Choice` element. 35 This does not include the object itself. 36 """ 37 return self.choice 38 39 @classmethod 40 def getClass(cls, choice): 41 """ Get the class corresponding to the current `choice` 42 43 It may be implemented by any derived class, if a different logic than the `Register` class 44 is followed to store the name/class bindings. 45 :param choice: The name of the alternative that is being looked for. 46 :return: The class corresponding to the provided `choice`. 47 """ 48 return cls.register.get(choice) 49 50 def __str__(self): 51 return self.choice 52 53 def __repr__(self): 54 return str(self.obj) 55 56 def todict(self, e): 57 """ Converts to dictionary 58 59 It is used to convert this object to an intermediary representation during 60 serialization. It takes an `Encoder` argument that is used to recursively 61 serialize inner data and structures (the `Encoder` provides standard methods 62 for converting base types to dictionaries).. 63 64 :param e: The `Encoder` that is being used. 65 :return: A dictionary compliants to the Language Specification's serialization 66 rules. 67 """ 68 # In case of Choice, the specific choice may be the implementation of an additional type, 69 # which affects its representation. So, first of all, get the representation of the inner 70 # data type 71 dic = {} 72 dic[self.choice] = e.todict(self.obj) 73 return dic 74 75 @classmethod 76 def fromdict(cls, dic, e): 77 """ Builds instance from dictionary 78 79 It is used during deserialization to create an openc2lib instance from the text message. 80 It takes an `Encoder` instance that is used to recursively build instances of the inner 81 objects (the `Encoder` provides standard methods to create instances of base objects like 82 strings, integers, boolean). 83 84 :param dic: The intermediary dictionary representation from which the object is built. 85 :param e: The `Encoder that is being used. 86 :return: An instance of this class initialized from the dictionary values. 87 """ 88 if not len(dic) == 1: 89 raise ValueError("Unexpected dict: ", dic) 90 91 for k, v in dic.items(): 92 # Expected to run one time only! 93 objtype = cls.getClass(k) 94 return cls(e.fromdict(objtype, v))
OpenC2 Choice Implements the OpenC2 Choice:
One field selected from a set of named fields. The API value has a name and a type.
It expect all allowed values to be provided in a Register class, which must be defined
as class attribute register in all derived classes (see Target and Actuator as examples).
15 def __init__(self, obj): 16 """ Initialize the `Choice` object 17 18 Objects used as `Choice` must be registered in advance in the `register` dictionary. 19 20 :arg obj: An object among those defined in the `register`. 21 """ 22 self.choice: str = self.register.getName(obj.__class__) 23 """ Selected name for the `Choice` """ 24 self.obj = obj 25 """ Class corresponding to the `choice` """
27 def getObj(self): 28 """ Returns the objet instance embedded in the `register`.""" 29 return self.obj
Returns the objet instance embedded in the register.
31 def getName(self): 32 """Returns the name of the choice 33 34 Returns the name of object, which is the selector carried by the `Choice` element. 35 This does not include the object itself. 36 """ 37 return self.choice
Returns the name of the choice
Returns the name of object, which is the selector carried by the Choice element.
This does not include the object itself.
39 @classmethod 40 def getClass(cls, choice): 41 """ Get the class corresponding to the current `choice` 42 43 It may be implemented by any derived class, if a different logic than the `Register` class 44 is followed to store the name/class bindings. 45 :param choice: The name of the alternative that is being looked for. 46 :return: The class corresponding to the provided `choice`. 47 """ 48 return cls.register.get(choice)
Get the class corresponding to the current choice
It may be implemented by any derived class, if a different logic than the Register class
is followed to store the name/class bindings.
Parameters
- choice: The name of the alternative that is being looked for.
Returns
The class corresponding to the provided
choice.
56 def todict(self, e): 57 """ Converts to dictionary 58 59 It is used to convert this object to an intermediary representation during 60 serialization. It takes an `Encoder` argument that is used to recursively 61 serialize inner data and structures (the `Encoder` provides standard methods 62 for converting base types to dictionaries).. 63 64 :param e: The `Encoder` that is being used. 65 :return: A dictionary compliants to the Language Specification's serialization 66 rules. 67 """ 68 # In case of Choice, the specific choice may be the implementation of an additional type, 69 # which affects its representation. So, first of all, get the representation of the inner 70 # data type 71 dic = {} 72 dic[self.choice] = e.todict(self.obj) 73 return dic
Converts to dictionary
It is used to convert this object to an intermediary representation during
serialization. It takes an Encoder argument that is used to recursively
serialize inner data and structures (the Encoder provides standard methods
for converting base types to dictionaries)..
Parameters
- e: The
Encoderthat is being used.
Returns
A dictionary compliants to the Language Specification's serialization rules.
75 @classmethod 76 def fromdict(cls, dic, e): 77 """ Builds instance from dictionary 78 79 It is used during deserialization to create an openc2lib instance from the text message. 80 It takes an `Encoder` instance that is used to recursively build instances of the inner 81 objects (the `Encoder` provides standard methods to create instances of base objects like 82 strings, integers, boolean). 83 84 :param dic: The intermediary dictionary representation from which the object is built. 85 :param e: The `Encoder that is being used. 86 :return: An instance of this class initialized from the dictionary values. 87 """ 88 if not len(dic) == 1: 89 raise ValueError("Unexpected dict: ", dic) 90 91 for k, v in dic.items(): 92 # Expected to run one time only! 93 objtype = cls.getClass(k) 94 return cls(e.fromdict(objtype, v))
Builds instance from dictionary
It is used during deserialization to create an openc2lib instance from the text message.
It takes an Encoder instance that is used to recursively build instances of the inner
objects (the Encoder provides standard methods to create instances of base objects like
strings, integers, boolean).
Parameters
- dic: The intermediary dictionary representation from which the object is built.
- e: The `Encoder that is being used.
Returns
An instance of this class initialized from the dictionary values.