openc2lib.core.extensions
OpenC2 Extensions
This module defines internal structures to register extensions defined by additional profiles. It also includes helper functions to be used as decorators to manage the registration of the extensions.
Classes that can be extended must allocate an item to keep track of all extensions registered in Profiles. This class must not be instantiated. Just use its class methods as decorators for both base and extended classes.
Each item in Extensions includes:
- the name of the class to be extended as the key;
- a
Registerobject as value, which will contain all the extensions.
Usage:
- Define a class as extensible by using the
@extensibledecorator; - Register extensions by using the
@extensiondecorator for classes that define an extension.
1""" OpenC2 Extensions 2 3 This module defines internal structures to register extensions defined by additional profiles. 4 It also includes helper functions to be used as decorators to manage the registration of the 5 extensions. 6 7 Classes that can be extended must allocate an item to keep track of all 8 extensions registered in Profiles. 9 This class must not be instantiated. Just use its class methods as decorators for both 10 base and extended classes. 11 12 Each item in `Extensions` includes: 13 - the name of the class to be extended as the key; 14 - a `Register` object as value, which will contain all the extensions. 15 16 Usage: 17 - Define a class as extensible by using the `@extensible` decorator; 18 - Register extensions by using the `@extension` decorator for classes that define an extension. 19""" 20import copy 21 22from openc2lib.core.register import Register 23 24Extensions = dict() 25""" Extensions 26 27 This is the registry that keeps track of all extensions to the core openc2lib data and 28 structures. 29""" 30 31def extensible(cls): 32 """ The `@extensible` decorator 33 34 The `@extensible` decorator makes Map-based class extensible. It adds internal class methods 35 that are used in the encoding/decondig processes to manage extensions. 36 This decorator must be used in front of all classes that can be extended in Profiles. 37 """ 38 if cls.__name__ not in Extensions: 39 Extensions[cls.__name__]=Register() 40 setattr(cls, 'register' , Extensions[cls.__name__]) 41 setattr(cls, 'base', None) 42 return cls 43 44def extension(nsid): 45 """ @extends decorator 46 47 This decorator must be used in front of all extensions to Map-based classes. 48 49 @cls: The class which is being extended (no need to specify) 50 @base: The base class to extend 51 @nsid: The profile name 52 @return: The class definition that is now registered and usable in openc2lib. 53 """ 54 def extend_wrapper(cls): 55 setattr(cls, 'base', cls.__base__) 56 setattr(cls, 'nsid', nsid) 57 Extensions[cls.__base__.__name__].add(nsid, cls) 58 cls.fieldtypes.update(copy.deepcopy(cls.__base__.fieldtypes)) 59 return cls 60 return extend_wrapper 61 62 63 64 65# The @property decorator is used to customize getters and setters for class attributes.
Extensions
This is the registry that keeps track of all extensions to the core openc2lib data and structures.
32def extensible(cls): 33 """ The `@extensible` decorator 34 35 The `@extensible` decorator makes Map-based class extensible. It adds internal class methods 36 that are used in the encoding/decondig processes to manage extensions. 37 This decorator must be used in front of all classes that can be extended in Profiles. 38 """ 39 if cls.__name__ not in Extensions: 40 Extensions[cls.__name__]=Register() 41 setattr(cls, 'register' , Extensions[cls.__name__]) 42 setattr(cls, 'base', None) 43 return cls
The @extensible decorator
The @extensible decorator makes Map-based class extensible. It adds internal class methods
that are used in the encoding/decondig processes to manage extensions.
This decorator must be used in front of all classes that can be extended in Profiles.
45def extension(nsid): 46 """ @extends decorator 47 48 This decorator must be used in front of all extensions to Map-based classes. 49 50 @cls: The class which is being extended (no need to specify) 51 @base: The base class to extend 52 @nsid: The profile name 53 @return: The class definition that is now registered and usable in openc2lib. 54 """ 55 def extend_wrapper(cls): 56 setattr(cls, 'base', cls.__base__) 57 setattr(cls, 'nsid', nsid) 58 Extensions[cls.__base__.__name__].add(nsid, cls) 59 cls.fieldtypes.update(copy.deepcopy(cls.__base__.fieldtypes)) 60 return cls 61 return extend_wrapper
@extends decorator
This decorator must be used in front of all extensions to Map-based classes.
@cls: The class which is being extended (no need to specify) @base: The base class to extend @nsid: The profile name @return: The class definition that is now registered and usable in openc2lib.