Opt: Use user-friendly yaml in notify

This commit is contained in:
18870 2022-08-20 12:11:05 +08:00
parent 7cba1ac678
commit cece2e67d3
5 changed files with 19 additions and 16 deletions

View File

@ -16,7 +16,7 @@
"Error": {
"HandleError": true,
"SaveError": true,
"OnePushConfig": "{}",
"OnePushConfig": "provider: null",
"ScreenshotLength": 1
},
"Optimization": {

View File

@ -136,8 +136,8 @@
},
"OnePushConfig": {
"type": "textarea",
"value": "{}",
"mode": "json"
"value": "provider: null",
"mode": "yaml"
},
"ScreenshotLength": {
"type": "input",

View File

@ -45,8 +45,8 @@ Error:
SaveError: true
OnePushConfig:
type: textarea
mode: json
value: '{}'
mode: yaml
value: 'provider: null'
ScreenshotLength: 1
Optimization:
ScreenshotInterval: 0.3

View File

@ -33,7 +33,7 @@ class GeneratedConfig:
# Group `Error`
Error_HandleError = True
Error_SaveError = True
Error_OnePushConfig = '{}'
Error_OnePushConfig = 'provider: null'
Error_ScreenshotLength = 1
# Group `Optimization`

View File

@ -1,4 +1,4 @@
import json
import yaml
import onepush.core
from onepush import get_notifier
from onepush.core import Provider
@ -9,28 +9,31 @@ from module.logger import logger
onepush.core.log = logger
def handle_notify(config: str, **kwargs) -> bool:
def handle_notify(_config: str, **kwargs) -> bool:
try:
config: dict = json.loads(config)
config = {}
for item in yaml.safe_load_all(_config):
config.update(item)
except Exception:
logger.error("Fail to load onepush config, skip sending")
return False
try:
if config.get("disabled") == True:
return True
notifier: Provider = get_notifier(config["provider"])
provider_name = config.pop("provider", None)
if provider_name is None:
logger.info("No provider specified, skip sending")
return False
notifier: Provider = get_notifier(provider_name)
required: list[str] = notifier.params["required"]
params: dict = config["params"]
params.update(kwargs)
config.update(kwargs)
# pre check
for key in required:
if key not in params:
if key not in config:
logger.warning(
f"Notifier {notifier.name} require param '{key}' but not provided"
)
notifier.notify(**params)
notifier.notify(**config)
except OnePushException:
logger.exception("Push notify failed")
return False