| 46 | # to pass objects which the config files will manipulate. |
| 47 | # \ingroup python |
| 48 | def loadConfig( searchPaths, contextDict = {}, raiseExceptions = False, subdirectory = "" ) : |
| 49 | |
| 50 | if isinstance( searchPaths, str ) : |
| 51 | searchPaths = IECore.SearchPath( os.environ.get( searchPaths, "" ) ) |
| 52 | |
| 53 | paths = searchPaths.paths |
| 54 | paths.reverse() |
| 55 | |
| 56 | visitedPaths = set() |
| 57 | for path in paths : |
| 58 | |
| 59 | if path in visitedPaths : |
| 60 | continue |
| 61 | else : |
| 62 | visitedPaths.add( path ) |
| 63 | |
| 64 | pyExtTest = re.compile( r"^[^~].*\.py$" ) |
| 65 | for dirPath, dirNames, fileNames in os.walk( os.path.join( path, subdirectory ) ) : |
| 66 | for fileName in filter( pyExtTest.search, sorted( fileNames ) ) : |
| 67 | fullFileName = os.path.abspath( os.path.join( dirPath, fileName ) ) |
| 68 | |
| 69 | IECore.msg( IECore.Msg.Level.Debug, "IECore.loadConfig", "Loading file \"%s\"" % fullFileName ) |
| 70 | |
| 71 | fileContextDict = contextDict.copy() |
| 72 | fileContextDict["__file__"] = fullFileName |
| 73 | |
| 74 | try : |
| 75 | with open( fullFileName, encoding = "utf-8" ) as f : |
| 76 | exec( |
| 77 | compile( f.read(), fullFileName, "exec" ), |
| 78 | fileContextDict, fileContextDict |
| 79 | ) |
| 80 | except Exception as m : |
| 81 | if raiseExceptions : |
| 82 | raise |
| 83 | else : |
| 84 | stacktrace = traceback.format_exc() |
| 85 | IECore.msg( IECore.Msg.Level.Error, "IECore.loadConfig", "Error executing file \"%s\" - \"%s\".\n %s" % ( fullFileName, m, stacktrace ) ) |
| 86 | |
| 87 | del fileContextDict["__file__"] |
| 88 | |
| 89 | loadConfig( "IECORE_CONFIG_PATHS", { "IECore" : IECore } ) |