From 8d05302c387169fa41f23b293c404bd3cb6561d6 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sat, 24 Feb 2024 18:21:27 +0300 Subject: [PATCH] Fix(py3): Fix encodings --- dump_db.py | 8 ++++++-- print-filename.py | 2 +- reload_db.py | 5 ++--- rescan_fs.py | 7 ++++++- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/dump_db.py b/dump_db.py index 470ff0a..ca3c6a7 100755 --- a/dump_db.py +++ b/dump_db.py @@ -6,11 +6,15 @@ This file is a part of XSetBg. """ import sys -from m_lib.defenc import default_encoding +from xsetbg_conf import xsetbg_conf from xsetbg_db import xsetbg_db if not xsetbg_db: sys.exit("Error: no database found") +fs_encoding = xsetbg_conf.get("images", "fs_encoding") for row in xsetbg_db.select(orderBy='-last_shown'): - print(row.id, row.last_shown, row.full_name.encode(default_encoding)) + sys.stdout.buffer.write( + ('%d %s %s\n' % (row.id, row.last_shown, row.full_name + )).encode(fs_encoding) + ) diff --git a/print-filename.py b/print-filename.py index d541397..e9f217b 100755 --- a/print-filename.py +++ b/print-filename.py @@ -58,4 +58,4 @@ index, output_encoding = get_args() filename = xsetbg_db.select('last_shown IS NOT NULL', orderBy='-last_shown')[index].full_name -print(filename.encode(output_encoding)) +sys.stdout.buffer.write(filename.encode(output_encoding) + b'\n') diff --git a/reload_db.py b/reload_db.py index ffa0013..c12607a 100755 --- a/reload_db.py +++ b/reload_db.py @@ -19,17 +19,16 @@ def convert_str(s): return int(float(s)) -dump_file = open(sys.argv[1], 'r') +dump_file = open(sys.argv[1], 'r', + encoding=xsetbg_conf.get("images", "fs_encoding")) xsetbg_db = recreate_db() count_new = count_old = count_updated = 0 -fs_encoding = xsetbg_conf.get("images", "fs_encoding") with SQLiteMassInsert() as txn: for line in dump_file: id, timestamp, filename = line.strip().split(None, 2) id = convert_str(id) timestamp = convert_str(timestamp) - filename = filename.decode(fs_encoding) try: if id: row = xsetbg_db.get(id) diff --git a/rescan_fs.py b/rescan_fs.py index 0abed44..346f92b 100755 --- a/rescan_fs.py +++ b/rescan_fs.py @@ -9,8 +9,11 @@ This file is a part of XSetBg. import os import subprocess + from sqlobject import SQLObjectNotFound from sqlobject.sqlbuilder import Insert, Update, Delete +from m_lib.defenc import default_encoding + from xsetbg_conf import xsetbg_dir, xsetbg_conf from xsetbg_db import recreate_db, SQLiteMassInsert @@ -58,7 +61,9 @@ with SQLiteMassInsert() as txn: # List images in all subdirectories for dirpath, dirs, files in os.walk(image_dir): for file in files: - full_name = os.path.join(dirpath, file).decode(fs_encoding) + if default_encoding != fs_encoding: + file = file.encode().decode(fs_encoding) + full_name = os.path.join(dirpath, file) try: row = xsetbg_db.byFull_name(full_name) except SQLObjectNotFound: -- 2.39.2