Skip to content

Commit

Permalink
Linter fixes and pre-commit update
Browse files Browse the repository at this point in the history
  • Loading branch information
ondratu committed Jul 1, 2024
1 parent c245ccc commit 7e7512f
Show file tree
Hide file tree
Showing 20 changed files with 465 additions and 435 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rebase.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ jobs:
name: Rebase
runs-on: ubuntu-latest
if: >-
github.event.issue.pull_request != '' &&
github.event.issue.pull_request != '' &&
(
contains(github.event.comment.body, '/rebase') ||
contains(github.event.comment.body, '/rebase') ||
contains(github.event.comment.body, '/autosquash')
)
steps:
Expand Down
13 changes: 10 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
---
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.6.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.0.270'
rev: 'v0.5.0'
hooks:
- id: ruff
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
rev: 7.1.0
hooks:
- id: flake8
- repo: https://github.com/Lucas-C/pre-commit-hooks-markup
Expand All @@ -27,3 +27,10 @@ repos:
rev: v3.0.0a5
hooks:
- id: pylint
language: system
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v1.10.1' # Use the sha / tag you want to point at
hooks:
- id: mypy
args: [--explicit-package-bases]
language: system
2 changes: 1 addition & 1 deletion doc/documentation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ CachedInput
~~~~~~~~~~~

When HTTP Forms are base64 encoded, FieldStorage use readline on request input
file. This is not so optimal. So there is CachedInput class, which is returned
file. This is not so optimal. So there is CachedInput class, which is returned

Proccess variables
~~~~~~~~~~~~~~~~~~
Expand Down
161 changes: 62 additions & 99 deletions examples/http_digest.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
"""HTTP WWW-Authenticate Digest Example."""
from wsgiref.simple_server import make_server
from sys import path as python_path
from os import path
import logging
from hashlib import sha256
from os import path
from sys import path as python_path
from time import time
from wsgiref.simple_server import make_server

import logging

python_path.insert(0, path.abspath(
path.join(path.dirname(__file__), path.pardir)))
python_path.insert(
0, path.abspath(path.join(path.dirname(__file__), path.pardir)))

# pylint: disable=wrong-import-position
from poorwsgi import Application, state # noqa
from poorwsgi import Application, state # noqa
from poorwsgi.digest import PasswordMap, check_digest, hexdigest # noqa
from poorwsgi.response import EmptyResponse, redirect # noqa
from poorwsgi.digest import check_digest, PasswordMap, hexdigest # noqa

FILE = path.join(path.dirname(__file__), 'test.digest')

Expand All @@ -38,151 +37,119 @@
def get_header(title):
"""Return HTML header list of lines."""
return (
"<html>",
"<head>",
"<html>", "<head>",
'<meta http-equiv="content-type" content="text/html; charset=utf-8"/>',
"<title>%s - %s</title>" % (__file__, title),
"</head>",
"<body>",
"<h1>%s - %s</h1>" % (__file__, title)
)
f"<title>{__file__} - {title}</title>", "</head>", "<body>",
f"<h1>{__file__} - {title}</h1>")


def get_footer():
"""Return HTML footer list of lines."""
return (
"<hr>",
"<small>Copyright (c) 2020 Ondřej Tůma. See ",
'<a href="http://poorhttp.zeropage.cz">poorhttp.zeropage.cz</a>'
'</small>.',
"</body>",
"</html>"
)
return ("<hr>", "<small>Copyright (c) 2020 Ondřej Tůma. See ",
'<a href="http://poorhttp.zeropage.cz">poorhttp.zeropage.cz</a>'
'</small>.', "</body>", "</html>")


def get_link(href, text=None, title=None):
"""Return HTML anchor."""
text = text or title or href
title = title or text
return '<a href="%s" title="%s">%s</a>' % (href, title, text)
return f'<a href="{href}" title="{title}">{text}</a>'


@app.route('/')
def root(req):
"""Return Root (Index) page."""
body = (
'<ul>',
'<li>%s - admin zone (admin/admin)</li>' % get_link('/admin_zone'),
'<li>%s - user zone (user/looser;sha/sha)</li>' % get_link('/user_zone'),
'<li>%s - user (user/looser)</li>' % get_link('/user'),
'<li>%s - utf-8 (Ondřej/heslíčko)</li>' % get_link('/user/utf-8'),
'<li>%s - foo (foo/bar)</li>' % get_link('/foo'),
'<li>%s - unknown</li>' % get_link('/unknown'),
'<li>%s - spaces in url</li>' % get_link('/spaces in url'),
'<li>%s - diacitics in url</li>' % get_link('/čeština v url'),
'<li>%s - unicode in url</li>' % get_link('/crazy in url 🤪'),
'</ul>'
)
body = ('<ul>', "<li>" + get_link('/admin_zone') +
" - admin zone (admin/admin)</li>", "<li>" +
get_link('/user_zone') + " - user zone (user/looser;sha/sha)</li>",
"<li>" + get_link('/user') + " - user (user/looser)</li>", "<li>" +
get_link('/user/utf-8') + " - utf-8 (Ondřej/heslíčko)</li>",
"<li>" + get_link('/foo') + " - foo (foo/bar)</li>",
"<li>" + get_link('/unknown') + " - unknown</li>",
"<li>" + get_link('/spaces in url') + " - spaces in url</li>",
"<li>" + get_link('/čeština v url') + " - diacitics in url</li>",
"<li>" + get_link('/crazy in url 🤪') + " - unicode in url</li>",
'</ul>')

for line in get_header("Root") + body + get_footer():
yield line.encode()+b'\n'
yield line.encode() + b'\n'


@app.route('/admin_zone')
@check_digest(ADMIN)
def admin_zone(req):
"""Page only for ADMIN realm."""
body = (
'<h2>%s test for %s algorithm.</h2>' % (ADMIN, app.auth_algorithm),
'<ul>',
'<li>'+get_link('/', 'Root')+'</li>',
'<li>'+get_link('/admin_zone?arg=42', 'one more time')+'</li>',
'</ul>'
)
body = (f'<h2>{ADMIN} test for {app.auth_algorithm} algorithm.</h2>',
'<ul>', '<li>' + get_link('/', 'Root') + '</li>',
'<li>' + get_link('/admin_zone?arg=42', 'one more time') + '</li>',
'</ul>')

for line in get_header("Root") + body + get_footer():
yield line.encode()+b'\n'
yield line.encode() + b'\n'


@app.route('/user_zone')
@check_digest(USER)
def user_zone(req):
"""Page for USER realm."""
body = (
'<h2>%s test for %s algorithm.</h2>' % (USER, app.auth_algorithm),
'User: %s' % req.user,
'<ul>',
'<li>'+get_link('/', 'Root')+'</li>',
'<li>'+get_link('/user_zone?param=text', 'one more time')+'</li>',
'</ul>'
)
body = (f'<h2>{USER} test for {app.auth_algorithm} algorithm.</h2>',
f'User: {req.user}', '<ul>',
'<li>' + get_link('/', 'Root') + '</li>', '<li>' +
get_link('/user_zone?param=text', 'one more time') + '</li>',
'</ul>')

for line in get_header("Root") + body + get_footer():
yield line.encode()+b'\n'
yield line.encode() + b'\n'


@app.route('/user')
@check_digest(USER, 'user')
def user_only(req):
"""Page for user only."""
body = (
'<h2>User test for %s algorithm.</h2>' % app.auth_algorithm,
'User: %s' % req.user,
'<ul>',
'<li>'+get_link('/', 'Root')+'</li>',
'<li>'+get_link('/user?param=1234', 'one more time')+'</li>',
'</ul>'
)
body = (f'<h2>User test for {app.auth_algorithm} algorithm.</h2>',
f'User: {req.user}', '<ul>',
'<li>' + get_link('/', 'Root') + '</li>', '<li>' +
get_link('/user?param=1234', 'one more time') + '</li>', '</ul>')

for line in get_header("Root") + body + get_footer():
yield line.encode()+b'\n'
yield line.encode() + b'\n'


@app.route('/foo')
@check_digest(USER, 'foo')
def foo_only(req):
"""Page for foo user only."""
body = (
'<h2>Foo test for %s algorithm.</h2>' % app.auth_algorithm,
'User: %s' % req.user,
'<ul>',
'<li>'+get_link('/', 'Root')+'</li>',
'</ul>',
'<form method="post" action="/foo/passwd">',
'<label>Enter new password:',
'<input type="password" name="password"/>',
'</label>',
'<button type="submit">Change Password</button>',
'</form>'
)
body = (f'<h2>Foo test for {app.auth_algorithm} algorithm.</h2>',
f'User: {req.user}', '<ul>', '<li>' + get_link('/', 'Root') +
'</li>', '</ul>', '<form method="post" action="/foo/passwd">',
'<label>Enter new password:',
'<input type="password" name="password"/>', '</label>',
'<button type="submit">Change Password</button>', '</form>')

for line in get_header("Root") + body + get_footer():
yield line.encode()+b'\n'
yield line.encode() + b'\n'


@app.route('/user/utf-8')
@check_digest(USER, 'Ondřej')
def utf8_chars(req):
"""Page for user only."""
body = (
'<h2>User test for %s algorithm.</h2>' % app.auth_algorithm,
'User: %s' % req.user,
'<ul>',
'<li>'+get_link('/', 'Root')+'</li>',
'<li>'+get_link('/user/utf-8?param=1234', 'one more time')+'</li>',
'</ul>'
)
body = (f'<h2>User test for {app.auth_algorithm} algorithm.</h2>',
f'User: {req.user}', '<ul>',
'<li>' + get_link('/', 'Root') + '</li>', '<li>' +
get_link('/user/utf-8?param=1234', 'one more time') + '</li>',
'</ul>')

for line in get_header("Root") + body + get_footer():
yield line.encode()+b'\n'
yield line.encode() + b'\n'


@app.route('/foo/passwd', method=state.METHOD_POST)
@check_digest(USER, 'foo')
def foo_password(req):
"""Change foo's password."""
digest = hexdigest(req.user, USER, req.form.get('password'),
app.auth_hash)
digest = hexdigest(req.user, USER, req.form.get('password'), app.auth_hash)
app.auth_map.set(USER, req.user, digest)
redirect('/foo')

Expand All @@ -196,17 +163,13 @@ def unknown_endpoint(req):

def generic_response(url, user):
"""Return generic response"""
body = (
'<h2>%s test for %s algorithm.</h2>' % (USER, app.auth_algorithm),
'User: %s' % user,
'<ul>',
'<li>'+get_link('/', 'Root')+'</li>',
'<li>'+get_link(url+'?param=text', 'one more time')+'</li>',
'</ul>'
)
body = (f'<h2>{USER} test for {app.auth_algorithm} algorithm.</h2>',
f'User: {user}', '<ul>', '<li>' + get_link('/', 'Root') + '</li>',
'<li>' + get_link(url + '?param=text', 'one more time') + '</li>',
'</ul>')

for line in get_header("Root") + body + get_footer():
yield line.encode()+b'\n'
yield line.encode() + b'\n'


@app.route('/spaces in url')
Expand Down
16 changes: 9 additions & 7 deletions examples/large_file.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Large file upload test."""
# pylint: disable=duplicate-code

from wsgiref.simple_server import make_server, WSGIServer
from socketserver import ThreadingMixIn
Expand All @@ -14,11 +15,11 @@
sys.path.insert(0, os.path.abspath(
os.path.join(EXAMPLES_PATH, os.path.pardir)))

# pylint: disable=import-error, disable=wrong-import-position
# pylint: disable=wrong-import-position
from poorwsgi import Application, state # noqa
from poorwsgi.request import FieldStorage # noqa
from poorwsgi.response import HTTPException # noqa
from poorwsgi.results import hbytes # noqa
from poorwsgi.results import hbytes # noqa

logger = log.getLogger()
logger.setLevel("DEBUG")
Expand All @@ -31,6 +32,7 @@

class Blackhole:
"""Dummy File Object"""

def __init__(self, filename):
log.debug("Start uploading file: %s", filename)
self.uploaded = 0
Expand All @@ -56,6 +58,7 @@ def hexdigest(self):

class Temporary:
"""Temporary file"""

def __init__(self, filename):
log.debug("Start uploading file: %s", filename)
self.uploaded = 0
Expand Down Expand Up @@ -152,11 +155,10 @@ def html_form(req, file_callback):
hexdigest = form['file'].file.hexdigest()

end = time() - start
args = (hbytes(bytes_read) +
(int(end),) +
hbytes(bytes_read / end) +
(hexdigest,))
stats = ("Upload: %.2f%s in %ds -> %.2f%sps SHA256: %s" % args)
size = hbytes(bytes_read)
speed = hbytes(bytes_read / end)
stats = (f"Upload: {size[0]:.2f}{size[1]} in {end}s -> "
f"{speed[0]:.2f}{speed[1]}ps SHA256: {hexdigest}")
log.info(stats)

if bytes_read != req.content_length:
Expand Down
1 change: 1 addition & 0 deletions examples/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
python_path.insert(0, os.path.abspath(
os.path.join(EXAMPLES_PATH, os.path.pardir)))

# pylint: disable=wrong-import-position
from poorwsgi import Application, state # noqa
from poorwsgi.response import JSONResponse # noqa

Expand Down
4 changes: 2 additions & 2 deletions examples/openapi3.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from poorwsgi import Application, state # noqa
from poorwsgi.response import Response, abort, HTTPException, \
JSONResponse # noqa
from poorwsgi.request import Request
from poorwsgi.request import Request # noqa
from poorwsgi.openapi_wrapper import OpenAPIRequest, \
OpenAPIResponse # noqa
from poorwsgi.session import PoorSession # noqa
Expand Down Expand Up @@ -111,7 +111,7 @@ def after_each_response(req, res):
return res
except OpenAPIError as error:
log.error("API output error: %s", str(error))
assert False, f"OpenAPI Error {str(error)}"
raise
return res


Expand Down
Loading

0 comments on commit 7e7512f

Please sign in to comment.