toxygen/toxygen/util.py

108 lines
2 KiB
Python
Raw Normal View History

import os
2016-02-26 15:32:36 +01:00
import time
2016-07-07 12:54:02 +02:00
import shutil
2016-10-31 22:04:48 +01:00
import sys
2017-02-12 21:15:33 +01:00
import re
2016-02-19 22:10:24 +01:00
2017-07-09 16:37:05 +02:00
program_version = '0.3.1'
2017-04-17 21:04:22 +02:00
def cached(func):
saved_result = None
def wrapped_func():
nonlocal saved_result
if saved_result is None:
saved_result = func()
return saved_result
return wrapped_func
2016-02-19 22:10:24 +01:00
def log(data):
2017-07-03 20:36:11 +02:00
try:
with open(curr_directory() + '/logs.log', 'a') as fl:
fl.write(str(data) + '\n')
except:
pass
2016-02-19 22:10:24 +01:00
2016-02-20 09:06:24 +01:00
2017-04-17 21:04:22 +02:00
@cached
def curr_directory():
return os.path.dirname(os.path.realpath(__file__))
2016-02-26 15:32:36 +01:00
def curr_time():
2016-03-13 13:06:06 +01:00
return time.strftime('%H:%M')
2016-07-07 12:54:02 +02:00
def copy(src, dest):
if not os.path.exists(dest):
os.makedirs(dest)
src_files = os.listdir(src)
for file_name in src_files:
full_file_name = os.path.join(src, file_name)
if os.path.isfile(full_file_name):
shutil.copy(full_file_name, dest)
else:
copy(full_file_name, os.path.join(dest, file_name))
2016-10-22 19:31:34 +02:00
def remove(folder):
2016-11-05 14:04:17 +01:00
if os.path.isdir(folder):
shutil.rmtree(folder)
2016-10-22 19:31:34 +02:00
2016-03-13 13:06:06 +01:00
def convert_time(t):
2017-01-21 22:19:56 +01:00
offset = time.timezone + time_offset() * 60
2016-08-30 19:23:55 +02:00
sec = int(t) - offset
2016-03-13 13:06:06 +01:00
m, s = divmod(sec, 60)
h, m = divmod(m, 60)
d, h = divmod(h, 24)
return '%02d:%02d' % (h, m)
2016-02-26 15:32:36 +01:00
2017-04-17 21:04:22 +02:00
@cached
2017-01-13 19:08:54 +01:00
def time_offset():
2017-01-21 22:19:56 +01:00
hours = int(time.strftime('%H'))
minutes = int(time.strftime('%M'))
2017-01-13 19:08:54 +01:00
sec = int(time.time()) - time.timezone
m, s = divmod(sec, 60)
h, m = divmod(m, 60)
d, h = divmod(h, 24)
2017-01-21 22:19:56 +01:00
result = hours * 60 + minutes - h * 60 - m
return result
2017-01-13 19:08:54 +01:00
2016-10-31 22:04:48 +01:00
def append_slash(s):
2016-10-31 22:24:37 +01:00
if len(s) and s[-1] not in ('\\', '/'):
2016-10-31 22:04:48 +01:00
s += '/'
return s
2017-06-11 21:58:11 +02:00
@cached
2016-10-31 22:04:48 +01:00
def is_64_bit():
return sys.maxsize > 2 ** 32
2017-02-12 21:15:33 +01:00
def is_re_valid(regex):
try:
re.compile(regex)
except re.error:
return False
else:
return True
2016-06-21 13:58:11 +02:00
class Singleton:
_instance = None
2016-06-22 13:35:22 +02:00
def __init__(self):
self.__class__._instance = self
2016-03-03 20:19:09 +01:00
@classmethod
def get_instance(cls):
return cls._instance