Skip to main content

Smoosh Python modules into one big one.

Project description

python-combiner

A command-line tool to compile Python files into one ginormous one, with plugin support and more.

Command-line usage

I think you can install it with pip since it's on PyPi, but no promises.

usage: python-combiner [-h] -i INPUT [-o [OUTPUT]] [--ignore-imports IGNORE_IMPORTS [IGNORE_IMPORTS ...]] [--remove-imports REMOVE_IMPORTS [REMOVE_IMPORTS ...]] [-p PRELUDE]
                       [-c DEFINE_CONSTANT DEFINE_CONSTANT] [-d DEFINE] [-m | --minify | --no-minify] [-j | --json | --no-json] [-t | --time | --no-time]
                       [--docstring | --no-docstring] [--module-hash-length MODULE_HASH_LENGTH] [--export-dictionary-mode {dict,munch,class,class_instance}]
                       [--export-names-mode {locals,static}]

Compiles/merges Python files.

options:
  -h, --help            show this help message and exit
  -i INPUT, --input INPUT
                        the input file, can be - for stdin
  -o [OUTPUT], --output [OUTPUT]
                        the output file. Defaults to stdout
  --ignore-imports IGNORE_IMPORTS [IGNORE_IMPORTS ...]
                        modules for which to ignore transforming imports for (i.e., leave them untouched)
  --remove-imports REMOVE_IMPORTS [REMOVE_IMPORTS ...]
                        modules for which to remove imports for
  -p PRELUDE, --prelude PRELUDE
                        some Python code to insert at the top of the file. must be well-formed parsable Python code
  -c DEFINE_CONSTANT DEFINE_CONSTANT, --define-constant DEFINE_CONSTANT DEFINE_CONSTANT
                        defines one compile-time constant as a string. use some name that you're sure won't collide with any in your code, i.e. __MY_CONSTANT__
  -d DEFINE, --define DEFINE
                        equivalent to defining a constant to be 1 using --define-constant.
  -m, --minify, --no-minify
                        minifies the result
  -j, --json, --no-json
                        outputs messages as json
  -t, --time, --no-time
                        puts the time at the top of the generated code. --no-time for deterministic builds (default: True)
  --docstring, --no-docstring
                        puts a generated docstring at the top of the module. added by default (default: True)
  --module-hash-length MODULE_HASH_LENGTH
                        the length of the hash used for making modules unique
  --export-dictionary-mode {dict,munch,class,class_instance}
                        the method that export dictionaries are converted to dot-accessible objects
  --export-names-mode {locals,static}
                        how module exports are determined. use 'locals' for compatibility with existing code. forced to 'static' if --export-dictionary-mode is set to 'class' or
                        'class_instance'

Library usage

import python_compiler

python_compiler.Compiler(
    source=input(),
    path="/path/to/source/file",
    options=python_compiler.CompilerOptions(
        # ... (see docstring)
        plugins=[
            # Plugins
            python_compiler.plugins.MinifyPlugin()
        ]
    )
)

For more examples, see the CLI source code for example usage. Note that path does not need to be a real path, but it's used for import resolution. The library is mostly documented using docstrings, so just read the source code for more help.

Plugins

Built-in plugins

Built-in plugins can be imported from the python-compiler.plugins module. See the docstrings for usage information.

MinifyPlugin

Uses python-minifier to minify the resulting code after bundling is performed. This can reduce the size of the resulting code by a factor of 3 or more, depending on the input.

ConstantsPlugin

Dynamically replaces variable names with content at compile-time. Similar to #DEFINEs if you're using the C preprocessor.

PreludePlugin

An easy way to add a snippet of code at the beginning of the output.

Plugin authoring

Plugins must inherit from the base Plugin class. An example plugin might go something like this:

class MyPlugin(python_compiler.plugin.Plugin):
    def hook_module(self, path, module):
        module = my_transformation(module)
        return module

There are a couple available hooks as of this writing:

  • hook_module
    A hook run before name translation is performed and modules are bundled
  • hook_module_post_transform
    A hook run after name translation is performed but before modules are bundled
  • hook_import
    A hook run on all imports a module imports
  • hook_import_resolution
    A hook run during the module resolution step. It can be used to define "virtual modules".
  • hook_output
    A hook called just prior to the end of code generation.

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

python_combiner-0.3.0.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

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

python_combiner-0.3.0-py3-none-any.whl (21.9 kB view details)

Uploaded Python 3

File details

Details for the file python_combiner-0.3.0.tar.gz.

File metadata

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

File hashes

Hashes for python_combiner-0.3.0.tar.gz
Algorithm Hash digest
SHA256 7d800553f3551d983d5818a978b65d0ba71aef0065079c1d66069b54560b06be
MD5 894f07bdf273298fd26c2111e17c4569
BLAKE2b-256 ba33307be901dbe5546b8d12240e2fa0796288613a6f285ad39e9ec9bbcbb1f2

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_combiner-0.3.0.tar.gz:

Publisher: publish.yml on zabackary/python-combiner

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

File details

Details for the file python_combiner-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_combiner-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 756f69dc45d4e976cad5b1d57fe4cdd97ca92f4c34585dd9901d852200de0f1c
MD5 4b82243e31905bf2ca0e6d49100b1df3
BLAKE2b-256 9b87291e9be1da5a1db3c6e968b6cb6ca0e938e1f02df01b0142e58ec634cfd6

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_combiner-0.3.0-py3-none-any.whl:

Publisher: publish.yml on zabackary/python-combiner

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