Skip to content

Commit

Permalink
Stop using **kw: Any in formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
mnot committed Dec 14, 2023
1 parent fb31204 commit b3216d0
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 45 deletions.
5 changes: 4 additions & 1 deletion redbot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ def main() -> None:
resource.set_request(args.url)

formatter = find_formatter(args.output_format, "text", args.descend)(
config, resource, output, tty_out=sys.stdout.isatty(), descend=args.descend
config,
resource,
output,
{"tty_out": sys.stdout.isatty(), "descend": args.descend},
)

formatter.bind_resource(resource)
Expand Down
19 changes: 16 additions & 3 deletions redbot/formatter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
import locale
import sys
import time
from typing import Optional, Any, Callable, List, Dict, Type, TYPE_CHECKING
from typing import Optional, Any, Callable, List, Dict, Tuple, Type, TYPE_CHECKING
import unittest

from markdown import Markdown
import thor
from thor.events import EventEmitter
from typing_extensions import TypedDict

if TYPE_CHECKING:
from redbot.resource import HttpResource # pylint: disable=cyclic-import
Expand Down Expand Up @@ -68,6 +69,18 @@ def available_formatters() -> List[str]:
return _formatters


class FormatterParams(TypedDict):
config: SectionProxy
resource: "HttpResource"
output: Callable[[str], None]
params: Dict[str, Any]


FormatterArgs = Tuple[
SectionProxy, "HttpResource", Callable[[str], None], Dict[str, Any]
]


class Formatter(EventEmitter):
"""
A formatter for HttpResources.
Expand All @@ -84,7 +97,7 @@ def __init__(
config: SectionProxy,
resource: "HttpResource",
output: Callable[[str], None],
**kw: Any,
params: Dict[str, Any],
) -> None:
"""
Formatter for the given URI, writing
Expand All @@ -96,7 +109,7 @@ def __init__(
self.resource = resource
self.lang = config["lang"]
self.output = output # output file object
self.kw = kw # extra keyword arguments
self.kw = params
self._markdown = Markdown(output_format="html")

def bind_resource(self, display_resource: "HttpResource") -> None:
Expand Down
8 changes: 4 additions & 4 deletions redbot/formatter/har.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import datetime
import json
from typing import Optional, Any, Dict, List
from typing_extensions import TypedDict
from typing_extensions import TypedDict, Unpack

from redbot import __version__
from redbot.formatter import Formatter
from redbot.formatter import Formatter, FormatterArgs
from redbot.resource import HttpResource
from redbot.type import StrHeaderListType

Expand All @@ -35,8 +35,8 @@ class HarFormatter(Formatter):
name = "har"
media_type = "application/json"

def __init__(self, *args: Any, **kw: Any) -> None:
Formatter.__init__(self, *args, **kw)
def __init__(self, *args: Unpack[FormatterArgs]) -> None:
Formatter.__init__(self, *args)
self.har: HarDict = {
"log": {
"version": "1.1",
Expand Down
6 changes: 4 additions & 2 deletions redbot/formatter/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
from typing import Any, List, Match, Tuple
from urllib.parse import urljoin

from typing_extensions import Unpack
from httplint import get_field_description
from httplint.note import Note, levels, categories
import thor.http.error as httperr

from redbot import __version__
from redbot.formatter import FormatterArgs
from redbot.formatter.html_base import (
BaseHtmlFormatter,
e_query_arg,
Expand Down Expand Up @@ -77,8 +79,8 @@ class SingleEntryHtmlFormatter(BaseHtmlFormatter):

name = "html"

def __init__(self, *args: Any, **kw: Any) -> None:
BaseHtmlFormatter.__init__(self, *args, **kw)
def __init__(self, *args: Unpack[FormatterArgs]) -> None:
BaseHtmlFormatter.__init__(self, *args)
self.templates.filters.update(
{
"header_present": self.format_header,
Expand Down
9 changes: 5 additions & 4 deletions redbot/formatter/html_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import json
import os
import time
from typing import Optional, Any, List, Tuple
from typing import Optional, List, Tuple
from urllib.parse import urljoin, urlencode, quote as urlquote

import httplint
from jinja2 import Environment, PackageLoader, select_autoescape
from markupsafe import Markup, escape
from typing_extensions import Unpack

import redbot
from redbot.formatter import Formatter, relative_time, f_num
from redbot.formatter import Formatter, FormatterArgs, relative_time, f_num
from redbot.webui.captcha import CAPTCHA_PROVIDERS

NL = "\n"
Expand Down Expand Up @@ -65,8 +66,8 @@ class BaseHtmlFormatter(Formatter):
),
)

def __init__(self, *args: Any, **kw: Any) -> None:
Formatter.__init__(self, *args, **kw)
def __init__(self, *args: Unpack[FormatterArgs]) -> None:
Formatter.__init__(self, *args)
self.templates.filters.update(
{
"f_num": f_num,
Expand Down
7 changes: 4 additions & 3 deletions redbot/formatter/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

import json
from typing import Optional, Any, List, Dict, Union
from typing_extensions import Unpack

from httplint import HttpResponseLinter
from httplint.note import categories, levels

from redbot.formatter import Formatter
from redbot.formatter import Formatter, FormatterArgs
from redbot.resource import HttpResource
from redbot.resource.fetch import RedHttpClient

Expand Down Expand Up @@ -37,8 +38,8 @@ class SlackFormatter(Formatter):
levels.INFO: ":information_source:",
}

def __init__(self, *args: Any, **kw: Any) -> None:
Formatter.__init__(self, *args, **kw)
def __init__(self, *args: Unpack[FormatterArgs]) -> None:
Formatter.__init__(self, *args)

def start_output(self) -> None:
pass
Expand Down
24 changes: 11 additions & 13 deletions redbot/formatter/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import operator
import re
import textwrap
from typing import Any, List, Optional
from typing import List, Optional
from typing_extensions import Unpack

from httplint import HttpResponseLinter
from httplint.note import Note, levels, categories
import thor.http.error as httperr

from redbot.formatter import Formatter
from redbot.formatter import Formatter, FormatterArgs
from redbot.resource import HttpResource

NL = "\n"
Expand Down Expand Up @@ -44,8 +45,8 @@ class BaseTextFormatter(Formatter):

error_template = "Error: %s\n"

def __init__(self, *args: Any, **kw: Any) -> None:
Formatter.__init__(self, *args, **kw)
def __init__(self, *args: Unpack[FormatterArgs]) -> None:
Formatter.__init__(self, *args)
self.verbose = False

def start_output(self) -> None:
Expand Down Expand Up @@ -146,18 +147,15 @@ class TextFormatter(BaseTextFormatter):
name = "txt"
media_type = "text/plain"

def __init__(self, *args: Any, **kw: Any) -> None:
BaseTextFormatter.__init__(self, *args, **kw)

def finish_output(self) -> None:
BaseTextFormatter.finish_output(self)


class VerboseTextFormatter(TextFormatter):
name = "txt_verbose"

def __init__(self, *args: Any, **kw: Any) -> None:
TextFormatter.__init__(self, *args, **kw)
def __init__(self, *args: Unpack[FormatterArgs]) -> None:
TextFormatter.__init__(self, *args)
self.verbose = True


Expand All @@ -170,8 +168,8 @@ class TextListFormatter(BaseTextFormatter):
media_type = "text/plain"
can_multiple = True

def __init__(self, *args: Any, **kw: Any) -> None:
BaseTextFormatter.__init__(self, *args, **kw)
def __init__(self, *args: Unpack[FormatterArgs]) -> None:
BaseTextFormatter.__init__(self, *args)

def finish_output(self) -> None:
"Fill in the template with RED's results."
Expand All @@ -194,8 +192,8 @@ def format_uri(self, resource: HttpResource) -> str:
class VerboseTextListFormatter(TextListFormatter):
name = "txt_verbose"

def __init__(self, *args: Any, **kw: Any) -> None:
TextListFormatter.__init__(self, *args, **kw)
def __init__(self, *args: Unpack[FormatterArgs]) -> None:
TextListFormatter.__init__(self, *args)
self.verbose = True


Expand Down
22 changes: 14 additions & 8 deletions redbot/webui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ def __init__(
self.config,
HttpResource(self.config),
self.output,
nonce=self.nonce,
{
"nonce": self.nonce,
},
),
b"405",
b"Method Not Allowed",
Expand All @@ -144,11 +146,13 @@ def run_test(self) -> None:
self.config,
top_resource,
self.output,
allow_save=self.test_id,
is_saved=False,
test_id=self.test_id,
descend=self.descend,
nonce=self.nonce,
{
"allow_save": self.test_id,
"is_saved": False,
"test_id": self.test_id,
"descend": self.descend,
"nonce": self.nonce,
},
)
continue_test = partial(self.continue_test, top_resource, formatter)
error_response = partial(self.error_response, formatter)
Expand Down Expand Up @@ -281,8 +285,10 @@ def show_default(self) -> None:
self.config,
resource,
self.output,
is_blank=self.test_uri == "",
nonce=self.nonce,
{
"is_blank": self.test_uri == "",
"nonce": self.nonce,
},
)
if self.check_name:
formatter.resource = cast(
Expand Down
10 changes: 6 additions & 4 deletions redbot/webui/saved_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,12 @@ def load_saved_test(webui: "RedWebUi") -> None:
webui.config,
display_resource,
webui.output,
allow_save=(not is_saved),
is_saved=True,
test_id=webui.test_id,
nonce=webui.nonce,
{
"allow_save": (not is_saved),
"is_saved": True,
"test_id": webui.test_id,
"nonce": webui.nonce,
},
)

webui.exchange.response_start(
Expand Down
9 changes: 6 additions & 3 deletions redbot/webui/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ def slack_run(webui: "RedWebUi") -> None:
webui.test_uri = webui.body_args.get("text", [""])[0].strip()
webui.test_id = init_save_file(webui)
slack_response_uri = webui.body_args.get("response_url", [""])[0].strip()
resource = HttpResource(webui.config)
formatter = slack.SlackFormatter(
webui.config,
None,
resource,
webui.output,
slack_uri=slack_response_uri,
test_id=webui.test_id,
{
"slack_uri": slack_response_uri,
"test_id": webui.test_id,
},
)

webui.exchange.response_start(
Expand Down

0 comments on commit b3216d0

Please sign in to comment.