openc2lib.types.base.record

 1import inspect
 2
 3from openc2lib.types.base.openc2_type import Openc2Type
 4
 5class Record(Openc2Type):
 6	""" OpenC2 Record
 7
 8		Implements OpenC2 Record: 
 9			>An ordered map from a list of keys with positions to values with 
10			positionally-defined semantics. Each key has a position and name, 
11			and is mapped to a type.
12
13		It expect keys to be public class attributes. All internal attributes 
14		must be kept private by prefixing it with an '_'.
15
16	"""
17	def todict(self, e):
18		""" Converts to dictionary 
19		
20			It is used to convert this object to an intermediary representation during 
21			serialization. It takes an `Encoder` argument that is used to recursively
22			serialize inner data and structures (the `Encoder` provides standard methods
23			for converting base types to dictionaries).. 
24
25			:param e: The `Encoder` that is being used.
26			:return: A dictionary compliants to the Language Specification's serialization
27			rules.
28		"""
29		objdic = vars(self)
30
31		dic = {}
32		for k,v in objdic.items():
33			# Fix keywords corresponding to variable names that clash with Python keywords
34			if isinstance(k, str) and k.endswith('_'):
35				k = k.rstrip('_')
36			# Remove empty and private elements; do not include non-string keys
37			if not v is None and not k.startswith('_') and isinstance(k, str):
38				dic[k] = v	
39
40		return e.todict(dic)
41
42	@classmethod
43	def fromdict(clstype, dic, e):
44		""" Builds instance from dictionary 
45
46			It is used during deserialization to create an openc2lib instance from the text message.
47			It takes an `Encoder` instance that is used to recursively build instances of the inner
48			objects (the `Encoder` provides standard methods to create instances of base objects like
49			strings, integers, boolean).
50
51			:param dic: The intermediary dictionary representation from which the object is built.
52			:param e: The `Encoder that is being used.
53			:return: An instance of this class initialized from the dictionary values.
54		"""
55		objdic = {}
56		# Retrieve class type for each field in the dictionary
57		fielddesc = None
58		for tpl in inspect.getmembers(clstype):
59			if tpl[0] == '__annotations__':
60				fielddesc = tpl[1]
61
62		for k,v in dic.items():
63			if k not in fielddesc:
64				raise Exception("Unknown field '" + k + "' from message")
65			objdic[k] = e.fromdict(fielddesc[k], v)
66
67
68		# A record should always have more than one field, so the following statement 
69		# should not raise exceptions
70		return clstype(**objdic)
class Record(openc2lib.types.base.openc2_type.Openc2Type):
 6class Record(Openc2Type):
 7	""" OpenC2 Record
 8
 9		Implements OpenC2 Record: 
10			>An ordered map from a list of keys with positions to values with 
11			positionally-defined semantics. Each key has a position and name, 
12			and is mapped to a type.
13
14		It expect keys to be public class attributes. All internal attributes 
15		must be kept private by prefixing it with an '_'.
16
17	"""
18	def todict(self, e):
19		""" Converts to dictionary 
20		
21			It is used to convert this object to an intermediary representation during 
22			serialization. It takes an `Encoder` argument that is used to recursively
23			serialize inner data and structures (the `Encoder` provides standard methods
24			for converting base types to dictionaries).. 
25
26			:param e: The `Encoder` that is being used.
27			:return: A dictionary compliants to the Language Specification's serialization
28			rules.
29		"""
30		objdic = vars(self)
31
32		dic = {}
33		for k,v in objdic.items():
34			# Fix keywords corresponding to variable names that clash with Python keywords
35			if isinstance(k, str) and k.endswith('_'):
36				k = k.rstrip('_')
37			# Remove empty and private elements; do not include non-string keys
38			if not v is None and not k.startswith('_') and isinstance(k, str):
39				dic[k] = v	
40
41		return e.todict(dic)
42
43	@classmethod
44	def fromdict(clstype, dic, e):
45		""" Builds instance from dictionary 
46
47			It is used during deserialization to create an openc2lib instance from the text message.
48			It takes an `Encoder` instance that is used to recursively build instances of the inner
49			objects (the `Encoder` provides standard methods to create instances of base objects like
50			strings, integers, boolean).
51
52			:param dic: The intermediary dictionary representation from which the object is built.
53			:param e: The `Encoder that is being used.
54			:return: An instance of this class initialized from the dictionary values.
55		"""
56		objdic = {}
57		# Retrieve class type for each field in the dictionary
58		fielddesc = None
59		for tpl in inspect.getmembers(clstype):
60			if tpl[0] == '__annotations__':
61				fielddesc = tpl[1]
62
63		for k,v in dic.items():
64			if k not in fielddesc:
65				raise Exception("Unknown field '" + k + "' from message")
66			objdic[k] = e.fromdict(fielddesc[k], v)
67
68
69		# A record should always have more than one field, so the following statement 
70		# should not raise exceptions
71		return clstype(**objdic)

OpenC2 Record

Implements OpenC2 Record:

An ordered map from a list of keys with positions to values with positionally-defined semantics. Each key has a position and name, and is mapped to a type.

It expect keys to be public class attributes. All internal attributes must be kept private by prefixing it with an '_'.

def todict(self, e):
18	def todict(self, e):
19		""" Converts to dictionary 
20		
21			It is used to convert this object to an intermediary representation during 
22			serialization. It takes an `Encoder` argument that is used to recursively
23			serialize inner data and structures (the `Encoder` provides standard methods
24			for converting base types to dictionaries).. 
25
26			:param e: The `Encoder` that is being used.
27			:return: A dictionary compliants to the Language Specification's serialization
28			rules.
29		"""
30		objdic = vars(self)
31
32		dic = {}
33		for k,v in objdic.items():
34			# Fix keywords corresponding to variable names that clash with Python keywords
35			if isinstance(k, str) and k.endswith('_'):
36				k = k.rstrip('_')
37			# Remove empty and private elements; do not include non-string keys
38			if not v is None and not k.startswith('_') and isinstance(k, str):
39				dic[k] = v	
40
41		return e.todict(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 Encoder that is being used.
Returns

A dictionary compliants to the Language Specification's serialization rules.

@classmethod
def fromdict(clstype, dic, e):
43	@classmethod
44	def fromdict(clstype, dic, e):
45		""" Builds instance from dictionary 
46
47			It is used during deserialization to create an openc2lib instance from the text message.
48			It takes an `Encoder` instance that is used to recursively build instances of the inner
49			objects (the `Encoder` provides standard methods to create instances of base objects like
50			strings, integers, boolean).
51
52			:param dic: The intermediary dictionary representation from which the object is built.
53			:param e: The `Encoder that is being used.
54			:return: An instance of this class initialized from the dictionary values.
55		"""
56		objdic = {}
57		# Retrieve class type for each field in the dictionary
58		fielddesc = None
59		for tpl in inspect.getmembers(clstype):
60			if tpl[0] == '__annotations__':
61				fielddesc = tpl[1]
62
63		for k,v in dic.items():
64			if k not in fielddesc:
65				raise Exception("Unknown field '" + k + "' from message")
66			objdic[k] = e.fromdict(fielddesc[k], v)
67
68
69		# A record should always have more than one field, so the following statement 
70		# should not raise exceptions
71		return clstype(**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.