This repository was archived by the owner on Jan 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo.py
More file actions
288 lines (228 loc) · 8.62 KB
/
demo.py
File metadata and controls
288 lines (228 loc) · 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python3
"""
Demo script showcasing the platform's three services.
This avoids import conflicts by demonstrating core functionality directly.
"""
import sys
import json
import tempfile
from pathlib import Path
# Add the src directory to Python path
sys.path.insert(0, str(Path(__file__).parent / "src" / "HandleGeneric v2" / "src"))
def demo_s2_metadata():
"""Demo S2 - Metadata extraction service."""
print("🔍 S2 - Metadata Extraction Demo")
print("=" * 40)
# Import the metadata provider
from platform.adapters.providers.python.metadata_provider import (
PythonMetadataProvider,
)
from platform.adapters.fs.local_fs import LocalFileSystem
# Sample Python code
sample_code = '''
"""A sample calculator module."""
import math
from typing import Union
class Calculator:
"""A simple calculator with basic operations."""
def __init__(self):
"""Initialize the calculator."""
self.history = []
def add(self, a: float, b: float) -> float:
"""Add two numbers."""
result = a + b
self.history.append(f"{a} + {b} = {result}")
return result
def multiply(self, a: float, b: float) -> float:
"""Multiply two numbers."""
result = a * b
self.history.append(f"{a} * {b} = {result}")
return result
def power(self, base: float, exponent: float) -> float:
"""Raise base to the power of exponent."""
result = math.pow(base, exponent)
self.history.append(f"{base} ** {exponent} = {result}")
return result
def validate_input(value: Union[int, float]) -> bool:
"""Validate numeric input."""
return isinstance(value, (int, float)) and not math.isnan(value)
def format_result(result: float) -> str:
"""Format calculation result."""
return f"{result:.2f}"
'''
# Extract metadata
provider = PythonMetadataProvider()
metadata = provider.parse_file(Path("calculator.py"), sample_code)
print(f"📄 File: {metadata.path}")
print(f"🔤 Language: {metadata.language}")
print(f"📊 Lines of Code: {metadata.loc}")
print(f"🔧 Functions: {len(metadata.functions)}")
for func in metadata.functions:
print(f" - {func}")
print(f"🏗️ Classes: {len(metadata.classes)}")
for cls in metadata.classes:
print(f" - {cls}")
print(f"📦 Imports: {len(metadata.imports)}")
for imp in metadata.imports[:3]: # Show first 3
print(f" - {imp}")
print("\n✅ Metadata extraction completed!\n")
return metadata
def demo_s3_validation():
"""Demo S3 - Validation service."""
print("🔍 S3 - Validation Demo")
print("=" * 40)
from platform.adapters.providers.python.syntax_validator import (
PythonSyntaxValidator,
)
validator = PythonSyntaxValidator()
# Test valid code
valid_code = """
def hello_world():
print("Hello, World!")
return True
"""
print("Testing valid Python code:")
result = validator.validate(Path("valid.py"), valid_code)
print(f"✅ Status: {result.status}")
print(f"📝 Issues: {len(result.issues)}")
# Test invalid code
invalid_code = """
def broken_function(
print("Missing closing parenthesis")
return False
"""
print("\nTesting invalid Python code:")
result = validator.validate(Path("invalid.py"), invalid_code)
print(f"❌ Status: {result.status}")
print(f"📝 Issues: {len(result.issues)}")
for issue in result.issues:
print(f" Line {issue.line}: {issue.message}")
print("\n✅ Syntax validation completed!\n")
def demo_s1_generation():
"""Demo S1 - Code generation service (prompt building)."""
print("🚀 S1 - Code Generation Demo")
print("=" * 40)
from platform.domain.models.requirements import Requirement
from platform.adapters.providers.python.codegen_provider import (
PythonCodeGenProvider,
)
# Create a sample requirement
requirement = Requirement(
id="CALC-001",
title="Basic Calculator Function",
description="Create a function that adds two numbers with error handling",
acceptance=[
"Must accept two numeric parameters",
"Must return the sum of the parameters",
"Must handle invalid inputs gracefully",
"Must include type hints and docstring",
],
)
# Generate prompt
provider = PythonCodeGenProvider()
context = {
"use_type_hints": True,
"test_framework": "pytest",
"style_guide": "PEP 8",
}
prompt = provider.build_prompt(requirement, context)
print(f"📋 Requirement: {requirement.title}")
print(f"📝 Description: {requirement.description}")
print(f"✅ Acceptance Criteria:")
for i, criteria in enumerate(requirement.acceptance, 1):
print(f" {i}. {criteria}")
print("\n🤖 Generated Prompt Preview:")
print("-" * 50)
# Show first 500 characters of prompt
print(prompt[:500] + "..." if len(prompt) > 500 else prompt)
print("-" * 50)
print("\n✅ Code generation prompt ready!")
print("💡 With an OpenAI API key, this would generate actual Python code.\n")
def demo_file_operations():
"""Demo file system operations."""
print("📁 File System Operations Demo")
print("=" * 40)
from platform.adapters.fs.local_fs import LocalFileSystem
from platform.adapters.fs.artifact_writer import LocalArtifactWriter
from platform.domain.models.generation import GeneratedFile
fs = LocalFileSystem()
writer = LocalArtifactWriter()
# Demo with temporary directory
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
# Write a test file
test_content = '''# Generated Calculator
def add(a, b):
"""Add two numbers."""
return a + b
def multiply(a, b):
"""Multiply two numbers."""
return a * b
'''
test_file = temp_path / "calculator.py"
fs.write_text(test_file, test_content)
# Read it back
read_content = fs.read_text(test_file)
print(f"✅ File write/read successful: {len(read_content)} characters")
# Scan directory
files = fs.scan(temp_path)
print(f"📂 Files found: {len(files)}")
for file in files:
print(f" - {file.name}")
# Demo artifact writer
generated_files = [
GeneratedFile(
path="main.py", content='print("Hello from main!")', language="python"
),
GeneratedFile(
path="utils.py", content="def utility(): pass", language="python"
),
]
output_dir = temp_path / "generated"
writer.write(output_dir, generated_files)
# Verify generated files
generated_files_found = list(output_dir.rglob("*.py"))
print(f"🎯 Generated files: {len(generated_files_found)}")
for file in generated_files_found:
print(f" - {file.name}")
print("\n✅ File operations completed!\n")
def main():
"""Run the full platform demo."""
print("🏗️ Enterprise Code Generation Platform Demo")
print("=" * 60)
print("🎯 Demonstrating three first-class services:")
print(" S1 - Code Generation from Requirements")
print(" S2 - Metadata Generation from Code")
print(" S3 - Validation (syntax → tests → AI logic)")
print("=" * 60)
print()
# Run demos
try:
demo_s2_metadata()
demo_s3_validation()
demo_s1_generation()
demo_file_operations()
print("🎉 Platform Demo Completed Successfully!")
print("\n📚 What you've seen:")
print("✅ Metadata extraction from Python code using AST parsing")
print("✅ Syntax validation with detailed error reporting")
print("✅ Prompt generation for AI-powered code creation")
print("✅ File system operations for artifact management")
print("\n🔧 To use the full platform:")
print("1. Install dependencies: pip install openai pydantic typer rich")
print("2. Set up .env file with your OpenAI API key")
print("3. Use the CLI for full functionality")
print("\n🚀 The platform is ready for:")
print(" - Multi-language support (Python, TypeScript, Java)")
print(" - AI-powered code generation")
print(" - Comprehensive validation pipelines")
print(" - Enterprise-grade observability")
except Exception as e:
print(f"❌ Demo failed: {e}")
import traceback
traceback.print_exc()
return False
return True
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)