from typing import List
import os
import re


def create_output_dir(dir_name) -> bool:
    if not os.path.isdir(dir_name) or not os.path.exists(dir_name):
        print("Creating output directory: %s" % dir_name)
        try:
            os.makedirs(dir_name)
        except OSError:
            print("Creation of the directory %s failed" % dir_name)
            return False
        else:
            print("Successfully created the directory %s " % dir_name)
            return True
    else:
        return True


def get_files_from_dir(dir_path: str, file_type: str = None) -> List[str]:
    if not os.path.isdir(dir_path):
        return []
    file_paths = [
        f for f in os.listdir(dir_path) if os.path.isfile(os.path.join(dir_path, f))
    ]
    if file_type is not None:
        file_paths = [f for f in file_paths if f.lower().endswith(file_type.lower())]
    return file_paths


def _int_string_sort(file_name) -> int:
    match = re.match("[0-9]+", os.path.basename(file_name))
    if not match:
        return 0
    return int(match[0])


def get_highest_numbered_file(
    dir_path: str, file_type: str = None, filter_keyword=None
) -> str:
    file_names = get_files_from_dir(dir_path)

    if file_type is not None:
        file_names = [
            file_name
            for file_name in file_names
            if file_name.lower().endswith(file_type.lower())
        ]
    if filter_keyword is not None:
        file_names = [
            file_name
            for file_name in file_names
            if filter_keyword.lower() in file_name.lower()
        ]
    if len(file_names) == 0:
        return None
    highest_numbered_file = sorted(file_names, key=_int_string_sort, reverse=True)[0]
    return os.path.join(dir_path, highest_numbered_file)

[((59, 11, 59, 56), 'os.path.join', 'os.path.join', ({(59, 24, 59, 32): 'dir_path', (59, 34, 59, 55): 'highest_numbered_file'}, {}), '(dir_path, highest_numbered_file)', False, 'import os\n'), ((22, 11, 22, 34), 'os.path.isdir', 'os.path.isdir', ({(22, 25, 22, 33): 'dir_path'}, {}), '(dir_path)', False, 'import os\n'), ((33, 31, 33, 58), 'os.path.basename', 'os.path.basename', ({(33, 48, 33, 57): 'file_name'}, {}), '(file_name)', False, 'import os\n'), ((7, 11, 7, 34), 'os.path.isdir', 'os.path.isdir', ({(7, 25, 7, 33): 'dir_name'}, {}), '(dir_name)', False, 'import os\n'), ((7, 42, 7, 66), 'os.path.exists', 'os.path.exists', ({(7, 57, 7, 65): 'dir_name'}, {}), '(dir_name)', False, 'import os\n'), ((10, 12, 10, 33), 'os.makedirs', 'os.makedirs', ({(10, 24, 10, 32): 'dir_name'}, {}), '(dir_name)', False, 'import os\n'), ((25, 19, 25, 39), 'os.listdir', 'os.listdir', ({(25, 30, 25, 38): 'dir_path'}, {}), '(dir_path)', False, 'import os\n'), ((25, 58, 25, 83), 'os.path.join', 'os.path.join', ({(25, 71, 25, 79): 'dir_path', (25, 81, 25, 82): 'f'}, {}), '(dir_path, f)', False, 'import os\n')]