Skip to content

Commit

Permalink
WIP namespace non-GUI modules
Browse files Browse the repository at this point in the history
  • Loading branch information
simonvanderveldt committed Mar 19, 2017
1 parent 3e195d5 commit a085229
Showing 1 changed file with 13 additions and 58 deletions.
71 changes: 13 additions & 58 deletions src/App/FreeCADInit.py
Expand Up @@ -72,74 +72,29 @@ def InitApplications():
SystemWideMacroDir = FreeCAD.getHomePath()+'Macro'
SystemWideMacroDir = os.path.realpath(SystemWideMacroDir)

#print FreeCAD.getHomePath()
if os.path.isdir(FreeCAD.getHomePath()+'src\\Tools'):
sys.path.append(FreeCAD.getHomePath()+'src\\Tools')
# Searching for module dirs +++++++++++++++++++++++++++++++++++++++++++++++++++
# Use dict to handle duplicated module names
ModDict = {}
if os.path.isdir(ModDir):
ModDirs = os.listdir(ModDir)
for i in ModDirs: ModDict[i.lower()] = os.path.join(ModDir,i)
else:
Wrn ("No modules found in " + ModDir + "\n")
# Search for additional modules in the home directory
if os.path.isdir(HomeMod):
HomeMods = os.listdir(HomeMod)
for i in HomeMods: ModDict[i.lower()] = os.path.join(HomeMod,i)
# Search for additional modules in the macro directory
if os.path.isdir(MacroMod):
MacroMods = os.listdir(MacroMod)
for i in MacroMods:
key = i.lower()
if key not in ModDict: ModDict[key] = os.path.join(MacroMod,i)
# Search for additional modules in command line
for i in AddPath:
if os.path.isdir(i): ModDict[i] = i
#AddModPaths = App.ParamGet("System parameter:AdditionalModulePaths")
#Err( AddModPaths)
# add also this path so that all modules search for libraries
# they depend on first here
PathExtension = BinDir + os.pathsep

# prepend all module paths to Python search path
Log('Init: Searching for modules...\n')

Log('Init: Searching for modules...\n')

# to have all the module-paths available in FreeCADGuiInit.py:
FreeCAD.__ModDirs__ = list(ModDict.values())

# this allows importing with:
# from FreeCAD.Module import package
FreeCAD.__path__ = [ModDir, Lib64Dir, LibDir, HomeMod]

# also add these directories to the sys.path to
# not change the old behaviour. once we have moved to
# proper python modules this can eventuelly be removed.
sys.path = [ModDir, Lib64Dir, LibDir] + sys.path

for Dir in ModDict.values():
if ((Dir != '') & (Dir != 'CVS') & (Dir != '__init__.py')):
sys.path.insert(0,Dir)
PathExtension += Dir + os.pathsep
InstallFile = os.path.join(Dir,"Init.py")
if (os.path.exists(InstallFile)):
try:
# XXX: This looks scary securitywise...

with open(InstallFile) as f:
exec(f.read())
except Exception as inst:
Log('Init: Initializing ' + Dir + '... failed\n')
Log('-'*100+'\n')
Log(traceback.format_exc())
Log('-'*100+'\n')
Err('During initialization the error ' + str(inst) + ' occurred in ' + InstallFile + '\n')
Err('Please look into the log file for further information')
else:
Log('Init: Initializing ' + Dir + '... done\n')
else:
Log('Init: Initializing ' + Dir + '(Init.py not found)... ignore\n')
sys.path = [ModDir] + sys.path

import pkgutil
import freecad.modules
for importer, module_name, ispkg in pkgutil.iter_modules(freecad.modules.__path__, "freecad.modules."):
Log("Submodule " + module_name + ", is a package: " + str(ispkg) + '\n')
if ispkg:
Log("Initializing " + module_name + '\n')
try:
__import__(module_name + '.init')
except ImportError as error:
Err('During initialization the error ' + str(error) + ' occurred in ' + module_name + '\n')

Log("Using "+ModDir+" as module path!\n")
# new paths must be prepended to avoid to load a wrong version of a library
Expand Down

0 comments on commit a085229

Please sign in to comment.