#!/usr/bin/env python
#  Ei!, emacs, this is -*-Python-*- mode
########################################################################
#
#       License: BSD
#       Created: February 10, 2004
#       Author:  Francesc Alted - falted@pytables.org
#
#       $Source: /cvsroot/pytables/pytables/utils/ptdump,v $
#       $Id: ptdump,v 1.4 2004/02/25 16:09:00 falted Exp $
#
########################################################################

"""This utility lets you look into the data and metadata of your data files.

Pass the flag -h to this for help on usage.

"""

from tables import *

import os.path
import sys
import getopt

def dumpLeaf(leaf):
    global verbose
    global dump
    global showattrs
    global rng
    if verbose:
        print repr(leaf)
    else:
        print str(leaf)
    if showattrs:
        print "  "+repr(leaf.attrs)
    if dump:
        print "  Data dump:"
        print leaf.read(rng.start, rng.stop, rng.step)
    
def dumpGroup(h5file):
    global verbose
    global dump
    global showattrs
    global rng
    
    if not verbose and not dump:
        # Print the simplest object tree
        print h5file
        return

    # Print detailed info on nodes
    for group in h5file(classname="Group"):
        if verbose:
            print repr(group)
        else:
            print str(group)
        if showattrs:
            print "  "+repr(group._v_attrs)
        for leaf in h5file.listNodes(group, 'Leaf'):
            if verbose:
                print repr(leaf)
            else:
                print str(leaf)
            if showattrs:
                print "  "+repr(leaf.attrs)
            if dump:
                print "  Data dump:"
                print leaf.read(rng.start, rng.stop, rng.step)
            #print


usage = \
"""usage: %s [-R start,stop,step] [-a] [-h] [-d] [-v] file[:nodepath]
  -R select a range of rows in the form "start,stop,step"
  -a show attributes in nodes (only useful when -v or -d are active as well)
  -d dump data information onb leaves
  -h prints help on usage
  -v means dumping more metainformation on nodes
            \n""" \
% os.path.basename(sys.argv[0])

try:
    opts, pargs = getopt.getopt(sys.argv[1:], 'R:ahdv')
except:
    sys.stderr.write(usage)
    sys.exit(0)

# if we pass too much parameters, abort
if len(pargs) <> 1: 
    sys.stderr.write(usage)
    sys.exit(0)

# default options
rng = slice(None)
showattrs = 0
verbose = 0
dump = 0

# Get the options
for option in opts:
    if option[0] == '-R':
        try:
            rng = eval("slice("+option[1]+")")
        except:
            print "Error when getting the range parameter."
            (type, value, traceback) = sys.exc_info()
            print "  The error was:", value
            sys.stderr.write(usage)
            sys.exit(0)

    elif option[0] == '-a':
        showattrs = 1
    elif option[0] == '-h':
        sys.stderr.write(usage)
        sys.exit(0)
    elif option[0] == '-v':
        verbose = 1
    elif option[0] == '-d':
        dump = 1
    else:
        print option[0], ": Unrecognized option"
        sys.stderr.write(usage)
        sys.exit(0)

# Catch the files passed as the last arguments
src = pargs[0].split(':')
if len(src) == 1:
    filename, nodename = src[0], "/"
else:
    filename, nodename = src
    if nodename == "":
        # case where filename == "filename:" instead of "filename:/"
        nodename = "/"

# Check whether the specified node is a group or a leaf
h5file = openFile(filename, 'r')
nodeobject = h5file.getNode(nodename)
if isinstance(nodeobject, Group):
    # Close the file again and reopen using the rootUEP
#     h5file.close()
#     h5file = openFile(filename, 'r', rootUEP=nodename)
    dumpGroup(h5file)
elif isinstance(nodeobject, Leaf):
    # If it is not a Group, it must be a Leaf
    dumpLeaf(nodeobject)
else:
    # This should never happen
    print "Unrecognized object:", nodeobject

# Close the file
h5file.close()
