[Python-checkins] distutils2: Rename metadata `from_dict` argument and method to `mapping` and `update`.

tarek.ziade python-checkins at python.org
Sun Aug 8 11:50:46 CEST 2010


tarek.ziade pushed e61b7c73e560 to distutils2:

http://hg.python.org/distutils2/rev/e61b7c73e560
changeset:   452:e61b7c73e560
user:        Alexis Metaireau <ametaireau at gmail.com>
date:        Wed Jul 21 14:24:05 2010 +0200
summary:     Rename metadata `from_dict` argument and method to `mapping` and `update`.
files:       src/distutils2/metadata.py

diff --git a/src/distutils2/metadata.py b/src/distutils2/metadata.py
--- a/src/distutils2/metadata.py
+++ b/src/distutils2/metadata.py
@@ -188,7 +188,7 @@
     "set" method, building the metadata from the dict.
     """
     def __init__(self, path=None, platform_dependent=False,
-                 execution_context=None, fileobj=None, from_dict=None):
+                 execution_context=None, fileobj=None, mapping=None):
         self._fields = {}
         self.version = None
         self.docutils_support = _HAS_DOCUTILS
@@ -198,8 +198,8 @@
         elif fileobj is not None:
             self.read_file(fileobj)
         self.execution_context = execution_context
-        if from_dict:
-            self.set_from_dict(from_dict)
+        if mapping:
+            self.update(mapping)
 
     def _set_best_version(self):
         self.version = _best_version(self._fields)
@@ -335,21 +335,35 @@
             for value in values:
                 self._write_field(fileobject, field, value)
 
-    def set_from_dict(self, from_dict):
-        """Set metadata values from the given dict.
+    def update(self, other=None, **kwargs):
+        """Set metadata values from the given mapping
 
         If overwrite is set to False, just add metadata values that are
         actually not defined.
 
         If there is existing values in conflict with the dictionary ones, the
-        dictionary values prevails. 
-        
+        new values prevails.
+
         Empty values (e.g. None and []) are not setted this way.
         """
-        for key, value in from_dict.items():
+        def _set(key, value):
             if value not in ([], None):
                 self.set(key, value)
 
+        if other is None:
+            pass
+        elif hasattr(other, 'iteritems'):  # iteritems saves memory and lookups
+            for k, v in other.iteritems():
+                _set(k, v)
+        elif hasattr(other, 'keys'):
+            for k in other.keys():
+                _set(k, v)
+        else:
+            for k, v in other:
+                _set(k, v)
+        if kwargs:
+            self.update(kwargs)
+
     def set(self, name, value):
         """Controls then sets a metadata field"""
         name = self._convert_name(name)

--
Repository URL: http://hg.python.org/distutils2


More information about the Python-checkins mailing list