From 76449bf60828519bc9a87d9bc137a10f205a7fe5 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Fri, 19 Jan 2018 15:24:44 +1000 Subject: [PATCH] config: add a basic storage backend $XDG_DATA_HOME/tuhi//settings.ini And that file contains a [Device] section with two entries, address and uuid. --- example.ini | 3 +++ tuhi.py | 19 ++++++++++++--- tuhi/config.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 example.ini create mode 100644 tuhi/config.py diff --git a/example.ini b/example.ini new file mode 100644 index 0000000..1786ea1 --- /dev/null +++ b/example.ini @@ -0,0 +1,3 @@ +[Device] +Address=E2:43:03:67:0E:01 +UUID=dead00beef00 diff --git a/tuhi.py b/tuhi.py index d08beff..61c9601 100755 --- a/tuhi.py +++ b/tuhi.py @@ -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): diff --git a/tuhi/config.py b/tuhi/config.py new file mode 100644 index 0000000..8b47807 --- /dev/null +++ b/tuhi/config.py @@ -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'] +