From 85d5c882949a67019d6d249fbac2ae1f0804bf6e Mon Sep 17 00:00:00 2001 From: Kumi Date: Thu, 23 May 2024 08:06:36 +0200 Subject: [PATCH] feat: Introduce pyproject.toml for build configuration Migrated project dependencies and metadata into a newly added `pyproject.toml`, aligning with modern Python packaging practices. Removed `requirements.txt` as dependencies are now specified in the unified project configuration. Restructured the project by moving Python code, static assets, and templates into a `src/structables` directory to encapsulate project components better and facilitate packaging. Moreover, refactor `main.py` to read environment variables earlier, streamline argument parsing, and ensure configurations are correctly applied before the Flask app initialization. This reorganization supports better project scalability, simplifies the build process, and enhances maintainability by consolidating project configurations and dependencies. Note: The usage of `pyproject.toml` requires tools that support PEP 518 and might necessitate updates to CI/CD pipelines or developer workflows. --- pyproject.toml | 31 ++++ requirements.txt | 2 - src/structables/__init__.py | 0 main.py => src/structables/main.py | 135 ++++++++++-------- .../structables/static}/css/style.css | 0 .../static}/dist/css/bootstrap.min.css | 0 .../static}/dist/css/bootstrap.min.css.map | 0 .../static}/dist/js/bootstrap.bundle.min.js | 0 .../dist/js/bootstrap.bundle.min.js.map | 0 .../structables/static}/img/logo.png | Bin .../structables/static}/img/logo_lg.png | Bin .../static}/img/magnifying-glass-solid.svg | 0 .../structables/templates}/400.html | 0 .../structables/templates}/404.html | 0 .../structables/templates}/429.html | 0 .../structables/templates}/500.html | 0 .../structables/templates}/archives.html | 0 .../templates}/article-review.html | 0 .../structables/templates}/article.html | 0 .../structables/templates}/base.html | 0 .../structables/templates}/category.html | 0 .../structables/templates}/collection.html | 0 .../structables/templates}/contest.html | 0 .../structables/templates}/contests.html | 0 .../structables/templates}/footer.html | 0 .../structables/templates}/header.html | 0 .../structables/templates}/iframe.html | 0 .../structables/templates}/index.html | 0 .../templates}/member-instructables.html | 0 .../structables/templates}/member.html | 0 .../structables/templates}/privacypolicy.html | 0 .../structables/templates}/projects.html | 0 .../structables/templates}/sitemap.html | 0 .../structables/templates}/style.html | 0 34 files changed, 108 insertions(+), 60 deletions(-) create mode 100644 pyproject.toml delete mode 100644 requirements.txt create mode 100644 src/structables/__init__.py rename main.py => src/structables/main.py (98%) rename {static => src/structables/static}/css/style.css (100%) rename {static => src/structables/static}/dist/css/bootstrap.min.css (100%) rename {static => src/structables/static}/dist/css/bootstrap.min.css.map (100%) rename {static => src/structables/static}/dist/js/bootstrap.bundle.min.js (100%) rename {static => src/structables/static}/dist/js/bootstrap.bundle.min.js.map (100%) rename {static => src/structables/static}/img/logo.png (100%) rename {static => src/structables/static}/img/logo_lg.png (100%) rename {static => src/structables/static}/img/magnifying-glass-solid.svg (100%) rename {templates => src/structables/templates}/400.html (100%) rename {templates => src/structables/templates}/404.html (100%) rename {templates => src/structables/templates}/429.html (100%) rename {templates => src/structables/templates}/500.html (100%) rename {templates => src/structables/templates}/archives.html (100%) rename {templates => src/structables/templates}/article-review.html (100%) rename {templates => src/structables/templates}/article.html (100%) rename {templates => src/structables/templates}/base.html (100%) rename {templates => src/structables/templates}/category.html (100%) rename {templates => src/structables/templates}/collection.html (100%) rename {templates => src/structables/templates}/contest.html (100%) rename {templates => src/structables/templates}/contests.html (100%) rename {templates => src/structables/templates}/footer.html (100%) rename {templates => src/structables/templates}/header.html (100%) rename {templates => src/structables/templates}/iframe.html (100%) rename {templates => src/structables/templates}/index.html (100%) rename {templates => src/structables/templates}/member-instructables.html (100%) rename {templates => src/structables/templates}/member.html (100%) rename {templates => src/structables/templates}/privacypolicy.html (100%) rename {templates => src/structables/templates}/projects.html (100%) rename {templates => src/structables/templates}/sitemap.html (100%) rename {templates => src/structables/templates}/style.html (100%) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..23d4fbc --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,31 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "structables" +version = "0.2.0" +authors = [ + { name="Private.coffee Team", email="support@private.coffee" }, +] +description = "A simple frontend for Instructables" +readme = "README.md" +license = { file="LICENSE" } +requires-python = ">=3.10" +classifiers = [ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: GNU Affero General Public License v3", + "Operating System :: OS Independent", +] +dependencies = [ + "flask", + "bs4", +] + +[project.scripts] +structables = "structables.main:main" + +[project.urls] +"Homepage" = "https://git.private.coffee/privatecoffee/structables" +"Bug Tracker" = "https://git.private.coffee/privatecoffee/structables/issues" +"Source Code" = "https://git.private.coffee/privatecoffee/structables" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 29a1f88..0000000 --- a/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -bs4 -flask diff --git a/src/structables/__init__.py b/src/structables/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/src/structables/main.py similarity index 98% rename from main.py rename to src/structables/main.py index 7068fb5..53537c9 100644 --- a/main.py +++ b/src/structables/main.py @@ -54,6 +54,10 @@ def get_typesense_api_key(): TYPESENSE_API_KEY = get_typesense_api_key() +debugmode = False +invidious = None +unsafe = False + def projects_search( query="*", @@ -156,64 +160,8 @@ def update_data(): ) -debugmode = os.environ.get("FLASK_DEBUG", os.environ.get("STRUCTABLES_DEBUG", False)) -invidious = os.environ.get("STRUCTABLES_INVIDIOUS") -unsafe = os.environ.get("STRUCTABLES_UNSAFE", False) - -if __name__ == "__main__": - parser = ArgumentParser() - parser.add_argument( - "-p", - "--port", - default=8002, - type=int, - help="Port to listen on", - ) - parser.add_argument( - "-d", - "--debug", - action="store_true", - help="Enable debug mode", - ) - parser.add_argument( - "-l", - "--listen-host", - default="127.0.0.1", - help="Host to listen on", - ) - parser.add_argument( - "-I", - "--invidious", - help="URL to Invidious instance, e.g. https://invidious.private.coffee/", - ) - parser.add_argument( - "-u", - "--unsafe", - action="store_true", - help="Display iframes regardless of origin", - ) - args = parser.parse_args() - - if args.debug: - debugmode = True - - if args.invidious: - invidious = args.invidious - - if args.unsafe: - unsafe = True - -print("Loading initial data...") - -update_data() - -print("Started!") - app = Flask(__name__, template_folder="templates", static_folder="static") -if debugmode: - app.logger.setLevel(logging.DEBUG) - @app.route("/cron/") def cron(): @@ -1187,8 +1135,9 @@ def route_proxy(): if url.startswith("https://cdn.instructables.com/") or url.startswith( "https://content.instructables.com/" ): + def generate(): - # Subfunction to allow streaming the data instead of + # Subfunction to allow streaming the data instead of # downloading all of it at once try: with urlopen(unquote(url)) as data: @@ -1199,6 +1148,7 @@ def route_proxy(): yield chunk except HTTPError as e: abort(e.code) + try: with urlopen(unquote(url)) as data: content_type = data.headers["content-type"] @@ -1259,5 +1209,74 @@ def internal_server_error(e): return render_template("500.html") -if __name__ == "__main__": +def main(): + global debugmode, invidious, unsafe + + parser = ArgumentParser() + parser.add_argument( + "-p", + "--port", + default=8002, + type=int, + help="Port to listen on", + ) + parser.add_argument( + "-d", + "--debug", + action="store_true", + help="Enable debug mode", + ) + parser.add_argument( + "-l", + "--listen-host", + default="127.0.0.1", + help="Host to listen on", + ) + parser.add_argument( + "-I", + "--invidious", + help="URL to Invidious instance, e.g. https://invidious.private.coffee/", + ) + parser.add_argument( + "-u", + "--unsafe", + action="store_true", + help="Display iframes regardless of origin", + ) + parser.add_argument( + "-P", + "--privacy-file", + default="privacy.txt", + help="File to read privacy policy from", + ) + args = parser.parse_args() + + debugmode = os.environ.get( + "FLASK_DEBUG", os.environ.get("STRUCTABLES_DEBUG", False) + ) + invidious = os.environ.get("STRUCTABLES_INVIDIOUS") + unsafe = os.environ.get("STRUCTABLES_UNSAFE", False) + + if args.debug: + debugmode = True + + if args.invidious: + invidious = args.invidious + + if args.unsafe: + unsafe = True + + print("Loading initial data...") + + update_data() + + print("Started!") + + if debugmode: + app.logger.setLevel(logging.DEBUG) + app.run(port=args.port, host=args.listen_host, debug=debugmode) + + +if __name__ == "__main__": + main() diff --git a/static/css/style.css b/src/structables/static/css/style.css similarity index 100% rename from static/css/style.css rename to src/structables/static/css/style.css diff --git a/static/dist/css/bootstrap.min.css b/src/structables/static/dist/css/bootstrap.min.css similarity index 100% rename from static/dist/css/bootstrap.min.css rename to src/structables/static/dist/css/bootstrap.min.css diff --git a/static/dist/css/bootstrap.min.css.map b/src/structables/static/dist/css/bootstrap.min.css.map similarity index 100% rename from static/dist/css/bootstrap.min.css.map rename to src/structables/static/dist/css/bootstrap.min.css.map diff --git a/static/dist/js/bootstrap.bundle.min.js b/src/structables/static/dist/js/bootstrap.bundle.min.js similarity index 100% rename from static/dist/js/bootstrap.bundle.min.js rename to src/structables/static/dist/js/bootstrap.bundle.min.js diff --git a/static/dist/js/bootstrap.bundle.min.js.map b/src/structables/static/dist/js/bootstrap.bundle.min.js.map similarity index 100% rename from static/dist/js/bootstrap.bundle.min.js.map rename to src/structables/static/dist/js/bootstrap.bundle.min.js.map diff --git a/static/img/logo.png b/src/structables/static/img/logo.png similarity index 100% rename from static/img/logo.png rename to src/structables/static/img/logo.png diff --git a/static/img/logo_lg.png b/src/structables/static/img/logo_lg.png similarity index 100% rename from static/img/logo_lg.png rename to src/structables/static/img/logo_lg.png diff --git a/static/img/magnifying-glass-solid.svg b/src/structables/static/img/magnifying-glass-solid.svg similarity index 100% rename from static/img/magnifying-glass-solid.svg rename to src/structables/static/img/magnifying-glass-solid.svg diff --git a/templates/400.html b/src/structables/templates/400.html similarity index 100% rename from templates/400.html rename to src/structables/templates/400.html diff --git a/templates/404.html b/src/structables/templates/404.html similarity index 100% rename from templates/404.html rename to src/structables/templates/404.html diff --git a/templates/429.html b/src/structables/templates/429.html similarity index 100% rename from templates/429.html rename to src/structables/templates/429.html diff --git a/templates/500.html b/src/structables/templates/500.html similarity index 100% rename from templates/500.html rename to src/structables/templates/500.html diff --git a/templates/archives.html b/src/structables/templates/archives.html similarity index 100% rename from templates/archives.html rename to src/structables/templates/archives.html diff --git a/templates/article-review.html b/src/structables/templates/article-review.html similarity index 100% rename from templates/article-review.html rename to src/structables/templates/article-review.html diff --git a/templates/article.html b/src/structables/templates/article.html similarity index 100% rename from templates/article.html rename to src/structables/templates/article.html diff --git a/templates/base.html b/src/structables/templates/base.html similarity index 100% rename from templates/base.html rename to src/structables/templates/base.html diff --git a/templates/category.html b/src/structables/templates/category.html similarity index 100% rename from templates/category.html rename to src/structables/templates/category.html diff --git a/templates/collection.html b/src/structables/templates/collection.html similarity index 100% rename from templates/collection.html rename to src/structables/templates/collection.html diff --git a/templates/contest.html b/src/structables/templates/contest.html similarity index 100% rename from templates/contest.html rename to src/structables/templates/contest.html diff --git a/templates/contests.html b/src/structables/templates/contests.html similarity index 100% rename from templates/contests.html rename to src/structables/templates/contests.html diff --git a/templates/footer.html b/src/structables/templates/footer.html similarity index 100% rename from templates/footer.html rename to src/structables/templates/footer.html diff --git a/templates/header.html b/src/structables/templates/header.html similarity index 100% rename from templates/header.html rename to src/structables/templates/header.html diff --git a/templates/iframe.html b/src/structables/templates/iframe.html similarity index 100% rename from templates/iframe.html rename to src/structables/templates/iframe.html diff --git a/templates/index.html b/src/structables/templates/index.html similarity index 100% rename from templates/index.html rename to src/structables/templates/index.html diff --git a/templates/member-instructables.html b/src/structables/templates/member-instructables.html similarity index 100% rename from templates/member-instructables.html rename to src/structables/templates/member-instructables.html diff --git a/templates/member.html b/src/structables/templates/member.html similarity index 100% rename from templates/member.html rename to src/structables/templates/member.html diff --git a/templates/privacypolicy.html b/src/structables/templates/privacypolicy.html similarity index 100% rename from templates/privacypolicy.html rename to src/structables/templates/privacypolicy.html diff --git a/templates/projects.html b/src/structables/templates/projects.html similarity index 100% rename from templates/projects.html rename to src/structables/templates/projects.html diff --git a/templates/sitemap.html b/src/structables/templates/sitemap.html similarity index 100% rename from templates/sitemap.html rename to src/structables/templates/sitemap.html diff --git a/templates/style.html b/src/structables/templates/style.html similarity index 100% rename from templates/style.html rename to src/structables/templates/style.html