From 9562b1faec4c092e3793cf354398611b3db8c291 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Wed, 6 Jan 2021 21:48:28 -0500 Subject: [PATCH] /s/flask_tools/web --- examples/flask_tools/manual_auth.py | 16 ---------------- .../{flask_tools => web}/authed_ratelimit.py | 8 ++++---- examples/{flask_tools => web}/files.py | 6 +++--- examples/{flask_tools => web}/login_wall.py | 12 ++++++------ examples/web/manual_auth.py | 16 ++++++++++++++++ examples/{flask_tools => web}/needs_params.py | 8 ++++---- examples/{flask_tools => web}/needs_sign_in.py | 8 ++++---- src/replit/__init__.py | 3 +-- src/replit/audio/__init__.py | 6 +----- src/replit/database/jsonkey.py | 10 ++-------- src/replit/termutils.py | 18 ++++++++---------- src/replit/{flask_tools => web}/__init__.py | 0 src/replit/{flask_tools => web}/app.py | 6 +----- src/replit/{flask_tools => web}/files.py | 0 src/replit/{flask_tools => web}/html.py | 0 src/replit/{flask_tools => web}/utils.py | 0 testapp.py | 11 +++++++---- 17 files changed, 57 insertions(+), 71 deletions(-) delete mode 100644 examples/flask_tools/manual_auth.py rename examples/{flask_tools => web}/authed_ratelimit.py (65%) rename examples/{flask_tools => web}/files.py (78%) rename examples/{flask_tools => web}/login_wall.py (50%) create mode 100644 examples/web/manual_auth.py rename examples/{flask_tools => web}/needs_params.py (80%) rename examples/{flask_tools => web}/needs_sign_in.py (57%) rename src/replit/{flask_tools => web}/__init__.py (100%) rename src/replit/{flask_tools => web}/app.py (98%) rename src/replit/{flask_tools => web}/files.py (100%) rename src/replit/{flask_tools => web}/html.py (100%) rename src/replit/{flask_tools => web}/utils.py (100%) diff --git a/examples/flask_tools/manual_auth.py b/examples/flask_tools/manual_auth.py deleted file mode 100644 index 762e209..0000000 --- a/examples/flask_tools/manual_auth.py +++ /dev/null @@ -1,16 +0,0 @@ -"""A basic example of repl auth.""" -from replit import flask_tools - -app = flask_tools.App(__name__) - - -@app.route("/") -def index(): - if flask_tools.signed_in: - return "Hello " + flask_tools.auth.name - else: - return flask_tools.signin() # optionally: simple.sigin(title="My title") - - -if __name__ == "__main__": - app.run() diff --git a/examples/flask_tools/authed_ratelimit.py b/examples/web/authed_ratelimit.py similarity index 65% rename from examples/flask_tools/authed_ratelimit.py rename to examples/web/authed_ratelimit.py index 0f1dda4..209c620 100644 --- a/examples/flask_tools/authed_ratelimit.py +++ b/examples/web/authed_ratelimit.py @@ -1,13 +1,13 @@ -from replit import flask_tools +from replit import web -app = flask_tools.App(__name__) +app = web.App(__name__) @app.route("/") -@flask_tools.authed_ratelimit( +@web.authed_ratelimit( max_requests=1, # Number of requests allowed period=1, # Amount of time before counter resets - login_res=flask_tools.Page(body=f"Sign in\n{maqpy.sign_in_snippet}"), + login_res=web.Page(body=f"Sign in\n{maqpy.sign_in_snippet}"), get_ratelimited_res=(lambda left: f"Too many requests, try again after {left} sec"), ) def index(): diff --git a/examples/flask_tools/files.py b/examples/web/files.py similarity index 78% rename from examples/flask_tools/files.py rename to examples/web/files.py index 04bfb67..834c492 100644 --- a/examples/flask_tools/files.py +++ b/examples/web/files.py @@ -1,13 +1,13 @@ -from replit import flask_tools +from replit import web -app = flask_tools.App(__name__) +app = web.App(__name__) @app.route("/") def index(): # Once a file is loaded once, it is cached in memory unless you pass the no_cache # kwarg to the File constructor - f = flask_tools.File(__file__) + f = web.File(__file__) # Tell the browser its just text, not a webpage # we can edit the headers directly because the file is just a flask.Response object f.headers["Content-Type"] = "text/plain" diff --git a/examples/flask_tools/login_wall.py b/examples/web/login_wall.py similarity index 50% rename from examples/flask_tools/login_wall.py rename to examples/web/login_wall.py index 22d5293..b874aa3 100644 --- a/examples/flask_tools/login_wall.py +++ b/examples/web/login_wall.py @@ -1,12 +1,12 @@ """An example of the app.login_wall() feature.""" -from replit.flask_tools import ReplitApp, Link -from replit.flask_tools import auth_info +from replit.web import ReplitApp, Link +from replit.web import auth_info -app = flask_tools.ReplitApp(__name__) +app = web.ReplitApp(__name__) def login_page() -> str: - return "Hello, please sign in to access this page!\n" + flask_tools.sign_in_snippet + return "Hello, please sign in to access this page!\n" + web.sign_in_snippet # this is an example of the arguments, but the exclude kwarg already defaults to this @@ -16,12 +16,12 @@ app.login_wall(exclude=["/"], handler=login_page) @app.route("/") def index() -> str: - return f"Hello! {flask_tools.Link('check out this page', href='/page')}" + return f"Hello! {web.Link('check out this page', href='/page')}" @app.route("/page") def page() -> str: - return f"Hello {flask_tools.auth.name}, if you are reading this you signed in." + return f"Hello {web.auth.name}, if you are reading this you signed in." if __name__ == "__main__": diff --git a/examples/web/manual_auth.py b/examples/web/manual_auth.py new file mode 100644 index 0000000..9093a89 --- /dev/null +++ b/examples/web/manual_auth.py @@ -0,0 +1,16 @@ +"""A basic example of repl auth.""" +from replit import web + +app = web.App(__name__) + + +@app.route("/") +def index(): + if web.signed_in: + return "Hello " + web.auth.name + else: + return web.signin() # optionally: simple.sigin(title="My title") + + +if __name__ == "__main__": + app.run() diff --git a/examples/flask_tools/needs_params.py b/examples/web/needs_params.py similarity index 80% rename from examples/flask_tools/needs_params.py rename to examples/web/needs_params.py index 538306b..989e650 100644 --- a/examples/flask_tools/needs_params.py +++ b/examples/web/needs_params.py @@ -1,6 +1,6 @@ -from replit import flask_tools +from replit import web -app = flask_tools.App(__name__) +app = web.App(__name__) @app.route("/") @@ -29,14 +29,14 @@ def onerror(missing): @app.route("/form", methods=["POST"]) -@maqpy.needs_params("param", onerror=onerror) +@web.needs_params("param", onerror=onerror) def form(param): return f"The value of param is: {param}" @app.route("/query") # source can be form, query, or a dictionary -@maqpy.needs_params("q", src="query", onerror=(lambda p: f"Need query param {p}")) +@web.needs_params("q", src="query", onerror=(lambda p: f"Need query param {p}")) def query(q): return f"The query param is: {q}" diff --git a/examples/flask_tools/needs_sign_in.py b/examples/web/needs_sign_in.py similarity index 57% rename from examples/flask_tools/needs_sign_in.py rename to examples/web/needs_sign_in.py index 09f9fb8..3154fbd 100644 --- a/examples/flask_tools/needs_sign_in.py +++ b/examples/web/needs_sign_in.py @@ -1,17 +1,17 @@ -from replit import flask_tools +from replit import web -app = flask_tools.App(__name__) +app = web.App(__name__) @app.route("/") -@maqpy.needs_sign_in(login_res=f"Hello! {flask_tools.sign_in_snippet}") +@web.needs_sign_in(login_res=f"Hello! {web.sign_in_snippet}") def index(): return "Index function" # needs_signin can also be called with no args @app.route("/test") -@maqpy.needs_sign_in +@web.needs_sign_in def test(): return "Test function" diff --git a/src/replit/__init__.py b/src/replit/__init__.py index 304f1b7..7fdf5ee 100644 --- a/src/replit/__init__.py +++ b/src/replit/__init__.py @@ -1,10 +1,9 @@ """The Replit Python module.""" -from . import flask_tools +from . import web from . import termutils from .audio import Audio from .database import db - from .users import ReplitUser # Backwards compatibility. diff --git a/src/replit/audio/__init__.py b/src/replit/audio/__init__.py index 44b8d74..3d8e4b3 100644 --- a/src/replit/audio/__init__.py +++ b/src/replit/audio/__init__.py @@ -331,11 +331,7 @@ class Audio: LoopCount=loop_count, Volume=volume, Type=str(ReaderType.tone), - Args=RequestArgs( - WaveType=wave_type, - Pitch=pitch, - Seconds=duration, - ), + Args=RequestArgs(WaveType=wave_type, Pitch=pitch, Seconds=duration,), ) with open("/tmp/audio", "w") as f: diff --git a/src/replit/database/jsonkey.py b/src/replit/database/jsonkey.py index ede2b80..9921889 100644 --- a/src/replit/database/jsonkey.py +++ b/src/replit/database/jsonkey.py @@ -84,10 +84,7 @@ class AsyncJSONKey: return await self._error("Invalid JSON data read", read) if not self._is_valid_type(data): - return await self._error( - self._type_mismatch_msg(data), - read, - ) + return await self._error(self._type_mismatch_msg(data), read,) return data async def _error(self, error: str, read: str) -> JSON_TYPE: @@ -212,10 +209,7 @@ class JSONKey(AsyncJSONKey): data = read if not self._is_valid_type(data): - return self._error( - self._type_mismatch_msg(data), - read, - ) + return self._error(self._type_mismatch_msg(data), read,) return data def _error(self, error: str, read: str) -> JSON_TYPE: diff --git a/src/replit/termutils.py b/src/replit/termutils.py index dff767e..4fd858b 100644 --- a/src/replit/termutils.py +++ b/src/replit/termutils.py @@ -126,16 +126,14 @@ class Bit: self.bg = f"\033[48;5;{value}m" -attributes = ( - { # use only repl.it supported ansi codes. Codes such as blink do not work. - "reset": 0, - "bold": 1, - "faint": 2, - "italic": 3, - "underline": 4, - "highlight": 7, - } -) +attributes = { # use only repl.it supported ansi codes. Codes such as blink do not work. + "reset": 0, + "bold": 1, + "faint": 2, + "italic": 3, + "underline": 4, + "highlight": 7, +} class Attr: diff --git a/src/replit/flask_tools/__init__.py b/src/replit/web/__init__.py similarity index 100% rename from src/replit/flask_tools/__init__.py rename to src/replit/web/__init__.py diff --git a/src/replit/flask_tools/app.py b/src/replit/web/app.py similarity index 98% rename from src/replit/flask_tools/app.py rename to src/replit/web/app.py index 176fbbb..79e0a74 100644 --- a/src/replit/flask_tools/app.py +++ b/src/replit/web/app.py @@ -93,11 +93,7 @@ class ReplitApp(flask.Flask): self.jinja_env.trim_blocks = True self.jinja_env.lstrip_blocks = True - def login_wall( - self, - exclude: Set[str] = ("/",), - handler: Callable = None, - ) -> None: + def login_wall(self, exclude: Set[str] = ("/",), handler: Callable = None,) -> None: """Require users to be logged-in on all pages. Args: diff --git a/src/replit/flask_tools/files.py b/src/replit/web/files.py similarity index 100% rename from src/replit/flask_tools/files.py rename to src/replit/web/files.py diff --git a/src/replit/flask_tools/html.py b/src/replit/web/html.py similarity index 100% rename from src/replit/flask_tools/html.py rename to src/replit/web/html.py diff --git a/src/replit/flask_tools/utils.py b/src/replit/web/utils.py similarity index 100% rename from src/replit/flask_tools/utils.py rename to src/replit/web/utils.py diff --git a/testapp.py b/testapp.py index b4c1e05..2c84f37 100644 --- a/testapp.py +++ b/testapp.py @@ -1,8 +1,11 @@ from flask import Flask + app = Flask(__name__) -@app.route('/') -def hello_world(): - return 'Hello, World!' -app.run(host='0.0.0.0') \ No newline at end of file +@app.route("/") +def hello_world(): + return "Hello, World!" + + +app.run(host="0.0.0.0")