Éric Araujo | 3a9f58f | 2011-06-01 20:42:49 +0200 | [diff] [blame] | 1 | :mod:`packaging.pypi.dist` --- Classes representing query results |
| 2 | ================================================================= |
| 3 | |
| 4 | .. module:: packaging.pypi.dist |
| 5 | :synopsis: Classes representing the results of queries to indexes. |
| 6 | |
| 7 | |
| 8 | Information coming from the indexes is held in instances of the classes defined |
| 9 | in this module. |
| 10 | |
| 11 | Keep in mind that each project (eg. FooBar) can have several releases |
| 12 | (eg. 1.1, 1.2, 1.3), and each of these releases can be provided in multiple |
| 13 | distributions (eg. a source distribution, a binary one, etc). |
| 14 | |
| 15 | |
| 16 | ReleaseInfo |
| 17 | ----------- |
| 18 | |
| 19 | Each release has a project name, version, metadata, and related distributions. |
| 20 | |
| 21 | This information is stored in :class:`ReleaseInfo` |
| 22 | objects. |
| 23 | |
| 24 | .. class:: ReleaseInfo |
| 25 | |
| 26 | |
| 27 | DistInfo |
| 28 | --------- |
| 29 | |
| 30 | :class:`DistInfo` is a simple class that contains |
| 31 | information related to distributions; mainly the URLs where distributions |
| 32 | can be found. |
| 33 | |
| 34 | .. class:: DistInfo |
| 35 | |
| 36 | |
| 37 | ReleasesList |
| 38 | ------------ |
| 39 | |
| 40 | The :mod:`~packaging.pypi.dist` module provides a class which works |
| 41 | with lists of :class:`ReleaseInfo` classes; |
| 42 | used to filter and order results. |
| 43 | |
| 44 | .. class:: ReleasesList |
| 45 | |
| 46 | |
| 47 | Example usage |
| 48 | ------------- |
| 49 | |
| 50 | Build a list of releases and order them |
| 51 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 52 | |
| 53 | Assuming we have a list of releases:: |
| 54 | |
| 55 | >>> from packaging.pypi.dist import ReleasesList, ReleaseInfo |
| 56 | >>> fb10 = ReleaseInfo("FooBar", "1.0") |
| 57 | >>> fb11 = ReleaseInfo("FooBar", "1.1") |
| 58 | >>> fb11a = ReleaseInfo("FooBar", "1.1a1") |
| 59 | >>> ReleasesList("FooBar", [fb11, fb11a, fb10]) |
| 60 | >>> releases.sort_releases() |
| 61 | >>> releases.get_versions() |
| 62 | ['1.1', '1.1a1', '1.0'] |
| 63 | >>> releases.add_release("1.2a1") |
| 64 | >>> releases.get_versions() |
| 65 | ['1.1', '1.1a1', '1.0', '1.2a1'] |
| 66 | >>> releases.sort_releases() |
| 67 | ['1.2a1', '1.1', '1.1a1', '1.0'] |
| 68 | >>> releases.sort_releases(prefer_final=True) |
| 69 | >>> releases.get_versions() |
| 70 | ['1.1', '1.0', '1.2a1', '1.1a1'] |
| 71 | |
| 72 | |
| 73 | Add distribution related information to releases |
| 74 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 75 | |
| 76 | It's easy to add distribution information to releases:: |
| 77 | |
| 78 | >>> from packaging.pypi.dist import ReleasesList, ReleaseInfo |
| 79 | >>> r = ReleaseInfo("FooBar", "1.0") |
| 80 | >>> r.add_distribution("sdist", url="http://example.org/foobar-1.0.tar.gz") |
| 81 | >>> r.dists |
| 82 | {'sdist': FooBar 1.0 sdist} |
| 83 | >>> r['sdist'].url |
| 84 | {'url': 'http://example.org/foobar-1.0.tar.gz', 'hashname': None, 'hashval': |
| 85 | None, 'is_external': True} |
| 86 | |
| 87 | |
| 88 | Getting attributes from the dist objects |
| 89 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 90 | |
| 91 | To abstract querying information returned from the indexes, attributes and |
| 92 | release information can be retrieved directly from dist objects. |
| 93 | |
| 94 | For instance, if you have a release instance that does not contain the metadata |
| 95 | attribute, it can be fetched by using the "fetch_metadata" method:: |
| 96 | |
| 97 | >>> r = Release("FooBar", "1.1") |
| 98 | >>> print r.metadata |
| 99 | None # metadata field is actually set to "None" |
| 100 | >>> r.fetch_metadata() |
| 101 | <Metadata for FooBar 1.1> |
| 102 | |
| 103 | .. XXX add proper roles to these constructs |
| 104 | |
| 105 | |
| 106 | It's possible to retrieve a project's releases (`fetch_releases`), |
| 107 | metadata (`fetch_metadata`) and distributions (`fetch_distributions`) using |
| 108 | a similar work flow. |
| 109 | |
| 110 | .. XXX what is possible? |
| 111 | |
| 112 | Internally, this is possible because while retrieving information about |
| 113 | projects, releases or distributions, a reference to the client used is |
| 114 | stored which can be accessed using the objects `_index` attribute. |