Skip to main content

Secondary development platform base

Project description

tspkg_base

Secondary development platform base for Python applications.

Provides AOP, Hook, and Rewrite capabilities, allowing external business packages to dynamically take over, rewrite, or enhance the execution logic of original methods without modifying the original project code.

Installation

pip install tspkg_base
# or
pip install tspkg_base==0.1.0 -i https://pypi.org/simple

Usage

Your Project

Example code

  • bootstrap code
    • This code is necessary because it loads the tspkg package.
from tspkg_base.runtime.bootstrap import bootstrap

result = bootstrap(tspkg_dir=tspkg_dir)
logger.info(f"Bootstrap result: {result['loaded']} loaded, {result['failed']} failed")
  • entry code
from tspkg_base import aop_entry


# Usage example
@aop_entry(key="order.create")
def create_order(user_id: int, product_id: int, quantity: int = 1) -> dict:
    """
    Create order (original method)

    This method is decorated with @aop_entry and can be taken over or enhanced
    by rewrite methods in secondary development packages.
    """
    logger.info(f"Creating order: user_id={user_id}, product_id={product_id}, quantity={quantity}")

    # Simulate order creation logic
    order_id = 10000 + user_id  # Simple order ID generation

    result = {
        "order_id": order_id,
        "user_id": user_id,
        "product_id": product_id,
        "quantity": quantity,
        "status": "created",
        "created_by": "original_code"
    }

    logger.info(f"Order created: {result}")
    return result

Secondary Development Project

Project's directory

your_business_pkg/
├── code/                    # required:Python source code
│   ├── __init__.py
│   └── your_module.py      # your rewrite code
└── ...                      # other[optional]

Example code

from tspkg_base.aop.decorators import rewrite
from tspkg_base.aop.context import ExecutionContext


@rewrite(target="order.create", mode="around")
def my_create_order(ctx: ExecutionContext):
    """
    Rewrite order creation logic

    This rewrite method will completely take over the execution of the original method.
    """
    # Get original method arguments
    args = ctx.get_args()
    kwargs = ctx.get_kwargs()

    user_id = args[0] if len(args) > 0 else kwargs.get('user_id')
    product_id = args[1] if len(args) > 1 else kwargs.get('product_id')
    quantity = args[2] if len(args) > 2 else kwargs.get('quantity', 1)

    # You can do some preprocessing here
    # For example: parameter validation, permission checks, etc.
    if user_id <= 0:
        return {
            "error": "Invalid user_id",
            "user_id": user_id
        }

    # Call original method
    original_result = ctx.call_original()

    # Modify result
    original_result["modified_by_rewrite"] = True
    original_result["rewrite_message"] = "This order was created with rewrite logic"
    original_result["rewrite_version"] = "1.0.0"

    # You can add additional business logic
    # For example: send notifications, log records, etc.

    return original_result

Pack Secondary Development Project

  • Please zip your project directory and rename the file extension from zip to tspkg.
  • Note that the directory compressed using zip software is not encrypted, but it can meet most scenarios.
  • This software supports encryption keys. If you need further assistance, please contact the author

UnPack Secondary Development Project

  • It should be noted that if you have specified the tspkg directory in your program, simply place tspkg in this directory. In short, they must be consistent.
# The directory parameter for loading tspkg is specified by tspkg_dir

result = bootstrap(tspkg_dir=tspkg_dir)

Features

  • AOP (Aspect-Oriented Programming) support
  • Hook mechanism
  • Method rewriting (Rewrite) capability
  • Dynamic code injection

License

MIT License

FAQ

Q: How to know the signature of the original method?

A: You need to confirm with the original project development team, or check the original project documentation. The rewrite code should be compatible with the original method signature.

Q: Can rewrite call other modules?

A: Yes, but please note:

  • Ensure that dependent modules are available in the original project
  • Avoid importing internal modules of the original project (may cause version conflicts)
  • It is recommended to only use standard library and known third-party libraries

Q: Will multiple rewrites conflict?

A: Each target can only have one rewrite. If multiple TSPKG packages contain the same target, the later loaded one will override the earlier one.

Q: How to update rewrite?

A: Repackage the TSPKG file, replace the original file, and then restart the original project.

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

tspkg_base-0.1.1.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

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

tspkg_base-0.1.1-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file tspkg_base-0.1.1.tar.gz.

File metadata

  • Download URL: tspkg_base-0.1.1.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for tspkg_base-0.1.1.tar.gz
Algorithm Hash digest
SHA256 97a0e096c2a3d41f2e3ada03d0092728ce9228d9a77f4b2b6dce2c3cb3b3c617
MD5 f467fb44840b6ce5b722c0a16714fb99
BLAKE2b-256 fe9a310dd05a3a8d5a98f65f827ff3bd8939397a2dd432e0465a49e301d459d6

See more details on using hashes here.

File details

Details for the file tspkg_base-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: tspkg_base-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for tspkg_base-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 69732ed2e1ec4a24caf377add7404c82bae7cd25a95e17bb61a6e2644b346331
MD5 8e5305016e25fc6dbedf32870333c647
BLAKE2b-256 1b5d8b213b52e8046cf6ccefcc8dab24644def3713bc904ff26f388b6af11d5d

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