Index: tkdeploader.py =================================================================== --- tkdeploader.py (revision 0) +++ tkdeploader.py (revision 0) @@ -0,0 +1,71 @@ +import os + +def basic_preloader(master, package=None, version=None, exact=False, + env_var=None): + """A basic preloader that adds a custom path to the list of + directories that Tcl uses when attempting to resolve packages with + the package command. + + The custom path is taken from the environment variable specified in + env_var, the other parameters received (package, version and exact) + are never used here. + """ + dep_path = os.environ.get(env_var) + if dep_path: + master.tk.eval( + 'global auto_path; ' + 'lappend auto_path {%s}' % dep_path) + + +class DependenceLoader(dict): + + def remove(self, package): + """Remove package so it will no longer be loaded when a Tcl + interpreter is created.""" + del self[package] + + + def add(self, package, preloader=None, version=None, exact=False, + **kwargs): + """Indicate that package must be loaded after the Tcl interpreter + is created. Adding the same package twice replaces the older. + + If preloader is specified it must be callable and should expect + to be invoked with a Tkinter.Tk instance, package, version, exact, + and any extra kwargs passed. + + If version is specified, and exact is True, then only the specific + package version will be accepted. Defining only version allows + Tcl to load newer versions of the package as long as the major + number is the same. If neither version nor exact are given then + any version of the package is acceptable for loading. + Remember to use a string when specifying version, like '1.2.3'. + """ + if preloader is not None and not hasattr(preloader, '__call__'): + raise TypeError('loader must be callable') + + self[package] = { + 'preloader': preloader, 'preloader_kw': kwargs, + 'version': version, + 'exact': exact} + + + def load_now(self, master): + """Load all the added packages.""" + for pkg, opts in self.iteritems(): + version, exact = opts['version'], opts['exact'] + + if opts['preloader'] is not None: + opts['preloader']( + master, pkg, version, exact, **opts['preloader_kw']) + + args = () + if exact: + args += ('-exact', ) + args += (pkg, version) + + # TclError may be raised now + master.tk.call('package', 'require', *args) + + +dependences = DependenceLoader() Property changes on: tkdeploader.py ___________________________________________________________________ Added: svn:mergeinfo