-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomRules.jl
99 lines (86 loc) · 3.47 KB
/
CustomRules.jl
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
import Dates
import JSON3
import ..FileSorterData: Rule, FileSorterApp, FileSort, DirSort, hook!, setup, process, findanalyzation, fullpath, stop
import ..FileSorterActionQueue: enqueue
export dispatch
struct PrintDepthOfFile <: Rule end
PrintDepthOfFile(::Any) = PrintDepthOfFile()
setup(app::FileSorterApp, ::PrintDepthOfFile) = hook!(app, DepthAnalyzer([0]))
function process(app::FileSorterApp, ::PrintDepthOfFile, file::FileSort)
depthAnalyzation = findanalyzation(DepthAnalyzation, file)
enqueue(app.actionQueue, PrintQueueItem(file.name, string(depthAnalyzation.depth)))
end
struct DeleteFilesByType <: Rule
targetTypes::Vector{AbstractString}
end
setup(app::FileSorterApp, ::DeleteFilesByType) = hook!(app, TypeAnalyzer())
function process(app::FileSorterApp, rule::DeleteFilesByType, file::FileSort)
typeAnalyzation = findanalyzation(TypeAnalyzation, file)
if typeAnalyzation.type in rule.targetTypes
enqueue(app.actionQueue, DeleteFile(fullpath(file)))
end
end
struct DeleteFilesByTypeCreatedSinceDays <: Rule
targetTypes::Vector{AbstractString}
modifiedSinceDays::Int
end
DeleteFilesByTypeCreatedSinceDays(args::Vector{AbstractString}) = DeleteFilesByTypeCreatedSinceDays(args[begin:end-1], parse(Int, args[end]))
setup(app::FileSorterApp, ::DeleteFilesByTypeCreatedSinceDays) = begin
hook!(app, TypeAnalyzer())
hook!(app, StatAnalyzer())
end
function process(app::FileSorterApp, rule::DeleteFilesByTypeCreatedSinceDays, file::FileSort)
typeAnalyzation = findanalyzation(TypeAnalyzation, file)
if !(typeAnalyzation.type in rule.targetTypes)
return
end
statAnalyzation = findanalyzation(StatAnalyzation, file)
timeSinceModification = Dates.now() - Dates.unix2datetime(statAnalyzation.stats.ctime)
if timeSinceModification <= Dates.Day(rule.modifiedSinceDays)
return
end
enqueue(app.actionQueue, DeleteFile(fullpath(file)))
end
struct SkipAnalysis <: Rule
skipAnlysisSinceMinutes::Int
fileRecordKeeper::String
end
mutable struct SkipAnalysisRecord
lastAnalysis::Dates.DateTime
end
SkipAnalysis(args::Vector{AbstractString}) = SkipAnalysis(parse(Int, args[begin]), args[end])
setup(app::FileSorterApp, rule::SkipAnalysis) = begin
projectDirectory = dirname(dirname(dirname(@__FILE__)))
dirPath = projectDirectory * "/tmp/"
if !isdir(dirPath)
mkdir(dirPath)
end
filePath = dirPath * rule.fileRecordKeeper *
(endswith(rule.fileRecordKeeper, ".json") ? "" : ".json")
if !isfile(filePath)
open(filePath, "w") do io
JSON3.write(io, SkipAnalysisRecord(Dates.now()))
end
return
end
content = JSON3.read(read(filePath, String), SkipAnalysisRecord)
sinceLastAnalysis = Dates.now() - Dates.DateTime(content.lastAnalysis)
if sinceLastAnalysis < Dates.Minute(rule.skipAnlysisSinceMinutes)
println("Skipping Analysis. Last analysis was " *
string(Dates.canonicalize(Dates.CompoundPeriod(sinceLastAnalysis))) *
" ago")
stop(app)
return
end
content.lastAnalysis = Dates.now()
open(filePath, "w") do io
JSON3.write(io, content)
end
end
function dispatch(name, args...)
return Dict(
"PrintDepthOfFile" => PrintDepthOfFile,
"DeleteFilesByType" => DeleteFilesByType,
"DeleteFilesByTypeCreatedSinceDays" => DeleteFilesByTypeCreatedSinceDays,
"SkipAnalysis" => SkipAnalysis)[name](convert(Vector{AbstractString}, collect(args)))
end