Skip to main content

Helpful tools and utils for applications running inside of Pergola Cloud

Project description

Pergola project tools

Helpful tools and utils for applications running inside of Pergola , the beautifully simple internal developer platform

Installation

pip install pergola-projecttools

Getting started

Configuration Management

Pergola offers secure stage specific configurations, which can easily by copied, adjusted and swapped. projecttools provides a simple but powerful approach to leverage those in your python application. See Pergola Configuration Management for details on the concept.

The class ConfigUtilBase provides access to a config-file mapped from Pergola configuration management.

How ConfigUtilBase provides access to a JSON config file

  1. At first, in your application, provide a path for config-files. Create a subdirectory named config and make sure it gets pushed to git (or your VCS provider). This can e.g. be enforced by adding a .gitignore file with the this content there:
*.json
!config.template.json

Note: This has the additional benefit of never accidentally commiting any json config files with passwords or other secrets.

  1. Put a file called config.json into the config subdirectory. Add some config values into the file, e.g.
{
  "username": "John",
  "appearance": {
      "theme": "dark",
      "size": 500
  }
}
  1. Use ConfigUtil to access your config utils, either quick or more sophisticated

the quick and simple method:

from pergolaprojecttools.config_util import ConfigUtilBase

# Use it right away
ConfigUtilBase.get_config_value("username") # returns 'John'
ConfigUtilBase.get_config_value("appearance") # returns a dict with 'theme' and 'size'

or the sophisticated method:

Create a class ConfigUtil inheriting from ConfigUtilBase and add get methods for all your config parameters, ensuring a safe usage. This approach also gives you more control, e.g. to specify the config file path or to navigate deeper into your JSON structure. For our example, this could look like:

class ConfigUtil(ConfigUtilBase):

    config_path = f"{os.getcwd()}/config/config.json"

    @classmethod
    def get_user(cls) -> str: 
        return cls.get_config_value('username')
    
    @classmethod
    def get_appearance(cls) -> dict:
        return cls.get_config_value('appearance')
    
    @classmethod
    def get_theme(cls) -> str: 
        return cls.get_inner_value(cls.get_config_value('appearance'), 'theme')
    
    @classmethod
    def get_size(cls) -> int: 
        return cls.get_inner_value(cls.get_config_value('appearance'), 'size')

Your own ConfigUtil class can be used directly like:

    the_user_name = ConfigUtil.get_user()

Try this locally, it works during development, in IDEs like Visual Studio or toolings like Jupyter

  1. To provide configurations in Pergola, got to the Config Management of a Pergola stage. Create a file called my-stage-config.json (named appropriately for your purpose).

  2. Map the file in Pergola Manifest:

components:
- name: my-component-name
  [...]
  files:
    - path: /app/config/config.json
      config-ref: my-stage-config.json
      [...]

A few notes here:

  • projecttools ConfigUtilBase class uses a default path for its config file of /config/config.json
  • Thus, the example above assumes the python application to be located in /app
  • config-ref, defined as my-stage-config.json is the name of the file in the Pergola stage config. The application will never see this file name, it will be mapped to /app/config/config.json
  • See Pergola Manifest Reference for details. Note, that this has the additional benefit of never accidentally commiting any json config files with passwords or other secrets.

And never forget: Do not put passwords or other secrets into your source code, always use a secure Pergola config!

Define config file location

To define an individual location for the config file, override the class variable config_path of ConfigUtilBase, e.g.

ConfigUtilBase.config_path = 'configuration/app_central.json'

Or, with subclassing and more versatile path logic:

import os
class ConfigUtil(ConfigUtilBase):

    config_path = f"{os.getcwd()}/configuration/app_central.json"

or

import pathlib
class ConfigUtil(ConfigUtilBase):
    config_path = f"{pathlib.Path(__file__).parent}/configuration/app_central.json"

Logging for Pergola

projecttools provides preconfigured simplified logging for Pergola applications.

Activation

It is activated by calling log.init_logging(). Parameters to tailor the behaviour may be used, but it works out of the box with no parameters at all.

Example:

from pergolaprojecttools import log

log.init_logging()

This way, you will automatically receive nice logging output to the console during development as well great logging output in Pergola, including pod runtime identifiers.

Usage

Simply call the log level methods with your log message:

from pergolaprojecttools import log

log.debug('My debug message')
log.info('My info message')
log.warning('My warning message')
log.error('My error message')

# log last exception with details:
log.logLastException('CONTEXT-INFO')

# add memory usage to log string:
from pergolaprojecttools.trace_util import get_mem
log.info('Important info ' + get_mem())

More control

Call log.init_logging() with parameters:

logger_basename: used for config of python logging and as part of the filenames log_folder: folder to store log files in (log_to_file needs to be True) log_level: one of debug, info, warn, error, default: debug log_to_file: True/False (at least one of [log_to_file,log_to_console] must be True) log_to_console: True/False (at least one of [log_to_file,log_to_console] must be True) log_in_pergola: True/False/None - if None is given, logging will try to detect its environment automatically

Re-configure at runtime

You can always deactivate logging with

from pergolaprojecttools import log
log.clear_logging()

and reactivate it with properties, e.g.

log.init_logging(logger_basename='central', log_folder='./logs/core/', log_to_file=True) 

Project details


Download files

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

Source Distribution

pergola_projecttools-2.2.0.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

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

pergola_projecttools-2.2.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file pergola_projecttools-2.2.0.tar.gz.

File metadata

  • Download URL: pergola_projecttools-2.2.0.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.6

File hashes

Hashes for pergola_projecttools-2.2.0.tar.gz
Algorithm Hash digest
SHA256 e7e448f4cc54f4139eea536ea8e0e9c22ef9689a7625048fcfb0408c7701b121
MD5 1237dee38a67de74e4a101dba5f7c7d7
BLAKE2b-256 b673f706f8669ab63332c292a8b84facc9e69652268d457095b7541946d638cd

See more details on using hashes here.

File details

Details for the file pergola_projecttools-2.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pergola_projecttools-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1debbe02fb71c33f268e4edd6a67b824b541f1009a6219c505b86e070c0c460f
MD5 1ba4ae9624c467908681844d16e27f5d
BLAKE2b-256 6b1d71dac02269b63ce29d692e55995a65ac098e1d3d3884d6c2f1a65580041a

See more details on using hashes here.

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