INFO:
SublimeText does not natively support module reloading.
This can be a hassle when developing plugins, as you will constantly need to restart SublimeText in order to update your modules.
module_loader
solves that issue by reloading all modules every time the plugin is saved.
It also removes the need for explicit declaration of modules. Just list the directories you want to load all modules from in moduleDirectories
, and module_loader
will do the rest.
CODE:
import imp, os, sys
def load ( moduleDirectories, pluginGlobals ):
moduleExtensions = [ "py" ] #▒▒▒ add extensions here to extend module support ▒▒▒#
moduleLoader_Name = "module_loader"
modulePaths = []
for path in sys.path:
if path.endswith ( "Sublime Text 3" + os.sep + "Packages" ):
packagesPath = path
break
for directory in moduleDirectories:
modulePaths.append ( packagesPath + os.sep + directory )
for index in range ( 0, 2 ): #▒▒▒ loads modules twice to ensure dependencies are updated ▒▒▒#
for path in modulePaths:
for file in os.listdir ( path ):
for extension in moduleExtensions:
if file.endswith ( os.extsep + extension ):
moduleName = os.path.basename( file )[ : - len ( os.extsep + extension ) ]
if moduleName != moduleLoader_Name:
fileObject, file, description = imp.find_module( moduleName, [ path ] )
pluginGlobals[ moduleName ] = imp.load_module ( moduleName, fileObject, file, description )
Demo Plugin @ GitHub
( it’s the same plugin shown in the GIFs below)
DEMO:
With module_loader
:
Without module_loader
:
IMPLEMENTATION:
To implement module_loader
, just put a copy of module_loader.py
in your MyPlugin/Modules
directory and include these 3 lines in your plugin:
from MyPlugin.Modules import module_loader
moduleDirectories = [ "MyPlugin/Modules", "MyPlugin/MoreModules" ]
module_loader.load ( moduleDirectories, globals() )
( update directories accordingly )