From bcd9eb368806842adba0e11d3c9fa1aa231b3aa7 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Sat, 8 Feb 2025 20:04:17 +0100
Subject: [PATCH] Adapt to package installation structure used with ytplom.

---
 .gitmodules                                     |  2 +-
 install.sh                                      | 13 +++++++++++++
 ledgplom                                        | 17 +++++++++++++++++
 plomlib => src/plomlib                          |  0
 requirements.txt => src/requirements.txt        |  0
 ledger.py => src/run.py                         | 12 ++++++++++--
 {templates => src/templates}/_base.tmpl         |  0
 {templates => src/templates}/_macros.tmpl       |  0
 {templates => src/templates}/balance.tmpl       |  0
 {templates => src/templates}/edit_raw.tmpl      |  0
 .../templates}/edit_structured.tmpl             |  0
 {templates => src/templates}/ledger_raw.tmpl    |  0
 .../templates}/ledger_structured.tmpl           |  0
 13 files changed, 41 insertions(+), 3 deletions(-)
 create mode 100755 install.sh
 create mode 100755 ledgplom
 rename plomlib => src/plomlib (100%)
 rename requirements.txt => src/requirements.txt (100%)
 rename ledger.py => src/run.py (98%)
 rename {templates => src/templates}/_base.tmpl (100%)
 rename {templates => src/templates}/_macros.tmpl (100%)
 rename {templates => src/templates}/balance.tmpl (100%)
 rename {templates => src/templates}/edit_raw.tmpl (100%)
 rename {templates => src/templates}/edit_structured.tmpl (100%)
 rename {templates => src/templates}/ledger_raw.tmpl (100%)
 rename {templates => src/templates}/ledger_structured.tmpl (100%)

diff --git a/.gitmodules b/.gitmodules
index 42cf7f3..4c97e95 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,3 +1,3 @@
 [submodule "plomlib"]
-	path = plomlib
+	path = src/plomlib
 	url = https://plomlompom.com/repos/clone/plomlib
diff --git a/install.sh b/install.sh
new file mode 100755
index 0000000..aa84e99
--- /dev/null
+++ b/install.sh
@@ -0,0 +1,13 @@
+#!/usr/bin/sh
+set -e
+
+PATH_APP_SHARE=~/.local/share/ledgplom
+PATH_LOCAL_BIN=~/.local/bin
+NAME_EXECUTABLE=ledgplom
+
+mkdir -p "${PATH_APP_SHARE}" "${PATH_LOCAL_BIN}"
+
+cp -r ./src/* "${PATH_APP_SHARE}/"
+cp "${NAME_EXECUTABLE}" "${PATH_LOCAL_BIN}/"
+
+echo "Installed executable to ${PATH_LOCAL_BIN}/${NAME_EXECUTABLE}, app files to ${PATH_APP_SHARE}."
diff --git a/ledgplom b/ledgplom
new file mode 100755
index 0000000..59a02b7
--- /dev/null
+++ b/ledgplom
@@ -0,0 +1,17 @@
+#!/usr/bin/sh
+set -e
+
+PATH_APP_SHARE=~/.local/share/ledgplom
+PATH_VENV="${PATH_APP_SHARE}/venv"
+
+python3 -m venv "${PATH_VENV}"
+. "${PATH_VENV}/bin/activate"
+
+if [ "$1" = "install_deps" ]; then
+    echo "Checking dependencies."
+    pip3 install -r "${PATH_APP_SHARE}/requirements.txt"
+    exit 0
+fi
+
+export PYTHONPATH="${PATH_APP_SHARE}:${PYTHONPATH}"
+python3 "${PATH_APP_SHARE}/run.py" $@
diff --git a/plomlib b/src/plomlib
similarity index 100%
rename from plomlib
rename to src/plomlib
diff --git a/requirements.txt b/src/requirements.txt
similarity index 100%
rename from requirements.txt
rename to src/requirements.txt
diff --git a/ledger.py b/src/run.py
similarity index 98%
rename from ledger.py
rename to src/run.py
index 96520b6..6f09306 100755
--- a/ledger.py
+++ b/src/run.py
@@ -1,12 +1,20 @@
 #!/usr/bin/env python3
-"""Viewer for ledger .dat files."""
+"""Viewer and editor for ledger .dat files."""
+
+# included libs
 from datetime import date as dt_date
 from decimal import Decimal, InvalidOperation as DecimalInvalidOperation
 from os import environ
 from pathlib import Path
 from sys import exit as sys_exit
 from typing import Any, Optional, Self
-from plomlib.web import PlomHttpHandler, PlomHttpServer, PlomQueryMap
+# might need module installation(s)
+try:
+    from plomlib.web import PlomHttpHandler, PlomHttpServer, PlomQueryMap
+except ModuleNotFoundError as e:
+    print('FAIL: Missing module(s), please run with "install_deps" argument.')
+    print(e)
+    sys_exit(1)
 
 
 LEDGER_DAT = environ.get('LEDGER_DAT')
diff --git a/templates/_base.tmpl b/src/templates/_base.tmpl
similarity index 100%
rename from templates/_base.tmpl
rename to src/templates/_base.tmpl
diff --git a/templates/_macros.tmpl b/src/templates/_macros.tmpl
similarity index 100%
rename from templates/_macros.tmpl
rename to src/templates/_macros.tmpl
diff --git a/templates/balance.tmpl b/src/templates/balance.tmpl
similarity index 100%
rename from templates/balance.tmpl
rename to src/templates/balance.tmpl
diff --git a/templates/edit_raw.tmpl b/src/templates/edit_raw.tmpl
similarity index 100%
rename from templates/edit_raw.tmpl
rename to src/templates/edit_raw.tmpl
diff --git a/templates/edit_structured.tmpl b/src/templates/edit_structured.tmpl
similarity index 100%
rename from templates/edit_structured.tmpl
rename to src/templates/edit_structured.tmpl
diff --git a/templates/ledger_raw.tmpl b/src/templates/ledger_raw.tmpl
similarity index 100%
rename from templates/ledger_raw.tmpl
rename to src/templates/ledger_raw.tmpl
diff --git a/templates/ledger_structured.tmpl b/src/templates/ledger_structured.tmpl
similarity index 100%
rename from templates/ledger_structured.tmpl
rename to src/templates/ledger_structured.tmpl
-- 
2.30.2