Module: libs/utilities.py

Purpose:

This module provides utility-based functionality for the project.

Platform:

Linux/Windows | Python 3.10+

Developer:

J Berendt

Email:

development@s3dev.uk

Comments:

n/a

class Utilities[source]

Bases: object

General (cross-project) utility functions.

static collect_files(path: str, ext: str, recursive: bool) list[source]

Collect all files for a given extension from a path.

Parameters:
  • path (str) – Full path serving as the root for the search.

  • ext (str, optional) –

    If the path argument refers to a directory, a specific file extension can be specified here. For example: ext = 'pdf'.

    If anything other than '**' is provided, all alpha-characters are parsed from the string, and prefixed with *.. Meaning, if '.pdf' is passed, the characters 'pdf' are parsed and prefixed with *. to create '*.pdf'. However, if 'things.foo' is passed, the derived extension will be '*.thingsfoo'. Defaults to ‘**’, for a recursive search.

  • recursive (bool) – Instruct the search to recurse into sub-directories.

Returns:

The list of full file paths returned by the glob call. Any directory-only paths are removed.

Return type:

list

static ispdf(path: str) bool[source]

Test the file signature. Verify this is a valid PDF file.

Parameters:

path (str) – Path to the file being tested.

Returns:

True if this is a valid PDF file, otherwise False.

Return type:

bool

static iszip(path: str) bool[source]

Test the file signature. Verify this is a valid ZIP archive.

Parameters:

path (str) – Path to the file being tested.

Returns:

True if this is a valid ZIP archive, otherwise False.

Return type:

bool

static parse_to_keywords(resp: str) list[source]

Parse the bot’s response into a list of keywords.

Parameters:

resp (str) – Text response directly from the bot.

Returns:

A list of keywords extracted from the response, separated by asterisks as bullet points.

Return type:

list