Skip to content

Commit

Permalink
Added Sphinx documentation builder and added doc comments (#107)
Browse files Browse the repository at this point in the history
* Added empty but configured Sphinx project

* Added bulk of documentation

* Added RTD config file

* Renamed doc to docs

* Squashed commits with RTD build attempts - Unsucceful in the end

Attempting to add apt install command to conf.py

Moved attempt at install to setup.py instead

Added Y flag to setup.py

Added apt install did not work - Trying while skipping setup.py altogether

Tried setting a default flag in setup.py for RTD

Forgot to re-enable setup.py in RTD yml

Changing from pip to setuptools build

Reverted setup.py to master state, added comments in .readthedocs.yml
  • Loading branch information
RobertoRoos authored Feb 10, 2021
1 parent 4739f82 commit 8fdfb09
Show file tree
Hide file tree
Showing 12 changed files with 410 additions and 4 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
# Build directories
build/
dist/
MANIFEST
hid.c
hidraw.c
.eggs/
hidapi.egg-info/

# Temp files
hid.*.pyd

# Testing
.tox/

# IDE files
.idea/

# Virtual environments
venv/

# Doc output
docs/_build
30 changes: 30 additions & 0 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# .readthedocs.yml
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Build documentation in the docs/ directory with Sphinx
sphinx:
configuration: docs/conf.py

# Also checkout any submodules
submodules:
include: all

# Optionally build your docs in additional formats such as PDF
formats:
- pdf

# Optionally set the version of Python and requirements required to build your docs
python:
version: 3.8
install:
- requirements: docs/requirements.txt
- method: setuptools
path: .

#
# Build on ReadTheDocs is not working because libusb and libudev are missing.
#
17 changes: 17 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ Build from source

$ sudo python setup.py install

**Alternatively**, you can run pip directly, which will call the necessary build and install commands::

$ pip install -e .

5. Test install::

$ python
Expand All @@ -81,3 +85,16 @@ to `this one <https://raw.githubusercontent.com/trezor/trezor-common/master/udev
in your udev rules directory.

Also you might need to call ``udevadm control --reload-rules`` to reload the rules.

Documentation
-------------

Documentation can be built using Sphinx::

$ cd docs && make html

The HTML output will be in ``docs/_build/html``.

The documentation is extracted from the code using ``autodoc``.

Note that the build output is used. So rebuilt the package before generating documentation.
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
Empty file added docs/_static/.gitkeep
Empty file.
15 changes: 15 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
API
===

List devices
------------

.. autofunction:: hid.enumerate


Device class
------------

.. autoclass:: hid.device
:members:
:undoc-members:
59 changes: 59 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
sys.path.insert(0, os.path.abspath('../')) # Add root directory to path


# -- Project information -----------------------------------------------------

project = 'HIDAPI'
copyright = '2021, Trezor'
author = 'Trezor'


# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'recommonmark',
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

source_suffix = {
'.rst': 'restructuredtext',
'.md': 'markdown',
}

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
61 changes: 61 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Examples

## Finding devices

List all info of all devices with:

```python
import hid

for device_dict in hid.enumerate():
keys = list(device_dict.keys())
keys.sort()
for key in keys:
print("%s : %s" % (key, device_dict[key]))
print()

```

## Connecting, reading and writing

```python
try:
print("Opening the device")

h = hid.device()
h.open(0x534C, 0x0001) # TREZOR VendorID/ProductID

print("Manufacturer: %s" % h.get_manufacturer_string())
print("Product: %s" % h.get_product_string())
print("Serial No: %s" % h.get_serial_number_string())

# enable non-blocking mode
h.set_nonblocking(1)

# write some data to the device
print("Write the data")
h.write([0, 63, 35, 35] + [0] * 61)

# wait
time.sleep(0.05)

# read back the answer
print("Read the data")
while True:
d = h.read(64)
if d:
print(d)
else:
break

print("Closing the device")
h.close()

except IOError as ex:
print(ex)
print("You probably don't have the hard-coded device.")
print("Update the h.open() line in this script with the one")
print("from the enumeration list output above and try again.")

print("Done")
```
23 changes: 23 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.. HIDAPI documentation master file, created by
sphinx-quickstart on Tue Feb 9 09:56:35 2021.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to HIDAPI's documentation!
==================================

.. toctree::
:maxdepth: 2
:caption: Contents:

Home <self>
examples.md
api.rst


Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build

if "%1" == "" goto help

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.http://sphinx-doc.org/
exit /b 1
)

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
2 changes: 2 additions & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sphinx
sphinx-rtd-theme
Loading

0 comments on commit 8fdfb09

Please sign in to comment.