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.
Files changed (200) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +9 -0
  3. data/.github/workflows/ruby.yml +77 -0
  4. data/.gitignore +5 -1
  5. data/.hound.yml +2 -0
  6. data/.rubocop.yml +87 -37
  7. data/.rubocop_todo.yml +62 -191
  8. data/.simplecov +5 -2
  9. data/CHANGELOG.md +63 -0
  10. data/Gemfile +4 -3
  11. data/INSTALL.md +26 -14
  12. data/README.md +52 -42
  13. data/Rakefile +93 -109
  14. data/TODO.md +9 -1
  15. data/alexandria-book-collection-manager.gemspec +50 -43
  16. data/bin/alexandria +30 -53
  17. data/doc/FAQ +2 -6
  18. data/doc/dependency_decisions.yml +27 -8
  19. data/lib/alexandria.rb +27 -37
  20. data/lib/alexandria/about.rb +50 -50
  21. data/lib/alexandria/book_providers.rb +90 -97
  22. data/lib/alexandria/book_providers/adlibris.rb +41 -76
  23. data/lib/alexandria/book_providers/amazon_aws.rb +96 -100
  24. data/lib/alexandria/book_providers/amazon_ecs_util.rb +295 -322
  25. data/lib/alexandria/book_providers/barnes_and_noble.rb +48 -45
  26. data/lib/alexandria/book_providers/douban.rb +26 -42
  27. data/lib/alexandria/book_providers/proxis.rb +44 -55
  28. data/lib/alexandria/book_providers/pseudomarc.rb +77 -85
  29. data/lib/alexandria/book_providers/siciliano.rb +64 -65
  30. data/lib/alexandria/book_providers/thalia.rb +42 -41
  31. data/lib/alexandria/book_providers/web.rb +15 -33
  32. data/lib/alexandria/book_providers/worldcat.rb +70 -97
  33. data/lib/alexandria/book_providers/z3950.rb +160 -173
  34. data/lib/alexandria/config.rb +1 -1
  35. data/lib/alexandria/console.rb +8 -21
  36. data/lib/alexandria/default_preferences.rb +37 -0
  37. data/lib/alexandria/execution_queue.rb +15 -13
  38. data/lib/alexandria/export_format.rb +47 -0
  39. data/lib/alexandria/export_library.rb +193 -300
  40. data/lib/alexandria/import_library.rb +108 -141
  41. data/lib/alexandria/import_library_csv.rb +43 -46
  42. data/lib/alexandria/library_collection.rb +79 -0
  43. data/lib/alexandria/library_sort_order.rb +45 -0
  44. data/lib/alexandria/library_store.rb +233 -0
  45. data/lib/alexandria/logging.rb +11 -13
  46. data/lib/alexandria/models/book.rb +13 -20
  47. data/lib/alexandria/models/library.rb +81 -353
  48. data/lib/alexandria/net.rb +5 -6
  49. data/lib/alexandria/preferences.rb +73 -87
  50. data/lib/alexandria/scanners.rb +2 -2
  51. data/lib/alexandria/scanners/{cuecat.rb → cue_cat.rb} +20 -18
  52. data/lib/alexandria/scanners/keyboard.rb +8 -8
  53. data/lib/alexandria/smart_library.rb +133 -170
  54. data/lib/alexandria/ui.rb +15 -15
  55. data/lib/alexandria/ui/about_dialog.rb +49 -0
  56. data/lib/alexandria/ui/{dialogs/acquire_dialog.rb → acquire_dialog.rb} +119 -136
  57. data/lib/alexandria/ui/alert_dialog.rb +64 -0
  58. data/lib/alexandria/ui/bad_isbns_dialog.rb +41 -0
  59. data/lib/alexandria/ui/{dialogs/barcode_animation.rb → barcode_animation.rb} +16 -15
  60. data/lib/alexandria/ui/{dialogs/book_properties_dialog.rb → book_properties_dialog.rb} +39 -52
  61. data/lib/alexandria/ui/book_properties_dialog_base.rb +318 -0
  62. data/lib/alexandria/ui/builder_base.rb +7 -27
  63. data/lib/alexandria/ui/calendar_popup.rb +58 -0
  64. data/lib/alexandria/ui/callbacks.rb +189 -183
  65. data/lib/alexandria/ui/completion_models.rb +10 -23
  66. data/lib/alexandria/ui/confirm_erase_dialog.rb +33 -0
  67. data/lib/alexandria/ui/conflict_while_copying_dialog.rb +34 -0
  68. data/lib/alexandria/ui/dndable.rb +7 -7
  69. data/lib/alexandria/ui/error_dialog.rb +25 -0
  70. data/lib/alexandria/ui/export_dialog.rb +142 -0
  71. data/lib/alexandria/ui/icons.rb +47 -63
  72. data/lib/alexandria/ui/iconview.rb +12 -10
  73. data/lib/alexandria/ui/iconview_tooltips.rb +41 -54
  74. data/lib/alexandria/ui/import_dialog.rb +157 -0
  75. data/lib/alexandria/ui/init.rb +21 -33
  76. data/lib/alexandria/ui/keep_bad_isbn_dialog.rb +36 -0
  77. data/lib/alexandria/ui/libraries_combo.rb +16 -14
  78. data/lib/alexandria/ui/listview.rb +73 -87
  79. data/lib/alexandria/ui/main_app.rb +24 -26
  80. data/lib/alexandria/ui/misc_dialogs.rb +10 -0
  81. data/lib/alexandria/ui/multi_drag_treeview.rb +28 -41
  82. data/lib/alexandria/ui/{dialogs/new_book_dialog.rb → new_book_dialog.rb} +156 -194
  83. data/lib/alexandria/ui/new_book_dialog_manual.rb +139 -0
  84. data/lib/alexandria/ui/new_provider_dialog.rb +100 -0
  85. data/lib/alexandria/ui/new_smart_library_dialog.rb +74 -0
  86. data/lib/alexandria/ui/preferences_dialog.rb +313 -0
  87. data/lib/alexandria/ui/provider_preferences_base_dialog.rb +95 -0
  88. data/lib/alexandria/ui/provider_preferences_dialog.rb +35 -0
  89. data/lib/alexandria/ui/really_delete_dialog.rb +53 -0
  90. data/lib/alexandria/ui/{sidepane.rb → sidepane_manager.rb} +56 -68
  91. data/lib/alexandria/ui/skip_entry_dialog.rb +33 -0
  92. data/lib/alexandria/ui/smart_library_properties_dialog.rb +60 -0
  93. data/lib/alexandria/ui/smart_library_properties_dialog_base.rb +242 -0
  94. data/lib/alexandria/ui/smart_library_rule_box.rb +119 -0
  95. data/lib/alexandria/ui/sound.rb +11 -13
  96. data/lib/alexandria/ui/ui_manager.rb +236 -251
  97. data/lib/alexandria/undo_manager.rb +1 -0
  98. data/lib/alexandria/version.rb +4 -19
  99. data/lib/alexandria/web_themes.rb +22 -21
  100. data/po/Makefile +2 -2
  101. data/po/cs.po +993 -880
  102. data/po/cy.po +957 -874
  103. data/po/de.po +990 -869
  104. data/po/el.po +989 -869
  105. data/po/es.po +985 -865
  106. data/po/fr.po +986 -870
  107. data/po/ga.po +907 -823
  108. data/po/gl.po +981 -865
  109. data/po/it.po +986 -868
  110. data/po/ja.po +969 -853
  111. data/po/mk.po +983 -863
  112. data/po/nb.po +979 -863
  113. data/po/nl.po +983 -864
  114. data/po/pl.po +1017 -974
  115. data/po/pt.po +988 -861
  116. data/po/pt_BR.po +984 -868
  117. data/po/ru.po +992 -873
  118. data/po/sk.po +987 -869
  119. data/po/sv.po +977 -861
  120. data/po/uk.po +975 -865
  121. data/po/zh_TW.po +976 -860
  122. data/schemas/alexandria.schemas +25 -3
  123. data/share/alexandria/glade/acquire_dialog__builder.glade +15 -12
  124. data/share/alexandria/glade/book_properties_dialog__builder.glade +171 -299
  125. data/share/alexandria/glade/main_app__builder.glade +24 -33
  126. data/share/alexandria/glade/new_book_dialog__builder.glade +27 -59
  127. data/share/alexandria/glade/preferences_dialog__builder.glade +250 -290
  128. data/share/gnome/help/alexandria/C/introduction.xml +0 -8
  129. data/share/gnome/help/alexandria/C/searching.xml +1 -1
  130. data/share/gnome/help/alexandria/C/smart-libraries.xml +2 -2
  131. data/share/gnome/help/alexandria/C/working-with-libraries.xml +1 -1
  132. data/share/gnome/help/alexandria/fr/alexandria.xml +1 -1
  133. data/share/gnome/help/alexandria/ja/introduction.xml +0 -8
  134. data/share/gnome/help/alexandria/ja/smart-libraries.xml +1 -1
  135. data/spec/alexandria/book_providers/world_cat_provider_spec.rb +160 -0
  136. data/spec/alexandria/book_providers_spec.rb +75 -171
  137. data/spec/alexandria/book_spec.rb +12 -10
  138. data/spec/alexandria/console_spec.rb +27 -0
  139. data/spec/alexandria/export_library_spec.rb +130 -0
  140. data/spec/alexandria/library_spec.rb +128 -172
  141. data/spec/alexandria/library_store_spec.rb +37 -0
  142. data/spec/alexandria/preferences_spec.rb +44 -17
  143. data/spec/alexandria/scanners/cue_cat_spec.rb +52 -0
  144. data/spec/alexandria/smart_library_spec.rb +30 -25
  145. data/spec/alexandria/ui/about_dialog_spec.rb +14 -0
  146. data/spec/alexandria/ui/acquire_dialog_spec.rb +14 -0
  147. data/spec/alexandria/ui/alert_dialog_spec.rb +16 -0
  148. data/spec/alexandria/ui/bad_isbns_dialog_spec.rb +14 -0
  149. data/spec/alexandria/ui/book_properties_dialog_spec.rb +17 -0
  150. data/spec/alexandria/ui/confirm_erase_dialog_spec.rb +14 -0
  151. data/spec/alexandria/ui/conflict_while_copying_dialog_spec.rb +16 -0
  152. data/spec/alexandria/ui/error_dialog_spec.rb +14 -0
  153. data/spec/alexandria/ui/export_dialog_spec.rb +36 -0
  154. data/spec/alexandria/ui/icons_spec.rb +26 -0
  155. data/spec/alexandria/ui/iconview_spec.rb +7 -21
  156. data/spec/alexandria/ui/import_dialog_spec.rb +46 -0
  157. data/spec/alexandria/ui/keep_bad_isbn_dialog_spec.rb +17 -0
  158. data/spec/alexandria/ui/main_app_spec.rb +7 -34
  159. data/spec/alexandria/ui/new_book_dialog_manual_spec.rb +15 -0
  160. data/spec/alexandria/ui/new_book_dialog_spec.rb +22 -0
  161. data/spec/alexandria/ui/new_provider_dialog_spec.rb +30 -0
  162. data/spec/alexandria/ui/new_smart_library_dialog_spec.rb +39 -0
  163. data/spec/alexandria/ui/preferences_dialog_spec.rb +14 -0
  164. data/spec/alexandria/ui/provider_preferences_dialog_spec.rb +34 -0
  165. data/spec/alexandria/ui/really_delete_dialog_spec.rb +16 -0
  166. data/spec/alexandria/ui/sidepane_manager_spec.rb +15 -0
  167. data/spec/alexandria/ui/skip_entry_dialog_spec.rb +14 -0
  168. data/spec/alexandria/ui/smart_library_properties_dialog_spec.rb +49 -0
  169. data/spec/alexandria/ui/sound_spec.rb +2 -2
  170. data/spec/alexandria/ui/ui_manager_spec.rb +43 -20
  171. data/spec/end_to_end/basic_run_spec.rb +52 -0
  172. data/spec/spec_helper.rb +65 -33
  173. data/tasks/setup.rb +2 -2
  174. data/tasks/spec.rake +16 -3
  175. data/util/rake/fileinstall.rb +39 -35
  176. data/util/rake/gettextgenerate.rb +7 -7
  177. data/util/rake/omfgenerate.rb +7 -7
  178. metadata +178 -45
  179. data/dogtail/basic_run_test.py +0 -9
  180. data/lib/alexandria/book_providers/deastore.rb +0 -265
  181. data/lib/alexandria/book_providers/mcu.rb +0 -182
  182. data/lib/alexandria/book_providers/renaud.rb +0 -149
  183. data/lib/alexandria/ui/dialogs/about_dialog.rb +0 -61
  184. data/lib/alexandria/ui/dialogs/alert_dialog.rb +0 -72
  185. data/lib/alexandria/ui/dialogs/bad_isbns_dialog.rb +0 -51
  186. data/lib/alexandria/ui/dialogs/book_properties_dialog_base.rb +0 -426
  187. data/lib/alexandria/ui/dialogs/export_dialog.rb +0 -171
  188. data/lib/alexandria/ui/dialogs/import_dialog.rb +0 -196
  189. data/lib/alexandria/ui/dialogs/misc_dialogs.rb +0 -87
  190. data/lib/alexandria/ui/dialogs/new_book_dialog_manual.rb +0 -154
  191. data/lib/alexandria/ui/dialogs/new_smart_library_dialog.rb +0 -74
  192. data/lib/alexandria/ui/dialogs/preferences_dialog.rb +0 -568
  193. data/lib/alexandria/ui/dialogs/smart_library_properties_dialog.rb +0 -59
  194. data/lib/alexandria/ui/dialogs/smart_library_properties_dialog_base.rb +0 -420
  195. data/spec/alexandria/scanners/cuecat_spec.rb +0 -67
  196. data/spec/alexandria/ui/dialogs_spec.rb +0 -96
  197. data/spec/alexandria/ui/sidepane_spec.rb +0 -29
  198. data/spec/alexandria/ui/ui_utilities_spec.rb +0 -62
  199. data/spec/alexandria/utilities_spec.rb +0 -52
  200. data/tasks/dogtail.rake +0 -6
@@ -1,40 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright (C) 2004-2006 Laurent Sansonetti
4
- # Copyright (C) 2010 Cathal Mc Ginley
5
- # Copyright (C) 2014,2015 Matijs van Zuijlen
3
+ # This file is part of Alexandria.
6
4
  #
7
- # Alexandria is free software; you can redistribute it and/or
8
- # modify it under the terms of the GNU General Public License as
9
- # published by the Free Software Foundation; either version 2 of the
10
- # License, or (at your option) any later version.
11
- #
12
- # Alexandria is distributed in the hope that it will be useful,
13
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
- # General Public License for more details.
16
- #
17
- # You should have received a copy of the GNU General Public
18
- # License along with Alexandria; see the file COPYING. If not,
19
- # write to the Free Software Foundation, Inc., 51 Franklin Street,
20
- # Fifth Floor, Boston, MA 02110-1301 USA.
21
- require 'gettext'
5
+ # See the file README.md for authorship and licensing information.
6
+
7
+ require "gettext"
8
+ require "tmpdir"
22
9
 
23
10
  module Alexandria
24
11
  class ImportFilter
25
12
  attr_reader :name, :patterns, :message
26
13
 
14
+ include Logging
27
15
  include GetText
28
16
  extend GetText
29
- bindtextdomain(Alexandria::TEXTDOMAIN, charset: 'UTF-8')
17
+ bindtextdomain(Alexandria::TEXTDOMAIN, charset: "UTF-8")
30
18
 
31
19
  def self.all
32
20
  [
33
- new(_('Autodetect'), ['*'], :import_autodetect),
34
- new(_('Archived Tellico XML (*.bc, *.tc)'),
35
- ['*.tc', '*.bc'], :import_as_tellico_xml_archive),
36
- new(_('ISBN List (*.txt)'), ['*.txt'], :import_as_isbn_list),
37
- new(_('GoodReads CSV'), ['*.csv'], :import_as_csv_file)
21
+ new(_("Autodetect"), ["*"], :import_autodetect),
22
+ new(_("Archived Tellico XML (*.bc, *.tc)"),
23
+ ["*.tc", "*.bc"], :import_as_tellico_xml_archive),
24
+ new(_("ISBN List (*.txt)"), ["*.txt"], :import_as_isbn_list),
25
+ new(_("GoodReads CSV"), ["*.csv"], :import_as_csv_file)
38
26
  ]
39
27
  end
40
28
 
@@ -47,7 +35,7 @@ module Alexandria
47
35
  end
48
36
 
49
37
  def invoke(library_name, filename)
50
- puts "Selected: #{@message} -- #{library_name} -- #{filename}"
38
+ log.debug { "Selected: #{@message} -- #{library_name} -- #{filename}" }
51
39
  Library.send(@message, library_name, filename,
52
40
  @on_iterate_cb, @on_error_cb)
53
41
  end
@@ -63,119 +51,100 @@ module Alexandria
63
51
 
64
52
  class Library
65
53
  def self.import_autodetect(*args)
66
- puts args.inspect
54
+ log.debug { args.inspect }
67
55
  filename = args[1]
68
- puts "Filename is #{filename} and ext is #{filename[-4..-1]}"
69
- puts "Beginning import: #{args[0]}, #{args[1]}"
70
- if filename[-4..-1] == '.txt'
56
+ log.debug { "Filename is #{filename} and ext is #{filename[-4..-1]}" }
57
+ log.debug { "Beginning import: #{args[0]}, #{args[1]}" }
58
+ if filename[-4..-1] == ".txt"
71
59
  import_as_isbn_list(*args)
72
- elsif ['.tc', '.bc'].include? filename[-3..-1]
73
- begin
74
- import_as_tellico_xml_archive(*args)
75
- rescue => e
76
- puts e.message
77
- puts e.backtrace.join("\n>> ")
78
- end
79
- elsif ['.csv'].include? filename[-4..-1]
60
+ elsif [".tc", ".bc"].include? filename[-3..-1]
61
+ import_as_tellico_xml_archive(*args)
62
+ elsif [".csv"].include? filename[-4..-1]
80
63
  import_as_csv_file(*args)
81
64
  else
82
- puts 'Bailing on this import!'
83
- raise 'Not supported type'
65
+ raise _("Unsupported type")
84
66
  end
85
67
  end
86
68
 
87
69
  def self.import_as_tellico_xml_archive(name, filename,
88
70
  on_iterate_cb, _on_error_cb)
89
- puts 'Starting import_as_tellico_xml_archive... '
71
+ log.debug { "Starting import_as_tellico_xml_archive... " }
90
72
  return nil unless system("unzip -qqt \"#{filename}\"")
91
- tmpdir = File.join(Dir.tmpdir, 'tellico_export')
73
+
74
+ tmpdir = File.join(Dir.tmpdir, "tellico_export")
92
75
  FileUtils.rm_rf(tmpdir) if File.exist?(tmpdir)
93
76
  Dir.mkdir(tmpdir)
94
77
  Dir.chdir(tmpdir) do
95
- begin
96
- system("unzip -qq \"#{filename}\"")
97
- file = File.exist?('bookcase.xml') ? 'bookcase.xml' : 'tellico.xml'
98
- xml = REXML::Document.new(File.open(file))
99
- raise unless ['bookcase', 'tellico'].include? xml.root.name
100
- # FIXME: handle multiple collections
101
- raise unless xml.root.elements.size == 1
102
- collection = xml.root.elements[1]
103
- raise unless collection.name == 'collection'
104
- type = collection.attribute('type').value.to_i
105
- raise unless (type == 2) || (type == 5)
106
-
107
- content = []
108
- entries = collection.elements.to_a('entry')
109
- (total = entries.size).times do |n|
110
- entry = entries[n]
111
- elements = entry.elements
112
- # Feed an array in here, tomorrow.
113
- keys = ['isbn', 'publisher', 'pub_year', 'binding']
114
-
115
- book_elements = [neaten(elements['title'].text)]
116
- book_elements += if !elements['authors'].nil?
117
- [elements['authors'].elements.to_a.map \
118
- { |x| neaten(x.text) }]
119
- else
120
- [[]]
121
- end
122
- book_elements += keys.map { |key|
123
- neaten(elements[key].text) if elements[key]
124
- }
125
- # isbn
126
- if book_elements[2].nil? || book_elements[2].strip.empty?
127
- book_elements[2] = nil
128
- else
129
- begin
130
- book_elements[2] = book_elements[2].strip
131
- book_elements[2] = Library.canonicalise_ean(book_elements[2])
132
- rescue => ex
133
- puts book_elements[2]
134
- puts ex.message
135
- puts ex.backtrace.join("\n> ")
136
- raise ex
137
- end
138
- end
139
- book_elements[4] = book_elements[4].to_i unless book_elements[4].nil? # publishing_year
140
- puts book_elements.inspect
141
- cover = (neaten(elements['cover'].text) if elements['cover'])
142
- puts cover
143
- book = Book.new(*book_elements)
144
- if elements['rating'] && Book::VALID_RATINGS.member?(elements['rating'].text.to_i)
145
- book.rating = elements['rating'].text.to_i
146
- end
147
- book.notes = neaten(elements['comments'].text) if elements['comments']
148
- content << [book, cover]
149
- on_iterate_cb&.call(n + 1, total)
150
- end
78
+ system("unzip -qq \"#{filename}\"")
79
+ file = File.exist?("bookcase.xml") ? "bookcase.xml" : "tellico.xml"
80
+ xml = REXML::Document.new(File.open(file))
81
+ raise unless ["bookcase", "tellico"].include? xml.root.name
82
+ # FIXME: handle multiple collections
83
+ raise unless xml.root.elements.size == 1
151
84
 
152
- library = Library.load(name)
153
- content.each do |book, cover|
154
- unless cover.nil?
155
- library.save_cover(book,
156
- File.join(Dir.pwd, 'images',
157
- cover))
158
- end
159
- library << book
160
- library.save(book)
85
+ collection = xml.root.elements[1]
86
+ raise unless collection.name == "collection"
87
+
88
+ type = collection.attribute("type").value.to_i
89
+ raise unless (type == 2) || (type == 5)
90
+
91
+ content = []
92
+ entries = collection.elements.to_a("entry")
93
+ (total = entries.size).times do |n|
94
+ entry = entries[n]
95
+ elements = entry.elements
96
+ # Feed an array in here, tomorrow.
97
+ keys = ["isbn", "publisher", "pub_year", "binding"]
98
+
99
+ book_elements = [neaten(elements["title"].text)]
100
+ book_elements += if !elements["authors"].nil?
101
+ [elements["authors"].elements.to_a.map \
102
+ { |x| neaten(x.text) }]
103
+ else
104
+ [[]]
105
+ end
106
+ book_elements += keys.map do |key|
107
+ neaten(elements[key].text) if elements[key]
161
108
  end
162
- return [library, []]
163
- rescue => e
164
- puts e.message
165
- return nil
109
+ book_elements[2] = Library.canonicalise_ean(book_elements[2])
110
+ # publishing_year
111
+ book_elements[4] = book_elements[4].to_i unless book_elements[4].nil?
112
+ log.debug { book_elements.inspect }
113
+ cover = (neaten(elements["cover"].text) if elements["cover"])
114
+ log.debug { cover }
115
+ book = Book.new(*book_elements)
116
+ if elements["rating"]
117
+ rating = elements["rating"].text.to_i
118
+ book.rating = rating if Book::VALID_RATINGS.member? rating
119
+ end
120
+ book.notes = neaten(elements["comments"].text) if elements["comments"]
121
+ content << [book, cover]
122
+ on_iterate_cb&.call(n + 1, total)
123
+ end
124
+
125
+ # TODO: Pass in library store as an argument
126
+ library = LibraryCollection.instance.library_store.load_library name
127
+ content.each do |book, cover|
128
+ library.save_cover(book, File.join(Dir.pwd, "images", cover)) unless cover.nil?
129
+ library << book
130
+ library.save(book)
166
131
  end
132
+ return [library, []]
133
+ rescue StandardError => ex
134
+ log.info { ex.message }
135
+ return nil
167
136
  end
168
137
  end
169
138
 
170
139
  def self.import_as_csv_file(name, filename, on_iterate_cb, _on_error_cb)
171
- require 'alexandria/import_library_csv'
140
+ require "alexandria/import_library_csv"
172
141
  books_and_covers = []
173
142
  line_count = IO.readlines(filename).reduce(0) { |count, _line| count + 1 }
174
143
 
175
144
  import_count = 0
176
145
  max_import = line_count - 1
177
146
 
178
- reader = CSV.open(filename, 'r')
147
+ reader = CSV.open(filename, "r")
179
148
  # Goodreads & LibraryThing now use csv header lines
180
149
  header = reader.shift
181
150
  importer = identify_csv_type(header)
@@ -187,7 +156,7 @@ module Alexandria
187
156
  if book.isbn
188
157
  # if we can search by ISBN, try to grab the cover
189
158
  begin
190
- dl_book, dl_cover = Alexandria::BookProviders.isbn_search(book.isbn)
159
+ dl_book, dl_cover = BookProviders.isbn_search(book.isbn)
191
160
  if dl_book.authors.size > book.authors.size
192
161
  # LibraryThing only supports a single author, so
193
162
  # attempt to include more author information if it's
@@ -196,9 +165,8 @@ module Alexandria
196
165
  end
197
166
  book.edition = dl_book.edition unless book.edition
198
167
  cover = dl_cover
199
- rescue
200
- puts "failed to get cover for #{book.title} #{book.isbn}" if $DEBUG
201
- # note failure
168
+ rescue StandardError
169
+ log.debug { "Failed to get cover for #{book.title} #{book.isbn}" }
202
170
  end
203
171
  end
204
172
 
@@ -213,12 +181,12 @@ module Alexandria
213
181
  # probably Goodreads' wonky ISBN fields ,,="043432432X",
214
182
  # this is a hack to fix up such files
215
183
  data = File.read(filename)
216
- data.gsub!(/\,\=\"/, ',"')
217
- csv_fixed = Tempfile.new('alexandria_import_csv_fixed_')
184
+ data.gsub!(/,="/, ',"')
185
+ csv_fixed = Tempfile.new("alexandria_import_csv_fixed_")
218
186
  csv_fixed.write(data)
219
187
  csv_fixed.close
220
188
 
221
- reader = CSV.open(csv_fixed.path, 'r')
189
+ reader = CSV.open(csv_fixed.path, "r")
222
190
  header = reader.shift
223
191
  importer = identify_csv_type(header)
224
192
 
@@ -226,12 +194,13 @@ module Alexandria
226
194
  end
227
195
  end
228
196
 
229
- library = Library.load(name)
197
+ # TODO: Pass in library store as an argument
198
+ library = LibraryCollection.instance.library_store.load_library name
230
199
 
231
200
  books_and_covers.each do |book, cover_uri|
232
- puts "Saving #{book.isbn} cover..." if $DEBUG
201
+ log.debug { "Saving #{book.isbn} cover" }
233
202
  library.save_cover(book, cover_uri) unless cover_uri.nil?
234
- puts "Saving #{book.isbn}..." if $DEBUG
203
+ log.debug { "Saving #{book.isbn}" }
235
204
  library << book
236
205
  library.save(book)
237
206
  end
@@ -240,20 +209,15 @@ module Alexandria
240
209
 
241
210
  def self.import_as_isbn_list(name, filename, on_iterate_cb,
242
211
  on_error_cb)
243
- puts 'Starting import_as_isbn_list... '
212
+ log.debug { "Starting import_as_isbn_list... " }
244
213
  isbn_list = IO.readlines(filename).map do |line|
245
- puts "Trying line #{line}" if $DEBUG
246
- # Let's preserve the failing isbns so we can report them later.
247
- begin
248
- [line.chomp, canonicalise_isbn(line.chomp)] unless line == "\n"
249
- rescue => e
250
- puts e.message
251
- [line.chomp, nil]
252
- end
214
+ log.debug { "Trying line #{line}" }
215
+ [line.chomp, canonicalise_isbn(line.chomp)] unless line == "\n"
253
216
  end
254
- puts "Isbn list: #{isbn_list.inspect}"
217
+ log.debug { "Isbn list: #{isbn_list.inspect}" }
255
218
  isbn_list.compact!
256
219
  return nil if isbn_list.empty?
220
+
257
221
  max_iterations = isbn_list.length * 2
258
222
  current_iteration = 1
259
223
  books = []
@@ -262,27 +226,30 @@ module Alexandria
262
226
  isbn_list.each do |isbn|
263
227
  begin
264
228
  if isbn[1]
265
- books << Alexandria::BookProviders.isbn_search(isbn[1])
229
+ books << BookProviders.isbn_search(isbn[1])
266
230
  else
267
231
  bad_isbns << isbn[0]
268
232
  end
269
- rescue => e
270
- puts e.message
233
+ rescue BookProviders::SearchEmptyError => ex
234
+ log.debug { ex.message }
271
235
  failed_lookup_isbns << isbn[1]
272
- puts "NOTE : ignoring on_error_cb #{on_error_cb}"
236
+ log.debug { "NOTE : ignoring on_error_cb #{on_error_cb}" }
273
237
  # return nil unless
274
238
  # (on_error_cb and on_error_cb.call(e.message))
275
239
  end
276
240
 
277
241
  on_iterate_cb&.call(current_iteration += 1, max_iterations)
278
242
  end
279
- puts "Bad Isbn list: #{bad_isbns.inspect}" if bad_isbns
280
- library = load(name)
281
- puts "Going with these #{books.length} books: #{books.inspect}" if $DEBUG
243
+ log.debug { "Bad Isbn list: #{bad_isbns.inspect}" } if bad_isbns
244
+
245
+ # TODO: Pass in library store as an argument
246
+ library = LibraryCollection.instance.library_store.load_library name
247
+
248
+ log.debug { "Going with these #{books.length} books: #{books.inspect}" }
282
249
  books.each do |book, cover_uri|
283
- puts "Saving #{book.isbn} cover..." if $DEBUG
250
+ log.debug { "Saving #{book.isbn} cover..." }
284
251
  library.save_cover(book, cover_uri) unless cover_uri.nil?
285
- puts "Saving #{book.isbn}..." if $DEBUG
252
+ log.debug { "Saving #{book.isbn}..." }
286
253
  library << book
287
254
  library.save(book)
288
255
  on_iterate_cb&.call(current_iteration += 1, max_iterations)
@@ -20,10 +20,10 @@
20
20
  # New CSV import code taken from Palatina and modified for
21
21
  # Alexandria. (29 Apr 2010)
22
22
 
23
- require 'csv'
24
- require 'date'
25
- require 'htmlentities'
26
- require 'tempfile'
23
+ require "csv"
24
+ require "date"
25
+ require "htmlentities"
26
+ require "tempfile"
27
27
 
28
28
  module Alexandria
29
29
  class CSVImport
@@ -47,21 +47,21 @@ module Alexandria
47
47
  class GoodreadsCSVImport < CSVImport
48
48
  def initialize(header)
49
49
  super(header)
50
- @title = index_of('Title')
51
- @author = index_of('Author')
52
- @additional_authors = index_of('Additional Authors')
53
- @isbn = index_of('ISBN')
54
- @publisher = index_of('Publisher')
55
- @publishing_year = index_of('Year Published')
56
- @edition = index_of('Binding')
50
+ @title = index_of("Title")
51
+ @author = index_of("Author")
52
+ @additional_authors = index_of("Additional Authors")
53
+ @isbn = index_of("ISBN")
54
+ @publisher = index_of("Publisher")
55
+ @publishing_year = index_of("Year Published")
56
+ @edition = index_of("Binding")
57
57
 
58
58
  # optional extras
59
- @notes = index_of('Private Notes')
60
- @rating = index_of('My Rating')
61
- @read_count = index_of('Read Count')
62
- @date_read = index_of('Date Read')
63
- @bookshelves = index_of('Bookshelves') # save names as tags
64
- @mainshelf = index_of('Exclusive Shelf')
59
+ @notes = index_of("Private Notes")
60
+ @rating = index_of("My Rating")
61
+ @read_count = index_of("Read Count")
62
+ @date_read = index_of("Date Read")
63
+ @bookshelves = index_of("Bookshelves") # save names as tags
64
+ @mainshelf = index_of("Exclusive Shelf")
65
65
  end
66
66
 
67
67
  def row_to_book(row)
@@ -69,11 +69,10 @@ module Alexandria
69
69
  authors = []
70
70
  authors << normalize(row[@author])
71
71
  additional = row[@additional_authors]
72
- additional.split(',').each do |add|
72
+ additional.split(",").each do |add|
73
73
  authors << normalize(add)
74
74
  end
75
- isbn = row[@isbn] # TODO: canonicalize_ean...
76
- isbn = Library.canonicalise_ean(isbn) if isbn
75
+ isbn = Library.canonicalise_ean(row[@isbn])
77
76
  publisher = normalize(row[@publisher])
78
77
  year = row[@publishing_year].to_i
79
78
  edition = normalize(row[@edition])
@@ -90,32 +89,30 @@ module Alexandria
90
89
  book.redd = true if count > 0
91
90
  end
92
91
  if row[@date_read]
93
- begin
94
- date = Date.strptime(str, '%d/%m/%y') # e.g. "14/01/10" => 2010-01-14
95
- book.redd_when = date
96
- book.redd = true
97
- rescue
98
- end
92
+ date = Date.strptime(str, "%d/%m/%y") # e.g. "14/01/10" => 2010-01-14
93
+ book.redd_when = date
94
+ book.redd = true
99
95
  end
100
96
  if row[@mainshelf]
101
- if row[@mainshelf] == 'read'
97
+ case row[@mainshelf]
98
+ when "read"
102
99
  book.redd = true
103
- elsif row[@mainshelf] == 'to-read'
100
+ when "to-read"
104
101
  book.redd = false
105
- book.tags = ['to read']
106
- elsif row[@mainshelf] == 'currently-reading'
102
+ book.tags = ["to read"]
103
+ when "currently-reading"
107
104
  book.redd = false
108
- book.tags = ['currently reading']
105
+ book.tags = ["currently reading"]
109
106
  end
110
107
  end
111
108
  if row[@bookshelves]
112
109
  shelves = normalize(row[@bookshelves]).split
113
110
  shelves.each do |shelf|
114
- tag = shelf.tr('-', ' ')
111
+ tag = shelf.tr("-", " ")
115
112
  book.tags << tag unless book.tags.include? tag
116
113
  end
117
114
  end
118
- puts "Goodreads loading #{book.title}" if $DEBUG
115
+ log.debug { "Goodreads loading #{book.title}" }
119
116
  book
120
117
  end
121
118
  end
@@ -149,10 +146,10 @@ module Alexandria
149
146
  # sometimes "Publisher (YEAR), Edition: NUM, Binding, NUM pages"
150
147
  publisher_info = normalize(row[@publisher_info])
151
148
  publisher = publisher_info
152
- publisher = Regexp.last_match[1] if publisher_info =~ /([^\(]+)\(/
149
+ publisher = Regexp.last_match[1] if publisher_info =~ /([^(]+)\(/
153
150
  edition = publisher_info # binding
154
- edition_info = publisher_info.split(',')
155
- edition = publisher_info.split(',')[-2] if edition_info.size >= 3
151
+ edition_info = publisher_info.split(",")
152
+ edition = publisher_info.split(",")[-2] if edition_info.size >= 3
156
153
 
157
154
  year = row[@publishing_year].to_i
158
155
 
@@ -166,13 +163,13 @@ module Alexandria
166
163
 
167
164
  book.rating = row[@rating].to_i if row[@rating]
168
165
  if row[@tags]
169
- tags = normalize(row[@tags]).split(',')
166
+ tags = normalize(row[@tags]).split(",")
170
167
  tags.each do |tag|
171
168
  book.tags << tag unless book.tags.include? tag
172
169
  end
173
170
  end
174
171
 
175
- puts "LibraryThing loading #{book.title}" if $DEBUG
172
+ log.debug { "LibraryThing loading #{book.title}" }
176
173
  book
177
174
  end
178
175
  end
@@ -186,20 +183,20 @@ module Alexandria
186
183
  is_librarything = false
187
184
  is_goodreads = false
188
185
  header.each do |head|
189
- if head == "'PUBLICATION INFO'"
186
+ case head
187
+ when "'PUBLICATION INFO'"
190
188
  is_librarything = true
191
189
  break
192
- elsif head == 'Year Published'
190
+ when "Year Published"
193
191
  is_goodreads = true
194
192
  break
195
193
  end
196
194
  end
197
- if is_librarything
198
- return LibraryThingCSVImport.new(header)
199
- elsif is_goodreads
200
- return GoodreadsCSVImport.new(header)
201
- end
202
- raise 'Not Recognized' unless is_librarything || is_goodreads
195
+
196
+ return LibraryThingCSVImport.new(header) if is_librarything
197
+ return GoodreadsCSVImport.new(header) if is_goodreads
198
+
199
+ raise _("Not Recognized") unless is_librarything || is_goodreads
203
200
  end
204
201
  end
205
202
  end