#! usr/bin/env python

def f_wrap(func):
    def wrap(fname):
        try:
            open(fname, 'r')
        except FileNotFoundError:
            if "." in fname:
                fname = fname.split("\\")[1]
            else:
                fname = "..\\" + fname
        return func(fname)
    return wrap

@f_wrap
def read_instr(fname):
    with open(fname, 'r') as get:
        lines = [line.strip(" \n").split(" ") \
                        for line in get.readlines()]
        lines = [list(map(int, i)) for i in lines]
    return lines[0], lines[1], lines[2:]

def bar_print(data, y_start, y_end):
    end_str = ""
    bar_format = "{:>3}" + ''.join(
                    [len(str(i[0])) * " " + "{}" for i in data]) + "\n"
    for ind in reversed(range(y_start, y_end+1)):
        end_str += bar_format.format(
                    ind, *["*" if k[2] >= ind else " " for k in data])
    end_str += ("   " + " ".join([str(i[0]) for i in data] + [str(data[-1][1])]))
    return end_str

def main(fname):
    head, records, data = read_instr(fname)
    print(bar_print(data, head[2], head[3]))

if __name__ == "__main__":
    from sys import argv
    try:
        main(argv[1])
    except IndexError:
        main("..\\ascii_bar_1.txt")

