diff --git a/main.py b/main.py index 0afc2f4..708f580 100644 --- a/main.py +++ b/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) diff --git a/static/widgets.js b/static/widgets.js index df1f83e..ea2929e 100644 --- a/static/widgets.js +++ b/static/widgets.js @@ -20,8 +20,10 @@ function addApplet(config) let a = $(""); let obj = fn(a, cfg); + a.addClass("block"); a.append(obj); + $("#blocks").append(a); if('cmd' in cfg)