Skip to content

Commit 2c433e2

Browse files
committed
Added API docs for versions submodule added in #65
Formatted docstrings to match sphinx Doogle style and added rst file to docs to display them.
1 parent dce6ac8 commit 2c433e2

File tree

3 files changed

+99
-21
lines changed

3 files changed

+99
-21
lines changed

docs/source/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,4 @@ Module and File Tools
6060
double_import_denier
6161
filewatcher
6262
modulewatcher
63+
versions

docs/source/api/versions.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
============================
2+
labscript_utils.versions
3+
============================
4+
5+
.. automodule:: labscript_utils.versions
6+
:members:
7+
:undoc-members:
8+
:private-members:

labscript_utils/versions.py

Lines changed: 90 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,20 @@ class BrokenInstall(RuntimeError):
4848

4949
def get_import_path(import_name):
5050
"""Get which entry in sys.path a module would be imported from, without importing
51-
it."""
51+
it.
52+
53+
Args:
54+
import_name (str): The module name.
55+
56+
Raises:
57+
ModuleNotFoundError: Raised if the module is not installed.
58+
NotImplementedError: Raised if the module is a "namespace package".
59+
Support for namepsace packages is not currently availabled.
60+
61+
Returns:
62+
str: The path to the folder containing the module.
63+
64+
"""
5265
spec = importlib.util.find_spec(import_name)
5366
if spec is None:
5467
raise ModuleNotFoundError(import_name)
@@ -66,8 +79,22 @@ def get_import_path(import_name):
6679

6780

6881
def _get_metadata_version(project_name, import_path):
69-
"""Return the metadata version for a package with the given project name located at
70-
the given import path, or None if there is no such package."""
82+
"""Gets the package metadata version.
83+
84+
Args:
85+
project_name (str): The package name (e.g. the name used when pip installing
86+
the package).
87+
import_path (str): The path to the folder containing the installed package.
88+
89+
Raises:
90+
:exc:`BrokenInstall`: Raised if the package installation is corrupted (multiple
91+
packages matching the given arguments were found). May occur if
92+
(un)installation for a particular package version only partially completed.
93+
94+
Returns:
95+
The metadata version for a package with the given project name located at
96+
the given import path, or None if there is no such package.
97+
"""
7198

7299
for finder in sys.meta_path:
73100
if hasattr(finder, 'find_distributions'):
@@ -84,8 +111,14 @@ def _get_metadata_version(project_name, import_path):
84111

85112

86113
def _get_literal_version(filename):
87-
"""Tokenize a source file and return any __version__ = <version> literal defined in
88-
it.
114+
"""Tokenize a source file and return any :code:`__version__ = <version>` literal
115+
defined in it.
116+
117+
Args:
118+
filename (str): The path to the file to tokenize.
119+
120+
Returns:
121+
Any version literal found matching the above criteria, or None.
89122
"""
90123
if not os.path.exists(filename):
91124
return None
@@ -110,17 +143,34 @@ def _get_literal_version(filename):
110143

111144

112145
def get_version(import_name, project_name=None, import_path=None):
113-
"""Try very hard to get the version of a package without importing it. if
114-
import_path is not given, first find where it would be imported from, without
146+
"""Try very hard to get the version of a package without importing it.
147+
148+
If import_path is not given, first find where it would be imported from, without
115149
importing it. Then look for metadata in the same import path with the given project
116150
name (note: this is not always the same as the import name, it is the name for
117151
example you would ask pip to install). If that is found, return the version info
118-
from it. Otherwise look for a __version__.py file in the package directory, or a
119-
__version__ = <version> literal defined in the package source (without executing
120-
it).
152+
from it. Otherwise look for a :code:`__version__.py` file in the package directory,
153+
or a :code:`__version__ = <version>` literal defined in the package source (without
154+
executing it).
155+
156+
Args:
157+
import_name (str): The module name.
158+
project_name (str, optional): The package name (e.g. the name used when pip
159+
installing the package). This must be specified if it does not match the
160+
module name.
161+
import_path (str, optional): The path to the folder containing the installed
162+
package.
163+
164+
Raises:
165+
NotImplementedError: Raised if the module name contains a period. Only
166+
top-level packages are supported at this time.
121167
122-
Return NotFound if the package cannot be found, and NoVersionInfo if the version
123-
cannot be obtained in the above way, or if it was found but was None."""
168+
Returns:
169+
The version literal of the package.
170+
If the package cannot be found, :class:`NotFound` is returned.
171+
If the version cannot be obtained in the above way, or if the version was found
172+
but was :code:`None`, :class:`NoVersionInfo` is returned.
173+
"""
124174
if project_name is None:
125175
project_name = import_name
126176
if '.' in import_name:
@@ -162,15 +212,34 @@ def get_version(import_name, project_name=None, import_path=None):
162212

163213

164214
def check_version(module_name, at_least, less_than, version=None, project_name=None):
165-
"""Check that the version of the given module is at least and less than the given
166-
version strings, and raise VersionException if not. Raise VersionException if the
167-
module was not found or its version could not be determined. This function uses
168-
get_version to determine version numbers without importing modules. In order to do
169-
this, project_name must be provided if it differs from module_name. For example,
170-
pyserial is imported as 'serial', but the project name, as passed to a 'pip install'
171-
command, is 'pyserial'. Therefore to check the version of pyserial, pass in
172-
module_name='serial' and project_name='pyserial'. You can also pass in a version
173-
string yourself, in which case no inspection of packages will take place.
215+
"""Checks if a module version is within specified bounds.
216+
217+
Checks that the version of the given module is at least and less than the given
218+
version strings. This function uses :func:`get_version` to determine version
219+
numbers without importing modules. In order to do this, :code:`project_name` must
220+
be provided if it differs from :code:`module_name`. For example, pyserial is
221+
imported as 'serial', but the project name, as passed to a 'pip install' command,
222+
is 'pyserial'. Therefore to check the version of pyserial, pass in
223+
:code:`module_name='serial'` and :code:`project_name='pyserial'`.
224+
You can also pass in a version string yourself, in which case no inspection of
225+
packages will take place.
226+
227+
Args:
228+
module_name (str): The name of the module to check.
229+
at_least (str): The minimum acceptable module version.
230+
less_than (str): The minimum unacceptable module version. Usually this would be
231+
the next major version if the package follows
232+
`semver <https://semver.org>`_.
233+
version (str, optional): The current version of the installed package. Useful when the
234+
package version is stored in a non-standard location.
235+
project_name (str, optional): The package name (e.g. the name used when pip
236+
installing the package). This must be specified if it does not match the
237+
module name.
238+
239+
Raises:
240+
:exc:`VersionException`: if the module was not found or its version could not
241+
be determined.
242+
174243
"""
175244
if version is None:
176245
version = get_version(module_name, project_name)

0 commit comments

Comments
 (0)