From 5b2f99516f6643f22f8a68896bf2a0274811bb9d Mon Sep 17 00:00:00 2001 From: xenofem Date: Mon, 22 Jan 2024 22:06:04 -0500 Subject: [PATCH] do fancy python packaging stuff --- .gitignore | 3 ++- MANIFEST.in | 2 ++ dlibrary/__init__.py | 1 + dlibrary.py => dlibrary/dlibrary.py | 20 +++++++++++++------ {static => dlibrary/static}/dlibrary.css | 0 {static => dlibrary/static}/viewer.css | 0 {static => dlibrary/static}/viewer.js | 0 {templates => dlibrary/templates}/base.html | 0 {templates => dlibrary/templates}/list.html | 0 {templates => dlibrary/templates}/utils.html | 0 {templates => dlibrary/templates}/viewer.html | 0 flake.nix | 3 +++ pyproject.toml | 17 ++++++++++++++++ requirements.txt | 4 ---- setup.py | 9 --------- 15 files changed, 39 insertions(+), 20 deletions(-) create mode 100644 MANIFEST.in create mode 100644 dlibrary/__init__.py rename dlibrary.py => dlibrary/dlibrary.py (96%) rename {static => dlibrary/static}/dlibrary.css (100%) rename {static => dlibrary/static}/viewer.css (100%) rename {static => dlibrary/static}/viewer.js (100%) rename {templates => dlibrary/templates}/base.html (100%) rename {templates => dlibrary/templates}/list.html (100%) rename {templates => dlibrary/templates}/utils.html (100%) rename {templates => dlibrary/templates}/viewer.html (100%) create mode 100644 pyproject.toml delete mode 100644 requirements.txt delete mode 100644 setup.py diff --git a/.gitignore b/.gitignore index c2c7f2f..3d7d3c1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ env -result \ No newline at end of file +result +*/__pycache__ \ No newline at end of file diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..14240ed --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,2 @@ +graft dlibrary/templates +graft dlibrary/static diff --git a/dlibrary/__init__.py b/dlibrary/__init__.py new file mode 100644 index 0000000..a4d7d64 --- /dev/null +++ b/dlibrary/__init__.py @@ -0,0 +1 @@ +from .dlibrary import * diff --git a/dlibrary.py b/dlibrary/dlibrary.py similarity index 96% rename from dlibrary.py rename to dlibrary/dlibrary.py index ac039d2..261c3fa 100755 --- a/dlibrary.py +++ b/dlibrary/dlibrary.py @@ -2,6 +2,7 @@ import argparse import asyncio +import importlib_resources as resources from pathlib import Path from os.path import relpath, splitext import re @@ -12,7 +13,7 @@ import zipfile from dlsite_async import DlsiteAPI import fitz -from jinja2 import Environment, FileSystemLoader, select_autoescape +from jinja2 import Environment, PackageLoader, select_autoescape import requests NUMBER_REGEX = re.compile('[0-9]+') @@ -295,11 +296,14 @@ def metadata(args): con.close() -def publish(args): - source_dir = Path(__file__).parent +def copy_contents(src, dest): + dest.mkdir(parents=True, exist_ok=True) + for item in src.iterdir(): + shutil.copyfile(item, dest / item.name) +def publish(args): jenv = Environment( - loader=FileSystemLoader(source_dir / "templates"), + loader=PackageLoader("dlibrary"), autoescape=select_autoescape() ) @@ -336,7 +340,8 @@ def publish(args): with open(work_dir / 'index.html', 'w') as f: f.write(viewer_template.render(depth=2, work=work, title=title, images=images)) - shutil.copytree(source_dir / 'static', args.destdir / 'site' / 'static', dirs_exist_ok=True) + with resources.as_file(resources.files("dlibrary")) as r: + copy_contents(r / 'static', args.destdir / 'site' / 'static') list_template = jenv.get_template("list.html") @@ -405,6 +410,9 @@ parser_metadata.set_defaults(func=metadata) parser_publish = subparsers.add_parser('publish', help='generate HTML/CSS/JS for library site') parser_publish.set_defaults(func=publish) -if __name__ == "__main__": +def main(): args = argparser.parse_args() args.func(args) + +if __name__ == "__main__": + main() diff --git a/static/dlibrary.css b/dlibrary/static/dlibrary.css similarity index 100% rename from static/dlibrary.css rename to dlibrary/static/dlibrary.css diff --git a/static/viewer.css b/dlibrary/static/viewer.css similarity index 100% rename from static/viewer.css rename to dlibrary/static/viewer.css diff --git a/static/viewer.js b/dlibrary/static/viewer.js similarity index 100% rename from static/viewer.js rename to dlibrary/static/viewer.js diff --git a/templates/base.html b/dlibrary/templates/base.html similarity index 100% rename from templates/base.html rename to dlibrary/templates/base.html diff --git a/templates/list.html b/dlibrary/templates/list.html similarity index 100% rename from templates/list.html rename to dlibrary/templates/list.html diff --git a/templates/utils.html b/dlibrary/templates/utils.html similarity index 100% rename from templates/utils.html rename to dlibrary/templates/utils.html diff --git a/templates/viewer.html b/dlibrary/templates/viewer.html similarity index 100% rename from templates/viewer.html rename to dlibrary/templates/viewer.html diff --git a/flake.nix b/flake.nix index 8ec9a08..63d5f48 100644 --- a/flake.nix +++ b/flake.nix @@ -48,11 +48,14 @@ dlibrary = buildPythonApplication { pname = "dlibrary"; version = "0.1"; + pyproject = true; propagatedBuildInputs = [ pymupdf requests dlsite-async jinja2 + importlib-resources + setuptools ]; src = ./.; }; diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..9f2b3a6 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,17 @@ +[project] +name = "dlibrary" +version = "0.1" +dependencies = [ + "requests", + "PyMuPDF", + "dlsite-async", + "jinja2", + "importlib_resources", +] + +[project.scripts] +dlibrary = "dlibrary:main" + +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 75ceaf5..0000000 --- a/requirements.txt +++ /dev/null @@ -1,4 +0,0 @@ -requests -PyMuPDF -dlsite-async -jinja2 diff --git a/setup.py b/setup.py deleted file mode 100644 index 05267ef..0000000 --- a/setup.py +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env python - -from setuptools import setup, find_packages - -setup(name='dlibrary', - version='0.1', - packages=find_packages(), - scripts=["dlibrary.py"], - )