date order

This commit is contained in:
Q
2024-08-10 12:35:50 +03:00
parent e773196ad7
commit f82e4d9913

View File

@@ -1,10 +1,14 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os, sys import math
import math, shutil, re import os
import re
import shutil
import sys
from datetime import datetime
from random import shuffle from random import shuffle
VERSION = "0.2" VERSION = "0.3"
def setup_options(): def setup_options():
@@ -21,7 +25,7 @@ def setup_options():
dest="order", dest="order",
default="sequence", default="sequence",
help="Splitting method.", help="Splitting method.",
choices=["sequence", "sparse", "regexp", "random"], choices=["sequence", "sparse", "regexp", "random", "date"],
) )
parser.add_argument( parser.add_argument(
"-m", "-m",
@@ -53,6 +57,15 @@ def setup_options():
default="", default="",
help="Regular expression for splitting. When set, order regexp used, -n or -i not used.", help="Regular expression for splitting. When set, order regexp used, -n or -i not used.",
) )
parser.add_argument(
"-d",
"--datefmt",
type=str,
action="store",
dest="datefmt",
default=None,
help="Date format for 'date' split. Defaults to %Y-%m-%d",
)
parser.add_argument( parser.add_argument(
"-n", "-n",
"-N", "-N",
@@ -81,15 +94,26 @@ def setup_options():
help="Folder to split.", help="Folder to split.",
) )
options = parser.parse_args() options = parser.parse_args()
if options.n is None and options.i is None and options.regexp == "":
parser.print_help()
parser.error("Either -n, -i or -r must be passed")
if options.regexp != "": if options.regexp != "":
options.order = "regexp" options.order = "regexp"
if options.regexp == "": if options.datefmt is not None:
options.order = "date"
if options.order in ("sequence", "sparse", "random"):
if options.n is None and options.i is None:
parser.print_help()
parser.error("Either -n or -i must be used")
if not options.i is None and not options.n is None: if not options.i is None and not options.n is None:
parser.print_help() parser.print_help()
parser.error("Both -n and -i cannot be used at the same time.") parser.error("Both -n and -i cannot be used at the same time.")
if options.order == "regexp":
if options.regexp == "":
parser.print_help()
parser.error("-r must be used")
if options.order == "date":
if options.datefmt is None:
options.datefmt = "%Y-%m-%d"
return options return options
@@ -168,6 +192,13 @@ def regexorder(inFiles, inFolder, outFolders, matcher, uniqlabel, link):
copyfileorfolder(f, inFolder, outFolders[outidx], link) copyfileorfolder(f, inFolder, outFolders[outidx], link)
def dateorder(inFiles, inFolder, outFolders, move):
"""Copy files by regex match"""
for f, d in zip(inFiles, outFolders):
copyfileorfolder(f, inFolder, d, move)
def regexmatches(inFiles, opts): def regexmatches(inFiles, opts):
matcher = re.compile(opts.regexp) matcher = re.compile(opts.regexp)
matches = [] matches = []
@@ -187,6 +218,17 @@ def regexmatches(inFiles, opts):
return (outFolders, uniqlabel, matcher) return (outFolders, uniqlabel, matcher)
def datematches(inFiles, opts):
matches = []
for f in inFiles:
matches.append(datetime.fromtimestamp(os.path.getmtime(os.path.join(opts.path, f))).strftime(opts.datefmt))
outFolders = []
for x in matches:
outFolders.append(os.path.join(opts.path, x))
print("Unique dates", len(set(matches)))
return outFolders
def offset(it): def offset(it):
total = 0 total = 0
for x in it: for x in it:
@@ -209,23 +251,22 @@ def main():
if options.files: if options.files:
inFiles = [f for f in inFiles if os.path.isfile(os.path.join(options.path, f))] inFiles = [f for f in inFiles if os.path.isfile(os.path.join(options.path, f))]
if options.n:
n = options.n
i = math.ceil(len(inFiles) / n)
else:
n = math.ceil(len(inFiles) / options.i)
i = options.i
if method == "regexp": if method == "regexp":
(outFolders, uniqlabel, matcher) = regexmatches(inFiles, options) (outFolders, uniqlabel, matcher) = regexmatches(inFiles, options)
elif method == "date":
outFolders = datematches(inFiles, options)
else: else:
if options.n:
n = options.n
i = math.ceil(len(inFiles) / n)
else:
n = math.ceil(len(inFiles) / options.i)
i = options.i
print("Splitting to {} folders, <={} files / folder".format(n, i)) print("Splitting to {} folders, <={} files / folder".format(n, i))
outFolders = [] outFolders = []
padding = "{:0" + str(len(str(n))) + "d}" padding = "{:0" + str(len(str(n))) + "d}"
for x in range(n): for x in range(n):
outFolders.append( outFolders.append(os.path.join(options.path, ("folder-" + padding).format(x + 1)))
os.path.join(options.path, ("folder-" + padding).format(x + 1))
)
if options.dry: if options.dry:
print("Not doing anything, --dry") print("Not doing anything, --dry")
@@ -242,6 +283,9 @@ def main():
portorder(inFiles, options.path, outFolders, n, options.move) portorder(inFiles, options.path, outFolders, n, options.move)
if method == "sequence": if method == "sequence":
fileorder(inFiles, options.path, outFolders, n, options.move) fileorder(inFiles, options.path, outFolders, n, options.move)
if method == "date":
dateorder(inFiles, options.path, outFolders, options.move)
report(outFolders) report(outFolders)