Added state_cmd for checking state, state_expect for expected output (using exit code if none specified)
This commit is contained in:
parent
db94af4b24
commit
af6b62ccae
28
main.py
28
main.py
|
@ -1,14 +1,28 @@
|
|||
|
||||
from flask import Flask, render_template, request
|
||||
from os import system
|
||||
import subprocess
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
def check_app_states(apps):
|
||||
for app in apps:
|
||||
if 'check_cmd' in app:
|
||||
exitcode = system(app['check_cmd'])
|
||||
app['default'] = app['default'] if exitcode == 0 else False
|
||||
if not 'default' in app:
|
||||
app['default'] = False
|
||||
if 'state_cmd' in app and len(app['state_cmd']) > 0:
|
||||
if 'state_expect' in app:
|
||||
try:
|
||||
result = subprocess.run(app['state_cmd'], shell=True, check=True)
|
||||
print(result)
|
||||
if result.stdout == app['state_expect']:
|
||||
app['default'] = not app['default'] if result.stdout == app['state_expect'] else app['default']
|
||||
except subprocess.CalledProcessError:
|
||||
continue # continue with default, current state could not be determined
|
||||
else:
|
||||
exitcode = system(app['state_cmd'])
|
||||
if exitcode == 0:
|
||||
app['default'] = not app['default'] if exitcode == 0 else app['default']
|
||||
return apps
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
|
@ -46,19 +60,19 @@ def index():
|
|||
},
|
||||
{
|
||||
'name': 'toggle',
|
||||
'text_on': '🔊',
|
||||
'text_off': '🔇',
|
||||
'text_on': 'file exist',
|
||||
'text_off': 'file gone',
|
||||
'default': False,
|
||||
'cmd_on': 'touch ~/testfile.tmp',
|
||||
'cmd_off': 'rm ~/testfile.tmp',
|
||||
'check_cmd': '~/check_if_file_exists.sh',
|
||||
'state_cmd': 'test -f ~/testfile.tmp',
|
||||
'cooldown': 1000,
|
||||
'show_cooldown': False
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
check_app_states(templateData)
|
||||
templateData['apps'] = check_app_states(templateData['apps'])
|
||||
|
||||
return render_template('index.html', **templateData)
|
||||
|
||||
|
|
|
@ -20,8 +20,10 @@ function addApplet(config)
|
|||
|
||||
let a = $("<a>");
|
||||
let obj = fn(a, cfg);
|
||||
|
||||
a.addClass("block");
|
||||
a.append(obj);
|
||||
|
||||
$("#blocks").append(a);
|
||||
|
||||
if('cmd' in cfg)
|
||||
|
|
Loading…
Reference in New Issue