openc2lib.core.register

Object registration

This module provides a registration mechanism to extend the elements provided by the Language Specification with additional definitions from the Profiles.

 1""" Object registration
 2
 3	This module provides a registration mechanism to extend the elements provided by the 
 4	Language Specification with additional definitions from the Profiles.
 5"""
 6
 7class Register(dict):
 8	""" List of registered elements
 9	
10		This class registers all available elements, both provided by the openc2lib and by Profiles.
11		The class is meant to be instantiated internally and filled in with the elements provided by 
12		the Language Specification. Profiles may fill in with additional definitions, to make their 
13		classes and names available to the core system for encoding/deconding purposes.
14	"""
15	
16	def add(self, name: str, register, identifier=None):
17		""" Add a new element
18	
19			Register a new element and make it available within the system. 
20			
21			This method throw an Exception if the element is already registered.
22
23			:param name: The name used for the element.
24			:param register: The class that defines the element.
25			:param identifier: A numeric value associated to the standard by the Specification (unused).
26			:return: None
27		"""
28		try:
29			list(self.keys())[list(self.values()).index(register)]
30		except ValueError:
31			# The item is not in the list
32			self[name] = register
33			return
34		raise ValueError("Element already registered")
35
36	def get(self, name: str):
37		""" Get element by name
38
39			Throws an exception if the given name does not correspond to any registered element.
40
41			:param name: The name of the element to return.
42			:return: The class  corresponding to the given name.
43		"""
44		return self[name]
45
46	def getName(self, register):
47		""" Get the name of a element
48
49			Given a class element, this method returns its name (the name it was registered with. 
50			Note that the returned name include the namespace prefix.
51
52			Throws an exception if the given element is not registered.
53
54			:param register: The class element to look for.
55			:return: A string with the name of the element.
56		"""
57		return list(self.keys())[list(self.values()).index(register)]
class Register(builtins.dict):
 8class Register(dict):
 9	""" List of registered elements
10	
11		This class registers all available elements, both provided by the openc2lib and by Profiles.
12		The class is meant to be instantiated internally and filled in with the elements provided by 
13		the Language Specification. Profiles may fill in with additional definitions, to make their 
14		classes and names available to the core system for encoding/deconding purposes.
15	"""
16	
17	def add(self, name: str, register, identifier=None):
18		""" Add a new element
19	
20			Register a new element and make it available within the system. 
21			
22			This method throw an Exception if the element is already registered.
23
24			:param name: The name used for the element.
25			:param register: The class that defines the element.
26			:param identifier: A numeric value associated to the standard by the Specification (unused).
27			:return: None
28		"""
29		try:
30			list(self.keys())[list(self.values()).index(register)]
31		except ValueError:
32			# The item is not in the list
33			self[name] = register
34			return
35		raise ValueError("Element already registered")
36
37	def get(self, name: str):
38		""" Get element by name
39
40			Throws an exception if the given name does not correspond to any registered element.
41
42			:param name: The name of the element to return.
43			:return: The class  corresponding to the given name.
44		"""
45		return self[name]
46
47	def getName(self, register):
48		""" Get the name of a element
49
50			Given a class element, this method returns its name (the name it was registered with. 
51			Note that the returned name include the namespace prefix.
52
53			Throws an exception if the given element is not registered.
54
55			:param register: The class element to look for.
56			:return: A string with the name of the element.
57		"""
58		return list(self.keys())[list(self.values()).index(register)]

List of registered elements

This class registers all available elements, both provided by the openc2lib and by Profiles. The class is meant to be instantiated internally and filled in with the elements provided by the Language Specification. Profiles may fill in with additional definitions, to make their classes and names available to the core system for encoding/deconding purposes.

def add(self, name: str, register, identifier=None):
17	def add(self, name: str, register, identifier=None):
18		""" Add a new element
19	
20			Register a new element and make it available within the system. 
21			
22			This method throw an Exception if the element is already registered.
23
24			:param name: The name used for the element.
25			:param register: The class that defines the element.
26			:param identifier: A numeric value associated to the standard by the Specification (unused).
27			:return: None
28		"""
29		try:
30			list(self.keys())[list(self.values()).index(register)]
31		except ValueError:
32			# The item is not in the list
33			self[name] = register
34			return
35		raise ValueError("Element already registered")

Add a new element

Register a new element and make it available within the system.

This method throw an Exception if the element is already registered.

Parameters
  • name: The name used for the element.
  • register: The class that defines the element.
  • identifier: A numeric value associated to the standard by the Specification (unused).
Returns

None

def get(self, name: str):
37	def get(self, name: str):
38		""" Get element by name
39
40			Throws an exception if the given name does not correspond to any registered element.
41
42			:param name: The name of the element to return.
43			:return: The class  corresponding to the given name.
44		"""
45		return self[name]

Get element by name

Throws an exception if the given name does not correspond to any registered element.

Parameters
  • name: The name of the element to return.
Returns

The class corresponding to the given name.

def getName(self, register):
47	def getName(self, register):
48		""" Get the name of a element
49
50			Given a class element, this method returns its name (the name it was registered with. 
51			Note that the returned name include the namespace prefix.
52
53			Throws an exception if the given element is not registered.
54
55			:param register: The class element to look for.
56			:return: A string with the name of the element.
57		"""
58		return list(self.keys())[list(self.values()).index(register)]

Get the name of a element

Given a class element, this method returns its name (the name it was registered with. Note that the returned name include the namespace prefix.

Throws an exception if the given element is not registered.

Parameters
  • register: The class element to look for.
Returns

A string with the name of the element.

Inherited Members
builtins.dict
setdefault
pop
popitem
keys
items
values
update
fromkeys
clear
copy