156 lines
5.5 KiB
Python
156 lines
5.5 KiB
Python
import MySQLdb
|
|
|
|
from .libraryparser import XMLLibraryParser, JSONLibraryParser, YAMLLibraryParser
|
|
|
|
from .LibraryOperation import LibraryOperation
|
|
from .operations.AssignAttributeOperation import AssignAttributeOperation
|
|
from .operations.CreateClassOperation import CreateClassOperation
|
|
from .operations.CreateInstanceOperation import CreateInstanceOperation
|
|
from .operations.PrepareInstanceOperation import PrepareInstanceOperation
|
|
from .operations.StoredProcedureOperation import StoredProcedureOperation
|
|
|
|
from .Guid import Guid
|
|
|
|
class MochaLibraryManager:
|
|
|
|
def __init__(self):
|
|
self.entityReferences = dict()
|
|
self.db = None
|
|
self._instances = []
|
|
self._operations = []
|
|
|
|
def connect(self, hostname, database, username, password):
|
|
self.db = MySQLdb.connect(host=hostname, user=username, passwd=password, db=database)
|
|
|
|
def commit(self):
|
|
for (class_gid, inst_gid, classIndex, index) in self._instances:
|
|
op = PrepareInstanceOperation(Guid(inst_gid), classIndex, index)
|
|
op.execute(self.db)
|
|
|
|
for op in self._operations:
|
|
op.execute(self.db)
|
|
|
|
self.db.commit()
|
|
|
|
def close(self):
|
|
self.db.close()
|
|
|
|
def print_error(self, cur):
|
|
rows = cur.fetchall()
|
|
for row in rows:
|
|
if 'error_description' in row:
|
|
print (row['error_description'])
|
|
|
|
def select_tenant(self, tenantName):
|
|
cur = self.db.cursor()
|
|
cur.execute("CALL mocha_select_tenant(mocha_get_tenant_by_name('" + tenantName + "'))")
|
|
self.print_error(cur)
|
|
|
|
def release_tenant(self):
|
|
cur = self.db.cursor()
|
|
cur.execute("CALL mocha_release_tenant()")
|
|
self.print_error(cur)
|
|
|
|
def create_instance(self, instanceGlobalId : Guid, classIndex : int, instanceIndex : int):
|
|
#self._operations.append(CreateInstanceOperation(instanceGlobalId, classIndex, instanceIndex))
|
|
|
|
# cur = self.db.cursor()
|
|
|
|
# strInstanceGlobalId = "NULL"
|
|
# if instanceGlobalId != None:
|
|
# strInstanceGlobalId = "mocha_normalize_uuid('" + instanceGlobalId + "')"
|
|
|
|
# query = "CALL mocha_create_instance(" + strInstanceGlobalId + ", " + str(classIndex) + ", " + str(instanceIndex) + ", NULL, NULL, @q_assigned_inst_id)"
|
|
# print(query)
|
|
# cur.execute(query)
|
|
# self.print_error(cur)
|
|
pass
|
|
|
|
def create_class(self, instanceGlobalId : Guid, classIndex):
|
|
#self._operations.append(CreateClassOperation(instanceGlobalId, classIndex))
|
|
pass
|
|
|
|
def create_instance_of(self, classGlobalId : Guid, instanceGlobalId : Guid, classIndex : int, instanceIndex : int):
|
|
self._operations.append(CreateInstanceOperation(instanceGlobalId, classIndex, instanceIndex))
|
|
# cur = self.db.cursor()
|
|
|
|
# strInstanceGlobalId = "NULL"
|
|
# if instanceGlobalId != None:
|
|
# strInstanceGlobalId = "mocha_normalize_uuid('" + instanceGlobalId + "')"
|
|
|
|
# query = "CALL mocha_create_instance_of(mocha_get_instance_by_global_identifier('" + classGlobalId + "'), " + strInstanceGlobalId + ", NULL, NULL, @q_assigned_inst_id)"
|
|
# print(query)
|
|
# cur.execute(query)
|
|
# self.print_error(cur)
|
|
|
|
def assign_relationship(self, instanceId : Guid, relationshipInstanceId : Guid, targetInstanceId : Guid):
|
|
print("-- assigning relationship " + relationshipInstanceId.get_value() + " = '" + targetInstanceId.get_value() + "'")
|
|
# self._operations.append(AssignRelationshipOperation(instanceId, relationshipInstanceId, targetInstanceId))
|
|
# query = "CALL mocha_assign_relationship(mocha_get_instance_by_global_identifier(mocha_normalize_uuid('" + instanceId.get_value() + "')), mocha_get_instance_by_global_identifier(mocha_normalize_uuid('" + relationshipInstanceId.get_value() + "')), mocha_get_instance_by_global_identifier(mocha_normalize_uuid('" + targetInstanceId.get_value() + "')), NULL, NULL);"
|
|
# print(query)
|
|
# cur.execute(query)
|
|
# self.print_error(cur)
|
|
|
|
def set_attribute_value(self, instanceId : Guid, attributeInstanceId : Guid, value):
|
|
print("-- assigning attribute " + attributeInstanceId.get_value() + " = '" + str(value) + "'")
|
|
self._operations.append(AssignAttributeOperation(instanceId, attributeInstanceId, value))
|
|
|
|
def install_from_path(self, path):
|
|
|
|
from glob import glob
|
|
|
|
xl = XMLLibraryParser(self)
|
|
jl = JSONLibraryParser(self)
|
|
yl = YAMLLibraryParser(self)
|
|
|
|
#xml_files = glob(path + "/**/*.xml", recursive=True)
|
|
#for xml_file in xml_files:
|
|
#s xl.load_file(xml_file)
|
|
|
|
|
|
#json_files = glob(path + "/**/*.json", recursive=True)
|
|
#for json_file in json_files:
|
|
# jl.load_file(json_file)
|
|
|
|
yaml_files = sorted(glob(path + "/**/*.yaml", recursive=True))
|
|
if len(yaml_files) == 0:
|
|
print ("no files found ; does the path exist?")
|
|
return 3
|
|
|
|
for yaml_file in yaml_files:
|
|
yl.load_entity_definitions_from_file(yaml_file)
|
|
for yaml_file in yaml_files:
|
|
yl.load_instances_from_file(yaml_file)
|
|
|
|
yl.apply_sugar()
|
|
|
|
def register_entity_reference(self, name, value):
|
|
self.entityReferences[name] = value
|
|
def define_entity_reference(self, name):
|
|
return self.entityReferences[name]
|
|
def expand_entity_references(self, value):
|
|
insideName = False
|
|
name = ""
|
|
retval = ""
|
|
for i in range(0, len(value)):
|
|
if value[i] == "&":
|
|
insideName = True
|
|
elif value[i] == ';':
|
|
insideName = False
|
|
|
|
if name in self.entityReferences:
|
|
retval += self.define_entity_reference(name)
|
|
else:
|
|
raise NameError("unknown entity ref '" + name + "'")
|
|
|
|
name = ""
|
|
elif insideName:
|
|
name += value[i]
|
|
else:
|
|
retval += value[i]
|
|
return retval
|
|
|
|
def add_instance(self, classGid : Guid, instGid : Guid, classIndex : int, index : int):
|
|
print("adding instance for class '" + classGid.get_value() + "' with globalid '" + instGid.get_value() + "' [" + str(index) + "]")
|
|
self._instances.append((classGid.get_value(), instGid.get_value(), classIndex, index))
|