From 171825a271260db7c884d682d6033dc28619317f Mon Sep 17 00:00:00 2001 From: png261 Date: Sun, 18 Jun 2023 15:14:51 +0700 Subject: [PATCH] feat: add version arg, improve settings --- pywalc/__init__.py | 2 ++ pywalc/__main__.py | 40 +++++++++++++++++++++++++++++++++------- pywalc/colors.py | 4 ++-- pywalc/server.py | 15 ++++++++------- pywalc/settings.py | 17 +++++++++++------ pywalc/wallpaper.py | 5 +++-- setup.py | 4 +++- 7 files changed, 62 insertions(+), 25 deletions(-) diff --git a/pywalc/__init__.py b/pywalc/__init__.py index 81561a6..253099c 100644 --- a/pywalc/__init__.py +++ b/pywalc/__init__.py @@ -1,3 +1,4 @@ +from .settings import __version__ from . import server from . import colors from . import theme @@ -5,6 +6,7 @@ from . import theme __all__ = [ + "__version__", "server", "colors", "theme", diff --git a/pywalc/__main__.py b/pywalc/__main__.py index fb57597..add905f 100644 --- a/pywalc/__main__.py +++ b/pywalc/__main__.py @@ -1,20 +1,46 @@ import os - -from . import util -from .settings import WAL_FILE, BACKUP_FILE, WALLPAPER_DIR, CACHE_DIR, PYWALL_CACHE +import argparse + +from .settings import ( + __version__, + PYWAL_FILE_PATH, + BACKUP_FILE, + WALLPAPER_DIR, + CACHE_DIR, + PYWAL_CURRENT_WALLPAPER, +) from .server import Server +from . import util + + +def get_args(): + """Get the script arguments.""" + description = "pywalc - Pywal client" + arg = argparse.ArgumentParser(description=description) + + arg.add_argument("-v", action="store_true", help='Print "pywalc" version.') + + return arg + + +def parse_args_exit(parser): + """Process args that exit.""" + args = parser.parse_args() + + if args.v: + parser.exit(0, "wal %s\n" % __version__) def main(): """Main script function.""" util.setup_logging() - util.create_dir(CACHE_DIR) util.create_dir(WALLPAPER_DIR) - util.copy_dir(WAL_FILE, BACKUP_FILE) + util.copy_dir(PYWAL_FILE_PATH, BACKUP_FILE) + util.copy_dir(PYWAL_CURRENT_WALLPAPER, os.path.join(WALLPAPER_DIR, "current")) - current_wallpaper = util.read_file(os.path.join(PYWALL_CACHE, "wal"))[0] - util.copy_dir(current_wallpaper, os.path.join(WALLPAPER_DIR, "current")) + parser = get_args() + parse_args_exit(parser) server = Server(2601) server.run() diff --git a/pywalc/colors.py b/pywalc/colors.py index 4638652..c112abf 100644 --- a/pywalc/colors.py +++ b/pywalc/colors.py @@ -1,13 +1,13 @@ import pywal from fastapi import Request -from .settings import BACKUP_FILE, WAL +from .settings import BACKUP_FILE, PYWAL_DATA from . import util class Color: def __init__(self): - self.data = WAL["colors"] + self.data = PYWAL_DATA["colors"] def get(self): return self.data diff --git a/pywalc/server.py b/pywalc/server.py index 8c66826..42d1dcb 100644 --- a/pywalc/server.py +++ b/pywalc/server.py @@ -10,7 +10,7 @@ from uvicorn import run from pycloudflared import try_cloudflare -from .settings import CACHE_DIR, OS +from .settings import CACHE_DIR, MODULE_DIR, OS from .colors import Color from .theme import Theme from .wallpaper import Wallpaper @@ -57,14 +57,15 @@ def __init__(self, port): ) self.server.mount("/cache", StaticFiles(directory=CACHE_DIR), name="cache") + self.server.mount( + "/static", + StaticFiles(directory=os.path.join(MODULE_DIR, "static")), + name="static", + ) - static_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "static") - self.server.mount("/static", StaticFiles(directory=static_dir), name="static") - - templates_dir = os.path.join( - os.path.dirname(os.path.abspath(__file__)), "templates" + self.templates = Jinja2Templates( + directory=os.path.join(MODULE_DIR, "templates") ) - self.templates = Jinja2Templates(directory=templates_dir) self._setup_routes() diff --git a/pywalc/settings.py b/pywalc/settings.py index 3b4f3a1..32d5f89 100644 --- a/pywalc/settings.py +++ b/pywalc/settings.py @@ -1,15 +1,20 @@ import pywal import os +from . import util + +__version__ = "0.1.0" HOME = pywal.settings.HOME -OS = pywal.settings.OS +XDG_CACHE_DIR = os.getenv("XDG_CACHE_HOME", os.path.join(HOME, ".cache")) + PYWALL_CACHE = pywal.settings.CACHE_DIR -MODULE_DIR = pywal.settings.MODULE_DIR +PYWAL_FILE_PATH = os.path.join(PYWALL_CACHE, "colors.json") +PYWAL_DATA = pywal.colors.file(PYWAL_FILE_PATH) +PYWAL_CURRENT_WALLPAPER = util.read_file(os.path.join(PYWALL_CACHE, "wal"))[0] -CACHE_DIR = os.path.join(HOME, ".cache", "pywalc") +MODULE_DIR = os.path.dirname(__file__) +CACHE_DIR = os.path.join(XDG_CACHE_DIR, "pywalc") WALLPAPER_DIR = os.path.join(CACHE_DIR, "wallpapers") - BACKUP_FILE = os.path.join(CACHE_DIR, "backup.json") -WAL_FILE = os.path.join(PYWALL_CACHE, "colors.json") -WAL = pywal.colors.file(WAL_FILE) +OS = pywal.settings.OS diff --git a/pywalc/wallpaper.py b/pywalc/wallpaper.py index 42c715c..f7f0d0a 100644 --- a/pywalc/wallpaper.py +++ b/pywalc/wallpaper.py @@ -1,10 +1,11 @@ import os import uuid -from typing import List import pywal +from fastapi import File, UploadFile +from typing import List + from .settings import WALLPAPER_DIR from . import util -from fastapi import File, UploadFile class Wallpaper: diff --git a/setup.py b/setup.py index 48a2b1e..edc989b 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ import setuptools LONG_DESC = open("README.md").read() -VERSION = "1.0.4" +VERSION = '0.1.0' DOWNLOAD = "https://github.com/png261/pywalc/archive/%s.tar.gz" % VERSION setuptools.setup( @@ -25,6 +25,7 @@ ], packages=["pywalc"], entry_points={"console_scripts": ["pywalc=pywalc.__main__:main"]}, + python_requires=">=3.5", install_requires=[ "pywal>=3.3.0", "fastapi>=0.97.0", @@ -33,6 +34,7 @@ "Jinja2>=3.1.2", "python-multipart>=0.0.6", "qrcode>=7.4.2", + "argparse>=1.4.0 ", ], include_package_data=True, zip_safe=False,