Wobuzz/wobuzz/utils.py

48 lines
1.2 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) -> str:
"""
Makes a name unique by adding a number to it if the name already exists.
"""
if name in self.unique_names:
num_duplicates = self.unique_names[name]
self.unique_names[name] += 1
name = f"{name} {str(num_duplicates).zfill(2)}"
else:
self.unique_names[name] = 1
return name