file_crawler 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/file_crawler/finder.rb +18 -49
- data/lib/file_crawler/finder/command.rb +0 -1
- data/lib/file_crawler/finder/command/collect.rb +18 -15
- data/lib/file_crawler/finder/command/move.rb +21 -16
- data/lib/file_crawler/finder/command/search.rb +16 -60
- data/lib/file_crawler/version.rb +1 -1
- metadata +2 -3
- data/lib/file_crawler/finder/command/organize.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b18468fb9ef26484e074f797eca9d4323ea34012
|
4
|
+
data.tar.gz: b970cc673130d37e5f46b6b59b2eb000b7dd8789
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2d1fadd395355fbda390bbc1683a0752077fd3cec402f15512b0613a9e0201cfd16fed8ad99aa715f328e941dd58ff748ae401cb6122e280fcf58685fdca405
|
7
|
+
data.tar.gz: 7fbb2d29b40af52deb561cabf703a9bd768e822eb54a05c2596b5bd8b436ccc4ea6bbe83136208a1365f8af0be97f7e26e6764162f2980c3efdad690faf51e69
|
data/lib/file_crawler/finder.rb
CHANGED
@@ -3,18 +3,14 @@ require_relative 'finder/command'
|
|
3
3
|
module FileCrawler
|
4
4
|
|
5
5
|
class Finder
|
6
|
-
attr_accessor :path
|
7
6
|
attr_reader :rows
|
8
7
|
|
9
8
|
include Command::Collect
|
10
9
|
include Command::Move
|
11
|
-
include Command::Organize
|
12
10
|
include Command::Search
|
13
11
|
|
14
|
-
def initialize
|
15
|
-
@path = path
|
12
|
+
def initialize
|
16
13
|
@rows = []
|
17
|
-
select_in_path(path) unless path.nil?
|
18
14
|
end
|
19
15
|
end
|
20
16
|
|
@@ -23,18 +19,8 @@ module FileCrawler
|
|
23
19
|
# exntesion [Array]
|
24
20
|
# extension_in_directory [Array]
|
25
21
|
def self.search(path, conditions = {})
|
26
|
-
finder = FileCrawler::Finder.new
|
27
|
-
|
28
|
-
case
|
29
|
-
when !conditions[:extension].nil?
|
30
|
-
finder.select_with_extension(conditions[:extension])
|
31
|
-
when conditions[:directory] == true
|
32
|
-
finder.select_directories
|
33
|
-
when conditions[:directory] == false
|
34
|
-
finder.select_files
|
35
|
-
when !conditions[:extension_in_directory].nil?
|
36
|
-
finder.select_with_extension_in_directory(conditions[:extension_in_directory])
|
37
|
-
end
|
22
|
+
finder = FileCrawler::Finder.new
|
23
|
+
finder.search(path, conditions)
|
38
24
|
|
39
25
|
finder.rows
|
40
26
|
end
|
@@ -45,37 +31,15 @@ module FileCrawler
|
|
45
31
|
def self.move(path, destination, conditions = {})
|
46
32
|
raise ArgumentError unless File.directory?(destination)
|
47
33
|
|
48
|
-
search_conditions = {
|
49
|
-
directory: true,
|
50
|
-
extension_in_directory: conditions[:extension_in_directory]
|
51
|
-
}
|
52
|
-
search_conditions[:directory] = nil unless search_conditions[:extension_in_directory].nil?
|
53
|
-
|
54
|
-
directories = search(path, search_conditions)
|
55
|
-
|
56
34
|
finder = FileCrawler::Finder.new
|
35
|
+
finder.search(path).move(destination)
|
57
36
|
|
58
|
-
|
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
|
37
|
+
finder.rows
|
64
38
|
end
|
65
39
|
|
66
40
|
# conditions
|
67
41
|
# - if dont have extension_in_directory, directory true
|
68
|
-
def self.collect(
|
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
|
-
|
42
|
+
def self.collect(path, conditions = {})
|
79
43
|
finder = FileCrawler::Finder.new
|
80
44
|
unless conditions[:regexs].nil?
|
81
45
|
conditions[:regexs].each {|regex|
|
@@ -83,18 +47,23 @@ module FileCrawler
|
|
83
47
|
}
|
84
48
|
end
|
85
49
|
|
86
|
-
finder.collect(
|
50
|
+
finder.search(path).collect(conditions)
|
51
|
+
|
52
|
+
finder.rows
|
87
53
|
end
|
88
54
|
|
89
|
-
def self.organize(
|
55
|
+
def self.organize(path, destination, conditions = {})
|
90
56
|
finder = FileCrawler::Finder.new
|
91
57
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
58
|
+
unless conditions[:regexs].nil?
|
59
|
+
conditions[:regexs].each {|regex|
|
60
|
+
finder.regexs << regex
|
61
|
+
}
|
62
|
+
end
|
63
|
+
|
64
|
+
finder.search(path).collect(conditions).move_from_collection(destination)
|
96
65
|
|
97
|
-
|
66
|
+
finder.rows
|
98
67
|
end
|
99
68
|
|
100
69
|
end
|
@@ -26,21 +26,10 @@ module FileCrawler
|
|
26
26
|
@regexs ||= []
|
27
27
|
end
|
28
28
|
|
29
|
-
def collect(
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
end
|
34
|
-
|
35
|
-
def decide_index_for_collect(string)
|
36
|
-
if !regexs.empty?
|
37
|
-
regexs.each {|regex|
|
38
|
-
return $1 unless regex.pattern.match(string).nil?
|
39
|
-
}
|
40
|
-
end
|
41
|
-
|
42
|
-
pattern = /[\p{Hiragana}|\p{Katakana}|\p{Han}|[a-zA-Z0-9]ー ]+/
|
43
|
-
return string.strip.scan(pattern).first
|
29
|
+
def collect(conditions = {})
|
30
|
+
tap {
|
31
|
+
@rows = collect_into_filename(@rows)
|
32
|
+
}
|
44
33
|
end
|
45
34
|
|
46
35
|
def collect_into_filename(file_paths)
|
@@ -56,6 +45,20 @@ module FileCrawler
|
|
56
45
|
hash
|
57
46
|
end
|
58
47
|
|
48
|
+
def decide_index_for_collect(string)
|
49
|
+
if !regexs.empty?
|
50
|
+
regexs.each {|regex|
|
51
|
+
return $1 unless regex.pattern.match(string).nil?
|
52
|
+
}
|
53
|
+
end
|
54
|
+
|
55
|
+
pattern = /[\p{Hiragana}|\p{Katakana}|\p{Han}|[a-zA-Z0-9]ー ]+/
|
56
|
+
result = string.strip.scan(pattern).first
|
57
|
+
return result unless result.nil?
|
58
|
+
|
59
|
+
string
|
60
|
+
end
|
61
|
+
|
59
62
|
end
|
60
63
|
end
|
61
64
|
end
|
@@ -5,28 +5,24 @@ module FileCrawler
|
|
5
5
|
module Command
|
6
6
|
module Move
|
7
7
|
|
8
|
-
def
|
9
|
-
|
10
|
-
|
8
|
+
def move(destination)
|
9
|
+
tap {
|
10
|
+
@rows = move_with_numbering(@rows, destination)
|
11
11
|
}
|
12
|
-
|
13
|
-
!valiable_to_move?(directory, destination)
|
14
|
-
}
|
15
|
-
|
16
|
-
create_directory_if_needed(destination)
|
17
|
-
FileUtils.mv(move_targets, destination)
|
12
|
+
end
|
18
13
|
|
19
|
-
|
20
|
-
|
21
|
-
|
14
|
+
def move_from_collection(destination)
|
15
|
+
tap {
|
16
|
+
@rows = move_from_collection_with_numbering(@rows, destination)
|
17
|
+
}
|
22
18
|
end
|
23
19
|
|
24
|
-
def
|
20
|
+
def move_with_numbering(source, destination)
|
25
21
|
move_targets = []
|
26
22
|
not_move_targets = []
|
27
23
|
rename_targets = []
|
28
24
|
|
29
|
-
|
25
|
+
source.each {|directory|
|
30
26
|
if is_same?(directory, destination)
|
31
27
|
not_move_targets << directory
|
32
28
|
next
|
@@ -57,6 +53,15 @@ module FileCrawler
|
|
57
53
|
} + not_move_targets + renamed_targets
|
58
54
|
end
|
59
55
|
|
56
|
+
def move_from_collection_with_numbering(source, destination)
|
57
|
+
result = source.map {|key, value|
|
58
|
+
directory = destination + '/' + key
|
59
|
+
move_with_numbering(value, directory)
|
60
|
+
}.flatten
|
61
|
+
|
62
|
+
result
|
63
|
+
end
|
64
|
+
|
60
65
|
def create_directory_if_needed(directory)
|
61
66
|
Dir.mkdir(directory, 0777) unless File.exist?(directory)
|
62
67
|
end
|
@@ -66,8 +71,8 @@ module FileCrawler
|
|
66
71
|
|
67
72
|
index = 1
|
68
73
|
new_filename = "#{filename} (#{index})"
|
69
|
-
while
|
70
|
-
|
74
|
+
while exist_file?(new_filename, destination)
|
75
|
+
index += 1
|
71
76
|
new_filename = "#{filename} (#{index})"
|
72
77
|
end
|
73
78
|
|
@@ -3,80 +3,36 @@ module FileCrawler
|
|
3
3
|
module Command
|
4
4
|
module Search
|
5
5
|
|
6
|
-
def
|
6
|
+
def search(path, conditions = {})
|
7
7
|
tap {
|
8
|
-
@rows =
|
8
|
+
@rows = search_directories(path)
|
9
9
|
}
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
13
|
-
|
14
|
-
|
15
|
-
}.map {|item|
|
16
|
-
path + '/' + item
|
17
|
-
}
|
18
|
-
end
|
19
|
-
|
20
|
-
def select_with_extension(extension)
|
21
|
-
tap {
|
22
|
-
@rows = find_rows_with_extension(extension, rows)
|
23
|
-
}
|
24
|
-
end
|
25
|
-
|
26
|
-
def find_rows_with_extension(extension, resource)
|
27
|
-
find_rows_files(resource).select {|item|
|
28
|
-
extension.any? {|e|
|
29
|
-
extname = File.extname(item).downcase
|
30
|
-
extname == ".#{e.downcase}"
|
31
|
-
}
|
32
|
-
}
|
33
|
-
end
|
34
|
-
|
35
|
-
def select_files
|
36
|
-
tap {
|
37
|
-
@rows = find_rows_files(rows)
|
38
|
-
}
|
39
|
-
end
|
12
|
+
def search_directories(path)
|
13
|
+
directories = search_directories_in_path(path)
|
14
|
+
return [path] if directories.empty?
|
40
15
|
|
41
|
-
|
42
|
-
|
43
|
-
|
16
|
+
result = []
|
17
|
+
directories.each {|item|
|
18
|
+
result += search_directories(item)
|
44
19
|
}
|
45
|
-
end
|
46
20
|
|
47
|
-
|
48
|
-
tap {
|
49
|
-
@rows = find_rows_directories(rows)
|
50
|
-
}
|
21
|
+
result
|
51
22
|
end
|
52
23
|
|
53
|
-
def
|
54
|
-
|
24
|
+
def search_directories_in_path(path)
|
25
|
+
find_files_in_path(path).select {|item|
|
55
26
|
File.directory?(item)
|
56
27
|
}
|
57
28
|
end
|
58
29
|
|
59
|
-
def
|
60
|
-
|
61
|
-
|
62
|
-
}
|
63
|
-
|
64
|
-
|
65
|
-
def find_rows_with_extension_in_directories(extension, directories)
|
66
|
-
directories.select {|directory|
|
67
|
-
variable_to_exist_with_extension_in_directory(extension, directory)
|
68
|
-
}
|
69
|
-
end
|
70
|
-
|
71
|
-
def variable_to_exist_with_extension_in_directory(extension, directory)
|
72
|
-
subfiles = find_rows_in_path(directory)
|
73
|
-
result = find_rows_with_extension(extension, subfiles)
|
74
|
-
return true if result.size > 0
|
75
|
-
|
76
|
-
subdirectories = find_rows_directories(subfiles).select {|subdirectory|
|
77
|
-
variable_to_exist_with_extension_in_directory(extension, subdirectory)
|
30
|
+
def find_files_in_path(path)
|
31
|
+
Dir.entries(path).select {|item|
|
32
|
+
!item.start_with?('.')
|
33
|
+
}.map {|item|
|
34
|
+
path + '/' + item
|
78
35
|
}
|
79
|
-
subdirectories.size > 0
|
80
36
|
end
|
81
37
|
end
|
82
38
|
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.3.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-
|
11
|
+
date: 2016-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -67,7 +67,6 @@ files:
|
|
67
67
|
- lib/file_crawler/finder/command.rb
|
68
68
|
- lib/file_crawler/finder/command/collect.rb
|
69
69
|
- lib/file_crawler/finder/command/move.rb
|
70
|
-
- lib/file_crawler/finder/command/organize.rb
|
71
70
|
- lib/file_crawler/finder/command/search.rb
|
72
71
|
- lib/file_crawler/version.rb
|
73
72
|
homepage: https://github.com/hirohisa/file_crawler
|