changeset 2:0ecb388a0b90

start power code
author drewp@bigasterisk.com
date Sat, 04 Mar 2023 12:10:36 -0800
parents 04c8a1b3976b
children 3d7fc94a404a
files pdm.lock power_linux.py power_osx.py pyproject.toml racc.py run.sh update.sh
diffstat 7 files changed, 117 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/pdm.lock	Fri Mar 03 21:24:53 2023 -0800
+++ b/pdm.lock	Sat Mar 04 12:10:36 2023 -0800
@@ -93,6 +93,19 @@
 summary = "C parser in Python"
 
 [[package]]
+name = "python-xlib"
+version = "0.33"
+summary = "Python X Library"
+dependencies = [
+    "six>=1.10.0",
+]
+
+[[package]]
+name = "quartz"
+version = "0.0.1.dev0"
+summary = ""
+
+[[package]]
 name = "runcmd"
 version = "2020.12.3"
 summary = ""
@@ -101,6 +114,12 @@
 ]
 
 [[package]]
+name = "six"
+version = "1.16.0"
+requires_python = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*"
+summary = "Python 2 and 3 compatibility utilities"
+
+[[package]]
 name = "sniffio"
 version = "1.3.0"
 requires_python = ">=3.7"
@@ -144,7 +163,7 @@
 
 [metadata]
 lock_version = "4.0"
-content_hash = "sha256:154a0840707c5c174819c62d9d6fe17c300e92fada090c6125686011e9ca2bc8"
+content_hash = "sha256:86695308ac1a377a8f1950ba4dce042957aee65d99007a48771f71b52557f734"
 
 [metadata.files]
 "anyio 3.6.2" = [
@@ -271,9 +290,20 @@
     {url = "https://files.pythonhosted.org/packages/5e/0b/95d387f5f4433cb0f53ff7ad859bd2c6051051cebbb564f139a999ab46de/pycparser-2.21.tar.gz", hash = "sha256:e644fdec12f7872f86c58ff790da456218b10f863970249516d60a5eaca77206"},
     {url = "https://files.pythonhosted.org/packages/62/d5/5f610ebe421e85889f2e55e33b7f9a6795bd982198517d912eb1c76e1a53/pycparser-2.21-py2.py3-none-any.whl", hash = "sha256:8ee45429555515e1f6b185e78100aea234072576aa43ab53aefcae078162fca9"},
 ]
+"python-xlib 0.33" = [
+    {url = "https://files.pythonhosted.org/packages/86/f5/8c0653e5bb54e0cbdfe27bf32d41f27bc4e12faa8742778c17f2a71be2c0/python-xlib-0.33.tar.gz", hash = "sha256:55af7906a2c75ce6cb280a584776080602444f75815a7aff4d287bb2d7018b32"},
+    {url = "https://files.pythonhosted.org/packages/fc/b8/ff33610932e0ee81ae7f1269c890f697d56ff74b9f5b2ee5d9b7fa2c5355/python_xlib-0.33-py2.py3-none-any.whl", hash = "sha256:c3534038d42e0df2f1392a1b30a15a4ff5fdc2b86cfa94f072bf11b10a164398"},
+]
+"quartz 0.0.1.dev0" = [
+    {url = "https://files.pythonhosted.org/packages/31/f2/4fac785ec7690dd2b9f87506aa52e702ed5d98dedc06a2e940c4a076136a/quartz-0.0.1.dev0.tar.gz", hash = "sha256:95d6adb994aa2713dc51fa9f1799227e247c826b71216ebf25cecab884685d35"},
+]
 "runcmd 2020.12.3" = [
     {url = "https://files.pythonhosted.org/packages/41/b7/08c18bc2944c0443aa77672e5610e861c6946f93f247bef0aa8d7737b248/runcmd-2020.12.3.tar.gz", hash = "sha256:1f2652c2e51404bb0faaa83610f23fdafd4c246b46e361a5dd6d4334d013fc24"},
 ]
+"six 1.16.0" = [
+    {url = "https://files.pythonhosted.org/packages/71/39/171f1c67cd00715f190ba0b100d606d440a28c93c7714febeca8b79af85e/six-1.16.0.tar.gz", hash = "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926"},
+    {url = "https://files.pythonhosted.org/packages/d9/5a/e7c31adbe875f2abbb91bd84cf2dc52d792b5a01506781dbcf25c91daf11/six-1.16.0-py2.py3-none-any.whl", hash = "sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254"},
+]
 "sniffio 1.3.0" = [
     {url = "https://files.pythonhosted.org/packages/c3/a0/5dba8ed157b0136607c7f2151db695885606968d1fae123dc3391e0cfdbf/sniffio-1.3.0-py3-none-any.whl", hash = "sha256:eecefdce1e5bbfb7ad2eeaabf7c1eeb404d7757c379bd1f7e5cce9d8bf425384"},
     {url = "https://files.pythonhosted.org/packages/cd/50/d49c388cae4ec10e8109b1b833fd265511840706808576df3ada99ecb0ac/sniffio-1.3.0.tar.gz", hash = "sha256:e60305c5e5d314f5389259b7f22aaa33d8f7dee49763119234af3755c55b9101"},
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/power_linux.py	Sat Mar 04 12:10:36 2023 -0800
@@ -0,0 +1,26 @@
+import subprocess
+
+from Xlib import display
+from Xlib.ext import dpms
+
+_display = display.Display()
+
+
+def is_screen_on():
+    _display.sync()
+    return _display.dpms_info().state== 1  # untested
+
+
+def go_lock_screen():
+    _display.dpms_force_level(dpms.DPMSModeOff)
+    _display.sync()
+
+
+def go_wake_screen():
+    _display.dpms_force_level(dpms.DPMSModeOn)
+    _display.sync()
+
+
+def go_sleep_mode():
+    subprocess.run(['systemctl', 'suspend'])
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/power_osx.py	Sat Mar 04 12:10:36 2023 -0800
@@ -0,0 +1,21 @@
+import subprocess
+import Quartz
+
+def is_screen_on():
+    d=Quartz.CGSessionCopyCurrentDictionary()
+    print(d)
+
+
+
+def go_lock_screen():
+    pass
+
+
+def go_wake_screen():
+    pass
+
+
+def go_sleep_mode():
+    subprocess.run(['pmset', 'sleepnow'])
+
+is_screen_on()
\ No newline at end of file
--- a/pyproject.toml	Fri Mar 03 21:24:53 2023 -0800
+++ b/pyproject.toml	Sat Mar 04 12:10:36 2023 -0800
@@ -11,13 +11,21 @@
     "psutil>=5.9.4",
     "background-loop>=1.7.0",
     "starlette-exporter>=0.15.1",
-    "xprintidle>=0.2",
-    "pulsectl-asyncio>=1.0.0",
-    "osascript>=2020.12.3",
 ]
 requires-python = ">=3.10"
 license = {text = "MIT"}
 
+[project.optional-dependencies]
+linux = [
+    "xprintidle>=0.2",
+    "pulsectl-asyncio>=1.0.0",
+    "python-xlib>=0.33",
+]
+osx = [
+    "osascript>=2020.12.3",
+    "quartz>=0.0.1.dev0"
+]
+
 [tool.pdm]
 
 [build-system]
--- a/racc.py	Fri Mar 03 21:24:53 2023 -0800
+++ b/racc.py	Sat Mar 04 12:10:36 2023 -0800
@@ -14,12 +14,14 @@
 import logging
 import socket
 import sys
-if sys.implementation._multiarch == 'darwin':
+if psutil.OSX:
     import idle_osx as idle
     import volume_osx as volume
-elif 'linux' in sys.implementation._multiarch:
+    import power_osx as power
+elif psutil.LINUX:
     import idle_linux as idle
     import volume_linux as volume
+    import power_linux as power
 else:
     raise NotImplementedError(repr(sys.implementation))
 
@@ -41,6 +43,7 @@
 
 RACC_RUNNING = Gauge("racc_running", "program is running", ['host', 'prog'])
 RACC_IDLE = Gauge("racc_idle", "desktop mouse/kb idle seconds", ['host'])
+RACC_SCREEN = Gauge("racc_screen", "screen in unlocked/on mode", ['host'])
 
 
 def update_progs(first_run):
@@ -61,7 +64,7 @@
     ]:
         RACC_RUNNING.labels(host=hostname, prog=p).set(p in progs)
     RACC_IDLE.labels(host=hostname).set(idle.get_idle_seconds())
-
+    RACC_SCREEN.labels(host=hostname).set(power.is_screen_on())
 
 async def root(req: Request) -> HTMLResponse:
     vol = await volume.get_volume()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/run.sh	Sat Mar 04 12:10:36 2023 -0800
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+if [ `uname` = Linux ]; then
+  pdm run python racc.py
+elif [ `uname` = Darwin ]; then
+  ${HOME}/Library/Python/3.10/bin/pdm run python racc.py
+else
+  echo `uname` unknown
+  exit 1
+fi
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/update.sh	Sat Mar 04 12:10:36 2023 -0800
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+if [ `uname` = Linux ]; then
+  pdm install
+  pdm install -G linux
+elif [ `uname` = Darwin ]; then
+  ${HOME}/Library/Python/3.10/bin/pdm install
+  ${HOME}/Library/Python/3.10/bin/pdm install -G osx
+else
+  echo `uname` unknown
+  exit 1
+fi