-
Notifications
You must be signed in to change notification settings - Fork 71
/
class_rcopy.ahk
398 lines (346 loc) · 11.9 KB
/
class_rcopy.ahk
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
; ahk: console
class RCopy
{
static CV_YEAR4 := "%y4%"
static CV_YEAR2 := "%y2%"
static CV_MONTH := "%m%"
static CV_DAY := "%d%"
static CV_X := "%x%"
static CV_Y := "%y%"
static CV_PCT := "%%"
static options := RCopy.set_defaults()
static context_vars := RCopy.set_context_vars()
set_defaults() ; IDEA: Rename to InitDefaults()
{
return { dest: RegExReplace(A_MyDocuments
, "\\Documents$"
, "\Pictures\Camera Roll")
, rename_as: ""
, source: ""
, start_with: 1
, dry_run: false
, file_types: [".*\.jpe?g"
, ".*\.tiff?"
, ".*\.orf"
, ".*\.dng"]
, verify: false
, help_context_vars: false
, help_file_types: false
, help: false }
}
set_context_vars() ; IDEA: Rename to InitCtxVars()
{
return { RCopy.CV_YEAR4: { value: "YYYY"
, desc: "4-digit year of creation date of the source file" }
, RCopy.CV_YEAR2: { value: "YY"
, desc: "2-digit year of creation date of the source file" }
, RCopy.CV_MONTH: { value: "MM"
, desc: "2-digit month of creation date of the source file" }
, RCopy.CV_DAY: { value: "DD"
, desc: "2-digit day of creation date of the source file" }
, RCopy.CV_X: { value: 1
, desc: "Sequence number which will be increased by every file "
. "processed (use e.g. %00x% to create 3 digits with leadings "
. "zeroes). Start value is 1 but can be changed by "
. "'--start-with' option" }
, RCopy.CV_Y: { value: 0
, desc: "Number of files to import (use e.g. %00y% to create "
. "3 digits with leading zeroes)."}
, RCopy.CV_PCT: { value: "%"
, desc: "Insert a single `%` character" } }
}
SetupSourcePath(source_path)
{
_log := new Logger("class." A_ThisFunc)
TestCase.Assert(source_path <> "", "Specify source", "error")
if (RegExMatch(source_path, "(\.|\*|\*\.|\*\.\*|\.\\\*)$"))
{
RCopy.options.source := source_path
}
else
{
is_dir := InStr(FileExist(source_path), "D") > 0
if (is_dir)
{
RCopy.options.source := source_path "\*.*"
}
else
{
SplitPath source_path, name, dir
RCopy.options.source := (dir <> "" ? dir "\" : "")
. (name <> "" ? name : "*.*")
}
}
return _log.Exit()
}
SetupTargetPath()
{
_log := new Logger("class." A_ThisFunc)
if (RCopy.options.dest)
{
dest_dir := RCopy.UseContext(RCopy.options.dest)
target_path := dest_dir . (SubStr(dest_dir, 0) = "\" ? "" : "\")
}
return _log.Exit(target_path)
}
CopyAndRename()
{
_log := new Logger("class." A_ThisFunc)
Ansi.WriteLine("Copy from: " RCopy.options.source)
Ansi.WriteLine(" to: " RCopy.options.dest)
fn_list := [], fn_discarded := []
RCopy.CollectFilenames(fn_list, fn_discarded)
Ansi.Write("`nFound %s %s, "
.printf((fn_list.MaxIndex() = "" ? ["no", "files"]
: fn_list.MaxIndex() = 1 ? ["1", "file"]
: [fn_list.MaxIndex(), "files"])))
Ansi.Write("discarded %s %s."
.printf((fn_discarded.MaxIndex() = "" ? ["no", "files"]
: fn_discarded.MaxIndex() = 1 ? ["1", "file"]
: [fn_discarded.MaxIndex(), "files"])))
Ansi.WriteLine("", true)
for i, file in fn_list
{
RCopy.FillFileContext(file.name, file.create_time)
target_dir := RCopy.SetupTargetPath()
if (RCopy.options.rename_as)
{
pattern := RCopy.UseContext(RCopy.options.rename_as)
. "." file.ext
}
else
{
pattern := file.name
}
Ansi.WriteLine(file.name " ... " target_dir pattern, true)
RCopy.IncreaseSequenceContext()
}
return _log.Exit()
}
CollectFilenames(ByRef passed_files, ByRef discarded_files)
{
_log := new Logger("class." A_ThisFunc)
md5sum := ""
passed_files := [], discarded_files := []
loop files, % RCopy.options.source
{
if (RegExMatch(A_LoopFileName, RCopy.FtExpr()))
{
if (RCopy.options.verify)
{
md5sum := RCopy.FileMD5(A_LoopFileFullPath)
}
passed_files.Push({ name: A_LoopFileName
, create_time: A_LoopFileTimeCreated
, ext: A_LoopFileExt
, csum: md5sum })
}
else
{
discarded_files.Push(A_LoopFileName)
}
}
RCopy.context_vars[RCopy.CV_Y].value := passed_files.MaxIndex()
return _log.Exit()
}
FileMD5(file_name)
{
_log := new Logger("class." A_ThisFunc)
file := FileOpen(file_name, "r")
FileGetSize size, %file_name%
size := file.RawRead(content, size)
file.Close()
return _log.Exit(Crypto.MD5.Encode(content, size))
}
UseContext(name) ; IDEA: Rename to UseCtx
{
_log := new Logger("class." A_ThisFunc)
p := 1
loop
{
if (p := RegExMatch(name, "%(d|m|y[24])%", $, p))
{
name := name.ReplaceAt(p, StrLen($)
, RCopy.context_vars[$].value)
p += StrLen($)
}
}
until (p = 0)
p := 1
loop
{
if (p := RegExMatch(name, "%0*([xy])%", $, p))
{
name := name.ReplaceAt(p, StrLen($)
, (RCopy.context_vars["%"
. $1
. "%"].value).Pad(String.PAD_NUMBER
, StrLen($)-2))
p += StrLen($)-2
}
}
until (p = 0)
name := StrReplace(name, RCopy.CV_PCT
, RCopy.context_vars[RCopy.CV_PCT].value)
return _log.Exit(name)
}
IncreaseSequenceContext() ; IDEA: Rename to IncSeqCtx
{
_log := new Logger("class." A_ThisFunc)
RCopy.context_vars[RCopy.CV_X].value += 1
return _log.Exit()
}
FillFileContext(filename, create_timstamp) ; IDEA: Rename to FillFileCtx
{
_log := new Logger("class." A_ThisFunc)
FormatTime year, %create_timstamp%, yyyy
FormatTime year2, %create_timstamp%, yy
FormatTime month, %create_timstamp%, MM
FormatTime day, %create_timstamp%, dd
RCopy.context_vars[RCopy.CV_YEAR4].value := year
RCopy.context_vars[RCopy.CV_YEAR2].value := year2
RCopy.context_vars[RCopy.CV_MONTH].value := month
RCopy.context_vars[RCopy.CV_DAY].value := day
return _log.Exit()
}
ListContextVars() ; IDEA: Rename to ListCtxVars()
{
_log := new Logger("class." A_ThisFunc)
dt := new DataTable()
dt.DefineColumn(new DataTable.Column(10))
dt.DefineColumn(new DataTable.Column.Wrapped(60))
for pattern, context_var in RCopy.context_vars
{
dt.AddData([ pattern, context_var.desc ])
}
Ansi.WriteLine("`n" dt.GetTableAsString(), true)
return _log.Exit()
}
ListFileTypes()
{
_log := new Logger("class." A_ThisFunc)
Ansi.WriteLine()
for i, file_type_expr in RCopy.options.file_types
{
Ansi.WriteLine(file_type_expr)
}
Ansi.WriteLine(,true)
return _log.Exit()
}
FtExpr()
{
_log := new Logger("class." A_ThisFunc)
ft_expr := ""
for i, file_type in RCopy.options.file_types
{
ft_expr .= (ft_expr = "" ? "" : "|") file_type
}
return _log.Exit("i)^(" ft_expr ")$")
}
CLI()
{
_log := new Logger("class." A_ThisFunc)
op := new OptParser("RCopy: [options] <source>",, "RCOPY_OPTS")
op.Add(new OptParser.Line("source"
, "Path and/or file pattern. Only supported file types will be "
. "copied, regardless of a given file pattern`n"))
op.Add(new OptParser.Group("Available options"))
op.Add(new OptParser.String("d", "dest", RCopy.options
, "dest", "dir"
, "Destination directory. Context variables my be used"
, OptParser.OPT_ARG, RCopy.options.dest, RCopy.options.dest))
op.Add(new OptParser.Line("", "(Default: " RCopy.options.dest ")"))
op.Add(new OptParser.String("r", "rename-as", RCopy.options
, "rename_as", "pattern"
, "Define a pattern to create the file name for the copied file. "
. "Context variables may be used (Default: name of the original "
. "file)"))
op.Add(new OptParser.String(0, "start-with", RCopy.options
, "start_with", "number"
, "Number to begin numbering with"
, OptParser.OPT_ARG
, RCopy.options.start_with, RCopy.options.start_with))
op.Add(new OptParser.String(0, "add-ft-expr", RCopy.options
, "file_types", "expr"
, "Add expression to select more file types"
, OptParser.OPT_ARG | OptParser.OPT_MULTIPLE
, RCopy.options.file_types, RCopy.options.file_types))
op.Add(new OptParser.Boolean("v", "verify", RCopy.options
, "verify"
, "Verify if the files have been properly copied"))
op.Add(new OptParser.Boolean(0, "dry-run", RCopy.options
, "dry_run"
, "Run without performing any file operation"))
op.Add(new OptParser.Boolean(0, "help-context-vars", RCopy.options
, "help_context_vars"
, "Show available context variables"))
op.Add(new OptParser.Boolean(0, "help-file-types", RCopy.options
, "help_file_types"
, "Show supported file types"))
op.Add(new OptParser.Line("--[no]env", "Ignore environment variable "
. op.stEnvVarName))
op.Add(new OptParser.Boolean("h", "help", RCopy.options
, "help"
, "Display usage", OptParser.OPT_HIDDEN))
return _log.Exit(op)
}
Run(args)
{
_log := new Logger("class." A_ThisFunc)
if (_log.Logs(Logger.Input))
{
_log.Input("args", args)
_log.Logs(Logger.Finest, "args:`n" LoggingHelper.Dump(args))
}
try
{
rc := 1
op := RCopy.CLI()
args := op.Parse(args)
if (RCopy.options.help)
{
Ansi.WriteLine(op.Usage())
rc := ""
}
else if (RCopy.options.help_context_vars)
{
RCopy.ListContextVars()
rc := ""
} else if (RCopy.options.help_file_types)
{
RCopy.ListFileTypes()
rc := ""
}
else
{
if (args.MaxIndex() > 1)
{
throw _log.Exit(Exception("Too many arguments"))
}
RCopy.SetupSourcePath(args[1])
RCopy.context_vars[RCopy.CV_X].value := RCopy.options.start_with
RCopy.CopyAndRename()
}
}
catch e
{
_log.Fatal(e.message)
Ansi.WriteLine(e.message)
Ansi.WriteLine(op.Usage())
rc := 0
}
return _log.Exit(rc)
}
}
#NoEnv ; NOTEST-BEGIN
#include <logging>
#include <system>
#include <ansi>
#include <datatable>
#include <string>
#include <optparser>
#include <testcase>
#include <crypto>
Main:
_main := new Logger("app.RCopy.label.main")
exitapp _main.Exit(RCopy.Run(System.vArgs)) ; NOTEST-END