]> git.phdru.name Git - xsetbg.git/commitdiff
Feat(mv): Move/rename a directory of images master
authorOleg Broytman <phd@phdru.name>
Wed, 20 Aug 2025 18:14:01 +0000 (21:14 +0300)
committerOleg Broytman <phd@phdru.name>
Wed, 20 Aug 2025 18:14:01 +0000 (21:14 +0300)
mv.py

diff --git a/mv.py b/mv.py
index 9adf21f305a4ceca4433564b79d87538715a43a7..19e69b7fb28b5db171bf2a0a6a0d034fd903af63 100755 (executable)
--- a/mv.py
+++ b/mv.py
@@ -1,5 +1,5 @@
 #! /usr/bin/env python3
-"""Move/rename an image
+"""Move/rename an image or a directory of images
 
 This file is a part of XSetBg.
 
@@ -40,12 +40,30 @@ connection = xsetbg_db._connection
 txn = connection.transaction()
 xsetbg_db._connection = txn
 
-rows = list(xsetbg_db.selectBy(full_name=from_filename))
-if not rows:
-    sys.exit("No rows by filename %s" % from_filename)
+if os.path.isdir(from_filename):  # Rename a directory
+    rows = list(xsetbg_db.select(
+        LIKE(xsetbg_db.q.full_name, from_filename + '/%')
+    ))
+    if not rows:
+        sys.exit("No rows in directory %s" % from_filename)
 
-row = rows[0]
-row.full_name = to_filename
+    for row in rows:
+        row.full_name = os.path.join(
+            to_filename,
+            # Remove separator to make the path relative
+            row.full_name[len(from_filename) + 1:])
+
+    dest_dir = os.path.dirname(to_filename)
+    if not os.path.exists(dest_dir):
+        os.makedirs(dest_dir)
+
+else:  # Rename just 1 file
+    rows = list(xsetbg_db.selectBy(full_name=from_filename))
+    if not rows:
+        sys.exit("No rows by filename %s" % from_filename)
+
+    row = rows[0]
+    row.full_name = to_filename
 
 os.rename(from_filename, to_filename)
-txn.commit()
+txn.commit(close=True)