pyprediktorutilities package

Subpackages

Submodules

pyprediktorutilities.file_transfer module

class pyprediktorutilities.file_transfer.SFTPClient(server: str, username: str, password: str, port: int)[source]

Bases: object

An SFTP client to upload files to a remote server

Parameters:
  • server (AnyUrl) – The server address

  • username (str) – The username

  • password (str) – The password

  • port (int) – The port number

Returns:

SFTPClient object

Return type:

Object

upload(files: list[str], remote_directory: str)[source]

Upload the files to the remote directory, create the directory recursively if it does not exist (and the remote server allows it)

Parameters:
  • files (list[str]) – _description_

  • remote_directory (str) – _description_

pyprediktorutilities.send_email module

class pyprediktorutilities.send_email.SendEmail(server: str, port: int, username: str, password: str)[source]

Bases: object

Helper function to send emails with attachments using SMTP

Parameters:
  • server (str) – SMTP server address

  • port (int) – SMTP server port

  • username (str) – SMTP server username

  • password (str) – SMTP server password

Returns:

SendEmail object

Return type:

Object

send_email(from_email: EmailStr, recipients: list[EmailStr], subject: str, body: str, files: list = [])[source]

_summary_

Parameters:
  • from_email (email address) – The sender email address

  • recipients (list of email addresses) – list of one or more recipient email addresses

  • subject (str) – The email subject

  • body (str) – The email body

  • files (list, optional) – A list of paths to files to include. Defaults to [].

Raises:
  • ValueError – If no recipients are specified, or a file path contains a NUL byte or a path traversal sequence

  • FileNotFoundError – If a file in files does not exist

pyprediktorutilities.shared module

pyprediktorutilities.shared.build_validated_url(base_url: str, endpoint: str) str[source]

Build a validated URL by safely appending an endpoint to a base URL.

Rejects endpoint values containing “../” (or its URL-encoded form) before the URL is ever assembled, then rebuilds the URL via urlparse/urlunparse.

Parameters:
  • base_url (str) – The base URL (scheme, host, and optional base path)

  • endpoint (str) – The endpoint path to append

Raises:

ValueError – If the endpoint contains a path traversal sequence, or the URL could not be built

Returns:

The safely constructed URL

Return type:

str

pyprediktorutilities.shared.request_from_api(rest_url: AnyUrl, method: Literal['GET', 'POST'], endpoint: str, data: str = None, params: dict = None, headers: dict = None, extended_timeout: bool = False) str[source]

Function to perform request against a REST API

Parameters:
  • rest_url (str) – The URL with trailing shash

  • method (str) – “GET” or “POST”

  • endpoint (str) – The last part of the url (without the leading slash)

  • data (str) – defaults to None but can contain the data to send to the endpoint

  • headers (str) – default to None but can contain the headers og the request

Raises:

ValueError – If the resulting URL uses a disallowed scheme or targets a disallowed (private/loopback/link-local/etc.) address

Returns:

The result if successfull

Return type:

JSON

pyprediktorutilities.shared.sanitize_file_path(file: str) str[source]

Sanitize a file path immediately before it is opened.

Rejects NUL bytes and “..” path traversal segments, then returns a normalized, fully-resolved absolute path.

Parameters:

file (str) – The file path to sanitize

Raises:

ValueError – If the path contains a NUL byte or a “..” traversal segment

Returns:

The normalized, absolute file path

Return type:

str

pyprediktorutilities.shared.validate_file(file: str)[source]
pyprediktorutilities.shared.validate_folder(folder: str)[source]
pyprediktorutilities.shared.validate_safe_path(base_directory: str, filename: str) str[source]

Resolve filename relative to base_directory and ensure the result does not escape base_directory.

Parameters:
  • base_directory (str) – The directory the file is expected to reside in

  • filename (str) – The (possibly untrusted) file name/path to validate

Raises:

ValueError – If the resolved path escapes base_directory

Returns:

The resolved, safe absolute path

Return type:

str

pyprediktorutilities.shared.validate_safe_url(url: str) str[source]

Guard against SSRF by rejecting obviously unsafe outbound request targets.

Only allows the http/https schemes, rejects a small denylist of well-known internal hostnames, and rejects requests to loopback, private, link-local, reserved, multicast or unspecified IP addresses (this covers the common cloud metadata endpoint 169.254.169.254 when it is used directly as the host).

But this performs no DNS resolution (to keep it fast and avoid depending on network access), so it does not protect against DNS rebinding attacks where a hostname resolves to an internal address only at connection time.

Parameters:

url (str) – The fully-formed URL that a request is about to be made to

Raises:

ValueError – If the URL uses a disallowed scheme or targets a disallowed host

Returns:

The validated URL, unchanged

Return type:

str

pyprediktorutilities.singleton module

class pyprediktorutilities.singleton.SingletonMeta[source]

Bases: type

This is a thread-safe implementation of Singleton.

pyprediktorutilities.templating module

class pyprediktorutilities.templating.Templating(path: str)[source]

Bases: object

Simple wrapper around a templating engine, allowing for easy rendering of templates such as XMLs and HTMLs. Build on top of Jinja2

Parameters:

path (str) – The path to the folder containing the templates

path

The path to the folder containing the templates

Type:

str

env

The environment to use for rendering

Type:

jinja2.Environment

Raises:

Examples

>>> from pyprediktorutilities.templating import Templating
>>> templating = Templating('templates')
>>> templates = templating.list_templates()
>>> template = templating.load_template('base.html')
>>> rendered_template = templating.render(template, title='Home')
>>> templating.render_to_file(template, 'output.html', title='Home')
list_templates() list[str][source]

Returns a list of templates in the folder

Returns:

A list of strings containing the template names

Return type:

list[str]

load_template(template: str) object[source]

Loads a template from the folder

Parameters:

template (str) – The file name of the template to load

Raises:
Returns:

The template object

Return type:

object

render(template: str, **kwargs) str[source]

Render a template with the given arguments and return a string

Parameters:
  • template (str) – The file name of the template to load

  • **kwargs – The arguments to pass to the template

Raises:

Exception – If the template could not be rendered

Returns:

The rendered template

Return type:

str

render_to_file(template: str, file: str, **kwargs) None[source]

Render a template with the given arguments and write to a file

Parameters:
  • template (str) – The file name of the template to load

  • file (str) – The path and file name of the file to write to.

  • **kwargs – The arguments to pass to the template

Raises:
Returns:

None

Return type:

None

Module contents