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 | |
Chris Liechti | ad11d17 | 2015-10-18 01:02:17 +0200 | [diff] [blame] | 19 | try: |
| 20 | import urlparse |
| 21 | except ImportError: |
| 22 | import urllib.parse as urlparse |
| 23 | |
Chris Liechti | 9eaa40c | 2016-02-12 23:32:59 +0100 | [diff] [blame^] | 24 | import serial |
| 25 | |
Chris Liechti | ad11d17 | 2015-10-18 01:02:17 +0200 | [diff] [blame] | 26 | |
| 27 | def serial_class_for_url(url): |
| 28 | """extract host and port from an URL string""" |
| 29 | parts = urlparse.urlsplit(url) |
| 30 | if parts.scheme != 'alt': |
Chris Liechti | 9eaa40c | 2016-02-12 23:32:59 +0100 | [diff] [blame^] | 31 | raise serial.SerialException( |
| 32 | 'expected a string in the form "alt://port[?option[=value][&option[=value]]]": ' |
| 33 | 'not starting with alt:// (%r)' % (parts.scheme,)) |
Chris Liechti | ad11d17 | 2015-10-18 01:02:17 +0200 | [diff] [blame] | 34 | class_name = 'Serial' |
| 35 | try: |
| 36 | for option, values in urlparse.parse_qs(parts.query, True).items(): |
| 37 | if option == 'class': |
| 38 | class_name = values[0] |
| 39 | else: |
| 40 | raise ValueError('unknown option: %r' % (option,)) |
| 41 | except ValueError as e: |
Chris Liechti | 9eaa40c | 2016-02-12 23:32:59 +0100 | [diff] [blame^] | 42 | raise serial.SerialException( |
| 43 | 'expected a string in the form ' |
| 44 | '"alt://port[?option[=value][&option[=value]]]": %s' % e) |
Chris Liechti | 7f4b4ee | 2016-01-16 23:21:42 +0100 | [diff] [blame] | 45 | if not hasattr(serial, class_name): |
| 46 | raise ValueError('unknown class: %r' % (class_name,)) |
| 47 | cls = getattr(serial, class_name) |
| 48 | if not issubclass(cls, serial.Serial): |
| 49 | raise ValueError('class %r is not an instance of Serial' % (class_name,)) |
| 50 | return (''.join([parts.netloc, parts.path]), cls) |
Chris Liechti | ad11d17 | 2015-10-18 01:02:17 +0200 | [diff] [blame] | 51 | |
| 52 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 53 | if __name__ == '__main__': |
Chris Liechti | b10daf4 | 2016-01-26 00:27:10 +0100 | [diff] [blame] | 54 | s = serial.serial_for_url('alt:///dev/ttyS0?class=PosixPollSerial') |
Chris Liechti | ad11d17 | 2015-10-18 01:02:17 +0200 | [diff] [blame] | 55 | print(s) |