예제 #1
0
def convert_and_transfer(in_path, dest_parent_dir_path):
    if fsu.is_dir(in_path):
        create_vid_converted_copy_of_dir(in_path, dest_parent_dir_path)
    elif fsu.is_file(in_path):
        if fsu.get_file_extension(in_path) in VID_FILE_TYPES_TO_BE_CONVERTED:
            convert_vid_file_to_mp4(in_path, dest_parent_dir_path)
        else:
            fsu.copy_object_to_dest(in_path, dest_parent_dir_path)
예제 #2
0
def rename_and_move_file_to_final_dest(in_vid_path, new_vid_name, dest_path):
    final_vid_path = dest_path + '\\' + new_vid_name
    
    if fsu.is_file(final_vid_path):
        raise Exception(new_vid_name + " already exists in DVD_RIP_DIR")
    
    parent_dir_path = fsu.get_parent_dir_from_path(in_vid_path)
    new_vid_path = parent_dir_path + '\\' + new_vid_name
    
    
    
    fsu.rename_file_overwrite(in_vid_path, new_vid_path)
    print('copying ' , new_vid_path, ' to ', dest_path, '...')
    fsu.copy_files_to_dest([new_vid_path], dest_path)
    fsu.delete_if_exists(new_vid_path)
예제 #3
0
def create_vid_converted_copy_of_dir(in_dir_path,
                                     dest_parent_dir_path,
                                     new_vid_type='mkv',
                                     delete_og=False):
    if fsu.is_dir(in_dir_path) != True:
        raise Exception("ERROR:  in_dir_path must point to dir")
    if fsu.get_parent_dir_from_path(in_dir_path) == dest_parent_dir_path:
        raise Exception(
            "ERROR:  dest_parent_dir_path cannot be the parent dir of in_dir_path"
        )

    # make new empty dir
    in_path_basename = fsu.get_basename_from_path(in_dir_path)
    new_dir_path = dest_parent_dir_path + '//' + in_path_basename
    fsu.make_dir_if_not_exist(new_dir_path)

    # copy all files to new dir and recursively run this function on all contained dirs
    obj_path_l = fsu.get_abs_path_l_of_all_objects_in_dir(in_dir_path)
    print(
        'obj_path_l:  ', obj_path_l
    )  #`````````````````````````````````````````````````````````````````````````````

    for obj_path in obj_path_l:
        if fsu.is_file(obj_path):
            # convert vid files, copy over other files
            if fsu.get_file_extension(
                    obj_path) in VID_FILE_TYPES_TO_BE_CONVERTED:
                if new_vid_type == 'mkv':
                    convert_vid_file_to_mkv(obj_path, new_dir_path)
                elif new_vid_type == 'mp4':
                    raise Exception(
                        "ERROR:  convert to mp4 will make you loose subs, and other stuff not set up for it, comment this out to continue"
                    )
                    convert_vid_file_to_mp4(obj_path, new_dir_path)
                else:
                    raise Exception("ERROR:  Invalid new_vid_type: ",
                                    new_vid_type)

                if delete_og:
                    fsu.delete_if_exists(obj_path)

            else:
                fsu.copy_object_to_dest(obj_path, new_dir_path)

        elif fsu.is_dir(obj_path):
            create_vid_converted_copy_of_dir(obj_path, new_dir_path)
        else:
            raise Exception("ERROR:  obj_path must be a file or a dir")
예제 #4
0
    def get_confirmed_code_dl__and_is_complete(store_name, value, quantity):
        def get_datetime_from_dt_csv_str(datetime_csv_str):
            ss = str_utils.multi_dim_split(['-', ' ', ':', "'"],
                                           datetime_csv_str)
            return datetime(int(ss[0]), int(ss[1]), int(ss[2]), int(ss[3]),
                            int(ss[4]), int(ss[5]))

        def add_to_code_d_if_exists_in_row_d(code_d, row_d, key_):
            if key_ in row_d.keys():
                code_d[key_] = row_d[key_]
            return code_d

        def build_code_d(row_d):
            code_d = {}

            header = 'main_code'
            if header in row_d.keys():
                code_d[header] = row_d[header][:-1]

            code_d = add_to_code_d_if_exists_in_row_d(code_d, row_d, 'pin')
            code_d = add_to_code_d_if_exists_in_row_d(
                code_d, row_d, 'biz_id'
            )  # eventually remove !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            return code_d

        confirmed_code_dl = []

        unused_code_csv_path = get__store_unused_codes_csv_path(
            code_req_d['store_name'])

        # return empty if code csv does not exist
        if not fsu.is_file(unused_code_csv_path):
            return confirmed_code_dl

        row_dl = logger.readCSV(unused_code_csv_path)
        store = STORE_D[
            store_name]  # will eventually be replaced with Store(store_name) !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        header_l = store.csv_header_l  # will eventually get this from config !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        row_num = 0
        while (len(confirmed_code_dl) < quantity and row_num < len(row_dl)):
            row_d = row_dl[row_num]
            if float(row_d['adv_value']) == float(value):

                code_d = build_code_d(row_d)

                last_confirm_datetime = get_datetime_from_dt_csv_str(
                    row_d['last_confirmed'])
                datetime_since_last_confirm = datetime.now(
                ) - last_confirm_datetime
                sec_since_last_confirm = datetime_since_last_confirm.total_seconds(
                )

                # if it has been too long since last check, re-check code
                if sec_since_last_confirm > MAX_CONFIRMED_CODE_AGE_DAYS * 3600:

                    #                     real_value = store.get_code_value(code_d) # put back !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                    real_value = 50  # remove, just for testing !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                    print('using ', real_value,
                          ' as test #, should check code for real, PUT BACK'
                          )  #`````````````````````````````````````````

                    # if after checking, the real value is less than the  value,
                    # remove the code from unused_codes and put it in failed_codes
                    if real_value < float(row_d['adv_value']):
                        logger.removeRowByHeaderVal('og_code_str',
                                                    row_d['og_code_str'],
                                                    unused_code_csv_path,
                                                    errorIfHeaderNotExist=True)

                        failed_codes_csv_path = get__store_failed_codes_csv_path(
                            store_name)
                        logger.logList(row_dl,
                                       failed_codes_csv_path,
                                       wantBackup=True,
                                       headerList=header_l,
                                       overwriteAction='append')
                        break

                # if code not old, or if you just checked and confirmed the code
                confirmed_code_dl.append(code_d)
            row_num += 1

        return confirmed_code_dl, len(confirmed_code_dl) == quantity
예제 #5
0
def make_code_card(kwargs, test_mode):
    template_type = kwargs['template_type']
    store_name = kwargs['store_name']

    img_paths_to_delete_l = [
        get__test_mode_blank_store_template_img_path(store_name),
        get__test_mode_blank_template_img_path(template_type)
    ]
    if test_mode:
        # remove the needed box coords from the json file if it exists
        if fsu.is_file(TEMPLATE_BOX_COORDS_JSON_PATH):
            dim_template_box_coords_ddd = json_logger.read(
                TEMPLATE_BOX_COORDS_JSON_PATH)

            if TEMPLATE_DIMS_STR in dim_template_box_coords_ddd:
                dim_template_box_coords_ddd.pop(TEMPLATE_DIMS_STR)

                json_logger.write(dim_template_box_coords_ddd,
                                  TEMPLATE_BOX_COORDS_JSON_PATH)

        # remove imgs so they get re-made
#         img_paths_to_delete_l = [get__normalized_color_template_img_path(template_type),
#                                  get__blank_template_img_path(template_type),
#                                  get__blank_store_template_img_path(store_name)]
        img_paths_to_delete_l.append(
            get__normalized_color_template_img_path(template_type))
        img_paths_to_delete_l.append(
            get__blank_template_img_path(template_type))
        img_paths_to_delete_l.append(
            get__blank_store_template_img_path(store_name))

    img_paths_to_delete_l.append(
        get__test_mode_blank_store_template_img_path(store_name))
    img_paths_to_delete_l.append(
        get__test_mode_blank_template_img_path(template_type))

    for img_path in img_paths_to_delete_l:
        fsu.delete_if_exists(img_path)

    # get template_type_box_coords from json file
    # if the json file does not exist, it will be created
    # if the box_coords are not in the json file, they will be loaded from the normalized_color_template_img
    # if the normalized_color_template_img does not exist, it will be created from the user-made color_template_img
    print('  Getting template_type_box_coords...')
    template_type_box_coords = get_template_type_box_coords(template_type)

    for box_title, box_coords in template_type_box_coords.items():
        print(box_title + ' : ' + str(box_coords))

    # get blank_store_template_img from path
    # if blank_store_template image does not exist, make it
    # if blank_template_img does not already exist, it will be created in the process
    print('  Getting blank_store_template_img...')
    if test_mode:
        blank_store_template_img_path = get__test_mode_blank_store_template_img_path(
            store_name)
    else:
        blank_store_template_img_path = get__blank_store_template_img_path(
            store_name)

    print('blank_store_template_img_path: ', blank_store_template_img_path
          )  #```````````````````````````````````````````````````````````

    if not fsu.is_file(blank_store_template_img_path):
        print(
            '    Blank_store_template_img does not exist, creating it now...')
        make_new_blank_store_template(kwargs, template_type_box_coords,
                                      test_mode)


#     else:
    blank_store_template_img = pu.open_img(blank_store_template_img_path)
    #     blank_store_template_img.show()

    print('  Making new code_card_img...')
    return make_new_code_card(kwargs, template_type_box_coords,
                              blank_store_template_img)
예제 #6
0
def make_new_blank_store_template(kwargs, box_coords, test_mode):
    store_name = kwargs['store_name']
    template_type = kwargs['template_type']

    normalized_color_template_img_path = get__normalized_color_template_img_path(
        template_type)

    if test_mode:
        blank_template_img_path = get__test_mode_blank_template_img_path(
            template_type)
        blank_store_template_img_path = get__test_mode_blank_store_template_img_path(
            store_name)
    else:
        blank_template_img_path = get__blank_template_img_path(template_type)
        blank_store_template_img_path = get__blank_store_template_img_path(
            store_name)

    # after getting the box coords from the color_template_img, replace all color boxes with background color to make
    # blank template that will be used to make blank store templates
    def make_new_blank_template(template_type):
        try:
            img = pu.open_img(normalized_color_template_img_path)
        except (FileNotFoundError):
            raise Exception(
                "ERROR:  Probably deleted normalized img without updating box coords, just run in test mode to fix"
            )
        box_color_l = TEMPLATE_COLORS_DD[template_type].values()

        # now that all the boxes should be all 1 color and match the defined box_colors, replace all color boxes with
        # background color to make blank template that will be used to make blank store templates
        if not test_mode:
            img = pu.replace_colors(img, box_color_l, BACKGROUND_COLOR)

        # add blank template labels
        box_title_l = TEMPLATE_COLORS_DD[template_type].keys()

        print('        Writing labels to blank_template_img...')
        for box_title in box_title_l:
            if box_title in BLANK_TEMPLATE_LBL_D.keys():
                img = write_txt_d_to_img_in_box_coords(
                    img, box_title, BLANK_TEMPLATE_LBL_D[box_title],
                    box_coords)

        img.save(blank_template_img_path)

    def build_txt_dd(kwargs, box_coords):
        txt_dd = {}

        # add instruc
        if 'instruc_type' in kwargs.keys() and 'instruc' in box_coords.keys():
            instruc_path = pv.INSTRUC_TXT_DIR_PATH + '\\' + kwargs[
                'instruc_type'] + '.txt'
            fsu.raise_exception_if_object_not_exist(
                instruc_path, "ERROR:  No txt file exists for instruc_type: " +
                kwargs['instruc_type'] + 'at ' + instruc_path)

            txt_dd['instruc'] = {
                'txt_lines': read_text_file(instruc_path),
                'param_d': INSTRUC_PARAM_D
            }

        # biz_id
        box_title = 'biz_id'
        if str_in_keys_of_all(box_title, [kwargs, box_coords]):
            txt_dd[box_title] = {
                'txt_lines': [kwargs['biz_id']],
                'param_d': CC_BLACK_LBL_PARAM_D
            }

        return txt_dd

    if not fsu.is_file(blank_template_img_path):
        print(
            '      Blank_template_img does not already exist, creating it now...'
        )
        make_new_blank_template(template_type)

    # make blank_store_template_img
    img = pu.open_img(blank_template_img_path)

    # write txt to img
    txt_dd = build_txt_dd(kwargs, box_coords)
    img = write_txt_dd_to_img(img, txt_dd, box_coords)

    # add images
    if 'logo' in TEMPLATE_COLORS_DD[template_type]:
        # if trimmed logo does not exist, make it by trimming original logo img
        trimmed_logo_img_path = pv.TRIMMED_LOGOS_DIR_PATH + '\\' + store_name + '__trimmed_logo.jpg'

        if not fsu.is_file(trimmed_logo_img_path):
            print(
                '      Trimmed_logo_img does not exist, trimming border of og_logo_img...'
            )
            og_logo_img_path = pv.OG_LOGOS_DIR_PATH + '\\' + store_name + '__og_logo.jpg'
            fsu.raise_exception_if_object_not_exist(
                og_logo_img_path, 'ERROR:  Logo img for ' + store_name +
                ' does not exist at ' + og_logo_img_path)

            logo_img = pu.open_img(og_logo_img_path)
            logo_img = pu.trim_border(logo_img)
            logo_img.save(trimmed_logo_img_path)

        trimmed_logo_img = pu.open_img(trimmed_logo_img_path)

        pu.paste_nicely_in_box_coords(trimmed_logo_img, img,
                                      box_coords['logo'], 'centered',
                                      'centered')

    #             img = pu.open_img("C:\\Users\\Brandon\\Documents\\Personal_Projects\\g_card_tools_root\\g_card_tools_big_data\\images\\code_cards\\492x1091\\color_template__normalized__g_card.png")
    #             pu.paste_nicely_in_box_coords(logo_img, img, box_coords['logo'], 'centered', 'centered')
    img.save(blank_store_template_img_path)
예제 #7
0
def get_template_type_box_coords(template_type):
    color_template_img_path = get__color_template_img_path(template_type)
    normalized_color_template_img_path = get__normalized_color_template_img_path(
        template_type)
    # read in data from json file if it exists
    if fsu.is_file(TEMPLATE_BOX_COORDS_JSON_PATH):
        dim_template_box_coords_ddd = json_logger.read(
            TEMPLATE_BOX_COORDS_JSON_PATH)
    else:
        dim_template_box_coords_ddd = {}

    # add template dims to ddd if not already exist
    if TEMPLATE_DIMS_STR not in dim_template_box_coords_ddd:
        dim_template_box_coords_ddd[TEMPLATE_DIMS_STR] = {}


#         template_box_coords_dd = dim_template_box_coords_ddd[TEMPLATE_DIMS_STR]

# add template_type if needed
    if template_type not in dim_template_box_coords_ddd[TEMPLATE_DIMS_STR]:
        dim_template_box_coords_ddd[TEMPLATE_DIMS_STR][template_type] = {}

    # if box coords don't already exist for template type, get them from image, also log in json file
    if dim_template_box_coords_ddd[TEMPLATE_DIMS_STR][template_type] == {}:
        #             color_template_img_path = TEMPLATE_DIMS_DIR_PATH + '\\color_template__' + template_type + '.JPG'

        # raise exception if color template img does not exist
        if not fsu.is_file(color_template_img_path):
            raise Exception(
                'ERROR:  color_template_img_path does not exist: ',
                color_template_img_path,
                '/ncannot get box_coords, maybe add a good way of adding new color templates here'
            )

        # normalize the colors of the original color_template_img if not already done
        if not fsu.is_file(normalized_color_template_img_path):
            print(
                '  Normalized_color_template_img does not exist, creating it now...'
            )
            img = pu.open_img(color_template_img_path)
            # power point likes to add new colors to images so first, need to normalize all colors by dominant -
            # meaning that if you have 100 (255, 255, 255) pixels and 50 (255, 255, 254) pixels, replace all with (255, 255, 255)
            print(
                '    Normalizing colors of original color_template_img by dominant...'
            )
            img = pu.normalize_colors__by_dominant(img,
                                                   COLOR_NORMILIZATION_FACTOR)

            # sometimes the new colors added by power point out-number the original colors, so use same method to normalize
            # all colors in img to the list of box colors
            box_color_l = TEMPLATE_COLORS_DD[template_type].values()
            print('    Normalizing those colors by list...')
            img = pu.normalize_colors__by_l(img, box_color_l,
                                            COLOR_NORMILIZATION_FACTOR)

            print('    Saving new normalized_color_template_img at ',
                  normalized_color_template_img_path, '...')
            img.save(normalized_color_template_img_path)

        # get box coords from normalized_color_template_img
        normalized_color_template_img = pu.open_img(
            normalized_color_template_img_path)
        print('  Getting box coords from normalized_color_template_img...')
        box_coords = pu.get_box_coords_d(normalized_color_template_img,
                                         TEMPLATE_COLORS_DD[template_type])
        #         print(box_coords)#```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

        dim_template_box_coords_ddd[TEMPLATE_DIMS_STR][
            template_type] = box_coords
        json_logger.write(dim_template_box_coords_ddd,
                          TEMPLATE_BOX_COORDS_JSON_PATH)

    return dim_template_box_coords_ddd[TEMPLATE_DIMS_STR][template_type]