file_crawler 0.4.1 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 2ae7e681e01e25bc786d86234309988d8209d53c
4
- data.tar.gz: 34e1cdc41285a4638de2907752500d65adde5c60
2
+ SHA256:
3
+ metadata.gz: df450fdad5aebd14ecd141e4264d9057df597798173557a2739fe7a6776f3fb6
4
+ data.tar.gz: 02d972b1d68d21fe2614f4bc46f717b5b064718a5c6369d1473567f53681586f
5
5
  SHA512:
6
- metadata.gz: cd2e9b07a4f4bc36b3ebe53622fd0edc5a62ef80930ce472c046d01ecba9d7d7ab26fe0a2a281666a2f88958b404a6343a64ede44844057e7164a40f8d02fc12
7
- data.tar.gz: 91d89b2fb72fd4deaaf49ea1591c6965c563919bba22a13d64565166f5e6b3a5a61cabee06e6a582376c587ee15dc31fe902eb56d6b5d01a0cd3718e25ed26fa
6
+ metadata.gz: 3f218741b1df108a251c0c782724f4438b919017b8908f8394dd74a615a863036a586fd2e7a357a1fcccaefcdbe2224c6ce34781e9fa3a980642a7f0c149828f
7
+ data.tar.gz: 0ffac33279d95fb1fa917be2b91afe188c2f0310808dd5cd8abc029eb27b8d2840d06a460be31dac713bfef991e62f1509282b80ac1512c86a753a97c8031932
@@ -1,4 +1,5 @@
1
1
  module FileCrawler
2
+
2
3
  class Regex
3
4
  attr_accessor :regexp_start, :regexp_end
4
5
 
@@ -16,50 +17,62 @@ module FileCrawler
16
17
  end
17
18
  end
18
19
 
19
- class Finder
20
- module Command
21
- module Collect
22
20
 
23
- attr_accessor :regexs
21
+ end
22
+
23
+ module FileCrawler::Finder::Command
24
24
 
25
- def regexs
26
- @regexs ||= []
27
- end
25
+ module Collect
26
+ include Base
28
27
 
29
- def collect(conditions = {})
30
- tap {
31
- @rows = collect_into_filename(@rows)
28
+ attr_accessor :regexs
29
+
30
+ def regexs
31
+ @regexs ||= []
32
+ end
33
+
34
+ def collect(options={})
35
+ tap {
36
+ if options[:regexs].is_a?(Array)
37
+ options[:regexs].each {|o|
38
+ regexs << FileCrawler::Regex.new(o[0], o[1]) if o.size == 2
32
39
  }
33
40
  end
34
41
 
35
- def collect_into_filename(file_paths)
36
- hash = {}
42
+ @collections = Organizer.new.run(@files, regexs)
43
+ }
44
+ end
37
45
 
38
- file_paths.each {|file_path|
39
- filename = File.basename(file_path)
40
- term = decide_index_for_collect(filename)
41
- hash[term] ||= []
42
- hash[term] << file_path
43
- }
46
+ class Organizer
44
47
 
45
- hash
46
- end
48
+ def run(filepaths, regexs)
49
+ hash = {}
47
50
 
48
- def decide_index_for_collect(string)
49
- if !regexs.empty?
50
- regexs.each {|regex|
51
- return $1.strip unless regex.pattern.match(string).nil?
52
- }
53
- end
51
+ filepaths.each {|filepath|
52
+ filename = File.basename(filepath)
53
+ term = decide_index(filename, regexs)
54
+ hash[term] ||= []
55
+ hash[term] << filepath
56
+ }
54
57
 
55
- pattern = /[\p{Hiragana}|\p{Katakana}|\p{Han}|[a-zA-Z0-9]ー  ]+/
56
- result = string.strip.scan(pattern).first
57
- return result.strip unless result.nil?
58
+ hash
59
+ end
58
60
 
59
- string.strip
61
+ def decide_index(string, regexs=[])
62
+ if !regexs.empty?
63
+ regexs.each {|regex|
64
+ return $1.strip unless regex.pattern.match(string).nil?
65
+ }
60
66
  end
61
67
 
68
+ pattern = /[\p{Hiragana}|\p{Katakana}|\p{Han}|[a-zA-Z0-9]ー  ]+/
69
+ result = string.strip.scan(pattern).first
70
+ return result.strip unless result.nil?
71
+
72
+ string.strip
62
73
  end
63
74
  end
75
+
64
76
  end
77
+
65
78
  end
@@ -1,105 +1,107 @@
1
1
  require 'fileutils'
2
2
 
3
- module FileCrawler
4
- class Finder
5
- module Command
6
- module Move
7
-
8
- def move(destination)
9
- tap {
10
- @rows = move_with_numbering(@rows, destination)
11
- }
12
- end
3
+ module FileCrawler::Finder::Command
4
+
5
+ module Move
6
+
7
+ def move(destination, options={dry_run: true})
8
+ tap {
9
+ target = @collections.empty? ? @files : @collections
10
+ fixer = Fixer.new
11
+ @targets = fixer.make_new_path(target, destination)
13
12
 
14
- def move_from_collection(destination)
15
- tap {
16
- @rows = move_from_collection_with_numbering(@rows, destination)
13
+ if !options[:dry_run]
14
+ fixer.make_mv(@targets).each {|cmd|
15
+ exec(cmd)
17
16
  }
18
17
  end
18
+ }
19
+ end
19
20
 
20
- def move_with_numbering(source, destination)
21
- move_targets = []
22
- not_move_targets = []
23
- rename_targets = []
24
-
25
- source.each {|directory|
26
- if is_same?(directory, destination)
27
- not_move_targets << directory
28
- next
29
- end
21
+ def cmds
22
+ return nil if @targets.nil?
30
23
 
31
- if exist_file?(directory, destination)
32
- rename_targets << directory
33
- next
34
- end
24
+ fixer = Fixer.new
25
+ fixer.make_mv(@targets)
26
+ end
35
27
 
36
- if move_targets.include?(directory)
37
- rename_targets << directory
38
- next
39
- end
28
+ class Fixer
40
29
 
41
- move_targets << directory
42
- }
43
-
44
- create_directory_if_needed(destination)
45
- FileUtils.mv(move_targets, destination)
30
+ def make_new_path(sources, destination)
31
+ case sources
32
+ when Hash
33
+ return make_new_path_for_collection(sources, destination)
34
+ when Array
35
+ return make_new_path_for_array(sources, destination)
36
+ when String
37
+ return make_new_path_for_array([sources], destination)
38
+ end
46
39
 
47
- renamed_targets = []
48
- rename_targets.each {|directory|
49
- filename = find_free_filename(directory, destination)
50
- to = destination + '/' + filename
51
- FileUtils.mv(directory, to)
40
+ ArgumentError
41
+ end
52
42
 
53
- renamed_targets << to
54
- }
43
+ def make_mv(filepaths)
44
+ cmds = []
45
+ make_fixed_paths(filepaths).map {|file|
46
+ cmds << "mv #{file[0]} #{file[1]}"
47
+ }
55
48
 
56
- move_targets.map {|directory|
57
- destination + '/' + File.basename(directory)
58
- } + not_move_targets + renamed_targets
59
- end
49
+ cmds
50
+ end
60
51
 
61
- def move_from_collection_with_numbering(source, destination)
62
- result = source.map {|key, value|
63
- directory = destination + '/' + key
64
- move_with_numbering(value, directory)
65
- }.flatten
52
+ def make_fixed_paths(filepaths)
53
+ dest = []
54
+ filepaths.each {|filepath|
55
+ fixed_path = fix_path(filepath[1], dest)
56
+ filepath[1] = fixed_path
57
+ dest << fixed_path
58
+ }
66
59
 
67
- result
68
- end
60
+ filepaths
61
+ end
69
62
 
70
- def create_directory_if_needed(directory)
71
- Dir.mkdir(directory, 0777) unless File.exist?(directory)
72
- end
63
+ def fix_path(filepath, check_array, index=0)
64
+ newpath = filepath
65
+ newpath = "#{filepath} (#{index})" if index > 0
66
+ return fix_path(filepath, check_array, index + 1) if exist?(newpath, check_array)
73
67
 
74
- def find_free_filename(current, destination)
75
- filename = File.basename(current)
68
+ newpath
69
+ end
76
70
 
77
- index = 1
78
- new_filename = "#{filename} (#{index})"
79
- while exist_file?(new_filename, destination)
80
- index += 1
81
- new_filename = "#{filename} (#{index})"
82
- end
71
+ private
72
+ def exist?(filepath, check_array)
73
+ return true if File.exist?(filepath)
83
74
 
84
- new_filename
85
- end
75
+ check_array.include?(filepath)
76
+ end
86
77
 
87
- def valiable_to_move?(current, destination)
88
- return false if is_same?(current, destination)
78
+ def make_new_path_for_array(sources, destination)
79
+ result = []
80
+ sources.each {|path|
81
+ result << [path, new_path(path, destination)]
82
+ }
89
83
 
90
- !exist_file?(current, destination)
91
- end
84
+ result
85
+ end
92
86
 
93
- def is_same?(current, destination)
94
- current == destination
95
- end
87
+ def make_new_path_for_collection(sources, destination)
88
+ result = []
89
+ sources.each {|dirname, paths|
90
+ new_dir = destination + '/' + dirname.to_s
91
+ paths.each {|path|
92
+ result << [path, new_path(path, new_dir)]
93
+ }
94
+ }
96
95
 
97
- def exist_file?(current, destination)
98
- next_path = destination + '/' + File.basename(current)
99
- File.exist?(next_path)
100
- end
96
+ result
97
+ end
101
98
 
99
+ def new_path(source, destination)
100
+ destination + '/' + File.basename(source)
102
101
  end
102
+
103
103
  end
104
+
104
105
  end
106
+
105
107
  end
@@ -1,41 +1,33 @@
1
- module FileCrawler
2
- class Finder
3
- module Command
4
- module Search
1
+ module FileCrawler::Finder::Command
5
2
 
6
- def search(path, conditions = {})
7
- tap {
8
- @rows = search_directories(path)
9
- }
10
- end
11
-
12
- def search_directories(path)
13
- directories = search_directories_in_path(path)
14
- return [path] if directories.empty?
3
+ module Search
4
+ include Base
15
5
 
16
- result = []
17
- directories.each {|item|
18
- result += search_directories(item)
19
- }
6
+ def search(path, options={})
7
+ tap {
8
+ @files = search_directories(path, options)
9
+ }
10
+ end
20
11
 
21
- result
22
- end
12
+ def search_directories(path, options={})
13
+ result = []
14
+ cmd = "find #{path} -type d"
15
+ if options[:maxdepth] && options[:maxdepth] > 0
16
+ cmd = "find #{path} -maxdepth #{options[:maxdepth]} -type d"
17
+ end
23
18
 
24
- def search_directories_in_path(path)
25
- find_files_in_path(path).select {|item|
26
- File.directory?(item)
27
- }
19
+ exec(cmd).each_line(chomp: true) {|item|
20
+ valid = true
21
+ if options[:exclude_invisible_file]
22
+ filename = File.basename(item)
23
+ valid = !filename.start_with?('.')
28
24
  end
25
+ result << item if valid
26
+ }
29
27
 
30
- def find_files_in_path(path)
31
- Dir.entries(path).select {|item|
32
- !item.start_with?('.')
33
- }.map {|item|
34
- path + '/' + item
35
- }
36
- end
37
- end
28
+ result
38
29
  end
30
+
39
31
  end
40
32
 
41
33
  end
@@ -1,4 +1,20 @@
1
+
2
+ class FileCrawler::Finder
3
+
4
+ module Command
5
+
6
+ module Base
7
+
8
+ def exec(str)
9
+ `#{str}`
10
+ end
11
+
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+
1
18
  require_relative 'command/collect'
2
19
  require_relative 'command/move'
3
- require_relative 'command/resemble'
4
20
  require_relative 'command/search'
@@ -3,76 +3,16 @@ require_relative 'finder/command'
3
3
  module FileCrawler
4
4
 
5
5
  class Finder
6
- attr_reader :rows
6
+ attr_accessor :files, :collections
7
7
 
8
8
  include Command::Collect
9
9
  include Command::Move
10
- include Command::Resemble
11
10
  include Command::Search
12
11
 
13
12
  def initialize
14
13
  @rows = []
14
+ @collections = []
15
15
  end
16
16
  end
17
17
 
18
- # @param conditions
19
- # directory [Boolean]
20
- # exntesion [Array]
21
- # extension_in_directory [Array]
22
- def self.search(path, conditions = {})
23
- finder = FileCrawler::Finder.new
24
- finder.search(path, conditions)
25
-
26
- finder.rows
27
- end
28
-
29
- # conditions
30
- # - if dont have extension_in_directory, directory true
31
- # - move check? if include, need condition[:force]
32
- def self.move(path, destination, conditions = {})
33
- raise ArgumentError unless File.directory?(destination)
34
-
35
- finder = FileCrawler::Finder.new
36
- finder.search(path).move(destination)
37
-
38
- finder.rows
39
- end
40
-
41
- # conditions
42
- # - if dont have extension_in_directory, directory true
43
- def self.collect(path, conditions = {})
44
- finder = FileCrawler::Finder.new
45
- unless conditions[:regexs].nil?
46
- conditions[:regexs].each {|regex|
47
- finder.regexs << regex
48
- }
49
- end
50
-
51
- finder.search(path).collect(conditions)
52
-
53
- finder.rows
54
- end
55
-
56
- def self.organize(path, destination, conditions = {})
57
- finder = FileCrawler::Finder.new
58
-
59
- unless conditions[:regexs].nil?
60
- conditions[:regexs].each {|regex|
61
- finder.regexs << regex
62
- }
63
- end
64
-
65
- finder.search(path).collect(conditions).move_from_collection(destination)
66
-
67
- finder.rows
68
- end
69
-
70
- def self.resemble(path, conditions = {})
71
- finder = FileCrawler::Finder.new
72
-
73
- finder.search(path).collect(conditions).resemble()
74
-
75
- finder.rows
76
- end
77
-
78
18
  end
@@ -1,3 +1,3 @@
1
1
  module FileCrawler
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5.0"
3
3
  end
data/lib/file_crawler.rb CHANGED
@@ -1,2 +1,5 @@
1
1
  require "file_crawler/version"
2
2
  require "file_crawler/finder"
3
+
4
+ module FileCrawler
5
+ end
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.1
4
+ version: 0.5.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: 2017-03-08 00:00:00.000000000 Z
11
+ date: 2018-04-20 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/resemble.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
@@ -90,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
89
  version: '0'
91
90
  requirements: []
92
91
  rubyforge_project:
93
- rubygems_version: 2.5.1
92
+ rubygems_version: 2.7.3
94
93
  signing_key:
95
94
  specification_version: 4
96
95
  summary: FileCrawler searches and controls files in local directory
@@ -1,31 +0,0 @@
1
- module FileCrawler
2
- class Finder
3
- module Command
4
- module Resemble
5
-
6
- def resemble()
7
- tap {
8
- @rows = resemble_in_collection(@rows.values)
9
- }
10
- end
11
-
12
- # use rows after using #collect
13
- def resemble_in_collection(collection)
14
- files = []
15
-
16
- pattern = /\(\d+\)$/
17
-
18
- collection.each {|files_in_same_directory|
19
- files_in_same_directory.each {|file|
20
- filename = File.basename(file)
21
- files << file unless pattern.match(filename).nil?
22
- }
23
- }
24
-
25
- files
26
- end
27
-
28
- end
29
- end
30
- end
31
- end