Welcome to sphobjinv!¶
A toolkit for inspection/manipulation of Sphinx objects inventories
When documentation is built using, e.g., Sphinx’s StandaloneHTMLBuilder,
an inventory of the named objects in the documentation set is dumped
to a file called objects.inv in the html build directory.
(One common location is, doc/build/html, though the exact location will vary
depending on the details of how Sphinx is configured.) This file is read by intersphinx when
generating links in other documentation.
Since version 1.0 of Sphinx (~July 2010), the data in these objects.inv inventories is compressed by
zlib (presumably to reduce storage requirements and improve download speeds; “version 2”),
whereas prior to that date the data was left uncompressed (“version 1”). This compression renders
the files non-human-readable. It is the purpose of this package to enable quick and simple
compression/decompression and inspection of these “version 2” inventory files.
In particular, sphobjinv was developed to satisfy two primary use cases:
- Searching and inspection of objects.inv contents in order to identify how to properly construct
intersphinxreferences.
- Assembly of new objects.inv files in order to allow
intersphinxcross-referencing of other documentation sets that were not created by Sphinx.
Install sphobjinv via pip:
$ pip install sphobjinv
The package is configured for use both as a command-line script and as a Python package.
Installing the optional dependency python-Levenshtein substantially
accelerates the the “suggest” functionality; see
here for more information.
The project source repository is on GitHub: bskinn/sphobjinv.
Command-Line Usage¶
The CLI for sphobjinv is implemented using two subparsers, one each for the
convert and suggest sub-functions.
More information about the implementation of these features can be
found here and in the documentation for the
Inventory object, in particular the
data_file() and
suggest() methods.
Some notes on these CLI docs:
- CLI examples are executed in a sandboxed directory pre-loaded with objects_attrs.inv (from, e.g., here).
Path(frompathlib) is imported into the namespace before all tests.- cli_run is a helper function that enables doctesting of CLI examples by mimicking execution of a shell command. It is described in more detail here.
- file_head is a helper function that retrieves the head of a specified file.
The options for the parent sphobjinv command are:
-
-h,--help¶ Show help message and exit
>>> cli_run('sphobjinv --help')
usage: sphobjinv [-h] [-v] {convert,suggest} ...
Format conversion for and introspection of intersphinx 'objects.inv' files.
optional arguments:
-h, --help show this help message and exit
-v, --version Print package version & other info
Subcommands:
{convert,suggest} Execution mode. Type 'sphobjinv [mode] -h' for more
information on available options. Mode names can be
abbreviated to their first two letters.
convert (co) Convert intersphinx inventory to zlib-compressed,
plaintext, or JSON formats.
suggest (su) Fuzzy-search intersphinx inventory for desired object(s).
-
-v,--version¶ Print package version & other info
>>> cli_run('sphobjinv --version')
sphobjinv v2.0
Copyright (c) Brian Skinn 2016-2018
License: The MIT License
Bug reports & feature requests: https://github.com/bskinn/sphobjinv
Documentation: http://sphobjinv.readthedocs.io
Command-Line Usage: “convert” Mode¶
The convert subparser is used for all conversions of
“version 2” Sphinx inventory
files among plaintext, zlib-compressed, and (unique to sphobjinv) JSON formats.
Currently, the sphobjinv CLI can read inventory data from local files
in any of these three formats, as well as the standard zlib-compressed format
from files in remote locations (see --url).
At the moment, the only output
method supported is writing to a local file, again in all three formats.
Note
If reading from stdin or writing to stdout would be useful to you, please leave a note at #74 so I can gauge interest.
Basic file conversion to the default output filename is straightforward:
>>> Path('objects_attrs.txt').is_file()
False
>>> cli_run('sphobjinv convert plain objects_attrs.inv')
Conversion completed.
'...objects_attrs.inv' converted to '...objects_attrs.txt' (plain).
>>> print(file_head('objects_attrs.txt', head=6))
# Sphinx inventory version 2
# Project: attrs
# Version: 17.2
# The remainder of this file is compressed using zlib.
attr.Attribute py:class 1 api.html#$ -
attr.Factory py:class 1 api.html#$ -
A different target filename can be specified, to avoid overwriting an existing file:
>>> cli_run('sphobjinv convert plain objects_attrs.inv', inp='n\n')
File exists. Overwrite (Y/N)? n
Exiting...
>>> cli_run('sphobjinv convert plain objects_attrs.inv objects_attrs_foo.txt')
Conversion completed.
'...objects_attrs.inv' converted to '...objects_attrs_foo.txt' (plain).
If you don’t provide an output file extension, the sphobjinv defaults (.inv/.txt/.json) will be used.
If you want to pull an input file directly from the Web, use
--url (note that the base filename is not inferred from the
indicated URL):
>>> cli_run('sphobjinv convert plain -u https://github.com/bskinn/sphobjinv/raw/dev/sphobjinv/test/resource/objects_attrs.inv')
Remote inventory found.
Conversion completed.
'https://github.com/b[...]ce/objects_attrs.inv' converted to '...objects.txt' (plain).
>>> print(file_head('objects.txt', head=6))
# Sphinx inventory version 2
# Project: attrs
# Version: 17.2
# The remainder of this file is compressed using zlib.
attr.Attribute py:class 1 api.html#$ -
attr.Factory py:class 1 api.html#$ -
The URL provided MUST have the leading protocol specified (here, https://).
It is not necessary to locate the objects.inv file before running sphobjinv; for most Sphinx documentation sets, if you provide a URL to any page in the docs, it will automatically find and use the correct objects.inv:
>>> cli_run('sphobjinv convert plain -ou https://docs.python.org/3/library/urllib.error.html#urllib.error.URLError')
No inventory at provided URL.
Attempting "https://docs.python.org/3/library/urllib.error.html/objects.inv" ...
Attempting "https://docs.python.org/3/library/objects.inv" ...
Attempting "https://docs.python.org/3/objects.inv" ...
Remote inventory found.
Conversion completed.
'...objects.inv' converted to '...objects.txt' (plain).
sphobjinv only supports download of zlib-compressed objects.inv files by URL. Plaintext download by URL is unreliable, presumably due to encoding problems. If download of JSON files by URL is desirable, please submit an issue.
Usage
>>> cli_run('sphobjinv convert --help', head=4)
usage: sphobjinv convert [-h] [-e | -c] [-o] [-q] [-u]
{zlib,plain,json} infile [outfile]
Convert intersphinx inventory to zlib-compressed, plaintext, or JSON formats.
Positional Arguments
-
mode¶ Conversion output format.
Must be one of plain, zlib, or json
-
outfile¶ (Optional) Path to desired output file. Defaults to same directory and main file name as input file but with extension .inv/.txt/.json, as appropriate for the output format. A bare path is accepted here, using the default output file name/extension.
Flags
-
-h,--help¶ Display convert help message and exit.
-
-o,--overwrite¶ If the output file already exists, overwrite without prompting for confirmation.
-
-q,--quiet¶ Suppress all output to stdout, regardless of success or failure. Useful for scripting/automation. Implies
--overwrite.
-
-e,--expand¶ Expand any abbreviations in uri or dispname fields before writing to output; see here. Cannot be specified with
--contract.
Command-Line Usage: “suggest” Mode¶
The suggest subparser is used to query an inventory for objects
fuzzy-matching a given search string. Fuzzy-matching is carried out via the
fuzzywuzzy library, against the Restructured Text-like representation of each
object exposed by SuperDataObj.as_rst:
>>> cli_run('sphobjinv suggest objects_attrs.inv instance')
:py:exception:`attr.exceptions.FrozenInstanceError`
:py:function:`attr.validators.instance_of`
The fuzzywuzzy match score and the index of the object within the inventory can
be printed by passing the --score and --index options,
respectively:
>>> cli_run('sphobjinv suggest objects_attrs.inv instance -s -i')
Name Score Index
----------------------------------------------------- ------- -------
:py:exception:`attr.exceptions.FrozenInstanceError` 90 9
:py:function:`attr.validators.instance_of` 90 23
If too few or too many matches are returned, the reporting threshold can be changed
via --thresh:
>>> cli_run('sphobjinv suggest objects_attrs.inv instance -s -i -t 48')
Name Score Index
----------------------------------------------------- ------- -------
:py:exception:`attr.exceptions.FrozenInstanceError` 90 9
:py:function:`attr.validators.instance_of` 90 23
:std:doc:`license` 51 47
:py:function:`attr.filters.include` 48 13
Remote objects.inv files can be retrieved for inspection by passing the
--url flag:
>>> cli_run('sphobjinv suggest https://github.com/bskinn/sphobjinv/raw/dev/sphobjinv/test/resource/objects_attrs.inv instance -u -t 48')
Remote inventory found.
:py:exception:`attr.exceptions.FrozenInstanceError`
:py:function:`attr.validators.instance_of`
:std:doc:`license`
:py:function:`attr.filters.include`
The URL provided MUST have the leading protocol specified (here, https://).
It is not necessary to locate the objects.inv file before running sphobjinv; for most Sphinx documentation sets, if you provide a URL to any page in the docs, it will automatically find and use the correct objects.inv:
>>> cli_run('sphobjinv suggest -u https://sphobjinv.readthedocs.io/en/v2.0rc1/cmdline.html compress')
No inventory at provided URL.
Attempting "https://sphobjinv.readthedocs.io/en/v2.0rc1/cmdline.html/objects.inv" ...
Attempting "https://sphobjinv.readthedocs.io/en/v2.0rc1/objects.inv" ...
Remote inventory found.
:py:function:`sphobjinv.zlib.compress`
:py:function:`sphobjinv.zlib.decompress`
sphobjinv only supports download of zlib-compressed objects.inv files by URL. Plaintext download by URL is unreliable, presumably due to encoding problems. If download of JSON files by URL is desirable, please submit an issue.
Usage
>>> cli_run('sphobjinv suggest --help', head=3)
usage: sphobjinv suggest [-h] [-a] [-i] [-s] [-t {0-100}] [-u] infile search
Fuzzy-search intersphinx inventory for desired object(s).
Positional Arguments
-
search¶ Search term for
fuzzywuzzymatching
Flags
-
-h,--help¶ Display suggest help message and exit.
-
-a,--all¶ Display all search results without prompting, regardless of the number of hits. Otherwise, prompt if number of results exceeds
sphobjinv.cmdline.SUGGEST_CONFIRM_LENGTH.
-
-i,--index¶ Display the index position within the
Inventory.objectslist for each search result returned.
-
-s,--score¶ Display the
fuzzywuzzymatch score for each search result returned.
-
-t,--thresh<#>¶ Change the
fuzzywuzzymatch quality threshold (0-100; higher values yield fewer results). Default is specified insphobjinv.cmdline.DEF_THRESH.
API Usage¶
In all of the below, the sphobjinv package has been imported as soi, and the working temp directory has been populated with the objects_attrs.inv inventory.
Inspecting an Inventory¶
Inspecting the contents of an existing inventory is handled entirely by the
Inventory class:
>>> inv = soi.Inventory('objects_attrs.inv')
>>> print(inv)
<Inventory (fname_zlib): attrs v17.2, 56 objects>
>>> inv.version
'17.2'
>>> inv.count
56
The individual objects contained in the inventory are represented by instances
of the DataObjStr class, which are stored in
a list in the objects attribute:
>>> len(inv.objects)
56
>>> dobj = inv.objects[0]
>>> dobj
DataObjStr(name='attr.Attribute', domain='py', role='class', priority='1', uri='api.html#$', dispname='-')
>>> dobj.name
'attr.Attribute'
>>> dobj.domain
'py'
>>> [d.name for d in inv.objects if 'validator' in d.uri]
['api_validators', 'examples_validators']
Inventory objects can also import from plaintext or zlib-compressed
inventories, as bytes:
>>> inv2 = soi.Inventory(inv.data_file())
>>> print(inv2)
<Inventory (bytes_plain): attrs v17.2, 56 objects>
>>> inv3 = soi.Inventory(soi.compress(inv.data_file()))
>>> print(inv3)
<Inventory (bytes_zlib): attrs v17.2, 56 objects>
Remote objects.inv files can also be retrieved via URL, with the url keyword argument:
>>> inv4 = soi.Inventory(url='https://github.com/bskinn/sphobjinv/raw/dev/sphobjinv/test/resource/objects_attrs.inv')
>>> print(inv4)
<Inventory (url): attrs v17.2, 56 objects>
Modifying an Inventory¶
The DataObjStr instances can be edited in place:
>>> inv = soi.Inventory('objects_attrs.inv')
>>> inv.objects[0]
DataObjStr(name='attr.Attribute', domain='py', role='class', priority='1', uri='api.html#$', dispname='-')
>>> inv.objects[0].uri = 'attribute.html'
>>> inv.objects[0]
DataObjStr(name='attr.Attribute', domain='py', role='class', priority='1', uri='attribute.html', dispname='-')
New instances can be easily created either by direct instantiation, or by
evolve():
>>> inv.objects.append(inv.objects[0].evolve(name='attr.Generator', uri='generator.html'))
>>> inv.count
57
>>> inv.objects[-1]
DataObjStr(name='attr.Generator', domain='py', role='class', priority='1', uri='generator.html', dispname='-')
The other attributes of the Inventory instance can also be freely modified:
>>> inv.project = 'not_attrs'
>>> inv.version = '0.1'
>>> print(inv)
<Inventory (fname_zlib): not_attrs v0.1, 57 objects>
Formatting Inventory Contents¶
The contents of the Inventory can be converted to
the plaintext objects.inv format as bytes via data_file():
>>> inv = soi.Inventory('objects_attrs.inv')
>>> print(*inv.data_file().splitlines()[:6], sep='\n')
b'# Sphinx inventory version 2'
b'# Project: attrs'
b'# Version: 17.2'
b'# The remainder of this file is compressed using zlib.'
b'attr.Attribute py:class 1 api.html#$ -'
b'attr.Factory py:class 1 api.html#$ -'
This method makes use of the DataObjStr.data_line
method to format each of the object information lines.
If desired, the shorthand used for the
uri and
dispname fields can be expanded:
>>> print(*inv.data_file(expand=True).splitlines()[4:6], sep='\n')
b'attr.Attribute py:class 1 api.html#attr.Attribute attr.Attribute'
b'attr.Factory py:class 1 api.html#attr.Factory attr.Factory'
>>> do = inv.objects[0]
>>> do.data_line(expand=True)
'attr.Attribute py:class 1 api.html#attr.Attribute attr.Attribute'
Exporting an Inventory¶
Inventory instances can be written to disk
in three formats: zlib-compressed objects.inv,
plaintext objects.txt, and JSON. The API does not provide single-function
means to do this, however.
To start, load the source objects.inv:
>>> from pathlib import Path
>>> inv = soi.Inventory('objects_attrs.inv')
To export plaintext:
>>> df = inv.data_file()
>>> soi.writebytes('objects_attrs.txt', df)
>>> print(*Path('objects_attrs.txt').read_text().splitlines()[:6], sep='\n')
# Sphinx inventory version 2
# Project: attrs
# Version: 17.2
# The remainder of this file is compressed using zlib.
attr.Attribute py:class 1 api.html#$ -
attr.Factory py:class 1 api.html#$ -
For zlib-compressed:
>>> dfc = soi.compress(df)
>>> soi.writebytes('objects_attrs_new.inv', dfc)
>>> print(*Path('objects_attrs_new.inv').read_bytes().splitlines()[:4], sep='\n')
b'# Sphinx inventory version 2'
b'# Project: attrs'
b'# Version: 17.2'
b'# The remainder of this file is compressed using zlib.'
>>> print(Path('objects_attrs_new.inv').read_bytes().splitlines()[6][:10])
b'5\xcb0\xd7\x9f>\xf3\x84\x89'
For JSON:
>>> jd = inv.json_dict()
>>> soi.writejson('objects_attrs.json', jd)
>>> print(Path('objects_attrs.json').read_text()[:51])
{"project": "attrs", "version": "17.2", "count": 56
Creating and Using a Custom objects.inv¶
The workflow presented here is introduced in the context of manually assembling an objects inventory, but the functionality is mainly intended for use downstream of a web-scraping or other automated content-extraction tool.
A (possibly obsolete) representative example of such a custom objects.inv can be found at the GitHub repo here.
Note
These instructions are for sphobjinv v2.0; the prior instructions for v1.0 can be found here.
Identify the head of the URI to the documentation.
Construct an
Inventorycontaining all of the objects of interest. Theurianddispnamevalues can be entered with or without the standard abbreviations.Create an empty
Inventory:>>> import sphobjinv as soi >>> inv = soi.Inventory() >>> print(inv) <Inventory (manual): None vNone, 0 objects>
Define the
projectandversionattributes:>>> inv.project = 'foobar' >>> inv.version = '1.5' >>> print(inv) <Inventory (manual): foobar v1.5, 0 objects>
Append new
DataObjStrinstances toobjectsas needed to populate the inventory:>>> o = soi.DataObjStr(name='baz', domain='py', role='class', ... priority='1', uri='api.html#$', dispname='-') >>> print(o) <DataObjStr:: :py:class:`baz`> >>> inv.objects.append(o) >>> print(inv) <Inventory (manual): foobar v1.5, 1 objects> >>> inv.objects.append(soi.DataObjStr(name='baz.quux', domain='py', ... role='method', priority='1', uri='api.html#$', dispname='-')) >>> inv.objects.append(soi.DataObjStr(name='quuux', domain='py', ... role='function', priority='1', uri='api.html#$', dispname='-')) >>> print(inv) <Inventory (manual): foobar v1.5, 3 objects>
Note
The role values here must be the full role names (“block directives”), described as the “directives” in the Sphinx documentation for domains, and not the abbreviated forms (“inline directives”) used when constructing cross-references.
Thus, for example, a
DataObjStrcorresponding to a method on a class should be constructed with role=’method’, not role=’meth’.
Export the
Inventoryin compressed form.Generate the text of the inventory file with
data_file(), optionally contracting theurianddispnamefields:>>> text = inv.data_file(contract=True)
Compress the file text:
>>> ztext = soi.compress(text)
Save to disk:
>>> soi.writebytes('objects_foobar.inv', ztext)
Transfer the compressed file to its distribution location.
- If only local access is needed, it can be kept local.
- If external access needed, upload to a suitable host.
Add an element to the
intersphinx_mappingparameter inconf.py.The key of the element is an arbitrary name, which can be used to specify the desired documentation set to be searched for the target object, in the event of a name collision between one or more documentation projects; e.g.:
:meth:`python:str.join`
The value of the element is a
tupleof length two:- The first element of the value tuple is the head URI for the
documentation repository,
identified in step (1),
to which the
uriof given object is appended when constructing anintersphinxcross-reference. - The second element of the value tuple can be
None, in which case the objects.inv file is assumed to be at the repository head URI. Otherwise, this element is the complete address of the distribution location of the compressed inventory file, from step (4), whether a local path or a remote URL.
- The first element of the value tuple is the head URI for the
documentation repository,
identified in step (1),
to which the
Examples:
intersphinx_mapping = { # Standard reference to web docs, with web objects.inv 'python': ('https://docs.python.org/3.5', None), # Django puts its objects.inv file in a non-standard location 'django': ('http://docs.djangoproject.com/en/dev/', 'https://docs.djangoproject.com/en/dev/_objects/'), # Drawing the Sphinx objects.inv from a local copy, but referring to the 1.7 web docs 'sphinx': ('http://www.sphinx-doc.org/en/1.7/', '/path/to/local/objects.inv', }
Speeding up “suggest” with python-Levenshtein¶
sphobjinv uses fuzzywuzzy for fuzzy-match searching of object
names/domains/roles as part of the
Inventory.suggest() functionality,
also implemented as the CLI suggest subcommand.
By default, fuzzywuzzy uses difflib.SequenceMatcher
from the Python standard library for its fuzzy searching.
However, it also can use python-Levenshtein,
a Python C extension providing
similar functionality. Thus, sphobjinv has been implemented with
python-Levenshtein as an optional dependency: it will be
used if it is available.
Installation¶
On Linux, simple installation via pip install python-Levenshtein should work properly in the vast majority of cases. This may work relatively smoothly for MacOS as well, but first-hand confirmation was not (yet) available as of this writing.
On Windows, again as of this writing, no binary wheel is available on PyPI. So, the easiest way to install is to download a wheel from Christoph Gohlke’s repository and run pip install C:\path\to\wheel. Just make sure to match Python version and CPU type (win32 vs win_amd64) when you download.
Performance Benchmark¶
The chart below presents one dataset illustrating the performance enhancement
that can be obtained by installing python-Levenshtein.
The timings plotted here are from execution of
timeit.repeat() in the
Python standard library around a
suggest() call,
searching for the term “function”, for a number of
objects.inv files from different projects (see
here).
The timings were collected using the following code:
import sphobjinv as soi
durations = {}
obj_counts = {}
for fn in os.listdir():
if fn.endswith('.inv'):
inv = soi.Inventory(fn)
# Execute the 'suggest' operation 20 times for each file
timings = timeit.repeat("inv.suggest('function')", repeat=20, number=1, globals=globals())
# Store the average timing for each file
durations.update({fn: sum(timings) / len(timings)})
# Store the number of objects
obj_counts.update({fn: inv.count})
As can be seen, the fifty-two objects.inv files in this dataset contain widely varying numbers of objects \(n\):
Unsurprisingly, larger inventories require more time to search.
Also relatively unsurprisingly, the time required appears to be
roughly \(O(n)\), since the fuzzy search must be performed once on the
as_rst representation of each object.
For this specific search, using python-Levenshtein instead of
difflib decreases the time required from 0.90 seconds per thousand objects
down to 0.15 seconds per thousand objects,
representing a performance improvement of almost exactly six-fold.
Other searches will likely exhibit somewhat better or worse
improvement from the use of python-Levenshtein,
depending on the average length of the reST-like representations
of the objects in an objects.inv
and the length of the search term.
Variations in Matching Behavior¶
Note that the matching scores calculated by
difflib and python-Levenshtein can often
differ appreciably. (This is illustrated in
issue #128
of the fuzzywuzzy GitHub repo.)
This difference in behavior doesn’t have much practical significance,
save for the potential of causing some confusion between users with/without
python-Levenshtein installed.
As an example, the following shows an excerpt of the results of a representative
CLI suggest call without
python-Levenshtein:
$ sphobjinv suggest objects_scipy.inv surface -asit 40
Name Score Index
------------------------------------------------------ ------- -------
:py:function:`scipy.misc.face` 64 1018
:py:function:`scipy.misc.source` 64 1032
:std:doc:`generated/scipy.misc.face` 64 4042
:std:doc:`generated/scipy.misc.source` 64 4056
:py:data:`scipy.stats.rice` 56 2688
:std:label:`continuous-rice` 56 2896
:py:method:`scipy.integrate.complex_ode.successful` 51 156
:py:method:`scipy.integrate.ode.successful` 51 171
:py:function:`scipy.linalg.lu_factor` 51 967
... more with score 51 ...
:py:attribute:`scipy.LowLevelCallable.signature` 50 5
:py:function:`scipy.constants.convert_temperature` 50 53
:py:function:`scipy.integrate.quadrature` 50 176
... more with score 50 and below ...
This is a similar excerpt with python-Levenshtein:
Name Score Index
------------------------------------------------------ ------- -------
:py:function:`scipy.misc.face` 64 1018
:py:function:`scipy.misc.source` 64 1032
:std:doc:`generated/scipy.misc.face` 64 4042
:std:doc:`generated/scipy.misc.source` 64 4056
:py:method:`scipy.integrate.ode.successful` 51 171
:py:function:`scipy.linalg.lu_factor` 51 967
:py:function:`scipy.linalg.subspace_angles` 51 1003
... more with score 51 ...
:py:function:`scipy.cluster.hierarchy.fcluster` 49 23
:py:function:`scipy.cluster.hierarchy.fclusterdata` 49 24
:py:method:`scipy.integrate.complex_ode.successful` 49 156
... more with score 49 and below ...
Sphinx objects.inv v2 Syntax¶
After decompression, “version 2” Sphinx objects.inv files
follow a syntax that, to the best of this author’s ability to determine,
is completely undocumented. The below
syntax is believed to be accurate as of Jun 2018 (Sphinx v1.7.4).
Based upon a quick git diff of the Sphinx repository, it is thought to be accurate for all
Sphinx versions >=1.0b1 that make use of this “version 2” objects.inv format.
The first line must be exactly:
# Sphinx inventory version 2
The second and third lines must obey the template:
# Project: <project name>
# Version: <full version number>
The above project name and version are used to populate mouseovers for
the intersphinx cross-references:
The fourth line must contain the string ‘zlib’ somewhere in it, but for the purposes of consistency it should be exactly:
# The remainder of this file is compressed using zlib.
All remaining lines of the file are the objects data, each laid out in the following syntax:
{name} {domain}:{role} {priority} {uri} {dispname}
{name}- The object name used when cross-referencing the object (falls between the backticks)
{domain}- The Sphinx domain used when cross-referencing the object (falls between the first and second colons; omitted if using the default domain)
{role}- The Sphinx role used when cross-referencing the object (falls between the second and third colons; or, between the first and second colons if using the default domain)
{priority}- Flag for placement in search results. Most will be 1 (standard priority) or -1 (omit from results)
{uri}- Relative URI for the location to which cross-references will point.
The base URI is taken from the relevant element of the
intersphinx_mappingconfiguration parameter ofconf.py. {dispname}- Default cross-reference text to be displayed in compiled documentation.
Note
The above fields MUST NOT contain spaces,
except for {dispname} which MAY contain them.
For illustration, the following is the entry for the
join() method of the str class in the
Python 3.5 objects.inv, broken out field-by-field:
str.join py:method 1 library/stdtypes.html#$ -
{name} = str.join
{domain} = py
{role} = method
{priority} = 1
{uri} = library/stdtypes.html#$
{dispname} = -
The above illustrates two shorthand notations that were introduced to shrink the size of the inventory file:
- If
{uri}has an anchor (technically a “fragment identifier,” the portion following the#symbol) and the tail of the anchor is identical to{name}, that tail is replaced with$.
- If
{dispname}is identical to{name}, it is stored as-.
Thus, a standard intersphinx reference to this method would take the form (the leading
:py could be omitted if py is the default domain):
:py:meth:`str.join`
The cross-reference would show as str.join() and link to the relative URI:
library/stdtypes.html#str.join
Other intersphinx Syntax Examples
To show as only join():
:py:meth:`~str.join`
To suppress the hyperlink as in str.join():
:py:meth:`!str.join`
To change the cross-reference text and omit the trailing parentheses
as in This is join!:
:py:obj:`This is join! <str.join>`
API¶
Most (all?) of the objects documented in the below submodules
are also exposed at the sphobjinv package root. For example,
both of the following will work to import the
Inventory class:
>>> from sphobjinv import Inventory
>>> from sphobjinv.inventory import Inventory
sphobjinv.data¶
sphobjinv data classes for individual objects.
sphobjinv is a toolkit for manipulation and inspection of
Sphinx objects.inv files.
- Author
- Brian Skinn (bskinn@alum.mit.edu)
- File Created
- 7 Nov 2017
- Copyright
- (c) Brian Skinn 2016-2018
- Source Repository
- http://www.github.com/bskinn/sphobjinv
- Documentation
- http://sphobjinv.readthedocs.io
- License
- The MIT License; see LICENSE.txt for full license terms
Members
-
class
DataFields¶ Enumfor the fields of objects.inv data objects.-
DispName= 'dispname'¶ Default display name for the object in rendered documentation when referenced as :domain:role:`name`
-
Domain= 'domain'¶ Sphinx domain housing the object
-
Name= 'name'¶ Object name, as recognized internally by Sphinx
-
Priority= 'priority'¶ Object search priority
-
Role= 'role'¶ Full name of Sphinx role to be used when referencing the object
-
URI= 'uri'¶ URI to the location of the object’s documentation, relative to the documentation root
-
-
class
DataObjBytes(name, domain, role, priority, uri, dispname, as_str=NOTHING, as_bytes=NOTHING)¶ SuperDataObjsubclass generatingbytesobject data.-
as_bytes¶ DataObjBytesversion of instance.
-
as_str¶ DataObjStrversion of instance.
-
dispname¶ Object default name in rendered documentation\(^\dagger\).
Possibly abbreviated; see here.
-
domain¶ Sphinx domain containing the object\(^\dagger\).
-
name¶ Object name, as recognized internally by Sphinx\(^\dagger\).
-
priority¶ Object search priority\(^\dagger\).
-
role¶ Sphinx role to be used when referencing the object\(^\dagger\).
-
-
class
DataObjStr(name, domain, role, priority, uri, dispname, as_bytes=NOTHING, as_str=NOTHING)¶ SuperDataObjsubclass generatingstrobject data.-
as_bytes¶ DataObjBytesversion of instance.
-
as_str¶ DataObjStrversion of instance.
-
dispname¶ Object default name in rendered documentation\(^\dagger\).
Possibly abbreviated; see here.
-
domain¶ Sphinx domain containing the object\(^\dagger\).
-
name¶ Object name, as recognized internally by Sphinx\(^\dagger\).
-
priority¶ Object search priority\(^\dagger\).
-
role¶ Sphinx role to be used when referencing the object\(^\dagger\).
-
-
class
SuperDataObj¶ Abstract base superclass defining common methods &c. for data objects.
Intended only to be subclassed by
DataObjBytesandDataObjStr, to allow definition of common methods, properties, etc. all in one place.Where marked with \(^\dagger\),
DataObjBytesinstances will returnbytesvalues, whereasDataObjStrinstances will returnstrvalues.-
as_bytes¶ DataObjBytesversion of instance.
-
as_rst¶ strreST reference-like object representation.Typically will NOT function as a proper reST reference in Sphinx source (e.g., a role of function must be referenced using :func: for the py domain).
-
as_str¶ DataObjStrversion of instance.
-
data_line(*, expand=False, contract=False)¶ Compose plaintext objects.inv data line from instance contents.
The format of the resulting data line is given by
data_line_fmt.DataObjBytesandDataObjStrinstances generate data lines asbytesandstr, respectively.Calling with both expand and contract as
Trueis invalid.Parameters: Returns: dl –
bytes(forDataObjBytes) orstr(forDataObjStr) – Object data lineRaises: ValueError– If both expand and contract areTrue
-
data_line_fmt= '{name} {domain}:{role} {priority} {uri} {dispname}'¶ Helper
strfor generating plaintext objects.inv data lines. The field names MUST match thestrvalues of theDataFieldsmembers.
-
dispname¶ Object default name in rendered documentation\(^\dagger\).
Possibly abbreviated; see here.
-
dispname_abbrev¶ Abbreviation character(s) for display name\(^\dagger\).
'-'orb'-'for version 2 objects.inv files.
-
dispname_contracted¶ Object display name, contracted with dispname_abbrev.
-
dispname_expanded¶ Object display name, with dispname_abbrev expanded.
-
domain¶ Sphinx domain containing the object\(^\dagger\).
-
evolve(**kwargs)¶ Create a new instance with changes applied.
This helper method provides a concise means for creating new instances with only a subset of changed data fields.
The names of any kwargs MUST be keys of the
dicts generated byjson_dict().Parameters: **kwargs – strorbytes– Revised value(s) to use in the new instance for the passed keyword argument(s).Returns: dobj – DataObjBytesorDataObjStr– New instance with updated data
-
json_dict(*, expand=False, contract=False)¶ Return the object data formatted as a flat
dict.The returned
dictis constructed such that it matches the relevant subschema ofsphobjinv.schema.json_schema, to facilitate implementation ofInventory.json_dict().The
dicts returned byDataObjBytesandDataObjStrboth havestrkeys, but they havebytesandstrvalues, respectively. Thedictkeys are identical to thestrvalues of theDataFieldsEnummembers.Calling with both expand and contract as
Trueis invalid.Parameters: Returns: d –
dict– Object dataRaises: ValueError– If both expand and contract areTrue
-
name¶ Object name, as recognized internally by Sphinx\(^\dagger\).
-
priority¶ Object search priority\(^\dagger\).
-
role¶ Sphinx role to be used when referencing the object\(^\dagger\).
-
rst_fmt= ':{domain}:{role}:`{name}`'¶ str.format()template for generating reST-like representations of object data foras_rst(used withInventory.suggest()).
-
uri_abbrev¶ Abbreviation character(s) for URI tail\(^\dagger\).
'$'orb'$'for version 2 objects.inv files.
-
uri_contracted¶ Object relative URI, contracted with uri_abbrev.
-
uri_expanded¶ Object relative URI, with uri_abbrev expanded.
-
sphobjinv.error¶
Custom errors for sphobjinv.
sphobjinv is a toolkit for manipulation and inspection of
Sphinx objects.inv files.
- Author
- Brian Skinn (bskinn@alum.mit.edu)
- File Created
- 5 Nov 2017
- Copyright
- (c) Brian Skinn 2016-2018
- Source Repository
- http://www.github.com/bskinn/sphobjinv
- Documentation
- http://sphobjinv.readthedocs.io
- License
- The MIT License; see LICENSE.txt for full license terms
Members
-
exception
SphobjinvError¶ Custom
sphobjinverror superclass.
sphobjinv.fileops¶
File I/O helpers for sphobjinv.
sphobjinv is a toolkit for manipulation and inspection of
Sphinx objects.inv files.
- Author
- Brian Skinn (bskinn@alum.mit.edu)
- File Created
- 5 Nov 2017
- Copyright
- (c) Brian Skinn 2016-2018
- Source Repository
- http://www.github.com/bskinn/sphobjinv
- Documentation
- http://sphobjinv.readthedocs.io
- License
- The MIT License; see LICENSE.txt for full license terms
Members
-
readbytes(path)¶ Read file contents and return as
bytes.Parameters: path – str– Path to file to be opened.Returns: b – bytes– Contents of the indicated file.
-
readjson(path)¶ Create
dictfrom JSON file.No data or schema validation is performed.
Parameters: path – str– Path to JSON file to be read.Returns: d – dict– Deserialized JSON.
-
urlwalk(url)¶ Generate a series of candidate objects.inv URLs.
URLs are based on the seed url passed in. Ensure that the path separator in url is the standard forward slash (‘/’).
Parameters: url – str– Seed URL defining directory structure to walk through.Yields: inv_url – str– Candidate URL for objects.inv location.
-
writebytes(path, contents)¶ Write indicated file contents.
Any existing file at path will be overwritten.
Parameters:
sphobjinv.inventory¶
sphobjinv data class for full inventories.
sphobjinv is a toolkit for manipulation and inspection of
Sphinx objects.inv files.
- Author
- Brian Skinn (bskinn@alum.mit.edu)
- File Created
- 7 Dec 2017
- Copyright
- (c) Brian Skinn 2016-2018
- Source Repository
- http://www.github.com/bskinn/sphobjinv
- Documentation
- http://sphobjinv.readthedocs.io
- License
- The MIT License; see LICENSE.txt for full license terms
Members
-
class
HeaderFields¶ Enumfor various inventory-level data items.A subset of these
Enumvalues is used in various Regex, JSON, and string formatting contexts withinInventoryandschema.json_schema.-
Count= 'count'¶ Number of objects contained in the inventory
-
Metadata= 'metadata'¶ The
strvalue of thisEnummember is accepted as a root-level key in adictto be imported into anInventory. The corresponding value in thedictmay contain any arbitrary data. Its possible presence is accounted for inschema.json_schema.The data associated with this key are ignored during import into an
Inventory.
-
Project= 'project'¶ Project name associated with an inventory
-
Version= 'version'¶ Project version associated with an inventory
-
-
class
Inventory(source=None, plaintext=None, zlib=None, fname_plain=None, fname_zlib=None, dict_json=None, url=None, count_error=True)¶ Entire contents of an objects.inv inventory.
All information is stored internally as
str, even if imported from abytessource.All arguments except count_error are used to specify the source from which the
Inventorycontents are to be populated. At most ONE of these source arguments may be other thanNone.The count_error argument is only relevant to the dict_json source type.
source
The
Inventorywill attempt to parse the indicated source object as each of the below types in sequence, except for url.This argument is included mainly as a convenience feature for use in interactive sessions, as invocations of the following form implicitly populate source, as the first positional argument:
>>> inv = Inventory(src_obj)
In most cases, for clarity it is recommended that programmatic instantiation of
Inventoryobjects utilize the below format-specific arguments.plaintext
Object is to be parsed as thebytesplaintext contents of an objects.inv inventory.zlib
Object is to be parsed as thebyteszlib-compressed contents of an objects.inv inventory.fname_plain
Object is thestrpath to a file containing the plaintext contents of an objects.inv inventory.fname_zlib
Object is thestrpath to a file containing the zlib-compressed contents of an objects.inv inventory.dict_json
Object is a
dictcontaining the contents of an objects.inv inventory, conforming to the JSON schema ofschema.json_schema.If count_error is passed as
True, then aValueErroris raised if the number of objects found in thedictdoes not match the value associated with its count key. If count_error is passed asFalse, an object count mismatch is ignored.url
Object is a
strURL to a zlib-compressed objects.inv file. Any URL type supported byurllib.requestSHOULD work; only http: and file: have been directly tested, however.No authentication is supported at this time.
Members
-
count¶ Count of objects currently in inventory.
-
data_file(*, expand=False, contract=False)¶ Generate a plaintext objects.inv as
bytes.bytesis used here as the output type since the most common use cases are anticipated to be either (1) dumping to file viasphobjinv.fileops.writebytes()or (2) compressing viasphobjinv.zlib.compress(), both of which takebytesinput.Calling with both expand and contract as
Trueis invalid.Parameters: Returns: b –
bytes– Inventory in plaintext objects.inv formatRaises: ValueError– If both expand and contract areTrue
-
header_preamble= '# Sphinx inventory version 2'¶ Preamble line for v2 objects.inv header
-
header_project= '# Project: {project}'¶ Project line
str.format()template for objects.inv header
-
header_version= '# Version: {version}'¶ Version line
str.format()template for objects.inv header
-
header_zlib= '# The remainder of this file is compressed using zlib.'¶ zlib compression line for v2 objects.inv header
-
json_dict(expand=False, contract=False)¶ Generate a flat
dictrepresentation of the inventory.The returned
dictmatches the schema ofsphobjinv.schema.json_schema.Calling with both expand and contract as
Trueis invalid.Parameters: Returns: Raises: ValueError– If both expand and contract areTrue
-
objects¶ listofDataObjStrrepresenting the data objects of the inventory. Can be edited directly to change the inventory contents. Undefined/random behavior/errors will result if the type of the elements is anything other thanDataObjStr.
-
objects_rst¶ listof objects formatted in astrreST-like representation.The format of each
strin thelistis given bydata.SuperDataObj.rst_fmt.Calling with both expand and contract as
Trueis invalid.Parameters: Returns: obj_l –
listofstr– Inventory object data in reST-like formatRaises: ValueError– If both expand and contract areTrue
-
source_type¶ SourceTypesEnumvalue indicating the type of source from which the instance was generated.
-
suggest(name, *, thresh=50, with_index=False, with_score=False)¶ Suggest objects in the inventory to match a name.
suggest()makes use of the powerful pattern-matching libraryfuzzywuzzyto identify potential matches to the given name within the inventory. The search is performed over thelistofstrgenerated byobjects_rst().thresh defines the minimum
fuzzywuzzymatch quality (an integer ranging from 0 to 100) required for a given object to be included in the results list. Can be any float value, but best results are generally obtained with values between 50 and 80, depending on the number of objects in the inventory, the confidence of the user in the match between name and the object(s) of interest, and the desired fidelity of the search results to name.This functionality is provided by the ‘suggest’ subparser of the command-line interface.
Parameters: - name –
str– Object name forfuzzywuzzypattern matching - thresh –
float–fuzzywuzzymatch quality threshold - with_index –
bool– Include with each matched name its index withinInventory.objects - with_score –
bool– Include with each matched name itsfuzzywuzzymatch quality score
Returns: res_l –
listIf both with_index and with_score are
False, members are thestrSuperDataObj.as_rst()representations of matching objects.If either is
True, members aretuples of the indicated match results:with_index == True: (as_rst, index)
with_score == True: (as_rst, score)
with_index == with_score == True: (as_rst, score, index)
- name –
-
-
class
SourceTypes¶ Enumfor the import mode used in instantiating anInventory.Since
Enumkeys iterate in definition order, the definition order here defines the order in whichInventoryobjects attempt to parse a source object passed toInventory.__init__()either as a positional argument or via the generic source keyword argument.This order DIFFERS from the documentation order, which is alphabetical.
-
DictJSON= 'dict_json'¶ Instantiation from a
dictvalidated againstschema.json_schema.
-
FnamePlaintext= 'fname_plain'¶ Instantiation from a plaintext objects.inv file on disk.
-
FnameZlib= 'fname_zlib'¶ Instantiation from a zlib-compressed objects.inv file on disk.
-
Manual= 'manual'¶ No source;
Inventorywas instantiated withprojectandversionas empty strings andobjectsas an emptylist.
-
URL= 'url'¶ Instantiation from a zlib-compressed objects.inv file downloaded from a URL.
-
sphobjinv.re¶
Helper regexes for sphobjinv.
sphobjinv is a toolkit for manipulation and inspection of
Sphinx objects.inv files.
- Author
- Brian Skinn (bskinn@alum.mit.edu)
- File Created
- 5 Nov 2017
- Copyright
- (c) Brian Skinn 2016-2018
- Source Repository
- http://www.github.com/bskinn/sphobjinv
- Documentation
- http://sphobjinv.readthedocs.io
- License
- The MIT License; see LICENSE.txt for full license terms
Members
-
p_data= re.compile(' ^ # Start of line\n (?P<name>[^#]\\S+) # --> Name\n \\s+ # Dividing space\n (?P<domain>\\w+) # --> Domain\n : , re.MULTILINE|re.VERBOSE)¶ Compiled re
strregex pattern for data lines instrdecompressed inventory files
-
pb_comments= re.compile(b'^#.*$', re.MULTILINE)¶ Compiled re
bytespattern for comment lines in decompressed inventory files
-
pb_data= re.compile(b' ^ # Start of line\n (?P<name>[^#]\\S+) # --> Name\n \\s+ # Dividing space\n (?P<domain>\\w+) # --> Domain\n : , re.MULTILINE|re.VERBOSE)¶ Compiled re
bytesregex pattern for data lines inbytesdecompressed inventory files
-
pb_project= re.compile(b'\n ^ # Start of line\n [#][ ]Project:[ ] # Preamble\n (?P<project>.+?) # Lazy rest of line is the project name\n \\r?$ # Ign, re.MULTILINE|re.VERBOSE)¶
-
pb_version= re.compile(b'\n ^ # Start of line\n [#][ ]Version:[ ] # Preamble\n (?P<version>.+?) # Lazy rest of line is the version\n \\r?$ # Ignore p, re.MULTILINE|re.VERBOSE)¶
-
ptn_data= ' ^ # Start of line\n (?P<name>[^#]\\S+) # --> Name\n \\s+ # Dividing space\n (?P<domain>\\w+) # --> Domain\n : # Dividing colon\n (?P<role>\\w+) # --> Role\n \\s+ # Dividing space\n (?P<priority>-?\\d+) # --> Priority\n \\s+ # Dividing space\n (?P<uri>\\S+) # --> URI\n \\s+ # Dividing space\n (?P<dispname>.+?) # --> Display name, lazy b/c possible CR\n \\r?$ # Ignore possible CR at EOL\n '¶
sphobjinv.schema¶
JSON schema to validate inventory dictionaries.
This module is part of sphobjinv,
a toolkit for manipulation and inspection of
Sphinx objects.inv files.
- Author
- Brian Skinn (bskinn@alum.mit.edu)
- File Created
- 7 Dec 2017
- Copyright
- (c) Brian Skinn 2016-2018
- Source Repository
- http://www.github.com/bskinn/sphobjinv
- Documentation
- http://sphobjinv.readthedocs.io
- License
- The MIT License; see LICENSE.txt for full license terms
Members
-
json_schema= {'$schema': 'http://json-schema.org/schema#', 'additionalProperties': False, 'patternProperties': {'^\\d+': {'additionalProperties': False, 'properties': {'dispname': {'type': 'string'}, 'name': {'type': 'string'}, 'priority': {'type': 'string'}, 'domain': {'type': 'string'}, 'uri': {'type': 'string'}, 'role': {'type': 'string'}}, 'required': ['dispname', 'name', 'priority', 'domain', 'uri', 'role'], 'type': 'object'}}, 'properties': {'metadata': {}, 'count': {'type': 'integer'}, 'version': {'type': 'string'}, 'project': {'type': 'string'}}, 'required': ['project', 'version', 'count'], 'type': 'object'}¶ JSON schema for validating the
dictforms of Sphinx objects.inv inventories as generated from or expected byInventoryclasses.
sphobjinv.zlib¶
zlib (de)compression helpers for sphobjinv.
sphobjinv is a toolkit for manipulation and inspection of
Sphinx objects.inv files.
- Author
- Brian Skinn (bskinn@alum.mit.edu)
- File Created
- 5 Nov 2017
- Copyright
- (c) Brian Skinn 2016-2018
- Source Repository
- http://www.github.com/bskinn/sphobjinv
- Documentation
- http://sphobjinv.readthedocs.io
- License
- The MIT License; see LICENSE.txt for full license terms
Members
-
compress(bstr)¶ Compress a version 2
intersphinxobjects.inv bytestring.The #-prefixed comment lines are left unchanged, whereas the plaintext data lines are compressed with
zlib.Parameters: bstr – bytes– Binary string containing the decompressed contents of an objects.inv file.Returns: out_b – bytes– Binary string containing the compressed objects.inv content.
-
decompress(bstr)¶ Decompress a version 2
intersphinxobjects.inv bytestring.The #-prefixed comment lines are left unchanged, whereas the
zlib-compressed data lines are decompressed to plaintext.Parameters: bstr – bytes– Binary string containing a compressed objects.inv file.Returns: out_b – bytes– Decompressed binary string containing the plaintext objects.inv content.
sphobjinv.cmdline (non-API)¶
CLI module for sphobjinv.
sphobjinv is a toolkit for manipulation and inspection of
Sphinx objects.inv files.
Note
This module is NOT part of the public API for sphobjinv.
Its entire contents should be considered implementation detail.
- Author
- Brian Skinn (bskinn@alum.mit.edu)
- File Created
- 17 May 2016
- Copyright
- (c) Brian Skinn 2016-2018
- Source Repository
- http://www.github.com/bskinn/sphobjinv
- Documentation
- http://sphobjinv.readthedocs.io
- License
- The MIT License; see LICENSE.txt for full license terms
Members
-
do_convert(inv, in_path, params)¶ Carry out the conversion operation, including writing output.
If
OVERWRITEis passed and the output file (the default location, or as passed toOUTFILE) exists, it will be overwritten without a prompt. Otherwise, the user will be queried if it is desired to overwrite the existing file.If
QUIETis passed, nothing will be printed to stdout (potentially useful for scripting), and any existing output file will be overwritten without prompting.Parameters:
-
do_suggest(inv, params)¶ Perform the suggest call and output the results.
Results are printed one per line.
If neither
INDEXnorSCOREis specified, the results are output without a header. If either or both are specified, the results are output in a lightweight tabular format.If the number of results exceeds
SUGGEST_CONFIRM_LENGTH, the user will be queried whether to display all of the returned results unlessALLis specified.No --quiet option is available here, since a silent mode for suggestion output is nonsensical.
Parameters:
-
err_format(exc)¶ Pretty-format an exception.
Parameters: exc – Exception– Exception instance to pretty-formatReturns: pretty_exc – str– Exception type and message formatted as ’{type}: {message}’
-
getparser()¶ Generate argument parser.
Returns: prs – ArgumentParser– Parser for commandline usage ofsphobjinv
-
import_infile(in_path)¶ Attempt import of indicated file.
Convenience function wrapping attempts to load an
Inventoryfrom a local path.Parameters: in_path – str– Path to input fileReturns: inv – InventoryorNone– If instantiation with the file at in_path succeeds, the resultingInventoryinstance; otherwise,None
-
inv_local(params)¶ Create
Inventoryfrom local source.Uses
resolve_inpath()to sanity-check and/or convertINFILE.Calls
sys.exit()internally in error-exit situations.Parameters: params – dict– Parameters/values mapping from the active subparserReturns: - inv –
Inventory– Object representation of the inventory atINFILE - in_path –
str– Input file path as resolved/checked byresolve_inpath()
- inv –
-
inv_url(params)¶ Create
Inventoryfrom file downloaded from URL.Initially, treats
INFILEas a download URL to be passed to the url initialization argument ofInventory.If an inventory is not found at that exact URL, progressively searches the directory tree of the URL for objects.inv.
Calls
sys.exit()internally in error-exit situations.Parameters: params – dict– Parameters/values mapping from the active subparserReturns:
-
main()¶ Handle command line invocation.
Parses command line arguments, handling the no-arguments and
VERSIONcases.Creates the
Inventoryfrom the indicated source and method.Invokes
do_convert()ordo_suggest()per the subparser name stored inSUBPARSER_NAME.
-
resolve_inpath(in_path)¶ Resolve the input file, handling invalid values.
Currently, only checks for existence and not-directory.
Parameters: in_path – str– Path to desired input fileReturns: abs_path – str– Absolute path to indicated fileRaises: FileNotFoundError– If a file is not found at the given path
-
resolve_outpath(out_path, in_path, params)¶ Resolve the output location, handling mode-specific defaults.
If the output path or basename are not specified, they are taken as the same as the input file. If the extension is unspecified, it is taken as the appropriate mode-specific value from
DEF_OUT_EXT.If
URLis passed, the input directory is taken to beos.getcwd()and the input basename is taken asDEF_BASENAME.Parameters: Returns: out_path –
str– Absolute path to the target output file
-
selective_print(thing, params)¶ Print thing if not in quiet mode.
Quiet mode is indicated by the value at the
QUIETkey within params.Quiet mode is not implemented for the “suggest” CLI mode.
Parameters: - thing – any – Object to be printed
- params –
dict– Parameters/values mapping from the active subparser
-
write_json(inv, path, *, expand=False, contract=False)¶ Write an
Inventoryto JSON.Writes output via
fileops.writejson().Calling with both expand and contract as
Trueis invalid.Parameters: Raises: ValueError– If both expand and contract areTrue
-
write_plaintext(inv, path, *, expand=False, contract=False)¶ Write an
Inventoryto plaintext.Newlines are inserted in an OS-aware manner, based on the value of
os.linesep.Calling with both expand and contract as
Trueis invalid.Parameters: Raises: ValueError– If both expand and contract areTrue
-
write_zlib(inv, path, *, expand=False, contract=False)¶ Write an
Inventoryto zlib-compressed format.Calling with both expand and contract asTrueis invalid.Parameters: Raises: ValueError– If both expand and contract areTrue
-
yesno_prompt(prompt)¶ Query user at stdin for yes/no confirmation.
Uses
input(), so will hang if used programmatically unless stdin is suitably mocked.The value returned from
input()must satisfy either resp.lower() == ‘n’ or resp.lower() == ‘y’, or else the query will be repeated ad infinitum. This function does NOT augment prompt to indicate the constraints on the accepted values.Parameters: prompt – str– Prompt to display to user that requests a ‘Y’ or ‘N’ responseReturns: resp – str– User response
-
ALL= 'all'¶ Optional argument name for use with the
SUGGESTsubparser, indicating to print all returned objects, regardless of the number returned, without asking for confirmation
-
CONTRACT= 'contract'¶ Optional argument name for use with the
CONVERTsubparser, indicating to contract URIs and display names to abbreviated forms in the generated output file
-
CONVERT= 'convert'¶ Subparser name for inventory file conversions; stored in
SUBPARSER_NAMEwhen selected
-
DEF_OUT_EXT= {'json': '.json', 'plain': '.txt', 'zlib': '.inv'}¶ Default extensions for an unspecified
OUTFILE
-
DEF_THRESH= 75¶ Default match threshold for
sphobjinv suggest --thresh
-
EXPAND= 'expand'¶ Optional argument name for use with the
CONVERTsubparser, indicating to expand URI and display name abbreviations in the generated output file
-
HELP_CONV_EXTS= "'.inv/.txt/.json'"¶ Help text for default extensions for the various conversion types
-
HELP_CO_PARSER= 'Convert intersphinx inventory to zlib-compressed, plaintext, or JSON formats.'¶ Help text for the
CONVERTsubparser
-
HELP_SU_PARSER= 'Fuzzy-search intersphinx inventory for desired object(s).'¶ Help text for the
SUGGESTsubparser
-
INDEX= 'index'¶ Optional argument name for use with the
SUGGESTsubparser, indicating to print the location index of each returned object withinINFILEalong with the object domain/role/name (may be specified withSCORE)
-
INFILE= 'infile'¶ Required positional argument name for use with both
CONVERTandSUGGESTsubparsers, holding the path (or URL, ifURLis specified) to the input file
-
MODE= 'mode'¶ Positional argument name for use with
CONVERTsubparser, indicating output file format (ZLIB,PLAINorJSON)
-
OUTFILE= 'outfile'¶ Optional positional argument name for use with the
CONVERTsubparser, holding the path to the output file (DEF_BASENAMEand the appropriate item fromDEF_OUT_EXTare used if this argument is not provided)
-
OVERWRITE= 'overwrite'¶ Optional argument name for use with the
CONVERTsubparser, indicating to overwrite any existing output file without prompting
-
QUIET= 'quiet'¶ Optional argument name for use with the
CONVERTsubparser, indicating to suppress console output
-
SCORE= 'score'¶ Optional argument name for use with the
SUGGESTsubparser, indicating to print thefuzzywuzzyscore of each returned object withinINFILEalong with the object domain/role/name (may be specified withINDEX)
-
SEARCH= 'search'¶ Positional argument name for use with the
SUGGESTsubparser, holding the search term forfuzzywuzzytext matching
-
SUGGEST= 'suggest'¶ Subparser name for inventory object suggestions; stored in
SUBPARSER_NAMEwhen selected
-
SUGGEST_CONFIRM_LENGTH= 30¶ Number of returned objects from a
SUGGESTsubparser invocation above which user will be prompted for confirmation to print the results (unlessALLis specified)
-
THRESH= 'thresh'¶ Optional argument name for use with the
SUGGESTsubparser, taking the minimum desiredfuzzywuzzymatch quality as one required argument
-
URL= 'url'¶ Optional argument name for use with both
CONVERTandSUGGESTsubparsers, indicating thatINFILEis to be treated as a URL rather than a local file path
-
VERSION= 'version'¶ Optional argument name for use with the base argument parser, to show version &c. info, and exit
-
VER_TXT= '\nsphobjinv v2.0\n\nCopyright (c) Brian Skinn 2016-2018\nLicense: The MIT License\n\nBug reports & feature requests: https://github.com/bskinn/sphobjinv\nDocumentation: http://sphobjinv.readthedocs.io\n'¶ Version &c. output blurb