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
¶ Enum
for various inventory-level data items.A subset of these
Enum
values is used in various Regex, JSON, and string formatting contexts withinInventory
andschema.json_schema
.-
Count
= 'count'¶ Number of objects contained in the inventory
-
Metadata
= 'metadata'¶ The
str
value of thisEnum
member is accepted as a root-level key in adict
to be imported into anInventory
. The corresponding value in thedict
may 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 abytes
source.All arguments except count_error are used to specify the source from which the
Inventory
contents 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
Inventory
will 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
Inventory
objects utilize the below format-specific arguments.plaintext
Object is to be parsed as thebytes
plaintext contents of an objects.inv inventory.zlib
Object is to be parsed as thebytes
zlib-compressed contents of an objects.inv inventory.fname_plain
Object is thestr
path to a file containing the plaintext contents of an objects.inv inventory.fname_zlib
Object is thestr
path to a file containing the zlib-compressed contents of an objects.inv inventory.dict_json
Object is a
dict
containing the contents of an objects.inv inventory, conforming to the JSON schema ofschema.json_schema
.If count_error is passed as
True
, then aValueError
is raised if the number of objects found in thedict
does 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
str
URL to a zlib-compressed objects.inv file. Any URL type supported byurllib.request
SHOULD 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
.bytes
is 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 takebytes
input.Calling with both expand and contract as
True
is 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
dict
representation of the inventory.The returned
dict
matches the schema ofsphobjinv.schema.json_schema
.Calling with both expand and contract as
True
is invalid.Parameters: Returns: Raises: ValueError
– If both expand and contract areTrue
-
objects
¶ list
ofDataObjStr
representing 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
¶ list
of objects formatted in astr
reST-like representation.The format of each
str
in thelist
is given bydata.SuperDataObj.rst_fmt
.Calling with both expand and contract as
True
is invalid.Parameters: Returns: obj_l –
list
ofstr
– Inventory object data in reST-like formatRaises: ValueError
– If both expand and contract areTrue
-
source_type
¶ SourceTypes
Enum
value 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 libraryfuzzywuzzy
to identify potential matches to the given name within the inventory. The search is performed over thelist
ofstr
generated byobjects_rst()
.thresh defines the minimum
fuzzywuzzy
match 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 forfuzzywuzzy
pattern matching - thresh –
float
–fuzzywuzzy
match quality threshold - with_index –
bool
– Include with each matched name its index withinInventory.objects
- with_score –
bool
– Include with each matched name itsfuzzywuzzy
match quality score
Returns: res_l –
list
If both with_index and with_score are
False
, members are thestr
SuperDataObj.as_rst()
representations of matching objects.If either is
True
, members aretuple
s 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
¶ Enum
for the import mode used in instantiating anInventory
.Since
Enum
keys iterate in definition order, the definition order here defines the order in whichInventory
objects 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
dict
validated 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;
Inventory
was instantiated withproject
andversion
as empty strings andobjects
as an emptylist
.
-
URL
= 'url'¶ Instantiation from a zlib-compressed objects.inv file downloaded from a URL.
-