subclient

Subsonic ncurses client
git clone https://git.e1e0.net/subclient.git
Log | Files | Refs | README | LICENSE

helpers.py (1373B)


      1 import os
      2 import threading
      3 import time
      4 
      5 HOME = os.environ.get('HOME')
      6 
      7 
      8 def format_duration(seconds):
      9     """Formats a number of seconds in hours:minutes:seconds
     10 
     11     :param seconds: The number of seconds to format
     12     :type seconds: int
     13     :return: String of the form "%H:%M:%S"
     14     :rtype: string
     15     """
     16     if seconds is None:
     17         return "-"
     18     time_format = "%H:%M:%S" if seconds > 3600 else "%M:%S"
     19     ty_res = time.gmtime(seconds)
     20     return time.strftime(time_format, ty_res)
     21 
     22 
     23 def get_config_file():
     24     config_home = os.environ.get('XDG_CONFIG_HOME')
     25     if config_home:
     26         config_file = config_home + '/subclient.ini'
     27     else:
     28         config_file = HOME + '/.config/subclient.ini'
     29 
     30     if not os.path.isfile(config_file):
     31         raise FileNotFoundError(f'Config file {config_file} does not exist')
     32 
     33     return config_file
     34 
     35 
     36 class Job(threading.Thread):
     37     def __init__(self, interval, execute, *args, **kwargs):
     38         threading.Thread.__init__(self)
     39         self.daemon = False
     40         self.stopped = threading.Event()
     41         self.interval = interval
     42         self.execute = execute
     43         self.args = args
     44         self.kwargs = kwargs
     45 
     46     def stop(self):
     47         self.stopped.set()
     48         self.join()
     49 
     50     def run(self):
     51         while not self.stopped.wait(self.interval.total_seconds()):
     52             self.execute(*self.args, **self.kwargs)