-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_system.py
More file actions
661 lines (535 loc) · 23.5 KB
/
plugin_system.py
File metadata and controls
661 lines (535 loc) · 23.5 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
#!/usr/bin/env python3
"""
J.A.R.V.I.S. - SISTEMA DE PLUGINS
Arquitectura extensible para agregar capacidades ilimitadas
"""
import os
import sys
import json
import importlib
import hashlib
from datetime import datetime
from typing import Dict, List, Optional, Any, Callable
from pathlib import Path
WORKING_DIR = "/data/data/com.termux/files/home/Jarvis"
PLUGINS_DIR = os.path.join(WORKING_DIR, "plugins")
PLUGINS_REGISTRY = os.path.join(WORKING_DIR, "plugins_registry.json")
os.makedirs(PLUGINS_DIR, exist_ok=True)
# ==================== REGISTRO DE PLUGINS ====================
class PluginRegistry:
"""Registro y gestor de plugins."""
def __init__(self):
self.plugins = {}
self.load_registry()
def load_registry(self):
"""Cargar registro de plugins."""
if os.path.exists(PLUGINS_REGISTRY):
with open(PLUGINS_REGISTRY, 'r', encoding='utf-8') as f:
self.plugins = json.load(f)
def save_registry(self):
"""Guardar registro de plugins."""
with open(PLUGINS_REGISTRY, 'w', encoding='utf-8') as f:
json.dump(self.plugins, f, indent=2, ensure_ascii=False)
def register_plugin(self, plugin_info: Dict) -> str:
"""Registrar plugin."""
plugin_id = plugin_info.get("id", f"plugin_{len(self.plugins)}")
self.plugins[plugin_id] = {
"id": plugin_id,
"name": plugin_info.get("name", "Unknown"),
"version": plugin_info.get("version", "1.0.0"),
"description": plugin_info.get("description", ""),
"author": plugin_info.get("author", "Anonymous"),
"file": plugin_info.get("file", ""),
"enabled": plugin_info.get("enabled", True),
"commands": plugin_info.get("commands", []),
"dependencies": plugin_info.get("dependencies", []),
"installed": datetime.now().isoformat(),
"updated": datetime.now().isoformat()
}
self.save_registry()
return plugin_id
def unregister_plugin(self, plugin_id: str) -> bool:
"""Eliminar plugin del registro."""
if plugin_id in self.plugins:
del self.plugins[plugin_id]
self.save_registry()
return True
return False
def get_plugin(self, plugin_id: str) -> Optional[Dict]:
"""Obtener información de plugin."""
return self.plugins.get(plugin_id)
def get_all_plugins(self) -> List[Dict]:
"""Obtener todos los plugins."""
return list(self.plugins.values())
def get_enabled_plugins(self) -> List[Dict]:
"""Obtener plugins habilitados."""
return [p for p in self.plugins.values() if p.get("enabled", True)]
def enable_plugin(self, plugin_id: str) -> bool:
"""Habilitar plugin."""
if plugin_id in self.plugins:
self.plugins[plugin_id]["enabled"] = True
self.plugins[plugin_id]["updated"] = datetime.now().isoformat()
self.save_registry()
return True
return False
def disable_plugin(self, plugin_id: str) -> bool:
"""Deshabilitar plugin."""
if plugin_id in self.plugins:
self.plugins[plugin_id]["enabled"] = False
self.plugins[plugin_id]["updated"] = datetime.now().isoformat()
self.save_registry()
return True
return False
def search_plugins(self, query: str) -> List[Dict]:
"""Buscar plugins."""
query_lower = query.lower()
return [
p for p in self.plugins.values()
if query_lower in p.get("name", "").lower() or
query_lower in p.get("description", "").lower()
]
# ==================== BASE CLASS PARA PLUGINS ====================
class PluginBase:
"""Clase base para todos los plugins."""
name = "Base Plugin"
version = "1.0.0"
description = "Plugin base"
author = "Unknown"
def __init__(self, jarvis_core=None):
self.jarvis = jarvis_core
self.enabled = True
self.commands = {}
def initialize(self) -> bool:
"""Inicializar plugin."""
return True
def shutdown(self) -> bool:
"""Cerrar plugin."""
return True
def register_command(self, name: str, handler: Callable, description: str = ""):
"""Registrar comando del plugin."""
self.commands[name] = {
"handler": handler,
"description": description
}
def execute_command(self, command: str, args: List[str] = None) -> Any:
"""Ejecutar comando del plugin."""
if command in self.commands:
return self.commands[command]["handler"](args or [])
return None
def get_commands(self) -> Dict:
"""Obtener comandos registrados."""
return {
name: {"description": info["description"]}
for name, info in self.commands.items()
}
def log(self, message: str, level: str = "info"):
"""Registrar log del plugin."""
print(f"[{self.name}] [{level.upper()}] {message}")
# ==================== PLUGINS BUILT-IN ====================
class SystemPlugin(PluginBase):
"""Plugin de sistema extendido."""
name = "System Extended"
version = "2.0.0"
description = "Funciones extendidas de sistema"
author = "J.A.R.V.I.S."
def initialize(self):
self.register_command("cpu", self.get_cpu_usage, "Uso de CPU")
self.register_command("memory", self.get_memory_usage, "Uso de memoria")
self.register_command("network", self.get_network_stats, "Estadísticas de red")
self.register_command("processes", self.list_top_processes, "Top procesos")
return True
def get_cpu_usage(self, args):
try:
with open('/proc/stat', 'r') as f:
line = f.readline()
parts = line.split()
idle = float(parts[4])
total = sum(float(p) for p in parts[1:8])
usage = ((total - idle) / total) * 100
return f"📊 CPU: {usage:.1f}%"
except:
return "❌ No se pudo obtener uso de CPU"
def get_memory_usage(self, args):
try:
with open('/proc/meminfo', 'r') as f:
lines = f.readlines()
mem_info = {}
for line in lines[:5]:
parts = line.split()
mem_info[parts[0].rstrip(':')] = int(parts[1])
total = mem_info.get('MemTotal', 0)
available = mem_info.get('MemAvailable', 0)
used = total - available
percent = (used / total) * 100 if total > 0 else 0
return f"💾 RAM: {used/1024/1024:.1f}GB / {total/1024/1024:.1f}GB ({percent:.1f}%)"
except:
return "❌ No se pudo obtener uso de memoria"
def get_network_stats(self, args):
try:
result = subprocess.run(
["ip", "addr", "show", "wlan0"],
capture_output=True, text=True, timeout=5
)
output = result.stdout
# Extraer IP
import re
ip_match = re.search(r'inet\s+([\d.]+)', output)
ip = ip_match.group(1) if ip_match else "N/A"
return f"🌐 Red:\n IP: {ip}\n Interface: wlan0"
except:
return "❌ No se pudo obtener info de red"
def list_top_processes(self, args):
try:
result = subprocess.run(
["ps", "-o", "pid,pcpu,mem,comm", "--sort=-pcpu"],
capture_output=True, text=True, timeout=5
)
lines = result.stdout.strip().split('\n')[:6]
return f"📊 Top 5 Procesos:\n```\n" + "\n".join(lines) + "\n```"
except:
return "❌ Error al listar procesos"
class NetworkPlugin(PluginBase):
"""Plugin de red y conectividad."""
name = "Network Tools"
version = "1.5.0"
description = "Herramientas de red avanzadas"
author = "J.A.R.V.I.S."
def initialize(self):
self.register_command("ping", self.ping_host, "Hacer ping a un host")
self.register_command("scan", self.scan_ports, "Escanear puertos")
self.register_command("speedtest", self.speedtest, "Test de velocidad")
self.register_command("dns", self.get_dns_info, "Info DNS")
return True
def ping_host(self, args):
host = args[0] if args else "8.8.8.8"
try:
result = subprocess.run(
["ping", "-c", "3", host],
capture_output=True, text=True, timeout=10
)
if result.returncode == 0:
# Extraer tiempo promedio
import re
match = re.search(r'min/avg/max = [\d.]+/([\d.]+)', result.stdout)
avg = match.group(1) if match else "N/A"
return f"🏓 Ping a {host}:\n Promedio: {avg}ms"
return f"❌ No se pudo hacer ping a {host}"
except:
return "❌ Error en ping"
def scan_ports(self, args):
host = args[0] if args else "localhost"
common_ports = [21, 22, 80, 443, 8080, 3306, 5432]
open_ports = []
for port in common_ports:
try:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(0.5)
result = sock.connect_ex((host, port))
if result == 0:
open_ports.append(port)
sock.close()
except:
pass
if open_ports:
return f"🔍 Puertos abiertos en {host}:\n " + ", ".join(map(str, open_ports))
return f"🔍 No se encontraron puertos abiertos en {host}"
def speedtest(self, args):
return "⏱️ Test de velocidad (requiere speedtest-cli):\n Instala: pip install speedtest-cli"
def get_dns_info(self, args):
try:
with open('/etc/resolv.conf', 'r') as f:
content = f.read()
dns_servers = []
for line in content.split('\n'):
if line.strip().startswith('nameserver'):
dns_servers.append(line.split()[1])
return f"🌐 DNS Servers:\n " + "\n ".join(dns_servers)
except:
return "❌ No se pudo obtener info DNS"
class IntegrationPlugin(PluginBase):
"""Plugin de integraciones externas."""
name = "Integrations"
version = "1.0.0"
description = "Integraciones con servicios externos"
author = "J.A.R.V.I.S."
def initialize(self):
self.register_command("github", self.github_info, "Info de repo GitHub")
self.register_command("weather", self.get_weather, "Clima de ciudad")
self.register_command("crypto", self.get_crypto_price, "Precio de cripto")
self.register_command("translate", self.translate_text, "Traducir texto")
return True
def github_info(self, args):
repo = args[0] if args else "torvalds/linux"
try:
import requests
resp = requests.get(f"https://api.github.com/repos/{repo}", timeout=10)
data = resp.json()
return (f"📦 **{data.get('full_name', repo)}**\n"
f"⭐ Stars: {data.get('stargazers_count', 0)}\n"
f"🍴 Forks: {data.get('forks_count', 0)}\n"
f"📝 {data.get('description', 'Sin descripción')}")
except:
return "❌ Error al obtener info de GitHub"
def get_weather(self, args):
city = args[0] if args else "Mexico City"
try:
import requests
resp = requests.get(
f"https://wttr.in/{city}?format=%c+%t+%h+%w",
headers={"User-Agent": "curl/7.68.0"},
timeout=10
)
return f"🌤️ **{city}:** {resp.text.strip()}"
except:
return "❌ Error al obtener clima"
def get_crypto_price(self, args):
coin = args[0] if args else "bitcoin"
try:
import requests
resp = requests.get(
f"https://api.coingecko.com/api/v3/simple/price?ids={coin}&vs_currencies=usd&include_24hr_change=true",
timeout=10
)
data = resp.json()
if coin in data:
price = data[coin].get("usd", 0)
change = data[coin].get("usd_24h_change", 0)
emoji = "📈" if change > 0 else "📉"
return f"{emoji} **{coin.title()}:** ${price:,} ({change:.2f}%)"
return f"❌ Criptomoneda no encontrada"
except:
return "❌ Error al obtener precio"
def translate_text(self, args):
return "🔄 Traducción (requiere API externa)\n Próximamente..."
class DevToolsPlugin(PluginBase):
"""Plugin de herramientas para desarrolladores."""
name = "Dev Tools"
version = "1.0.0"
description = "Herramientas para desarrolladores"
author = "J.A.R.V.I.S."
def initialize(self):
self.register_command("git_clone", self.git_clone, "Clonar repo Git")
self.register_command("pip_install", self.pip_install, "Instalar paquete Python")
self.register_command("file_tree", self.show_file_tree, "Árbol de archivos")
self.register_command("code_stats", self.get_code_stats, "Estadísticas de código")
return True
def git_clone(self, args):
url = args[0] if args else ""
if not url:
return "❌ URL requerida"
try:
result = subprocess.run(
["git", "clone", url],
capture_output=True, text=True, timeout=60,
cwd=WORKING_DIR
)
if result.returncode == 0:
return f"✅ Repositorio clonado:\n{result.stdout}"
return f"❌ Error: {result.stderr}"
except:
return "❌ Error al clonar (asegúrate de tener git: pkg install git)"
def pip_install(self, args):
package = args[0] if args else ""
if not package:
return "❌ Paquete requerido"
try:
result = subprocess.run(
["pip", "install", package],
capture_output=True, text=True, timeout=120
)
return f"📦 Instalando {package}:\n{result.stdout[-1000:]}"
except:
return "❌ Error al instalar paquete"
def show_file_tree(self, args):
path = args[0] if args else WORKING_DIR
try:
result = subprocess.run(
["tree", "-L", "2", path],
capture_output=True, text=True, timeout=10
)
if result.returncode == 0:
return f"📁 Estructura:\n```\n{result.stdout}\n```"
else:
# Fallback si no hay tree
return self._simple_tree(path)
except:
return self._simple_tree(path)
def _simple_tree(self, path: str, indent: str = "") -> str:
"""Árbol simple sin comando tree."""
result = []
try:
items = sorted(os.listdir(path))
for i, item in enumerate(items[:20]):
is_last = i == len(items) - 1
prefix = "└── " if is_last else "├── "
result.append(f"{indent}{prefix}{item}")
item_path = os.path.join(path, item)
if os.path.isdir(item_path) and not item.startswith('.'):
child_indent = indent + (" " if is_last else "│ ")
result.extend(self._simple_tree(item_path, child_indent).split('\n')[:5])
except:
pass
return f"📁 {path}:\n" + "\n".join(result)
def get_code_stats(self, args):
path = args[0] if args else WORKING_DIR
stats = {"files": 0, "lines": 0, "code": 0, "comments": 0, "blank": 0}
extensions = ['.py', '.js', '.ts', '.cpp', '.c', '.rs', '.java', '.go']
try:
for root, dirs, files in os.walk(path):
dirs[:] = [d for d in dirs if not d.startswith('.')]
for file in files:
if any(file.endswith(ext) for ext in extensions):
filepath = os.path.join(root, file)
stats["files"] += 1
try:
with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
for line in f:
stats["lines"] += 1
stripped = line.strip()
if not stripped:
stats["blank"] += 1
elif stripped.startswith(('#', '//', '/*', '*')):
stats["comments"] += 1
else:
stats["code"] += 1
except:
pass
except:
pass
return (f"📊 **Estadísticas de Código:**\n"
f" Archivos: {stats['files']}\n"
f" Líneas totales: {stats['lines']}\n"
f" Código: {stats['code']}\n"
f" Comentarios: {stats['comments']}\n"
f" Líneas en blanco: {stats['blank']}")
# ==================== GESTOR DE PLUGINS ====================
class PluginManager:
"""Gestor principal de plugins."""
def __init__(self, jarvis_core=None):
self.registry = PluginRegistry()
self.loaded_plugins = {}
self.jarvis = jarvis_core
# Plugins built-in
self.built_in_plugins = [
SystemPlugin,
NetworkPlugin,
IntegrationPlugin,
DevToolsPlugin
]
def load_built_in_plugins(self):
"""Cargar plugins built-in."""
for plugin_class in self.built_in_plugins:
try:
plugin = plugin_class(self.jarvis)
if plugin.initialize():
plugin_id = plugin.name.lower().replace(' ', '_')
self.loaded_plugins[plugin_id] = plugin
# Registrar en registry
self.registry.register_plugin({
"id": plugin_id,
"name": plugin.name,
"version": plugin.version,
"description": plugin.description,
"author": plugin.author,
"enabled": True,
"commands": list(plugin.get_commands().keys())
})
except Exception as e:
print(f"Error cargando plugin {plugin_class.__name__}: {e}")
def load_plugin_from_file(self, filepath: str) -> bool:
"""Cargar plugin desde archivo .py"""
try:
spec = importlib.util.spec_from_file_location("plugin_module", filepath)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
# Buscar clase que herede de PluginBase
for attr_name in dir(module):
attr = getattr(module, attr_name)
if isinstance(attr, type) and issubclass(attr, PluginBase) and attr != PluginBase:
plugin = attr(self.jarvis)
if plugin.initialize():
plugin_id = attr_name.lower()
self.loaded_plugins[plugin_id] = plugin
return True
return False
except Exception as e:
print(f"Error cargando plugin {filepath}: {e}")
return False
def install_plugin(self, plugin_info: Dict) -> str:
"""Instalar plugin."""
plugin_id = self.registry.register_plugin(plugin_info)
# Si tiene archivo, cargarlo
if plugin_info.get("file"):
filepath = os.path.join(PLUGINS_DIR, plugin_info["file"])
if os.path.exists(filepath):
self.load_plugin_from_file(filepath)
return plugin_id
def uninstall_plugin(self, plugin_id: str) -> bool:
"""Desinstalar plugin."""
# Cerrar plugin
if plugin_id in self.loaded_plugins:
self.loaded_plugins[plugin_id].shutdown()
del self.loaded_plugins[plugin_id]
# Eliminar del registro
return self.registry.unregister_plugin(plugin_id)
def execute_plugin_command(self, plugin_id: str, command: str, args: List[str] = None) -> Any:
"""Ejecutar comando de plugin."""
if plugin_id in self.loaded_plugins:
plugin = self.loaded_plugins[plugin_id]
if plugin.enabled:
return plugin.execute_command(command, args)
return f"❌ Plugin {plugin_id} está deshabilitado"
return f"❌ Plugin {plugin_id} no encontrado"
def get_all_commands(self) -> Dict[str, Dict]:
"""Obtener todos los comandos de todos los plugins."""
all_commands = {}
for plugin_id, plugin in self.loaded_plugins.items():
if plugin.enabled:
commands = plugin.get_commands()
for cmd_name, cmd_info in commands.items():
all_commands[f"{plugin_id}:{cmd_name}"] = cmd_info
return all_commands
def get_plugin_stats(self) -> Dict:
"""Obtener estadísticas de plugins."""
return {
"total_plugins": len(self.registry.plugins),
"loaded_plugins": len(self.loaded_plugins),
"enabled_plugins": len([p for p in self.loaded_plugins.values() if p.enabled]),
"total_commands": len(self.get_all_commands())
}
# ==================== INSTANCIA GLOBAL ====================
plugin_manager = PluginManager()
def test_plugins():
"""Probar sistema de plugins."""
print("🔌 Probando Sistema de Plugins...")
# Cargar built-in plugins
plugin_manager.load_built_in_plugins()
# Mostrar estadísticas
print("\n📊 Estadísticas:")
stats = plugin_manager.get_plugin_stats()
for key, value in stats.items():
print(f" {key}: {value}")
# Listar plugins
print("\n📦 Plugins cargados:")
for plugin_id, plugin in plugin_manager.loaded_plugins.items():
print(f" • {plugin.name} v{plugin.version}")
commands = plugin.get_commands()
for cmd, info in commands.items():
print(f" - {cmd}: {info['description']}")
# Ejecutar comando de ejemplo
print("\n🧪 Ejecutando comandos de prueba:")
# System plugin
result = plugin_manager.execute_plugin_command("system_extended", "cpu", [])
print(f" CPU: {result}")
result = plugin_manager.execute_plugin_command("system_extended", "memory", [])
print(f" Memoria: {result}")
# Network plugin
result = plugin_manager.execute_plugin_command("network_tools", "ping", ["8.8.8.8"])
print(f" Ping: {result}")
# Integration plugin
result = plugin_manager.execute_plugin_command("integrations", "crypto", ["bitcoin"])
print(f" Crypto: {result}")
print("\n✅ Sistema de plugins probado")
if __name__ == "__main__":
test_plugins()