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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
11
12
13
14
15
16
17
18
19
20
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]

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