Skip to main content

Generate source code from Code2Logic output files

Project description

Logic2Code - Code Generation from Logic Files

PyPI version Python 3.9+ License: Apache 2.0 Code style: black

Generate source code from Code2Logic output files.

Reads YAML, Hybrid YAML, or TOON format files and produces working code scaffolds.

✨ Features

  • 🏗️ Code scaffolds - Complete class and function structures
  • 📝 Type hints - Full type annotation support
  • 📚 Docstrings - Auto-generated documentation
  • 🤖 LLM integration - Optional AI-powered implementations
  • 🔄 Refactoring support - Logic → Code → Logic workflow

🚀 Installation

pip install logic2code

Or with LLM support:

pip install logic2code[llm]  # With lolm for LLM generation

📖 Quick Start

CLI

# Show what can be generated
logic2code project.c2l.yaml --summary

# Generate Python code
logic2code project.c2l.yaml -o generated_src/

# Generate stubs only
logic2code project.c2l.yaml -o src/ --stubs-only

# Generate specific modules
logic2code project.c2l.yaml -o src/ --modules "analyzer.py,parsers.py"

Python API

from logic2code import CodeGenerator

# Create generator
generator = CodeGenerator('project.c2l.yaml')

# Get summary
summary = generator.summary()
print(f"Modules: {summary['total_modules']}")
print(f"Classes: {summary['total_classes']}")

# Generate code
result = generator.generate('output/')
print(f"Generated {result.files_generated} files")

# Generate single module
code = generator.generate_module('analyzer.py')
print(code)

📋 Generated Code Example

@dataclass
class ProjectInfo:
    """Information about analyzed project."""
    name: str
    total_files: int = 0
    total_lines: int = 0
    modules: List[ModuleInfo] = field(default_factory=list)


async def analyze_project(
    path: str,
    use_treesitter: bool = True,
    verbose: bool = False
) -> ProjectInfo:
    """Analyzes project and returns ProjectInfo."""
    raise NotImplementedError("TODO: Implement analyze_project")

🤖 LLM-Enhanced Generation

from logic2code import CodeGenerator, GeneratorConfig

config = GeneratorConfig(
    use_llm=True,
    llm_provider='openrouter'
)

generator = CodeGenerator('project.c2l.yaml', config)
result = generator.generate('output/')

🖥️ CLI Reference

Option Description
-o, --output DIR Output directory
-l, --language LANG Target language (default: python)
--stubs-only Generate stubs only
--no-docstrings Skip docstring generation
--no-type-hints Skip type hints
--no-init Skip __init__.py generation
--flat Flat output structure
--modules LIST Comma-separated modules to generate
--summary Show summary without generating
-v, --verbose Verbose output

⚙️ Configuration

from logic2code import CodeGenerator, GeneratorConfig

config = GeneratorConfig(
    language='python',
    stubs_only=False,
    include_docstrings=True,
    include_type_hints=True,
    generate_init=True,
    preserve_structure=True,
    use_llm=False,
    llm_provider=None,
)

generator = CodeGenerator('project.c2l.yaml', config)

🔄 Refactoring Workflow

# 1. Analyze original code
code2logic src/ -f yaml -o project.c2l.yaml

# 2. Modify logic file (add/change functions)
# Edit project.c2l.yaml

# 3. Generate new code
logic2code project.c2l.yaml -o new_src/

# 4. Compare and merge
diff -r src/ new_src/

🧰 Monorepo (code2logic) workflow

If you use logic2code inside the code2logic monorepo, you can manage all packages from the repository root:

make test-all
make build-subpackages
make publish-all

See: docs/19-monorepo-workflow.md.

🧪 Development

# Install dev dependencies
make install-dev

# Run tests
make test

# Format code
make format

# Lint
make lint

# Build package
make build

# Publish to PyPI
make publish

📄 License

Apache 2.0 License - see LICENSE for details.

🔗 Links

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

logic2code-0.1.2.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

logic2code-0.1.2-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file logic2code-0.1.2.tar.gz.

File metadata

  • Download URL: logic2code-0.1.2.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for logic2code-0.1.2.tar.gz
Algorithm Hash digest
SHA256 67be7f8182f0a4394472c1badef1e292b99b47b773e24622ca90303a65dd8ed5
MD5 3dee06127b98fdea9dac84bbd38e5db5
BLAKE2b-256 662119c533bca50d7d8d966ad6bf9f6a24bff00f8b178ea0bf1578e74247c12c

See more details on using hashes here.

File details

Details for the file logic2code-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: logic2code-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for logic2code-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8ef8c5c89c60fac13c5635d2247849a072a3e3d17afade0adfdafefc9d25605e
MD5 c231a5e573d6ab33c09debec74575d67
BLAKE2b-256 53ce1278d07c1b3a190c07f0c1553574ec49ad365eb1b83a2eca9f1edaf6adf0

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