ahio  1.0.0
I/O Communication Library
__init__.py
Go to the documentation of this file.
1 # -*- coding: utf-8; -*-
2 #
3 # Copyright (c) 2016 Álan Crístoffer
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 # THE SOFTWARE.
22 
23 import importlib.machinery
24 import os
25 import sys
26 
27 # path to the drivers folder
28 __modules_path = os.path.dirname(os.path.realpath(__file__))
29 # tries to load every file in the drivers folder
30 __modules = (__load_driver(driver) for driver in os.listdir(__modules_path))
31 # filters out the False elements, leaving only valid drivers
32 __modules = (d for d in __modules if d)
33 # only installed drivers that are available in this platform
34 __available = None
35 
36 
37 
41  global __modules
42  global __available
43 
44  if type(__modules) is not list:
45  __modules = list(__modules)
46 
47  if not __available:
48  __available = [d.ahioDriverInfo.NAME
49  for d in __modules
50  if d.ahioDriverInfo.AVAILABLE]
51 
52  return __available
53 
54 
55 
64 def driver_info(name):
65  driver = __locate_driver_named(name)
66  return driver.ahioDriverInfo if driver else None
67 
68 
69 
79  driver = __locate_driver_named(name)
80  return driver.Driver() if driver else None
81 
82 
83 
92 def __load_driver(name):
93  try:
94  mod_name = 'ahio.drivers.' + name.replace('.py', '')
95  driver_path = __modules_path + '/' + name
96  loader = importlib.machinery.SourceFileLoader(mod_name, driver_path)
97  driver = loader.load_module()
98  return driver if hasattr(driver, 'ahioDriverInfo') else False
99  except Exception:
100  return False
101 
102 
103 
108 def __locate_driver_named(name):
109  global __modules
110 
111  if type(__modules) is not list:
112  __modules = list(__modules)
113 
114  ms = [d for d in __modules if d.ahioDriverInfo.NAME == name]
115  if not ms:
116  return None
117  return ms[0]
def new_driver_object(name)
Instantiates a new object of the named driver.
Definition: __init__.py:78
def available_drivers()
Returns a list of available drivers names.
Definition: __init__.py:40
def driver_info(name)
Returns driver metadata.
Definition: __init__.py:64