diff options
author | cliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a> | 2011-03-04 02:08:32 +0000 |
---|---|---|
committer | cliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a> | 2011-03-04 02:08:32 +0000 |
commit | 2a6d533e6f0a025c80ff43dbfbec68e675566307 (patch) | |
tree | 6ec684f3f7e189090a2c396ebbc98b0fd9ccc6da | |
parent | 8b2080b7cbbe31903d110d01f7b491ee4d427c40 (diff) | |
download | pyserial-git-2a6d533e6f0a025c80ff43dbfbec68e675566307.tar.gz |
implement URL handler as modules and import them at runtime. this allows to create more handlers as plug-in for pySerial
-rw-r--r-- | pyserial/serial/__init__.py | 20 | ||||
-rw-r--r-- | pyserial/serial/urlhandler/__init__.py | 0 | ||||
-rw-r--r-- | pyserial/serial/urlhandler/protocol_loop.py (renamed from pyserial/serial/loopback_connection.py) | 4 | ||||
-rw-r--r-- | pyserial/serial/urlhandler/protocol_rfc2217.py | 11 | ||||
-rw-r--r-- | pyserial/serial/urlhandler/protocol_socket.py (renamed from pyserial/serial/socket_connection.py) | 4 |
5 files changed, 25 insertions, 14 deletions
diff --git a/pyserial/serial/__init__.py b/pyserial/serial/__init__.py index 2c49007..cc5e6dc 100644 --- a/pyserial/serial/__init__.py +++ b/pyserial/serial/__init__.py @@ -38,18 +38,18 @@ def serial_for_url(url, *args, **kwargs): try: url_nocase = url.lower() except AttributeError: - # its not a string, use default + # it's not a string, use default pass else: - if url_nocase.startswith('rfc2217://'): - import rfc2217 # late import, so that users that don't use it don't have to load it - klass = rfc2217.Serial # RFC2217 implementation - elif url_nocase.startswith('socket://'): - import socket_connection # late import, so that users that don't use it don't have to load it - klass = socket_connection.Serial - elif url_nocase.startswith('loop://'): - import loopback_connection # late import, so that users that don't use it don't have to load it - klass = loopback_connection.Serial + if '://' in url_nocase: + protocol = url_nocase.split('://', 1)[0] + module_name = 'serial.urlhandler.protocol_%s' % (protocol,) + try: + handler_module = __import__(module_name) + except ImportError: + raise ValueError('invalid URL, protocol %r not known' % (protocol,)) + else: + klass = sys.modules[module_name].Serial else: klass = Serial # 'native' implementation # instantiate and open when desired diff --git a/pyserial/serial/urlhandler/__init__.py b/pyserial/serial/urlhandler/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/pyserial/serial/urlhandler/__init__.py diff --git a/pyserial/serial/loopback_connection.py b/pyserial/serial/urlhandler/protocol_loop.py index 784d7f8..eb510da 100644 --- a/pyserial/serial/loopback_connection.py +++ b/pyserial/serial/urlhandler/protocol_loop.py @@ -8,14 +8,14 @@ # The purpose of this module is.. well... You can run the unit tests with it. # and it was so easy to implement ;-) # -# (C) 2001-2009 Chris Liechti <cliechti@gmx.net> +# (C) 2001-2011 Chris Liechti <cliechti@gmx.net> # this is distributed under a free software license, see license.txt # # URL format: loop://[option[/option...]] # options: # - "debug" print diagnostic messages -from serialutil import * +from serial.serialutil import * import threading import time import logging diff --git a/pyserial/serial/urlhandler/protocol_rfc2217.py b/pyserial/serial/urlhandler/protocol_rfc2217.py new file mode 100644 index 0000000..981ba45 --- /dev/null +++ b/pyserial/serial/urlhandler/protocol_rfc2217.py @@ -0,0 +1,11 @@ +#! python +# +# Python Serial Port Extension for Win32, Linux, BSD, Jython +# see ../__init__.py +# +# This is a thin wrapper to load the rfc2271 implementation. +# +# (C) 2011 Chris Liechti <cliechti@gmx.net> +# this is distributed under a free software license, see license.txt + +from serial.rfc2217 import Serial diff --git a/pyserial/serial/socket_connection.py b/pyserial/serial/urlhandler/protocol_socket.py index e34fc2c..facd553 100644 --- a/pyserial/serial/socket_connection.py +++ b/pyserial/serial/urlhandler/protocol_socket.py @@ -10,14 +10,14 @@ # The purpose of this module is that applications using pySerial can connect to # TCP/IP to serial port converters that do not support RFC 2217. # -# (C) 2001-2009 Chris Liechti <cliechti@gmx.net> +# (C) 2001-2011 Chris Liechti <cliechti@gmx.net> # this is distributed under a free software license, see license.txt # # URL format: socket://<host>:<port>[/option[/option...]] # options: # - "debug" print diagnostic messages -from serialutil import * +from serial.serialutil import * import time import socket import logging |