Chris Liechti | ad11d17 | 2015-10-18 01:02:17 +0200 | [diff] [blame] | 1 | #! python |
| 2 | # |
Chris Liechti | 3e02f70 | 2015-12-16 23:06:04 +0100 | [diff] [blame] | 3 | # This module implements a special URL handler that allows selecting an |
Chris Liechti | ad11d17 | 2015-10-18 01:02:17 +0200 | [diff] [blame] | 4 | # alternate implementation provided by some backends. |
| 5 | # |
Chris Liechti | 3e02f70 | 2015-12-16 23:06:04 +0100 | [diff] [blame] | 6 | # This file is part of pySerial. https://github.com/pyserial/pyserial |
Chris Liechti | ad11d17 | 2015-10-18 01:02:17 +0200 | [diff] [blame] | 7 | # (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 McKee | 057387c | 2018-02-07 22:10:38 -0600 | [diff] [blame] | 19 | from __future__ import absolute_import |
| 20 | |
Chris Liechti | ad11d17 | 2015-10-18 01:02:17 +0200 | [diff] [blame] | 21 | try: |
| 22 | import urlparse |
| 23 | except ImportError: |
| 24 | import urllib.parse as urlparse |
| 25 | |
Chris Liechti | 9eaa40c | 2016-02-12 23:32:59 +0100 | [diff] [blame] | 26 | import serial |
| 27 | |
Chris Liechti | ad11d17 | 2015-10-18 01:02:17 +0200 | [diff] [blame] | 28 | |
| 29 | def 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 Liechti | 9eaa40c | 2016-02-12 23:32:59 +0100 | [diff] [blame] | 33 | raise serial.SerialException( |
| 34 | 'expected a string in the form "alt://port[?option[=value][&option[=value]]]": ' |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame] | 35 | 'not starting with alt:// ({!r})'.format(parts.scheme)) |
Chris Liechti | ad11d17 | 2015-10-18 01:02:17 +0200 | [diff] [blame] | 36 | 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 Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame] | 42 | raise ValueError('unknown option: {!r}'.format(option)) |
Chris Liechti | ad11d17 | 2015-10-18 01:02:17 +0200 | [diff] [blame] | 43 | except ValueError as e: |
Chris Liechti | 9eaa40c | 2016-02-12 23:32:59 +0100 | [diff] [blame] | 44 | raise serial.SerialException( |
| 45 | 'expected a string in the form ' |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame] | 46 | '"alt://port[?option[=value][&option[=value]]]": {!r}'.format(e)) |
Chris Liechti | 7f4b4ee | 2016-01-16 23:21:42 +0100 | [diff] [blame] | 47 | if not hasattr(serial, class_name): |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame] | 48 | raise ValueError('unknown class: {!r}'.format(class_name)) |
Chris Liechti | 7f4b4ee | 2016-01-16 23:21:42 +0100 | [diff] [blame] | 49 | cls = getattr(serial, class_name) |
| 50 | if not issubclass(cls, serial.Serial): |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame] | 51 | raise ValueError('class {!r} is not an instance of Serial'.format(class_name)) |
Chris Liechti | 7f4b4ee | 2016-01-16 23:21:42 +0100 | [diff] [blame] | 52 | return (''.join([parts.netloc, parts.path]), cls) |
Chris Liechti | ad11d17 | 2015-10-18 01:02:17 +0200 | [diff] [blame] | 53 | |
| 54 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 55 | if __name__ == '__main__': |
Chris Liechti | b10daf4 | 2016-01-26 00:27:10 +0100 | [diff] [blame] | 56 | s = serial.serial_for_url('alt:///dev/ttyS0?class=PosixPollSerial') |
Chris Liechti | ad11d17 | 2015-10-18 01:02:17 +0200 | [diff] [blame] | 57 | print(s) |