Module: containers.py — badsnakes - v0.2.0 Documentation

# Module: containers.py

Purpose:

  This module provides the container classes for the various node types parsed from source code by the AST parser.

Platform:

  Linux/Windows | Python 3.10+

Developer:

  J Berendt

Email:

  development@s3dev.uk

Comments:

  n/a

class badsnakes.libs.containers._NodeBase(*, name: str = None, value: str | int = None, line: int = None, line_end: int = None, severity: Severity = Severity.OK)

  Bases: `object`

  Private base class inherited by each AST node type container.

  Parameters:

    - name (str, optional) – Node name. Defaults to None.

    - value (str | int, optional) – Node value. Defaults to None.

    - line (int, optional) – Starting line number. Defaults to None.

    - line_end (int, optional) – Ending line number. Defaults to None.

  Implementation note:

    Although many instances of this class will exist during an extraction, `__slots__` has intentionally not been implemented in either this class nor its children due to higher memory usage.

    When a class is inherited, the attributes are copied to its children, thus requiring more memory. During development research, using `__slots__` for this class increased memory usage.

  Developer note:

    Do not add any attributes to this class, nor the child classes as they will propagate through into the container objects and trigger errors on execution.

  property _log_template

    Set the generalised logging template for all containers.

  property _report_template

    Set the generalised reporting template for all containers.

  report_longstring()

    Display a formatted view for long strings.

  tolower()

    Convert the named attribute values to lower case.

class badsnakes.libs.containers.Assign(*, name: str = None, value: str | int = None, line: int = None, line_end: int = None, severity: Severity = Severity.OK)

  Bases: `_NodeBase`

  Container class for parsed assignment nodes: `ast.Assign`.

  Parameters:

    Refer to the docstring for the `_NodeBase` class for argument descriptions.

  report()

    Display a formatted view for assignments.

  property _log_template

    Set the generalised logging template for all containers.

  property _report_template

    Set the generalised reporting template for all containers.

  report_longstring()

    Display a formatted view for long strings.

  tolower()

    Convert the named attribute values to lower case.

class badsnakes.libs.containers.Attribute(*, name: str = None, value: str | int = None, line: int = None, line_end: int = None, severity: Severity = Severity.OK)

  Bases: `_NodeBase`

  Container class for parsed attribute nodes: `ast.Attribute`.

  Parameters:

    Refer to the docstring for the `_NodeBase` class for argument descriptions.

  report()

    Display a formatted view for object attributes.

  property _log_template

    Set the generalised logging template for all containers.

  property _report_template

    Set the generalised reporting template for all containers.

  report_longstring()

    Display a formatted view for long strings.

  tolower()

    Convert the named attribute values to lower case.

class badsnakes.libs.containers.Call(module: str = None, **kwargs)

  Bases: `_NodeBase`

  Container class for parsed function call nodes: `ast.Call`.

  Parameters:

    module (str) – Name of the module being imported.

  Refer to the docstring for the `_NodeBase` class for additional argument descriptions.

  report()

    Display a formatted view for function calls.

  property _log_template

    Set the generalised logging template for all containers.

  property _report_template

    Set the generalised reporting template for all containers.

  report_longstring()

    Display a formatted view for long strings.

  tolower()

    Convert the named attribute values to lower case.

class badsnakes.libs.containers.CodeText(reason: str = None, **kwargs)

  Bases: `_NodeBase`

  Container class for textual code analysis.

  Parameters:

    reason (str) – Reason the code in the module was flagged.

  Refer to the docstring for the `_NodeBase` class for additional argument descriptions.

  report()

    Display a formatted view for code text findings.

  property _log_template

    Set the generalised logging template for all containers.

  property _report_template

    Set the generalised reporting template for all containers.

  report_longstring()

    Display a formatted view for long strings.

  tolower()

    Convert the named attribute values to lower case.

class badsnakes.libs.containers.Constant(searchstr: str = None, **kwargs)

  Bases: `_NodeBase`

  Container class for parsed constant nodes: `ast.Constant`.

  Parameters:

    searchstr (str) – Blacklisted keyword found in the constant.

  Refer to the docstring for the `_NodeBase` class for additional argument descriptions.

  report()

    Display a formatted view for constants.

    This method accounts for both constants as strings and arguments passed into function calls.

  property _log_template

    Set the generalised logging template for all containers.

  property _report_template

    Set the generalised reporting template for all containers.

  report_longstring()

    Display a formatted view for long strings.

  tolower()

    Convert the named attribute values to lower case.

class badsnakes.libs.containers.FunctionDef(*, name: str = None, value: str | int = None, line: int = None, line_end: int = None, severity: Severity = Severity.OK)

  Bases: `_NodeBase`

  Container class for parsed function definition nodes: `ast.FunctionDef`.

  Parameters:

    Refer to the docstring for the `_NodeBase` class for argument descriptions.

  report()

    Display a formatted view for function definitions.

  property _log_template

    Set the generalised logging template for all containers.

  property _report_template

    Set the generalised reporting template for all containers.

  report_longstring()

    Display a formatted view for long strings.

  tolower()

    Convert the named attribute values to lower case.

class badsnakes.libs.containers.Import(module: str, asname: str = None, **kwargs)

  Bases: `_NodeBase`

  Container class for module import nodes: `ast.Import`.

  Parameters:

    - module (str) – Name of the module being imported.

    - asname (str, optional) – An import’s alias if the library is imported using `import mylib as _mylib`. Defaults to None.

  Refer to the docstring for the `_NodeBase` class for additional argument descriptions.

  report()

    Display a formatted view for module imports.

    This method accounts for the various ways modules and libraries can be imported.

  property _log_template

    Set the generalised logging template for all containers.

  property _report_template

    Set the generalised reporting template for all containers.

  report_longstring()

    Display a formatted view for long strings.

  tolower()

    Convert the named attribute values to lower case.