blob: f40ed3e62f0bd0c28640343e1d2e27da8b8be6e4 [file] [log] [blame]
Steven Michalske2a9486d2015-08-06 22:35:21 -07001#!/usr/bin/env python
Chris Liechtifbdd8a02015-08-09 02:37:45 +02002#
cliechtifab09872009-02-07 00:25:44 +00003# portable serial port access with python
4# this is a wrapper module for different platform implementations
cliechti89b4af12002-02-12 23:24:41 +00005#
Chris Liechtifbdd8a02015-08-09 02:37:45 +02006# (C) 2001-2015 Chris Liechti <cliechti@gmx.net>
7#
8# SPDX-License-Identifier: BSD-3-Clause
cliechti89b4af12002-02-12 23:24:41 +00009
Steven Michalske2a9486d2015-08-06 22:35:21 -070010VERSION = '3.0a'
cliechti89b4af12002-02-12 23:24:41 +000011
Chris Liechtibaec2a32015-08-07 14:42:15 +020012import importlib
cliechti0bfe5252008-06-21 01:36:52 +000013import sys
14
cliechti4ff97242008-06-21 18:14:46 +000015if sys.platform == 'cli':
cliechti39cfb7b2011-08-22 00:30:07 +000016 from serial.serialcli import *
cliechti89b4af12002-02-12 23:24:41 +000017else:
cliechti4ff97242008-06-21 18:14:46 +000018 import os
cliechtifab09872009-02-07 00:25:44 +000019 # chose an implementation, depending on os
cliechti0bfe5252008-06-21 01:36:52 +000020 if os.name == 'nt': #sys.platform == 'win32':
cliechti39cfb7b2011-08-22 00:30:07 +000021 from serial.serialwin32 import *
cliechti0bfe5252008-06-21 01:36:52 +000022 elif os.name == 'posix':
cliechti39cfb7b2011-08-22 00:30:07 +000023 from serial.serialposix import *
cliechti0bfe5252008-06-21 01:36:52 +000024 elif os.name == 'java':
cliechti39cfb7b2011-08-22 00:30:07 +000025 from serial.serialjava import *
cliechti0bfe5252008-06-21 01:36:52 +000026 else:
cliechti330185e2011-08-18 22:45:17 +000027 raise ImportError("Sorry: no implementation for your platform ('%s') available" % (os.name,))
28
cliechti89b4af12002-02-12 23:24:41 +000029
cliechtie542b362011-03-18 00:49:16 +000030protocol_handler_packages = [
31 'serial.urlhandler',
32 ]
cliechti109486b2009-08-02 00:00:11 +000033
cliechtie3ab3532009-08-05 12:40:38 +000034def serial_for_url(url, *args, **kwargs):
cliechtie542b362011-03-18 00:49:16 +000035 """\
cliechti330185e2011-08-18 22:45:17 +000036 Get an instance of the Serial class, depending on port/url. The port is not
37 opened when the keyword parameter 'do_not_open' is true, by default it
38 is. All other parameters are directly passed to the __init__ method when
39 the port is instantiated.
cliechtie542b362011-03-18 00:49:16 +000040
41 The list of package names that is searched for protocol handlers is kept in
cliechti330185e2011-08-18 22:45:17 +000042 ``protocol_handler_packages``.
cliechtie542b362011-03-18 00:49:16 +000043
44 e.g. we want to support a URL ``foobar://``. A module
45 ``my_handlers.protocol_foobar`` is provided by the user. Then
46 ``protocol_handler_packages.append("my_handlers")`` would extend the search
47 path so that ``serial_for_url("foobar://"))`` would work.
48 """
cliechti9b1d3ad2009-08-02 23:47:04 +000049 # check remove extra parameter to not confuse the Serial class
50 do_open = 'do_not_open' not in kwargs or not kwargs['do_not_open']
51 if 'do_not_open' in kwargs: del kwargs['do_not_open']
cliechtiaaf778a2009-08-05 17:59:15 +000052 # the default is to use the native version
53 klass = Serial # 'native' implementation
cliechti9b1d3ad2009-08-02 23:47:04 +000054 # check port type and get class
cliechtiaaf778a2009-08-05 17:59:15 +000055 try:
Chris Liechtibaec2a32015-08-07 14:42:15 +020056 url_lowercase = url.lower()
cliechtiaaf778a2009-08-05 17:59:15 +000057 except AttributeError:
cliechti2a6d5332011-03-04 02:08:32 +000058 # it's not a string, use default
cliechtiaaf778a2009-08-05 17:59:15 +000059 pass
cliechti109486b2009-08-02 00:00:11 +000060 else:
Chris Liechtibaec2a32015-08-07 14:42:15 +020061 if '://' in url_lowercase:
62 protocol = url_lowercase.split('://', 1)[0]
63 module_name = '.protocol_%s' % (protocol,)
cliechtie542b362011-03-18 00:49:16 +000064 for package_name in protocol_handler_packages:
Chris Liechtibaec2a32015-08-07 14:42:15 +020065 package = importlib.import_module(package_name)
cliechtie542b362011-03-18 00:49:16 +000066 try:
Chris Liechtibaec2a32015-08-07 14:42:15 +020067 handler_module = importlib.import_module(module_name, package_name)
cliechtie542b362011-03-18 00:49:16 +000068 except ImportError:
69 pass
70 else:
Chris Liechtibaec2a32015-08-07 14:42:15 +020071 klass = handler_module.Serial
cliechtie542b362011-03-18 00:49:16 +000072 break
cliechti2a6d5332011-03-04 02:08:32 +000073 else:
cliechtie542b362011-03-18 00:49:16 +000074 raise ValueError('invalid URL, protocol %r not known' % (protocol,))
cliechtiaaf778a2009-08-05 17:59:15 +000075 else:
76 klass = Serial # 'native' implementation
cliechti9b1d3ad2009-08-02 23:47:04 +000077 # instantiate and open when desired
cliechti109486b2009-08-02 00:00:11 +000078 instance = klass(None, *args, **kwargs)
79 instance.port = url
cliechti9b1d3ad2009-08-02 23:47:04 +000080 if do_open:
cliechti109486b2009-08-02 00:00:11 +000081 instance.open()
82 return instance