def _parseConstraints(self, xml): """ Create list of constraint (objects Constraint) :param xml: :return: """ if xml.nodeName != "table": raise ParseError("Element is not a table","_parseConstraints") list = [] xmlConstraints = xml.getElementsByTagName("constraint") for item in xmlConstraints: constraint = Constraint() attributes = item.attributes.items() for name, val in attributes: if name.lower() == "name": constraint.name = val elif name.lower() == "kind": constraint.kind = val elif name.lower() == "items": detail = ConstraintDetail() detail.value = val constraint.details.append(detail) constraint.items = val elif name.lower() == "props": for prop in val.split(", "): if prop == "has_value_edit": constraint.has_value_edit = True elif prop == "cascading_delete": constraint.cascading_delete = False elif prop == "full_cascading_delete": constraint.cascading_delete = True else: raise ParseError("Invalid format of propertiess: \"{}\"".format(val), self) elif name.lower() == "reference": constraint.reference = val elif name.lower() == 'expression': constraint.expression = val else: raise ParseError("Invalid attribute name \"{}\"".format(name), self) list.append(constraint) for detail_node in item.childNodes: if detail_node.tagName != 'item': raise ParseError("item not found") detail = self._create_constraint_detail(detail_node._attrs) constraint.details.append(detail) return list
def _parseIndexes(self, xml): """ Create list of indexes (object Index) :param xml: :return: """ if xml.nodeName != "table": raise TypeError("Element is not a table") list = [] xml_indexes = xml.getElementsByTagName("index") for item in xml_indexes: tmp = Index() attributes = item.attributes.items() for name, val in attributes: if name.lower() == "field": detail = IndexDetail() detail.value = val tmp.details.append(detail) elif name.lower() == "items": tmp.items = val elif name.lower() == "name": tmp.items = val elif name.lower() == "props": for prop in val.split(", "): if prop == 'local': tmp.local = True if prop == "fulltext": tmp.kind = "fulltext" elif prop == "uniqueness": tmp.kind = "uniqueness" else: raise ParseError("Invalid format of propertiess: \"{}\"".format(val), self) else: raise ParseError("Invalid attribute name \"{}\"".format(name), self) for detail_node in item.childNodes: if detail_node.tagName != 'item': raise Exception() detail = self._create_index_detail(detail_node._attrs) tmp.details.append(detail) list.append(tmp) return list
def create_index(self,attr_dict): index = Index() if attr_dict is None: return index index_id = None table_id = None for attr in attr_dict: if attr == 'name': index.name = attr_dict[attr] elif attr == 'field': detail = IndexDetail() detail.value = attr_dict[attr] index.details.append(detail) print(index.details) elif attr == 'kind': index.kind = attr_dict[attr] elif attr == 'local': index.local = attr_dict[attr] elif attr == 'uniqueness': index.uniqueness = attr_dict[attr] elif attr == 'fulltext': index.fulltext = attr_dict[attr] elif attr == 'id': index_id = attr_dict[attr] elif attr == 'table_id': table_id = attr_dict[attr] else: raise ParseError("Unsupported attribute {}".format(attr),self) return index, index_id, table_id
def create_constraint(self,attr_dict): constraint = Constraint() if attr_dict is None: return constraint constraint_id = None table_id = None for attr in attr_dict: if attr == 'name': constraint.name = attr_dict[attr] elif attr == 'constraint_type': constraint.kind = attr_dict[attr] elif attr == 'items': detail = ConstraintDetail() detail.value = attr_dict[attr] constraint.details.append(detail) elif attr == 'reference': constraint.reference = attr_dict[attr] elif attr == 'unique_key_id': constraint.constraint = attr_dict[attr] elif attr == 'expression': constraint.expression = attr_dict[attr] elif attr == 'has_value_edit': constraint.has_value_edit = attr_dict[attr] elif attr == 'cascading_delete': constraint.cascading_delete = attr_dict[attr] elif attr == 'id': constraint_id = attr_dict[attr] elif attr == 'table_id': table_id = attr_dict[attr] else: raise ParseError("Unsupported attribute {}".format(attr),self) return constraint, constraint_id, table_id
def create_table(self,attr_dict): table = Table() table_id = None schema_id = None for attr in attr_dict: if attr == 'name': table.name = attr_dict[attr] elif attr == 'description': table.descr = attr_dict[attr] elif attr == 'temporal_mode': table.ht_table_flags = attr_dict[attr] elif attr == 'access_level': table.access_level = attr_dict[attr] elif attr == 'can_add': table.add = attr_dict[attr] elif attr == 'can_edit': table.edit = attr_dict[attr] elif attr == 'can_delete': table.delete = attr_dict[attr] elif attr == 'means': table.means = attr_dict[attr] elif attr == 'schema_id': schema_id = attr_dict[attr] elif attr == 'id': table_id = attr_dict[attr] else: raise ParseError("Unsupported attribute {}".format(attr),self) return table, table_id, schema_id
def _parseTable(self,item): """ Parse table from xml :param xml: xml.dom.minidom.Document :return: """ table = Table() attributes = item.attributes.items() for name, val in attributes: if name.lower() == "name": table.name = val elif name.lower() == "description": table.descr = val elif name.lower() == "props": for prop in val.split(", "): if prop == "add": table.add = True elif prop == "edit": table.edit = True elif prop == "delete": table.delete = True else: raise ParseError("Invalid format of propertiess: \"{}\" ".format(val), self) return table
def convertRam2Xdb(self, schema, xml_path): """ Create ram representation of incoming Schema object :param schema: :param xml_path: :return: """ if schema is None: raise ParseError("Schema not found", self) node = self.create_schema(schema) node.appendChild(self.xml.createElement("custom")) if schema.domains: domains = self.xml.createElement("domains") for domain in self.create_domain(schema.domains): domains.appendChild(domain) node.appendChild(domains) tables = self.xml.createElement("tables") for table in self.create_table(schema.tables): tables.appendChild(table) node.appendChild(tables) try: self.xml.appendChild(node) if xml_path: Writer.write_xml(xml_path, self.xml) except Exception: raise ItemNotFoundException("domains, tables", schema.name)
def _parseFields(safe, table): """ :param table: Dbc.Table() to find fields in. :return: fields_list: list of fields in table. Parse table to find fields. """ if table.nodeName != "table": raise ParseError("Element is not a table","_parseFields") fields_list = list() pos = 1 for field in table.getElementsByTagName("field"): fld = Field() fld.position = str(pos) for an, av in field.attributes.items(): if an.lower() == "name": fld.name = av elif an.lower() == "rname": fld.rname = av elif an.lower() == "domain": fld.domain = av elif an.lower() == "description": fld.descr = av elif an.lower() == "props": for prop in av.split(", "): if prop == "input": fld.input = True elif prop == "edit": fld.edit = True elif prop == "show_in_grid": fld.show_in_grid = True elif prop == "show_in_details": fld.show_in_details = True elif prop == "autocalculated": fld.autocalculated = True elif prop == "is_mean": fld.is_mean = True elif prop == "required": fld.required = True else: raise ParseError("Invalid attribute name \"{}\"".format(fld.name), fld.self) else: raise ParseError("Invalid attribute name \"{}\"".format(fld.name), fld.self) pos += 1 fields_list.append(fld) return fields_list
def _create_constraint_detail(self, attr_dict): """ Create detail of constraint :param attr_dict: :return: """ detail = ConstraintDetail() for attr, val in attr_dict: if attr == 'value': detail.value = val else: raise ParseError("Invalid attribute name \"{}\"".format(attr),self) return detail
def _parseDomains(self): """ Create list of domains (objects Domain) :param xml: xml.dom.minidom.Document :return: """ list = [] domain_parent = self.xml.getElementsByTagName("domain") for item in domain_parent: domain = Domain() attributes = item.attributes.items() for name, val in attributes: if name.lower() == "name": domain.name = val elif name.lower() == "description": domain.descr = val elif name.lower() == "type": domain.type = val elif name.lower() == "align": domain.align = val elif name.lower() == "width": domain.width = val elif name.lower() == "precision": domain.precision = val elif name.lower() == "char_length": domain.char_length = val elif name.lower() == "length": domain.length = val elif name.lower() == "scale": domain.scale = val elif name.lower() == "props": for prop in val.split(", "): if prop == "show_null": domain.show_null = True elif prop == "summable": domain.summable = True elif prop == "case_sensitive": domain.case_sensitive = True elif prop == "show_lead_nulls": domain.show_lead_nulls = True elif prop == "thousands_separator": domain.thousands_separator = True else: raise ParseError("Invalid format of propertiess: \"{}\"".format(val), self) list.append(domain) return list
def create_constraint_detail(self,attr_dict): detail = ConstraintDetail() detail_id = None constraint_id = None for attr in attr_dict: if attr == 'field_name': detail.value = attr_dict[attr] elif attr == 'id': detail_id = attr_dict[attr] elif attr == 'constraint_id': constraint_id = attr_dict[attr] else: raise ParseError("Unsupported attribute {}".format(attr),self) return detail, detail_id, constraint_id
def create_schema(self, schema_row): schema = Schema() schema_id = None for attr in schema_row: if attr == 'name': schema.name = schema_row[attr] elif attr == 'fulltext_engine': schema.fulltext_engine = schema_row[attr] elif attr == 'version': schema.version = schema_row[attr] elif attr == 'description': schema.descr = schema_row[attr] elif attr == 'id': schema_id = schema_row[attr] else: raise ParseError("Unsupported attribute {}".format(attr), self) return schema, schema_id
def _create_index_detail(self, attr_dict): """ Create detail of index :param attr_dict: :return: """ detail = IndexDetail() for attr, val in attr_dict: if attr == 'value': detail.value = val elif attr == 'expression': detail.expression = val elif attr == 'descend': detail.descend = val else: raise ParseError("Invalid attribute name \"{}\"".format(attr), self) return detail
def _parseSchema(self): """ Create schema from xml/xdb :param xml: xml.dom.minidom.Document :return: Schema """ schema = Schema() attributes = self.xml.documentElement.attributes.items() for name, val in attributes: if name.lower() == "name": schema.name = val elif name.lower() == "version": schema.version = val elif name.lower() == "fulltext_engine": schema.fulltext_engine = val elif name.lower() == "description": schema.descr = val else: raise ParseError("Invalid attribute name \"{}\"".format(name), self) #генерируем исключение return schema
def create_index_detail(self,attr_dict): detail = IndexDetail() detail_id = None index_id = None for attr in attr_dict: if attr == 'field_name': detail.value = attr_dict[attr] elif attr == 'expression': detail.expression = attr_dict[attr] elif attr == 'descend': detail.descend = attr_dict[attr] elif attr == 'id': detail_id = attr_dict[attr] elif attr == 'index_id': index_id = attr_dict[attr] else: raise ParseError("Unsupported attribute {}".format(attr),self) return detail, detail_id, index_id
def create_domain(self,attr_dict): domain = Domain() domain_id = None for attr in attr_dict: if attr == 'name': domain.name = attr_dict[attr] elif attr == 'data_type_name': domain.type = attr_dict[attr] elif attr == 'data_type_id': domain.type = attr_dict[attr] elif attr == 'align': domain.align = attr_dict[attr] elif attr == 'width': domain.width = attr_dict[attr] elif attr == 'char_length': domain.char_length = attr_dict[attr] elif attr == 'description': domain.descr = attr_dict[attr] elif attr == 'length': domain.length = attr_dict[attr] elif attr == 'scale': domain.scale = attr_dict[attr] elif attr == 'precision': domain.precision = attr_dict[attr] elif attr == 'case_sensitive': domain.case_sensitive = attr_dict[attr] elif attr == 'show_null': domain.show_null = attr_dict[attr] elif attr == 'show_lead_nulls': domain.show_lead_nulls = attr_dict[attr] elif attr == 'thousands_separator': domain.thousands_separator = attr_dict[attr] elif attr == 'summable': domain.summable = attr_dict[attr] elif attr == 'id': domain_id = attr_dict[attr] else: raise ParseError("Unsupported attribute {}".format(attr),self) return domain, domain_id
def create_field(self,attr_dict): field = Field() field_id = None table_id = None for attr in attr_dict: if attr == 'name': field.name = attr_dict[attr] elif attr == 'russian_short_name': field.rname = attr_dict[attr] elif attr == 'domain_name': field.domain = attr_dict[attr] elif attr == 'type': field.type = attr_dict[attr] elif attr == 'description': field.descr = attr_dict[attr] elif attr == 'can_input': field.input = attr_dict[attr] elif attr == 'can_edit': field.edit = attr_dict[attr] elif attr == 'show_in_grid': field.show_in_grid = attr_dict[attr] elif attr == 'show_in_details': field.show_in_details = attr_dict[attr] elif attr == 'is_mean': field.is_mean = attr_dict[attr] elif attr == 'autocalculated': field.autocalculated = attr_dict[attr] elif attr == 'required': field.required = attr_dict[attr] elif attr == 'id': field_id = attr_dict[attr] elif attr == 'table_id': table_id = attr_dict[attr] else: raise ParseError("Unsupported attribute {}".format(attr),self) return field, field_id, table_id