From: Christian Heller Date: Sun, 24 Nov 2024 17:11:37 +0000 (+0100) Subject: Move app-relevant data into ~/.local/share/, add installer script for this. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/%7B%7Bprefix%7D%7D/static/tasks?a=commitdiff_plain;h=3141a52a3be5b5611af56321f4dfebd8b7d17f30;p=ytplom Move app-relevant data into ~/.local/share/, add installer script for this. --- diff --git a/install.py b/install.py new file mode 100755 index 0000000..db188e8 --- /dev/null +++ b/install.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +'''Ensure expected directories, files for app to work exist.''' +from shutil import copyfile +from os import makedirs, scandir +from os.path import basename, join as path_join +from ytplom.misc import NAME_TEMPLATES_DIR, PATH_APP_DATA, PATH_TEMPLATES + +print(f'ensuring {PATH_APP_DATA}') +makedirs(PATH_APP_DATA) +print(f'ensuring {PATH_TEMPLATES}') +makedirs(PATH_TEMPLATES) +for e in scandir(NAME_TEMPLATES_DIR): + target_path = path_join(PATH_TEMPLATES, basename(e.path)) + print(f'copying {e.path} to {target_path}') + copyfile(e.path, target_path) +print(f'installation finished') diff --git a/ytplom/misc.py b/ytplom/misc.py index 8c28a67..fe5f429 100644 --- a/ytplom/misc.py +++ b/ytplom/misc.py @@ -3,8 +3,8 @@ # included libs from typing import TypeAlias, Optional, NewType, Callable, Self, Any from os import chdir, environ, getcwd, makedirs, scandir, remove as os_remove -from os.path import (isdir, isfile, exists as path_exists, join as path_join, - splitext, basename) +from os.path import (dirname, isdir, isfile, exists as path_exists, + join as path_join, splitext, basename) from random import shuffle from time import time, sleep from datetime import datetime, timedelta @@ -45,24 +45,26 @@ TemplateContext: TypeAlias = dict[ | list['YoutubeVideo'] | list['YoutubeQuery'] | list[tuple[YoutubeId, PathStr]] | list[tuple[PathStr, PathStr]]] -# local data reasonably expected to be in user home directory +# major expected directories PATH_HOME = PathStr(environ.get('HOME', '')) -PATH_WORKDIR = PathStr(path_join(PATH_HOME, 'ytplom')) -PATH_THUMBNAILS = PathStr(path_join(PATH_WORKDIR, 'thumbnails')) -PATH_DB = PathStr(path_join(PATH_WORKDIR, 'db.sql')) -PATH_DOWNLOADS = PathStr(path_join(PATH_WORKDIR, 'downloads')) -PATH_TEMP = PathStr(path_join(PATH_WORKDIR, 'temp')) - -# template paths; might move outside PATH_WORKDIR in the future -PATH_TEMPLATES = PathStr(path_join(PATH_WORKDIR, 'templates')) +PATH_APP_DATA = PathStr(path_join(PATH_HOME, '.local/share/ytplom')) +PATH_CACHE = PathStr(path_join(PATH_HOME, '.cache/ytplom')) + +# paths for rather dynamic data +PATH_DOWNLOADS = PathStr(path_join(PATH_HOME, 'ytplom_downloads')) +PATH_DB = PathStr(path_join(PATH_APP_DATA, 'db.sql')) +PATH_TEMP = PathStr(path_join(PATH_CACHE, 'temp')) +PATH_THUMBNAILS = PathStr(path_join(PATH_CACHE, 'thumbnails')) + +# template paths +NAME_TEMPLATES_DIR = 'templates' +PATH_TEMPLATES = PathStr(path_join(PATH_APP_DATA, NAME_TEMPLATES_DIR)) NAME_TEMPLATE_QUERIES = PathStr('queries.tmpl') NAME_TEMPLATE_RESULTS = PathStr('results.tmpl') NAME_TEMPLATE_VIDEOS = PathStr('videos.tmpl') NAME_TEMPLATE_VIDEO = PathStr('video.tmpl') NAME_TEMPLATE_YT_VIDEO = PathStr('yt_video.tmpl') NAME_TEMPLATE_PLAYLIST = PathStr('playlist.tmpl') -PATH_TEMPLATE_QUERIES = PathStr(path_join(PATH_TEMPLATES, - NAME_TEMPLATE_QUERIES)) # yt_dlp config YT_DOWNLOAD_FORMAT = 'bestvideo[height<=1080][width<=1920]+bestaudio'\ @@ -112,7 +114,7 @@ CREATE TABLE quota_costs ( CREATE TABLE files ( rel_path TEXT PRIMARY KEY, yt_id TEXT NOT NULL DEFAULT "", - flags INTEGER NOT NULL DEFAULT 0; + flags INTEGER NOT NULL DEFAULT 0, FOREIGN KEY (yt_id) REFERENCES yt_videos(id) ); ''' @@ -150,6 +152,11 @@ class DatabaseConnection: if path_exists(self._path): raise CantWriteException(f'no DB at {self._path}; would create' ', but something\'s already there?') + path_db_dir = dirname(self._path) + if not isdir(path_db_dir): + raise NotFoundException( + f'cannot find {path_db_dir} as directory to put DB ' + 'into, did you run install.py?') with sql_connect(self._path) as conn: conn.executescript(SCRIPT_INIT_DB) self._conn = sql_connect(self._path)