kleinere umstrukturierung

auswertung der konfig fuer mastodon, default_config erweitert,
set_values() in get_status_and_timestamp() umbenannt,
statusstring und timestamp werden in main() aufgerufen und an andere funktionen uebergeben
This commit is contained in:
+++ 2022-07-10 18:17:33 +02:00
parent c4d02a73c9
commit edece83dd1

View file

@ -14,6 +14,7 @@ import os
import socket import socket
import ssl import ssl
import sys import sys
from mastodon import Mastodon
from time import time, sleep from time import time, sleep
import configparser import configparser
@ -102,7 +103,7 @@ def receive_buffer_is_valid(raw_data):
return False return False
def change_status(raw_data, api): def change_status(status, timestamp, filename):
''' '''
Write the new status together with a timestamp into the Space API JSON. Write the new status together with a timestamp into the Space API JSON.
param 1: byte object param 1: byte object
@ -112,14 +113,13 @@ def change_status(raw_data, api):
logging.debug('Change status API') logging.debug('Change status API')
# todo: use walrus operator := when migrating to python >= 3.8 # todo: use walrus operator := when migrating to python >= 3.8
data = read_api(api) data = read_api(filename)
if data is False: if data is False:
return False return False
status, timestamp = set_values(raw_data) if os.access(filename, os.W_OK):
if os.access(api, os.W_OK):
logging.debug('API file is writable') logging.debug('API file is writable')
with open(api, 'w') as api_file: with open(filename, 'w') as api_file:
logging.debug('API file open successfull') logging.debug('API file open successfull')
data["state"]["open"] = status data["state"]["open"] = status
data["state"]["lastchange"] = timestamp data["state"]["lastchange"] = timestamp
@ -133,7 +133,7 @@ def change_status(raw_data, api):
else: else:
logging.error('API file is not writable. Wrong permissions?') logging.error('API file is not writable. Wrong permissions?')
return False return False
logging.info('Status successfull changed to {}'.format(status)) logging.info('API file successfull changed to {}'.format(status))
return True return True
@ -164,7 +164,7 @@ def read_api(api):
return api_json_data return api_json_data
def set_values(raw_data): def get_status_and_time(raw_data):
''' '''
Create a timestamp, changes the value of the given byte into a string Create a timestamp, changes the value of the given byte into a string
and returns both. and returns both.
@ -211,6 +211,11 @@ def main():
'api': { 'api': {
'api': './api', 'api': './api',
'template': './api_template' 'template': './api_template'
},
'mastodon': {
'send': 'false',
'host': 'localhost',
'token': 'aaaaa-bbbbb-ccccc-ddddd-eeeee'
} }
} }
configfile = './apistatusd.conf' configfile = './apistatusd.conf'
@ -293,8 +298,12 @@ def main():
raw_data = conn.recv(1) raw_data = conn.recv(1)
if receive_buffer_is_valid(raw_data) is True: if receive_buffer_is_valid(raw_data) is True:
if change_status(raw_data, config['api']['api']) is True: status, timestamp = get_status_and_time(raw_data)
if change_status(status, timestamp, config['api']['api']) is True:
answer = raw_data answer = raw_data
if config['mastodon']['send'].lower() == 'true':
logging.debug('Try to toot status')
else: logging.debug('Toot is set to false')
if conn: if conn:
logging.debug('Send {} back'.format(raw_data)) logging.debug('Send {} back'.format(raw_data))
conn.send(answer) conn.send(answer)