Ejemplo n.º 1
0
    def _rebase_ref(
        self,
        schema: s_schema.Schema,
        context: sd.CommandContext,
        scls: s_referencing.ReferencedInheritingObject,
        old_bases: List[InheritingObject],
        new_bases: List[InheritingObject],
    ) -> Tuple[s_schema.Schema, s_referencing.AlterReferencedInheritingObject]:
        old_base_names = [b.get_name(schema) for b in old_bases]
        new_base_names = [b.get_name(schema) for b in new_bases]

        removed, added = delta_bases(old_base_names, new_base_names)

        rebase = sd.ObjectCommandMeta.get_command_class(
            RebaseInheritingObject, type(scls))

        alter = sd.ObjectCommandMeta.get_command_class(sd.AlterObject,
                                                       type(scls))

        new_bases_coll = so.ObjectList.create(schema, new_bases)
        schema = scls.set_field_value(schema, 'bases', new_bases_coll)
        ancestors = so.compute_ancestors(schema, scls)
        ancestors_coll = so.ObjectList.create(schema, ancestors)

        alter_cmd = alter(
            classname=scls.get_name(schema),
            metaclass=type(scls),
        )

        if rebase is not None:
            rebase_cmd = rebase(
                classname=scls.get_name(schema),
                metaclass=type(scls),
                removed_bases=removed,
                added_bases=added,
            )

            rebase_cmd.set_attribute_value(
                'bases',
                new_bases_coll,
            )

            rebase_cmd.set_attribute_value(
                'ancestors',
                ancestors_coll,
            )

            alter_cmd.add(rebase_cmd)

        alter_cmd.set_attribute_value(
            'bases',
            new_bases_coll,
        )

        alter_cmd.set_attribute_value(
            'ancestors',
            ancestors_coll,
        )

        return schema, alter_cmd
Ejemplo n.º 2
0
    def _rebase_ref(
        self,
        schema: s_schema.Schema,
        context: sd.CommandContext,
        scls: s_referencing.ReferencedInheritingObject,
        old_bases: Sequence[so.InheritingObject],
        new_bases: Sequence[so.InheritingObject],
    ) -> Tuple[s_schema.Schema, sd.Command]:
        from . import referencing as s_referencing

        old_base_names = [b.get_name(schema) for b in old_bases]
        new_base_names = [b.get_name(schema) for b in new_bases]

        removed, added = delta_bases(old_base_names, new_base_names)

        rebase = sd.get_object_command_class(RebaseInheritingObject,
                                             type(scls))

        alter_cmd_root, alter_cmd, _ = (scls.init_delta_branch(
            schema, context, sd.AlterObject))
        assert isinstance(alter_cmd, AlterInheritingObject)

        new_bases_coll = so.ObjectList.create(schema, new_bases)
        schema = scls.set_field_value(schema, 'bases', new_bases_coll)
        ancestors = so.compute_ancestors(schema, scls)
        ancestors_coll = so.ObjectList[
            s_referencing.ReferencedInheritingObject].create(
                schema, ancestors)

        if rebase is not None:
            rebase_cmd = rebase(
                classname=scls.get_name(schema),
                removed_bases=removed,
                added_bases=added,
            )

            rebase_cmd.set_attribute_value(
                'bases',
                new_bases_coll,
            )

            rebase_cmd.set_attribute_value(
                'ancestors',
                ancestors_coll,
            )

            alter_cmd.add(rebase_cmd)

        alter_cmd.set_attribute_value(
            'bases',
            new_bases_coll,
        )

        alter_cmd.set_attribute_value(
            'ancestors',
            ancestors_coll,
        )

        schema = alter_cmd_root.apply(schema, context)

        return schema, alter_cmd_root