From ad53a13e2c97122632ffdde2ae7e9d6e314b74a6 Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Thu, 26 Jan 2017 23:49:06 +0000 Subject: [PATCH] Load modules from files in python 3.3, 3.4 and 3.5 --- src/fenrir/core/commandManager.py | 10 +++------- src/fenrir/core/settingsManager.py | 7 +++---- src/fenrir/utils/module_utils.py | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 src/fenrir/utils/module_utils.py diff --git a/src/fenrir/core/commandManager.py b/src/fenrir/core/commandManager.py index a1b1779f..80c44b4d 100644 --- a/src/fenrir/core/commandManager.py +++ b/src/fenrir/core/commandManager.py @@ -4,10 +4,10 @@ # Fenrir TTY screen reader # By Chrys, Storm Dragon, and contributers. -import importlib.util import glob, os, time import __main__ from core import debug +from utils import module_utils class commandManager(): def __init__(self): @@ -39,9 +39,7 @@ class commandManager(): if fileName.startswith('__'): continue if fileExtension.lower() == '.py': - spec = importlib.util.spec_from_file_location(fileName, command) - command_mod = importlib.util.module_from_spec(spec) - spec.loader.exec_module(command_mod) + command_mod = module_utils.importModule(fileName, command) self.env['commands'][section][fileName.upper()] = command_mod.command() self.env['commandsIgnore'][section][fileName.upper()[fileName.upper().find('-')+1:]+'_IGNORE'] = False self.env['commands'][section][fileName.upper()].initialize(self.env) @@ -77,9 +75,7 @@ class commandManager(): fileName = fileName.split('/')[-1] if fileName.startswith('__'): continue - spec = importlib.util.spec_from_file_location(fileName ,subCommand) - command_mod = importlib.util.module_from_spec(spec) - spec.loader.exec_module(command_mod) + command_mod = module_utils.importModule(fileName ,subCommand) self.env['commands'][section][fileName.upper()] = command_mod.command() self.env['commands'][section][fileName.upper()].initialize(self.env,command) self.env['runtime']['debug'].writeDebugOut("Load script:" + section + "." + fileName.upper() ,debug.debugLevel.INFO, onAnyLevel=True) diff --git a/src/fenrir/core/settingsManager.py b/src/fenrir/core/settingsManager.py index 27c2e618..5c13c003 100644 --- a/src/fenrir/core/settingsManager.py +++ b/src/fenrir/core/settingsManager.py @@ -4,7 +4,6 @@ # Fenrir TTY screen reader # By Chrys, Storm Dragon, and contributers. -import importlib.util import os import __main__ from configparser import ConfigParser @@ -19,6 +18,7 @@ from core import environment from core import inputEvent from core.settings import settings from core import debug +from utils import module_utils class settingsManager(): def __init__(self): @@ -178,9 +178,8 @@ class settingsManager(): try: if self.env['runtime'][driverType] != None: self.env['runtime'][driverType].shutdown(self.env) - spec = importlib.util.spec_from_file_location(driverName, os.path.dirname(os.path.realpath(__main__.__file__)) + "/" + driverType + '/' + driverName + '.py') - driver_mod = importlib.util.module_from_spec(spec) - spec.loader.exec_module(driver_mod) + driver_mod = module_utils.importModule(driverName, +os.path.dirname(os.path.realpath(__main__.__file__)) + "/" + driverType + '/' + driverName + '.py') self.env['runtime'][driverType] = driver_mod.driver() self.env['runtime'][driverType].initialize(self.env) self.env['runtime']['debug'].writeDebugOut('Loading Driver ' + driverType +" OK",debug.debugLevel.INFO, onAnyLevel=True) diff --git a/src/fenrir/utils/module_utils.py b/src/fenrir/utils/module_utils.py new file mode 100644 index 00000000..e6018db3 --- /dev/null +++ b/src/fenrir/utils/module_utils.py @@ -0,0 +1,19 @@ +#!/bin/python +# -*- coding: utf-8 -*- +# Fenrir TTY screen reader +# By Chrys, Storm Dragon, and contributers. +import sys +version = sys.version[:3] # we only need major.minor version. +if version == "3.3" or version == "3.4": + from importlib.machinery import SourceFileLoader +else: # Python 3.5+, no support for python < 3.3. + import importlib.util + +def importModule(moduleName, moduleLocation): + if version == "3.3" or version == "3.4": + return SourceFileLoader(moduleName, moduleLocation).load_module() + else: + spec = importlib.util.spec_from_file_location(moduleName, moduleLocation) + driver_mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(driver_mod) + return driver_mod