Wobuzz/wobuzz/utils.py

55 lines
1.4 KiB
Python

#!/usr/bin/python3
import os
from pathlib import Path
class Utils:
home_path = str(Path.home())
wobuzz_location = os.path.dirname(os.path.abspath(__file__))
settings_location = f"{wobuzz_location}/settings.json"
def __init__(self, app):
self.app = app
self.unique_names = []
def format_time(self, ms):
seconds = int(ms / 1000) % 60
minutes = int(ms / (1000 * 60)) % 60
hours = int(ms / (1000 * 60 * 60))
seconds_str, minutes_str, hours_str = ("",) * 3 # create empty strings
if hours > 0:
hours_str = f"{hours}:"
minutes_str = f"{minutes}:".zfill(2)
seconds_str = f"{seconds}".zfill(2)
output = hours_str + minutes_str + seconds_str
return output
def unique_name(self, name: str, ignore: str = None) -> str:
"""
Makes a name unique by adding a number to it if the name already exists.
"""
# just return the original name if it is the ignore name
# this can be useful when e.g. renaming something bc. u don't want to append a number when the user doesn't
# change anything
if name == ignore:
return name
occurrences = 0
unique_name = name
while unique_name in self.unique_names:
occurrences += 1
unique_name = f"{name} {str(occurrences).zfill(2)}"
self.unique_names.append(unique_name)
return unique_name