## written by michael vorberg (mv@empty98.de)
## edited for pixelpuff by stefan ihringer (stefan@bildfehler.de)
##
## version 1.1 (2010-06-14): tries to open parent directory if desired path does not exist

import nuke
import os

def _get_directory(rawpath):
    """
    Make a path cross-platform by replacing network shares 
    """
    path = rawpath
    if (nuke.env["WIN32"]):
        path = path.replace("/Volumes/O/", "//calculon/o/")
        path = path.replace("/","\\")
    else:
        path = path.replace("//calculon/o/", "/Volumes/O/")

    path = os.path.dirname(path)

    # check if directory exists and try parent directories in case
    while (not os.path.exists(path)) and (len(path) > 2):
#        print "path not found: ", path
#        print "trying parent directory"
        path = os.path.dirname(path)

    return path
    



def exploreThis():
    """
    Shows the location of a footage element (Read or Write nodes) or an
    FBX/OBJ file (ReadGeo node) in an Explorer or Finder window.
    """
 
    if (nuke.env["WIN32"]):
        command = "explorer /e,"
    else:
        command = "open "

    m = nuke.selectedNode()
    if m.Class() in ('Read', 'Write', 'ReadGeo2'):
        path = _get_directory(m.knob("file").value())
        os.system(command + path)
    else:
        nuke.ask ('please select a Read, Write or a ReadGeo')

