mirror of
https://github.com/volatilityfoundation/volatility3.git
synced 2026-09-29 04:54:51 +02:00
Remove constraints as a thing, they'll need to come back, but in a different form.
This commit is contained in:
@@ -83,6 +83,88 @@ class RequirementInterface(validity.ValidityRoutines, metaclass = ABCMeta):
|
||||
"""
|
||||
|
||||
|
||||
class ClassRequirement(RequirementInterface):
|
||||
"""Requires a specific class"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
RequirementInterface.__init__(self, *args, **kwargs)
|
||||
self._cls = None
|
||||
|
||||
@property
|
||||
def cls(self):
|
||||
return self._cls
|
||||
|
||||
def validate(self, context, config_path):
|
||||
"""Checks to see if a class can be recovered"""
|
||||
value = self.config_value(context, config_path, None)
|
||||
self._cls = None
|
||||
if value is not None:
|
||||
if "." in value:
|
||||
# TODO: consider importing the prefix
|
||||
module = sys.modules.get(value[:value.rindex(".")], None)
|
||||
class_name = value[value.rindex(".") + 1:]
|
||||
if hasattr(module, class_name):
|
||||
self._cls = getattr(module, class_name)
|
||||
else:
|
||||
if value in globals():
|
||||
self._cls = globals()[value]
|
||||
return self._cls is not None
|
||||
|
||||
|
||||
class ConstructableRequirementInterface(RequirementInterface):
|
||||
def __init__(self, *args, **kwargs):
|
||||
RequirementInterface.__init__(self, *args, **kwargs)
|
||||
self.add_requirement(ClassRequirement("class", "Class of the translation layer"))
|
||||
self._current_class_requirements = set()
|
||||
|
||||
@abstractmethod
|
||||
def construct(self, context, config_path):
|
||||
"""Method for constructing within the context any required elements from subrequirements"""
|
||||
|
||||
def _check_class(self, context, config_path):
|
||||
"""Method to check if the class Requirement is valid and if so populate the other requirements
|
||||
(but no need to validate, since we're invalid already)
|
||||
"""
|
||||
class_req = self.requirements['class']
|
||||
subreq_config_path = path_join(config_path, self.name)
|
||||
if class_req.validate(context, subreq_config_path):
|
||||
# We have a class, and since it's validated we can construct our requirements from it
|
||||
if issubclass(class_req.cls, ConfigurableInterface):
|
||||
# In case the class has changed, clear out the old requirements
|
||||
for old_req in self._current_class_requirements.copy():
|
||||
del self._requirements[old_req]
|
||||
self._current_class_requirements.remove(old_req)
|
||||
# And add the new ones
|
||||
for requirement in class_req.cls.get_requirements():
|
||||
self._current_class_requirements.add(requirement.name)
|
||||
self.add_requirement(requirement)
|
||||
|
||||
def _construct_class(self, context, config_path, requirement_dict = None):
|
||||
"""Constructs the class, handing args and the subrequirements as parameters to __init__"""
|
||||
cls = self.requirements["class"].cls
|
||||
|
||||
# These classes all have a name property
|
||||
# We could subclass this out as a NameableInterface, but it seems a little excessive
|
||||
# FIXME: We can't test this, because importing the other interfaces causes all kinds of import loops
|
||||
# if not issubclass(cls, [interfaces.layers.TranslationLayerInterface,
|
||||
# interfaces.symbols.SymbolTableInterface]):
|
||||
# return None
|
||||
|
||||
if requirement_dict is None:
|
||||
requirement_dict = {}
|
||||
|
||||
node_config = context.config.branch(config_path)
|
||||
# Construct the class
|
||||
for req in cls.get_requirements():
|
||||
if req.name in node_config.data and req.name != "class":
|
||||
requirement_dict[req.name] = node_config.data[req.name]
|
||||
# Fulfillment must happen, exceptions happening here mean the requirements aren't correct
|
||||
# and these need to be raised and fixed, rather than caught and ignored
|
||||
obj = cls(**requirement_dict)
|
||||
context.config[config_path] = obj.name
|
||||
return obj
|
||||
|
||||
|
||||
class ConfigurableInterface(validity.ValidityRoutines):
|
||||
"""Class to allow objects to have requirements and read configuration data from the context config tree"""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user