blob: 2e666ca7258faf228a4ffbcc24e9796ecf39ade7 [file] [log] [blame]
Chris Liechtiad11d172015-10-18 01:02:17 +02001#! python
2#
Chris Liechti3e02f702015-12-16 23:06:04 +01003# This module implements a special URL handler that allows selecting an
Chris Liechtiad11d172015-10-18 01:02:17 +02004# alternate implementation provided by some backends.
5#
Chris Liechti3e02f702015-12-16 23:06:04 +01006# This file is part of pySerial. https://github.com/pyserial/pyserial
Chris Liechtiad11d172015-10-18 01:02:17 +02007# (C) 2015 Chris Liechti <cliechti@gmx.net>
8#
9# SPDX-License-Identifier: BSD-3-Clause
10#
11# URL format: alt://port[?option[=value][&option[=value]]]
12# options:
13# - class=X used class named X instead of Serial
14#
15# example:
16# use poll based implementation on Posix (Linux):
17# python -m serial.tools.miniterm alt:///dev/ttyUSB0?class=PosixPollSerial
18
Kurt McKee057387c2018-02-07 22:10:38 -060019from __future__ import absolute_import
20
Chris Liechtiad11d172015-10-18 01:02:17 +020021try:
22 import urlparse
23except ImportError:
24 import urllib.parse as urlparse
25
Chris Liechti9eaa40c2016-02-12 23:32:59 +010026import serial
27
Chris Liechtiad11d172015-10-18 01:02:17 +020028
29def serial_class_for_url(url):
30 """extract host and port from an URL string"""
31 parts = urlparse.urlsplit(url)
32 if parts.scheme != 'alt':
Chris Liechti9eaa40c2016-02-12 23:32:59 +010033 raise serial.SerialException(
34 'expected a string in the form "alt://port[?option[=value][&option[=value]]]": '
Chris Liechtic8f3f822016-06-08 03:35:28 +020035 'not starting with alt:// ({!r})'.format(parts.scheme))
Chris Liechtiad11d172015-10-18 01:02:17 +020036 class_name = 'Serial'
37 try:
38 for option, values in urlparse.parse_qs(parts.query, True).items():
39 if option == 'class':
40 class_name = values[0]
41 else:
Chris Liechtic8f3f822016-06-08 03:35:28 +020042 raise ValueError('unknown option: {!r}'.format(option))
Chris Liechtiad11d172015-10-18 01:02:17 +020043 except ValueError as e:
Chris Liechti9eaa40c2016-02-12 23:32:59 +010044 raise serial.SerialException(
45 'expected a string in the form '
Chris Liechtic8f3f822016-06-08 03:35:28 +020046 '"alt://port[?option[=value][&option[=value]]]": {!r}'.format(e))
Chris Liechti7f4b4ee2016-01-16 23:21:42 +010047 if not hasattr(serial, class_name):
Chris Liechtic8f3f822016-06-08 03:35:28 +020048 raise ValueError('unknown class: {!r}'.format(class_name))
Chris Liechti7f4b4ee2016-01-16 23:21:42 +010049 cls = getattr(serial, class_name)
50 if not issubclass(cls, serial.Serial):
Chris Liechtic8f3f822016-06-08 03:35:28 +020051 raise ValueError('class {!r} is not an instance of Serial'.format(class_name))
Chris Liechti7f4b4ee2016-01-16 23:21:42 +010052 return (''.join([parts.netloc, parts.path]), cls)
Chris Liechtiad11d172015-10-18 01:02:17 +020053
54# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
55if __name__ == '__main__':
Chris Liechtib10daf42016-01-26 00:27:10 +010056 s = serial.serial_for_url('alt:///dev/ttyS0?class=PosixPollSerial')
Chris Liechtiad11d172015-10-18 01:02:17 +020057 print(s)