diff --git a/examples/maqpy/authed_ratelimit.py b/examples/flask_tools/authed_ratelimit.py similarity index 65% rename from examples/maqpy/authed_ratelimit.py rename to examples/flask_tools/authed_ratelimit.py index 6b5337e..0f1dda4 100644 --- a/examples/maqpy/authed_ratelimit.py +++ b/examples/flask_tools/authed_ratelimit.py @@ -1,13 +1,13 @@ -from replit import maqpy +from replit import flask_tools -app = maqpy.App(__name__) +app = flask_tools.App(__name__) @app.route("/") -@maqpy.authed_ratelimit( +@flask_tools.authed_ratelimit( max_requests=1, # Number of requests allowed period=1, # Amount of time before counter resets - login_res=maqpy.Page(body=f"Sign in\n{maqpy.sign_in_snippet}"), + login_res=flask_tools.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/maqpy/files.py b/examples/flask_tools/files.py similarity index 78% rename from examples/maqpy/files.py rename to examples/flask_tools/files.py index 6937bfe..04bfb67 100644 --- a/examples/maqpy/files.py +++ b/examples/flask_tools/files.py @@ -1,13 +1,13 @@ -from replit import maqpy +from replit import flask_tools -app = maqpy.App(__name__) +app = flask_tools.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 = maqpy.File(__file__) + f = flask_tools.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/maqpy/login_wall.py b/examples/flask_tools/login_wall.py similarity index 50% rename from examples/maqpy/login_wall.py rename to examples/flask_tools/login_wall.py index 0123986..22d5293 100644 --- a/examples/maqpy/login_wall.py +++ b/examples/flask_tools/login_wall.py @@ -1,11 +1,12 @@ """An example of the app.login_wall() feature.""" -from replit import maqpy +from replit.flask_tools import ReplitApp, Link +from replit.flask_tools import auth_info -app = maqpy.App(__name__) +app = flask_tools.ReplitApp(__name__) def login_page() -> str: - return "Hello, please sign in to access this page!\n" + maqpy.sign_in_snippet + return "Hello, please sign in to access this page!\n" + flask_tools.sign_in_snippet # this is an example of the arguments, but the exclude kwarg already defaults to this @@ -15,12 +16,12 @@ app.login_wall(exclude=["/"], handler=login_page) @app.route("/") def index() -> str: - return f"Hello! {maqpy.Link('check out this page', href='/page')}" + return f"Hello! {flask_tools.Link('check out this page', href='/page')}" @app.route("/page") def page() -> str: - return f"Hello {maqpy.auth.name}, if you are reading this you signed in." + return f"Hello {flask_tools.auth.name}, if you are reading this you signed in." if __name__ == "__main__": diff --git a/examples/flask_tools/manual_auth.py b/examples/flask_tools/manual_auth.py new file mode 100644 index 0000000..762e209 --- /dev/null +++ b/examples/flask_tools/manual_auth.py @@ -0,0 +1,16 @@ +"""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/maqpy/needs_params.py b/examples/flask_tools/needs_params.py similarity index 93% rename from examples/maqpy/needs_params.py rename to examples/flask_tools/needs_params.py index 4ec7181..538306b 100644 --- a/examples/maqpy/needs_params.py +++ b/examples/flask_tools/needs_params.py @@ -1,6 +1,6 @@ -from replit import maqpy +from replit import flask_tools -app = maqpy.App(__name__) +app = flask_tools.App(__name__) @app.route("/") diff --git a/examples/maqpy/needs_sign_in.py b/examples/flask_tools/needs_sign_in.py similarity index 63% rename from examples/maqpy/needs_sign_in.py rename to examples/flask_tools/needs_sign_in.py index fb23a87..09f9fb8 100644 --- a/examples/maqpy/needs_sign_in.py +++ b/examples/flask_tools/needs_sign_in.py @@ -1,10 +1,10 @@ -from replit import maqpy +from replit import flask_tools -app = maqpy.App(__name__) +app = flask_tools.App(__name__) @app.route("/") -@maqpy.needs_sign_in(login_res=f"Hello! {maqpy.sign_in_snippet}") +@maqpy.needs_sign_in(login_res=f"Hello! {flask_tools.sign_in_snippet}") def index(): return "Index function" diff --git a/examples/maqpy/manual_auth.py b/examples/maqpy/manual_auth.py deleted file mode 100644 index 5ad891f..0000000 --- a/examples/maqpy/manual_auth.py +++ /dev/null @@ -1,16 +0,0 @@ -"""A basic example of repl auth.""" -from replit import maqpy - -app = maqpy.App(__name__) - - -@app.route("/") -def index(): - if maqpy.signed_in: - return "Hello " + maqpy.auth.name - else: - return maqpy.signin() # optionally: simple.sigin(title="My title") - - -if __name__ == "__main__": - app.run() diff --git a/main.sh b/main.sh new file mode 100644 index 0000000..975cd92 --- /dev/null +++ b/main.sh @@ -0,0 +1 @@ +python testapp.py \ No newline at end of file diff --git a/src/replit/__init__.py b/src/replit/__init__.py index 113acec..e3b2df4 100644 --- a/src/replit/__init__.py +++ b/src/replit/__init__.py @@ -1,13 +1,12 @@ -"""The replit python module.""" -from . import maqpy +"""The Replit Python module.""" + +from . import flask_tools from . import termutils from .audio import Audio from .database import db - -def clear() -> None: - """Clear the terminal.""" - print("\033[H\033[2J", end="", flush=True) +# Backwards compatibility. +from .termutils import clear audio = Audio() diff --git a/src/replit/maqpy/__init__.py b/src/replit/flask_tools/__init__.py similarity index 93% rename from src/replit/maqpy/__init__.py rename to src/replit/flask_tools/__init__.py index 36ab163..11ef61b 100644 --- a/src/replit/maqpy/__init__.py +++ b/src/replit/flask_tools/__init__.py @@ -1,4 +1,5 @@ -"""Make apps quickly in python.""" +"""Make apps quickly using Python.""" + import os import flask @@ -6,7 +7,7 @@ from werkzeug.local import LocalProxy from . import files from . import html -from .app import App +from .app import ReplitApp from .files import File from .html import HTMLElement, Link, Page, Paragraph from .utils import ( diff --git a/src/replit/maqpy/app.py b/src/replit/flask_tools/app.py similarity index 89% rename from src/replit/maqpy/app.py rename to src/replit/flask_tools/app.py index 4ded028..dfab82a 100644 --- a/src/replit/maqpy/app.py +++ b/src/replit/flask_tools/app.py @@ -1,4 +1,5 @@ -"""Core of maqpy.""" +"""Core of flask_tools.""" + from dataclasses import dataclass from functools import wraps from pathlib import Path @@ -10,7 +11,7 @@ from .utils import sign_in @dataclass -class ReplitAuthContext: +class ReplitUserContext: """A dataclass defining a Repl Auth state.""" user_id: int @@ -19,7 +20,7 @@ class ReplitAuthContext: @classmethod def from_headers(cls, headers: dict) -> Any: - """Initialize an instance using the Replit magic headers. + """Initialize an instance using the Replit identification headers. Args: headers (dict): A dictionary of headers received @@ -34,8 +35,8 @@ class ReplitAuthContext: ) @property - def signed_in(self) -> bool: - """Check whether the user is signed in with repl auth. + def is_authenticated(self) -> bool: + """Check whether the user is authenticated in with Replit Auth. Returns: bool: whether or not the authentication is activated. @@ -43,7 +44,7 @@ class ReplitAuthContext: return bool(self.name) -class Request(flask.Request): +class ReplitRequest(flask.Request): """Represents a client request.""" def __init__(self, *args: Any, **kwargs: Any) -> None: @@ -57,23 +58,23 @@ class Request(flask.Request): self.update_auth() def update_auth(self) -> None: - """Update the auth property to be a ReplitAuthContext.""" - self.auth = ReplitAuthContext.from_headers(self.headers) + """Update the user_info property to be a ReplitUserContext.""" + self.user_info = ReplitUserContext.from_headers(self.headers) @property - def signed_in(self) -> bool: - """Check whether the user is signed in with repl auth. + def is_authenticated(self) -> bool: + """Check whether the user is authenticated with Replit. Returns: bool: Whether or not the user is signed in """ - return self.auth.signed_in + return self.user_info.is_authenticated -class App(flask.Flask): +class ReplitApp(flask.Flask): """Represents a web application.""" - request_class = Request + request_class = ReplitRequest def __init__( self, import_name: str, nice_jinja: bool = True, **kwargs: Any @@ -154,7 +155,7 @@ class App(flask.Flask): ) def _run(self, *args: Any, **kwargs: Any) -> Any: - """Interface with the underlying flask instance's run function. + """Interface with the underlying Flask instance's run function. Args: args (Any): The arguments to be passed to the superclass' run method. @@ -211,4 +212,4 @@ class App(flask.Flask): port=port, debug=True, extra_files=watch_files, - ) + ) \ No newline at end of file diff --git a/src/replit/maqpy/files.py b/src/replit/flask_tools/files.py similarity index 93% rename from src/replit/maqpy/files.py rename to src/replit/flask_tools/files.py index 6e299fd..c950b8e 100644 --- a/src/replit/maqpy/files.py +++ b/src/replit/flask_tools/files.py @@ -1,4 +1,5 @@ -"""Utitilities for interacting with static files.""" +"""Utilities for interacting with static files.""" + import flask diff --git a/src/replit/maqpy/html.py b/src/replit/flask_tools/html.py similarity index 99% rename from src/replit/maqpy/html.py rename to src/replit/flask_tools/html.py index 70f5eb1..8b2157a 100644 --- a/src/replit/maqpy/html.py +++ b/src/replit/flask_tools/html.py @@ -1,4 +1,5 @@ """Python object representations of HTML.""" + from abc import ABC import flask diff --git a/src/replit/maqpy/utils.py b/src/replit/flask_tools/utils.py similarity index 99% rename from src/replit/maqpy/utils.py rename to src/replit/flask_tools/utils.py index d416431..a22ba46 100644 --- a/src/replit/maqpy/utils.py +++ b/src/replit/flask_tools/utils.py @@ -1,4 +1,5 @@ """Utitilities to make development easier.""" + from functools import wraps import time from typing import Any, Callable, Iterable, Optional, Union diff --git a/src/replit/termutils.py b/src/replit/termutils.py index 9cf83e4..dff767e 100644 --- a/src/replit/termutils.py +++ b/src/replit/termutils.py @@ -1,6 +1,5 @@ """Pure Python ANSI Color Escape Code generator.""" - import colorsys diff --git a/testapp.py b/testapp.py new file mode 100644 index 0000000..b4c1e05 --- /dev/null +++ b/testapp.py @@ -0,0 +1,8 @@ +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 diff --git a/tests/test_database.py b/tests/test_database.py index 8974a91..9116c88 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -3,8 +3,8 @@ import os import unittest -from replit.database import AsyncDatabase, AsyncJSONKey, Database import requests +from replit.database import AsyncDatabase, AsyncJSONKey, Database class TestAsyncDatabase(unittest.IsolatedAsyncioTestCase):