Merge pull request #223 from valyrie97/tmp_db
Add flag to offload database operations to a different locationpull/237/head
commit
c02d610cdb
|
|
@ -0,0 +1,8 @@
|
||||||
|
|
||||||
|
|
||||||
|
def preload(parser):
|
||||||
|
parser.add_argument(
|
||||||
|
"--image-browser-tmp-db",
|
||||||
|
action="store_true",
|
||||||
|
help="Copy database file to and from /tmp when transacting (workaround for filesystems sqlite does not support)"
|
||||||
|
)
|
||||||
|
|
@ -82,9 +82,9 @@ def check_image_browser_active_tabs():
|
||||||
last_default_tab = wib_db.get_last_default_tab()
|
last_default_tab = wib_db.get_last_default_tab()
|
||||||
if last_default_tab[0] == "Others":
|
if last_default_tab[0] == "Others":
|
||||||
# New tabs don't exist yet in image_browser_active_tabs, add them
|
# New tabs don't exist yet in image_browser_active_tabs, add them
|
||||||
conn, cursor = wib_db.transaction_begin()
|
with wib_db.transaction() as cursor:
|
||||||
wib_db.update_db_data(cursor, "last_default_tab", "Maintenance")
|
wib_db.update_db_data(cursor, "last_default_tab", "Maintenance")
|
||||||
wib_db.transaction_end(conn, cursor)
|
|
||||||
if hasattr(opts, "image_browser_active_tabs"):
|
if hasattr(opts, "image_browser_active_tabs"):
|
||||||
active_and_new_tabs = f"{opts.image_browser_active_tabs}, All, Maintenance"
|
active_and_new_tabs = f"{opts.image_browser_active_tabs}, All, Maintenance"
|
||||||
shared.opts.__setattr__("image_browser_active_tabs", active_and_new_tabs)
|
shared.opts.__setattr__("image_browser_active_tabs", active_and_new_tabs)
|
||||||
|
|
@ -457,87 +457,86 @@ def cache_exif(fileinfos):
|
||||||
cache_exif_start = time.time()
|
cache_exif_start = time.time()
|
||||||
new_exif = 0
|
new_exif = 0
|
||||||
new_aes = 0
|
new_aes = 0
|
||||||
conn, cursor = wib_db.transaction_begin()
|
with wib_db.transaction() as cursor:
|
||||||
for fi_info in fileinfos:
|
for fi_info in fileinfos:
|
||||||
if any(fi_info[0].endswith(ext) for ext in image_ext_list):
|
if any(fi_info[0].endswith(ext) for ext in image_ext_list):
|
||||||
found_exif = False
|
found_exif = False
|
||||||
found_aes = False
|
found_aes = False
|
||||||
if fi_info[0] in exif_cache:
|
if fi_info[0] in exif_cache:
|
||||||
found_exif = True
|
found_exif = True
|
||||||
if fi_info[0] in aes_cache:
|
if fi_info[0] in aes_cache:
|
||||||
found_aes = True
|
found_aes = True
|
||||||
if not found_exif or not found_aes:
|
if not found_exif or not found_aes:
|
||||||
exif_cache[fi_info[0]] = "0"
|
exif_cache[fi_info[0]] = "0"
|
||||||
aes_cache[fi_info[0]] = "0"
|
aes_cache[fi_info[0]] = "0"
|
||||||
try:
|
|
||||||
image = Image.open(fi_info[0])
|
|
||||||
(_, allExif, allExif_html) = modules.extras.run_pnginfo(image)
|
|
||||||
image.close()
|
|
||||||
except SyntaxError:
|
|
||||||
allExif = False
|
|
||||||
logger.warning(f"Extension and content don't match: {fi_info[0]}")
|
|
||||||
except UnidentifiedImageError as e:
|
|
||||||
allExif = False
|
|
||||||
logger.warning(f"UnidentifiedImageError: {e}")
|
|
||||||
except Image.DecompressionBombError as e:
|
|
||||||
allExif = False
|
|
||||||
logger.warning(f"DecompressionBombError: {e}: {fi_info[0]}")
|
|
||||||
except PermissionError as e:
|
|
||||||
allExif = False
|
|
||||||
logger.warning(f"PermissionError: {e}: {fi_info[0]}")
|
|
||||||
except FileNotFoundError as e:
|
|
||||||
allExif = False
|
|
||||||
logger.warning(f"FileNotFoundError: {e}: {fi_info[0]}")
|
|
||||||
except OSError as e:
|
|
||||||
if e.errno == 22:
|
|
||||||
logger.warning(f"Caught OSError with error code 22: {fi_info[0]}")
|
|
||||||
else:
|
|
||||||
raise
|
|
||||||
if allExif:
|
|
||||||
exif_cache[fi_info[0]] = allExif
|
|
||||||
wib_db.update_exif_data(conn, fi_info[0], allExif)
|
|
||||||
new_exif = new_exif + 1
|
|
||||||
|
|
||||||
m = re.search("(?:aesthetic_score:|Score:) (\d+.\d+)", allExif, flags=re.IGNORECASE)
|
|
||||||
if m:
|
|
||||||
aes_value = m.group(1)
|
|
||||||
else:
|
|
||||||
aes_value = "0"
|
|
||||||
aes_cache[fi_info[0]] = aes_value
|
|
||||||
wib_db.update_exif_data_by_key(conn, fi_info[0], "aesthetic_score", aes_value)
|
|
||||||
new_aes = new_aes + 1
|
|
||||||
else:
|
|
||||||
try:
|
try:
|
||||||
filename = os.path.splitext(fi_info[0])[0] + ".txt"
|
image = Image.open(fi_info[0])
|
||||||
geninfo = ""
|
(_, allExif, allExif_html) = modules.extras.run_pnginfo(image)
|
||||||
with open(filename) as f:
|
image.close()
|
||||||
for line in f:
|
except SyntaxError:
|
||||||
geninfo += line
|
allExif = False
|
||||||
exif_cache[fi_info[0]] = geninfo
|
logger.warning(f"Extension and content don't match: {fi_info[0]}")
|
||||||
wib_db.update_exif_data_by_key(conn, fi_info[0], geninfo)
|
except UnidentifiedImageError as e:
|
||||||
|
allExif = False
|
||||||
|
logger.warning(f"UnidentifiedImageError: {e}")
|
||||||
|
except Image.DecompressionBombError as e:
|
||||||
|
allExif = False
|
||||||
|
logger.warning(f"DecompressionBombError: {e}: {fi_info[0]}")
|
||||||
|
except PermissionError as e:
|
||||||
|
allExif = False
|
||||||
|
logger.warning(f"PermissionError: {e}: {fi_info[0]}")
|
||||||
|
except FileNotFoundError as e:
|
||||||
|
allExif = False
|
||||||
|
logger.warning(f"FileNotFoundError: {e}: {fi_info[0]}")
|
||||||
|
except OSError as e:
|
||||||
|
if e.errno == 22:
|
||||||
|
logger.warning(f"Caught OSError with error code 22: {fi_info[0]}")
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
if allExif:
|
||||||
|
exif_cache[fi_info[0]] = allExif
|
||||||
|
wib_db.update_exif_data(cursor, fi_info[0], allExif)
|
||||||
new_exif = new_exif + 1
|
new_exif = new_exif + 1
|
||||||
|
|
||||||
m = re.search("(?:aesthetic_score:|Score:) (\d+.\d+)", geninfo, flags=re.IGNORECASE)
|
m = re.search("(?:aesthetic_score:|Score:) (\d+.\d+)", allExif, flags=re.IGNORECASE)
|
||||||
if m:
|
if m:
|
||||||
aes_value = m.group(1)
|
aes_value = m.group(1)
|
||||||
else:
|
else:
|
||||||
aes_value = "0"
|
aes_value = "0"
|
||||||
aes_cache[fi_info[0]] = aes_value
|
aes_cache[fi_info[0]] = aes_value
|
||||||
wib_db.update_exif_data_by_key(conn, fi_info[0], "aesthetic_score", aes_value)
|
wib_db.update_exif_data_by_key(cursor, fi_info[0], "aesthetic_score", aes_value)
|
||||||
new_aes = new_aes + 1
|
new_aes = new_aes + 1
|
||||||
except Exception:
|
else:
|
||||||
logger.warning(f"cache_exif: No EXIF in image or txt file for {fi_info[0]}")
|
try:
|
||||||
# Saved with defaults to not scan it again next time
|
filename = os.path.splitext(fi_info[0])[0] + ".txt"
|
||||||
exif_cache[fi_info[0]] = "0"
|
geninfo = ""
|
||||||
allExif = "0"
|
with open(filename) as f:
|
||||||
wib_db.update_exif_data(conn, fi_info[0], allExif)
|
for line in f:
|
||||||
new_exif = new_exif + 1
|
geninfo += line
|
||||||
|
exif_cache[fi_info[0]] = geninfo
|
||||||
|
wib_db.update_exif_data_by_key(cursor, fi_info[0], geninfo)
|
||||||
|
new_exif = new_exif + 1
|
||||||
|
|
||||||
aes_value = "0"
|
m = re.search("(?:aesthetic_score:|Score:) (\d+.\d+)", geninfo, flags=re.IGNORECASE)
|
||||||
aes_cache[fi_info[0]] = aes_value
|
if m:
|
||||||
wib_db.update_exif_data_by_key(conn, fi_info[0], "aesthetic_score", aes_value)
|
aes_value = m.group(1)
|
||||||
new_aes = new_aes + 1
|
else:
|
||||||
wib_db.transaction_end(conn, cursor)
|
aes_value = "0"
|
||||||
|
aes_cache[fi_info[0]] = aes_value
|
||||||
|
wib_db.update_exif_data_by_key(cursor, fi_info[0], "aesthetic_score", aes_value)
|
||||||
|
new_aes = new_aes + 1
|
||||||
|
except Exception:
|
||||||
|
logger.warning(f"cache_exif: No EXIF in image or txt file for {fi_info[0]}")
|
||||||
|
# Saved with defaults to not scan it again next time
|
||||||
|
exif_cache[fi_info[0]] = "0"
|
||||||
|
allExif = "0"
|
||||||
|
wib_db.update_exif_data(cursor, fi_info[0], allExif)
|
||||||
|
new_exif = new_exif + 1
|
||||||
|
|
||||||
|
aes_value = "0"
|
||||||
|
aes_cache[fi_info[0]] = aes_value
|
||||||
|
wib_db.update_exif_data_by_key(cursor, fi_info[0], "aesthetic_score", aes_value)
|
||||||
|
new_aes = new_aes + 1
|
||||||
|
|
||||||
if yappi_do:
|
if yappi_do:
|
||||||
yappi.stop()
|
yappi.stop()
|
||||||
|
|
@ -573,9 +572,8 @@ def exif_rebuild(maint_wait):
|
||||||
def exif_delete_0(maint_wait):
|
def exif_delete_0(maint_wait):
|
||||||
global exif_cache, aes_cache
|
global exif_cache, aes_cache
|
||||||
if opts.image_browser_scan_exif:
|
if opts.image_browser_scan_exif:
|
||||||
conn, cursor = wib_db.transaction_begin()
|
with wib_db.transaction() as cursor:
|
||||||
wib_db.delete_exif_0(cursor)
|
wib_db.delete_exif_0(cursor)
|
||||||
wib_db.transaction_end(conn, cursor)
|
|
||||||
exif_cache = wib_db.load_exif_data(exif_cache)
|
exif_cache = wib_db.load_exif_data(exif_cache)
|
||||||
aes_cache = wib_db.load_aes_data(aes_cache)
|
aes_cache = wib_db.load_aes_data(aes_cache)
|
||||||
maint_last_msg = "Delete finished"
|
maint_last_msg = "Delete finished"
|
||||||
|
|
@ -594,17 +592,16 @@ def exif_update_dirs(maint_update_dirs_path_recorder, maint_update_dirs_exif_dat
|
||||||
maint_update_dirs_from = os.path.realpath(maint_update_dirs_from)
|
maint_update_dirs_from = os.path.realpath(maint_update_dirs_from)
|
||||||
maint_update_dirs_to = os.path.realpath(maint_update_dirs_to)
|
maint_update_dirs_to = os.path.realpath(maint_update_dirs_to)
|
||||||
rows = 0
|
rows = 0
|
||||||
conn, cursor = wib_db.transaction_begin()
|
with wib_db.transaction() as cursor:
|
||||||
if maint_update_dirs_path_recorder:
|
if maint_update_dirs_path_recorder:
|
||||||
wib_db.update_path_recorder_mult(cursor, maint_update_dirs_from, maint_update_dirs_to)
|
wib_db.update_path_recorder_mult(cursor, maint_update_dirs_from, maint_update_dirs_to)
|
||||||
rows = rows + cursor.rowcount
|
rows = rows + cursor.rowcount
|
||||||
if maint_update_dirs_exif_data:
|
if maint_update_dirs_exif_data:
|
||||||
wib_db.update_exif_data_mult(cursor, maint_update_dirs_from, maint_update_dirs_to)
|
wib_db.update_exif_data_mult(cursor, maint_update_dirs_from, maint_update_dirs_to)
|
||||||
rows = rows + cursor.rowcount
|
rows = rows + cursor.rowcount
|
||||||
if maint_update_dirs_ranking:
|
if maint_update_dirs_ranking:
|
||||||
wib_db.update_ranking_mult(cursor, maint_update_dirs_from, maint_update_dirs_to)
|
wib_db.update_ranking_mult(cursor, maint_update_dirs_from, maint_update_dirs_to)
|
||||||
rows = rows + cursor.rowcount
|
rows = rows + cursor.rowcount
|
||||||
wib_db.transaction_end(conn, cursor)
|
|
||||||
if rows == 0:
|
if rows == 0:
|
||||||
maint_last_msg = "No rows updated"
|
maint_last_msg = "No rows updated"
|
||||||
else:
|
else:
|
||||||
|
|
@ -614,9 +611,8 @@ def exif_update_dirs(maint_update_dirs_path_recorder, maint_update_dirs_exif_dat
|
||||||
|
|
||||||
def recreate_hash(maint_wait):
|
def recreate_hash(maint_wait):
|
||||||
version = str(db_version)
|
version = str(db_version)
|
||||||
conn, cursor = wib_db.transaction_begin()
|
with wib_db.transaction() as cursor:
|
||||||
wib_db.migrate_filehash(cursor, version)
|
wib_db.migrate_filehash(cursor, version)
|
||||||
wib_db.transaction_end(conn, cursor)
|
|
||||||
maint_last_msg = "Hashes recreated"
|
maint_last_msg = "Hashes recreated"
|
||||||
|
|
||||||
return maint_wait, maint_last_msg
|
return maint_wait, maint_last_msg
|
||||||
|
|
@ -632,33 +628,31 @@ def reapply_ranking(path_recorder, maint_wait):
|
||||||
if os.path.exists(key):
|
if os.path.exists(key):
|
||||||
dirs[key] = key
|
dirs[key] = key
|
||||||
|
|
||||||
conn, cursor = wib_db.transaction_begin()
|
with wib_db.transaction() as cursor:
|
||||||
|
# Traverse all known dirs, check if missing rankings are due to moved files
|
||||||
|
for key in dirs.keys():
|
||||||
|
fileinfos = traverse_all_files(key, [], "", 0)
|
||||||
|
for (file, _) in fileinfos:
|
||||||
|
# Is there a ranking for this full filepath
|
||||||
|
ranking_by_file = wib_db.get_ranking_by_file(cursor, file)
|
||||||
|
if ranking_by_file is None:
|
||||||
|
name = os.path.basename(file)
|
||||||
|
(ranking_by_name, alternate_hash) = wib_db.get_ranking_by_name(cursor, name)
|
||||||
|
# Is there a ranking only for the filename
|
||||||
|
if ranking_by_name is not None:
|
||||||
|
hash = wib_db.get_hash(file)
|
||||||
|
(alternate_file, alternate_ranking) = ranking_by_name
|
||||||
|
if alternate_ranking is not None and alternate_hash is not None:
|
||||||
|
(alternate_hash,) = alternate_hash
|
||||||
|
# Does the found filename's file have no hash or the same hash?
|
||||||
|
if alternate_hash is None or hash == alternate_hash:
|
||||||
|
if os.path.exists(alternate_file):
|
||||||
|
# Insert ranking as a copy of the found filename's ranking
|
||||||
|
wib_db.insert_ranking(cursor, file, alternate_ranking, hash)
|
||||||
|
else:
|
||||||
|
# Replace ranking of the found filename
|
||||||
|
wib_db.replace_ranking(cursor, file, alternate_file, hash)
|
||||||
|
|
||||||
# Traverse all known dirs, check if missing rankings are due to moved files
|
|
||||||
for key in dirs.keys():
|
|
||||||
fileinfos = traverse_all_files(key, [], "", 0)
|
|
||||||
for (file, _) in fileinfos:
|
|
||||||
# Is there a ranking for this full filepath
|
|
||||||
ranking_by_file = wib_db.get_ranking_by_file(cursor, file)
|
|
||||||
if ranking_by_file is None:
|
|
||||||
name = os.path.basename(file)
|
|
||||||
(ranking_by_name, alternate_hash) = wib_db.get_ranking_by_name(cursor, name)
|
|
||||||
# Is there a ranking only for the filename
|
|
||||||
if ranking_by_name is not None:
|
|
||||||
hash = wib_db.get_hash(file)
|
|
||||||
(alternate_file, alternate_ranking) = ranking_by_name
|
|
||||||
if alternate_ranking is not None and alternate_hash is not None:
|
|
||||||
(alternate_hash,) = alternate_hash
|
|
||||||
# Does the found filename's file have no hash or the same hash?
|
|
||||||
if alternate_hash is None or hash == alternate_hash:
|
|
||||||
if os.path.exists(alternate_file):
|
|
||||||
# Insert ranking as a copy of the found filename's ranking
|
|
||||||
wib_db.insert_ranking(cursor, file, alternate_ranking, hash)
|
|
||||||
else:
|
|
||||||
# Replace ranking of the found filename
|
|
||||||
wib_db.replace_ranking(cursor, file, alternate_file, hash)
|
|
||||||
|
|
||||||
wib_db.transaction_end(conn, cursor)
|
|
||||||
maint_last_msg = "Rankings reapplied"
|
maint_last_msg = "Rankings reapplied"
|
||||||
|
|
||||||
return maint_wait, maint_last_msg
|
return maint_wait, maint_last_msg
|
||||||
|
|
@ -743,73 +737,71 @@ def get_all_images(dir_name, sort_by, sort_order, keyword, tab_base_tag_box, img
|
||||||
filenames = [finfo[0] for finfo in fileinfos]
|
filenames = [finfo[0] for finfo in fileinfos]
|
||||||
|
|
||||||
if opts.image_browser_scan_exif:
|
if opts.image_browser_scan_exif:
|
||||||
conn, cursor = wib_db.transaction_begin()
|
with wib_db.transaction() as cursor:
|
||||||
if len(exif_keyword) != 0:
|
if len(exif_keyword) != 0:
|
||||||
if use_regex:
|
if use_regex:
|
||||||
regex_error = False
|
regex_error = False
|
||||||
|
try:
|
||||||
|
test_re = re.compile(exif_keyword, re.DOTALL)
|
||||||
|
except re.error as e:
|
||||||
|
regex_error = True
|
||||||
|
print(f"Regex error: {e}")
|
||||||
|
if (use_regex and not regex_error) or not use_regex:
|
||||||
|
if negative_prompt_search == "Yes":
|
||||||
|
fileinfos = [x for x in fileinfos if exif_search(exif_keyword, exif_cache[x[0]], use_regex, case_sensitive)]
|
||||||
|
else:
|
||||||
|
result = []
|
||||||
|
for file_info in fileinfos:
|
||||||
|
file_name = file_info[0]
|
||||||
|
file_exif = exif_cache[file_name]
|
||||||
|
file_exif_lc = file_exif.lower()
|
||||||
|
start_index = file_exif_lc.find(np)
|
||||||
|
end_index = file_exif.find("\n", start_index)
|
||||||
|
if negative_prompt_search == "Only":
|
||||||
|
start_index = start_index + len(np)
|
||||||
|
sub_string = file_exif[start_index:end_index].strip()
|
||||||
|
if exif_search(exif_keyword, sub_string, use_regex, case_sensitive):
|
||||||
|
result.append(file_info)
|
||||||
|
else:
|
||||||
|
sub_string = file_exif[start_index:end_index].strip()
|
||||||
|
file_exif = file_exif.replace(sub_string, "")
|
||||||
|
|
||||||
|
if exif_search(exif_keyword, file_exif, use_regex, case_sensitive):
|
||||||
|
result.append(file_info)
|
||||||
|
fileinfos = result
|
||||||
|
filenames = [finfo[0] for finfo in fileinfos]
|
||||||
|
wib_db.fill_work_files(cursor, fileinfos)
|
||||||
|
if len(aes_filter_min) != 0 or len(aes_filter_max) != 0:
|
||||||
try:
|
try:
|
||||||
test_re = re.compile(exif_keyword, re.DOTALL)
|
aes_filter_min_num = float(aes_filter_min)
|
||||||
except re.error as e:
|
except ValueError:
|
||||||
regex_error = True
|
aes_filter_min_num = sys.float_info.min
|
||||||
print(f"Regex error: {e}")
|
try:
|
||||||
if (use_regex and not regex_error) or not use_regex:
|
aes_filter_max_num = float(aes_filter_max)
|
||||||
if negative_prompt_search == "Yes":
|
except ValueError:
|
||||||
fileinfos = [x for x in fileinfos if exif_search(exif_keyword, exif_cache[x[0]], use_regex, case_sensitive)]
|
aes_filter_max_num = sys.float_info.max
|
||||||
else:
|
|
||||||
result = []
|
fileinfos = wib_db.filter_aes(cursor, fileinfos, aes_filter_min_num, aes_filter_max_num)
|
||||||
for file_info in fileinfos:
|
filenames = [finfo[0] for finfo in fileinfos]
|
||||||
file_name = file_info[0]
|
if ranking_filter != "All":
|
||||||
file_exif = exif_cache[file_name]
|
ranking_filter_min_num = 1
|
||||||
file_exif_lc = file_exif.lower()
|
ranking_filter_max_num = 5
|
||||||
start_index = file_exif_lc.find(np)
|
if ranking_filter == "Min-max":
|
||||||
end_index = file_exif.find("\n", start_index)
|
try:
|
||||||
if negative_prompt_search == "Only":
|
ranking_filter_min_num = int(ranking_filter_min)
|
||||||
start_index = start_index + len(np)
|
except ValueError:
|
||||||
sub_string = file_exif[start_index:end_index].strip()
|
ranking_filter_min_num = 0
|
||||||
if exif_search(exif_keyword, sub_string, use_regex, case_sensitive):
|
try:
|
||||||
result.append(file_info)
|
ranking_filter_max_num = int(ranking_filter_max)
|
||||||
else:
|
except ValueError:
|
||||||
sub_string = file_exif[start_index:end_index].strip()
|
ranking_filter_max_num = 0
|
||||||
file_exif = file_exif.replace(sub_string, "")
|
if ranking_filter_min_num < 1:
|
||||||
|
ranking_filter_min_num = 1
|
||||||
if exif_search(exif_keyword, file_exif, use_regex, case_sensitive):
|
if ranking_filter_max_num < 1 or ranking_filter_max_num > 5:
|
||||||
result.append(file_info)
|
ranking_filter_max_num = 5
|
||||||
fileinfos = result
|
|
||||||
|
fileinfos = wib_db.filter_ranking(cursor, fileinfos, ranking_filter, ranking_filter_min_num, ranking_filter_max_num)
|
||||||
filenames = [finfo[0] for finfo in fileinfos]
|
filenames = [finfo[0] for finfo in fileinfos]
|
||||||
wib_db.fill_work_files(cursor, fileinfos)
|
|
||||||
if len(aes_filter_min) != 0 or len(aes_filter_max) != 0:
|
|
||||||
try:
|
|
||||||
aes_filter_min_num = float(aes_filter_min)
|
|
||||||
except ValueError:
|
|
||||||
aes_filter_min_num = sys.float_info.min
|
|
||||||
try:
|
|
||||||
aes_filter_max_num = float(aes_filter_max)
|
|
||||||
except ValueError:
|
|
||||||
aes_filter_max_num = sys.float_info.max
|
|
||||||
|
|
||||||
fileinfos = wib_db.filter_aes(cursor, fileinfos, aes_filter_min_num, aes_filter_max_num)
|
|
||||||
filenames = [finfo[0] for finfo in fileinfos]
|
|
||||||
if ranking_filter != "All":
|
|
||||||
ranking_filter_min_num = 1
|
|
||||||
ranking_filter_max_num = 5
|
|
||||||
if ranking_filter == "Min-max":
|
|
||||||
try:
|
|
||||||
ranking_filter_min_num = int(ranking_filter_min)
|
|
||||||
except ValueError:
|
|
||||||
ranking_filter_min_num = 0
|
|
||||||
try:
|
|
||||||
ranking_filter_max_num = int(ranking_filter_max)
|
|
||||||
except ValueError:
|
|
||||||
ranking_filter_max_num = 0
|
|
||||||
if ranking_filter_min_num < 1:
|
|
||||||
ranking_filter_min_num = 1
|
|
||||||
if ranking_filter_max_num < 1 or ranking_filter_max_num > 5:
|
|
||||||
ranking_filter_max_num = 5
|
|
||||||
|
|
||||||
fileinfos = wib_db.filter_ranking(cursor, fileinfos, ranking_filter, ranking_filter_min_num, ranking_filter_max_num)
|
|
||||||
filenames = [finfo[0] for finfo in fileinfos]
|
|
||||||
|
|
||||||
wib_db.transaction_end(conn, cursor)
|
|
||||||
|
|
||||||
if sort_by == "date":
|
if sort_by == "date":
|
||||||
if sort_order == up_symbol:
|
if sort_order == up_symbol:
|
||||||
|
|
@ -905,11 +897,11 @@ def get_image_thumbnail(image_list):
|
||||||
|
|
||||||
def set_tooltip_info(image_list):
|
def set_tooltip_info(image_list):
|
||||||
image_browser_img_info = {}
|
image_browser_img_info = {}
|
||||||
conn, cursor = wib_db.transaction_begin()
|
with wib_db.transaction() as cursor:
|
||||||
for filename in image_list:
|
for filename in image_list:
|
||||||
x, y = wib_db.select_x_y(cursor, filename)
|
x, y = wib_db.select_x_y(cursor, filename)
|
||||||
image_browser_img_info[filename] = {"x": x, "y": y}
|
image_browser_img_info[filename] = {"x": x, "y": y}
|
||||||
wib_db.transaction_end(conn, cursor)
|
|
||||||
image_browser_img_info_json = json.dumps(image_browser_img_info)
|
image_browser_img_info_json = json.dumps(image_browser_img_info)
|
||||||
return image_browser_img_info_json
|
return image_browser_img_info_json
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,11 @@ import hashlib
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from modules import scripts
|
from shutil import copy2
|
||||||
|
from modules import scripts, shared
|
||||||
|
from tempfile import gettempdir
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
version = 7
|
version = 7
|
||||||
|
|
||||||
|
|
@ -12,11 +15,38 @@ aes_cache_file = os.path.join(scripts.basedir(), "aes_scores.json")
|
||||||
exif_cache_file = os.path.join(scripts.basedir(), "exif_data.json")
|
exif_cache_file = os.path.join(scripts.basedir(), "exif_data.json")
|
||||||
ranking_file = os.path.join(scripts.basedir(), "ranking.json")
|
ranking_file = os.path.join(scripts.basedir(), "ranking.json")
|
||||||
archive = os.path.join(scripts.basedir(), "archive")
|
archive = os.path.join(scripts.basedir(), "archive")
|
||||||
db_file = os.path.join(scripts.basedir(), "wib.sqlite3")
|
source_db_file = os.path.join(scripts.basedir(), "wib.sqlite3")
|
||||||
|
tmp_db_file = os.path.join(gettempdir(), "sd-images-browser.sqlite3")
|
||||||
|
|
||||||
|
db_file = source_db_file
|
||||||
|
if getattr(shared.cmd_opts, "image_browser_tmp_db", False):
|
||||||
|
db_file = tmp_db_file
|
||||||
|
if os.path.exists(source_db_file):
|
||||||
|
copy2(source_db_file, tmp_db_file)
|
||||||
|
elif os.path.exists(tmp_db_file):
|
||||||
|
os.remove(tmp_db_file)
|
||||||
|
|
||||||
|
def backup_tmp_db():
|
||||||
|
if(db_file == tmp_db_file):
|
||||||
|
copy2(tmp_db_file, source_db_file)
|
||||||
|
|
||||||
np = "Negative prompt: "
|
np = "Negative prompt: "
|
||||||
st = "Steps: "
|
st = "Steps: "
|
||||||
timeout = 30
|
timeout = 30
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def transaction(db = db_file):
|
||||||
|
conn = sqlite3.connect(db, timeout=timeout)
|
||||||
|
try:
|
||||||
|
conn.isolation_level = None
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute("BEGIN")
|
||||||
|
yield cursor
|
||||||
|
cursor.execute("COMMIT")
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
backup_tmp_db()
|
||||||
|
|
||||||
def create_filehash(cursor):
|
def create_filehash(cursor):
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
CREATE TABLE IF NOT EXISTS filehash (
|
CREATE TABLE IF NOT EXISTS filehash (
|
||||||
|
|
@ -292,8 +322,7 @@ def update_db_data(cursor, key, value):
|
||||||
return
|
return
|
||||||
|
|
||||||
def get_version():
|
def get_version():
|
||||||
with sqlite3.connect(db_file, timeout=timeout) as conn:
|
with transaction() as cursor:
|
||||||
cursor = conn.cursor()
|
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
SELECT value
|
SELECT value
|
||||||
FROM db_data
|
FROM db_data
|
||||||
|
|
@ -304,8 +333,7 @@ def get_version():
|
||||||
return db_version
|
return db_version
|
||||||
|
|
||||||
def get_last_default_tab():
|
def get_last_default_tab():
|
||||||
with sqlite3.connect(db_file, timeout=timeout) as conn:
|
with transaction() as cursor:
|
||||||
cursor = conn.cursor()
|
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
SELECT value
|
SELECT value
|
||||||
FROM db_data
|
FROM db_data
|
||||||
|
|
@ -424,42 +452,40 @@ def migrate_ranking_dirs(cursor, db_version):
|
||||||
|
|
||||||
def check():
|
def check():
|
||||||
if not os.path.exists(db_file):
|
if not os.path.exists(db_file):
|
||||||
conn, cursor = transaction_begin()
|
|
||||||
print("Image Browser: Creating database")
|
print("Image Browser: Creating database")
|
||||||
create_db(cursor)
|
with transaction() as cursor:
|
||||||
update_db_data(cursor, "version", version)
|
create_db(cursor)
|
||||||
update_db_data(cursor, "last_default_tab", "Maintenance")
|
update_db_data(cursor, "version", version)
|
||||||
migrate_path_recorder(cursor)
|
update_db_data(cursor, "last_default_tab", "Maintenance")
|
||||||
migrate_exif_data(cursor)
|
migrate_path_recorder(cursor)
|
||||||
migrate_ranking(cursor)
|
migrate_exif_data(cursor)
|
||||||
migrate_filehash(cursor, str(version))
|
migrate_ranking(cursor)
|
||||||
transaction_end(conn, cursor)
|
migrate_filehash(cursor, str(version))
|
||||||
print("Image Browser: Database created")
|
print("Image Browser: Database created")
|
||||||
db_version = get_version()
|
db_version = get_version()
|
||||||
conn, cursor = transaction_begin()
|
|
||||||
if db_version[0] <= "2":
|
with transaction() as cursor:
|
||||||
# version 1 database had mixed path notations, changed them all to abspath
|
if db_version[0] <= "2":
|
||||||
# version 2 database still had mixed path notations, because of windows short name, changed them all to realpath
|
# version 1 database had mixed path notations, changed them all to abspath
|
||||||
print(f"Image Browser: Upgrading database from version {db_version[0]} to version {version}")
|
# version 2 database still had mixed path notations, because of windows short name, changed them all to realpath
|
||||||
migrate_path_recorder_dirs(cursor)
|
print(f"Image Browser: Upgrading database from version {db_version[0]} to version {version}")
|
||||||
migrate_exif_data_dirs(cursor)
|
migrate_path_recorder_dirs(cursor)
|
||||||
migrate_ranking_dirs(cursor, db_version[0])
|
migrate_exif_data_dirs(cursor)
|
||||||
if db_version[0] <= "4":
|
migrate_ranking_dirs(cursor, db_version[0])
|
||||||
migrate_filehash(cursor, db_version[0])
|
if db_version[0] <= "4":
|
||||||
if db_version[0] <= "5":
|
migrate_filehash(cursor, db_version[0])
|
||||||
migrate_work_files(cursor)
|
if db_version[0] <= "5":
|
||||||
if db_version[0] <= "6":
|
migrate_work_files(cursor)
|
||||||
update_db_data(cursor, "last_default_tab", "Others")
|
if db_version[0] <= "6":
|
||||||
|
update_db_data(cursor, "last_default_tab", "Others")
|
||||||
|
|
||||||
update_db_data(cursor, "version", version)
|
update_db_data(cursor, "version", version)
|
||||||
print(f"Image Browser: Database upgraded from version {db_version[0]} to version {version}")
|
print(f"Image Browser: Database upgraded from version {db_version[0]} to version {version}")
|
||||||
transaction_end(conn, cursor)
|
|
||||||
|
|
||||||
return version
|
return version
|
||||||
|
|
||||||
def load_path_recorder():
|
def load_path_recorder():
|
||||||
with sqlite3.connect(db_file, timeout=timeout) as conn:
|
with transaction() as cursor:
|
||||||
cursor = conn.cursor()
|
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
SELECT path, depth, path_display
|
SELECT path, depth, path_display
|
||||||
FROM path_recorder
|
FROM path_recorder
|
||||||
|
|
@ -469,8 +495,7 @@ def load_path_recorder():
|
||||||
return path_recorder
|
return path_recorder
|
||||||
|
|
||||||
def select_ranking(file):
|
def select_ranking(file):
|
||||||
with sqlite3.connect(db_file, timeout=timeout) as conn:
|
with transaction() as cursor:
|
||||||
cursor = conn.cursor()
|
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
SELECT ranking
|
SELECT ranking
|
||||||
FROM ranking
|
FROM ranking
|
||||||
|
|
@ -487,8 +512,7 @@ def select_ranking(file):
|
||||||
|
|
||||||
def update_ranking(file, ranking):
|
def update_ranking(file, ranking):
|
||||||
name = os.path.basename(file)
|
name = os.path.basename(file)
|
||||||
with sqlite3.connect(db_file, timeout=timeout) as conn:
|
with transaction() as cursor:
|
||||||
cursor = conn.cursor()
|
|
||||||
if ranking == "None":
|
if ranking == "None":
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
DELETE FROM ranking
|
DELETE FROM ranking
|
||||||
|
|
@ -511,8 +535,7 @@ def update_ranking(file, ranking):
|
||||||
return
|
return
|
||||||
|
|
||||||
def update_path_recorder(path, depth, path_display):
|
def update_path_recorder(path, depth, path_display):
|
||||||
with sqlite3.connect(db_file, timeout=timeout) as conn:
|
with transaction() as cursor:
|
||||||
cursor = conn.cursor()
|
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
INSERT OR REPLACE
|
INSERT OR REPLACE
|
||||||
INTO path_recorder (path, depth, path_display)
|
INTO path_recorder (path, depth, path_display)
|
||||||
|
|
@ -522,8 +545,7 @@ def update_path_recorder(path, depth, path_display):
|
||||||
return
|
return
|
||||||
|
|
||||||
def update_path_recorder(path, depth, path_display):
|
def update_path_recorder(path, depth, path_display):
|
||||||
with sqlite3.connect(db_file, timeout=timeout) as conn:
|
with transaction() as cursor:
|
||||||
cursor = conn.cursor()
|
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
INSERT OR REPLACE
|
INSERT OR REPLACE
|
||||||
INTO path_recorder (path, depth, path_display)
|
INTO path_recorder (path, depth, path_display)
|
||||||
|
|
@ -533,8 +555,7 @@ def update_path_recorder(path, depth, path_display):
|
||||||
return
|
return
|
||||||
|
|
||||||
def delete_path_recorder(path):
|
def delete_path_recorder(path):
|
||||||
with sqlite3.connect(db_file, timeout=timeout) as conn:
|
with transaction() as cursor:
|
||||||
cursor = conn.cursor()
|
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
DELETE FROM path_recorder
|
DELETE FROM path_recorder
|
||||||
WHERE path = ?
|
WHERE path = ?
|
||||||
|
|
@ -649,18 +670,6 @@ def replace_ranking(cursor, file, alternate_file, hash):
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def transaction_begin():
|
|
||||||
conn = sqlite3.connect(db_file, timeout=timeout)
|
|
||||||
conn.isolation_level = None
|
|
||||||
cursor = conn.cursor()
|
|
||||||
cursor.execute("BEGIN")
|
|
||||||
return conn, cursor
|
|
||||||
|
|
||||||
def transaction_end(conn, cursor):
|
|
||||||
cursor.execute("COMMIT")
|
|
||||||
conn.close()
|
|
||||||
return
|
|
||||||
|
|
||||||
def update_exif_data_by_key(cursor, file, key, value):
|
def update_exif_data_by_key(cursor, file, key, value):
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
INSERT OR REPLACE
|
INSERT OR REPLACE
|
||||||
|
|
@ -671,8 +680,7 @@ def update_exif_data_by_key(cursor, file, key, value):
|
||||||
return
|
return
|
||||||
|
|
||||||
def select_prompts(file):
|
def select_prompts(file):
|
||||||
with sqlite3.connect(db_file, timeout=timeout) as conn:
|
with transaction() as cursor:
|
||||||
cursor = conn.cursor()
|
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
SELECT key, value
|
SELECT key, value
|
||||||
FROM exif_data
|
FROM exif_data
|
||||||
|
|
@ -693,8 +701,7 @@ def select_prompts(file):
|
||||||
return prompt, neg_prompt
|
return prompt, neg_prompt
|
||||||
|
|
||||||
def load_exif_data(exif_cache):
|
def load_exif_data(exif_cache):
|
||||||
with sqlite3.connect(db_file, timeout=timeout) as conn:
|
with transaction() as cursor:
|
||||||
cursor = conn.cursor()
|
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
SELECT file, group_concat(
|
SELECT file, group_concat(
|
||||||
case when key = 'prompt' or key = 'negative_prompt' then key || ': ' || value || '\n'
|
case when key = 'prompt' or key = 'negative_prompt' then key || ': ' || value || '\n'
|
||||||
|
|
@ -719,8 +726,7 @@ def load_exif_data(exif_cache):
|
||||||
return exif_cache
|
return exif_cache
|
||||||
|
|
||||||
def load_exif_data_by_key(cache, key1, key2):
|
def load_exif_data_by_key(cache, key1, key2):
|
||||||
with sqlite3.connect(db_file, timeout=timeout) as conn:
|
with transaction() as cursor:
|
||||||
cursor = conn.cursor()
|
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
SELECT file, value
|
SELECT file, value
|
||||||
FROM exif_data
|
FROM exif_data
|
||||||
|
|
@ -734,8 +740,7 @@ def load_exif_data_by_key(cache, key1, key2):
|
||||||
return cache
|
return cache
|
||||||
|
|
||||||
def get_exif_dirs():
|
def get_exif_dirs():
|
||||||
with sqlite3.connect(db_file, timeout=timeout) as conn:
|
with transaction() as cursor:
|
||||||
cursor = conn.cursor()
|
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
SELECT file
|
SELECT file
|
||||||
FROM exif_data
|
FROM exif_data
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue