[Python-checkins] distutils2: Improve Metadata instantiation
tarek.ziade
python-checkins at python.org
Sun Aug 8 11:50:47 CEST 2010
tarek.ziade pushed fdd3e43969eb to distutils2:
http://hg.python.org/distutils2/rev/fdd3e43969eb
changeset: 477:fdd3e43969eb
user: ?ric Araujo <merwok at netwok.org>
date: Thu Aug 05 17:25:42 2010 +0200
summary: Improve Metadata instantiation
files: src/distutils2/metadata.py, src/distutils2/tests/test_metadata.py
diff --git a/src/distutils2/metadata.py b/src/distutils2/metadata.py
--- a/src/distutils2/metadata.py
+++ b/src/distutils2/metadata.py
@@ -182,23 +182,30 @@
class DistributionMetadata(object):
"""The metadata of a release.
- Supports versions 1.0, 1.1 and 1.2 (auto-detected).
+ Supports versions 1.0, 1.1 and 1.2 (auto-detected). You can
+ instantiate the class with one of these arguments (or none):
+ - *path*, the path to a METADATA file
+ - *fileobj* give a file-like object with METADATA as content
+ - *mapping* is a dict-like object
+ """
+ # TODO document that execution_context and platform_dependent are used
+ # to filter on query, not when setting a key
+ # also document the mapping API and UNKNOWN default key
- 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, mapping=None):
self._fields = {}
self.version = None
self.docutils_support = _HAS_DOCUTILS
self.platform_dependent = platform_dependent
+ self.execution_context = execution_context
+ if [path, fileobj, mapping].count(None) < 2:
+ raise TypeError('path, fileobj and mapping are exclusive')
if path is not None:
self.read(path)
elif fileobj is not None:
self.read_file(fileobj)
- self.execution_context = execution_context
- if mapping:
+ elif mapping is not None:
self.update(mapping)
def _set_best_version(self):
@@ -329,7 +336,7 @@
def update(self, other=None, **kwargs):
"""Set metadata values from the given mapping
-
+
Convert the keys to Metadata fields. Given keys that don't match a
metadata argument will not be used.
diff --git a/src/distutils2/tests/test_metadata.py b/src/distutils2/tests/test_metadata.py
--- a/src/distutils2/tests/test_metadata.py
+++ b/src/distutils2/tests/test_metadata.py
@@ -13,6 +13,36 @@
class DistributionMetadataTestCase(LoggingSilencer, unittest.TestCase):
+ def test_instantiation(self):
+ PKG_INFO = os.path.join(os.path.dirname(__file__), 'PKG-INFO')
+ fp = open(PKG_INFO)
+ try:
+ contents = fp.read()
+ finally:
+ fp.close()
+ fp = StringIO(contents)
+
+ m = DistributionMetadata()
+ self.assertRaises(MetadataUnrecognizedVersionError, m.items)
+
+ m = DistributionMetadata(PKG_INFO)
+ self.assertEqual(len(m.items()), 22)
+
+ m = DistributionMetadata(fileobj=fp)
+ self.assertEqual(len(m.items()), 22)
+
+ m = DistributionMetadata(mapping=dict(name='Test', version='1.0'))
+ self.assertEqual(len(m.items()), 11)
+
+ d = dict(m.items())
+ self.assertRaises(TypeError, DistributionMetadata,
+ PKG_INFO, fileobj=fp)
+ self.assertRaises(TypeError, DistributionMetadata,
+ PKG_INFO, mapping=d)
+ self.assertRaises(TypeError, DistributionMetadata,
+ fileobj=fp, mapping=d)
+ self.assertRaises(TypeError, DistributionMetadata,
+ PKG_INFO, mapping=m, fileobj=fp)
def test_interpret(self):
sys_platform = sys.platform
@@ -114,15 +144,15 @@
metadata.read_file(out)
self.assertEqual(wanted, metadata['Description'])
- def test_mapper_apis(self):
+ def test_mapping_api(self):
PKG_INFO = os.path.join(os.path.dirname(__file__), 'PKG-INFO')
content = open(PKG_INFO).read()
content = content % sys.platform
- metadata = DistributionMetadata()
- metadata.read_file(StringIO(content))
+ metadata = DistributionMetadata(fileobj=StringIO(content))
self.assertIn('Version', metadata.keys())
self.assertIn('0.5', metadata.values())
self.assertIn(('Version', '0.5'), metadata.items())
+ #TODO test update
def test_versions(self):
metadata = DistributionMetadata()
--
Repository URL: http://hg.python.org/distutils2
More information about the Python-checkins
mailing list