Skip to content

Commit

Permalink
Add in ability to pass arguments to the kivy app #4
Browse files Browse the repository at this point in the history
Add in the ability to run arbitry arguments on the kivy app. This resolve the issue of using kvhot with applications that require additional input

---------

Co-authored-by: Alex Hellier <[email protected]>
  • Loading branch information
Bobspadger and Alex Hellier authored Sep 16, 2024
1 parent b37b2a8 commit eb9c495
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 35 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ options:
-b BLACKLIST [BLACKLIST ...], --blacklist BLACKLIST [BLACKLIST ...]
exclude specific files/dirs from being monitored.
-V, --version show program's version number and exit
--app-args arguments to pass to the kivy application as a single quoted string, this will be split and passed on to the kivy application, example --app-args "arg1 arg2 --my-kwarg=value"
```

*(Works on windows, linux and osx)*
74 changes: 41 additions & 33 deletions kvhot/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import importlib.metadata
import os
import shlex
import subprocess
import sys
from typing import Union
Expand Down Expand Up @@ -30,37 +31,40 @@ def check_target_dir(path: str) -> str:
return path


parser = argparse.ArgumentParser(
prog="kvhot",
description="Automatically restarts the Kivy application "
"whenever files within the project directory change.",
)
parser.add_argument(
"target_dir",
type=check_target_dir,
help="directory of the entry-point (main.py) of the kivy application",
)
parser.add_argument(
"--width", type=int, default=350, help="width of the window."
)
parser.add_argument(
"--height", type=int, default=650, help="height of the window."
)
parser.add_argument(
"--top", type=int, default=0, help="top position of the window."
)
parser.add_argument(
"--left", type=int, default=0, help="left position of the window."
)
parser.add_argument(
"-b",
"--blacklist",
nargs="+",
help="exclude specific files/dirs from being monitored.",
)
parser.add_argument(
"-V", "--version", action="version", version="%(prog)s " + __version__
)
def parse_args():
parser = argparse.ArgumentParser(
prog="kvhot",
description="Automatically restarts the Kivy application "
"whenever files within the project directory change.",
)
parser.add_argument(
"target_dir",
type=check_target_dir,
help="directory of the entry-point (main.py) of the kivy application",
)
parser.add_argument("--width", type=int, default=350, help="width of the window.")
parser.add_argument("--height", type=int, default=650, help="height of the window.")
parser.add_argument("--top", type=int, default=0, help="top position of the window.")
parser.add_argument("--left", type=int, default=0, help="left position of the window.")
parser.add_argument(
"-b",
"--blacklist",
nargs="+",
help="exclude specific files/dirs from being monitored.",
)
parser.add_argument("-V", "--version", action="version", version="%(prog)s " + __version__)

# Arbitrary arguments to pass through to the kivy application
parser.add_argument(
"--app-args",
help="arguments to pass to the kivy application as a single quoted string, this will be split and passed on to the kivy application",
)
args = parser.parse_args()
if args.app_args:
args.app_args = shlex.split(args.app_args)
else:
args.app_args = []
return args


def log(s: str):
Expand Down Expand Up @@ -114,12 +118,14 @@ def __init__(
top: int,
left: int,
blacklist: Union[list[str], None],
app_args: list,
):
self.target_dir = target_dir
self.main_py = os.path.join(self.target_dir, "main.py")
self.blacklist_filter = DefaultFilter(
ignore_paths=filter_blacklist_paths(blacklist)
)
self.app_args = app_args

os.environ["KCFG_GRAPHICS_WIDTH"] = str(width)
os.environ["KCFG_GRAPHICS_HEIGHT"] = str(height)
Expand All @@ -140,7 +146,8 @@ def _start_app(self, changes: Union[FileChange, None] = None):
log(f"{change_type} {file_path}, restarting app...")
self._process.kill()

self._process = subprocess.Popen([sys.executable, self.main_py])
command = [sys.executable, self.main_py] + self.app_args
self._process = subprocess.Popen(command)

def _stop_app(self):
if self._process:
Expand All @@ -164,7 +171,8 @@ def start(self):


def main():
args = vars(parser.parse_args())
args = parse_args()
args = vars(args)
w = Watcher(**args)
w.start()

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ build-backend = 'setuptools.build_meta'

[project]
name = 'kvhot'
version = 'v2024.02.07'
version = 'v2024.09.12'
description = 'Automatically restarts the Kivy application whenever files within the project directory change.'
readme = 'README.md'
authors = [{ name = 'Kulothungan U.G', email = '[email protected]' }]
authors = [{ name = 'Kulothungan U.G', email = '[email protected]' }, { name = 'Alexander Hellier', email = '[email protected]' }]
license = { file = 'LICENSE' }
keywords = ['kivy', 'kivymd', 'hotrestart', 'gui']
dependencies = [
Expand Down

0 comments on commit eb9c495

Please sign in to comment.