home · contact · privacy
Move app-relevant data into ~/.local/share/, add installer script for this.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 24 Nov 2024 17:11:37 +0000 (18:11 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 24 Nov 2024 17:11:37 +0000 (18:11 +0100)
install.py [new file with mode: 0755]
ytplom/misc.py

diff --git a/install.py b/install.py
new file mode 100755 (executable)
index 0000000..db188e8
--- /dev/null
@@ -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')
index 8c28a678a3edebaa12a65a99addfd342e94dd547..fe5f429b4937fdf77721ff4c00a834a2b38ffaf1 100644 (file)
@@ -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)