epub-search 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8b13b7b323e7bac69c408b9fddae5c6a1032336b
4
- data.tar.gz: 5b365484dc01ebb3e354e9997121f0df75bbefa0
3
+ metadata.gz: 4ed411d6029d1a65837e0c574e68a698e069da58
4
+ data.tar.gz: e6d33b7d2c5d246ae7b5fbe66973fb058a3db5a6
5
5
  SHA512:
6
- metadata.gz: a8c64c2c72bd1907bc4b20c1a4e66e147c3165a463dc9bbf7dd7bf7604e990bb2b566d1ca4e69af559bbafc9a310d964c851498b5696df6b584bf6b8313d82f0
7
- data.tar.gz: 31067155ad835d1d717fadd98b64b0ede7d3accb9886d8a8dad7784fd5d08c5a83fd2a94ec1561757faa4476d6e7bbaf11c01515eaad5db89731f7c9aff8ae5b
6
+ metadata.gz: 0e1694e9a1adb8108eea54288e5117d3f062206ff1e39afb3f44103814add105ccb59bba79b3ecd91eade825ca01f598411bb3b346783fba04bfee6332295a87
7
+ data.tar.gz: bb52658436263d8b3d8647a0d905f347b3c96a3f4132ea0d58d23acd1f07d017374f898fc0b384201fcd4ce452f9ace0048b34a88ff1142a510202e0cfcf6566
@@ -4,6 +4,9 @@ Change Log
4
4
  0.0.3
5
5
  -----
6
6
  * Specify the version of Listen gem as '>= 1.0.0' to aopt to changed API
7
+ * Use monitor for thread safety
8
+ * Remove record for book which was removed during watcher was sleeping
9
+ * Add recored of book which was added during watcher was sleeping
7
10
 
8
11
  0.0.2
9
12
  -----
@@ -27,7 +27,7 @@ Usage
27
27
 
28
28
  To watch directories in backend:
29
29
 
30
- $ epub-search watch >/dev/null 2>&1 &
30
+ $ epub-search watch --daemonize
31
31
 
32
32
  Configuration
33
33
  -------------
@@ -13,11 +13,8 @@ class Watch
13
13
 
14
14
  def run(notify_on_change: true, daemonize: false, debug: false)
15
15
  @notify, @daemonize, @debug = notify_on_change, daemonize, debug
16
- if @debug
17
- $stderr.puts "notify_on_change: #{@notify}"
18
- $stderr.puts "daemonize: #{@daemonize}"
19
- $stderr.puts "debug: #{@debug}"
20
- end
16
+
17
+ log_run_parameters if @debug
21
18
 
22
19
  $PROGRAM_NAME = File.basename($PROGRAM_NAME)
23
20
  $stderr.puts 'start to watch:'
@@ -30,52 +27,7 @@ class Watch
30
27
  EPUB::Search::Database::Actor.supervise_as :db, @db
31
28
 
32
29
  catch_up
33
- begin
34
- Listen.to! *@directories, :filter => EPUB_RE do |modified, added, removed|
35
- modified.each do |file_path|
36
- next unless file_path =~ EPUB_RE
37
- file_path.force_encoding 'UTF-8'
38
- begin
39
- $stderr.puts "update #{file_path}"
40
- Celluloid::Actor[:db].async.update file_path
41
- title = EPUB::Parser.parse(file_path).title
42
- notify %Q|UPDATED: #{title}\n#{file_path}|
43
- FileUtils.touch exit_time_file
44
- rescue => error
45
- $stderr.puts error
46
- $stderr.puts error.backtrace if @debug
47
- end
48
- end
49
- added.each do |file_path|
50
- next unless file_path =~ EPUB_RE
51
- file_path.force_encoding 'UTF-8'
52
- begin
53
- Celluloid::Actor[:db].async.add file_path
54
- title = EPUB::Parser.parse(file_path).title
55
- notify %Q|ADDED: #{title}\n#{file_path}|
56
- FileUtils.touch exit_time_file
57
- rescue => error
58
- $stderr.puts error
59
- $stderr.puts error.backtrace if @debug
60
- end
61
- end
62
- removed.each do |file_path|
63
- next unless file_path =~ EPUB_RE
64
- file_path.force_encoding 'UTF-8'
65
- begin
66
- Celluloid::Actor[:db].async.remove file_path
67
- notify %Q|REMOVED:\n#{file_path}|
68
- FileUtils.touch exit_time_file
69
- rescue => error
70
- $stderr.puts error
71
- $stderr.puts error.backtrace if @debug
72
- end
73
- end
74
- end
75
- ensure
76
- pid_file.delete
77
- FileUtils.touch exit_time_file
78
- end
30
+ start_watch
79
31
  end
80
32
 
81
33
  private
@@ -97,26 +49,58 @@ class Watch
97
49
  end
98
50
 
99
51
  def catch_up
52
+ locations = @db.locations
100
53
  @directories.each do |dir|
101
54
  Dir["#{dir}/**/*.epub"].each do |file_path|
102
- next if File.file? exit_time_file and File.mtime(file_path) < exit_time
103
- begin
104
- removed = Celluloid::Actor[:db].remove(file_path)
105
- Celluloid::Actor[:db].async.add file_path
106
- operation = removed.zero? ? 'ADDED' : 'UPDATED'
107
- title = EPUB::Parser.parse(file_path).title
108
- notify "#{operation}: #{title}\n#{file_path}"
109
- FileUtils.touch exit_time_file
110
- rescue => error
111
- $stderr.puts error
112
- $stderr.puts error.backtrace if @debug
55
+ if locations.include? file_path
56
+ next if File.file? exit_time_file and File.mtime(file_path) < exit_time
57
+ begin
58
+ removed = Celluloid::Actor[:db].remove(file_path)
59
+ Celluloid::Actor[:db].async.add file_path
60
+ operation = removed.zero? ? 'ADDED' : 'UPDATED'
61
+ title = EPUB::Parser.parse(file_path).title
62
+ notify "#{operation}: #{title}\n#{file_path}"
63
+ FileUtils.touch exit_time_file
64
+ rescue => error
65
+ $stderr.puts error
66
+ $stderr.puts error.backtrace if @debug
67
+ end
68
+ else
69
+ on_file_changed file_path, :add, 'ADDED'
113
70
  end
114
71
  end
115
72
  end
116
- rescue
73
+ locations.each do |location|
74
+ on_file_changed location.dup, :remove, 'REMOVED' unless File.exist? location
75
+ end
76
+ rescue => err
77
+ $stderr.puts err
117
78
  pid_file.unlink
118
79
  end
119
80
 
81
+ def start_watch
82
+ Listen.to! *@directories, :filter => EPUB_RE do |modified, added, removed|
83
+ modified.each do |file_path|
84
+ on_file_changed file_path, :update, 'UPDATED'
85
+ end
86
+ added.each do |file_path|
87
+ on_file_changed file_path, :add, 'ADDED'
88
+ end
89
+ removed.each do |file_path|
90
+ on_file_changed file_path, :remove, 'REMOVED'
91
+ end
92
+ end
93
+ ensure
94
+ pid_file.delete
95
+ FileUtils.touch exit_time_file
96
+ end
97
+
98
+ def log_run_parameters
99
+ $stderr.puts "notify_on_change: #{@notify}"
100
+ $stderr.puts "daemonize: #{@daemonize}"
101
+ $stderr.puts "debug: #{@debug}"
102
+ end
103
+
120
104
  def write_pid_file
121
105
  if pid_file.exist?
122
106
  pid = pid_file.read.to_i
@@ -132,6 +116,19 @@ class Watch
132
116
  end
133
117
  end
134
118
 
119
+ def on_file_changed(file_path, operation, label)
120
+ file_path.force_encoding 'UTF-8'
121
+ begin
122
+ Celluloid::Actor[:db].async.__send__ operation, file_path
123
+ title = operation == :remove ? '' : EPUB::Parser.parse(file_path).title
124
+ notify %Q|#{label}: #{title}\n#{file_path}|
125
+ FileUtils.touch exit_time_file
126
+ rescue => error
127
+ $stderr.puts error
128
+ $stderr.puts error.backtrace if @debug
129
+ end
130
+ end
131
+
135
132
  def notify(message)
136
133
  $stderr.puts message
137
134
  Notify.notify $PROGRAM_NAME, message if notify?
@@ -1,12 +1,17 @@
1
+ require 'monitor'
2
+
1
3
  module EPUB
2
4
  module Search
3
5
  class Database
6
+ include MonitorMixin
7
+
4
8
  DIR_NAME = 'db'
5
9
  FILE_NAME = 'epub-search.db'
6
10
 
7
11
  attr_reader :db_dir
8
12
 
9
13
  def initialize(db_dir)
14
+ super()
10
15
  @db_dir = Pathname === db_dir ? db_dir : Pathname.new(db_dir)
11
16
  Groonga::Context.default_options = {:encoding => :utf8}
12
17
  end
@@ -49,22 +54,25 @@ module EPUB
49
54
  file_path = Pathname.new(file_path) unless file_path.kind_of? Pathname
50
55
  location = file_path.expand_path
51
56
  book = EPUB::Parser.parse(location)
52
- record_count = 0
53
- open do
54
- book.each_content do |content|
55
- next unless content.media_type == 'application/xhtml+xml'
56
- doc = Nokogiri.XML(content.read)
57
- page_title = doc.search('title').first.text
58
- body = Nokogiri.XML(doc.search('body').first.to_xml).content
59
- pages.add('location' => location.to_s,
60
- 'iri' => content.href.to_s,
61
- 'book_title' => book.title,
62
- 'page_title' => page_title,
63
- 'content' => body)
64
- record_count += 1
57
+ contents = book.each_content.select {|content|
58
+ content.media_type == 'application/xhtml+xml'
59
+ }
60
+ contents.each do |content|
61
+ doc = Nokogiri.XML(content.read)
62
+ title_elem = doc.search('title').first
63
+ page_title = title_elem ? title_elem.text : ''
64
+ body = Nokogiri.XML(doc.search('body').first.to_xml).content
65
+ synchronize do
66
+ open do
67
+ pages.add('location' => location.to_s,
68
+ 'iri' => content.href.to_s,
69
+ 'book_title' => book.title,
70
+ 'page_title' => page_title,
71
+ 'content' => body)
72
+ end
65
73
  end
66
74
  end
67
- record_count
75
+ contents.length
68
76
  end
69
77
 
70
78
  # @param [Pathname|String] file_path path of book
@@ -73,13 +81,15 @@ module EPUB
73
81
  file_path = Pathname.new(file_path) unless file_path.kind_of? Pathname
74
82
  location = file_path.expand_path
75
83
  record_count = 0
76
- open do
77
- records = pages.select {|record|
78
- record.location == location.to_path
79
- }
80
- records.each do |record|
81
- record.key.delete
82
- record_count += 1
84
+ synchronize do
85
+ open do
86
+ records = pages.select {|record|
87
+ record.location == location.to_path
88
+ }
89
+ records.each do |record|
90
+ record.key.delete
91
+ record_count += 1
92
+ end
83
93
  end
84
94
  end
85
95
  record_count
@@ -110,6 +120,12 @@ module EPUB
110
120
  end
111
121
  end
112
122
 
123
+ def locations
124
+ open do
125
+ pages.group_by(&:location).collect {|location, records| location}
126
+ end
127
+ end
128
+
113
129
  private
114
130
 
115
131
  def open
@@ -1,5 +1,5 @@
1
1
  module EPUB
2
2
  module Search
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epub-search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - KITAITI Makoto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-27 00:00:00.000000000 Z
11
+ date: 2013-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: epub-parser
@@ -275,7 +275,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
275
275
  version: '0'
276
276
  requirements: []
277
277
  rubyforge_project:
278
- rubygems_version: 2.0.0
278
+ rubygems_version: 2.0.2
279
279
  signing_key:
280
280
  specification_version: 4
281
281
  summary: Full text search for EPUB books