blob: d7dcde355f0f57387a787343a21854532ac4c321 [file] [log] [blame]
Steven Michalske2a9486d2015-08-06 22:35:21 -07001#!/usr/bin/env python
cliechtifab09872009-02-07 00:25:44 +00002
3# portable serial port access with python
4# this is a wrapper module for different platform implementations
cliechti89b4af12002-02-12 23:24:41 +00005#
cliechtif5716442010-01-02 03:06:21 +00006# (C) 2001-2010 Chris Liechti <cliechti@gmx.net>
cliechti89b4af12002-02-12 23:24:41 +00007# this is distributed under a free software license, see license.txt
8
Steven Michalske2a9486d2015-08-06 22:35:21 -07009VERSION = '3.0a'
cliechti89b4af12002-02-12 23:24:41 +000010
Chris Liechtibaec2a32015-08-07 14:42:15 +020011import importlib
cliechti0bfe5252008-06-21 01:36:52 +000012import sys
13
cliechti4ff97242008-06-21 18:14:46 +000014if sys.platform == 'cli':
cliechti39cfb7b2011-08-22 00:30:07 +000015 from serial.serialcli import *
cliechti89b4af12002-02-12 23:24:41 +000016else:
cliechti4ff97242008-06-21 18:14:46 +000017 import os
cliechtifab09872009-02-07 00:25:44 +000018 # chose an implementation, depending on os
cliechti0bfe5252008-06-21 01:36:52 +000019 if os.name == 'nt': #sys.platform == 'win32':
cliechti39cfb7b2011-08-22 00:30:07 +000020 from serial.serialwin32 import *
cliechti0bfe5252008-06-21 01:36:52 +000021 elif os.name == 'posix':
cliechti39cfb7b2011-08-22 00:30:07 +000022 from serial.serialposix import *
cliechti0bfe5252008-06-21 01:36:52 +000023 elif os.name == 'java':
cliechti39cfb7b2011-08-22 00:30:07 +000024 from serial.serialjava import *
cliechti0bfe5252008-06-21 01:36:52 +000025 else:
cliechti330185e2011-08-18 22:45:17 +000026 raise ImportError("Sorry: no implementation for your platform ('%s') available" % (os.name,))
27
cliechti89b4af12002-02-12 23:24:41 +000028
cliechtie542b362011-03-18 00:49:16 +000029protocol_handler_packages = [
30 'serial.urlhandler',
31 ]
cliechti109486b2009-08-02 00:00:11 +000032
cliechtie3ab3532009-08-05 12:40:38 +000033def serial_for_url(url, *args, **kwargs):
cliechtie542b362011-03-18 00:49:16 +000034 """\
cliechti330185e2011-08-18 22:45:17 +000035 Get an instance of the Serial class, depending on port/url. The port is not
36 opened when the keyword parameter 'do_not_open' is true, by default it
37 is. All other parameters are directly passed to the __init__ method when
38 the port is instantiated.
cliechtie542b362011-03-18 00:49:16 +000039
40 The list of package names that is searched for protocol handlers is kept in
cliechti330185e2011-08-18 22:45:17 +000041 ``protocol_handler_packages``.
cliechtie542b362011-03-18 00:49:16 +000042
43 e.g. we want to support a URL ``foobar://``. A module
44 ``my_handlers.protocol_foobar`` is provided by the user. Then
45 ``protocol_handler_packages.append("my_handlers")`` would extend the search
46 path so that ``serial_for_url("foobar://"))`` would work.
47 """
cliechti9b1d3ad2009-08-02 23:47:04 +000048 # check remove extra parameter to not confuse the Serial class
49 do_open = 'do_not_open' not in kwargs or not kwargs['do_not_open']
50 if 'do_not_open' in kwargs: del kwargs['do_not_open']
cliechtiaaf778a2009-08-05 17:59:15 +000051 # the default is to use the native version
52 klass = Serial # 'native' implementation
cliechti9b1d3ad2009-08-02 23:47:04 +000053 # check port type and get class
cliechtiaaf778a2009-08-05 17:59:15 +000054 try:
Chris Liechtibaec2a32015-08-07 14:42:15 +020055 url_lowercase = url.lower()
cliechtiaaf778a2009-08-05 17:59:15 +000056 except AttributeError:
cliechti2a6d5332011-03-04 02:08:32 +000057 # it's not a string, use default
cliechtiaaf778a2009-08-05 17:59:15 +000058 pass
cliechti109486b2009-08-02 00:00:11 +000059 else:
Chris Liechtibaec2a32015-08-07 14:42:15 +020060 if '://' in url_lowercase:
61 protocol = url_lowercase.split('://', 1)[0]
62 module_name = '.protocol_%s' % (protocol,)
cliechtie542b362011-03-18 00:49:16 +000063 for package_name in protocol_handler_packages:
Chris Liechtibaec2a32015-08-07 14:42:15 +020064 package = importlib.import_module(package_name)
cliechtie542b362011-03-18 00:49:16 +000065 try:
Chris Liechtibaec2a32015-08-07 14:42:15 +020066 handler_module = importlib.import_module(module_name, package_name)
cliechtie542b362011-03-18 00:49:16 +000067 except ImportError:
68 pass
69 else:
Chris Liechtibaec2a32015-08-07 14:42:15 +020070 klass = handler_module.Serial
cliechtie542b362011-03-18 00:49:16 +000071 break
cliechti2a6d5332011-03-04 02:08:32 +000072 else:
cliechtie542b362011-03-18 00:49:16 +000073 raise ValueError('invalid URL, protocol %r not known' % (protocol,))
cliechtiaaf778a2009-08-05 17:59:15 +000074 else:
75 klass = Serial # 'native' implementation
cliechti9b1d3ad2009-08-02 23:47:04 +000076 # instantiate and open when desired
cliechti109486b2009-08-02 00:00:11 +000077 instance = klass(None, *args, **kwargs)
78 instance.port = url
cliechti9b1d3ad2009-08-02 23:47:04 +000079 if do_open:
cliechti109486b2009-08-02 00:00:11 +000080 instance.open()
81 return instance