Home | History | Annotate | Download | only in serial
      1 #!/usr/bin/env python 
      2 
      3 # portable serial port access with python
      4 # this is a wrapper module for different platform implementations
      5 #
      6 # (C) 2001-2010 Chris Liechti <cliechti (at] gmx.net>
      7 # this is distributed under a free software license, see license.txt
      8 
      9 VERSION = '2.7'
     10 
     11 import sys
     12 
     13 if sys.platform == 'cli':
     14     from serial.serialcli import *
     15 else:
     16     import os
     17     # chose an implementation, depending on os
     18     if os.name == 'nt': #sys.platform == 'win32':
     19         from serial.serialwin32 import *
     20     elif os.name == 'posix':
     21         from serial.serialposix import *
     22     elif os.name == 'java':
     23         from serial.serialjava import *
     24     else:
     25         raise ImportError("Sorry: no implementation for your platform ('%s') available" % (os.name,))
     26 
     27 
     28 protocol_handler_packages = [
     29         'serial.urlhandler',
     30         ]
     31 
     32 def serial_for_url(url, *args, **kwargs):
     33     """\
     34     Get an instance of the Serial class, depending on port/url. The port is not
     35     opened when the keyword parameter 'do_not_open' is true, by default it
     36     is. All other parameters are directly passed to the __init__ method when
     37     the port is instantiated.
     38 
     39     The list of package names that is searched for protocol handlers is kept in
     40     ``protocol_handler_packages``.
     41 
     42     e.g. we want to support a URL ``foobar://``. A module
     43     ``my_handlers.protocol_foobar`` is provided by the user. Then
     44     ``protocol_handler_packages.append("my_handlers")`` would extend the search
     45     path so that ``serial_for_url("foobar://"))`` would work.
     46     """
     47     # check remove extra parameter to not confuse the Serial class
     48     do_open = 'do_not_open' not in kwargs or not kwargs['do_not_open']
     49     if 'do_not_open' in kwargs: del kwargs['do_not_open']
     50     # the default is to use the native version
     51     klass = Serial   # 'native' implementation
     52     # check port type and get class
     53     try:
     54         url_nocase = url.lower()
     55     except AttributeError:
     56         # it's not a string, use default
     57         pass
     58     else:
     59         if '://' in url_nocase:
     60             protocol = url_nocase.split('://', 1)[0]
     61             for package_name in protocol_handler_packages:
     62                 module_name = '%s.protocol_%s' % (package_name, protocol,)
     63                 try:
     64                     handler_module = __import__(module_name)
     65                 except ImportError:
     66                     pass
     67                 else:
     68                     klass = sys.modules[module_name].Serial
     69                     break
     70             else:
     71                 raise ValueError('invalid URL, protocol %r not known' % (protocol,))
     72         else:
     73             klass = Serial   # 'native' implementation
     74     # instantiate and open when desired
     75     instance = klass(None, *args, **kwargs)
     76     instance.port = url
     77     if do_open:
     78         instance.open()
     79     return instance
     80