108 lines
3.0 KiB
Python
108 lines
3.0 KiB
Python
import os
|
|
import json
|
|
|
|
|
|
class Command:
|
|
def __init__(self):
|
|
self.command = ''
|
|
|
|
|
|
class Commands:
|
|
def __init__(self, data_folder):
|
|
self.filename = os.path.join(data_folder, 'commands.json')
|
|
data = []
|
|
try:
|
|
with open(self.filename, 'r') as f:
|
|
data = json.load(f)
|
|
except FileNotFoundError:
|
|
pass
|
|
|
|
self.commands = []
|
|
for c in data:
|
|
cmd = Command()
|
|
cmd.command = c
|
|
self.commands.append(cmd)
|
|
|
|
def get(self, command):
|
|
for i, c in enumerate(self.commands):
|
|
if command == c.command:
|
|
return i
|
|
return None
|
|
|
|
def find(self, command):
|
|
return self.commands[self.get(command=command)]
|
|
|
|
|
|
class Instruccion:
|
|
def __init__(self):
|
|
self.instruccion = ''
|
|
self.aliases = []
|
|
self.command = None
|
|
self.params = {}
|
|
|
|
|
|
class Instrucciones:
|
|
def __init__(self, data_folder):
|
|
self.filename = os.path.join(data_folder, 'instrucciones.json')
|
|
data = []
|
|
try:
|
|
with open(self.filename, 'r') as f:
|
|
data = json.load(f)
|
|
except FileNotFoundError:
|
|
pass
|
|
|
|
self.commands = Commands(data_folder)
|
|
|
|
self.instrucciones = []
|
|
for d in data:
|
|
i = Instruccion()
|
|
i.instruccion = d['name']
|
|
if 'aliases' in d:
|
|
for a in d['aliases']:
|
|
i.aliases.append(a)
|
|
if 'params' in d:
|
|
for param, val in d['params'].items():
|
|
i.params[param] = val
|
|
if 'command' in d:
|
|
i.command = self.commands.find(d['command'])
|
|
self.instrucciones.append(i)
|
|
|
|
def get(self, instruccion):
|
|
if not self.is_valid(instruccion):
|
|
return None
|
|
for i, ins in enumerate(self.instrucciones):
|
|
if instruccion == ins.instruccion:
|
|
return i
|
|
if instruccion in ins.aliases:
|
|
return i
|
|
|
|
def find(self, instruccion):
|
|
if not self.is_valid(instruccion):
|
|
return None
|
|
return self.instrucciones[self.get(instruccion)]
|
|
|
|
def is_valid(self, instruccion):
|
|
for i in self.instrucciones:
|
|
if instruccion == i.instruccion:
|
|
return True
|
|
if instruccion in i.aliases:
|
|
return True
|
|
return False
|
|
|
|
def add(self, instruccion, aliases: list = None):
|
|
if self.is_valid(instruccion):
|
|
if aliases is not None:
|
|
i = self.get(instruccion)
|
|
self.instrucciones[i].aliases = aliases
|
|
return
|
|
ins = Instruccion()
|
|
ins.instruccion = instruccion
|
|
if aliases is not None:
|
|
ins.aliases = aliases
|
|
self.instrucciones.append(ins)
|
|
|
|
def save(self):
|
|
data = [{'instruccion': i.instruccion, 'aliases': i.aliases} for i in self.instrucciones]
|
|
with open(self.filename, 'w') as f:
|
|
json.dump(data, f, indent=4)
|