openc2lib.types.base.map
1import logging 2 3from openc2lib.types.base.openc2_type import Openc2Type 4 5logger = logging.getLogger(__name__) 6 7class Map(Openc2Type, dict): 8 """ OpenC2 Map 9 10 Implements OpenC2 Map: 11 >An unordered map from a set of specified keys to values with semantics 12 bound to each key. Each field has an id, name and type. 13 14 However, the id is not considered in this implementation. 15 16 The implementation follows a similar logic than `Array`. Each derived class 17 is expected to provide a `fieldtypes` class attribute that associate field names 18 with their class definition. 19 20 Additionally, according to the Language Specification, `Map`s may be extended by 21 Profiles. Such extensions must use the `base` and `register` class attributes to 22 bind to the base element they extend and the `Profile` in which they are defined. 23 """ 24 fieldtypes: dict = None 25 """ Field types 26 27 A `dictionary` which keys are field names and which values are the corresponding classes. 28 Must be provided by any derived class. 29 """ 30 base = None 31 """ Base class 32 33 Data types defined in the Language Specification shall not set this field. Data types defined in 34 Profiles that extends a Data Type defined in the Language Specification, must set this field to 35 the corresponding class of the base Data Type. 36 37 Note: Extensions defined in the openc2lib context are recommended to use the same name of the base 38 Data Type, and to distinguish them through appropriate usage of the namespacing mechanism. 39 """ 40 register = None 41 """ Registered extensions 42 43 Classes that implement a Data Type defined in the Language Specification will use this field to 44 register extensions defined by external Profiles. Classes that define extensions within Profiles 45 shall register themselves according to the specific documentation of the base type class, but 46 shall not modify this field. 47 """ 48 49 def todict(self, e): 50 """ Converts to dictionary 51 52 It is used to convert this object to an intermediary representation during 53 serialization. It takes an `Encoder` argument that is used to recursively 54 serialize inner data and structures (the `Encoder` provides standard methods 55 for converting base types to dictionaries).. 56 57 :param e: The `Encoder` that is being used. 58 :return: A dictionary compliants to the Language Specification's serialization 59 rules. 60 """ 61 newdic=dict() 62 63 # This is necessary because self.base.fieldtypes does 64 # not exist for non-extended classes 65 if self.base is None: 66 return e.todict(dict(self)) 67 68 for k,v in self.items(): 69 if k not in self.fieldtypes: 70 raise ValueError('Unknown field: ', k) 71 if k in self.base.fieldtypes: 72 newdic[k] = v 73 else: 74 if self.nsid not in newdic: 75 newdic[self.nsid]={} 76 newdic[self.nsid][k]=v 77 78 return e.todict(newdic) 79 80 @classmethod 81 def fromdict(cls, dic, e): 82 """ Builds instance from dictionary 83 84 It is used during deserialization to create an openc2lib instance from the text message. 85 It takes an `Encoder` instance that is used to recursively build instances of the inner 86 objects (the `Encoder` provides standard methods to create instances of base objects like 87 strings, integers, boolean). 88 89 :param dic: The intermediary dictionary representation from which the object is built. 90 :param e: The `Encoder that is being used. 91 :return: An instance of this class initialized from the dictionary values. 92 """ 93 objdic = {} 94 extension = None 95 logger.debug('Building %s from %s in Map', cls, dic) 96 for k,v in dic.items(): 97 if k in cls.fieldtypes: 98 objdic[k] = e.fromdict(cls.fieldtypes[k], v) 99 elif k in cls.register: 100 logger.debug(' Using profile %s to decode: %s', k, v) 101 extension = cls.register[k] 102 for l,w in v.items(): 103 objdic[l] = e.fromdict(extension.fieldtypes[l], w) 104 else: 105 raise TypeError("Unexpected field: ", k) 106 107 if extension is not None: 108 cls = extension 109 110 return cls(objdic)
8class Map(Openc2Type, dict): 9 """ OpenC2 Map 10 11 Implements OpenC2 Map: 12 >An unordered map from a set of specified keys to values with semantics 13 bound to each key. Each field has an id, name and type. 14 15 However, the id is not considered in this implementation. 16 17 The implementation follows a similar logic than `Array`. Each derived class 18 is expected to provide a `fieldtypes` class attribute that associate field names 19 with their class definition. 20 21 Additionally, according to the Language Specification, `Map`s may be extended by 22 Profiles. Such extensions must use the `base` and `register` class attributes to 23 bind to the base element they extend and the `Profile` in which they are defined. 24 """ 25 fieldtypes: dict = None 26 """ Field types 27 28 A `dictionary` which keys are field names and which values are the corresponding classes. 29 Must be provided by any derived class. 30 """ 31 base = None 32 """ Base class 33 34 Data types defined in the Language Specification shall not set this field. Data types defined in 35 Profiles that extends a Data Type defined in the Language Specification, must set this field to 36 the corresponding class of the base Data Type. 37 38 Note: Extensions defined in the openc2lib context are recommended to use the same name of the base 39 Data Type, and to distinguish them through appropriate usage of the namespacing mechanism. 40 """ 41 register = None 42 """ Registered extensions 43 44 Classes that implement a Data Type defined in the Language Specification will use this field to 45 register extensions defined by external Profiles. Classes that define extensions within Profiles 46 shall register themselves according to the specific documentation of the base type class, but 47 shall not modify this field. 48 """ 49 50 def todict(self, e): 51 """ Converts to dictionary 52 53 It is used to convert this object to an intermediary representation during 54 serialization. It takes an `Encoder` argument that is used to recursively 55 serialize inner data and structures (the `Encoder` provides standard methods 56 for converting base types to dictionaries).. 57 58 :param e: The `Encoder` that is being used. 59 :return: A dictionary compliants to the Language Specification's serialization 60 rules. 61 """ 62 newdic=dict() 63 64 # This is necessary because self.base.fieldtypes does 65 # not exist for non-extended classes 66 if self.base is None: 67 return e.todict(dict(self)) 68 69 for k,v in self.items(): 70 if k not in self.fieldtypes: 71 raise ValueError('Unknown field: ', k) 72 if k in self.base.fieldtypes: 73 newdic[k] = v 74 else: 75 if self.nsid not in newdic: 76 newdic[self.nsid]={} 77 newdic[self.nsid][k]=v 78 79 return e.todict(newdic) 80 81 @classmethod 82 def fromdict(cls, dic, e): 83 """ Builds instance from dictionary 84 85 It is used during deserialization to create an openc2lib instance from the text message. 86 It takes an `Encoder` instance that is used to recursively build instances of the inner 87 objects (the `Encoder` provides standard methods to create instances of base objects like 88 strings, integers, boolean). 89 90 :param dic: The intermediary dictionary representation from which the object is built. 91 :param e: The `Encoder that is being used. 92 :return: An instance of this class initialized from the dictionary values. 93 """ 94 objdic = {} 95 extension = None 96 logger.debug('Building %s from %s in Map', cls, dic) 97 for k,v in dic.items(): 98 if k in cls.fieldtypes: 99 objdic[k] = e.fromdict(cls.fieldtypes[k], v) 100 elif k in cls.register: 101 logger.debug(' Using profile %s to decode: %s', k, v) 102 extension = cls.register[k] 103 for l,w in v.items(): 104 objdic[l] = e.fromdict(extension.fieldtypes[l], w) 105 else: 106 raise TypeError("Unexpected field: ", k) 107 108 if extension is not None: 109 cls = extension 110 111 return cls(objdic)
OpenC2 Map
Implements OpenC2 Map:
An unordered map from a set of specified keys to values with semantics bound to each key. Each field has an id, name and type.
However, the id is not considered in this implementation.
The implementation follows a similar logic than Array. Each derived class
is expected to provide a fieldtypes class attribute that associate field names
with their class definition.
Additionally, according to the Language Specification, Maps may be extended by
Profiles. Such extensions must use the base and register class attributes to
bind to the base element they extend and the Profile in which they are defined.
Field types
A dictionary which keys are field names and which values are the corresponding classes.
Must be provided by any derived class.
Base class
Data types defined in the Language Specification shall not set this field. Data types defined in Profiles that extends a Data Type defined in the Language Specification, must set this field to the corresponding class of the base Data Type.
Note: Extensions defined in the openc2lib context are recommended to use the same name of the base Data Type, and to distinguish them through appropriate usage of the namespacing mechanism.
Registered extensions
Classes that implement a Data Type defined in the Language Specification will use this field to register extensions defined by external Profiles. Classes that define extensions within Profiles shall register themselves according to the specific documentation of the base type class, but shall not modify this field.
50 def todict(self, e): 51 """ Converts to dictionary 52 53 It is used to convert this object to an intermediary representation during 54 serialization. It takes an `Encoder` argument that is used to recursively 55 serialize inner data and structures (the `Encoder` provides standard methods 56 for converting base types to dictionaries).. 57 58 :param e: The `Encoder` that is being used. 59 :return: A dictionary compliants to the Language Specification's serialization 60 rules. 61 """ 62 newdic=dict() 63 64 # This is necessary because self.base.fieldtypes does 65 # not exist for non-extended classes 66 if self.base is None: 67 return e.todict(dict(self)) 68 69 for k,v in self.items(): 70 if k not in self.fieldtypes: 71 raise ValueError('Unknown field: ', k) 72 if k in self.base.fieldtypes: 73 newdic[k] = v 74 else: 75 if self.nsid not in newdic: 76 newdic[self.nsid]={} 77 newdic[self.nsid][k]=v 78 79 return e.todict(newdic)
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.
81 @classmethod 82 def fromdict(cls, dic, e): 83 """ Builds instance from dictionary 84 85 It is used during deserialization to create an openc2lib instance from the text message. 86 It takes an `Encoder` instance that is used to recursively build instances of the inner 87 objects (the `Encoder` provides standard methods to create instances of base objects like 88 strings, integers, boolean). 89 90 :param dic: The intermediary dictionary representation from which the object is built. 91 :param e: The `Encoder that is being used. 92 :return: An instance of this class initialized from the dictionary values. 93 """ 94 objdic = {} 95 extension = None 96 logger.debug('Building %s from %s in Map', cls, dic) 97 for k,v in dic.items(): 98 if k in cls.fieldtypes: 99 objdic[k] = e.fromdict(cls.fieldtypes[k], v) 100 elif k in cls.register: 101 logger.debug(' Using profile %s to decode: %s', k, v) 102 extension = cls.register[k] 103 for l,w in v.items(): 104 objdic[l] = e.fromdict(extension.fieldtypes[l], w) 105 else: 106 raise TypeError("Unexpected field: ", k) 107 108 if extension is not None: 109 cls = extension 110 111 return cls(objdic)
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.
Inherited Members
- builtins.dict
- get
- setdefault
- pop
- popitem
- keys
- items
- values
- update
- fromkeys
- clear
- copy