def draw_graph(text,ind):
    try:
        sen=text.strip()
        print 'show',sen
        if not sen.startswith('(') or\
                not sen.endswith(')'):
            print 'tree error'
            return
        tree = MultiTree()
        tree.createTree(sen)
        writer = treeWriter(tree)
        outfile=pwd_path+'graph/'+str(ind)+'_'+time.strftime('%m-%d-%H:%M:%S')+'.png'
        writer.write(outfile)
        del tree, writer
        #return '/home/hdz/Desktop/QA/tree.png'
        print outfile
    except:
        print 'error'
        outfile=pwd_path+'graph/'+str(ind)+'_'+time.strftime('%m-%d-%H:%M:%S')+'.png'
        writer.write(outfile)
        del tree, writer
        #return '/home/hdz/Desktop/QA/tree.png'
        print outfile
    except:
        print 'error'
def draw_graph_main(text):
    ind=0
    for sen in text.split('\n'):
        if len(sen.strip())<=1:
            continue
        draw_graph(sen,ind)
        ind+=1
if __name__ == '__main__':
    tree = MultiTree()
    ##sen a CTB tree##
    sen='(IP (VP (ADVP (AD 全面))(VP (VV 推行)(NP (NP (NN 教育)(NN 收费))(NP (NN 公示)(NN 制度)))))(PU 。))'
    sen='(IP (IP (NP (CP (IP (NP (ADJP (JJ 超级))(NP (NN 病菌)))(VP (VV 出现)))(DEC 的))(NP (NN 根源)))(VP (VV 在于)(IP (NP (NN 人们))(VP (PP (P 在)(LCP (IP (VP (VV 患病)))(LC 时)))(VP (VV 滥用)(NP (NN 抗生素)))))))(PU 。))'
    tree.createTree(sen.decode(default_coding))
##    print '###'
##    tree.preorderTravel()
##    print '###'
    writer = treeWriter(tree)
    if len(sys.argv) > 1:#your graph file name
        outfile = sys.argv[1]
        writer.write(outfile) #write result to outfile
    else:
        writer.write() #write result to tree.png