Skip to content

Commit

Permalink
Merge pull request #33 from wsanchez/docs
Browse files Browse the repository at this point in the history
Add documentation to build
  • Loading branch information
wsanchez committed Feb 3, 2017
2 parents a0009dd + 8ddc26d commit 9b276a8
Show file tree
Hide file tree
Showing 20 changed files with 238 additions and 125 deletions.
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ install:
matrix:
include:
- python: 3.5
env: TOXENV=flake8,mypy
env: TOXENV=flake8

- python: 3.5
env: TOXENV=mypy

- python: 3.5
env: TOXENV=docs

- python: 3.5
env: TOXENV=py35-coverage,coverage_codecov
Expand Down
8 changes: 1 addition & 7 deletions bin/develop
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,11 @@ venv_init () {

echo "Installing Tox...";
"${python}" -m pip install --upgrade tox;

echo "Installing mock...";
"${python}" -m pip install --upgrade mock;

echo "Installing Hypothesis...";
"${python}" -m pip install --upgrade hypothesis;
}

venv_install_requirements () {
echo "Installing required packages...";
"${python}" -m pip install --requirement "${wd}/requirements.txt";
"${python}" -m pip install --requirement "${wd}/requirements-dev.txt";
}


Expand Down
45 changes: 45 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
extensions = ["sphinx.ext.autodoc"]

# Project info

project = "sample-klein-app"
copyright = "2016-2017"
author = u"Wilfredo S\xe1nchez Vega"

# File names

templates_path = []
html_static_path = []
source_suffix = [".rst", ".md"]
master_doc = "index"
exclude_patterns = []

# Styling

html_theme = "sphinx_rtd_theme"

# Pedantry

nitpicky = True
nitpick_ignore = [
# Bugs in Python documentation
("py:class", "float" ),
("py:class", "int" ),
("py:class", "object" ),
("py:class", "Union" ),
("py:data" , "sys.argv" ),
("py:exc" , "ValueError"),
("py:obj" , "None" ),

# Need to learn how to intersphinx with Twisted
("py:class", "twisted.internet.defer.Deferred" ),
("py:class", "twisted.trial._synctest.SynchronousTestCase" ),
("py:class", "twisted.trial.unittest.SynchronousTestCase" ),
("py:class", "twisted.trial.unittest.TestCase" ),
("py:const", "twisted.web.http.NOT_FOUND" ),
("py:meth" , "twisted.trial.unittest.SynchronousTestCase.successResultOf"),
("py:mod" , "twisted.trial.unittest" ),

# Need to learn how to intersphinx with Klein
("py:class", "klein.app.Klein"),
]
16 changes: 16 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Sample Klein Application
========================

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

sample_klein_app


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

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
3 changes: 3 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-r requirements.txt
mock
hypothesis
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@

def main():
"""
Run L{setup}.
Run :func:`setup`.
"""
setup(
name=name,
Expand Down
7 changes: 3 additions & 4 deletions src/sample_klein_app/application/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
Shared main function.
"""

import sys
from typing import Sequence


def main(applicationClass, argv: Sequence[str] = sys.argv) -> None:
def main(applicationClass, argv: Sequence[str] = None) -> None:
"""
Executable entry point
@param applicationClass: A Klein application to run.
:param applicationClass: A Klein application to run.
@param argv: Command line arguments.
:param argv: Command line arguments. If :obj:`None`, use :data:`sys.argv`.
"""
application = applicationClass()
application.router.run("localhost", 8080)
14 changes: 7 additions & 7 deletions src/sample_klein_app/application/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def root(self, request: IRequest) -> KleinRenderable:
Responds with a message noting the nature of the application.
@param request: The request to respond to.
:param request: The request to respond to.
"""
return "This is a web application composed from multiple applications."

Expand All @@ -44,9 +44,9 @@ def dns(self, request: IRequest) -> KleinRenderable:
"""
DNS application resource.
Routes requests to L{DNSApplication}.
Routes requests to :class:`.dns.Application`.
@param request: The request to respond to.
:param request: The request to respond to.
"""
return DNSApplication().router.resource()

Expand All @@ -55,9 +55,9 @@ def hello(self, request: IRequest) -> KleinRenderable:
"""
Hello application resource.
Routes requests to L{HelloApplication}.
Routes requests to :class:`.hello.Application`.
@param request: The request to respond to.
:param request: The request to respond to.
"""
return HelloApplication().router.resource()

Expand All @@ -66,9 +66,9 @@ def math(self, request: IRequest) -> KleinRenderable:
"""
Math application resource.
Routes requests to L{MathApplication}.
Routes requests to :class:`.math.Application`.
@param request: The request to respond to.
:param request: The request to respond to.
"""
return MathApplication().router.resource()

Expand Down
6 changes: 3 additions & 3 deletions src/sample_klein_app/application/dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def root(self, request: IRequest) -> KleinRenderable:
Responds with a message noting the nature of the application.
@param request: The request to respond to.
:param request: The request to respond to.
"""
return "DNS API."

Expand All @@ -47,9 +47,9 @@ async def hostname(self, request: IRequest, name: str) -> KleinRenderable:
Performs a lookup on the given name and responds with the resulting
address.
@param request: The request to respond to.
:param request: The request to respond to.
@param name: A host name.
:param name: A host name.
"""
try:
address = await getHostByName(name)
Expand Down
2 changes: 1 addition & 1 deletion src/sample_klein_app/application/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def hello(self, request: IRequest) -> KleinRenderable:
Responds with a message noting the nature of the application.
@param request: The request to respond to.
:param request: The request to respond to.
"""
return "Hello!"

Expand Down
14 changes: 13 additions & 1 deletion src/sample_klein_app/application/klein.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from twisted.web.iweb import IRenderable
from twisted.web.resource import IResource

from klein import Klein
from klein import Klein as SuperKlein


__all__ = (
Expand All @@ -18,3 +18,15 @@

# Expected return types for route methods
KleinRenderable = Union[str, IResource, IRenderable]


#
# Subclass Klein so that we can override its documentation to work with Sphinx;
# Klein uses epydoc syntax, which is not compatible, and because the router
# is an attribute of each application, its doc string shows up in our
# documentation.
#
class Klein(SuperKlein):
"""
Request router.
"""
34 changes: 17 additions & 17 deletions src/sample_klein_app/application/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def root(self, request: IRequest) -> KleinRenderable:
Responds with a message noting the nature of the application.
@param request: The request to respond to.
:param request: The request to respond to.
"""
return "Math happens here."

Expand All @@ -45,11 +45,11 @@ def add(self, request: IRequest, a: str, b: str) -> KleinRenderable:
Adds the two given numbers and responds with the result.
@param request: The request to respond to.
:param request: The request to respond to.
@param a: A number to add to C{b}.
:param a: A number to add to ``b``.
@param b: A number to add to C{a}.
:param b: A number to add to ``a``.
"""
x = self.numberify(a) + self.numberify(b) # type: ignore #see #21
return "{}".format(x)
Expand All @@ -62,11 +62,11 @@ def subtract(self, request: IRequest, a: str, b: str) -> KleinRenderable:
Subtracts one of two given numbers from the other and responds with the
result.
@param request: The request to respond to.
:param request: The request to respond to.
@param a: A number to subtract C{b} from.
:param a: A number to subtract ``b`` from.
@param b: A number to subtract from C{a}.
:param b: A number to subtract from ``a``.
"""
x = self.numberify(a) - self.numberify(b) # type: ignore #see #21
return "{}".format(x)
Expand All @@ -78,11 +78,11 @@ def multiply(self, request: IRequest, a: str, b: str) -> KleinRenderable:
Multiplies the two given numbers and responds with the result.
@param request: The request to respond to.
:param request: The request to respond to.
@param a: A number to multiply with C{b}.
:param a: A number to multiply with ``b``.
@param b: A number to multiply with C{a}.
:param b: A number to multiply with ``a``.
"""
x = self.numberify(a) * self.numberify(b) # type: ignore #see #21
return "{}".format(x)
Expand All @@ -95,23 +95,23 @@ def divide(self, request: IRequest, a: str, b: str) -> KleinRenderable:
Divides one of two given numbers from the other and responds with the
result.
@param request: The request to respond to.
:param request: The request to respond to.
@param a: A number to divide by C{b}.
:param a: A number to divide by ``b``.
@param b: A number to divide C{a} by.
:param b: A number to divide ``a`` by.
"""
x = self.numberify(a) / self.numberify(b) # type: ignore #see #21
return "{}".format(x)

@router.handle_errors(ValueError)
def valueError(self, request: IRequest, failure) -> KleinRenderable:
"""
Error handler for L{ValueError}.
Error handler for :exc:`ValueError`.
@param request: The request to respond to.
:param request: The request to respond to.
@param failure: The failure that occurred.
:param failure: The failure that occurred.
"""
request.setResponseCode(http.BAD_REQUEST)
return "Invalid inputs provided."
Expand All @@ -121,7 +121,7 @@ def numberify(string: str) -> Union[int, float]:
"""
Convert a string into a number.
@param string: A string to convert into a number.
:param string: A string to convert into a number.
"""
try:
return int(string)
Expand Down
2 changes: 1 addition & 1 deletion src/sample_klein_app/application/test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""
Tests for L{sample_klein_app.application}.
Tests for :mod:`sample_klein_app.application`.
"""
14 changes: 7 additions & 7 deletions src/sample_klein_app/application/test/mock_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ async def assertResponse(
Generate and process a request using the given application and assert
that the response is as expected.
@param application: The application to route the request to.
:param application: The application to route the request to.
@param request_path: The path portion of the request URI.
:param request_path: The path portion of the request URI.
@param response_code: The expected HTTP status code for the response.
:param response_code: The expected HTTP status code for the response.
@param response_data: The expected HTTP entity body data for the response.
:param response_data: The expected HTTP entity body data for the response.
@param response_location_path: The expected C{"Location"} HTTP header value
:param response_location_path: The expected ``Location`` HTTP header value
for the response.
"""
request = mock_request(request_path)
Expand Down Expand Up @@ -64,9 +64,9 @@ async def render(application, request: IRequest) -> None:
"""
Render a response from the given application for the given request.
@param application: The application to route the request to.
:param application: The application to route the request to.
@param request: The request to route the request to the application.
:param request: The request to route the request to the application.
"""
resource = application.router.resource()
await _render(resource, request, notifyFinish=True)

0 comments on commit 9b276a8

Please sign in to comment.