Moved time and append functions to yandereBot.py
This commit is contained in:
parent
24747be4ee
commit
521087cbe8
@ -162,101 +162,3 @@ def get_hash_list_blacklist(v_pictures, v_blacklist, max_size=None):
|
|||||||
if not is_hash_blacklisted(lineHash, ret, v_blacklist, max_size):
|
if not is_hash_blacklisted(lineHash, ret, v_blacklist, max_size):
|
||||||
ret.append(lineHash)
|
ret.append(lineHash)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------- TIME FUNCTIONS ---------------------------------------------
|
|
||||||
def time_add_seconds(dt, seconds):
|
|
||||||
return dt + datetime.timedelta(0, seconds)
|
|
||||||
|
|
||||||
|
|
||||||
def time_diff_seconds(d1, d2):
|
|
||||||
return (d1-d2).total_seconds()
|
|
||||||
|
|
||||||
|
|
||||||
def input_time_format(s):
|
|
||||||
return s.replace("-", "")
|
|
||||||
|
|
||||||
|
|
||||||
def humanize_time_delta(total_seconds):
|
|
||||||
m, s = divmod(int(total_seconds), 60)
|
|
||||||
h, m = divmod(m, 60)
|
|
||||||
d, h = divmod(h, 24)
|
|
||||||
return d, h, m, s
|
|
||||||
|
|
||||||
|
|
||||||
# -------------------------------- Pretty Print Functions ------------------------------------
|
|
||||||
# If value is a subclass of string, return it in parentheses (used for generating a python configuration file)
|
|
||||||
def _pp_str(v):
|
|
||||||
if issubclass(str, type(v)):
|
|
||||||
return '"{}"'.format(v)
|
|
||||||
else:
|
|
||||||
return "{}".format(v)
|
|
||||||
|
|
||||||
|
|
||||||
# Prints out a dictionary in pretty print form as valid python code.
|
|
||||||
# setting_dict should consist of string formats with key value pairs defined in setting_dict ex. '{key} {value}'.
|
|
||||||
def _pp_dict(head, setting_dict, fmt, tail):
|
|
||||||
if head:
|
|
||||||
print(head)
|
|
||||||
last_index = len(setting_dict) - 1
|
|
||||||
# i, k -> index and key value of setting_dict
|
|
||||||
for i, k in enumerate(setting_dict):
|
|
||||||
_fmt = fmt
|
|
||||||
_k = _pp_str(k)
|
|
||||||
_v = _pp_str(setting_dict[k])
|
|
||||||
# Add comma separator on all but the last value setting
|
|
||||||
if i < last_index:
|
|
||||||
_fmt += ","
|
|
||||||
print(_fmt.format(_k, _v))
|
|
||||||
if tail:
|
|
||||||
print(tail)
|
|
||||||
|
|
||||||
|
|
||||||
def pp_ordered_dict(setting, setting_dict):
|
|
||||||
_pp_dict(
|
|
||||||
"{} = OrderedDict([".format(setting),
|
|
||||||
setting_dict,
|
|
||||||
'\t({},\t{})',
|
|
||||||
"])")
|
|
||||||
|
|
||||||
|
|
||||||
def pp_dict(setting, setting_dict):
|
|
||||||
_pp_dict(
|
|
||||||
"{} = {{".format(setting),
|
|
||||||
setting_dict,
|
|
||||||
'\t{}:\t{}',
|
|
||||||
"}")
|
|
||||||
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------------------------------
|
|
||||||
def sync_to_disk(file_handle):
|
|
||||||
file_handle.flush()
|
|
||||||
os.fsync(file_handle.fileno())
|
|
||||||
|
|
||||||
|
|
||||||
def append_file(f_name, s, atomic_saving=False, sync_save=False, tmp_dir="/var/tmp"):
|
|
||||||
file_handle = None
|
|
||||||
if atomic_saving:
|
|
||||||
import tempfile
|
|
||||||
file_handle = tempfile.NamedTemporaryFile(dir=tmp_dir)
|
|
||||||
with contextlib.suppress(IOError):
|
|
||||||
with open(f_name, "rb") as src:
|
|
||||||
shutil.copyfileobj(src, file_handle)
|
|
||||||
if sync_save:
|
|
||||||
sync_to_disk(file_handle)
|
|
||||||
else:
|
|
||||||
file_handle = open(f_name, "ab")
|
|
||||||
|
|
||||||
file_handle.write(str.encode(s + os.linesep))
|
|
||||||
|
|
||||||
if sync_save:
|
|
||||||
sync_to_disk(file_handle)
|
|
||||||
|
|
||||||
if atomic_saving:
|
|
||||||
file_handle.seek(0)
|
|
||||||
with open(f_name, "wb") as dst_file:
|
|
||||||
shutil.copyfileobj(file_handle, dst_file)
|
|
||||||
if sync_save:
|
|
||||||
sync_to_disk(dst_file)
|
|
||||||
|
|
||||||
file_handle.close()
|
|
||||||
|
Reference in New Issue
Block a user