Skip to main content

Shared utilities for logging and configuration management

Project description

Spello Consulting Utility Library

A Python utility library for log file management and YAML configuration file management.

Example code

import sys

from config_schemas import ConfigSchema
from sc_utility import SCConfigManager, SCLogger

CONFIG_FILE = "config.yaml"
EXCEL_FILE = "sample_excel.xlsx"

def main():
    """Main function to run the example code."""
    print(f"Hello from sc-utility! running on {platform.system()}")

    # Get our config schema, validation schema, and placeholders
    schemas = ConfigSchema()

    # Initialize the SCConfigManager class
    try:
        config = SCConfigManager(
            config_file=CONFIG_FILE,
            default_config=schemas.default,
            validation_schema=schemas.validation,
            placeholders=schemas.placeholders
        )
    except RuntimeError as e:
        print(f"Configuration file error: {e}", file=sys.stderr)
        return

    # Get some parameteer from the config file
    print(f"API key = {config.get('AmberAPI', 'APIKey')}")

    # Initialize the SCLogger class
    try:
        logger = SCLogger(config.get_logger_settings())
    except RuntimeError as e:
        print(f"Logger initialisation error: {e}", file=sys.stderr)
        return

    # Log a message to the log file
    logger.log_message("This is a test message at the debug level.", "debug")

    # Setup email
    email_settings = config.get_email_settings()
    logger.register_email_settings(email_settings)

    text_msg = "Hello world from the sc_utility library."
    if logger.send_email("Hello world", text_msg):
        logger.log_message("Email sent OK.", "detailed")

    # Test the ExcelReader class
    excel_reader = ExcelReader(EXCEL_FILE)
    try:
        table_data = excel_reader.extract_data(source_name="Table1", source_type="table")
        print(f"Extracted table data: {table_data}")
    except ImportError as e:
        logger.log_fatal_error(f"Error extracting table: {e}")

    # See if there was a fatal error during a prior run. 
    if logger.get_fatal_error():
        print("Prior fatal error detected.")
        logger.clear_fatal_error()

if __name__ == "__main__":
    main()

Configuration File

The library uses a YAML file for configuration. An example of included with the project (config.yaml.example). Copy this to your_app.yaml before using the library. Here's an example config file - the library expects to find the Files and Email sections in the file:

# Just an example section to show how to set up a section
AmberAPI:
    APIKey: somerandomkey342
    BaseUrl: https://api.amber.com.au/v1
    Timeout: 15

Files:
    LogfileName: logfile.log
    LogfileMaxLines: 500
    LogfileVerbosity: detailed
    ConsoleVerbosity: detailed

Email:
    EnableEmail: True
    SMTPServer: smtp.gmail.com
    SMTPPort: 587
    SMTPUsername: me@gmail.com
    SMTPPassword: <Your SMTP password>
    SubjectPrefix: "[Bob Portfolio]: "

Configuration Parameters

Section: Files

Parameter Description
LogfileName The name of the log file, can be a relative or absolute path.
LogfileMaxLines Maximum number of lines to keep in the log file. If zero, file will never be truncated.
LogfileVerbosity The level of detail captured in the log file. One of: none; error; warning; summary; detailed; debug; all
ConsoleVerbosity Controls the amount of information written to the console. One of: error; warning; summary; detailed; debug; all. Errors are written to stderr all other messages are written to stdout

Section: Email

Parameter Description
EnableEmail Set to True if you want to allow the app to send emails. If True, the remaining settings in this section must be configured correctly.
SMTPServer The SMTP host name that supports TLS encryption. If using a Google account, set to smtp.gmail.com
SMTPPort The port number to use to connect to the SMTP server. If using a Google account, set to 587
SMTPUsername Your username used to login to the SMTP server. If using a Google account, set to your Google email address.
SMTPPassword The password used to login to the SMTP server. If using a Google account, create an app password for the app at https://myaccount.google.com/apppasswords
SubjectPrefix Optional. If set, the app will add this text to the start of any email subject line for emails it sends.

Library reference

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sc_utility-1.1.8.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sc_utility-1.1.8-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file sc_utility-1.1.8.tar.gz.

File metadata

  • Download URL: sc_utility-1.1.8.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sc_utility-1.1.8.tar.gz
Algorithm Hash digest
SHA256 419b0fdd50988ffbf67c450552489a8f145df6b38d46a14bd3527a8c8066d793
MD5 316d7520d02aa90742b0baf840f742bc
BLAKE2b-256 0226cc99344b0e0b9c3583f6c83ebaa927409208e2b2dd816e99562ed87e7c15

See more details on using hashes here.

Provenance

The following attestation bundles were made for sc_utility-1.1.8.tar.gz:

Publisher: publish.yml on NickElseySpelloC/sc_utility

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sc_utility-1.1.8-py3-none-any.whl.

File metadata

  • Download URL: sc_utility-1.1.8-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sc_utility-1.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 417a63092e30063569722aa4a47d237ec4f0d0afdc4ff912d39b8482eb6527e5
MD5 f8fc4c4ed603e6c6e8778133930ed280
BLAKE2b-256 51dedca51aff6b20444fa34e58ea5e3f8593c8fbce11cfcf5a3d32711361be67

See more details on using hashes here.

Provenance

The following attestation bundles were made for sc_utility-1.1.8-py3-none-any.whl:

Publisher: publish.yml on NickElseySpelloC/sc_utility

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page