Skip to content

common

songbirdcore.common

find_file(path, filename)

Simple glob search for a file

Parameters:

  • path (str) –

    the path to the root folder to search within

  • filename (str) –

    the filename (supports glob patterns)

Returns:

  • List[str]

    List[str]: list of paths found that match

Source code in songbirdcore/common.py
114
115
116
117
118
119
120
121
122
123
124
125
def find_file(path: str, filename: str) -> List[str]:
    """Simple glob search for a file

    Args:
        path (str): the path to the root folder to search within
        filename (str): the filename (supports glob patterns)

    Returns:
        List[str]: list of paths found that match
    """
    paths = glob.glob(os.path.join(path, filename))
    return paths

fname_duper(fname, limit, count, dup_key)

Generates a duplicate filename for when a filename already exists

Parameters:

  • fname (str) –

    filename

  • limit (int) –

    a limit of dups before quitting the attempt

  • count (int) –

    recursive count tracker

  • dup_key (str) –

    the key to use as the duplicate addon

Returns:

  • Optional[str]

    Optional[str]: the modified filename, or None if the limit has been reached.

Source code in songbirdcore/common.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def fname_duper(fname: str, limit: int, count: int, dup_key: str) -> Optional[str]:
    """Generates a duplicate filename for when a filename already exists

    Args:
        fname (str): filename
        limit (int): a limit of dups before quitting the attempt
        count (int): recursive count tracker
        dup_key (str): the key to use as the duplicate addon

    Returns:
        Optional[str]: the modified filename, or None if the limit has been reached.
    """
    fname_split = os.path.splitext(fname)
    fname_noext = fname_split[0]
    ext = fname_split[1]
    if count == limit:
        logger.error(
            f"Max retry limit {limit} reached for fname {fname}. Please try changing some filenames and try again later."
        )
        return None
    if os.path.exists(fname):
        fname = fname_duper(fname_noext + dup_key + ext, limit, count + 1, dup_key)

    return fname

name_plate(entries)

renders the songbird entrypoint name plate

Parameters:

  • entries (List[str]) –

    add additional entries to the nameplate via this list

Source code in songbirdcore/common.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def name_plate(entries: List[str]) -> None:
    """renders the songbird entrypoint name plate

    Args:
        entries (List[str]): add additional entries to the nameplate via this list
    """
    # load project version from file
    print("===============================")
    print("=----Welcome to songbird🐦----=")
    print("===============================")
    print(f"--songbirdcore {version}")
    for entry in entries:
        print(entry)
    print("===============================")
    print("Message from developer:")
    print(
        "\t- dependencies have been upgraded, make sure you have run 'playwright install'."
    )
    print(
        "\t- If you encounter errors, please create an issue here https://github.com/cboin1996/songbirdcore/issues/"
    )
    print(
        f"At the main menu, type one of {[mode.value for mode in modes.Modes]} to switch modes!"
    )

pretty_list_of_basemodel_printer(list_of_models, ignore_keys=None)

renders a list to stdio given a list of pydantic BaseModel objects

Parameters:

  • list_of_models (List[BaseModel]) –

    list of pydantic models to print

  • ignore_keys (Optional[List[str]], default: None ) –

    any keys/fields in BaseModel not to print

Source code in songbirdcore/common.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def pretty_list_of_basemodel_printer(
    list_of_models: List[BaseModel], ignore_keys: Optional[List[str]] = None
):
    """
    renders a list to stdio given a list of pydantic BaseModel objects

    Args:
        list_of_models (List[BaseModel]): list of pydantic models to print
        ignore_keys (Optional[List[str]], optional): any keys/fields in BaseModel not to print
    """
    i = len(list_of_models) - 1
    logger.info("------------------------")
    for element in reversed(list_of_models):
        logger.info(i)
        for k, v in element.model_dump().items():
            if ignore_keys is not None:
                if (
                    k not in ignore_keys
                ):  # print the key and value if not in ignore_keys or special_dict
                    print("\t%s - %s" % (k, v))

            else:  # (default case) print the key and value
                print("\t%s - %s" % (k, v))
        i -= 1
        print("------------------------")

pretty_lst_printer(lyst)

print a list to stdio

Parameters:

  • lyst (List) –

    the list to print

Source code in songbirdcore/common.py
155
156
157
158
159
160
161
162
def pretty_lst_printer(lyst: List):
    """print a list to stdio

    Args:
        lyst (List): the list to print
    """
    for idx, item in enumerate(lyst):
        logger.info(f"\t [{idx}] - {item}")

remove_illegal_characters(filename)

Used for stripping file names of illegal characters used for saving

Parameters:

  • filename (str) –

    the file's name to strip illegal characters from

Returns:

  • str ( str ) –

    stripped file name

Source code in songbirdcore/common.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def remove_illegal_characters(filename) -> str:
    """
    Used for stripping file names of illegal characters used for saving

    Args:
        filename (str): the file's name to strip illegal characters from

    Returns:
        str: stripped file name
    """
    return (
        filename.replace("\\", "")
        .replace('"', "")
        .replace("/", "")
        .replace("*", "")
        .replace("?", "")
        .replace("<", "")
        .replace(">", "")
        .replace("|", "")
        .replace("'", "")
        .replace(":", "")
    )

set_logger_config_globally(log_level=logging.INFO)

Sets the python logging module settings for output to stdout and to file.

Parameters:

  • log_level (str, default: INFO ) –

    the log level

Source code in songbirdcore/common.py
23
24
25
26
27
28
29
30
31
32
33
34
35
def set_logger_config_globally(log_level=logging.INFO) -> None:
    """Sets the python logging module settings for output
    to stdout and to file.

    Args:
        log_level (str): the log level

    """
    logging.basicConfig(
        level=log_level,
        format="[%(levelname)s] %(name)s: %(message)s",
        handlers=[logging.StreamHandler()],
    )