# -*- coding: utf8 -*-
'''
$RCSfile: showobject.py,v $
$Revision: 875 $
$Author: markus $
$Date: 2008-04-24 02:23:32 +0200 (Do, 24. Apr 2008) $

function view shows any objects, lists, ...
'''
from types import *


def LoL2String(LoL):
    sublists = 0
    try:
        for l in LoL:
            if type(l) in [TupleType, ListType]:
                sublists = 1
    except:
        pass
    if sublists == 1:
        SublistLength = []
        if type(LoL) in [type([1,2]), type((1,2))]:
            for l in LoL:
                SublistLength.append(LoL2String(l))
        else:
            SublistLength = 1
        return SublistLength
    else:
        # no sublists existing, return length
        try:
            return len(LoL)
        except:
            return 0
    
    
def viewObj(obj, viewObjFlag, indent):
    result = ''
    props = obj.__dict__.keys()
    props.sort()
    for prop in props:
        result = result + "\n%-35s %-30s %s" %(" "*2*indent + prop, getType(getattr(obj, prop)), getValue(getattr(obj, prop)))
        result = result + view(getattr(obj, prop), viewObjFlag, indent+1)
    return result


def viewDict(dict, viewObjFlag, indent):
    result = ''
    keys = dict.keys()
    keys.sort()
    for k in keys:
        result = result + "\n%-35s %-30s %s" %(" "*2*indent + str(k), getType(dict[k]), getValue(dict[k]))
        result = result + view(dict[k],viewObjFlag, indent+1)
    return result


def viewList(list, viewObjFlag, indent):
    result = ''
    for i in range(len(list)):
        k = list[i]
        result = result + "\n%-35s %-30s %s" %( (" "*2*indent +"[%i]" %(i)), getType(k), getValue(k))
        result = result + view(k,viewObjFlag, indent+1)
    return result

def viewTuple(list, viewObjFlag, indent):
    result = ''
    for i in range(len(list)):
        k = list[i]
        result = result + "\n%-35s %-30s %s" %( (" "*2*indent +"[%i]" %(i)), getType(k), getValue(k))
        result = result + view(k,viewObjFlag, indent+1)
    return result

def view(any, viewObjFlag=0, indent=0):
    if type(any) == DictType:
        return viewDict(any,viewObjFlag,indent)
    elif type(any) == ListType:
        return viewList(any,viewObjFlag,indent)
    elif type(any) == TupleType:
        return viewTuple(any,viewObjFlag,indent)
    elif type(any) == InstanceType:
        if not hasattr(any, '__repr__') or viewObjFlag>0:
            return viewObj(any,viewObjFlag,indent)
        else:
            return ''
    else:
        return ''

def getValue(a):
    if type(a) in (type('a'), type(u'ö'), type(1), type(1.2)):
        try:
            return str(a)
        except:
            return 'no ASCII'
    elif type(a) == InstanceType:
        if hasattr(a,'__repr__'):
            return str(a)
        else:
            return '--'
    elif type(a) == FileType:
        return str(a)
    else:
        return '--'
        
def getType(x):
    if type(x) == InstanceType:
        return "<%s>"%(str(x.__class__))
    else:
        return type(x)
        
    
# seperate function
def showDict(dict):
    keys = dict.keys()
    keys.sort()
    for k in keys:
        print "%-50s %s" %(k, dict[k])


def allClassMembers(aClass):
    try:
        mro = list(aClass.__mro__)
    except:
        # a classic class
        def getmro(aClass, recurse):
            mro = [aClass]
            for base in aClass.__bases__: mro.extend(recurse(base, recurse))
            return mro
        mro = getmro(aClass, getmro)
    mro.reverse()
    members = {}
    for someClass in mro: members.update(vars(someClass))
    return members





if __name__ == "__main__":
    a = {}
    b = {}
    a['hi'] = 10
    a['his'] = 10    
    b['df'] = 10
    a['dic'] = b
    tup = (b,a,666)
    c = [432,65,3,5,'tbfhre',u'öägf',a, tup]

    print view(c,0)
    print view([None])
