Merge pull request #6 from manuelcortez/master

Load modules from files in python 3.3, 3.4 and 3.5
This commit is contained in:
chrys87 2017-01-27 10:47:51 +01:00 committed by GitHub
commit 60591530ff
3 changed files with 25 additions and 11 deletions

View File

@ -4,10 +4,10 @@
# Fenrir TTY screen reader # Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers. # By Chrys, Storm Dragon, and contributers.
import importlib.util
import glob, os, time import glob, os, time
import __main__ import __main__
from core import debug from core import debug
from utils import module_utils
class commandManager(): class commandManager():
def __init__(self): def __init__(self):
@ -39,9 +39,7 @@ class commandManager():
if fileName.startswith('__'): if fileName.startswith('__'):
continue continue
if fileExtension.lower() == '.py': if fileExtension.lower() == '.py':
spec = importlib.util.spec_from_file_location(fileName, command) command_mod = module_utils.importModule(fileName, command)
command_mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(command_mod)
self.env['commands'][section][fileName.upper()] = command_mod.command() self.env['commands'][section][fileName.upper()] = command_mod.command()
self.env['commandsIgnore'][section][fileName.upper()[fileName.upper().find('-')+1:]+'_IGNORE'] = False self.env['commandsIgnore'][section][fileName.upper()[fileName.upper().find('-')+1:]+'_IGNORE'] = False
self.env['commands'][section][fileName.upper()].initialize(self.env) self.env['commands'][section][fileName.upper()].initialize(self.env)
@ -77,9 +75,7 @@ class commandManager():
fileName = fileName.split('/')[-1] fileName = fileName.split('/')[-1]
if fileName.startswith('__'): if fileName.startswith('__'):
continue continue
spec = importlib.util.spec_from_file_location(fileName ,subCommand) command_mod = module_utils.importModule(fileName ,subCommand)
command_mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(command_mod)
self.env['commands'][section][fileName.upper()] = command_mod.command() self.env['commands'][section][fileName.upper()] = command_mod.command()
self.env['commands'][section][fileName.upper()].initialize(self.env,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) 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 # Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers. # By Chrys, Storm Dragon, and contributers.
import importlib.util
import os import os
import __main__ import __main__
from configparser import ConfigParser from configparser import ConfigParser
@ -19,6 +18,7 @@ from core import environment
from core import inputEvent from core import inputEvent
from core.settings import settings from core.settings import settings
from core import debug from core import debug
from utils import module_utils
class settingsManager(): class settingsManager():
def __init__(self): def __init__(self):
@ -178,9 +178,8 @@ class settingsManager():
try: try:
if self.env['runtime'][driverType] != None: if self.env['runtime'][driverType] != None:
self.env['runtime'][driverType].shutdown(self.env) 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 = module_utils.importModule(driverName,
driver_mod = importlib.util.module_from_spec(spec) os.path.dirname(os.path.realpath(__main__.__file__)) + "/" + driverType + '/' + driverName + '.py')
spec.loader.exec_module(driver_mod)
self.env['runtime'][driverType] = driver_mod.driver() self.env['runtime'][driverType] = driver_mod.driver()
self.env['runtime'][driverType].initialize(self.env) self.env['runtime'][driverType].initialize(self.env)
self.env['runtime']['debug'].writeDebugOut('Loading Driver ' + driverType +" OK",debug.debugLevel.INFO, onAnyLevel=True) 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