Skip to main content

A Python tool to extract and organize custom code for HubSpot customcode workflows.

Project description

Python Import Extractor and Code Processor

This Python script is designed to address a specific challenge when working with custom code in HubSpot workflows. HubSpot does not allow the use of custom libraries or imports in its customs codes, forcing developers to duplicate code across multiple customs codes. This script helps streamline this process by extracting and organizing reusable code snippets, making it easier to manage and maintain custom logic across customs codes.

Beyond being a simple library, this project proposes a development and code organization strategy tailored for HubSpot customs codes. It ensures modularity, reusability, and maintainability by structuring code into specific directories and enforcing import conventions.


Table of Contents


Features

  • Import Extraction: Recursively extracts imports and reusable functions from Python files, including nested imports.
  • Code Classification: Classifies code into standard libraries, constants, and custom libraries for better organization.
  • Code Deduplication: Helps avoid code duplication by extracting reusable functions and constants into a centralized structure.
  • HubSpot customs codes Compatibility: Generates clean, self-contained code snippets that can be directly used in HubSpot customs codes.
  • Development Organization: Provides a clear directory structure and import conventions for scalable development.
  • Path Handling: Uses pathlib and os for cross-platform file and directory management.
  • Logging: Provides detailed logging for debugging and tracking operations.
  • Customizable Depth: Allows setting a maximum depth for recursive import analysis.

Why This Script?

When working with HubSpot customs codes, developers often face the limitation of not being able to import custom libraries or shared code. This leads to:

  • Code Duplication: The same functions or constants are copied across multiple customs codes, making maintenance difficult.
  • Error-Prone Updates: Changes to shared logic require manual updates in every custom code, increasing the risk of inconsistencies.
  • Lack of Modularity: Without the ability to import libraries, customs codes become cluttered and harder to manage.

This script solves these issues by:

  1. Extracting reusable code (functions, constants, etc.) from your custom libraries.
  2. Organizing the extracted code into a structured format.
  3. Generating clean, self-contained code snippets that can be directly used in HubSpot customs codes.

Development Organization

The code is organized into the following directories:

project-root/
│
├── customcode/ # Main programs to be analyzed
│ ├── workflow1.py
│ ├── workflow2.py
│ └── ...
│
├── lib/ # Custom function libraries
│ ├── utils.py
│ ├── helpers.py
│ └── ...
│
├── constants/ # HubSpot environment constants
│ ├── pipelines.py # Example: Pipeline IDs and names
│ ├── stages.py # Example: Stage IDs and names
│ ├── properties.py # Example: Custom property names
│ └── ...
│
└── generated/ # Results of transformations
├── workflow1.py
├── workflow2.py
└── ...

Directory Roles

  • customcode/: Contains the main programs (e.g., HubSpot customs codes) that will be analyzed. These files are the entry points for the script.
  • lib/: Stores reusable functions and utilities. These are shared across multiple custom codes to avoid duplication.
  • constants/: Contains environment-specific constants, such as HubSpot pipeline IDs, stage IDs, or custom property names. These are centralized to ensure consistency.
  • generated/: Stores the output files after processing. These files are self-contained and ready to be used in HubSpot custom code.

Import Conventions

To ensure clean and modular code, the following import conventions are enforced:

  1. Use from ... import:
  • Always use from mylib import function_name instead of import mylib.
  • This avoids the need to prefix function calls with mylib.function_name(), making the code cleaner and more readable.

Example:

# Good
from lib.utils import format_date, calculate_discount

# Bad
import lib.utils
  1. Avoid Package References:

By using from ... import, you ensure that the generated code does not rely on package structures.

  1. Centralize Constants:

Import constants from the constants/ directory to ensure consistency across customs codes.

  1. Examples:
from constants.pipelines import SALES_PIPELINE_ID
from constants.stages import QUALIFIED_LEAD_STAGE_ID

Command Line Usage (CLI)

The program can be used via the hsprocessor command (defined by hubspot_py_preprocessor.cli:main).

Installation

pip install hubspot-py-preprocessor

Usage

hsprocessor /path/to/your/code /path/to/output/dir --max_depth 5

See https://gitlab.com/dbm-01/hubspot-py-preprocessor-examples.git for usage examples

  • customcode/: Contains the main programs (e.g., HubSpot customs codes) that will be analyzed. These files are the entry points for the script.

  • /path/to/your/project (required): The root directory where your Python files are located. The script will recursively scan all .py files in this folder.

  • /path/to/output/dir (required): The directory where the processed (inlined) Python files will be saved.

  • --max_depth 5 (optional): Sets the maximum recursion depth for analyzing imports. By default, it is 10.

If you omit the --max_depth parameter, the script defaults to 10. Here’s a summary of the main arguments:

  • input_directory ( str): The path to the project folder containing your Python files.
  • output_directory (**str`): The path to the folder where processed files should be saved.
  • --max_depth ( int, default= 10): Specifies how many levels of nested imports to analyze before stopping.

When you run the command, the script will:

  1. Recursively traverse the specified input_directory.
  2. Identify and inline any custom imports within each Python file according to the rules set by the preprocessor.
  3. Generate new self-contained Python files in the output_directory.

Use the --help flag to see a quick reference of all available arguments.

License

MIT License

Copyright (c) [2005] [David BRAHIM]


Contact

Open an issue to get help or suggest improvements.

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

hubspot_py_preprocessor-0.3.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

hubspot_py_preprocessor-0.3.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hubspot_py_preprocessor-0.3.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for hubspot_py_preprocessor-0.3.0.tar.gz
Algorithm Hash digest
SHA256 5eb3a8479bdc95bb4ed16fd8ac2ff299cfbca50908638ce930a12ef68f90f9d6
MD5 d7612d4f073160383a3a1c0ed58d2742
BLAKE2b-256 dcd3b9eb3120ee06aad0cff3d1794553114061edfded280f0737db58dae31688

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hubspot_py_preprocessor-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for hubspot_py_preprocessor-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3a92090a2421fa2e678ec6c376fe9dd0b5c03bfbe1e5b814bd57f364ae5600e7
MD5 bca4a68957bff11b4de20eabebbcb346
BLAKE2b-256 4d26b19a301a402c2ebb8f98d7b8d0aa2874ceaf947b5da04af57fe676094171

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