Skip to content

Commit 083fd2c

Browse files
committed
refactor: startup scripts refactor and cleanup
1 parent 2b62149 commit 083fd2c

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

tinyticker/web/__main__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
)
1616
from . import logger
1717
from .app import create_app
18-
from .startup import STARTUP_DIR, run_scripts
18+
from .startup import STARTUP_DIR, get_scripts, run_scripts
1919

2020

2121
def parse_args(args: List[str]) -> argparse.Namespace:
@@ -82,8 +82,13 @@ def main():
8282
if not STARTUP_DIR.is_dir():
8383
STARTUP_DIR.mkdir(parents=True)
8484

85-
logger.info("Running startup scripts.")
86-
processes = run_scripts()
85+
scripts = get_scripts()
86+
if scripts:
87+
logger.info("Running startup scripts.")
88+
logger.debug("Startup scripts: %s", [script.name for script in scripts])
89+
processes = run_scripts(scripts)
90+
else:
91+
processes = []
8792

8893
if args.show_qrcode:
8994
logger.info("Generating qrcode.")

tinyticker/web/app.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from ..tickers._base import INTERVAL_LOOKBACKS
2323
from ..waveshare_lib.models import MODELS
2424
from .command import COMMANDS, reboot
25-
from .startup import STARTUP_DIR
25+
from .startup import STARTUP_DIR, get_scripts
2626

2727
LOGGER = logging.getLogger(__name__)
2828
TEMPLATE_PATH = str(Path(__file__).parent / "templates")
@@ -208,8 +208,8 @@ def get_startup_script(filename):
208208
return send_from_directory(STARTUP_DIR, filename, mimetype="text/plain")
209209

210210
@app.route("/startup")
211-
def startup_scippts():
212-
files = sorted([path.name for path in STARTUP_DIR.glob("*")])
211+
def startup_scripts():
212+
files = sorted([script.name for script in get_scripts()])
213213
return render_template("startup.html", files=files)
214214

215215
return app

tinyticker/web/startup.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
import logging
2+
import os
23
import subprocess
34
from pathlib import Path
5+
from typing import List
46

57
LOGGER = logging.getLogger(__name__)
68
STARTUP_DIR = Path.home() / ".config" / "tinyticker" / "startup"
79

810

9-
def run_scripts():
10-
scripts = [
11+
def get_scripts() -> List[Path]:
12+
"""Get the startup scripts."""
13+
return [
1114
file
12-
for file in STARTUP_DIR.glob("*")
13-
if file.is_file() and not file.name.startswith(".")
15+
for file in STARTUP_DIR.iterdir()
16+
if file.is_file() and not file.name.startswith(".") and os.access(file, os.X_OK)
1417
]
15-
LOGGER.debug("Running startup scripts: %s", [file.name for file in scripts])
18+
19+
20+
def run_scripts(scripts: List[Path]) -> List[subprocess.Popen]:
21+
"""Run the startup scripts. The scripts must be executable.
22+
23+
Args:
24+
scripts: The scripts to run.
25+
26+
Returns:
27+
The processes running the scripts.
28+
"""
1629
processes = []
1730
for file in scripts:
1831
LOGGER.debug("Running startup script: %s", file.name)
19-
processes.append(subprocess.Popen(f"{file}", shell=True))
32+
processes.append(subprocess.Popen(str(file), shell=True))
2033
return processes

0 commit comments

Comments
 (0)