#! /usr/bin/env python3
-"""Move/rename an image
+"""Move/rename an image or a directory of images
This file is a part of XSetBg.
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)