Exemple #1
0
def test_model(tmp_path):

    epc = os.path.join(tmp_path, 'model.epc')
    model = rq.new_model(epc)
    assert model is not None
    crs = rqc.Crs(model)
    crs_root = crs.create_xml()
    model.store_epc()
    assert os.path.exists(epc)
    md_datum_1 = rqw.MdDatum(model,
                             location=(0.0, 0.0, -50.0),
                             crs_uuid=crs.uuid)
    md_datum_1.create_xml(title='Datum & 1')
    md_datum_2 = rqw.MdDatum(model,
                             location=(3.0, 0.0, -50.0),
                             crs_uuid=crs.uuid)
    md_datum_2.create_xml(title='Datum < 2')
    assert len(model.uuids(obj_type='MdDatum')) == 2
    model.store_epc()

    model = rq.Model(epc)
    assert model is not None
    assert len(model.uuids(obj_type='MdDatum')) == 2
    datum_part_1 = model.part(obj_type='MdDatum', title='1', title_mode='ends')
    datum_part_2 = model.part(obj_type='MdDatum', title='2', title_mode='ends')
    assert datum_part_1 is not None and datum_part_2 is not None and datum_part_1 != datum_part_2
    datum_uuid_1 = rqet.uuid_in_part_name(datum_part_1)
    datum_uuid_2 = rqet.uuid_in_part_name(datum_part_2)
    assert not bu.matching_uuids(datum_uuid_1, datum_uuid_2)
    p1 = model.uuid_part_dict[bu.uuid_as_int(datum_uuid_1)]
    p2 = model.uuid_part_dict[bu.uuid_as_int(datum_uuid_2)]
    assert p1 == datum_part_1 and p2 == datum_part_2
Exemple #2
0
    def force_part_equivalence(self, immigrant_part, resident_part):
        """Forces immigrant part to be treated as equivalent to resident part."""

        assert immigrant_part is not None and resident_part is not None
        if immigrant_part == resident_part:
            return
        self.force_uuid_equivalence(rqet.uuid_in_part_name(immigrant_part),
                                    rqet.uuid_in_part_name(resident_part))
Exemple #3
0
def _uuid(model,
          parts_list=None,
          obj_type=None,
          uuid=None,
          title=None,
          title_mode='is',
          title_case_sensitive=False,
          extra={},
          related_uuid=None,
          epc_subdir=None,
          multiple_handling='exception'):
    """Returns the uuid of a part matching all of the arguments passed."""

    part = _part(model,
                 parts_list=parts_list,
                 obj_type=obj_type,
                 uuid=uuid,
                 title=title,
                 title_mode=title_mode,
                 title_case_sensitive=title_case_sensitive,
                 extra=extra,
                 related_uuid=related_uuid,
                 epc_subdir=epc_subdir,
                 multiple_handling=multiple_handling)
    if part is None:
        return None
    return rqet.uuid_in_part_name(part)
Exemple #4
0
def _parts_list_filtered_by_related_uuid(model,
                                         parts_list,
                                         uuid,
                                         uuid_is_source=None):
    """From a list of parts, returns a list of those parts which have a relationship with the given uuid."""

    if not model.rels_present or parts_list is None or uuid is None:
        return None
    filtered_list = []
    this_part = _part_for_uuid(model, uuid)

    if this_part is not None:
        rels_part_root = _root_for_part(
            model, rqet.rels_part_name_for_part(this_part), is_rels=True)
        if rels_part_root is not None:
            for relation_node in rels_part_root:
                if rqet.stripped_of_prefix(
                        relation_node.tag) != 'Relationship':
                    continue
                target_part = relation_node.attrib['Target']
                if target_part not in parts_list:
                    continue
                if uuid_is_source is not None:
                    source_dest = relation_node.attrib['Type']
                    if uuid_is_source:
                        if 'source' not in source_dest:
                            continue
                    else:
                        if 'source' in source_dest:
                            continue
                filtered_list.append(target_part)

    for part in parts_list:
        if part in filtered_list:
            continue
        rels_part_root = _root_for_part(model,
                                        rqet.rels_part_name_for_part(part),
                                        is_rels=True)
        if rels_part_root is None:
            continue
        for relation_node in rels_part_root:
            if rqet.stripped_of_prefix(relation_node.tag) != 'Relationship':
                continue
            target_part = relation_node.attrib['Target']
            relation_uuid = rqet.uuid_in_part_name(target_part)
            if bu.matching_uuids(uuid, relation_uuid):
                if uuid_is_source is not None:
                    source_dest = relation_node.attrib['Type']
                    if uuid_is_source:
                        if 'source' in source_dest:
                            continue  # relation is source, so uuid is not
                    else:
                        if 'source' not in source_dest:
                            continue  # relation is not source, so uuid is
                filtered_list.append(part)
                break

    return filtered_list
Exemple #5
0
def _cache_name(part):
    """Returns the attribute name used for the cached copy of the property array for the given part.

    :meta private:
    """

    if part is None:
        return None
    uuid = rqet.uuid_in_part_name(part)
    if uuid is None:
        return None
    return _cache_name_for_uuid(uuid)
Exemple #6
0
    def equivalent_uuid_for_part(self,
                                 part,
                                 immigrant_model=None,
                                 ignore_identical_part=False):
        """Returns uuid of an equivalent part in resident model, or None if no equivalent found."""

        #      log.debug('Looking for equivalent uuid for: ' + str(part))
        if not part:
            return None
        if immigrant_model is None:
            immigrant_model = self.model
        immigrant_uuid = rqet.uuid_in_part_name(part)
        #      log.debug('   immigrant uuid: ' + str(immigrant_uuid))
        if immigrant_uuid in self.map:
            #         log.debug('   known to be equivalent to: ' + str(self.map[immigrant_uuid]))
            return self.map[immigrant_uuid]
        obj_type = immigrant_model.type_of_part(part, strip_obj=True)
        if obj_type is None or obj_type not in consolidatable_list:
            return None
        #      log.debug('   object type is consolidatable')
        resident_uuids = self.model.uuids(obj_type=obj_type)
        if resident_uuids is None or len(resident_uuids) == 0:
            #         log.debug('   no resident parts found of type: ' + str(obj_type))
            return None
#      log.debug(f'   {len(resident_uuids)} resident parts of same class')
        if not ignore_identical_part:
            for resident_uuid in resident_uuids:
                if bu.matching_uuids(resident_uuid, immigrant_uuid):
                    #               log.debug('   uuid already resident: ' + str(resident_uuid))
                    return resident_uuid


#      log.debug('   preparing immigrant object')
        if obj_type.endswith('Interpretation') or obj_type.endswith('Feature'):
            immigrant_obj = rqo.__dict__[obj_type](immigrant_model,
                                                   uuid=immigrant_uuid)
        elif obj_type.endswith('Crs'):
            immigrant_obj = rqc.Crs(immigrant_model, uuid=immigrant_uuid)
        elif obj_type == 'TimeSeries':
            immigrant_obj = rqt.TimeSeries(immigrant_model,
                                           uuid=immigrant_uuid)
        elif obj_type == 'StringTableLookup':
            immigrant_obj = rqp.StringLookup(immigrant_model,
                                             uuid=immigrant_uuid)
        elif obj_type == 'PropertyKind':
            immigrant_obj = rqp.PropertyKind(immigrant_model,
                                             uuid=immigrant_uuid)
        else:
            raise Exception('code failure')
        assert immigrant_obj is not None
        for resident_uuid in resident_uuids:
            #         log.debug('   considering resident: ' + str(resident_uuid))
            if ignore_identical_part and bu.matching_uuids(
                    resident_uuid, immigrant_uuid):
                continue
            if obj_type.endswith('Interpretation') or obj_type.endswith(
                    'Feature'):
                resident_obj = rqo.__dict__[obj_type](self.model,
                                                      uuid=resident_uuid)
            elif obj_type.endswith('Crs'):
                resident_obj = rqc.Crs(self.model, uuid=resident_uuid)
            elif obj_type == 'TimeSeries':
                resident_obj = rqt.TimeSeries(self.model, uuid=resident_uuid)
            elif obj_type == 'StringTableLookup':
                resident_obj = rqp.StringLookup(self.model, uuid=resident_uuid)
            elif obj_type == 'PropertyKind':
                resident_obj = rqp.PropertyKind(self.model, uuid=resident_uuid)
            else:
                raise Exception('code failure')
            assert resident_obj is not None
            #         log.debug('   comparing with: ' + str(resident_obj.uuid))
            if immigrant_obj == resident_obj:  # note: == operator overloaded with equivalence method for these classes
                while resident_uuid in self.map:
                    #               log.debug('   following equivalence for: ' + str(resident_uuid))
                    resident_uuid = self.map[resident_uuid]
                self.map[immigrant_uuid] = resident_uuid
                #            log.debug('   new equivalence found with: ' + str(resident_uuid))
                return resident_uuid
        return None