Skip to main content

Set up global environment and run another script within, ala pdb, profile, etc..

Project description

https://github.com/rpatterson/python-main-wrapper/workflows/Run%20linter,%20tests%20and,%20and%20release/badge.svg

Use main-wrapper either as a command-line script or as a library to make Python scripts that set up or change some global Python environment and then run another script within that environment. It seeks to combine into one library all the gloss and polish of other Python software that does this, such as using python -m with pdb and profile in the standard library or the coverage package’s command-line run command.

Installation

Install using any tool for installing standard Python 3 distributions such as pip:

$ sudo pip3 install main-wrapper

Usage

You may use this package either as a library in your code that needs to wrap another script or as a command-line script.

To use as a library, use the provided decorator to wrap your function that sets up the global environment you need the script to be run in:

import logging
import argparse

import mainwrapper

parser = argparse.ArgumentParser()
parser.add_argument(
    "--level",
    default="INFO",
    help="The level of messages to log at or above",
)

@mainwrapper.wrap_main(parser)
def main(level=parser.get_default("level")):
    """
    As an example, this function will set up logging at level INFO.
    """
    logging.basicConfig(level=getattr(logging, level))

The changes to Python’s global execution environment that support running the wrapper function and the final script are also cleaned up upon completion, so it should be possible to use this library to execute multiple scripts in the same process as if they were run independently.

See the command-line help for details the options and arguments for using this package as a command-line script:

$ usage: python-main-wrapper [-h] wrapper script

Set up global environment and run another script within, ala pdb, profile, etc..  Both
script arguments may either be a path to a Python script, a Python module or package
to be run in the same manner as Python's `-m` option, or a setuptools
`path.to.import:callable` entry-point.

positional arguments:
  wrapper     A Python script that sets up the environment
  script      The Python script to run within the wrapper's environment

optional arguments:
  -h, --help  show this help message and exit

Motivation

I found myself writing such wrapper scripts repeatedly over the years and kept struggling to remember how I did it last time. I made this package to capture all that knowledge and to have one place to put improvements as I discover them.

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

main-wrapper-0.1.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

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

main_wrapper-0.1.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file main-wrapper-0.1.0.tar.gz.

File metadata

  • Download URL: main-wrapper-0.1.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

File hashes

Hashes for main-wrapper-0.1.0.tar.gz
Algorithm Hash digest
SHA256 638a091663f83e96948288aa828403a1237771f768a98023da74f958a7e4a735
MD5 29d1beb1add6e474e8cf239152a91569
BLAKE2b-256 38214170633d2b3ddd99211d48f1b2bec864a33c69f9129ea1e4ec9dc68a55b1

See more details on using hashes here.

File details

Details for the file main_wrapper-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: main_wrapper-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

File hashes

Hashes for main_wrapper-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 84654f674521b9ccd53bc618cda33854acd05ddbd39648a5e16c56703f2906dd
MD5 dc00eaab3854b96b9d2a886bdc7fadca
BLAKE2b-256 e01c28a0bbd91b1c07cc6ea0c980c7003e45921e66766cb88ab74a6cc3c734e5

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