

__file__ = script.getResource(script.getId()+".py").getAbsolutePath()

def __init_python_path():
    import sys
    from org.gvsig.scripting import ScriptingLocator
    
    new_path = list()
    new_path.append(sys.path[0])

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

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

    if folders != None:
        for folder in folders:
            path = folder.getCanonicalPath()
            if not path in new_path:
              new_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 new_path:
              new_path.append(path)
    
    new_path.append("__classpath__")
    new_path.append("__pyclasspath__/")

    # Add global libs from uselib module
    # Deben ir a final para reproducir el mismo entorno que tenia el script cuando
    # se ejecuto e hizo el use_xxx.
    manager = ScriptingLocator.getManager()
    global_path = manager.getProperty("jython.global.path")
    if global_path != None:
        new_path.extend(global_path)

    del sys.path[:]
    for path in new_path:
        sys.path.append(path)


__init_python_path()

del __init_python_path


