cat_attack.file_manipulation

 1import glob
 2import os
 3
 4from openpyxl import load_workbook, Workbook
 5from openpyxl.worksheet.worksheet import Worksheet
 6from openpyxl.utils.exceptions import InvalidFileException
 7
 8from cat_attack.cat_image import download_cat_pic
 9
10SEARCH_URL = "http://api.thecatapi.com/v1/images/search"
11
12
13def find_xlsx_files(folder_path: str) -> list[str]:
14    """
15    Finds all .xlsx files in a given directory and returns a list of found file paths. If the directory path isn't
16    valid or contains no .xlsx files it returns an empty list.
17
18    :param folder_path: path to a directory presumably containing .xlsx files
19    :type folder_path: str
20    :return: a list of found file paths
21    :rtype: list[str]
22    """
23    if not os.path.isdir(folder_path):
24        print(f"Folder path: {folder_path} you provided isn't valid.")
25        return []
26    found_files = glob.glob(folder_path + "/**/*.xlsx", recursive=True)
27
28    if not found_files:
29        print("No .xlsx files found in this folder.")
30        return []
31    else:
32        return found_files
33
34
35def attack_file(file: str, search_params: dict[str, str], ignore_already_attacked: bool = False) -> None:
36    """
37    Adds an "Important" sheet into each file and inserts a random cat image. If
38    ignore_already_attacked is set to True then the function will ignore all the .xlsx files that already have an
39    "Important" sheet. If the file cannot be opened or the image can't be downloaded it prints an error message.
40
41    :param file: file path
42    :type file: str
43    :param search_params: search parameters used to modify the query string in the url
44    :type search_params: dict[str, str]
45    :param ignore_already_attacked: whether a file has already been attacked (has an "Important" sheet), optional
46    :type ignore_already_attacked: bool
47    :return: nothing
48    :rtype: None
49    """
50    try:
51        current_file: Workbook = load_workbook(file)
52    except InvalidFileException:
53        print(f"Failed to open {os.path.basename(file)}.")
54    else:
55        if ignore_already_attacked and "Important" in current_file.sheetnames:
56            print(f"{os.path.basename(file)} has already been attacked 🙀")
57            return
58
59        important_sheet: Worksheet = current_file.create_sheet("Important")
60        cat_image = download_cat_pic(SEARCH_URL, search_params)
61
62        if cat_image is None:
63            print(f"Unable to download a cat image for {os.path.basename(file)}")
64            return
65
66        important_sheet.add_image(cat_image, "A1")
67        current_file.save(file)
SEARCH_URL = 'http://api.thecatapi.com/v1/images/search'
def find_xlsx_files(folder_path: str) -> list[str]:
14def find_xlsx_files(folder_path: str) -> list[str]:
15    """
16    Finds all .xlsx files in a given directory and returns a list of found file paths. If the directory path isn't
17    valid or contains no .xlsx files it returns an empty list.
18
19    :param folder_path: path to a directory presumably containing .xlsx files
20    :type folder_path: str
21    :return: a list of found file paths
22    :rtype: list[str]
23    """
24    if not os.path.isdir(folder_path):
25        print(f"Folder path: {folder_path} you provided isn't valid.")
26        return []
27    found_files = glob.glob(folder_path + "/**/*.xlsx", recursive=True)
28
29    if not found_files:
30        print("No .xlsx files found in this folder.")
31        return []
32    else:
33        return found_files

Finds all .xlsx files in a given directory and returns a list of found file paths. If the directory path isn't valid or contains no .xlsx files it returns an empty list.

Parameters
  • folder_path: path to a directory presumably containing .xlsx files
Returns

a list of found file paths

def attack_file( file: str, search_params: dict[str, str], ignore_already_attacked: bool = False) -> None:
36def attack_file(file: str, search_params: dict[str, str], ignore_already_attacked: bool = False) -> None:
37    """
38    Adds an "Important" sheet into each file and inserts a random cat image. If
39    ignore_already_attacked is set to True then the function will ignore all the .xlsx files that already have an
40    "Important" sheet. If the file cannot be opened or the image can't be downloaded it prints an error message.
41
42    :param file: file path
43    :type file: str
44    :param search_params: search parameters used to modify the query string in the url
45    :type search_params: dict[str, str]
46    :param ignore_already_attacked: whether a file has already been attacked (has an "Important" sheet), optional
47    :type ignore_already_attacked: bool
48    :return: nothing
49    :rtype: None
50    """
51    try:
52        current_file: Workbook = load_workbook(file)
53    except InvalidFileException:
54        print(f"Failed to open {os.path.basename(file)}.")
55    else:
56        if ignore_already_attacked and "Important" in current_file.sheetnames:
57            print(f"{os.path.basename(file)} has already been attacked 🙀")
58            return
59
60        important_sheet: Worksheet = current_file.create_sheet("Important")
61        cat_image = download_cat_pic(SEARCH_URL, search_params)
62
63        if cat_image is None:
64            print(f"Unable to download a cat image for {os.path.basename(file)}")
65            return
66
67        important_sheet.add_image(cat_image, "A1")
68        current_file.save(file)

Adds an "Important" sheet into each file and inserts a random cat image. If ignore_already_attacked is set to True then the function will ignore all the .xlsx files that already have an "Important" sheet. If the file cannot be opened or the image can't be downloaded it prints an error message.

Parameters
  • file: file path
  • search_params: search parameters used to modify the query string in the url
  • ignore_already_attacked: whether a file has already been attacked (has an "Important" sheet), optional
Returns

nothing