Skip to content

Instantly share code, notes, and snippets.

@Enteleform
Created June 17, 2016 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Enteleform/668c2f1d33189e90e1cd8c19a62732c9 to your computer and use it in GitHub Desktop.
Save Enteleform/668c2f1d33189e90e1cd8c19a62732c9 to your computer and use it in GitHub Desktop.
[Sublime Text] Modules + Dependencies : Best Practices
import sublime
import os, sys
directory = "enteleform_utils"
relative_ModulePath = directory + os.sep
moduleDirectory = "Modules"
moduleExtension = "py"
settingsDirectory = "Settings"
moduleDirectory_Loader_Name = "_MODULE_DIRECTORY_LOADER"
moduleFile_Loader_Name = "_MODULE_FILE_LOADER"
def get_PackagesPath():
packagesPath = sublime.packages_path()
if packagesPath == None \
or packagesPath.strip() == "":
for path in sys.path:
if path.endswith( "Sublime Text 3" + os.sep + "Packages" ):
packagesPath = path
break
if packagesPath.strip() == "":
packagesPath = None
return( packagesPath )
def get_Failed_PackagesPath( packagesPath ):
packagesPath = os.sep + "Packages" + packagesPath[ len( "None" ) : ]
return( packagesPath )
def get_ModulePath( moduleName ):
modulePath = ""
packagesPath = get_PackagesPath()
if packagesPath != None:
modulePath += ( packagesPath + os.sep )
else:
modulePath += ( "Packages" + os.sep )
modulePath = \
modulePath \
+ relative_ModulePath \
+ moduleDirectory + os.sep \
+ moduleName + os.extsep + moduleExtension
return( modulePath )
from enteleform_utils import _SELF
from enteleform_utils.Modules import File
from enteleform_utils.Modules import Log
import imp, os
def load( moduleDirectories, pluginGlobals ):
moduleExtensions = [ "py" ]
modulePaths = []
packagesPath = _SELF.get_PackagesPath()
selfPath = str( packagesPath ) + os.sep + _SELF.directory + os.sep + _SELF.moduleDirectory_Loader_Name + os.extsep + _SELF.moduleExtension
if packagesPath == None:
Log.writeError (
pluginFile = _SELF.get_Failed_PackagesPath( selfPath ),
errorNotes = "No \"Packages\" Path Found",
)
return
for directory in moduleDirectories:
if directory == _SELF.directory:
directory += os.sep + _SELF.moduleDirectory
modulePaths.append( packagesPath + os.sep + directory )
for index in range( 0, 2 ):
for path in modulePaths:
for fileName in os.listdir( path ):
for extension in moduleExtensions:
F = File.get_Data( path + os.sep + fileName, True )
if F.is_Valid == False \
or F.extension != extension \
or F.name == _SELF.moduleFile_Loader_Name \
or F.name == _SELF.moduleDirectory_Loader_Name:
continue
fileObject, file, description = imp.find_module( F.name, [ path ] )
pluginGlobals[ F.name ] = imp.load_module( F.name, fileObject, file, description )
from enteleform_utils import _SELF
from enteleform_utils.Modules import File
from enteleform_utils.Modules import Log
import imp, os
def load( moduleFiles, pluginGlobals ):
packagesPath = _SELF.get_PackagesPath()
selfPath = str( packagesPath ) + os.sep + _SELF.directory + os.sep + _SELF.moduleFile_Loader_Name + os.extsep + _SELF.moduleExtension
if packagesPath == None:
Log.writeError (
pluginFile = _SELF.get_Failed_PackagesPath( selfPath ),
errorNotes = "No \"Packages\" Path Found",
)
return
for index in range ( 0, 2 ):
for file in moduleFiles:
if file.startswith( _SELF.relative_selfPath ):
file = \
packagesPath + os.sep \
+ _SELF.directory + os.sep \
+ _SELF.moduleDirectory + os.sep \
+ file[ len( _SELF.relative_selfPath ) : ] + os.extsep + _SELF.moduleExtension
F = File.get_Data( file, True )
if F.is_Valid == False \
or F.name == _SELF.moduleFile_Loader_Name \
or F.name == _SELF.moduleDirectory_Loader_Name:
Log.writeError (
pluginFile = selfPath,
errorNotes = "Invalid Module",
errorSource = file,
)
continue
fileObject, file, description = imp.find_module( F.name, [ F.path ] )
pluginGlobals[ F.name ] = imp.load_module( F.name, fileObject, file, description )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment