Skip to main content

CLI tool for generating DDD architecture in Python projects

Project description

PyConstructor ๐Ÿ—๏ธ

Python Version License

PyConstructor is a command-line tool that helps developers quickly create a project structure following Domain-Driven Design (DDD) principles. The tool generates architecture based on a YAML configuration that defines bounded contexts, entities, repositories, services, use cases, and other DDD elements.

๐Ÿš€ Quick Start

Installation

# Install via pip
pip install pyconstructor
# Install via uv
uv add pyconstructor

# Generate YAML file with example data
pyc init

# Edit the generated ddd-config.yaml file
# ...

# Generate structure
pyc run

Basic Usage

  1. Initialize a new project with a preset configuration:
pyc init --preset <PresetType(Optional argument, default to Standard)>
  1. Validate your configuration (Optional command):
pyc validate
  1. Preview the project structure (Optional command):
pyc preview --file <file_name> (Optional argument)
  1. Generate the project:
pyc run --file <file_name> (Optional argument)

๐Ÿ“‹ Available Commands

Core Commands

Command Description Example
init Initialize a new project with a preset configuration pyc init --preset standard
validate Validate your YAML configuration pyc validate --file custom-config.yaml
preview Preview the project structure without generating files pyc preview --file custom-config.yaml
run Generate the project structure pyc run --file custom-config.yaml

Command Options

init Command

# Create project with standard preset
pyc init --preset standard

# Force overwrite existing config
pyc init --preset standard --force

validate Command

# Validate default config (ddd-config.yaml)
pyc validate

# Validate specific config file
pyc validate --file custom-config.yaml

preview Command

# Preview default config
pyc preview

# Preview specific config
pyc preview --file custom-config.yaml

Output:

  • Displays the project structure tree in the console
  • Generates a structure.md file with the same tree view for future reference

Example output:

app/
โ”œโ”€โ”€ domain/
โ”‚   โ”œโ”€โ”€ user/
โ”‚   โ”‚   โ”œโ”€โ”€ entities/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ user.py
โ”‚   โ”‚   โ””โ”€โ”€ value_objects/
โ”‚   โ”‚       โ””โ”€โ”€ email.py
โ”‚   โ””โ”€โ”€ catalog/
โ”‚       โ””โ”€โ”€ entities/
โ”‚           โ””โ”€โ”€ product.py
โ”œโ”€โ”€ application/
โ”‚   โ””โ”€โ”€ user/
โ”‚       โ””โ”€โ”€ use_cases/
โ”‚           โ””โ”€โ”€ register_user.py
โ””โ”€โ”€ infrastructure/
    โ””โ”€โ”€ repositories/
        โ””โ”€โ”€ user_repository.py

run Command

# Generate from default config
pyc run

# Generate from specific config
pyc run --file custom-config.yaml

Architecture Presets

PyConstructor comes with three built-in presets:

Simple Preset

Basic DDD structure without bounded contexts:

pyc init --preset simple

Standard Preset

Default preset with bounded contexts:

pybuilder init --preset standard

Advanced Preset

Complex structure with nested contexts:

pyc init --preset advanced

Configuration

The tool uses YAML configuration files to define your project structure. Example configurations are provided in the src/templates/config_templates directory.

Configuration Reference

Settings Section

settings:
  preset: "standard"  # One of: "simple", "standard", "advanced"
  use_contexts: true  # Whether to use bounded contexts
  contexts_layout: "flat"  # One of: "flat", "nested"
  group_components: true  # Group similar components in directories
  init_imports: false  # Initialize imports in __init__.py files
  root_name: "src"  # Root directory name

Simple Configuration Example

settings:
  preset: "simple"

layers:
  domain:
    entities: User, Product
    value_objects: Email, Price

Standard Configuration Example

settings:
  preset: "standard"

layers:
  domain:
    contexts:
      - name: user
        entities: [User, Profile]
        value_objects: [Email, Password]
      - name: catalog
        entities: [Product, Category]

Advanced Configuration Example (for microservice architecture)

settings:
  preset: "advanced"

layers:
  contexts:
    - name: user_context
      domain:
        entities: User
        value_objects: Email
      application:
        use_cases: CreateUser
      infrastructure:
        repositories: UserRepository

    - name: payment_context
      domain:
        entities: Payment
      application:
        use_cases: ProcessPayment
      infrastructure:
        repositories: TransactionRepository

Complete Configuration Example

Here's a complete example showing all available options:

settings:
  preset: "advanced"
  use_contexts: true
  contexts_layout: "nested"
  group_components: true
  init_imports: true
  root_name: "src"

layers:
  contexts:
    - name: user_context
      domain:
        entities: User, Profile
        value_objects: Email, Password, UserRole
        aggregates: UserAggregate
        repositories: UserRepository
        services: UserService
      application:
        use_cases: CreateUser, UpdateUser, DeleteUser
        commands: CreateUserCommand, UpdateUserCommand
        queries: GetUserQuery, ListUsersQuery
        events: UserCreatedEvent, UserUpdatedEvent
        dtos: UserDTO, UserCreateDTO
        mappers: UserMapper
      infrastructure:
        repositories: UserRepositoryImpl
        services: UserServiceImpl
      interface:
        controllers: [UserController]
        middleware: AuthMiddleware

    - name: order_context
      domain:
        entities: Order, OrderItem
        value_objects: Money, OrderStatus
        aggregates: OrderAggregate
        repositories: OrderRepository
        services: OrderService
      application:
        use_cases: CreateOrder, UpdateOrder
        commands: CreateOrderCommand
        queries: GetOrderQuery
        events: OrderCreatedEvent
        dtos: OrderDTO
        mappers: OrderMapper
      infrastructure:
        repositories: OrderRepositoryImpl
      interface:
        controllers: OrderController

Generated Structure

When using the advanced configuration above, the tool will generate a structure like this:

src/
โ”œโ”€โ”€ user_context/
โ”‚   โ”œโ”€โ”€ domain/
โ”‚   โ”‚   โ”œโ”€โ”€ entities/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ user.py
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ profile.py
โ”‚   โ”‚   โ”œโ”€โ”€ value_objects/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ email.py
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ password.py
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ user_role.py
โ”‚   โ”‚   โ”œโ”€โ”€ aggregates/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ user_aggregate.py
โ”‚   โ”‚   โ”œโ”€โ”€ repositories/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ user_repository.py
โ”‚   โ”‚   โ””โ”€โ”€ services/
โ”‚   โ”‚       โ””โ”€โ”€ user_service.py
โ”‚   โ”œโ”€โ”€ application/
โ”‚   โ”‚   โ”œโ”€โ”€ use_cases/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ create_user.py
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ update_user.py
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ delete_user.py
โ”‚   โ”‚   โ”œโ”€โ”€ commands/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ create_user_command.py
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ update_user_command.py
โ”‚   โ”‚   โ”œโ”€โ”€ queries/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ get_user_query.py
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ list_users_query.py
โ”‚   โ”‚   โ”œโ”€โ”€ events/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ user_created_event.py
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ user_updated_event.py
โ”‚   โ”‚   โ”œโ”€โ”€ dtos/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ user_dto.py
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ user_create_dto.py
โ”‚   โ”‚   โ””โ”€โ”€ mappers/
โ”‚   โ”‚       โ””โ”€โ”€ user_mapper.py
โ”‚   โ”œโ”€โ”€ infrastructure/
โ”‚   โ”‚   โ”œโ”€โ”€ repositories/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ user_repository_impl.py
โ”‚   โ”‚   โ””โ”€โ”€ services/
โ”‚   โ”‚       โ””โ”€โ”€ user_service_impl.py
โ”‚   โ””โ”€โ”€ interface/
โ”‚       โ”œโ”€โ”€ controllers/
โ”‚       โ”‚   โ””โ”€โ”€ user_controller.py
โ”‚       โ””โ”€โ”€ middleware/
โ”‚           โ””โ”€โ”€ auth_middleware.py
โ””โ”€โ”€ order_context/
    โ””โ”€โ”€ ... (similar structure)

Customizing Templates

You can customize the generated files by modifying the templates in the src/templates directory. Each component type has its own template file that you can modify to suit your needs.

General Questions

Q: Which preset should I choose? A: Start with the "simple" preset for small projects, "standard" for medium-sized applications, and "advanced" for complex microservices.

Q: Can I modify the generated structure after creation? A: Yes, you can modify the configuration and regenerate the structure, but be careful with existing files.

Q: How do I handle shared components between contexts? A: Create a separate "shared" context for common components, or use the infrastructure layer for shared implementations.

Configuration Questions

Q: What's the difference between flat and nested contexts? A: Flat contexts are organized by layers first, then contexts. Nested contexts are organized by contexts first, then layers.

Q: How do I add new component types? A: You can extend the configuration schema and add new templates in the src/templates directory.

Q: Can I customize the generated file templates? A: Yes, all templates are located in the src/templates directory and can be modified to match your needs.

Technical Questions

Q: How do I handle dependencies between contexts? A: Use interfaces in the domain layer and implementations in the infrastructure layer to maintain loose coupling.

Q: What's the recommended way to handle database access? A: Use repositories in the domain layer for interfaces and implement them in the infrastructure layer.

Q: How do I implement event handling between contexts? A: Define events in the domain layer and implement handlers in the application layer.

๐Ÿค Contributing

Contributions are welcome. Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git switch -c feature/amazing-feature)
  3. Run tests (pytest)
  4. Commit your changes (git commit -m 'Add amazing feature')
  5. Push to the branch (git push origin feature/amazing-feature)
  6. Open a Pull Request

๐Ÿ“„ License

This project is licensed under the MIT Licenseโ€”see the LICENSE file for details.

๐Ÿ‘ค Author

Grigoriy Sokolov (Sokolov_Gr@proton.me)

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

pyconstructor-0.2.1.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

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

pyconstructor-0.2.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file pyconstructor-0.2.1.tar.gz.

File metadata

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

File hashes

Hashes for pyconstructor-0.2.1.tar.gz
Algorithm Hash digest
SHA256 cdd4ec659a24dc9a67d89e4bc12357b743fc58b1d1ec1ff0ec81b9b142a10869
MD5 ee382a5926a076ce1744a6215022f4e7
BLAKE2b-256 b0f301553b3129f04a262cd72d646d9d8fcaf3f8acc7a4d248ac291bb2517eb3

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyconstructor-0.2.1.tar.gz:

Publisher: publish.yaml on SokolovG/PyConstruct

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

File details

Details for the file pyconstructor-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: pyconstructor-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pyconstructor-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f50ce73354509e8628121a761c2fcf8657fdf53b6379ebc7f9e3bfcb679b7deb
MD5 a1a1c86b974aae95b403142ce58c4c22
BLAKE2b-256 d02d0cec875bfc47fc06db433f1003820d2d464916b22a3152e417001bee4969

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyconstructor-0.2.1-py3-none-any.whl:

Publisher: publish.yaml on SokolovG/PyConstruct

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