

def __new__(cls, name, bases, attrs):
    if (not bases):
        return super(BaseModel, cls).__new__(cls, name, bases, attrs)
    meta_options = {
        
    }
    meta = attrs.pop('Meta', None)
    if meta:
        for (k, v) in meta.__dict__.items():
            if (not k.startswith('_')):
                meta_options[k] = v
    model_pk = getattr(meta, 'primary_key', None)
    parent_pk = None
    for b in bases:
        if (not hasattr(b, '_meta')):
            continue
        base_meta = getattr(b, '_meta')
        if (parent_pk is None):
            parent_pk = deepcopy(base_meta.primary_key)
        all_inheritable = (cls.inheritable | base_meta._additional_keys)
        for (k, v) in base_meta.__dict__.items():
            if ((k in all_inheritable) and (k not in meta_options)):
                meta_options[k] = v
        for (k, v) in b.__dict__.items():
            if (k in attrs):
                continue
            if isinstance(v, FieldDescriptor):
                if (not v.field.primary_key):
                    attrs[k] = deepcopy(v.field)
    cls = super(BaseModel, cls).__new__(cls, name, bases, attrs)
    ModelOptionsBase = meta_options.get('model_options_base', ModelOptions)
    cls._meta = ModelOptionsBase(cls, **meta_options)
    cls._data = None
    cls._meta.indexes = list(cls._meta.indexes)
    if (not cls._meta.db_table):
        cls._meta.db_table = re.sub('[^\\w]+', '_', cls.__name__.lower())
    fields = []
    for (name, attr) in cls.__dict__.items():
        if isinstance(attr, Field):
            if (attr.primary_key and model_pk):
                raise ValueError('primary key is overdetermined.')
            elif attr.primary_key:
                (model_pk, pk_name) = (attr, name)
            else:
                fields.append((attr, name))
    composite_key = False
    if (model_pk is None):
        if parent_pk:
            (model_pk, pk_name) = (parent_pk, parent_pk.name)
        else:
            (model_pk, pk_name) = (PrimaryKeyField(primary_key=True), 'id')
    elif isinstance(model_pk, CompositeKey):
        pk_name = '_composite_key'
        composite_key = True
    if (model_pk is not False):
        model_pk.add_to_class(cls, pk_name)
        cls._meta.primary_key = model_pk
        cls._meta.auto_increment = (isinstance(model_pk, PrimaryKeyField) or bool(model_pk.sequence))
        cls._meta.composite_key = composite_key
    for (field, name) in fields:
        field.add_to_class(cls, name)
    if hasattr(cls, '__unicode__'):
        setattr(cls, '__repr__', (lambda self: ('<%s: %r>' % (cls.__name__, self.__unicode__()))))
    exc_name = ('%sDoesNotExist' % cls.__name__)
    exc_attrs = {
        '__module__': cls.__module__,
    }
    exception_class = type(exc_name, (DoesNotExist,), exc_attrs)
    cls.DoesNotExist = exception_class
    cls._meta.prepared()
    if hasattr(cls, 'validate_model'):
        cls.validate_model()
    return cls

dataset/ETHPy150Open coleifer/peewee/peewee.py BaseModel.__new__/original