Debian Codegraph @

testing/web-platform/tests/tools/third_party/packaging/docs/markers.rst

raw
1 Markers
2 =======
3
4 .. currentmodule:: packaging.markers
5
6 One extra requirement of dealing with dependencies is the ability to specify
7 if it is required depending on the operating system or Python version in use.
8 `PEP 508`_ defines the scheme which has been implemented by this module.
9
10 Usage
11 -----
12
13 .. doctest::
14
15 >>> from packaging.markers import Marker, UndefinedEnvironmentName
16 >>> marker = Marker("python_version>'2'")
17 >>> marker
18 <Marker('python_version > "2"')>
19 >>> # We can evaluate the marker to see if it is satisfied
20 >>> marker.evaluate()
21 True
22 >>> # We can also override the environment
23 >>> env = {'python_version': '1.5.4'}
24 >>> marker.evaluate(environment=env)
25 False
26 >>> # Multiple markers can be ANDed
27 >>> and_marker = Marker("os_name=='a' and os_name=='b'")
28 >>> and_marker
29 <Marker('os_name == "a" and os_name == "b"')>
30 >>> # Multiple markers can be ORed
31 >>> or_marker = Marker("os_name=='a' or os_name=='b'")
32 >>> or_marker
33 <Marker('os_name == "a" or os_name == "b"')>
34 >>> # Markers can be also used with extras, to pull in dependencies if
35 >>> # a certain extra is being installed
36 >>> extra = Marker('extra == "bar"')
37 >>> # Evaluating an extra marker with no environment is an error
38 >>> try:
39 ... extra.evaluate()
40 ... except UndefinedEnvironmentName:
41 ... pass
42 >>> extra_environment = {'extra': ''}
43 >>> extra.evaluate(environment=extra_environment)
44 False
45 >>> extra_environment['extra'] = 'bar'
46 >>> extra.evaluate(environment=extra_environment)
47 True
48
49
50 Reference
51 ---------
52
53 .. class:: Marker(markers)
54
55 This class abstracts handling markers for dependencies of a project. It can
56 be passed a single marker or multiple markers that are ANDed or ORed
57 together. Each marker will be parsed according to PEP 508.
58
59 :param str markers: The string representation of a marker or markers.
60 :raises InvalidMarker: If the given ``markers`` are not parseable, then
61 this exception will be raised.
62
63 .. method:: evaluate(environment=None)
64
65 Evaluate the marker given the context of the current Python process.
66
67 :param dict environment: A dictionary containing keys and values to
68 override the detected environment.
69 :raises: UndefinedComparison: If the marker uses a PEP 440 comparison on
70 strings which are not valid PEP 440 versions.
71 :raises: UndefinedEnvironmentName: If the marker accesses a value that
72 isn't present inside of the environment
73 dictionary.
74
75 .. exception:: InvalidMarker
76
77 Raised when attempting to create a :class:`Marker` with a string that
78 does not conform to PEP 508.
79
80
81 .. exception:: UndefinedComparison
82
83 Raised when attempting to evaluate a :class:`Marker` with a PEP 440
84 comparison operator against values that are not valid PEP 440 versions.
85
86
87 .. exception:: UndefinedEnvironmentName
88
89 Raised when attempting to evaluate a :class:`Marker` with a value that is
90 missing from the evaluation environment.
91
92
93 .. _`PEP 508`: https://www.python.org/dev/peps/pep-0508/