Configuration file (#15)

* Add support for config file

Closes #1

* Support changing config path

The path to the config file is accepted as an argument

* Remove old code

Remove the code for the old backend selection method
This commit is contained in:
Samuel Sloniker 2021-06-20 16:37:04 -07:00 committed by GitHub
parent 67136506a4
commit 4d5ce06eb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 9 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ __pycache__
pieces
newlist
oldlist
conf.txt

View File

@ -5,12 +5,14 @@ import time
def _long_touch(x, y):
os.environ['DISPLAY'] = config['display']
subprocess.run(['xdotool', 'mousemove', str(x), str(y), 'mousedown', '1'])
time.sleep(2)
subprocess.run(['xdotool', 'mouseup', '1'])
def get_img():
os.environ['DISPLAY'] = config['display']
xwd = subprocess.Popen(['xwd', '-root', '-silent'], stdout=subprocess.PIPE)
convert = subprocess.Popen(['convert', 'xwd:-', 'bmp:img.bmp'], stdin=xwd.stdout)
convert.wait()
@ -18,6 +20,7 @@ def get_img():
def touch(x, y, is_long):
os.environ['DISPLAY'] = config['display']
if is_long:
threading.Thread(target=_long_touch, args=(x, y,)).start()
else:

31
server/parse_config.py Normal file
View File

@ -0,0 +1,31 @@
import shlex
class ConfigSyntaxError(BaseException):
pass
def loadl(lines):
output = {}
for line in [ i.strip() for i in lines ]:
words = shlex.split(line, comments=True)
if len(words) == 0:
pass
elif len(words) == 2:
if words[0] in output:
raise ConfigSyntaxError('keys cannot be redefined')
output[words[0]] = words[1]
else:
raise ConfigSyntaxError('lines must consist of exactly two tokens')
return output
def loads(string):
return loadl(string.split('\n'))
def load(f):
return loadl(f.readlines())

View File

@ -1,10 +1,3 @@
# Select backend - 'port8080' uses HamClock's port 8080 service; 'x11' uses an
# X11 server (typically Xvfb) (make sure DISPLAY is set correctly!)
use_backend = 'x11'
# use_backend = 'port8080'
import os
import base64
import threading
@ -17,9 +10,20 @@ import imgproc as hcapi
import argon2
import asyncio
import importlib
import parse_config
import sys
try:
conf = sys.argv[1]
except IndexError:
conf = 'conf.txt'
hcapi.backend = importlib.import_module(f'backends.{use_backend}')
with open(conf) as f:
config_data = parse_config.load(f)
hcapi.backend = importlib.import_module(f'backends.{config_data["backend"]}')
hcapi.backend.config = config_data
hcapi.config = config_data
ph = argon2.PasswordHasher()
@ -91,7 +95,7 @@ class HCRAServer(tornado.websocket.WebSocketHandler):
else:
_, password, x, y, w, is_long = message.split(' ')
try:
ph.verify('$argon2id$v=19$m=102400,t=2,p=8$NExqSUh+0wzBznBG9jM6ww$MkaPLZ6WPAegb8BI+IL7Bg', password)
ph.verify(config_data['password_argon2'], password)
x, y, w, is_long = int(x), int(y), int(w), is_long == 'true'
hcapi.touch(x, y, w, is_long)
except argon2.exceptions.VerifyMismatchError: