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
import os, sys
import math, shutil, re
import math
import os
import re
import shutil
import sys
from datetime import datetime
from random import shuffle
VERSION = "0.2"
VERSION = "0.3"
def setup_options():
@@ -21,7 +25,7 @@ def setup_options():
dest="order",
default="sequence",
help="Splitting method.",
choices=["sequence", "sparse", "regexp", "random"],
choices=["sequence", "sparse", "regexp", "random", "date"],
)
parser.add_argument(
"-m",
@@ -53,6 +57,15 @@ def setup_options():
default="",
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(
"-n",
"-N",
@@ -81,15 +94,26 @@ def setup_options():
help="Folder to split.",
)
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 != "":
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:
parser.print_help()
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
@@ -168,6 +192,13 @@ def regexorder(inFiles, inFolder, outFolders, matcher, uniqlabel, 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):
matcher = re.compile(opts.regexp)
matches = []
@@ -187,6 +218,17 @@ def regexmatches(inFiles, opts):
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):
total = 0
for x in it:
@@ -209,23 +251,22 @@ def main():
if options.files:
inFiles = [f for f in inFiles if os.path.isfile(os.path.join(options.path, f))]
if method == "regexp":
(outFolders, uniqlabel, matcher) = regexmatches(inFiles, options)
elif method == "date":
outFolders = datematches(inFiles, options)
else:
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":
(outFolders, uniqlabel, matcher) = regexmatches(inFiles, options)
else:
print("Splitting to {} folders, <={} files / folder".format(n, i))
outFolders = []
padding = "{:0" + str(len(str(n))) + "d}"
for x in range(n):
outFolders.append(
os.path.join(options.path, ("folder-" + padding).format(x + 1))
)
outFolders.append(os.path.join(options.path, ("folder-" + padding).format(x + 1)))
if options.dry:
print("Not doing anything, --dry")
@@ -242,6 +283,9 @@ def main():
portorder(inFiles, options.path, outFolders, n, options.move)
if method == "sequence":
fileorder(inFiles, options.path, outFolders, n, options.move)
if method == "date":
dateorder(inFiles, options.path, outFolders, options.move)
report(outFolders)