import os import json class Instruccion: def __init__(self): self.instruccion = '' self.aliases = [] 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.instrucciones = [] for d in data: i = Instruccion() i.instruccion = d['instruccion'] for a in d['aliases']: i.aliases.append(a) 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 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)