module

pipen_filters.filters

Provides the filters

Functions
  • add_filter(aliases) (Callable) Add a filter to the FILTERS</>
  • as_path(pth) (Path) Convert a path to a Path object</>
  • basename(pth) (str) Get the basename of a path</>
  • commonprefix(*paths, basename_only) (str) Get the common prefix of a set of paths</>
  • config(x, loader) (Mapping) Get the configuration (python dictionary) from a file</>
  • dirname(pth) (str) Get the directory name of a path</>
  • exists(pth) (bool) Check if a path exists</>
  • ext(pth, ignore, recursive) (str) Get the extension of a file</>
  • ext0(pth, ignore, recursive) (str) Get the extension of a file without the leading dot</>
  • filename(pth, ignore, recursive) (str) Get the filename of a file.</>
  • filename0(pth, ignore, recursive) (str) Get the filename of a file without the extension</>
  • getatime(pth) (int) Get the access time of a file, return -1 if the file does not exist</>
  • getctime(pth) (int) Get the creation time of a file, return -1 if the file does not exist</>
  • getmtime(pth) (int) Get the modification time of a file, return -1 if the file does not exist</>
  • getsize(pth) (int) Get the size of a file, return -1 if the file does not exist</>
  • glob(*paths) (List) Glob a path</>
  • glob0(*paths) (str) Glob a path and return the first result</>
  • isdir(pth) (bool) Check if a path is a directory</>
  • isempty(pth, ignore_ws, nonfile_as_empty) (bool) Check if a file is empty</>
  • isfile(pth) (bool) Check if a path is a file</>
  • islink(pth) (bool) Check if a path is a symlink</>
  • joinpaths(*paths) (str) Join paths.</>
  • json_dumps(var) (str) Dump an object to json.</>
  • json_load(pth) (Any) Load a json file</>
  • json_loads(jsonstr) (Any) Load a json string to an object</>
  • prefix(pth, ignore, recursive) (str) Get the prefix of a file</>
  • prefix0(pth, ignore, recursive) (str) Get the prefix of a file without the extension</>
  • quote(var) (str) Quote a string</>
  • read(file, *args, **kwargs) (Union) Read the contents from a file</>
  • readlines(file, *args, **kwargs) (Union) Read the lines from a file</>
  • readlink(pth) (str) Get the link of a symlink</>
  • realpath(pth) (str) Get the real path of a path</>
  • regex_replace(string, pattern, repl, count, flags) (str) Replace the matched pattern with a string</>
  • slugify(string, *args, **kwargs) (str) Slugify a string</>
  • squote(var) (str) Quote a string with single quotes</>
  • toml(var) (str) Dump an object to toml.</>
  • toml_load(pth) (Any) Load a toml file. null will be loaded as None</>
  • toml_loads(tomlstr) (Any) Load a toml string to an object, null will be loaded as None</>
function

pipen_filters.filters.add_filter(aliases=None)

Add a filter to the FILTERS

Examples

Filters added: myfilter

>>> @add_filter
... def myfilter(var):
...     return var

>>> @add_filter()
... def myfilter(var):
...     return var

Filters added: myfilter, myfilter2

>>> @add_filter("myfilter2")
... def myfilter(var):
...     return var

Parameters
  • aliases (Union, optional) The aliases of the filter, the filter itself, or None
Returns (Callable)

The filter itself if used directly @add_filter; orThe decorator to add the filter if used with arguments @add_filter(...)

function

pipen_filters.filters.realpath(pth)

Get the real path of a path

Parameters
  • pth (PathLike) The path to the file
Returns (str)

The real path of the file

function

pipen_filters.filters.commonprefix(*paths, basename_only=True)

Get the common prefix of a set of paths

Examples
>>> commonprefix("/a/b/abc.txt", "/a/b/abc.png")>>> # "abc."
>>> commonprefix("/a/b/abc.txt", "/a/b/abc.png", basename_only=False)
>>> # "/a/b/abc."
Parameters
  • *paths (PathLike) The paths to find commonprefix agaist
  • basename_only (bool, optional) Only search on the basenames
Returns (str)

The common prefix of the paths

function

pipen_filters.filters.dirname(pth)

Get the directory name of a path

For example, /a/b/c.txt => /a/b/

Parameters
  • pth (PathLike) The path to the file
Returns (str)

The directory name of the file

function

pipen_filters.filters.basename(pth)

Get the basename of a path

For example, /a/b/c.txt => c.txt

Parameters
  • pth (PathLike) The path to the file
Returns (str)

The basename of the file

function

pipen_filters.filters.ext(pth, ignore=[], recursive=False)

Get the extension of a file

For example, /a/b/c.txt => .txt.

Aliases: suffix

Parameters
  • pth (PathLike) The path to the file
  • ignore (list[str] | str, optional) The extensions to ignoreThe extensions can be with or without leading dot
  • recursive (bool, optional) Recursively ignore the extensions from the end
Returns (str)

The extension of the file

function

pipen_filters.filters.ext0(pth, ignore=[], recursive=False)

Get the extension of a file without the leading dot

For example, /a/b/c.txt => txt.

Aliases: suffix0

Parameters
  • pth (PathLike) The path to the file
  • ignore (list[str] | str, optional) The extensions to ignoreThe extensions can be with or without leading dot
  • recursive (bool, optional) Recursively ignore the extensions from the end
Returns (str)

The extension of the file without the leading dot

function

pipen_filters.filters.prefix(pth, ignore=[], recursive=False)

Get the prefix of a file

For example, /a/b/c.txt => /a/b/c

Parameters
  • pth (PathLike) The path to the file
  • ignore (list[str] | str, optional) The extensions to ignoreThe extensions can be with or without leading dot
  • recursive (bool, optional) Recursively ignore the extensions from the end
Returns (str)

The prefix of the file

function

pipen_filters.filters.prefix0(pth, ignore=[], recursive=False)

Get the prefix of a file without the extension

For example, /a/b/c.d.txt => /a/b/c.d

Parameters
  • pth (PathLike) The path to the file
  • ignore (list[str] | str, optional) The extensions to ignoreThe extensions can be with or without leading dot
  • recursive (bool, optional) Recursively ignore the extensions from the end
Returns (str)

The prefix of the file without the extension

function

pipen_filters.filters.filename(pth, ignore=[], recursive=False)

Get the filename of a file.

For example, /a/b/c.d.txt => c.d.

Aliases: fn, stem

Parameters
  • pth (PathLike) The path to the file
  • ignore (list[str] | str, optional) The extensions to ignoreThe extensions can be with or without leading dot
  • recursive (bool, optional) Recursively ignore the extensions from the end
Returns (str)

The filename of the file

function

pipen_filters.filters.filename0(pth, ignore=[], recursive=False)

Get the filename of a file without the extension

For example, /a/b/c.d.txt => c.

Aliases: fn0, stem0

Parameters
  • pth (PathLike) The path to the file
  • ignore (list[str] | str, optional) The extensions to ignoreThe extensions can be with or without leading dot
  • recursive (bool, optional) Recursively ignore the extensions from the end
Returns (str)

The filename of the file without the extension

function

pipen_filters.filters.joinpaths(*paths)

Join paths.

For example, joinpaths("a", "b") => "a/b".

Aliases: joinpath

Parameters
  • *paths (PathLike) The paths to join
Returns (str)

The joined path

function

pipen_filters.filters.as_path(pth)

Convert a path to a Path object

Parameters
  • pth (PathLike) The path to convert
Returns (Path)

The Path object

function

pipen_filters.filters.isdir(pth)

Check if a path is a directory

Parameters
  • pth (PathLike) The path to check
Returns (bool)

True if the path is a directory, False otherwise

function

pipen_filters.filters.isfile(pth)

Check if a path is a file

Parameters
  • pth (PathLike) The path to check
Returns (bool)

True if the path is a file, False otherwise

function

pipen_filters.filters.exists(pth)

Check if a path exists

Parameters
  • pth (PathLike) The path to check
Returns (bool)

True if the path exists, False otherwise

function

pipen_filters.filters.getsize(pth)

Get the size of a file, return -1 if the file does not exist

Parameters
  • pth (PathLike) The path to the file
Returns (int)

The size of the file

function

pipen_filters.filters.getmtime(pth)

Get the modification time of a file, return -1 if the file does not exist

Parameters
  • pth (PathLike) The path to the file
Returns (int)

The modification time of the file

function

pipen_filters.filters.getctime(pth)

Get the creation time of a file, return -1 if the file does not exist

Parameters
  • pth (PathLike) The path to the file
Returns (int)

The creation time of the file

function

pipen_filters.filters.getatime(pth)

Get the access time of a file, return -1 if the file does not exist

Parameters
  • pth (PathLike) The path to the file
Returns (int)

The access time of the file

function

pipen_filters.filters.isempty(pth, ignore_ws=True, nonfile_as_empty=False)

Check if a file is empty

Parameters
  • pth (PathLike) The path to the file
  • ignore_ws (bool, optional) Ignore whitespaces?
  • nonfile_as_empty (bool, optional) Treat non-file as empty?
Returns (bool)

True if the file is empty, False otherwise

function

pipen_filters.filters.quote(var)

Quote a string

Parameters
  • var (Any) The string to quote
Returns (str)

The quoted string

function

pipen_filters.filters.squote(var)

Quote a string with single quotes

Parameters
  • var (Any) The string to quote
Returns (str)

The quoted string

function

pipen_filters.filters.json_dumps(var)

Dump an object to json.

Aliases: json

Parameters
  • var (Any) The object to dump
Returns (str)

The json string

function

pipen_filters.filters.json_load(pth)

Load a json file

Parameters
  • pth (PathLike) The path to the json file
Returns (Any)

The loaded object

function

pipen_filters.filters.json_loads(jsonstr)

Load a json string to an object

Parameters
  • jsonstr (str) The json string
Returns (Any)

The loaded object

function

pipen_filters.filters.toml(var)

Dump an object to toml.

Aliases: toml_dumps

Parameters
  • var (Any) The object to dump
Returns (str)

The toml string

function

pipen_filters.filters.toml_load(pth)

Load a toml file. null will be loaded as None

Parameters
  • pth (PathLike) The path to the toml file
Returns (Any)

The loaded object

function

pipen_filters.filters.toml_loads(tomlstr)

Load a toml string to an object, null will be loaded as None

Parameters
  • tomlstr (str) The toml string
Returns (Any)

The loaded object

function

pipen_filters.filters.config(x, loader=None)

Get the configuration (python dictionary) from a file

Parameters
  • x (Any) The path to the file, dict or string of configurations (json or toml)
  • loader (str, optional) The loader to use, defaults to auto-detectIf x is a dict, this argument is ignored if x is a string and is not a file path, then x will be loaded as a toml string if loader is not specified if x is a file path, then x will be loaded according to the file extension
Returns (Mapping)

The config

function

pipen_filters.filters.glob(*paths)

Glob a path

Parameters
  • *paths (PathLike) The paths to glob
Returns (List)

The globbed paths

function

pipen_filters.filters.glob0(*paths)

Glob a path and return the first result

Parameters
  • *paths (PathLike) The paths to glob
Returns (str)

The first globbed path

function

pipen_filters.filters.read(file, *args, **kwargs)

Read the contents from a file

Parameters
  • file (PathLike) The path to the file
  • *args (Any) and
  • **kwargs (Any) Other arguments passed to open()
Returns (Union)

The contents of the file

function

pipen_filters.filters.readlines(file, *args, **kwargs)

Read the lines from a file

Parameters
  • file (PathLike) The path to the file
  • *args (Any) and
  • **kwargs (Any) Other arguments to open()
Returns (Union)

A list of lines in the file

function

pipen_filters.filters.regex_replace(string, pattern, repl, count=0, flags=0)

Replace the matched pattern with a string

Parameters
  • string (str) The string to search
  • pattern (str) The pattern to search
  • repl (str) The string to replace
  • flags (int, optional) The regex flags
Returns (str)

The replaced string

function

pipen_filters.filters.slugify(string, *args, **kwargs)

Slugify a string

Parameters
  • string (str) The string to slugify
  • *args (Any) and
  • **kwargs (Any) Other arguments to slugify()
Returns (str)

The slugified string