Skip to content

Path parts

PathPartsExtract

Bases: ExtractionMethod

Extracts the parts of a given path skipping skip number of top level parts.

Method name: path_parts

Example configuration

.. code-block:: yaml

- method: path_parts
  inputs:
    path: $uri
    skip: 2
Source code in extraction_methods/plugins/path_parts.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class PathPartsExtract(ExtractionMethod):
    """
    Extracts the parts of a given path skipping ``skip`` number
    of top level parts.

    **Method name:** ``path_parts``

    Example configuration:
        .. code-block:: yaml

            - method: path_parts
              inputs:
                path: $uri
                skip: 2
    """

    input_class = PathPartsInput

    @update_input
    def run(self, body: dict[str, Any]) -> dict[str, Any]:

        path = Path(self.input.path)

        parts = list(path.parts)[self.input.skip :]

        body["filename"] = parts.pop()

        dir_level = 1
        for part in parts:
            body[f"_dir{dir_level}"] = part
            dir_level += 1

        return body

PathPartsInput

Bases: Input

Model for Path Parts Input.

Parameters:

Name Type Description Default
path str

path for method to run on.

'$uri'
skip int

number of path parts to skip.

0
Source code in extraction_methods/plugins/path_parts.py
23
24
25
26
27
28
29
30
31
32
33
34
35
class PathPartsInput(Input):
    """
    Model for Path Parts Input.
    """

    path: str = Field(
        default="$uri",
        description="path for method to run on.",
    )
    skip: int = Field(
        default=0,
        description="number of path parts to skip.",
    )