Skip to content

Commit

Permalink
1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Eeems committed May 27, 2024
1 parent 5144ae1 commit e3c635d
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 4 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
VERSION := $(shell grep -m 1 version pyproject.toml | tr -s ' ' | tr -d '"' | tr -d "'" | cut -d' ' -f3)
PATH := "/opt/bin:/opt/sbin:/home/root/.local/bin:/opt/bin:/opt/sbin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin"

define SCRIPT
export PATH=${PATH}
if ! type pip &> /dev/null; then
if ! type opkg &> /dev/null; then
echo "Opkg not found, please install toltec"
Expand Down Expand Up @@ -29,6 +31,8 @@ install: deploy
echo -e "$$SCRIPT" | ssh [email protected] bash -le

test: install
cat test.py | ssh [email protected] /opt/bin/python -u
cat test.py \
| ssh [email protected] \
"bash -ec 'PATH=${PATH} /opt/bin/python -u'"

.PHONY: clean install test deploy
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,18 @@ Usage

```python
import launcherctl

if (
launcherctl.launchers.current.name != "oxide"
and "oxide" in launcherctl.launchers
):
launcherctl.launchers.switch("oxide", start=True)
# or
launcherctl.launchers["oxide"].enable(start=True)

if (
"calculator" in launcherctl.apps
and "calculator" not in launcherctl.apps.running.keys()
):
launcherctl.apps["calculator"].start()
```
10 changes: 8 additions & 2 deletions launcherctl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
import subprocess
import sys
from ._app import App
from ._app import api as apps

from ._launcher import Launcher
from ._launcher import api as launchers

from ._util import LauncherCtlException
from ._util import launcherctl
61 changes: 61 additions & 0 deletions launcherctl/_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from ._util import launcherctl


class App:
def __init__(self, name: str):
self.name = name

@property
def is_running(self) -> bool:
return self.name in api.running.keys()

@property
def is_paused(self) -> bool:
return self.name in api.paused.keys()

def start(self) -> None:
launcherctl("start-app", self.name)

def stop(self) -> None:
launcherctl("stop-app", self.name)

def pause(self) -> None:
launcherctl("pause-app", self.name)

def resume(self) -> None:
launcherctl("resume-app", self.name)


class API:
def keys(self) -> list[str]:
return [x.decode("utf-8") for x in launcherctl("list-apps").splitlines()]

def __contains__(self, key: str) -> bool:
return key in self.keys()

def __getitem__(self, key: str) -> App:
if key not in self:
raise KeyError()

return App(key)

@property
def running(self) -> dict[App]:
return {
x: App(x)
for x in [
x.decode("utf-8") for x in launcherctl("list-running-apps").splitlines()
]
}

@property
def paused(self) -> dict[App]:
return {
x: App(x)
for x in [
x.decode("utf-8") for x in launcherctl("list-paused-apps").splitlines()
]
}


api = API()
76 changes: 76 additions & 0 deletions launcherctl/_launcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import subprocess

from typing import Callable

from ._app import App
from ._util import launcherctl
from ._util import LauncherCtlException


class Launcher:
def __init__(self, name: str):
self.name = name

def logs(self, onlogline: Callable[[str], None] = None) -> list[str] | None:
pass

def start(self):
launcherctl("start-launcher", self.name)

def stop(self):
launcherctl("stop-launcher", self.name)

def enable(self, start: bool = False):
if start:
launcherctl("switch-launcher", "--start", self.name)

else:
launcherctl("switch-launcher", self.name)

@property
def is_current(self) -> bool:
return (
subprocess.call(["/opt/bin/launcherctl", "is-current-launcher", self.name])
== 0
)

@property
def is_enabled(self) -> bool:
return (
subprocess.call(["/opt/bin/launcherctl", "is-enabled-launcher", self.name])
== 0
)

@property
def is_active(self) -> bool:
return (
subprocess.call(["/opt/bin/launcherctl", "is-active-launcher", self.name])
== 0
)


class API:
def keys(self) -> list[str]:
return [x.decode("utf-8") for x in launcherctl("list-launchers").splitlines()]

def __contains__(self, key: str) -> bool:
return key in self.keys()

def __getitem__(self, key: str) -> App:
if key not in self:
raise KeyError()

return Launcher(key)

@property
def current(self):
return Launcher(launcherctl("status").splitlines()[0][14:-4].decode("utf-8"))

def switch(launcher: Launcher | str, start: bool = False):
if not isinstance(Launcher, launcher):
launcher = Launcher(launcher)

launcher.enable(start)


api = API()
13 changes: 13 additions & 0 deletions launcherctl/_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import subprocess


def LauncherCtlException(Exception):
pass


def launcherctl(*args) -> str | None:
try:
return subprocess.check_output(["/opt/bin/launcherctl"] + list(args))
except subprocess.CalledProcessError as e:
stdout = e.output.decode("utf-8")
raise LauncherCtlException(stdout) from e
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "launcherctl"
description = "Python wrapper around launcherctl."
readme = "README.md"
version = "0.0.1"
version = "1.0.0"
requires-python = ">= 3.11"
authors = [
{name = "Nathaniel van Diepen", email = "[email protected]"},
Expand Down
32 changes: 32 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
import launcherctl

print(launcherctl.launchers.current.name)
print([x for x in launcherctl.launchers.keys()])
if "oxide" in launcherctl.launchers:
print("oxide:")
oxide = launcherctl.launchers["oxide"]
print(f" current: {oxide.is_current}")
print(f" enabled: {oxide.is_enabled}")
print(f" active: {oxide.is_active}")

if "xochitl" in launcherctl.launchers:
print("xochitl:")
xochitl = launcherctl.launchers["xochitl"]
print(f" current: {xochitl.is_current}")
print(f" enabled: {xochitl.is_enabled}")
print(f" active: {xochitl.is_active}")

launcherctl.launchers["xochitl"].is_current
print([x for x in launcherctl.apps.keys()])
print([x for x in launcherctl.apps.running.keys()])
print([x for x in launcherctl.apps.paused.keys()])
if "calculator" in launcherctl.apps:
calculator = launcherctl.apps["calculator"]
print("calculator:")
print(f" running: {calculator.is_running}")
print(f" paused: {calculator.is_paused}")

if "xochitl" in launcherctl.apps:
xochitl = launcherctl.apps["xochitl"]
print("xochitl:")
print(f" running: {xochitl.is_running}")
print(f" paused: {xochitl.is_paused}")

0 comments on commit e3c635d

Please sign in to comment.