

def use_plugin(pluginName):
  from org.gvsig.andami import PluginsLocator

  pluginsManager = PluginsLocator.getManager()
  other = pluginsManager.getPlugin(pluginName)
  scripting = pluginsManager.getPlugin("org.gvsig.scripting.app.mainplugin")
  scripting.addDependencyWithPlugin(other)

def use_jar(fname, root=__file__, isglobal=False):
  from org.gvsig.scripting import ScriptingLocator
  from java.io import File
  import sys
  import os

  if isinstance(fname,File):
    f = fname
    fname = f.getPath()
  else:
    f = File(fname)
  if not f.isAbsolute() :
    rf = File(root)
    if rf.isFile() :
      rf = rf.getParentFile()
    f = File( rf,fname)

  fname = f.getCanonicalPath()
  use_libs(fname,isglobal=isglobal)

def use_libs(folder, isglobal=False):
  from org.gvsig.scripting import ScriptingLocator
  import sys
  if not folder in sys.path:
    sys.path.append(folder)
    if isglobal:
      manager = ScriptingLocator.getManager()
      global_path = manager.getProperty("jython.global.path")
      if global_path == None:
        global_path = list()
        global_path.append(folder)    
        manager.setProperty("jython.global.path",global_path)
      elif not folder in global_path:
        global_path.append(folder)

def use_jars(base,folder, isglobal=False):
  import os, os.path

  if os.path.isfile(base):
    base = os.path.dirname(base)
  folder = os.path.join(base,folder)
  for f in os.listdir(folder):
    if f.endswith(".jar"):
      pathname = os.path.join(folder,f)
      use_jar(pathname,isglobal=isglobal)
