

__file__ = script.getFile().getAbsolutePath()


def __init_python_path():
    import sys

    # Add folder of script
    path = script.getFile().getParentFile().getCanonicalPath()
    if not path in sys.path:
      sys.path.append(path)

    # Add folder of root user scripts
    path = script.getManager().getUserFolder().getFile().getCanonicalPath()
    if not path in sys.path:
      sys.path.append(path)
    
    # Add folders of libs
    folders = script.getManager().getLibFolders()
    if folders != None:
        for folder in folders:
            path = folder.getCanonicalPath()
            if not path in sys.path:
              sys.path.append(path)

    # Add the scripts folder of system folders
    folders = script.getManager().getSystemFolder()
    if folders != None:
        for folder in folders.getUnits():
            path = folder.getFile().getCanonicalPath()
            if not path in sys.path:
              sys.path.append(path)


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__):
  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()
  if not fname in sys.path:
    sys.path.append(fname) 


__init_python_path()

del __init_python_path


