Skip to content

Commit

Permalink
Documentation for the new API
Browse files Browse the repository at this point in the history
  • Loading branch information
masklinn committed Nov 3, 2023
1 parent 7ff511e commit 07bbec1
Show file tree
Hide file tree
Showing 12 changed files with 267 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ dist/
tmp/
regexes.yaml
_regexes.py
# sphinx build dir
_build/
49 changes: 49 additions & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Changelog
---------

This repository is mostly a wrapper for the `User Agent String Parser
<https://github.com/ua-parser/uap-core>`_ data.

As such, "data" changes should be lookled up in `the main project's
releases <https://github.com/ua-parser/uap-core/tags>`_, at least
assuming this is synchronised.

API-level changes are documented below:

1.0 (pending)
~~~~~~~~~~~~~

First truly major change in a long time. I still have no idea what
versioning scheme to use though.

- Dropped compatibility with CPython 2.7, CPython 3.6, and CPython
3.7, this includes the legacy API.
- A brand new fully typed API following PEP8 conventions has been
added, the surface follows the logic of the old one (because it's
simple and it works) but it returns proper instances (rather than
undifferentiated dicts) and should allow for the development of
faster opt-in parsers with less fear of breaking existing use-cases.

Notably, the new API also makes initialising parsers from custom
yaml files rather more practical than having to set an environment
variable.

Consider migrating.
- The new API switches over from the google-style to the sphinx-style
docstring format as hopefully we'll publish something on RTD
eventually.
- The legacy parsing API (``ua_parser.user_agent_parser``) is
recommended against but not deprecated yet, although the ``Parse*``
functions are pending deprecation, and:

- ``PrettyUserAgent`` and ``PrettyOS`` are tentatively deprecated as
it's not clear what their use-case is, but they could be
un-deprecated.
- Any use of the ``jsParseBits`` (js overrides in the ``Parse*``
functions) is deprecated.
- ``GetFilters`` is deprecated.
- ``ParseWithJSOverrides`` and ``Pretty`` are formally deprecated
(they were never not deprecated).
- The ``MatchSpans`` methods of the ``*Parser`` objects has been
removed, they're undocumented, not used internally, and don't have
a clear purpose.
20 changes: 20 additions & 0 deletions doc/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)
27 changes: 27 additions & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
API
===

Main Helpers
------------

.. automodule:: ua_parser
:members:

Core Types
----------

.. automodule:: ua_parser.types
:members:

Basic Parser
------------

.. automodule:: ua_parser.basic
:members:

Caches
------

.. automodule:: ua_parser.caching
:members:

1 change: 1 addition & 0 deletions doc/changelog.rst
35 changes: 35 additions & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = "ua-parser"
copyright = "2023, ua-parser team"
author = "ua-parser team"

version = "1.0.0a2"
release = "1.0.0a2"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = ["sphinx.ext.autodoc", "sphinx.ext.viewcode", "sphinx.ext.intersphinx"]

templates_path = ["_templates"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]


# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = "alabaster"
html_static_path = ["_static"]

intersphinx_mapping = {
"python": ("https://docs.python.org/3", None),
}

autodoc_member_order = "bysource"
autodoc_default_flags = ["members", "special-members"]
44 changes: 44 additions & 0 deletions doc/development.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.. highlight:: sh

Development
===========

Prerequisites
-------------

- git
- tox_, not entirely required but probably a good idea in order to run
tests and maintenance tasks locally
- optionally something like pyenv_ in order to install multiple
version of Python and run tests with all of them, see
:download:`../tox.ini` for the currently supported list

Setup
-----

Clone the project from github::

$ git clone --recurse-submodule https://github.com/ua-parser/uap-python

The project mostly uses tox_ to run tests and maintenance task, `tox
exec`_ can be used to open shells in the environments for interactive
testing.

However it's also possible to install from the local copy, either via
an editable install::

$ pip install -e .

which allows editing the code and having it reflect immediately in the
installation (code generation excluded), or a regular install::

$ pip install .

which requires re-installing after each change.

Tox
---

.. _pyenv: https://github.com/pyenv/pyenv
.. _tox: https://tox.wiki/en/latest/index.html
.. _tox exec: https://tox.wiki/en/latest/cli_interface.html#tox-exec-(e)
24 changes: 24 additions & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.. ua-parser documentation master file, created by
sphinx-quickstart on Sun Jul 2 21:43:06 2023.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to ua-parser's documentation!
=====================================

.. toctree::
:maxdepth: 1
:caption: Contents:

quickstart <quickstart>
api
migrating
changelog
development

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

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
35 changes: 35 additions & 0 deletions doc/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

%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.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

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

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

:end
popd
22 changes: 22 additions & 0 deletions doc/migrating.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Migrating from 0.x
==================

While migrating is not quite necessary as the legacy API is not
getting removed just yet, it's also pretty simple:

- Remove one level of API, while the utility functions of the legacy
API live in ``ua_parser.user_agent_parser`` the new API lives in
``ua_parser`` at the root of the package.
- Convert the function names from ``PascalCase`` to ``snake_case``.
- Wrap the calls in ``dataclasses.asdict``: the functions now return
proper types, but these types are directly compatible with the old
results.

Alternatively, convert the caller code to leveraging the new typed
API, items become attributes, and optional attributes are `Optional
<https://docs.python.org/3/library/typing.html#typing.Optional>`_.

That should be it. If there are issues with the process, `just ask
<https://github.com/ua-parser/uap-python/discussions/categories/general>`_
or `open an issue
<https://github.com/ua-parser/uap-python/issues/new>`_.
1 change: 1 addition & 0 deletions doc/quickstart.rst
7 changes: 7 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ deps =
types-PyYaml
commands = mypy --check-untyped-defs --no-implicit-optional {posargs:src tests}

[testenv:doc]
deps =
sphinx
pyyaml
allowlist_externals = make, sphinx-build
commands = make -C doc {posargs}

[flake8]
max_line_length = 88
filename = src/ua_parser/

0 comments on commit 07bbec1

Please sign in to comment.