photofloat/scanner/CachePath.py

91 lines
1.9 KiB
Python
Raw Normal View History

2011-05-06 00:37:15 +02:00
import os.path
from datetime import datetime
2011-05-23 11:25:45 +02:00
def message(category, text):
if message.level <= 0:
sep = " "
else:
sep = "--"
print("%s %s%s[%s]%s%s" % (
datetime.now().isoformat(),
max(0, message.level) * " |",
sep,
category,
max(1, (14 - len(category))) * " ",
text))
2011-05-23 11:25:45 +02:00
message.level = -1
2011-05-23 11:25:45 +02:00
def next_level():
message.level += 1
2011-05-23 11:25:45 +02:00
def back_level():
message.level -= 1
2011-05-06 00:37:15 +02:00
def set_cache_path_base(base):
trim_base.base = base
2011-05-06 00:37:15 +02:00
def untrim_base(path):
return os.path.join(trim_base.base, path)
2011-05-06 00:37:15 +02:00
def trim_base_custom(path, base):
if path.startswith(base):
path = path[len(base):]
if path.startswith('/'):
path = path[1:]
return path
2011-05-06 00:37:15 +02:00
def trim_base(path):
return trim_base_custom(path, trim_base.base)
2014-02-14 05:28:59 +01:00
def cache_base(path, filepath=False):
if len(path) == 0:
return "root"
elif filepath and len(path.split(os.sep)) < 2:
path = "root-" + path
path = trim_base(path).replace(
'/', '-').replace(
' ', '_').replace(
'(', '').replace(
'&', '').replace(
',', '').replace(
')', '').replace(
'#', '').replace(
'[', '').replace(
']', '').replace(
'"', '').replace(
"'", '').replace(
'_-_', '-').lower()
while path.find("--") != -1:
path = path.replace("--", "-")
while path.find("__") != -1:
path = path.replace("__", "_")
return path
2011-05-06 00:37:15 +02:00
def json_cache(path):
return cache_base(path) + ".json"
2011-05-06 00:37:15 +02:00
def image_cache(path, size, square=False):
if square:
suffix = str(size) + "s"
else:
suffix = str(size)
return cache_base(path, True) + "_" + suffix + ".jpg"
2014-02-14 05:28:59 +01:00
def video_cache(path):
return cache_base(path, True) + ".mp4"
2011-05-06 00:37:15 +02:00
def file_mtime(path):
return datetime.fromtimestamp(int(os.path.getmtime(path)))