Skip to content

Utils

Utilities

Various utility functions S.D.G.

get_safe_filename(clip_save_path, filename, extension=static.Clip.save_extension)

Make a filename that will not overwrite other files

Parameters:

Name Type Description Default
clip_save_path str

The path to the folder to save the clip in.

required
filename str

The desired base filename.

required
extension str

The file name extension for the type of file to save. Defaults to static.Clip.save_extension

save_extension

Returns:

Name Type Description
safe_filename str

The base filename with a numeric suffix added as needed.

Source code in rumchat_actor/utils.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def get_safe_filename(clip_save_path, filename, extension = static.Clip.save_extension):
    """Make a filename that will not overwrite other files

Args:
    clip_save_path (str): The path to the folder to save the clip in.
    filename (str): The desired base filename.
    extension (str): The file name extension for the type of file to save.
        Defaults to static.Clip.save_extension

Returns:
    safe_filename (str): The base filename with a numeric suffix added as needed."""

    increment = 0
    safe_filename = filename
    while os.path.exists(os.path.join(clip_save_path, safe_filename + "." + extension)):
        increment += 1
        safe_filename = filename + f"({increment})"
    return safe_filename

is_staff(user)

Check if a user is channel staff

Parameters:

Name Type Description Default
user User

A user in the chat (importantly with a badges attribute)

required

Returns:

Name Type Description
Result bool

Does the user have staff badges (admin or moderator)?

Source code in rumchat_actor/utils.py
13
14
15
16
17
18
19
20
21
22
def is_staff(user):
    """Check if a user is channel staff

Args:
    user (cocorum.chatapi.User): A user in the chat (importantly with a badges attribute)

Returns:
    Result (bool): Does the user have staff badges (admin or moderator)?"""

    return True in [badge in user.badges for badge in static.Moderation.staff_badges]

multiple_choice(title, options)

Allow the user to choose between multiple options. Automatically selects a lone option.

Parameters:

Name Type Description Default
title str

The question at hand.

required
options Sequence[str]

A subscriptable of option strings.

required

Returns:

Name Type Description
choice str

The chosen option.

Source code in rumchat_actor/utils.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def multiple_choice(title: str, options: Sequence[str]) -> str:
    """Allow the user to choose between multiple options.
        Automatically selects a lone option.

    Args:
        title (str): The question at hand.
        options (Sequence[str]): A subscriptable of option strings.

    Returns:
        choice (str): The chosen option."""

    # The function can't work if there's no options to choose from!
    assert len(options) > 0, "Too few options"

    # If there's just one option, choose it automatically
    if len(options) == 1:
        print(f"Only one option for, \"{title}\", and that is \"{options[0]}\".")
        return options[0]

    # Find the 'biggest' option by length, and then find out how long it is.
    option_max_width = len(max(options, key=len))

    # We're going to show a number next to each option as well, so we'd better
    # get the visual length of the biggest number as well.
    num_width = len(str(len(options)))

    # Make sure we get a valid choice.
    # The return statements will exit this loop for us.
    while True:
        # Display the question and the options, and get an input
        print(title)

        for i, option in enumerate(options):
            # Line all the options and their numbering up
            print(f"{i + 1:0{num_width}d}··{option:·>{option_max_width}}")

        # Finally, ask the user for some input.
        entry = input("Choice: ")

        # Option was typed directly
        if entry in options:
            return entry

        # Number was typed
        if entry.isnumeric():
            try:
                return options[int(entry) - 1]

            # The number wasn't a valid option index
            except IndexError:
                print("Entered number does not match an option.")

        # Something was typed but it was invalid
        if entry:
            print("Invalid entry. Please type a number or the option itself.")

Note that cocorum.utils is imported globally into this module.