Files
DemonEditor/app/commons.py
2018-08-24 12:03:51 +03:00

74 lines
1.6 KiB
Python

import logging
from functools import wraps
from threading import Thread, Timer
from gi.repository import GLib
_LOG_FILE = "demon-editor.log"
_DATE_FORMAT = "%d-%m-%y %H:%M:%S"
_LOGGER_NAME = "main_logger"
logging.Logger(_LOGGER_NAME)
logging.basicConfig(level=logging.INFO,
filename=_LOG_FILE,
format="%(asctime)s %(message)s",
datefmt=_DATE_FORMAT)
def get_logger():
return logging.getLogger(_LOGGER_NAME)
def log(message, level=logging.ERROR):
get_logger().log(level, message)
def run_idle(func):
""" Runs a function with a lower priority """
@wraps(func)
def wrapper(*args, **kwargs):
GLib.idle_add(func, *args, **kwargs)
return wrapper
def run_task(func):
""" Runs function in separate thread """
@wraps(func)
def wrapper(*args, **kwargs):
task = Thread(target=func, args=args, kwargs=kwargs, daemon=True)
task.start()
return wrapper
def run_with_delay(timeout=5):
""" Starts the function with a delay.
If the previous timer still works, it will canceled!
"""
def run_with(func):
timer = None
@wraps(func)
def wrapper(*args, **kwargs):
nonlocal timer
if timer and timer.is_alive():
timer.cancel()
def run():
GLib.idle_add(func, *args, **kwargs, priority=GLib.PRIORITY_LOW)
timer = Timer(interval=timeout, function=run)
timer.start()
return wrapper
return run_with
if __name__ == "__main__":
pass