跳转到主要内容

设置全局环境并在其中运行另一个脚本,例如pdb、profile等。

项目描述

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

使用main-wrapper作为命令行脚本或库,以便编写Python脚本,这些脚本设置或更改某些全局Python环境,然后在其中运行另一个脚本。它试图将其他Python软件的所有华丽和光鲜之处结合到一个库中,例如使用标准库中的python -mpdbprofile,或者使用coverage包的命令行run命令。

安装

使用安装标准Python 3分发的任何工具进行安装,例如pip

$ sudo pip3 install main-wrapper

用法

您可以将此包用作代码中需要包装另一个脚本的库,或作为命令行脚本。

要将它用作库,请使用提供的装饰器来包装您需要脚本在其中运行的全球环境设置函数

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))

支持运行包装函数和最终脚本的Python全局执行环境的更改也将在完成后清理,因此可以使用此库在同一个进程中执行多个脚本,就像它们独立运行一样。

有关使用此包作为命令行脚本的选项和参数的详细信息,请参阅命令行帮助。

$ 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

请注意,此包在内部使用argparse.ArgumentParser.parse_known_args,因此请确保使用其支持--约定来分隔传递给包装脚本的参数和选项。

$ python-main-wrapper site site:_script --help
...
site.py [--user-base] [--user-site]

Without arguments print some useful information
With arguments print the value of USER_BASE and/or USER_SITE separated
by ':'.

Exit codes with --user-base or --user-site:
  0 - user site directory is enabled
  1 - user site directory is disabled by user
  2 - user site directory is disabled by super user
      or for security reasons
 >2 - unknown error

动机

多年来,我反复编写此类包装脚本,并且一直在努力记住上次是如何做的。我制作了这个包来收集所有这些知识,并且当我发现改进时,有一个地方可以放置它们。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

main-wrapper-0.1.1.tar.gz (8.7 kB 查看哈希值)

上传时间:

构建分布

main_wrapper-0.1.1-py3-none-any.whl (7.8 kB 查看哈希值)

上传时间: Python 3

支持

AWSAWS云计算和安全赞助商DatadogDatadog监控FastlyFastlyCDNGoogleGoogle下载分析MicrosoftMicrosoftPSF赞助商PingdomPingdom监控SentrySentry错误日志StatusPageStatusPage状态页面