alexandria-book-collection-manager 0.7.1 → 0.7.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (210) hide show
  1. checksums.yaml +5 -5
  2. data/.github/dependabot.yml +9 -0
  3. data/.gitignore +5 -2
  4. data/.hound.yml +2 -0
  5. data/.rubocop.yml +113 -45
  6. data/.rubocop_todo.yml +82 -170
  7. data/.simplecov +5 -1
  8. data/.travis.yml +45 -0
  9. data/.yardopts +1 -1
  10. data/CHANGELOG.md +60 -0
  11. data/ChangeLog.0 +33 -35
  12. data/Gemfile +6 -5
  13. data/INSTALL.md +164 -0
  14. data/README.md +52 -42
  15. data/Rakefile +95 -109
  16. data/TODO.md +9 -1
  17. data/alexandria-book-collection-manager.gemspec +52 -45
  18. data/bin/alexandria +31 -53
  19. data/doc/AUTHORS +61 -0
  20. data/doc/BUGS +31 -0
  21. data/doc/FAQ +365 -0
  22. data/doc/HACKING +19 -0
  23. data/doc/NEWS +341 -0
  24. data/doc/alexandria.1 +120 -0
  25. data/doc/cuecat_support.rdoc +67 -0
  26. data/doc/dependency_decisions.yml +80 -0
  27. data/lib/alexandria.rb +29 -37
  28. data/lib/alexandria/about.rb +52 -51
  29. data/lib/alexandria/book_providers.rb +94 -101
  30. data/lib/alexandria/book_providers/adlibris.rb +45 -85
  31. data/lib/alexandria/book_providers/amazon_aws.rb +105 -113
  32. data/lib/alexandria/book_providers/amazon_ecs_util.rb +293 -324
  33. data/lib/alexandria/book_providers/barnes_and_noble.rb +54 -53
  34. data/lib/alexandria/book_providers/douban.rb +29 -51
  35. data/lib/alexandria/book_providers/proxis.rb +42 -59
  36. data/lib/alexandria/book_providers/pseudomarc.rb +79 -99
  37. data/lib/alexandria/book_providers/siciliano.rb +68 -70
  38. data/lib/alexandria/book_providers/thalia.rb +46 -45
  39. data/lib/alexandria/book_providers/web.rb +17 -33
  40. data/lib/alexandria/book_providers/worldcat.rb +74 -102
  41. data/lib/alexandria/book_providers/z3950.rb +170 -174
  42. data/lib/alexandria/config.rb +5 -3
  43. data/lib/alexandria/console.rb +10 -21
  44. data/lib/alexandria/default_preferences.rb +37 -0
  45. data/lib/alexandria/execution_queue.rb +17 -15
  46. data/lib/alexandria/export_format.rb +47 -0
  47. data/lib/alexandria/export_library.rb +188 -302
  48. data/lib/alexandria/import_library.rb +114 -155
  49. data/lib/alexandria/import_library_csv.rb +46 -96
  50. data/lib/alexandria/library_collection.rb +79 -0
  51. data/lib/alexandria/library_sort_order.rb +45 -0
  52. data/lib/alexandria/library_store.rb +233 -0
  53. data/lib/alexandria/logging.rb +15 -19
  54. data/lib/alexandria/models/book.rb +15 -20
  55. data/lib/alexandria/models/library.rb +81 -363
  56. data/lib/alexandria/net.rb +7 -6
  57. data/lib/alexandria/preferences.rb +73 -91
  58. data/lib/alexandria/scanners.rb +4 -2
  59. data/lib/alexandria/scanners/{cuecat.rb → cue_cat.rb} +24 -20
  60. data/lib/alexandria/scanners/keyboard.rb +10 -8
  61. data/lib/alexandria/smart_library.rb +135 -171
  62. data/lib/alexandria/ui.rb +17 -15
  63. data/lib/alexandria/ui/about_dialog.rb +49 -0
  64. data/lib/alexandria/ui/{dialogs/acquire_dialog.rb → acquire_dialog.rb} +129 -152
  65. data/lib/alexandria/ui/alert_dialog.rb +64 -0
  66. data/lib/alexandria/ui/bad_isbns_dialog.rb +41 -0
  67. data/lib/alexandria/ui/{dialogs/barcode_animation.rb → barcode_animation.rb} +18 -15
  68. data/lib/alexandria/ui/{dialogs/book_properties_dialog.rb → book_properties_dialog.rb} +44 -61
  69. data/lib/alexandria/ui/{dialogs/book_properties_dialog_base.rb → book_properties_dialog_base.rb} +84 -89
  70. data/lib/alexandria/ui/builder_base.rb +9 -27
  71. data/lib/alexandria/ui/callbacks.rb +188 -186
  72. data/lib/alexandria/ui/columns.rb +2 -0
  73. data/lib/alexandria/ui/completion_models.rb +12 -23
  74. data/lib/alexandria/ui/confirm_erase_dialog.rb +33 -0
  75. data/lib/alexandria/ui/conflict_while_copying_dialog.rb +34 -0
  76. data/lib/alexandria/ui/dndable.rb +10 -8
  77. data/lib/alexandria/ui/error_dialog.rb +25 -0
  78. data/lib/alexandria/ui/export_dialog.rb +139 -0
  79. data/lib/alexandria/ui/icons.rb +49 -65
  80. data/lib/alexandria/ui/iconview.rb +15 -13
  81. data/lib/alexandria/ui/iconview_tooltips.rb +43 -58
  82. data/lib/alexandria/ui/import_dialog.rb +157 -0
  83. data/lib/alexandria/ui/init.rb +23 -33
  84. data/lib/alexandria/ui/keep_bad_isbn_dialog.rb +36 -0
  85. data/lib/alexandria/ui/libraries_combo.rb +18 -14
  86. data/lib/alexandria/ui/listview.rb +77 -88
  87. data/lib/alexandria/ui/main_app.rb +26 -26
  88. data/lib/alexandria/ui/misc_dialogs.rb +10 -0
  89. data/lib/alexandria/ui/multi_drag_treeview.rb +30 -41
  90. data/lib/alexandria/ui/{dialogs/new_book_dialog.rb → new_book_dialog.rb} +168 -215
  91. data/lib/alexandria/ui/new_book_dialog_manual.rb +139 -0
  92. data/lib/alexandria/ui/new_provider_dialog.rb +100 -0
  93. data/lib/alexandria/ui/new_smart_library_dialog.rb +74 -0
  94. data/lib/alexandria/ui/preferences_dialog.rb +313 -0
  95. data/lib/alexandria/ui/provider_preferences_base_dialog.rb +95 -0
  96. data/lib/alexandria/ui/provider_preferences_dialog.rb +35 -0
  97. data/lib/alexandria/ui/really_delete_dialog.rb +53 -0
  98. data/lib/alexandria/ui/{sidepane.rb → sidepane_manager.rb} +62 -72
  99. data/lib/alexandria/ui/skip_entry_dialog.rb +33 -0
  100. data/lib/alexandria/ui/smart_library_properties_dialog.rb +60 -0
  101. data/lib/alexandria/ui/{dialogs/smart_library_properties_dialog_base.rb → smart_library_properties_dialog_base.rb} +96 -172
  102. data/lib/alexandria/ui/smart_library_rule_box.rb +119 -0
  103. data/lib/alexandria/ui/sound.rb +13 -13
  104. data/lib/alexandria/ui/ui_manager.rb +262 -283
  105. data/lib/alexandria/undo_manager.rb +3 -0
  106. data/lib/alexandria/version.rb +6 -19
  107. data/lib/alexandria/web_themes.rb +24 -21
  108. data/po/Makefile +2 -2
  109. data/po/cs.po +993 -880
  110. data/po/cy.po +957 -874
  111. data/po/de.po +990 -869
  112. data/po/el.po +989 -869
  113. data/po/es.po +985 -865
  114. data/po/fr.po +986 -870
  115. data/po/ga.po +907 -823
  116. data/po/gl.po +981 -865
  117. data/po/it.po +986 -868
  118. data/po/ja.po +969 -853
  119. data/po/mk.po +983 -863
  120. data/po/nb.po +979 -863
  121. data/po/nl.po +983 -864
  122. data/po/pl.po +1020 -969
  123. data/po/pt.po +988 -861
  124. data/po/pt_BR.po +984 -868
  125. data/po/ru.po +992 -873
  126. data/po/sk.po +987 -869
  127. data/po/sv.po +977 -861
  128. data/po/uk.po +975 -865
  129. data/po/zh_TW.po +976 -860
  130. data/schemas/alexandria.schemas +25 -3
  131. data/share/alexandria/glade/acquire_dialog__builder.glade +15 -12
  132. data/share/alexandria/glade/book_properties_dialog__builder.glade +171 -299
  133. data/share/alexandria/glade/main_app__builder.glade +24 -33
  134. data/share/alexandria/glade/new_book_dialog__builder.glade +27 -59
  135. data/share/alexandria/glade/preferences_dialog__builder.glade +250 -290
  136. data/share/gnome/help/alexandria/C/introduction.xml +0 -8
  137. data/share/gnome/help/alexandria/C/searching.xml +1 -1
  138. data/share/gnome/help/alexandria/C/smart-libraries.xml +2 -2
  139. data/share/gnome/help/alexandria/C/working-with-libraries.xml +1 -1
  140. data/share/gnome/help/alexandria/fr/alexandria.xml +1 -1
  141. data/share/gnome/help/alexandria/ja/introduction.xml +0 -8
  142. data/share/gnome/help/alexandria/ja/smart-libraries.xml +1 -1
  143. data/spec/alexandria/book_providers/world_cat_provider_spec.rb +160 -0
  144. data/spec/alexandria/book_providers_spec.rb +77 -210
  145. data/spec/alexandria/book_spec.rb +16 -12
  146. data/spec/alexandria/console_spec.rb +27 -0
  147. data/spec/alexandria/export_library_spec.rb +130 -0
  148. data/spec/alexandria/library_spec.rb +130 -172
  149. data/spec/alexandria/library_store_spec.rb +37 -0
  150. data/spec/alexandria/preferences_spec.rb +46 -17
  151. data/spec/alexandria/scanners/cue_cat_spec.rb +52 -0
  152. data/spec/alexandria/smart_library_spec.rb +32 -25
  153. data/spec/alexandria/ui/about_dialog_spec.rb +14 -0
  154. data/spec/alexandria/ui/acquire_dialog_spec.rb +14 -0
  155. data/spec/alexandria/ui/alert_dialog_spec.rb +16 -0
  156. data/spec/alexandria/ui/bad_isbns_dialog_spec.rb +14 -0
  157. data/spec/alexandria/ui/book_properties_dialog_spec.rb +17 -0
  158. data/spec/alexandria/ui/confirm_erase_dialog_spec.rb +14 -0
  159. data/spec/alexandria/ui/conflict_while_copying_dialog_spec.rb +16 -0
  160. data/spec/alexandria/ui/error_dialog_spec.rb +14 -0
  161. data/spec/alexandria/ui/export_dialog_spec.rb +15 -0
  162. data/spec/alexandria/ui/icons_spec.rb +26 -0
  163. data/spec/alexandria/ui/iconview_spec.rb +9 -21
  164. data/spec/alexandria/ui/import_dialog_spec.rb +41 -0
  165. data/spec/alexandria/ui/keep_bad_isbn_dialog_spec.rb +17 -0
  166. data/spec/alexandria/ui/main_app_spec.rb +8 -33
  167. data/spec/alexandria/ui/new_book_dialog_manual_spec.rb +15 -0
  168. data/spec/alexandria/ui/new_book_dialog_spec.rb +22 -0
  169. data/spec/alexandria/ui/new_provider_dialog_spec.rb +30 -0
  170. data/spec/alexandria/ui/new_smart_library_dialog_spec.rb +39 -0
  171. data/spec/alexandria/ui/preferences_dialog_spec.rb +14 -0
  172. data/spec/alexandria/ui/provider_preferences_dialog_spec.rb +34 -0
  173. data/spec/alexandria/ui/really_delete_dialog_spec.rb +16 -0
  174. data/spec/alexandria/ui/sidepane_manager_spec.rb +15 -0
  175. data/spec/alexandria/ui/skip_entry_dialog_spec.rb +14 -0
  176. data/spec/alexandria/ui/smart_library_properties_dialog_spec.rb +32 -0
  177. data/spec/alexandria/ui/sound_spec.rb +4 -2
  178. data/spec/alexandria/ui/ui_manager_spec.rb +45 -20
  179. data/spec/end_to_end/basic_run_spec.rb +57 -0
  180. data/spec/spec_helper.rb +66 -33
  181. data/tasks/setup.rb +5 -3
  182. data/tasks/spec.rake +18 -3
  183. data/util/rake/fileinstall.rb +38 -40
  184. data/util/rake/gettextgenerate.rb +15 -70
  185. data/util/rake/omfgenerate.rb +10 -10
  186. metadata +176 -60
  187. data/INSTALL.rdoc +0 -148
  188. data/dogtail/basic_run_test.py +0 -9
  189. data/lib/alexandria/book_providers/bol_it.rb +0 -160
  190. data/lib/alexandria/book_providers/deastore.rb +0 -273
  191. data/lib/alexandria/book_providers/ibs_it.rb +0 -147
  192. data/lib/alexandria/book_providers/mcu.rb +0 -169
  193. data/lib/alexandria/book_providers/renaud.rb +0 -140
  194. data/lib/alexandria/book_providers/webster_it.rb +0 -167
  195. data/lib/alexandria/ui/dialogs/about_dialog.rb +0 -59
  196. data/lib/alexandria/ui/dialogs/alert_dialog.rb +0 -70
  197. data/lib/alexandria/ui/dialogs/bad_isbns_dialog.rb +0 -43
  198. data/lib/alexandria/ui/dialogs/export_dialog.rb +0 -171
  199. data/lib/alexandria/ui/dialogs/import_dialog.rb +0 -196
  200. data/lib/alexandria/ui/dialogs/misc_dialogs.rb +0 -85
  201. data/lib/alexandria/ui/dialogs/new_book_dialog_manual.rb +0 -154
  202. data/lib/alexandria/ui/dialogs/new_smart_library_dialog.rb +0 -74
  203. data/lib/alexandria/ui/dialogs/preferences_dialog.rb +0 -578
  204. data/lib/alexandria/ui/dialogs/smart_library_properties_dialog.rb +0 -57
  205. data/spec/alexandria/scanners/cuecat_spec.rb +0 -65
  206. data/spec/alexandria/ui/dialogs_spec.rb +0 -94
  207. data/spec/alexandria/ui/sidepane_spec.rb +0 -27
  208. data/spec/alexandria/ui/ui_utilities_spec.rb +0 -60
  209. data/spec/alexandria/utilities_spec.rb +0 -50
  210. data/tasks/dogtail.rake +0 -4
@@ -1,29 +1,15 @@
1
- # Copyright (C) 2004-2006 Laurent Sansonetti
2
- # Copyright (C) 2014 Matijs van Zuijlen
3
- #
4
- # Alexandria is free software; you can redistribute it and/or
5
- # modify it under the terms of the GNU General Public License as
6
- # published by the Free Software Foundation; either version 2 of the
7
- # License, or (at your option) any later version.
8
- #
9
- # Alexandria is distributed in the hope that it will be useful,
10
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
- # General Public License for more details.
1
+ # frozen_string_literal: true
2
+
3
+ # This file is part of Alexandria.
13
4
  #
14
- # You should have received a copy of the GNU General Public
15
- # License along with Alexandria; see the file COPYING. If not,
16
- # write to the Free Software Foundation, Inc., 51 Franklin Street,
17
- # Fifth Floor, Boston, MA 02110-1301 USA.
18
-
19
- require 'yaml'
20
- require 'fileutils'
21
- require 'rexml/document'
22
- require 'tempfile'
23
- require 'etc'
24
- require 'open-uri'
25
- require 'observer'
26
- require 'singleton'
5
+ # See the file README.md for authorship and licensing information.
6
+
7
+ require "yaml"
8
+ require "fileutils"
9
+ require "rexml/document"
10
+ require "tempfile"
11
+ require "etc"
12
+ require "alexandria/library_store"
27
13
 
28
14
  module Alexandria
29
15
  class Library < Array
@@ -31,18 +17,19 @@ module Alexandria
31
17
 
32
18
  attr_reader :name
33
19
  attr_accessor :ruined_books, :updating, :deleted_books
34
- DIR = File.join(ENV['HOME'], '.alexandria')
35
- EXT = { book: '.yaml', cover: '.cover' }.freeze
20
+
21
+ DEFAULT_DIR = File.join(ENV["HOME"], ".alexandria")
22
+ EXT = { book: ".yaml", cover: ".cover" }.freeze
36
23
 
37
24
  include GetText
38
25
  extend GetText
39
- bindtextdomain(Alexandria::TEXTDOMAIN, charset: 'UTF-8')
26
+ bindtextdomain(Alexandria::TEXTDOMAIN, charset: "UTF-8")
40
27
 
41
28
  BOOK_ADDED, BOOK_UPDATED, BOOK_REMOVED = (0..3).to_a
42
29
  include Observable
43
30
 
44
31
  def path
45
- File.join(DIR, @name)
32
+ File.join(@store.library_dir, @name)
46
33
  end
47
34
 
48
35
  def updating?
@@ -50,198 +37,19 @@ module Alexandria
50
37
  end
51
38
 
52
39
  def self.generate_new_name(existing_libraries,
53
- from_base = _('Untitled'))
40
+ from_base = _("Untitled"))
54
41
  i = 1
55
42
  name = nil
56
43
  all_libraries = existing_libraries + @@deleted_libraries
57
44
  loop do
58
45
  name = i == 1 ? from_base : from_base + " #{i}"
59
46
  break unless all_libraries.find { |x| x.name == name }
47
+
60
48
  i += 1
61
49
  end
62
50
  name
63
51
  end
64
52
 
65
- FIX_BIGNUM_REGEX =
66
- /loaned_since:\s*(\!ruby\/object\:Bignum\s*)?(\d+)\n/
67
-
68
- def self.load(name)
69
- test = [0, nil]
70
- ruined_books = []
71
- library = Library.new(name)
72
- FileUtils.mkdir_p(library.path) unless File.exist?(library.path)
73
- Dir.chdir(library.path) do
74
- Dir['*' + EXT[:book]].each do |filename|
75
- test[1] = filename if (test[0]).zero?
76
-
77
- unless File.size? test[1]
78
- log.warn { "Book file #{test[1]} was empty" }
79
- md = /([\dxX]{10,13})#{EXT[:book]}/.match(filename)
80
- if md
81
- file_isbn = md[1]
82
- ruined_books << [nil, file_isbn, library]
83
- else
84
- log.warn { "Filename #{filename} does not contain an ISBN" }
85
- # TODO: delete this file...
86
- end
87
- next
88
- end
89
- book = regularize_book_from_yaml(test[1])
90
- old_isbn = book.isbn
91
- old_pub_year = book.publishing_year
92
- begin
93
- begin
94
- book.isbn = canonicalise_ean(book.isbn).to_s unless book.isbn.nil?
95
- raise "Not a book: #{book.inspect}" unless book.is_a?(Book)
96
- rescue InvalidISBNError
97
- book.isbn = old_isbn
98
- end
99
-
100
- book.publishing_year = book.publishing_year.to_i unless book.publishing_year.nil?
101
-
102
- # Or if isbn has changed
103
- raise "#{test[1]} isbn is not okay" unless book.isbn == old_isbn
104
-
105
- # Re-save book if Alexandria::DATA_VERSION changes
106
- raise "#{test[1]} version is not okay" unless book.version == Alexandria::DATA_VERSION
107
-
108
- # Or if publishing year has changed
109
- raise "#{test[1]} pub year is not okay" unless book.publishing_year == old_pub_year
110
-
111
- # ruined_books << [book, book.isbn, library]
112
- book.library = library.name
113
-
114
- ## TODO copy cover image file, if necessary
115
- # due to #26909 cover files for books without ISBN are re-saved as "g#{ident}.cover"
116
- if book.isbn.nil? || book.isbn.empty?
117
- if File.exist? library.old_cover(book)
118
- log.debug { "#{library.name}; book #{book.title} has no ISBN, fixing cover image" }
119
- FileUtils::Verbose.mv(library.old_cover(book), library.cover(book))
120
- end
121
- end
122
-
123
- library << book
124
- rescue
125
- book.version = Alexandria::DATA_VERSION
126
- savedfilename = library.simple_save(book)
127
- test[0] = test[0] + 1
128
- test[1] = savedfilename
129
-
130
- # retries the Dir.each block...
131
- # but gives up after three tries
132
- redo unless test[0] > 2
133
-
134
- else
135
- test = [0, nil]
136
- end
137
- end
138
-
139
- # Since 0.4.0 the cover files '_small.jpg' and
140
- # '_medium.jpg' have been deprecated for a single medium
141
- # cover file named '.cover'.
142
-
143
- Dir['*' + '_medium.jpg'].each do |medium_cover|
144
- begin
145
- FileUtils.mv(medium_cover,
146
- medium_cover.sub(/_medium\.jpg$/,
147
- EXT[:cover]))
148
- rescue
149
- end
150
- end
151
-
152
- Dir['*' + EXT[:cover]].each do |cover|
153
- next if cover[0] == 'g'
154
- md = /(.+)\.cover/.match(cover)
155
- begin
156
- ean = canonicalise_ean(md[1])
157
- rescue
158
- ean = md[1]
159
- end
160
- begin
161
- FileUtils.mv(cover, ean + EXT[:cover]) unless cover == ean + EXT[:cover]
162
- rescue
163
- end
164
- end
165
-
166
- FileUtils.rm_f(Dir['*_small.jpg'])
167
- end
168
- library.ruined_books = ruined_books
169
-
170
- library
171
- end
172
-
173
- def self.regularize_book_from_yaml(name)
174
- text = IO.read(name)
175
-
176
- # Code to remove the mystery string in books imported from Amazon
177
- # (In the past, still?) To allow ruby-amazon to be removed.
178
-
179
- # The string is removed on load, but can't make it stick, maybe has to do with cache
180
-
181
- if text =~ /!str:Amazon::Search::Response/
182
- log.debug { "Removing Ruby/Amazon strings from #{name}" }
183
- text.gsub!('!str:Amazon::Search::Response', '')
184
- end
185
-
186
- # Backward compatibility with versions <= 0.6.0, where the
187
- # loaned_since field was a numeric.
188
- if (md = FIX_BIGNUM_REGEX.match(text))
189
- new_yaml = Time.at(md[2].to_i).to_yaml
190
- # Remove the "---" prefix.
191
- new_yaml.sub!(/^\s*\-+\s*/, '')
192
- text.sub!(md[0], "loaned_since: #{new_yaml}\n")
193
- end
194
-
195
- # TODO: Ensure book loading passes through Book#initialize
196
- book = YAML.load(text)
197
-
198
- unless book.isbn.class == String
199
- # HACK
200
- md = /isbn: (.+)/.match(text)
201
- if md
202
- string_isbn = md[1].strip
203
- book.isbn = string_isbn
204
- end
205
- end
206
-
207
- # another HACK of the same type as above
208
- unless book.saved_ident.class == String
209
-
210
- md2 = /saved_ident: (.+)/.match(text)
211
- if md2
212
- string_saved_ident = md2[1].strip
213
- log.debug { "fixing saved_ident #{book.saved_ident} -> #{string_saved_ident}" }
214
- book.saved_ident = string_saved_ident
215
- end
216
- end
217
- if (book.isbn.class == String) && book.isbn.empty?
218
- book.isbn = nil # save trouble later
219
- end
220
- book
221
- end
222
-
223
- def self.loadall
224
- a = []
225
- begin
226
- Dir.entries(DIR).each do |file|
227
- # Skip hidden files.
228
- next if file =~ /^\./
229
- # Skip non-directory files.
230
- next unless File.stat(File.join(DIR, file)).directory?
231
-
232
- a << load(file)
233
- end
234
-
235
- rescue Errno::ENOENT
236
- FileUtils.mkdir_p(DIR)
237
- end
238
- # Create the default library if there is no library yet.
239
-
240
- a << load(_('My Library')) if a.empty?
241
-
242
- a
243
- end
244
-
245
53
  def self.move(source_library, dest_library, *books)
246
54
  dest = dest_library.path
247
55
  books.each do |book|
@@ -263,23 +71,14 @@ module Alexandria
263
71
  end
264
72
  end
265
73
 
266
- class NoISBNError < StandardError
267
- end
74
+ def self.extract_numbers(entry)
75
+ return [] if entry.nil? || entry.empty?
268
76
 
269
- class InvalidISBNError < StandardError
270
- attr_reader :isbn
271
- def initialize(isbn = nil)
272
- super()
273
- @isbn = isbn
274
- end
275
- end
276
-
277
- def self.extract_numbers(isbn)
278
- raise NoISBNError, 'Nil ISBN' if isbn.nil? || isbn.empty?
77
+ normalized = entry.delete("- ").upcase
78
+ return [] unless /\A[\dX]*\Z/.match?(normalized)
279
79
 
280
- isbn.delete('- ').upcase.split('').map do |x|
281
- raise InvalidISBNError, isbn unless x =~ /[\dX]/
282
- x == 'X' ? 10 : x.to_i
80
+ normalized.split("").map do |char|
81
+ char == "X" ? 10 : char.to_i
283
82
  end
284
83
  end
285
84
 
@@ -288,19 +87,17 @@ module Alexandria
288
87
  accumulator + numbers[i] * (i + 1)
289
88
  end % 11
290
89
 
291
- sum == 10 ? 'X' : sum
90
+ sum == 10 ? "X" : sum
292
91
  end
293
92
 
294
93
  def self.valid_isbn?(isbn)
295
94
  numbers = extract_numbers(isbn)
296
95
  (numbers.length == 10) && isbn_checksum(numbers).zero?
297
- rescue InvalidISBNError
298
- false
299
96
  end
300
97
 
301
98
  def self.ean_checksum(numbers)
302
- -(numbers.values_at(1, 3, 5, 7, 9, 11).reduce(:+) * 3 +
303
- numbers.values_at(0, 2, 4, 6, 8, 10).reduce(:+)) % 10
99
+ -(numbers.values_at(1, 3, 5, 7, 9, 11).sum * 3 +
100
+ numbers.values_at(0, 2, 4, 6, 8, 10).sum) % 10
304
101
  end
305
102
 
306
103
  def self.valid_ean?(ean)
@@ -309,36 +106,32 @@ module Alexandria
309
106
  (ean_checksum(numbers[0..11]) == numbers[12])) ||
310
107
  ((numbers.length == 18) &&
311
108
  (ean_checksum(numbers[0..11]) == numbers[12]))
312
- rescue InvalidISBNError
313
- false
314
109
  end
315
110
 
316
111
  def self.upc_checksum(numbers)
317
- -(numbers.values_at(0, 2, 4, 6, 8, 10).reduce(:+) * 3 +
318
- numbers.values_at(1, 3, 5, 7, 9).reduce(:+)) % 10
112
+ -(numbers.values_at(0, 2, 4, 6, 8, 10).sum * 3 +
113
+ numbers.values_at(1, 3, 5, 7, 9).sum) % 10
319
114
  end
320
115
 
321
116
  def self.valid_upc?(upc)
322
117
  numbers = extract_numbers(upc)
323
118
  ((numbers.length == 17) &&
324
119
  (upc_checksum(numbers[0..10]) == numbers[11]))
325
- rescue InvalidISBNError
326
- false
327
120
  end
328
121
 
329
122
  AMERICAN_UPC_LOOKUP = {
330
- '014794' => '08041', '018926' => '0445', '02778' => '0449',
331
- '037145' => '0812', '042799' => '0785', '043144' => '0688',
332
- '044903' => '0312', '045863' => '0517', '046594' => '0064',
333
- '047132' => '0152', '051487' => '08167', '051488' => '0140',
334
- '060771' => '0002', '065373' => '0373', '070992' => '0523',
335
- '070993' => '0446', '070999' => '0345', '071001' => '0380',
336
- '071009' => '0440', '071125' => '088677', '071136' => '0451',
337
- '071149' => '0451', '071152' => '0515', '071162' => '0451',
338
- '071268' => '08217', '071831' => '0425', '071842' => '08439',
339
- '072742' => '0441', '076714' => '0671', '076783' => '0553',
340
- '076814' => '0449', '078021' => '0872', '079808' => '0394',
341
- '090129' => '0679', '099455' => '0061', '099769' => '0451'
123
+ "014794" => "08041", "018926" => "0445", "02778" => "0449",
124
+ "037145" => "0812", "042799" => "0785", "043144" => "0688",
125
+ "044903" => "0312", "045863" => "0517", "046594" => "0064",
126
+ "047132" => "0152", "051487" => "08167", "051488" => "0140",
127
+ "060771" => "0002", "065373" => "0373", "070992" => "0523",
128
+ "070993" => "0446", "070999" => "0345", "071001" => "0380",
129
+ "071009" => "0440", "071125" => "088677", "071136" => "0451",
130
+ "071149" => "0451", "071152" => "0515", "071162" => "0451",
131
+ "071268" => "08217", "071831" => "0425", "071842" => "08439",
132
+ "072742" => "0441", "076714" => "0671", "076783" => "0553",
133
+ "076814" => "0449", "078021" => "0872", "079808" => "0394",
134
+ "090129" => "0679", "099455" => "0061", "099769" => "0451"
342
135
  }.freeze
343
136
 
344
137
  def self.upc_convert(upc)
@@ -347,27 +140,23 @@ module Alexandria
347
140
  end
348
141
 
349
142
  def self.canonicalise_ean(code)
350
- code = code.to_s.delete('- ')
143
+ code = code.to_s.delete("- ")
351
144
  if valid_ean?(code)
352
- return code
145
+ code
353
146
  elsif valid_isbn?(code)
354
- code = '978' + code[0..8]
355
- return code + String(ean_checksum(extract_numbers(code)))
147
+ code = "978" + code[0..8]
148
+ code + String(ean_checksum(extract_numbers(code)))
356
149
  elsif valid_upc?(code)
357
150
  isbn10 = canonicalise_isbn
358
- code = '978' + isbn10[0..8]
359
- return code + String(ean_checksum(extract_numbers(code)))
360
- ## raise "fix function Alexandria::Library.canonicalise_ean"
361
- else
362
- raise InvalidISBNError, code
151
+ code = "978" + isbn10[0..8]
152
+ code + String(ean_checksum(extract_numbers(code)))
363
153
  end
364
154
  end
365
155
 
366
156
  def self.canonicalise_isbn(isbn)
367
157
  numbers = extract_numbers(isbn)
368
- if valid_ean?(isbn) && (numbers[0..2] != [9, 7, 8])
369
- return isbn
370
- end
158
+ return isbn if valid_ean?(isbn) && (numbers[0..2] != [9, 7, 8])
159
+
371
160
  canonical = if valid_ean?(isbn)
372
161
  # Looks like an EAN number -- extract the intersting part and
373
162
  # calculate a checksum. It would be nice if we could validate
@@ -381,10 +170,10 @@ module Alexandria
381
170
  elsif valid_isbn?(isbn)
382
171
  # Seems to be a valid ISBN number.
383
172
  numbers[0..-2] + [isbn_checksum(numbers[0..-2])]
384
- else
385
- raise InvalidISBNError, isbn
386
173
  end
387
174
 
175
+ return unless canonical
176
+
388
177
  canonical.map(&:to_s).join
389
178
  end
390
179
 
@@ -392,23 +181,17 @@ module Alexandria
392
181
  # Let's initialize the saved identifier if not already
393
182
  # (backward compatibility from 0.4.0)
394
183
  # book.saved_ident ||= book.ident
395
- if book.saved_ident.nil? || book.saved_ident.empty?
396
- book.saved_ident = book.ident
397
- end
184
+ book.saved_ident = book.ident if book.saved_ident.nil? || book.saved_ident.empty?
398
185
  if book.ident != book.saved_ident
399
- # log.debug { "Backwards compatibility step: #{book.saved_ident.inspect}, #{book.ident.inspect}" }
400
186
  FileUtils.rm(yaml(book.saved_ident))
401
- end
402
- if File.exist?(cover(book.saved_ident))
403
- begin
187
+ if File.exist?(cover(book.saved_ident))
404
188
  FileUtils.mv(cover(book.saved_ident), cover(book.ident))
405
- rescue
406
189
  end
407
190
  end
408
191
  book.saved_ident = book.ident
409
192
 
410
- filename = book.saved_ident.to_s + '.yaml'
411
- File.open(filename, 'w') { |io| io.puts book.to_yaml }
193
+ filename = book.saved_ident.to_s + ".yaml"
194
+ File.open(filename, "w") { |io| io.puts book.to_yaml }
412
195
  filename
413
196
  end
414
197
 
@@ -436,14 +219,14 @@ module Alexandria
436
219
 
437
220
  temp_book = book.dup
438
221
  temp_book.library = nil
439
- File.open(yaml(temp_book), 'w') { |io| io.puts temp_book.to_yaml }
222
+ File.open(yaml(temp_book), "w") { |io| io.puts temp_book.to_yaml }
440
223
 
441
224
  # Do not notify twice.
442
- if changed?
443
- notify_observers(self,
444
- already_there ? BOOK_UPDATED : BOOK_ADDED,
445
- book)
446
- end
225
+ return unless changed?
226
+
227
+ notify_observers(self,
228
+ already_there ? BOOK_UPDATED : BOOK_ADDED,
229
+ book)
447
230
  end
448
231
 
449
232
  def transport
@@ -455,7 +238,7 @@ module Alexandria
455
238
  Dir.chdir(path) do
456
239
  # Fetch the cover picture.
457
240
  cover_file = cover(book)
458
- File.open(cover_file, 'w') do |io|
241
+ File.open(cover_file, "w") do |io|
459
242
  uri = URI.parse(cover_uri)
460
243
  if uri.scheme.nil?
461
244
  # Regular filename.
@@ -467,9 +250,7 @@ module Alexandria
467
250
  end
468
251
 
469
252
  # Remove the file if its blank.
470
- if Alexandria::UI::Icons.blank?(cover_file)
471
- File.delete(cover_file)
472
- end
253
+ File.delete(cover_file) if Alexandria::UI::Icons.blank?(cover_file)
473
254
  end
474
255
  end
475
256
 
@@ -498,11 +279,15 @@ module Alexandria
498
279
  if book.nil?
499
280
  # Delete the whole library.
500
281
  raise if @@deleted_libraries.include?(self)
282
+
501
283
  @@deleted_libraries << self
502
284
  else
503
285
  if @deleted_books.include?(book)
504
- doubles = @deleted_books.reject { |b| !b.equal?(book) }
505
- raise ArgumentError, "Book #{book.isbn} was already deleted" unless doubles.empty?
286
+ doubles = @deleted_books.select { |b| b.equal?(book) }
287
+ unless doubles.empty?
288
+ raise ArgumentError, format(_("Book %<isbn>s was already deleted"),
289
+ isbn: book.isbn)
290
+ end
506
291
  end
507
292
  @deleted_books << book
508
293
  i = index(book)
@@ -525,9 +310,11 @@ module Alexandria
525
310
  if book.nil?
526
311
  # Undelete the whole library.
527
312
  raise unless @@deleted_libraries.include?(self)
313
+
528
314
  @@deleted_libraries.delete(self)
529
315
  else
530
316
  raise unless @deleted_books.include?(book)
317
+
531
318
  @deleted_books.delete(book)
532
319
  unless include?(book)
533
320
  changed
@@ -563,7 +350,7 @@ module Alexandria
563
350
  when Integer
564
351
  something
565
352
  else
566
- raise "#{something} is a #{something.class}"
353
+ raise NotImplementedError
567
354
  end
568
355
  File.join(path, ident.to_s + EXT[:cover])
569
356
  end
@@ -577,13 +364,13 @@ module Alexandria
577
364
  when Integer
578
365
  something
579
366
  else
580
- raise "#{something} is #{something.class}"
367
+ raise NotImplementedError
581
368
  end
582
369
  File.join(basedir, ident.to_s + EXT[:book])
583
370
  end
584
371
 
585
372
  def name=(name)
586
- File.rename(path, File.join(DIR, name))
373
+ File.rename(path, File.join(dir, name))
587
374
  @name = name
588
375
  end
589
376
 
@@ -595,8 +382,8 @@ module Alexandria
595
382
  length - n_rated
596
383
  end
597
384
 
598
- def ==(object)
599
- object.is_a?(self.class) && object.name == name
385
+ def ==(other)
386
+ other.is_a?(self.class) && other.name == name
600
387
  end
601
388
 
602
389
  def copy_covers(somewhere)
@@ -604,96 +391,27 @@ module Alexandria
604
391
  FileUtils.mkdir(somewhere)
605
392
  each do |book|
606
393
  next unless File.exist?(cover(book))
394
+
607
395
  FileUtils.cp(cover(book),
608
396
  File.join(somewhere, final_cover(book)))
609
397
  end
610
398
  end
611
399
 
612
400
  def self.jpeg?(file)
613
- 'JFIF' == IO.read(file, 10)[6..9]
401
+ IO.read(file, 10)[6..9] == "JFIF"
614
402
  end
615
403
 
616
404
  def final_cover(book)
617
405
  # TODO: what about PNG?
618
- book.ident + (Library.jpeg?(cover(book)) ? '.jpg' : '.gif')
406
+ book.ident + (Library.jpeg?(cover(book)) ? ".jpg" : ".gif")
619
407
  end
620
408
 
621
409
  protected
622
410
 
623
- def initialize(name)
411
+ def initialize(name, store = nil)
624
412
  @name = name
413
+ @store = store
625
414
  @deleted_books = []
626
415
  end
627
416
  end
628
-
629
- class Libraries
630
- attr_reader :all_libraries, :ruined_books, :deleted_books
631
-
632
- include Observable
633
- include Singleton
634
-
635
- def reload
636
- @all_libraries.clear
637
- @all_libraries.concat(Library.loadall)
638
- @all_libraries.concat(SmartLibrary.loadall)
639
-
640
- ruined = []
641
- deleted = []
642
- all_regular_libraries.each { |library|
643
- ruined += library.ruined_books
644
- # make deleted books from each library accessible so we don't crash on smart libraries
645
- deleted += library.deleted_books
646
- }
647
- @ruined_books = ruined
648
- @deleted_books = deleted
649
- end
650
-
651
- def all_regular_libraries
652
- @all_libraries.select { |x| x.is_a?(Library) }
653
- end
654
-
655
- def all_smart_libraries
656
- @all_libraries.select { |x| x.is_a?(SmartLibrary) }
657
- end
658
-
659
- # def all_dynamic_libraries
660
- # @all_libraries.select { |x| x.is_a?(SmartLibrary) }
661
- # end
662
-
663
- LIBRARY_ADDED = 1
664
- LIBRARY_REMOVED = 2
665
-
666
- def add_library(library)
667
- @all_libraries << library
668
- notify(LIBRARY_ADDED, library)
669
- end
670
-
671
- def remove_library(library)
672
- @all_libraries.delete(library)
673
- notify(LIBRARY_REMOVED, library)
674
- end
675
-
676
- def really_delete_deleted_libraries
677
- Library.really_delete_deleted_libraries
678
- SmartLibrary.really_delete_deleted_libraries
679
- end
680
-
681
- def really_save_all_books
682
- all_regular_libraries.each do |library|
683
- library.each { |book| library.save(book, true) }
684
- end
685
- end
686
-
687
- private
688
-
689
- def initialize
690
- @all_libraries = []
691
- @deleted_books = []
692
- end
693
-
694
- def notify(action, library)
695
- changed
696
- notify_observers(self, action, library)
697
- end
698
- end
699
417
  end