config: add a basic storage backend

$XDG_DATA_HOME/tuhi/<mac address>/settings.ini

And that file contains a [Device] section with two entries, address and uuid.
pull/9/head
Peter Hutterer 2018-01-19 15:24:44 +10:00
parent 5f9766cb71
commit 76449bf608
3 changed files with 83 additions and 3 deletions

3
example.ini Normal file
View File

@ -0,0 +1,3 @@
[Device]
Address=E2:43:03:67:0E:01
UUID=dead00beef00

19
tuhi.py
View File

@ -20,6 +20,7 @@ from gi.repository import GObject
from tuhi.dbusserver import TuhiDBusServer
from tuhi.ble import BlueZDeviceManager
from tuhi.wacom import WacomDevice, Stroke
from tuhi.config import TuhiConfig
logging.basicConfig(format='%(levelname)s: %(name)s: %(message)s',
level=logging.INFO)
@ -76,14 +77,16 @@ class TuhiDevice(GObject.Object):
over Tuhi's DBus interface
"""
def __init__(self, bluez_device, tuhi_dbus_device, paired=True):
def __init__(self, bluez_device, tuhi_dbus_device, uuid=None, paired=True):
GObject.Object.__init__(self)
self._tuhi_dbus_device = tuhi_dbus_device
self._wacom_device = WacomDevice(bluez_device)
self._wacom_device = WacomDevice(bluez_device, uuid)
self._wacom_device.connect('drawing', self._on_drawing_received)
self._wacom_device.connect('done', self._on_fetching_finished, bluez_device)
self._wacom_device.connect('button-press-required', self._on_button_press_required)
self.drawings = []
# We need either uuid or paired as false
assert uuid is not None or paired is False
self.paired = paired
bluez_device.connect('connected', self._on_bluez_device_connected)
@ -165,6 +168,8 @@ class Tuhi(GObject.Object):
self.bluez.connect('discovery-started', self._on_bluez_discovery_started)
self.bluez.connect('discovery-stopped', self._on_bluez_discovery_stopped)
self.config = TuhiConfig()
self.devices = {}
self._search_stop_handler = None
@ -199,8 +204,16 @@ class Tuhi(GObject.Object):
if Tuhi._is_pairing_device(bluez_device):
return
try:
config = self.config.devices[bluez_device.address]
uuid = config['uuid']
except KeyError:
logger.info('{}: device without config, must be paired first'.format(bluez_device.address))
return
logger.debug('{}: uuid {}'.format(bluez_device.address, uuid))
tuhi_dbus_device = self.server.create_device(bluez_device)
d = TuhiDevice(bluez_device, tuhi_dbus_device)
d = TuhiDevice(bluez_device, tuhi_dbus_device, uuid=uuid)
self.devices[bluez_device.address] = d
def _on_bluez_discovery_started(self, manager):

64
tuhi/config.py Normal file
View File

@ -0,0 +1,64 @@
#!/usr/bin/env python3
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
from gi.repository import GObject
import xdg.BaseDirectory
import os
import configparser
import re
import logging
logger = logging.getLogger('tuhi.config')
ROOT_PATH = os.path.join(xdg.BaseDirectory.xdg_data_home, 'tuhi')
class TuhiConfig(GObject.Object):
def __init__(self):
GObject.Object.__init__(self)
try:
os.mkdir(ROOT_PATH)
except FileExistsError:
pass
self._devices = {}
self._scan_config_dir()
@property
def devices(self):
"""
Returns a dictionary with the bluetooth address as key
"""
return self._devices
def _scan_config_dir(self):
with os.scandir(ROOT_PATH) as it:
for entry in it:
if entry.is_file():
continue
if not re.match('^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$', entry.name):
continue
path = os.path.join(ROOT_PATH, entry.name, 'settings.ini')
if not os.path.isfile(path):
continue
logger.debug("{}: configuration found".format(entry.name))
config = configparser.ConfigParser()
config.read(path)
assert config['Device']['Address'] == entry.name
self._devices[entry.name] = config['Device']