
    *1f                        d Z ddlZddlZddlZddlmZmZ ddlmZm	Z	m
Z
 ddlmZmZmZ  ej                  e      Z G d dej$                        Z G d d	ej(                  j*                  ej(                  j,                  e
      Zy)zPlugins are the `functions` of the volatility framework.

They are called and carry out some algorithms on data stored in layers
using objects constructed from symbols.
    N)ABCMetaabstractmethod)ListTupleType)
exceptions	constants
interfacesc                        e Zd ZdZdeddf fdZed        Zej                  defd       Ze	d        Z
ededefd	       Zd
 Zd Z xZS )FileHandlerInterfacezClass for storing Files in the plugin as a means to output a file when necessary.

    This can be used as ContextManager that will close/produce the file automatically when exiting the context block
    filenamereturnNc                 >    d| _         || _        t        |           y)zsCreates a FileHandler

        Args:
            filename: The requested name of the filename for the data
        N)_preferred_filenamepreferred_filenamesuper__init__)selfr   	__class__s     Z/home/panchajanya/Development/work/volatility3/volatility3/framework/interfaces/plugins.pyr   zFileHandlerInterface.__init__   s      $( "*    c                     | j                   S )zThe preferred filename to save the data to.
        Until this file has been written, this value may not be the final filename the data is written to.
        )r   r   s    r   r   z'FileHandlerInterface.preferred_filename&   s    
 '''r   c                     | j                   rt        d      t        |t              st	        d      t
        j                  j                  |v rt        d      || _	        y)zSets the preferred filenamez.FileHandler name cannot be changed once closedz/FileHandler preferred filenames must be stringsz4FileHandler filenames cannot contain path separatorsN)
closedIOError
isinstancestr	TypeErrorospathsep
ValueErrorr   )r   r   s     r   r   z'FileHandlerInterface.preferred_filename-   sQ     ;;JKK(C(MNN77;;("STT#+ r   c                      y)zAMethod that commits the file and fixes the final filename for useN r   s    r   closezFileHandlerInterface.close8       r   c                 :    d}d}| D ]  }||v r||z  }|dz  } |S )z]Sanititizes the filename to ensure only a specific whitelist of characters is allowed throughzSabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.- ()[]{}!$%^:#~?<>,| ?r%   )r   allowedresultchars       r   sanitize_filenamez&FileHandlerInterface.sanitize_filename<   s>     h 	Dw$#		
 r   c                     | S Nr%   r   s    r   	__enter__zFileHandlerInterface.__enter__H   s    r   c                     |||| j                          y t        j                  d| j                   dt	        |              | j                          y )NzFile z could not be written: )r&   vollogwarningr   r   )r   exc_type	exc_value	tracebacks       r   __exit__zFileHandlerInterface.__exit__K   sO    	 1i6GJJLNN0011HYHXY JJLr   )__name__
__module____qualname____doc__r   r   propertyr   setterr   r&   staticmethodr.   r1   r8   __classcell__r   s   @r   r   r      s    
   ( ( ,3 , , P P 	C 	C 	 	r   r   c            	       >    e Zd ZU dZdZeeeef   ed<   	 	 ddej                  j                  dedej                  ddf fd	Zed
        Zdee   ddfdZedeej,                  j.                     f fd       Zedej4                  j6                  fd       Z xZS )PluginInterfaceaU  Class that defines the basic interface that all Plugins must maintain.

    The constructor must only take a `context` and `config_path`, so
    that plugins can be launched automatically.  As such all
    configuration information must be provided through the requirements
    and configuration information in the context it is passed.
    )r   r   r   _required_framework_versionNcontextconfig_pathprogress_callbackr   c                 n   t         |   ||       |xs d | _        | j                  ||      r*t        j                  d       t        j                  d      | j                         D ]>  }|j                  | j                  vs|j                  | j                  |j                  <   @ t        | _        y)a  

        Args:
            context: The context that the plugin will operate within
            config_path: The path to configuration data within the context configuration data
            progress_callback: A callable that can provide feedback at progress points
        c                      y r0   r%   )fss     r   <lambda>z*PluginInterface.__init__.<locals>.<lambda>   r'   r   zPlugin failed validationz+The plugin configuration failed to validateN)r   r   _progress_callbackunsatisfiedr3   r4   r   PluginRequirementExceptionget_requirementsnameconfigdefaultr   _file_handler)r   rE   rF   rG   requirementr   s        r   r   zPluginInterface.__init__t   s     	+."3"J8I G[1NN5677=   002 	DKt{{20;0C0CK,,-	D :Nr   c                     | j                   S )z:Returns a context manager and thus can be called like open)rT   r   s    r   openzPluginInterface.open   s     !!!r   handlerc                 H    t        |t              st        d      || _        y)z0Sets the file handler to be used by this plugin.z6FileHandler must be a subclass of FileHandlerInterfaceN)
issubclassr   r#   rT   )r   rX   s     r   set_open_methodzPluginInterface.set_open_method   s!    '#78UVV$r   c                      t         |          S )z6Returns a list of Requirement objects for this plugin.)r   rP   )clsr   s    r   rP   z PluginInterface.get_requirements   s     w'))r   c                      y)a  Executes the functionality of the code.

        .. note:: This method expects `self.validate` to have been called to ensure all necessary options have been provided

        Returns:
            A TreeGrid object that can then be passed to a Renderer.
        Nr%   r   s    r   runzPluginInterface.run   r'   r   r0   )r9   r:   r;   r<   rD   r   int__annotations__r
   rE   ContextInterfacer   r	   ProgressCallbackr   r=   rW   r   r   r[   classmethodr   configurationRequirementInterfacerP   r   	renderersTreeGridr_   r@   rA   s   @r   rC   rC   c   s    
 9BsC}!5A{ 9=	N##44N N %55	N
 
N: " "%t,@'A %d % *j&>&>&S&S!T * * Z))22  r   rC   )	metaclass)r<   iologgingr    abcr   r   typingr   r   r   volatility3.frameworkr   r	   r
   	getLoggerr9   r3   	RawIOBaser   re   ConfigurableInterfaceVersionableInterfacerC   r%   r   r   <module>rs      st    
  	 ' $ $ C C			8	$<2<< <ZF2211Fr   