FileTree Plugin

Simple visitor API used to generate source trees that can be applied to the jsTree javascript plugin.

All classes of this module should be treated as internal. However, if you want to include a new visitor, there are two ways to implement it:

  1. Define a python function that inserts necessary data:

    1from mastf.MASTF.utils.filetree import visitor
    2
    3@visitor(suffix=r".*\.(txt)$")
    4def visit_txt(file: pathlib.Path, children: list, root_name: str):
    5    ... # handle file and add the item to the children list
    
  2. Insert a new JSON structure to /mastf/json/filetypes_rules.json:

    {
        # ...
        "{name}": {
            "is_dir": False,
            "suffix": "{pattern}",
            "language": "{language}"
        }
        # ...
    }
    

    Whereby name corresponds to a SVG file with the same name stored in /mastf/static/static/filetypes/. Use a pattern within the suffix variable to apply your filter to more than just one file type. The specified language will be used when showing the file in the web frontend.

mastf.MASTF.utils.filetree.visitor(is_dir=False, suffix: str = '.*')[source]

Creates a new visitor by wrapping the underlying function

Parameters:
  • is_dir (bool, optional) – describes whether the visitor applies to directories, defaults to False

  • suffix (str, optional) – pattern for files, defaults to r".*"

mastf.MASTF.utils.filetree.apply_rules(root: Path, root_name: str) dict[source]

Applies loaded rules to the given file path.

Parameters:
  • root (pathlib.Path) – the root file

  • root_name (str) – the root node’s name (may differ from file name)

Returns:

a dictionary that can be used within jsTree definitions

Return type:

dict

Classes

class mastf.MASTF.utils.filetree._Visitor(is_dir: bool, suffix: str, clb)[source]

Internal visitor class used to add the files internally.

Each instance stores the RegEx pattern to identify a file matching a given ruleset. Additionally, a callback function is defined that will be called whenever the pattern is matched.

clb = None

The callback function with the following structure:

>>> def function(file: pathlib.Path, children: list, root_name: str):
...     pass
>>> clb = function
common_path = None

Common path can be used to apply filters according to the common base path.

is_dir = False

Tells the internal algorithm to match only directories with the pattern.

suffix = None

The RegEx pattern to identify a specific file set.

class mastf.MASTF.utils.filetree._FileDesc(file: Path, file_type: str, root_name: str, language: str = None)[source]

Internal wrapper class to create JSTree JSON data.