Exemple #1
0
    def __new__(mcs, name, bases, clsdict):
        """Process metadata and generate the type respectively"""
        schema = clsdict.get(mcs._attr_name_schema, list())
        slots = clsdict.get(mcs._attr_name_slots, list())

        if isinstance(slots, tuple):
            slots = list(slots)
        elif isinstance(slots, string_types):
            slots = [slots]
        # end convert slots to list

        if isinstance(schema, tuple):
            schema = list(schema)

        # next, behave similar to slots, such that we will find all _schema_ specifications and put them into
        # our own _schema_ - we find schemas horizontally and vertically.
        schema_attrs = set(pair[0] for pair in schema)

        def add_schema_from(cls):
            """concatenate schemas into one datastructure"""
            # allow overrides - therefore the base classes must not override anything that's already contained
            cls_schema = cls.__dict__.get(mcs._attr_name_schema, tuple())
            for attr, default in cls_schema:
                if attr in schema_attrs:
                    continue
                schema.append((attr, default))
                schema_attrs.add(attr)
            # end for each attribute in cls schema
        # end handle schema concatenation

        def add_schema_recursive(cls):
            """recursively use add_schema_from"""
            add_schema_from(cls)
            for base in cls.__bases__:
                add_schema_recursive(base)
            # end for each base
        # end add_schema_recursive

        for base in bases:
            add_schema_recursive(base)
        # end for each base to add recursively

        # make slots for each schema attribute
        for attr, _ in schema:
            if attr not in slots:
                slots.append(attr)
            # end prevent duplicate additions
        # end for each attribute to add as slot

        # update the changed attributes
        clsdict[mcs._attr_name_slots] = tuple(slots)
        clsdict[mcs._attr_name_schema] = tuple(schema)

        return Meta.__new__(mcs, name, bases, clsdict)
Exemple #2
0
    def __new__(metacls, name, bases, clsdict):
        """Setup descriptors to facilitate and automate attribute access"""
        alf_schema = clsdict.get("alf_schema")
        if alf_schema:
            for attr in ("options", "mandatory_options"):
                schema = getattr(alf_schema, attr)
                if schema:
                    metacls._setup_descriptors(schema, clsdict)
                # end check schema exists
            # end for each attr
        # end have schema

        return Meta.__new__(metacls, name, bases, clsdict)