[yum-cvs] yum/yum config.py,1.113,1.114

James Bowes jbowes at linux.duke.edu
Wed Feb 28 01:50:43 UTC 2007


Update of /home/groups/yum/cvs/yum/yum
In directory login1.linux.duke.edu:/tmp/cvs-serv7359/yum

Modified Files:
	config.py 
Log Message:
Use dark metaclass magic to reduce the number of times we have to examine
the attributes of config stuff. Before we did it per instance, now it is per
class.


Index: config.py
===================================================================
RCS file: /home/groups/yum/cvs/yum/yum/config.py,v
retrieving revision 1.113
retrieving revision 1.114
diff -u -r1.113 -r1.114
--- config.py	17 Feb 2007 00:00:15 -0000	1.113
+++ config.py	28 Feb 2007 01:50:41 -0000	1.114
@@ -326,17 +326,34 @@
             return BytesOption.parse(self, s)
 
 
+class autoopt(type):
+
+    def __init__(cls, name, bases, dict):
+        super(autoopt, cls).__init__(name, bases, dict)
+
+        cls.options = []
+        for base in bases:
+            options = getattr(base, 'options', [])
+            cls.options.extend(options)
+
+        for key in dict.keys():
+            option = getattr(cls, key)
+            if isinstance(option, Option):
+                cls.options.append((key, option))
+
 class BaseConfig(object):
     '''
     Base class for storing configuration definitions. Subclass when creating
     your own definitons.
     '''
 
+    __metaclass__ = autoopt
+    options = []
+
     def __init__(self):
         self._section = None
 
-        for name in self.iterkeys():
-            option = self.optionobj(name)
+        for (name, option) in self.options:
             option.setup(self, name)
 
     def __str__(self):
@@ -357,8 +374,7 @@
         self.cfg = parser
         self._section = section
 
-        for name in self.iterkeys():
-            option = self.optionobj(name)
+        for (name, option) in self.options:
             value = None
             try:
                 value = parser.get(section, name)
@@ -393,7 +409,7 @@
     def iterkeys(self):
         '''Yield the names of all defined options in the instance.
         '''
-        for name, item in self.iteritems():
+        for name, item in self.options:
             yield name
 
     def iteritems(self):
@@ -401,10 +417,7 @@
 
         The value returned is the parsed, validated option value.
         '''
-        # Use dir() so that we see inherited options too
-        for name in dir(self):
-            if self.isoption(name):
-                yield (name, getattr(self, name))
+        return self.options
 
     def write(self, fileobj, section=None, always=()):
         '''Write out the configuration to a file-like object
@@ -425,7 +438,7 @@
         fileobj.write('[%s]\n' % section)
 
         # Write options
-        for name, value in self.iteritems():
+        for name, value in self.options:
             option = self.optionobj(name)
 
             if always is None or name in always or option.default != value:




More information about the Yum-cvs-commits mailing list