[Python-checkins] distutils2: Add a way to construct DistributionMetadata from a dict.

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


tarek.ziade pushed d67ed881c17d to distutils2:

http://hg.python.org/distutils2/rev/d67ed881c17d
changeset:   448:d67ed881c17d
user:        Alexis Metaireau <ametaireau at gmail.com>
date:        Tue Jul 20 15:26:18 2010 +0200
summary:     Add a way to construct DistributionMetadata from a dict.
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
@@ -183,9 +183,12 @@
 
 class DistributionMetadata(object):
     """Distribution meta-data class (1.0 or 1.2).
+
+    if from_dict attribute is set, all key/values pairs will be sent to the
+    "set" method, building the metadata from the dict.
     """
     def __init__(self, path=None, platform_dependent=False,
-                 execution_context=None, fileobj=None):
+                 execution_context=None, fileobj=None, from_dict=None):
         self._fields = {}
         self.version = None
         self.docutils_support = _HAS_DOCUTILS
@@ -195,6 +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)
 
     def _set_best_version(self):
         self.version = _best_version(self._fields)
@@ -330,6 +335,21 @@
             for value in values:
                 self._write_field(fileobject, field, value)
 
+    def set_from_dict(self, from_dict):
+        """Set metadata values from the given dict.
+
+        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. 
+        
+        Empty values (e.g. None and []) are not setted this way.
+        """
+        for key, value in from_dict.items():
+            if value not in ([], None):
+                self.set(key, value)
+
     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