-
Notifications
You must be signed in to change notification settings - Fork 0
/
card2disk.py
153 lines (131 loc) · 4.21 KB
/
card2disk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import os
import sys
import csv
import math
import stat
import shutil
import logging
import itertools as it
from pathlib import Path
from datetime import datetime
from argparse import ArgumentParser
from multiprocessing import Pool, Lock, JoinableQueue
#
# Setup logger
#
lvl = os.environ.get('PYTHONLOGLEVEL', 'warning').upper()
fmt = '[ %(asctime)s %(levelname)s %(process)d ] %(message)s'
logging.basicConfig(format=fmt, datefmt="%H:%M:%S", level=lvl)
logging.captureWarnings(True)
#
#
#
class ExifPath:
_exif_dt = '%Y:%m:%d %H:%M:%S'
def __init__(self, with_videos):
self.support = set([
'image',
])
if with_videos:
self.support.add('video')
self.dates = (
('CreateDate', self._exif_dt),
('DateTimeOriginal', self._exif_dt),
('ModifyDate', self._exif_dt),
('FileModifyDate', self._exif_dt + '%z'),
)
def __call__(self, exif, suffix):
#
# Make sure the file type is supported
#
(mime, _) = exif['MIMEType'].split('/', maxsplit=1)
if mime.casefold() not in self.support:
raise TypeError(f'Unsupported file type "{mime}"')
#
# Use the time to build a filename
#
ctime = (self
.mktime(exif)
.strftime('%Y %m-%b %d-%H%M%S')
.upper()
.split())
path = Path(*ctime)
return path.with_suffix(suffix)
#
# Extract the time at which the picture was taken
#
def mktime(self, exif):
for (k, s) in self.dates:
creation = exif.get(k)
try:
return datetime.strptime(creation, s)
except (TypeError, ValueError):
logging.debug(f'Invalid {k}: "{creation}"')
raise ValueError('Cannot establish creation time')
#
# Use the time to create a destination file name
#
class PathName:
@staticmethod
def zcount(upper):
if upper < 1:
raise ArithmeticError('Max count must be greater than zero')
digits = math.floor(math.log10(upper - 1)) + 1
for i in it.count():
if i >= upper:
break
yield f'{i:0{digits}d}'
def __init__(self, destination, lock, maxtries):
self.destination = destination
self.lock = lock
self.maxtries = maxtries
def __call__(self, path):
basename = path.with_suffix('')
self.lock.acquire()
try:
for i in self.zcount(self.maxtries):
fname = f'{basename}-{i}{path.suffix}'
target = self.destination.joinpath(fname)
if not target.exists():
target.parent.mkdir(parents=True, exist_ok=True)
target.touch()
return target
finally:
self.lock.release()
raise FileExistsError('Cannot create unique filename')
def func(queue, lock, args):
exif2path = ExifPath(args.with_videos)
path2fname = PathName(args.destination, lock, args.max_tries)
mode = stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH
while True:
exif = queue.get()
source = Path(exif['SourceFile'])
try:
path = exif2path(exif, source.suffix.lower())
target = path2fname(path)
(src, dst) = map(str, (source, target))
shutil.copy2(src, dst)
os.chmod(dst, mode)
logging.info('{} -> {}'.format(source.name, target))
except (TypeError, ValueError, FileExistsError) as err:
logging.error('{} {}'.format(source, err))
finally:
queue.task_done()
if __name__ == '__main__':
arguments = ArgumentParser()
arguments.add_argument('--destination', type=Path)
arguments.add_argument('--max-tries', type=int, default=100)
arguments.add_argument('--with-videos', action='store_true')
arguments.add_argument('--workers', type=int)
args = arguments.parse_args()
queue = JoinableQueue()
initargs = (
queue,
Lock(),
args,
)
with Pool(args.workers, func, initargs):
reader = csv.DictReader(sys.stdin)
for row in reader:
queue.put(row)
queue.join()