summaryrefslogtreecommitdiffstats
path: root/Doc/library/packaging.metadata.rst
blob: 332d69df4f7bda861815e828b6eb52463ceb84ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
:mod:`packaging.metadata` --- Metadata handling
===============================================

.. module:: packaging.metadata
   :synopsis: Class holding the metadata of a release.


.. TODO use sphinx-autogen to generate basic doc from the docstrings

.. class:: Metadata

   This class can read and write metadata files complying with any of the
   defined versions: 1.0 (:PEP:`241`), 1.1 (:PEP:`314`) and 1.2 (:PEP:`345`).  It
   implements methods to parse Metadata files and write them, and a mapping
   interface to its contents.

   The :PEP:`345` implementation supports the micro-language for the environment
   markers, and displays warnings when versions that are supposed to be
   :PEP:`386`-compliant are violating the specification.


Reading metadata
----------------

The :class:`Metadata` class can be instantiated
with the path of the metadata file, and provides a dict-like interface to the
values::

   >>> from packaging.metadata import Metadata
   >>> metadata = Metadata('PKG-INFO')
   >>> metadata.keys()[:5]
   ('Metadata-Version', 'Name', 'Version', 'Platform', 'Supported-Platform')
   >>> metadata['Name']
   'CLVault'
   >>> metadata['Version']
   '0.5'
   >>> metadata['Requires-Dist']
   ["pywin32; sys.platform == 'win32'", "Sphinx"]


The fields that support environment markers can be automatically ignored if
the object is instantiated using the ``platform_dependent`` option.
:class:`Metadata` will interpret in this case
the markers and will automatically remove the fields that are not compliant
with the running environment. Here's an example under Mac OS X. The win32
dependency we saw earlier is ignored::

   >>> from packaging.metadata import Metadata
   >>> metadata = Metadata('PKG-INFO', platform_dependent=True)
   >>> metadata['Requires-Dist']
   ['Sphinx']


If you want to provide your own execution context, let's say to test the
metadata under a particular environment that is not the current environment,
you can provide your own values in the ``execution_context`` option, which
is the dict that may contain one or more keys of the context the micro-language
expects.

Here's an example, simulating a win32 environment::

   >>> from packaging.metadata import Metadata
   >>> context = {'sys.platform': 'win32'}
   >>> metadata = Metadata('PKG-INFO', platform_dependent=True,
   ...                     execution_context=context)
   ...
   >>> metadata['Requires-Dist'] = ["pywin32; sys.platform == 'win32'",
   ...                              "Sphinx"]
   ...
   >>> metadata['Requires-Dist']
   ['pywin32', 'Sphinx']


Writing metadata
----------------

Writing metadata can be done using the ``write`` method::

   >>> metadata.write('/to/my/PKG-INFO')

The class will pick the best version for the metadata, depending on the values
provided. If all the values provided exist in all versions, the class will
use :attr:`PKG_INFO_PREFERRED_VERSION`.  It is set by default to 1.0, the most
widespread version.


Conflict checking and best version
----------------------------------

Some fields in :PEP:`345` have to comply with the version number specification
defined in :PEP:`386`.  When they don't comply, a warning is emitted::

   >>> from packaging.metadata import Metadata
   >>> metadata = Metadata()
   >>> metadata['Requires-Dist'] = ['Funky (Groovie)']
   "Funky (Groovie)" is not a valid predicate
   >>> metadata['Requires-Dist'] = ['Funky (1.2)']

See also :mod:`packaging.version`.


.. TODO talk about check()


:mod:`packaging.markers` --- Environment markers
================================================

.. module:: packaging.markers
   :synopsis: Micro-language for environment markers


This is an implementation of environment markers `as defined in PEP 345
<http://www.python.org/dev/peps/pep-0345/#environment-markers>`_.  It is used
for some metadata fields.

.. function:: interpret(marker, execution_context=None)

   Interpret a marker and return a boolean result depending on the environment.
   Example:

      >>> interpret("python_version > '1.0'")
      True