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