Load modules from files in python 3.3, 3.4 and 3.5

This commit is contained in:
Manuel Cortez 2017-01-26 23:49:06 +00:00
parent b44b0f39cf
commit ad53a13e2c
3 changed files with 25 additions and 11 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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