Importers
- class sphinx_hosting.importers.PageTreeNode(page: SphinxPage, parent_title: str | None = None, next_title: str | None = None)[source]
A data structure to temporarily hold relationships between
sphinx_hosting.models.SphinxPageobjects while importing pages.In the page JSON we get from Sphinx, we only know the titles of related pages, so we store them here along with the
sphinx_hosting.models.SphinxPagewe created from our JSON, and then do another pass through thesePageTreeNodeobjects to link our pages together.This is used in
SphinxPackageImporter.link_pages.- page: SphinxPage
- class sphinx_hosting.importers.SphinxPackageImporter[source]
Usage:
SphinxPackageImporter().run(sphinx_tarfilename)`Import a tarfile of a built set of Sphinx documentation into the database.
Important
Before importing, there must be a
sphinx_hosting.models.Projectin the database whosemachine_namematches theprojectin Sphinx’sconf.pyconfig file for the docs to be imported.The documentation package should have been built via the
jsonoutput fromsphinx-build, so either:make jsonor:
sphinx-build -n -b json build/json
The tarfile should be built like so:
cd build tar zcf mydocs.tar.gz json
ensuring that the package contents are enclosed in a folder.
When run,
SphinxPackageImporterwill look inside the tarfile at theglobalcontext.jsonfile to determine which project and version we should associate these pages with.The
projectkey in will be used to look up thesphinx_hosting.models.Projectto associate these Sphinx pages with, usingprojectassphinx_hosting.models.Project.machine_nameThe
versionkey will be used to create a newsphinx_hosting.models.Versionobject tied to that project
Once the
sphinx_hosting.models.Versionhas been created, the pages in the tarfile will be created assphinx_hosting.models.SphinxPageobjects, and the images will be created assphinx_hosting.models.SphinxImageobjects.- get_version(force: bool = False) Version[source]
Look in
packagefor a member namedglobalcontext.json, and load that file as JSON.Extract these things from that JSON:
the version string from the
releasekey.the
machine_nameof theProjectfor this documentation tarfile as the slugified version of theprojectkey
Return a new
Versioninstance on the project.- Keyword Arguments:
force – if
True, re-use an existing version, purging any docs and images associated with it first- Raises:
Project.DoesNotExist – no
Projectexists whosemachine_namematches the slugifiedprojectsetting in the Sphinx package’sconf.pyVersionAlreadyExists – a
Versionwith version stringreleasefrom the Sphinxconf.pyalready exists for our project, andforcewas notTrue
- import_documents(package: TarFile, version: Version) None[source]
Import all downloadable documents in our Sphinx documentation into the database (and our Django storage) before importing any pages.
- Parameters:
package – the opened Sphinx documentation tarfile
version – the
Versionwhich which to associate our documents
- import_images(package: TarFile, version: Version) None[source]
Import all images in our Sphinx documentation into the database before importing any pages, then return a lookup dict for doing
<img src="image_path">replacements in the page bodies.- Parameters:
package – the opened Sphinx documentation tarfile
version – the
Versionwhich which to associate our images
- import_pages(package: TarFile, version: Version) None[source]
Import a all pages from
packageinto the database assphinx_hosting.models.SphinxPageobjects, associating them withVersionversion.- Parameters:
package – the tarfile of the sphinx docs
version – the
Versionobject to associated data
- Returns:
The page linkage tree for consumption by
link_pages.
- link_pages() None[source]
Given
page_tree`, a list of page linkages (parent, next, prev), link all thesphinx_hosting.models.SphinxPageobjects in that list to their next page and their parent page.- Parameters:
tree – the page linkage tree
- load_config(package: TarFile) None[source]
Load the
globalcontext.jsonfile for later reference.- Parameters:
package – the opened Sphinx documentation tarfile
- run(filename: str | None = None, file_obj: IO | None = None, force: bool = False) Version[source]
Load the pages in the tarfile identified by
filenameinto the database asVersionversionofProjectproject. See the class docs forSphinxPackageImporterfor more background on how to prepare the package named byfilename.- Keyword Arguments:
filename – the filename of the gzipped tar archive of the Sphinx pages
file_obj – an open file object of the gzipped tar archive of the Sphinx pages
force – if
True, overwrite the docs for an existing version
- Raises:
Project.DoesNotExist – no
Projectexists whosemachine_namematches the slugifiedprojectsetting in the Sphinx package’sconf.pyVersionAlreadyExists – a
Versionwith version stringreleasefrom the Sphinx package’sconf.pyalready exists for our project, andforcewas notTrue
- image_map: dict[str, sphinx_hosting.models.SphinxImage]
- page_tree: dict[str, sphinx_hosting.importers.PageTreeNode]