summaryrefslogtreecommitdiff
path: root/attoconf/classy.py
diff options
context:
space:
mode:
authorBen Longbons <b.r.longbons@gmail.com>2014-07-17 11:49:24 -0700
committerBen Longbons <b.r.longbons@gmail.com>2014-07-17 11:50:04 -0700
commitd33fcf6cc7380633329fdaf2e7f7543bbdd2490e (patch)
treea93784103a38b10e72abedff927c6951288885d6 /attoconf/classy.py
parent9d681429a3c78520614be1f4852de8ffb1bfcd8a (diff)
downloadattobuild-d33fcf6cc7380633329fdaf2e7f7543bbdd2490e.tar.gz
attobuild-d33fcf6cc7380633329fdaf2e7f7543bbdd2490e.tar.bz2
attobuild-d33fcf6cc7380633329fdaf2e7f7543bbdd2490e.tar.xz
attobuild-d33fcf6cc7380633329fdaf2e7f7543bbdd2490e.zip
Use a metaclass instead of @add_slots
Diffstat (limited to 'attoconf/classy.py')
-rw-r--r--attoconf/classy.py35
1 files changed, 22 insertions, 13 deletions
diff --git a/attoconf/classy.py b/attoconf/classy.py
index a27230d..98d54aa 100644
--- a/attoconf/classy.py
+++ b/attoconf/classy.py
@@ -1,4 +1,4 @@
-# Copyright 2013 Ben Longbons <b.r.longbons@gmail.com>
+# Copyright 2013-2014 Ben Longbons <b.r.longbons@gmail.com>
#
# This file is part of attoconf.
#
@@ -19,24 +19,33 @@ from __future__ import print_function, division, absolute_import
from .core import Project, Build
-# TODO: put this in a metaclass
-# some of the init/jiggle logic would also be simplified by a metaclass
+class PolymorphicSlotMergerMetaclass(type):
+
+ def __new__(meta, name, bases, dct):
+ if '__slots__' not in dct:
+ s = ()
+ for b in bases:
+ s2 = b.__dict__.get('_merge_slots_', ())
+ if isinstance(s2, basestring):
+ s2 = (s2,)
+ s += s2
+ s += dct.get('_merge_slots_', ())
+ dct['__slots__'] = s
+ else:
+ assert dct['__slots__'] == ()
+ return type.__new__(meta, name, bases, dct)
+
+
+# TODO: remove this for 1.0
def add_slots(cls):
- ''' A decorator that fixes __slots__ after multiple inheritance.
- '''
- return type(cls.__name__, cls.__bases__,
- dict(cls.__dict__, __slots__=cls.slots()))
+ return cls
class ClassyProject(Project):
''' A more convenient, objectish, way of setting up a project.
'''
__slots__ = ()
- @classmethod
- def slots(self):
- ''' slots don't work too well with multiple inheritance,
- so make the most-derived class create all the slots
- '''
- return ()
+ _merge_slots_ = ()
+ __metaclass__ = PolymorphicSlotMergerMetaclass
def __init__(self, srcdir):
super(ClassyProject, self).__init__(srcdir)