file_crawler 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/file_crawler/finder.rb +37 -1
- data/lib/file_crawler/finder/command.rb +2 -0
- data/lib/file_crawler/finder/command/collect.rb +52 -0
- data/lib/file_crawler/finder/command/move.rb +70 -7
- data/lib/file_crawler/finder/command/organize.rb +8 -0
- data/lib/file_crawler/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 56b1c0421bfe77a692ac4fe8cce0fc983efbdffb
|
4
|
+
data.tar.gz: 98bd4a1017ee31dc96e7292a562db54e350dcd89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 756de8dbc1e566231d4865785ac34e2b151157e4c19df0c1e40f5f4769baaa695acc58a774aa010683e55a1427c5784f201ca46d40096b8092414c1d7e4b2f44
|
7
|
+
data.tar.gz: 2046ac2818fac0e19b00b3d853b6dae4f42b2e85cffdeb41bc124a4d848a6d08eea8db7603b9f059d754bcd7c3591fa8de84951b7482e19f26135bbd19cb1dc4
|
data/lib/file_crawler/finder.rb
CHANGED
@@ -6,7 +6,9 @@ module FileCrawler
|
|
6
6
|
attr_accessor :path
|
7
7
|
attr_reader :rows
|
8
8
|
|
9
|
+
include Command::Collect
|
9
10
|
include Command::Move
|
11
|
+
include Command::Organize
|
10
12
|
include Command::Search
|
11
13
|
|
12
14
|
def initialize(path = nil)
|
@@ -52,7 +54,41 @@ module FileCrawler
|
|
52
54
|
directories = search(path, search_conditions)
|
53
55
|
|
54
56
|
finder = FileCrawler::Finder.new
|
55
|
-
|
57
|
+
|
58
|
+
case
|
59
|
+
when conditions[:numbering] == true
|
60
|
+
finder.move_directories_with_numbering(directories, destination)
|
61
|
+
else
|
62
|
+
finder.move_directories_not_exist_destination(directories, destination)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# conditions
|
67
|
+
# - if dont have extension_in_directory, directory true
|
68
|
+
def self.collect(directories, conditions = {})
|
69
|
+
search_conditions = {
|
70
|
+
directory: true,
|
71
|
+
extension_in_directory: conditions[:extension_in_directory]
|
72
|
+
}
|
73
|
+
search_conditions[:directory] = nil unless search_conditions[:extension_in_directory].nil?
|
74
|
+
|
75
|
+
files = directories.map {|path|
|
76
|
+
search(path, search_conditions)
|
77
|
+
}.flatten
|
78
|
+
|
79
|
+
finder = FileCrawler::Finder.new
|
80
|
+
finder.collect(files, conditions)
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.organize(directories, destination, conditions = {})
|
84
|
+
finder = FileCrawler::Finder.new
|
85
|
+
|
86
|
+
result = collect(directories, conditions).map {|key, value|
|
87
|
+
directory = destination + '/' + key
|
88
|
+
finder.move_directories_with_numbering(value, directory)
|
89
|
+
}.flatten
|
90
|
+
|
91
|
+
result
|
56
92
|
end
|
57
93
|
|
58
94
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module FileCrawler
|
2
|
+
class Finder
|
3
|
+
module Command
|
4
|
+
module Collect
|
5
|
+
|
6
|
+
def collect(file_paths, conditions = {})
|
7
|
+
collection = collect_into_filename(file_paths)
|
8
|
+
|
9
|
+
case
|
10
|
+
when !conditions[:unique].nil?
|
11
|
+
collection = unique_in_collection(collection)
|
12
|
+
end
|
13
|
+
|
14
|
+
collection
|
15
|
+
end
|
16
|
+
|
17
|
+
def split_for_collect(string)
|
18
|
+
pattern = /[\p{Hiragana}|\p{Katakana}|\p{Han}|[a-zA-Z0-9]ー]+/
|
19
|
+
return string.scan(pattern)
|
20
|
+
end
|
21
|
+
|
22
|
+
def collect_into_filename(file_paths)
|
23
|
+
hash = {}
|
24
|
+
|
25
|
+
file_paths.each {|file_path|
|
26
|
+
filename = File.basename(file_path)
|
27
|
+
split_for_collect(filename).each {|term|
|
28
|
+
hash[term] ||= []
|
29
|
+
hash[term] << file_path
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
hash
|
34
|
+
end
|
35
|
+
|
36
|
+
def unique_in_collection(collection)
|
37
|
+
hash = {}
|
38
|
+
|
39
|
+
collection.sort {|(k1, v1), (k2, v2)|
|
40
|
+
v2.size <=> v1.size && k1 <=> k2
|
41
|
+
}.each {|term, file_paths|
|
42
|
+
selection = file_paths.select {|v| !hash.values.flatten.include?(v) }
|
43
|
+
hash[term] = selection unless selection.empty?
|
44
|
+
}
|
45
|
+
|
46
|
+
hash
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -5,7 +5,7 @@ module FileCrawler
|
|
5
5
|
module Command
|
6
6
|
module Move
|
7
7
|
|
8
|
-
def
|
8
|
+
def move_directories_not_exist_destination(directories, destination)
|
9
9
|
move_targets = directories.select {|directory|
|
10
10
|
valiable_to_move?(directory, destination)
|
11
11
|
}
|
@@ -13,19 +13,82 @@ module FileCrawler
|
|
13
13
|
!valiable_to_move?(directory, destination)
|
14
14
|
}
|
15
15
|
|
16
|
+
create_directory_if_needed(destination)
|
16
17
|
FileUtils.mv(move_targets, destination)
|
17
18
|
|
18
|
-
move_targets.map {|
|
19
|
-
destination + '/' + File.basename(
|
19
|
+
move_targets.map {|directory|
|
20
|
+
destination + '/' + File.basename(directory)
|
20
21
|
} + not_move_targets
|
21
22
|
end
|
22
23
|
|
23
|
-
def
|
24
|
-
|
24
|
+
def move_directories_with_numbering(directories, destination)
|
25
|
+
move_targets = []
|
26
|
+
not_move_targets = []
|
27
|
+
rename_targets = []
|
25
28
|
|
26
|
-
|
27
|
-
|
29
|
+
directories.each {|directory|
|
30
|
+
if is_same?(directory, destination)
|
31
|
+
not_move_targets << directory
|
32
|
+
next
|
33
|
+
end
|
34
|
+
|
35
|
+
if exist_file?(directory, destination)
|
36
|
+
rename_targets << directory
|
37
|
+
next
|
38
|
+
end
|
39
|
+
|
40
|
+
move_targets << directory
|
41
|
+
}
|
42
|
+
|
43
|
+
create_directory_if_needed(destination)
|
44
|
+
FileUtils.mv(move_targets, destination)
|
45
|
+
|
46
|
+
renamed_targets = []
|
47
|
+
rename_targets.each {|directory|
|
48
|
+
filename = find_free_filename(directory, destination)
|
49
|
+
to = destination + '/' + filename
|
50
|
+
FileUtils.mv(directory, to)
|
51
|
+
|
52
|
+
renamed_targets << to
|
53
|
+
}
|
54
|
+
|
55
|
+
move_targets.map {|directory|
|
56
|
+
destination + '/' + File.basename(directory)
|
57
|
+
} + not_move_targets + renamed_targets
|
28
58
|
end
|
59
|
+
|
60
|
+
def create_directory_if_needed(directory)
|
61
|
+
Dir.mkdir(directory, 0777) unless File.exist?(directory)
|
62
|
+
end
|
63
|
+
|
64
|
+
def find_free_filename(current, destination)
|
65
|
+
filename = File.basename(current)
|
66
|
+
|
67
|
+
index = 1
|
68
|
+
new_filename = "#{filename} (#{index})"
|
69
|
+
while File.exist?(new_filename)
|
70
|
+
i += 1
|
71
|
+
new_filename = "#{filename} (#{index})"
|
72
|
+
end
|
73
|
+
|
74
|
+
new_filename
|
75
|
+
end
|
76
|
+
|
77
|
+
def valiable_to_move?(current, destination)
|
78
|
+
return false if is_same?(current, destination)
|
79
|
+
|
80
|
+
!exist_file?(current, destination)
|
81
|
+
end
|
82
|
+
|
83
|
+
def is_same?(current, destination)
|
84
|
+
current == destination
|
85
|
+
end
|
86
|
+
|
87
|
+
def exist_file?(current, destination)
|
88
|
+
next_path = destination + '/' + File.basename(current)
|
89
|
+
File.exist?(next_path)
|
90
|
+
end
|
91
|
+
|
29
92
|
end
|
30
93
|
end
|
31
94
|
end
|
data/lib/file_crawler/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: file_crawler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hirohisa Kawasaki
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -65,7 +65,9 @@ files:
|
|
65
65
|
- lib/file_crawler.rb
|
66
66
|
- lib/file_crawler/finder.rb
|
67
67
|
- lib/file_crawler/finder/command.rb
|
68
|
+
- lib/file_crawler/finder/command/collect.rb
|
68
69
|
- lib/file_crawler/finder/command/move.rb
|
70
|
+
- lib/file_crawler/finder/command/organize.rb
|
69
71
|
- lib/file_crawler/finder/command/search.rb
|
70
72
|
- lib/file_crawler/version.rb
|
71
73
|
homepage: https://github.com/hirohisa/file_crawler
|