From 00f2af274ea8f1bc53898d8d84104b8e78aeb9bf Mon Sep 17 00:00:00 2001 From: Andriy Ivaneyko Date: Wed, 6 Dec 2023 05:03:35 -0500 Subject: [PATCH] Introduce ruff execution on the examples and scripts folder #2867 (#2869) --- Makefile | 4 ++-- examples/amending_request_object.py | 1 + examples/exception_monitoring.py | 3 +-- examples/http_redirect.py | 1 + examples/log_request_id.py | 5 ++++- examples/modify_header_example.py | 2 +- examples/request_stream/client.py | 1 + examples/run_async_advanced.py | 1 + scripts/changelog.py | 9 ++++++--- scripts/release.py | 15 +++++++++------ 10 files changed, 27 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index f43db1e28b..f27e956d53 100644 --- a/Makefile +++ b/Makefile @@ -53,11 +53,11 @@ docker-test: clean .PHONY: fix fix: - ruff check sanic --fix + ruff check sanic examples scripts --fix .PHONY: format format: - ruff format sanic + ruff format sanic examples scripts .PHONY: pretty pretty: fix format diff --git a/examples/amending_request_object.py b/examples/amending_request_object.py index 63f8d1d5d3..3c5bb59ddb 100644 --- a/examples/amending_request_object.py +++ b/examples/amending_request_object.py @@ -25,5 +25,6 @@ def key_exist_handler(request): return text("num does not exist in request") + if __name__ == "__main__": app.run(host="0.0.0.0", port=8000, debug=True) diff --git a/examples/exception_monitoring.py b/examples/exception_monitoring.py index eb52ed9a59..00e7ca4bf2 100644 --- a/examples/exception_monitoring.py +++ b/examples/exception_monitoring.py @@ -7,6 +7,7 @@ class' default handler, we can do anything including sending exceptions to an external service. """ +from sanic import Sanic from sanic.exceptions import SanicException from sanic.handlers import ErrorHandler @@ -37,8 +38,6 @@ def default(self, request, exception): server's error_handler to an instance of our CustomHandler """ -from sanic import Sanic - handler = CustomHandler() app = Sanic("Example", error_handler=handler) diff --git a/examples/http_redirect.py b/examples/http_redirect.py index 34dc43abc7..2a8c69c540 100644 --- a/examples/http_redirect.py +++ b/examples/http_redirect.py @@ -69,5 +69,6 @@ async def runner(app: Sanic, app_server: AsyncioServer): app.is_running = False app.is_stopping = True + if __name__ == "__main__": https.run(port=HTTPS_PORT, debug=True) diff --git a/examples/log_request_id.py b/examples/log_request_id.py index 87825640df..b4cfe85ea2 100644 --- a/examples/log_request_id.py +++ b/examples/log_request_id.py @@ -35,7 +35,10 @@ def filter(self, record): }, "formatters": { "default": { - "format": "%(asctime)s %(levelname)s %(name)s:%(lineno)d %(request_id)s | %(message)s", + "format": ( + "%(asctime)s %(levelname)s %(name)s:%(lineno)d" + " %(request_id)s | %(message)s" + ), }, }, "loggers": { diff --git a/examples/modify_header_example.py b/examples/modify_header_example.py index 6455cb85b5..0526b49999 100644 --- a/examples/modify_header_example.py +++ b/examples/modify_header_example.py @@ -18,7 +18,7 @@ def handle_request(request): @app.route("/unauthorized") -def handle_request(request): +def handle_unauthorized_request(request): return response.json( {"message": "You are not authorized"}, headers={"X-Served-By": "sanic"}, diff --git a/examples/request_stream/client.py b/examples/request_stream/client.py index c6c4f73ded..b9565634f2 100644 --- a/examples/request_stream/client.py +++ b/examples/request_stream/client.py @@ -1,5 +1,6 @@ import requests + # Warning: This is a heavy process. data = "" diff --git a/examples/run_async_advanced.py b/examples/run_async_advanced.py index c782d49a4d..917beefa9c 100644 --- a/examples/run_async_advanced.py +++ b/examples/run_async_advanced.py @@ -35,6 +35,7 @@ async def after_server_stop(app, loop): async def test(request): return response.json({"answer": "42"}) + if __name__ == "__main__": asyncio.set_event_loop(uvloop.new_event_loop()) serv_coro = app.create_server( diff --git a/scripts/changelog.py b/scripts/changelog.py index 8848c42605..4ed3a93975 100755 --- a/scripts/changelog.py +++ b/scripts/changelog.py @@ -1,15 +1,18 @@ #!/usr/bin/env python -from os import path import sys +from os import path + + if __name__ == "__main__": try: - import towncrier import click + import towncrier except ImportError: print( - "Please make sure you have a installed towncrier and click before using this tool" + "Please make sure you have installed towncrier and " + "click before using this tool" ) sys.exit(1) diff --git a/scripts/release.py b/scripts/release.py index e2b9b88711..547e53bfdc 100755 --- a/scripts/release.py +++ b/scripts/release.py @@ -1,18 +1,21 @@ #!/usr/bin/env python +import sys + from argparse import ArgumentParser, Namespace from collections import OrderedDict from configparser import RawConfigParser from datetime import datetime from json import dumps -from os import path, chdir -from subprocess import Popen, PIPE +from os import chdir, path +from subprocess import PIPE, Popen -from jinja2 import Environment, BaseLoader -from requests import patch -import sys import towncrier +from jinja2 import BaseLoader, Environment +from requests import patch + + GIT_COMMANDS = { "get_tag": ["git describe --tags --abbrev=0"], "commit_version_change": [ @@ -78,7 +81,7 @@ def _run_shell_command(command: list): output, error = process.communicate() return_code = process.returncode return output.decode("utf-8"), error, return_code - except: + except Exception: return None, None, -1