示例#1
0
def main():
    print("Begining test:")
    wids = utils.get_terminal_height()
    hits = utils.get_terminal_width()
    print("Screen width: {}\nScreen height :{}".format(wids, hits))
    print("-" * 80)
    print("Testing get_box_size function (this WILL end in an exception):")
    screens = [140, 120, 80, 60, 59, 40, 0]
    days = [10, 12, 15, 5]
    try:
        for size in screens:
            for day in days:
                mc, bw = get_box_size(day, size)
                print("{}: {:>5} {}: {:>3} {}: {:>3} {}: {:>4} {}: {}".format(
                    "Window width:",
                    size,
                    "Days in forcast",
                    day,
                    "max_cols",
                    mc,
                    "box_width",
                    bw,
                    "Columns used",
                    mc * bw))
    except IndexError as e:
        print("Exception correctly caught: \n\t{}".format(e))
        print("-" * 80)
        raise e
示例#2
0
def week_forecast(weat_db, width):
    color_flag = True   # this will eventually get set in a config file
    #  color_flag = False   # this will eventually get set in a config file
    COLORS = utils.get_colors(color_flag=color_flag)
    #  width = utils.get_terminal_width()
    height = utils.get_terminal_height()
    #  there has to be a more elegant way to do the following:
    format_tups = temp_return_format()
    format_list = list(x[0] for x in format_tups['body'])
    labels = []
    #  labels = list(x[1] for x in format_tups['body'])
    for lab in format_tups['body']:
        if lab[2] == 1:
            labels.append(lab[1])
        elif lab[2] > 1:
            temp = []
            temp.append(lab[1])
            for i in range(lab[2] - 1):
                temp.append('')
            labels.extend(temp)
        else:
            raise ValueError("formatter returned negative value")
    box_width = get_box_width(width - utils.max_len(labels))
    #  box_height = get_box_height(format_list, height)
    box_height = get_box_height(format_tups['body'], height)
    formatted_days = format_day_list(weat_db, box_width,
                                     box_height, format_list,
                                     COLORS)
    formatted_header = format_header(None, utils.max_len(labels), box_width,
                                     COLORS)
    results = join_days(formatted_days, formatted_header, labels, width,
                        box_width, box_height, COLORS)
    return results
示例#3
0
def grid_forecast(weat_db, width):
    # first, initialize all the vars
    color_flag = True   # this will eventually get set in a config file
    num_days = len(weat_db)
    #  width = utils.get_terminal_width()
    height = utils.get_terminal_height()
    COLORS = utils.get_colors(color_flag=color_flag)
    max_cols, box_width = get_box_size(len(weat_db), width)
    cols = min(max_cols, width//box_width)
    rows = math.ceil(num_days/cols)
    while rows * 9 > height:    # truncate if too long:
        rows -= 1
    res = []
    for c in range(rows):
        res.append([])

    # now we build the thing
    day_nums = min(rows * max_cols, len(weat_db))
    for day_index in range(day_nums):
        # if the start of a row the whole list gets popped on there...
        if len(res[day_index//cols]) == 0:
            res[day_index//cols] = add_box_lines(
                new_new_forecast_day_format(weat_db[day_index], box_width,
                                            weat_db[0]['high']['fahrenheit'],
                                            weat_db[0]['low']['fahrenheit'],
                                            COLORS),
                day_index, day_nums, cols)
        # or we have to add line by line
        else:
            temp = add_box_lines(new_new_forecast_day_format(
                weat_db[day_index], box_width,
                weat_db[0]['high']['fahrenheit'],
                weat_db[0]['low']['fahrenheit'],
                COLORS),
                day_index, day_nums, cols)
            for lin in range(len(temp)):
                res[day_index//cols][lin] += temp[lin]

    # return the result
    # but first flatten, and add padding:
    padding = ' ' * ((width - len(res[0][0])) // 2)
    res2 = []
    for c in res:
        for lin in c:
            res2.append(padding + lin)
    return res2
示例#4
0
def newer_cols_formatter(l, l2, start, func_obj, COLOR,
                         col_height=19, col_width=6):
    # important screen info!
    import sys
    home_dir = '/usr/self/weather/'
    if home_dir not in sys.path:
        sys.path.append(home_dir)
    import utils.utilities as utils
    screen_width = utils.get_terminal_width()
    screen_height = utils.get_terminal_height()

    # massage the func_obj into shape:
    if isinstance(func_obj, dict):
        func_obj = dict_to_obj(func_obj)
    func_obj_defaults = {'scale_format':        lambda x: str(x),
                         'right_string':        '| ',
                         'fargs':               [],
                         'label_fargs':         [],
                         'color_func':          '',
                         'label_color_func':    ''}
    for key in func_obj_defaults:
        if not hasattr(func_obj, key):
            setattr(func_obj, key, func_obj_defaults[key])

    if start is None:
        ind = 0
        while l[ind] is None:
            ind += 1
        start = l[ind]
    if hasattr(func_obj, 'scale_max') and hasattr(func_obj, 'scale_min'):
        mx = func_obj.scale_max(max(list(z for z in l if z is not None)))
        mn = func_obj.scale_min(min(list(z for z in l if z is not None)))
    else:
        mx = max(list(z for z in l if z is not None))
        mn = min(list(z for z in l if z is not None))

    col_height = min(col_height, screen_height - 4)
    if hasattr(func_obj, 'height_func'):
        old_col_height = col_height
        col_height = func_obj.height_func(mn, mx, col_height)
        if old_col_height != col_height:
            print("Changed {} to {}".format(old_col_height, col_height))

    zindexer = indexer_maker(mn, mx, col_height)
    # TODO:     get screen width and height (utils.get_terminal_width etc)
    #           limit l and l2 depending on col_width into screen width
    cols = column_maker(l[:((screen_width//col_width) - 2)],
                        start, zindexer, col_height, col_width,
                        func_obj, func_obj.color_func, COLOR)
    scale = scale_formatter(mx, mn, col_height, col_width,
                            func_obj.scale_format,
                            func_obj.right_string,
                            *func_obj.fargs)
    labels = func_obj.label_formatter(l2[:((screen_width//col_width) - 2)], 
                                      col_width,
                                      func_obj.label_func,
                                      func_obj.label_color_func,
                                      func_obj.label_test_func,
                                      COLOR,
                                      *func_obj.label_fargs)
    res = join_all(cols, scale, labels)
    return res