[Python-checkins] distutils2: Changing the metadata.version API and relocating the metadata_to_dict function.

tarek.ziade python-checkins at python.org
Wed Feb 16 22:23:56 CET 2011


tarek.ziade pushed a1f153865c36 to distutils2:

http://hg.python.org/distutils2/rev/a1f153865c36
changeset:   1018:a1f153865c36
user:        Kelsey Hightower <kelsey.hightower at gmail.com>
date:        Thu Feb 10 00:14:01 2011 -0500
summary:
  Changing the metadata.version API and relocating the metadata_to_dict function.

A new function, get_metadata_version, replaces the metadata.version attribute as the preferred method of retrieving the metadata version.

The metadata_to_dict function has been relocated from distutils2.util to distutils2.metadata to help improve organization.

files:
  distutils2/command/register.py
  distutils2/command/upload.py
  distutils2/metadata.py
  distutils2/tests/test_metadata.py
  distutils2/util.py

diff --git a/distutils2/command/register.py b/distutils2/command/register.py
--- a/distutils2/command/register.py
+++ b/distutils2/command/register.py
@@ -14,9 +14,9 @@
 
 from distutils2.command.cmd import Command
 from distutils2 import logger
-from distutils2.util import (metadata_to_dict, read_pypirc, generate_pypirc,
-                             DEFAULT_REPOSITORY, DEFAULT_REALM,
-                             get_pypirc_path)
+from distutils2.metadata import metadata_to_dict
+from distutils2.util import (read_pypirc, generate_pypirc, DEFAULT_REPOSITORY,
+                             DEFAULT_REALM, get_pypirc_path)
 
 class register(Command):
 
diff --git a/distutils2/command/upload.py b/distutils2/command/upload.py
--- a/distutils2/command/upload.py
+++ b/distutils2/command/upload.py
@@ -20,8 +20,8 @@
 from distutils2.errors import DistutilsOptionError
 from distutils2.util import spawn
 from distutils2.command.cmd import Command
-from distutils2.util import (metadata_to_dict, read_pypirc,
-                             DEFAULT_REPOSITORY, DEFAULT_REALM)
+from distutils2.metadata import metadata_to_dict
+from distutils2.util import read_pypirc, DEFAULT_REPOSITORY, DEFAULT_REALM
 
 
 class upload(Command):
diff --git a/distutils2/metadata.py b/distutils2/metadata.py
--- a/distutils2/metadata.py
+++ b/distutils2/metadata.py
@@ -39,8 +39,8 @@
     _HAS_DOCUTILS = False
 
 # public API of this module
-__all__ = ['Metadata', 'PKG_INFO_ENCODING',
-           'PKG_INFO_PREFERRED_VERSION']
+__all__ = ['Metadata', 'get_metadata_version', 'metadata_to_dict',
+           'PKG_INFO_ENCODING', 'PKG_INFO_PREFERRED_VERSION']
 
 # Encoding used for the PKG-INFO files
 PKG_INFO_ENCODING = 'utf-8'
@@ -137,6 +137,53 @@
     # default marker when 1.0 is disqualified
     return '1.2'
 
+
+def get_metadata_version(metadata):
+    """Return the Metadata-Version attribute
+
+    - *metadata* give a METADATA object
+    """
+    return metadata['Metadata-Version']
+
+
+def metadata_to_dict(metadata):
+    """Convert a metadata object to a dict
+
+    - *metadata* give a METADATA object
+    """
+    data = {
+        'metadata_version': metadata['Metadata-Version'],
+        'name': metadata['Name'],
+        'version': metadata['Version'],
+        'summary': metadata['Summary'],
+        'home_page': metadata['Home-page'],
+        'author': metadata['Author'],
+        'author_email': metadata['Author-email'],
+        'license': metadata['License'],
+        'description': metadata['Description'],
+        'keywords': metadata['Keywords'],
+        'platform': metadata['Platform'],
+        'classifier': metadata['Classifier'],
+        'download_url': metadata['Download-URL'],
+    }
+
+    if metadata['Metadata-Version'] == '1.2':
+        data['requires_dist'] = metadata['Requires-Dist']
+        data['requires_python'] = metadata['Requires-Python']
+        data['requires_external'] = metadata['Requires-External']
+        data['provides_dist'] = metadata['Provides-Dist']
+        data['obsoletes_dist'] = metadata['Obsoletes-Dist']
+        data['project_url'] = [','.join(url) for url in
+                               metadata['Project-URL']]
+
+    elif metadata['Metadata-Version'] == '1.1':
+        data['provides'] = metadata['Provides']
+        data['requires'] = metadata['Requires']
+        data['obsoletes'] = metadata['Obsoletes']
+
+    return data
+
+
 _ATTR2FIELD = {
     'metadata_version': 'Metadata-Version',
     'name': 'Name',
@@ -205,7 +252,6 @@
                  display_warnings=False):
         self._fields = {}
         self.display_warnings = display_warnings
-        self.version = None
         self.requires_files = []
         self.docutils_support = _HAS_DOCUTILS
         self.platform_dependent = platform_dependent
@@ -220,8 +266,7 @@
             self.update(mapping)
 
     def _set_best_version(self):
-        self.version = _best_version(self._fields)
-        self._fields['Metadata-Version'] = self.version
+        self._fields['Metadata-Version'] = _best_version(self._fields)
 
     def _write_field(self, file, name, value):
         file.write('%s: %s\n' % (name, value))
@@ -318,9 +363,9 @@
     def read_file(self, fileob):
         """Read the metadata values from a file object."""
         msg = message_from_file(fileob)
-        self.version = msg['metadata-version']
+        self._fields['Metadata-Version'] = msg['metadata-version']
 
-        for field in _version2fieldlist(self.version):
+        for field in _version2fieldlist(self['Metadata-Version']):
             if field in _LISTFIELDS:
                 # we can have multiple lines
                 values = msg.get_all(field)
@@ -344,7 +389,7 @@
     def write_file(self, fileobject):
         """Write the PKG-INFO format data to a file object."""
         self._set_best_version()
-        for field in _version2fieldlist(self.version):
+        for field in _version2fieldlist(self['Metadata-Version']):
             values = self.get(field)
             if field in _ELEMENTSFIELD:
                 self._write_field(fileobject, field, ','.join(values))
@@ -509,7 +554,7 @@
     # Mapping API
 
     def keys(self):
-        return _version2fieldlist(self.version)
+        return _version2fieldlist(self['Metadata-Version'])
 
     def values(self):
         return [self[key] for key in self.keys()]
diff --git a/distutils2/tests/test_metadata.py b/distutils2/tests/test_metadata.py
--- a/distutils2/tests/test_metadata.py
+++ b/distutils2/tests/test_metadata.py
@@ -4,7 +4,7 @@
 import platform
 from StringIO import StringIO
 
-from distutils2.metadata import (Metadata,
+from distutils2.metadata import (Metadata, get_metadata_version,
                                  PKG_INFO_PREFERRED_VERSION)
 from distutils2.tests import run_unittest, unittest
 from distutils2.tests.support import LoggingCatcher, WarningsCatcher
@@ -126,18 +126,23 @@
         del metadata['Obsoletes-Dist']
         metadata['Version'] = '1'
         self.assertEqual(metadata['Metadata-Version'], '1.0')
+        self.assertEqual(get_metadata_version(metadata), '1.0')
 
         PKG_INFO = os.path.join(os.path.dirname(__file__),
                                 'SETUPTOOLS-PKG-INFO')
         metadata.read_file(StringIO(open(PKG_INFO).read()))
         self.assertEqual(metadata['Metadata-Version'], '1.0')
+        self.assertEqual(get_metadata_version(metadata), '1.0')
 
         PKG_INFO = os.path.join(os.path.dirname(__file__),
                                 'SETUPTOOLS-PKG-INFO2')
         metadata.read_file(StringIO(open(PKG_INFO).read()))
         self.assertEqual(metadata['Metadata-Version'], '1.1')
+        self.assertEqual(get_metadata_version(metadata), '1.1')
 
-        metadata.version = '1.618'
+        # Update the _fields dict directly to prevent 'Metadata-Version'
+        # from being updated by the _set_best_version() method.
+        metadata._fields['Metadata-Version'] = '1.618'
         self.assertRaises(MetadataUnrecognizedVersionError, metadata.keys)
 
     # XXX Spurious Warnings were disabled
@@ -169,7 +174,7 @@
         metadata['Project-URL'] = [('one', 'http://ok')]
         self.assertEqual(metadata['Project-URL'],
                           [('one', 'http://ok')])
-        self.assertEqual(metadata.version, '1.2')
+        self.assertEqual(metadata['Metadata-Version'], '1.2')
 
     def test_check_version(self):
         metadata = Metadata()
@@ -244,9 +249,13 @@
     def test_best_choice(self):
         metadata = Metadata()
         metadata['Version'] = '1.0'
-        self.assertEqual(metadata.version, PKG_INFO_PREFERRED_VERSION)
+        self.assertEqual(metadata['Metadata-Version'],
+                         PKG_INFO_PREFERRED_VERSION)
+        self.assertEqual(get_metadata_version(metadata),
+                         PKG_INFO_PREFERRED_VERSION)
         metadata['Classifier'] = ['ok']
-        self.assertEqual(metadata.version, '1.2')
+        self.assertEqual(metadata['Metadata-Version'], '1.2')
+        self.assertEqual(get_metadata_version(metadata), '1.2')
 
     def test_project_urls(self):
         # project-url is a bit specific, make sure we write it
diff --git a/distutils2/util.py b/distutils2/util.py
--- a/distutils2/util.py
+++ b/distutils2/util.py
@@ -924,41 +924,6 @@
     return {}
 
 
-def metadata_to_dict(meta):
-    """XXX might want to move it to the Metadata class."""
-    data = {
-        'metadata_version': meta.version,
-        'name': meta['Name'],
-        'version': meta['Version'],
-        'summary': meta['Summary'],
-        'home_page': meta['Home-page'],
-        'author': meta['Author'],
-        'author_email': meta['Author-email'],
-        'license': meta['License'],
-        'description': meta['Description'],
-        'keywords': meta['Keywords'],
-        'platform': meta['Platform'],
-        'classifier': meta['Classifier'],
-        'download_url': meta['Download-URL'],
-    }
-
-    if meta.version == '1.2':
-        data['requires_dist'] = meta['Requires-Dist']
-        data['requires_python'] = meta['Requires-Python']
-        data['requires_external'] = meta['Requires-External']
-        data['provides_dist'] = meta['Provides-Dist']
-        data['obsoletes_dist'] = meta['Obsoletes-Dist']
-        data['project_url'] = [','.join(url) for url in
-                                meta['Project-URL']]
-
-    elif meta.version == '1.1':
-        data['provides'] = meta['Provides']
-        data['requires'] = meta['Requires']
-        data['obsoletes'] = meta['Obsoletes']
-
-    return data
-
-
 # utility functions for 2to3 support
 
 def run_2to3(files, doctests_only=False, fixer_names=None,

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


More information about the Python-checkins mailing list