#!/usr/bin/env python

import os
import sys
import argparse

from groundstation import logger
log = logger.getLogger(__name__)

from groundstation.gref import Gref

from groundstation.station import Station

from collections import OrderedDict

station = Station.from_env(None)


def _create_option_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument("channel", nargs=1)
    parser.add_argument("identifier", nargs=1)
    return parser


def main(argv):
    parser = _create_option_parser()
    args = parser.parse_args(sys.argv[1:])

    children = OrderedDict()
    graph_tips = OrderedDict()

    channel = args.channel.pop()
    identifier = args.identifier.pop()

    gref = Gref(station.store, channel, identifier)
    roots = []
    processed = []
    tips = gref.tips()
    lanes = []
    while tips:
        tip = tips.pop(0)
        if tip in processed:
            continue
        processed.append(tip)
        parents = gref.direct_parents(tip)
        for parent in parents:
            try:
                children[parent].append(tip)
            except KeyError:
                children[parent] = [tip]
        if parents == []:
            if tip not in roots:
                roots.append(tip)
        else:
            tips.extend(parents)

    buf = []
    def _print_tip(tip, lane, chr="* "):
        if tip in roots:
            tip = "%s (root)" % (tip)
        buf.append("%s%s%s\t%s" % ("".join(lanes[0:lane]), chr, "".join(lanes[lane+1:]), tip))

    lane = 0
    lanes.append("| ")
    for root in reversed(roots):
        graph_tips[root] = [lane]
        lanes.append("  ")
        lane += 1

    while graph_tips:
        k, v = graph_tips.popitem(False)
        lane = v[0]
        _print_tip(k, lane)
        if k in graph_tips:
            continue
        if k not in children:
            _print_tip("", lane, "/ ")
            lanes[lane] = "  "
            continue
        for i in children[k]:
            if lanes[lane] != "| ":
                _print_tip("", lane, "/ ")

            lanes[lane] = "| "
            graph_tips[i] = [lane]
            lane += 1

    while buf:
        print buf.pop()

if __name__ == "__main__":
    main(sys.argv)

82d3da7916b33a537b639ef8e4f14539cb4b61ba
#!/usr/bin/env python

import os
import sys
import argparse

from groundstation import logger
log = logger.getLogger(__name__)

from groundstation.gref import Gref

from groundstation.station import Station

from collections import OrderedDict

station = Station.from_env(None)


def _create_option_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument("channel", nargs=1)
    parser.add_argument("identifier", nargs=1)
    return parser


def main(argv):
    parser = _create_option_parser()
    args = parser.parse_args(sys.argv[1:])

    children = OrderedDict()
    graph_tips = OrderedDict()

    channel = args.channel.pop()
    identifier = args.identifier.pop()

    gref = Gref(station.store, channel, identifier)
    roots = []
    processed = []
    tips = gref.tips()
    lanes = []
    while tips:
        tip = tips.pop(0)
        if tip in processed:
            continue
        processed.append(tip)
        parents = gref.direct_parents(tip)
        for parent in parents:
            try:
                children[parent].append(tip)
            except KeyError:
                children[parent] = [tip]
        if parents == []:
            if tip not in roots:
                roots.append(tip)
        else:
            tips.extend(parents)

    buf = []
    def _print_tip(tip, lane, chr="* "):
        if tip in roots:
            tip = "%s (root)" % (tip)
        buf.append("%s%s%s\t%s" % ("".join(lanes[0:lane]), chr, "".join(lanes[lane+1:]), tip))

    lane = 0
    lanes.append("| ")
    for root in reversed(roots):
        graph_tips[root] = [lane]
        lanes.append("  ")
        lane += 1

    while graph_tips:
        k, v = graph_tips.popitem(False)
        lane = v[0]
        _print_tip(k, lane)
        if k in graph_tips:
            continue
        if k not in children:
            _print_tip("", lane, "/ ")
            lanes[lane] = "  "
            continue
        for i in children[k]:
            if lanes[lane] != "| ":
                _print_tip("", lane, "/ ")

            lanes[lane] = "| "
            graph_tips[i] = [lane]
            lane += 1

    while buf:
        print buf.pop()

if __name__ == "__main__":
    main(sys.argv)
