-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyWebforge.tsx
More file actions
743 lines (612 loc) · 26.1 KB
/
PyWebforge.tsx
File metadata and controls
743 lines (612 loc) · 26.1 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
#PyWebForge Ultimate
#Created by Ruvan Swanepoel (MrShagnasty)
import os
import ast
import json
import traceback
import subprocess
import sys
import importlib
import inspect
from datetime import datetime
from typing import Dict, List, Any, Optional, Tuple
import logging
from pathlib import Path
import re
import shutil
import tempfile
# ================== AI FRAMEWORK CORE ==================
class PyWebForgeAI:
"""
Intelligent AI Framework for PyWebForge Ultimate
Provides code analysis, self-repair, and intelligent project management
"""
def __init__(self):
self.creator = "Ruvan Swanepoel (MrShagnasty)"
self.version = "2.0"
self.knowledge_base = {}
self.error_patterns = {}
self.repair_strategies = {}
self.module_cache = {}
self.setup_logging()
def setup_logging(self):
"""Configure intelligent logging system"""
os.makedirs('logs', exist_ok=True)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - PyWebForgeAI - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('logs/ai_framework.log'),
logging.StreamHandler()
]
)
self.logger = logging.getLogger('PyWebForgeAI')
# ======== CODE ANALYSIS & UNDERSTANDING ========
def analyze_project_structure(self, project_path: str) -> Dict:
"""
Deep analysis of project structure and dependencies
Returns comprehensive project map
"""
analysis = {
'modules': [],
'dependencies': set(),
'entry_points': [],
'api_endpoints': [],
'database_models': [],
'frontend_components': [],
'missing_files': [],
'errors': [],
'suggestions': []
}
try:
# Scan for Python modules
for root, dirs, files in os.walk(project_path):
for file in files:
if file.endswith('.py'):
filepath = os.path.join(root, file)
module_info = self.analyze_module(filepath)
analysis['modules'].append(module_info)
analysis['dependencies'].update(module_info.get('imports', []))
# Detect missing critical files
critical_files = ['requirements.txt', 'config.py', 'database.py']
for cf in critical_files:
if not os.path.exists(os.path.join(project_path, cf)):
analysis['missing_files'].append(cf)
analysis['suggestions'].append(f"Generate {cf} for complete setup")
# Identify entry points
if os.path.exists(os.path.join(project_path, 'app.py')):
analysis['entry_points'].append('app.py')
if os.path.exists(os.path.join(project_path, 'main.py')):
analysis['entry_points'].append('main.py')
except Exception as e:
analysis['errors'].append(f"Analysis error: {str(e)}")
self.logger.error(f"Project analysis failed: {e}")
return analysis
def analyze_module(self, filepath: str) -> Dict:
"""
Analyze individual Python module for functions, classes, and issues
"""
module_info = {
'path': filepath,
'name': os.path.basename(filepath),
'functions': [],
'classes': [],
'imports': [],
'issues': [],
'complexity': 0,
'lines': 0
}
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
module_info['lines'] = len(content.splitlines())
# Parse AST
tree = ast.parse(content)
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
func_info = {
'name': node.name,
'args': [arg.arg for arg in node.args.args],
'docstring': ast.get_docstring(node),
'complexity': self.calculate_complexity(node)
}
module_info['functions'].append(func_info)
elif isinstance(node, ast.ClassDef):
class_info = {
'name': node.name,
'methods': [],
'docstring': ast.get_docstring(node)
}
for item in node.body:
if isinstance(item, ast.FunctionDef):
class_info['methods'].append(item.name)
module_info['classes'].append(class_info)
elif isinstance(node, ast.Import):
for alias in node.names:
module_info['imports'].append(alias.name)
elif isinstance(node, ast.ImportFrom):
if node.module:
module_info['imports'].append(node.module)
except SyntaxError as e:
module_info['issues'].append(f"Syntax error: {e}")
self.attempt_repair(filepath, 'syntax_error', str(e))
except Exception as e:
module_info['issues'].append(f"Analysis error: {e}")
return module_info
def calculate_complexity(self, node: ast.AST) -> int:
"""Calculate cyclomatic complexity of a function"""
complexity = 1
for child in ast.walk(node):
if isinstance(child, (ast.If, ast.While, ast.For, ast.ExceptHandler)):
complexity += 1
return complexity
# ======== SELF-REPAIR CAPABILITIES ========
def attempt_repair(self, filepath: str, error_type: str, error_msg: str) -> bool:
"""
Attempt to automatically repair code issues
Returns True if repair successful
"""
self.logger.info(f"Attempting repair for {filepath} - {error_type}")
repair_strategies = {
'syntax_error': self.repair_syntax_error,
'import_error': self.repair_import_error,
'indentation_error': self.repair_indentation,
'missing_dependency': self.install_missing_dependency,
'encoding_error': self.repair_encoding,
'type_error': self.repair_type_error
}
if error_type in repair_strategies:
return repair_strategies[error_type](filepath, error_msg)
return False
def repair_syntax_error(self, filepath: str, error_msg: str) -> bool:
"""Repair common syntax errors"""
try:
with open(filepath, 'r', encoding='utf-8') as f:
lines = f.readlines()
# Common fixes
fixed = False
for i, line in enumerate(lines):
# Fix missing colons
if 'expected ":"' in error_msg:
if line.strip().startswith(('def ', 'class ', 'if ', 'for ', 'while ', 'try', 'except')):
if not line.rstrip().endswith(':'):
lines[i] = line.rstrip() + ':\n'
fixed = True
# Fix unclosed brackets
if 'unexpected EOF' in error_msg:
open_brackets = line.count('(') + line.count('[') + line.count('{')
close_brackets = line.count(')') + line.count(']') + line.count('}')
if open_brackets > close_brackets:
lines[i] = line.rstrip() + ')' * (open_brackets - close_brackets) + '\n'
fixed = True
if fixed:
# Backup original
shutil.copy(filepath, filepath + '.backup')
# Write fixed version
with open(filepath, 'w', encoding='utf-8') as f:
f.writelines(lines)
self.logger.info(f"Successfully repaired syntax in {filepath}")
return True
except Exception as e:
self.logger.error(f"Repair failed: {e}")
return False
def repair_import_error(self, filepath: str, error_msg: str) -> bool:
"""Fix import errors by installing or adjusting imports"""
try:
# Extract module name from error
match = re.search(r"No module named '(\w+)'", error_msg)
if match:
module_name = match.group(1)
return self.install_missing_dependency(module_name, error_msg)
except Exception as e:
self.logger.error(f"Import repair failed: {e}")
return False
def install_missing_dependency(self, module_name: str, error_msg: str) -> bool:
"""Automatically install missing Python packages"""
try:
# Map common module names to package names
package_map = {
'cv2': 'opencv-python',
'PIL': 'Pillow',
'sklearn': 'scikit-learn',
'dotenv': 'python-dotenv'
}
package = package_map.get(module_name, module_name)
self.logger.info(f"Installing missing package: {package}")
subprocess.check_call([sys.executable, '-m', 'pip', 'install', package])
# Update requirements.txt
with open('requirements.txt', 'a') as f:
f.write(f"\n{package}")
return True
except Exception as e:
self.logger.error(f"Failed to install {module_name}: {e}")
return False
def repair_indentation(self, filepath: str, error_msg: str) -> bool:
"""Fix indentation errors"""
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
# Fix mixed tabs and spaces
lines = content.splitlines()
fixed_lines = []
for line in lines:
# Convert tabs to 4 spaces
fixed_line = line.replace('\t', ' ')
fixed_lines.append(fixed_line)
# Write fixed content
with open(filepath, 'w', encoding='utf-8') as f:
f.write('\n'.join(fixed_lines))
self.logger.info(f"Fixed indentation in {filepath}")
return True
except Exception as e:
self.logger.error(f"Indentation repair failed: {e}")
return False
# ======== INTELLIGENT CODE GENERATION ========
def generate_missing_components(self, project_analysis: Dict) -> Dict[str, str]:
"""
Generate missing but required components
Returns dict of filename: content
"""
generated = {}
# Generate missing requirements.txt
if 'requirements.txt' in project_analysis['missing_files']:
deps = project_analysis['dependencies']
generated['requirements.txt'] = self.generate_requirements(deps)
# Generate missing config
if 'config.py' in project_analysis['missing_files']:
generated['config.py'] = self.generate_config()
# Generate missing database models
if 'database.py' in project_analysis['missing_files']:
generated['database.py'] = self.generate_database_models()
# Generate API wrapper if modules exist but no app.py
if not project_analysis['entry_points'] and project_analysis['modules']:
generated['app.py'] = self.generate_flask_app(project_analysis['modules'])
return generated
def generate_requirements(self, dependencies: set) -> str:
"""Generate requirements.txt from detected imports"""
# Map import names to pip packages
package_map = {
'flask': 'Flask==3.0.0',
'flask_cors': 'Flask-CORS==4.0.0',
'flask_sqlalchemy': 'Flask-SQLAlchemy==3.1.1',
'numpy': 'numpy==1.24.3',
'pandas': 'pandas==2.0.3',
'requests': 'requests==2.31.0',
'pytest': 'pytest==7.4.0',
'opencv': 'opencv-python==4.8.0',
'PIL': 'Pillow==10.0.0',
'sklearn': 'scikit-learn==1.3.0'
}
requirements = ['# Auto-generated by PyWebForge AI Framework',
f'# {self.creator}', '']
for dep in dependencies:
base_module = dep.split('.')[0]
if base_module in package_map:
requirements.append(package_map[base_module])
elif not base_module.startswith('_') and base_module not in sys.stdlib_module_names:
requirements.append(base_module)
return '\n'.join(requirements)
def generate_config(self) -> str:
"""Generate configuration file"""
return f'''"""
Configuration for PyWebForge Application
{self.creator}
Auto-generated by AI Framework
"""
import os
from datetime import timedelta
class Config:
SECRET_KEY = os.environ.get('SECRET_KEY') or 'pywebforge-{os.urandom(16).hex()}'
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///pywebforge.db'
SQLALCHEMY_TRACK_MODIFICATIONS = False
UPLOAD_FOLDER = 'uploads'
MAX_CONTENT_LENGTH = 16 * 1024 * 1024
LOG_LEVEL = 'INFO'
class DevelopmentConfig(Config):
DEBUG = True
class ProductionConfig(Config):
DEBUG = False
config = {{
'development': DevelopmentConfig,
'production': ProductionConfig,
'default': DevelopmentConfig
}}
'''
def generate_database_models(self) -> str:
"""Generate database models"""
return f'''"""
Database Models - Auto-generated by PyWebForge AI
{self.creator}
"""
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
import json
db = SQLAlchemy()
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
metadata = db.Column(db.JSON)
class APICall(db.Model):
__tablename__ = 'api_calls'
id = db.Column(db.Integer, primary_key=True)
endpoint = db.Column(db.String(200))
method = db.Column(db.String(10))
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
response_time = db.Column(db.Float)
status_code = db.Column(db.Integer)
class ErrorLog(db.Model):
__tablename__ = 'error_logs'
id = db.Column(db.Integer, primary_key=True)
error_type = db.Column(db.String(100))
error_message = db.Column(db.Text)
stack_trace = db.Column(db.Text)
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
resolved = db.Column(db.Boolean, default=False)
'''
def generate_flask_app(self, modules: List[Dict]) -> str:
"""Generate complete Flask application"""
return f'''"""
Auto-Generated Flask Application by PyWebForge AI
{self.creator}
Generated: {datetime.now().isoformat()}
"""
from flask import Flask, request, jsonify, render_template
from flask_cors import CORS
import importlib
import sys
import os
import logging
# Initialize Flask
app = Flask(__name__)
CORS(app)
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Load configuration
app.config.from_object('config.Config')
# Initialize database
try:
from database import db
db.init_app(app)
with app.app_context():
db.create_all()
except ImportError:
logger.warning("Database module not found")
# Dynamic module loading
loaded_modules = {{}}
def load_modules():
"""Load all Python modules dynamically"""
module_paths = ['modules', 'utils', 'models', 'custom']
for path in module_paths:
if os.path.exists(path):
sys.path.insert(0, path)
for file in os.listdir(path):
if file.endswith('.py') and not file.startswith('__'):
try:
module_name = file[:-3]
module = importlib.import_module(module_name)
loaded_modules[module_name] = module
logger.info(f"Loaded module: {{module_name}}")
except Exception as e:
logger.error(f"Failed to load {{file}}: {{e}}")
# Load modules on startup
load_modules()
@app.route('/')
def index():
"""Main page"""
return jsonify({{
'status': 'online',
'creator': '{self.creator}',
'modules': list(loaded_modules.keys()),
'version': '2.0'
}})
@app.route('/api/<module>/<function>', methods=['GET', 'POST'])
def call_function(module, function):
"""Dynamic API endpoint for module functions"""
try:
if module not in loaded_modules:
return jsonify({{'error': f'Module {{module}} not found'}}), 404
if not hasattr(loaded_modules[module], function):
return jsonify({{'error': f'Function {{function}} not found'}}), 404
func = getattr(loaded_modules[module], function)
# Get parameters
data = request.get_json() if request.method == 'POST' else request.args.to_dict()
# Execute function
result = func(**data) if data else func()
return jsonify({{
'success': True,
'result': result,
'module': module,
'function': function
}})
except Exception as e:
logger.error(f"Error calling {{module}}.{{function}}: {{e}}")
return jsonify({{'error': str(e)}}), 500
@app.route('/health')
def health():
"""Health check endpoint"""
return jsonify({{
'status': 'healthy',
'modules_loaded': len(loaded_modules),
'creator': '{self.creator}'
}})
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)
'''
# ======== PROJECT ORCHESTRATION ========
def orchestrate_build(self, project_path: str, output_path: str) -> Dict:
"""
Main orchestration function - analyzes, repairs, and builds complete app
"""
self.logger.info(f"Starting PyWebForge AI orchestration for {project_path}")
result = {
'status': 'starting',
'analysis': {},
'repairs': [],
'generated_files': [],
'errors': [],
'success': False
}
try:
# Step 1: Analyze project
result['analysis'] = self.analyze_project_structure(project_path)
# Step 2: Attempt repairs for any issues found
for module in result['analysis']['modules']:
if module.get('issues'):
for issue in module['issues']:
if self.attempt_repair(module['path'], 'syntax_error', issue):
result['repairs'].append(f"Fixed: {module['path']} - {issue}")
# Step 3: Generate missing components
missing_components = self.generate_missing_components(result['analysis'])
# Step 4: Write generated files
os.makedirs(output_path, exist_ok=True)
for filename, content in missing_components.items():
filepath = os.path.join(output_path, filename)
with open(filepath, 'w', encoding='utf-8') as f:
f.write(content)
result['generated_files'].append(filepath)
self.logger.info(f"Generated: {filepath}")
# Step 5: Copy existing modules
for module in result['analysis']['modules']:
if not module.get('issues'):
dest = os.path.join(output_path, 'modules', os.path.basename(module['path']))
os.makedirs(os.path.dirname(dest), exist_ok=True)
shutil.copy(module['path'], dest)
result['status'] = 'complete'
result['success'] = True
except Exception as e:
result['errors'].append(str(e))
result['status'] = 'failed'
self.logger.error(f"Orchestration failed: {e}")
return result
# ======== MONITORING & LEARNING ========
def monitor_runtime(self, app_path: str):
"""
Monitor running application and learn from errors
"""
log_file = os.path.join(app_path, 'logs', 'pywebforge.log')
if os.path.exists(log_file):
with open(log_file, 'r') as f:
for line in f:
if 'ERROR' in line:
self.learn_from_error(line)
def learn_from_error(self, error_line: str):
"""
Learn from errors to improve future repairs
"""
# Extract error pattern
if 'ModuleNotFoundError' in error_line:
match = re.search(r"No module named '(\w+)'", error_line)
if match:
module = match.group(1)
self.error_patterns[f'missing_{module}'] = {
'type': 'import_error',
'solution': f'pip install {module}'
}
# Save learned patterns
self.save_knowledge_base()
def save_knowledge_base(self):
"""Save learned patterns and solutions"""
kb_path = 'knowledge_base/ai_learning.json'
os.makedirs(os.path.dirname(kb_path), exist_ok=True)
with open(kb_path, 'w') as f:
json.dump({
'error_patterns': self.error_patterns,
'repair_strategies': list(self.repair_strategies.keys()),
'creator': self.creator,
'version': self.version,
'last_updated': datetime.now().isoformat()
}, f, indent=2)
# ================== MAIN INTEGRATION ==================
class PyWebForgeIntegration:
"""
Integration layer between AI Framework and PyWebForge Ultimate
"""
def __init__(self):
self.ai = PyWebForgeAI()
self.creator = "Ruvan Swanepoel (MrShagnasty)"
def build_with_ai(self, source_path: str, output_path: str = 'generated_app') -> Dict:
"""
Build complete web app with AI assistance
"""
print(f"🚀 PyWebForge Ultimate AI Builder")
print(f"Created by {self.creator}")
print("="*50)
# Run AI orchestration
result = self.ai.orchestrate_build(source_path, output_path)
if result['success']:
print(f"✅ Build successful!")
print(f"Generated {len(result['generated_files'])} files")
print(f"Performed {len(result['repairs'])} automatic repairs")
print(f"\nYour app is ready at: {output_path}/")
print("\nRun: python app.py")
else:
print(f"❌ Build failed")
for error in result['errors']:
print(f" - {error}")
return result
def monitor_and_repair(self, app_path: str):
"""
Continuous monitoring and self-repair
"""
print(f"🔍 Monitoring {app_path} for issues...")
while True:
try:
# Monitor logs
self.ai.monitor_runtime(app_path)
# Check for new issues
analysis = self.ai.analyze_project_structure(app_path)
# Attempt repairs
for module in analysis['modules']:
if module.get('issues'):
for issue in module['issues']:
if self.ai.attempt_repair(module['path'], 'syntax_error', issue):
print(f"✅ Auto-repaired: {module['path']}")
# Sleep before next check
import time
time.sleep(30)
except KeyboardInterrupt:
print("\n👋 Monitoring stopped")
break
except Exception as e:
print(f"Monitor error: {e}")
# ================== CLI INTERFACE ==================
def main():
"""
Command-line interface for PyWebForge AI
"""
import argparse
parser = argparse.ArgumentParser(
description='PyWebForge Ultimate - AI-Powered Web App Builder',
epilog='Created by Ruvan Swanepoel (MrShagnasty)'
)
parser.add_argument('command', choices=['build', 'monitor', 'analyze', 'repair'],
help='Command to execute')
parser.add_argument('path', help='Path to project or module')
parser.add_argument('--output', '-o', default='generated_app',
help='Output path for generated app')
parser.add_argument('--verbose', '-v', action='store_true',
help='Verbose output')
args = parser.parse_args()
integrator = PyWebForgeIntegration()
if args.command == 'build':
integrator.build_with_ai(args.path, args.output)
elif args.command == 'monitor':
integrator.monitor_and_repair(args.path)
elif args.command == 'analyze':
ai = PyWebForgeAI()
analysis = ai.analyze_project_structure(args.path)
print(json.dumps(analysis, indent=2))
elif args.command == 'repair':
ai = PyWebForgeAI()
module_info = ai.analyze_module(args.path)
for issue in module_info.get('issues', []):
if ai.attempt_repair(args.path, 'syntax_error', issue):
print(f"✅ Repaired: {issue}")
else:
print(f"❌ Could not repair: {issue}")
if __name__ == '__main__':
main()