Drop the semi-duplicate separate Protocol class

We can hook into the ProtocolVersion here, so let's de-duplicate this.
Should be backwards-compatible for config files but new config files will have
the uppercase name written now.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
pull/153/head
Peter Hutterer 2019-08-09 14:27:54 +10:00
parent 552e06a0fc
commit 5e4ab89f41
3 changed files with 43 additions and 39 deletions

View File

@ -19,7 +19,7 @@ import configparser
import re import re
import logging import logging
from .drawing import Drawing from .drawing import Drawing
from .wacom import Protocol from .protocol import ProtocolVersion
logger = logging.getLogger('tuhi.config') logger = logging.getLogger('tuhi.config')
@ -73,13 +73,13 @@ class TuhiConfig(GObject.Object):
assert config['Device']['Address'] == entry.name assert config['Device']['Address'] == entry.name
if 'Protocol' not in config['Device']: if 'Protocol' not in config['Device']:
config['Device']['Protocol'] = Protocol.UNKNOWN.value config['Device']['Protocol'] = ProtocolVersion.ANY.name.lower()
self._devices[entry.name] = config['Device'] self._devices[entry.name] = config['Device']
def new_device(self, address, uuid, protocol): def new_device(self, address, uuid, protocol):
assert is_btaddr(address) assert is_btaddr(address)
assert len(uuid) == 12 assert len(uuid) == 12
assert protocol != Protocol.UNKNOWN assert protocol != ProtocolVersion.ANY
logger.debug(f'{address}: adding new config, UUID {uuid}') logger.debug(f'{address}: adding new config, UUID {uuid}')
path = os.path.join(self.config_dir, address) path = os.path.join(self.config_dir, address)
@ -100,7 +100,7 @@ class TuhiConfig(GObject.Object):
config['Device'] = { config['Device'] = {
'Address': address, 'Address': address,
'UUID': uuid, 'UUID': uuid,
'Protocol': protocol.value, 'Protocol': protocol.name.lower(),
} }
with open(path, 'w') as configfile: with open(path, 'w') as configfile:

View File

@ -166,6 +166,29 @@ class ProtocolVersion(enum.IntEnum):
SLATE = 2 SLATE = 2
INTUOS_PRO = 3 INTUOS_PRO = 3
@classmethod
def from_string(cls, string):
'''
Return the Enum value for the given string, allowing for different
spellings. Specifically: INTUOS_PRO, intuos_pro and intuos-pro are
all allowed for the ``INTUOS_PRO`` enum value.
:raise ValueError: if the name cannot be mapped.
'''
names = {e.name: e for e in cls}
if string in names:
return names[string]
names = {e.name.lower(): e for e in cls}
if string in names:
return names[string]
names = {e.name.lower().replace('_', '-'): e for e in cls}
if string in names:
return names[string]
raise ValueError(string)
class Mode(enum.IntEnum): class Mode(enum.IntEnum):
''' '''
@ -1065,8 +1088,8 @@ class MsgRegisterWaitForButtonSlateOrIntuosPro(Msg):
def _handle_reply(self, reply): def _handle_reply(self, reply):
if reply.opcode == 0xe4: if reply.opcode == 0xe4:
self.protocol_version = ProtocolVersion.SLATE self.protocol_version = ProtocolVersion.SLATE
elif reply.opcode == 0x53: elif reply.opcode == 0x53:
self.protocol_version = ProtocolVersion.INTUOS_PRO self.protocol_version = ProtocolVersion.INTUOS_PRO
else: else:
raise UnexpectedReply(reply) raise UnexpectedReply(reply)

View File

@ -40,14 +40,6 @@ SYSEVENT_NOTIFICATION_SERVICE_UUID = '3a340720-c572-11e5-86c5-0002a5d5c51b' # N
SYSEVENT_NOTIFICATION_CHRC_UUID = '3a340721-c572-11e5-86c5-0002a5d5c51b' # NOQA SYSEVENT_NOTIFICATION_CHRC_UUID = '3a340721-c572-11e5-86c5-0002a5d5c51b' # NOQA
@enum.unique
class Protocol(enum.Enum):
UNKNOWN = 'unknown'
SPARK = 'spark'
SLATE = 'slate'
INTUOS_PRO = 'intuos-pro'
@enum.unique @enum.unique
class DeviceMode(enum.Enum): class DeviceMode(enum.Enum):
REGISTER = 1 REGISTER = 1
@ -544,8 +536,6 @@ class WacomRegisterHelper(WacomProtocolLowLevelComm):
return SYSEVENT_NOTIFICATION_CHRC_UUID not in device.characteristics return SYSEVENT_NOTIFICATION_CHRC_UUID not in device.characteristics
def register_device(self, uuid): def register_device(self, uuid):
protocol = Protocol.UNKNOWN
if self.is_spark(self.device): if self.is_spark(self.device):
self.p = tuhi.protocol.Protocol(ProtocolVersion.SPARK, self.nordic_data_exchange) self.p = tuhi.protocol.Protocol(ProtocolVersion.SPARK, self.nordic_data_exchange)
# The spark replies with b3 01 01 when in pairing mode # The spark replies with b3 01 01 when in pairing mode
@ -571,15 +561,9 @@ class WacomRegisterHelper(WacomProtocolLowLevelComm):
protocol_version = self.p.execute(Interactions.REGISTER_WAIT_FOR_BUTTON).protocol_version protocol_version = self.p.execute(Interactions.REGISTER_WAIT_FOR_BUTTON).protocol_version
if protocol_version == ProtocolVersion.ANY: if protocol_version == ProtocolVersion.ANY:
raise WacomException(f'Unknown protocol version: {protocol_version}') raise WacomException(f'Unknown protocol version: {protocol_version}')
pvmap = { return protocol_version
ProtocolVersion.SPARK: Protocol.SPARK,
ProtocolVersion.SLATE: Protocol.SLATE,
ProtocolVersion.INTUOS_PRO: Protocol.INTUOS_PRO,
}
return pvmap[protocol_version]
class WacomProtocolBase(WacomProtocolLowLevelComm): class WacomProtocolBase(WacomProtocolLowLevelComm):
@ -591,7 +575,7 @@ class WacomProtocolBase(WacomProtocolLowLevelComm):
:param device: the BlueZDevice object that is this wacom device :param device: the BlueZDevice object that is this wacom device
:param uuid: the UUID {to be} assigned to the device :param uuid: the UUID {to be} assigned to the device
''' '''
protocol = Protocol.UNKNOWN protocol = ProtocolVersion.ANY
__gsignals__ = { __gsignals__ = {
# Signal sent for each single drawing that becomes available. The # Signal sent for each single drawing that becomes available. The
@ -956,7 +940,7 @@ class WacomProtocolSpark(WacomProtocolBase):
y_max = 14300 y_max = 14300
pressure = 2047 pressure = 2047
protocol = Protocol.SPARK protocol = ProtocolVersion.SPARK
packet_handlers = [WacomPacketHandlerEndOfStroke, packet_handlers = [WacomPacketHandlerEndOfStroke,
WacomPacketHandlerEndOfSequence] WacomPacketHandlerEndOfSequence]
@ -982,7 +966,7 @@ class WacomProtocolSlate(WacomProtocolSpark):
y_max = 14300 y_max = 14300
pressure = 2047 pressure = 2047
protocol = Protocol.SLATE protocol = ProtocolVersion.SLATE
packet_handlers = [WacomPacketHandlerStrokePrefixSlate] packet_handlers = [WacomPacketHandlerStrokePrefixSlate]
def __init__(self, device, uuid, protocol_version=ProtocolVersion.SLATE): def __init__(self, device, uuid, protocol_version=ProtocolVersion.SLATE):
@ -1069,7 +1053,7 @@ class WacomProtocolIntuosPro(WacomProtocolSlate):
y_max = 29600 y_max = 29600
pressure = 4095 pressure = 4095
protocol = Protocol.INTUOS_PRO protocol = ProtocolVersion.INTUOS_PRO
packet_handlers = [WacomPacketHandlerStrokePrefixIntuosPro, packet_handlers = [WacomPacketHandlerStrokePrefixIntuosPro,
WacomPacketHandlerStrokeTimestampIntuosPro, WacomPacketHandlerStrokeTimestampIntuosPro,
WacomPacketHandlerUnknownFixedStrokeDataIntuosPro] WacomPacketHandlerUnknownFixedStrokeDataIntuosPro]
@ -1161,24 +1145,21 @@ class WacomDevice(GObject.Object):
else: else:
self._uuid = self._config['uuid'] self._uuid = self._config['uuid']
# retrieve the protocol from the config file
protocol = Protocol.UNKNOWN
try: try:
protocol = next(p for p in Protocol if p.value == self._config['Protocol']) protocol = ProtocolVersion.from_string(self._config['Protocol'])
except StopIteration: except KeyError:
raise WacomCorruptDataException(f'Missing Protocol entry from config file. Please delete config file and re-register device')
except ValueError:
logger.error(f'Unknown protocol in configuration: {self._config["Protocol"]}') logger.error(f'Unknown protocol in configuration: {self._config["Protocol"]}')
raise WacomCorruptDataException(f'Unknown Protocol {self._config["Protocol"]}') raise WacomCorruptDataException(f'Unknown Protocol {self._config["Protocol"]}')
if protocol == Protocol.UNKNOWN:
raise WacomCorruptDataException(f'Missing Protocol entry from config file. Please delete config file and re-register device')
self._init_protocol(protocol) self._init_protocol(protocol)
def _init_protocol(self, protocol): def _init_protocol(self, protocol):
protocols = { protocols = {
Protocol.SPARK: WacomProtocolSpark, ProtocolVersion.SPARK: WacomProtocolSpark,
Protocol.SLATE: WacomProtocolSlate, ProtocolVersion.SLATE: WacomProtocolSlate,
Protocol.INTUOS_PRO: WacomProtocolIntuosPro, ProtocolVersion.INTUOS_PRO: WacomProtocolIntuosPro,
} }
if protocol not in protocols: if protocol not in protocols:
@ -1186,7 +1167,7 @@ class WacomDevice(GObject.Object):
pclass = protocols[protocol] pclass = protocols[protocol]
self._wacom_protocol = pclass(self._device, self._uuid) self._wacom_protocol = pclass(self._device, self._uuid)
logger.debug(f'{self._device.name} is using protocol {protocol}') logger.debug(f'{self._device.name} is using protocol {protocol.name}')
self._wacom_protocol.connect( self._wacom_protocol.connect(
'drawing', 'drawing',