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,38 +1,28 @@
1
- # Copyright (C) 2004-2006 Laurent Sansonetti
2
- # Copyright (C) 2010 Cathal Mc Ginley
3
- # Copyright (C) 2014,2015 Matijs van Zuijlen
4
- #
5
- # Alexandria is free software; you can redistribute it and/or
6
- # modify it under the terms of the GNU General Public License as
7
- # published by the Free Software Foundation; either version 2 of the
8
- # License, or (at your option) any later version.
9
- #
10
- # Alexandria is distributed in the hope that it will be useful,
11
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
- # General Public License for more details.
1
+ # frozen_string_literal: true
2
+
3
+ # This file is part of Alexandria.
14
4
  #
15
- # You should have received a copy of the GNU General Public
16
- # License along with Alexandria; see the file COPYING. If not,
17
- # write to the Free Software Foundation, Inc., 51 Franklin Street,
18
- # Fifth Floor, Boston, MA 02110-1301 USA.
19
- require 'gettext'
5
+ # See the file README.md for authorship and licensing information.
6
+
7
+ require "gettext"
8
+ require "tmpdir"
20
9
 
21
10
  module Alexandria
22
11
  class ImportFilter
23
12
  attr_reader :name, :patterns, :message
24
13
 
14
+ include Logging
25
15
  include GetText
26
16
  extend GetText
27
- bindtextdomain(Alexandria::TEXTDOMAIN, charset: 'UTF-8')
17
+ bindtextdomain(Alexandria::TEXTDOMAIN, charset: "UTF-8")
28
18
 
29
19
  def self.all
30
20
  [
31
- new(_('Autodetect'), ['*'], :import_autodetect),
32
- new(_('Archived Tellico XML (*.bc, *.tc)'),
33
- ['*.tc', '*.bc'], :import_as_tellico_xml_archive),
34
- new(_('ISBN List (*.txt)'), ['*.txt'], :import_as_isbn_list),
35
- 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)
36
26
  ]
37
27
  end
38
28
 
@@ -45,7 +35,7 @@ module Alexandria
45
35
  end
46
36
 
47
37
  def invoke(library_name, filename)
48
- puts "Selected: #{@message} -- #{library_name} -- #{filename}"
38
+ log.debug { "Selected: #{@message} -- #{library_name} -- #{filename}" }
49
39
  Library.send(@message, library_name, filename,
50
40
  @on_iterate_cb, @on_error_cb)
51
41
  end
@@ -61,121 +51,100 @@ module Alexandria
61
51
 
62
52
  class Library
63
53
  def self.import_autodetect(*args)
64
- puts args.inspect
54
+ log.debug { args.inspect }
65
55
  filename = args[1]
66
- puts "Filename is #{filename} and ext is #{filename[-4..-1]}"
67
- puts "Beginning import: #{args[0]}, #{args[1]}"
68
- 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"
69
59
  import_as_isbn_list(*args)
70
- elsif ['.tc', '.bc'].include? filename[-3..-1]
71
- begin
72
- import_as_tellico_xml_archive(*args)
73
- rescue => e
74
- puts e.message
75
- puts e.backtrace.join("\n>> ")
76
- end
77
- 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]
78
63
  import_as_csv_file(*args)
79
64
  else
80
- puts 'Bailing on this import!'
81
- raise 'Not supported type'
65
+ raise _("Unsupported type")
82
66
  end
83
67
  end
84
68
 
85
69
  def self.import_as_tellico_xml_archive(name, filename,
86
70
  on_iterate_cb, _on_error_cb)
87
- puts 'Starting import_as_tellico_xml_archive... '
71
+ log.debug { "Starting import_as_tellico_xml_archive... " }
88
72
  return nil unless system("unzip -qqt \"#{filename}\"")
89
- tmpdir = File.join(Dir.tmpdir, 'tellico_export')
73
+
74
+ tmpdir = File.join(Dir.tmpdir, "tellico_export")
90
75
  FileUtils.rm_rf(tmpdir) if File.exist?(tmpdir)
91
76
  Dir.mkdir(tmpdir)
92
77
  Dir.chdir(tmpdir) do
93
- begin
94
- system("unzip -qq \"#{filename}\"")
95
- file = File.exist?('bookcase.xml') ? 'bookcase.xml' : 'tellico.xml'
96
- xml = REXML::Document.new(File.open(file))
97
- raise unless ['bookcase', 'tellico'].include? xml.root.name
98
- # FIXME: handle multiple collections
99
- raise unless xml.root.elements.size == 1
100
- collection = xml.root.elements[1]
101
- raise unless collection.name == 'collection'
102
- type = collection.attribute('type').value.to_i
103
- raise unless (type == 2) || (type == 5)
104
-
105
- content = []
106
- entries = collection.elements.to_a('entry')
107
- (total = entries.size).times do |n|
108
- entry = entries[n]
109
- elements = entry.elements
110
- # Feed an array in here, tomorrow.
111
- keys = ['isbn', 'publisher', 'pub_year', 'binding']
112
-
113
- book_elements = [neaten(elements['title'].text)]
114
- book_elements += if !elements['authors'].nil?
115
- [elements['authors'].elements.to_a.map \
116
- { |x| neaten(x.text) }]
117
- else
118
- [[]]
119
- end
120
- book_elements += keys.map { |key|
121
- neaten(elements[key].text) if elements[key]
122
- }
123
- # isbn
124
- if book_elements[2].nil? || book_elements[2].strip.empty?
125
- book_elements[2] = nil
126
- else
127
- begin
128
- book_elements[2] = book_elements[2].strip
129
- book_elements[2] = Library.canonicalise_ean(book_elements[2])
130
- rescue => ex
131
- puts book_elements[2]
132
- puts ex.message
133
- puts ex.backtrace.join("\n> ")
134
- raise ex
135
- end
136
- end
137
- book_elements[4] = book_elements[4].to_i unless book_elements[4].nil? # publishing_year
138
- puts book_elements.inspect
139
- cover = if elements['cover']
140
- neaten(elements['cover'].text)
141
- end
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) if on_iterate_cb
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)
166
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)
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,28 +156,23 @@ 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
194
163
  # available
195
164
  book.authors = dl_book.authors
196
165
  end
197
- unless book.edition
198
- book.edition = dl_book.edition
199
- end
166
+ book.edition = dl_book.edition unless book.edition
200
167
  cover = dl_cover
201
- rescue
202
- puts "failed to get cover for #{book.title} #{book.isbn}" if $DEBUG
203
- # note failure
168
+ rescue StandardError
169
+ log.debug { "Failed to get cover for #{book.title} #{book.isbn}" }
204
170
  end
205
171
  end
206
172
 
207
173
  books_and_covers << [book, cover]
208
174
  import_count += 1
209
- if on_iterate_cb
210
- on_iterate_cb.call(import_count, max_import)
211
- end
175
+ on_iterate_cb&.call(import_count, max_import)
212
176
  end
213
177
  rescue CSV::IllegalFormatError
214
178
  unless failed_once
@@ -217,12 +181,12 @@ module Alexandria
217
181
  # probably Goodreads' wonky ISBN fields ,,="043432432X",
218
182
  # this is a hack to fix up such files
219
183
  data = File.read(filename)
220
- data.gsub!(/\,\=\"/, ',"')
221
- csv_fixed = Tempfile.new('alexandria_import_csv_fixed_')
184
+ data.gsub!(/,="/, ',"')
185
+ csv_fixed = Tempfile.new("alexandria_import_csv_fixed_")
222
186
  csv_fixed.write(data)
223
187
  csv_fixed.close
224
188
 
225
- reader = CSV.open(csv_fixed.path, 'r')
189
+ reader = CSV.open(csv_fixed.path, "r")
226
190
  header = reader.shift
227
191
  importer = identify_csv_type(header)
228
192
 
@@ -230,12 +194,13 @@ module Alexandria
230
194
  end
231
195
  end
232
196
 
233
- library = Library.load(name)
197
+ # TODO: Pass in library store as an argument
198
+ library = LibraryCollection.instance.library_store.load_library name
234
199
 
235
200
  books_and_covers.each do |book, cover_uri|
236
- puts "Saving #{book.isbn} cover..." if $DEBUG
201
+ log.debug { "Saving #{book.isbn} cover" }
237
202
  library.save_cover(book, cover_uri) unless cover_uri.nil?
238
- puts "Saving #{book.isbn}..." if $DEBUG
203
+ log.debug { "Saving #{book.isbn}" }
239
204
  library << book
240
205
  library.save(book)
241
206
  end
@@ -244,20 +209,15 @@ module Alexandria
244
209
 
245
210
  def self.import_as_isbn_list(name, filename, on_iterate_cb,
246
211
  on_error_cb)
247
- puts 'Starting import_as_isbn_list... '
212
+ log.debug { "Starting import_as_isbn_list... " }
248
213
  isbn_list = IO.readlines(filename).map do |line|
249
- puts "Trying line #{line}" if $DEBUG
250
- # Let's preserve the failing isbns so we can report them later.
251
- begin
252
- [line.chomp, canonicalise_isbn(line.chomp)] unless line == "\n"
253
- rescue => e
254
- puts e.message
255
- [line.chomp, nil]
256
- end
214
+ log.debug { "Trying line #{line}" }
215
+ [line.chomp, canonicalise_isbn(line.chomp)] unless line == "\n"
257
216
  end
258
- puts "Isbn list: #{isbn_list.inspect}"
217
+ log.debug { "Isbn list: #{isbn_list.inspect}" }
259
218
  isbn_list.compact!
260
219
  return nil if isbn_list.empty?
220
+
261
221
  max_iterations = isbn_list.length * 2
262
222
  current_iteration = 1
263
223
  books = []
@@ -266,34 +226,33 @@ module Alexandria
266
226
  isbn_list.each do |isbn|
267
227
  begin
268
228
  if isbn[1]
269
- books << Alexandria::BookProviders.isbn_search(isbn[1])
229
+ books << BookProviders.isbn_search(isbn[1])
270
230
  else
271
231
  bad_isbns << isbn[0]
272
232
  end
273
- rescue => e
274
- puts e.message
233
+ rescue BookProviders::SearchEmptyError => ex
234
+ log.debug { ex.message }
275
235
  failed_lookup_isbns << isbn[1]
276
- puts "NOTE : ignoring on_error_cb #{on_error_cb}"
236
+ log.debug { "NOTE : ignoring on_error_cb #{on_error_cb}" }
277
237
  # return nil unless
278
238
  # (on_error_cb and on_error_cb.call(e.message))
279
239
  end
280
240
 
281
- if on_iterate_cb
282
- on_iterate_cb.call(current_iteration += 1, max_iterations)
283
- end
241
+ on_iterate_cb&.call(current_iteration += 1, max_iterations)
284
242
  end
285
- puts "Bad Isbn list: #{bad_isbns.inspect}" if bad_isbns
286
- library = load(name)
287
- 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}" }
288
249
  books.each do |book, cover_uri|
289
- puts "Saving #{book.isbn} cover..." if $DEBUG
250
+ log.debug { "Saving #{book.isbn} cover..." }
290
251
  library.save_cover(book, cover_uri) unless cover_uri.nil?
291
- puts "Saving #{book.isbn}..." if $DEBUG
252
+ log.debug { "Saving #{book.isbn}..." }
292
253
  library << book
293
254
  library.save(book)
294
- if on_iterate_cb
295
- on_iterate_cb.call(current_iteration += 1, max_iterations)
296
- end
255
+ on_iterate_cb&.call(current_iteration += 1, max_iterations)
297
256
  end
298
257
  [library, bad_isbns, failed_lookup_isbns]
299
258
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright (C) 2010 Cathal Mc Ginley
2
4
  #
3
5
  # Alexandria is free software; you can redistribute it and/or
@@ -18,10 +20,10 @@
18
20
  # New CSV import code taken from Palatina and modified for
19
21
  # Alexandria. (29 Apr 2010)
20
22
 
21
- require 'csv'
22
- require 'date'
23
- require 'htmlentities'
24
- require 'tempfile'
23
+ require "csv"
24
+ require "date"
25
+ require "htmlentities"
26
+ require "tempfile"
25
27
 
26
28
  module Alexandria
27
29
  class CSVImport
@@ -45,21 +47,21 @@ module Alexandria
45
47
  class GoodreadsCSVImport < CSVImport
46
48
  def initialize(header)
47
49
  super(header)
48
- @title = index_of('Title')
49
- @author = index_of('Author')
50
- @additional_authors = index_of('Additional Authors')
51
- @isbn = index_of('ISBN')
52
- @publisher = index_of('Publisher')
53
- @publishing_year = index_of('Year Published')
54
- @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")
55
57
 
56
58
  # optional extras
57
- @notes = index_of('Private Notes')
58
- @rating = index_of('My Rating')
59
- @read_count = index_of('Read Count')
60
- @date_read = index_of('Date Read')
61
- @bookshelves = index_of('Bookshelves') # save names as tags
62
- @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")
63
65
  end
64
66
 
65
67
  def row_to_book(row)
@@ -67,11 +69,10 @@ module Alexandria
67
69
  authors = []
68
70
  authors << normalize(row[@author])
69
71
  additional = row[@additional_authors]
70
- additional.split(',').each do |add|
72
+ additional.split(",").each do |add|
71
73
  authors << normalize(add)
72
74
  end
73
- isbn = row[@isbn] # TODO: canonicalize_ean...
74
- isbn = Library.canonicalise_ean(isbn) if isbn
75
+ isbn = Library.canonicalise_ean(row[@isbn])
75
76
  publisher = normalize(row[@publisher])
76
77
  year = row[@publishing_year].to_i
77
78
  edition = normalize(row[@edition])
@@ -88,33 +89,30 @@ module Alexandria
88
89
  book.redd = true if count > 0
89
90
  end
90
91
  if row[@date_read]
91
- begin
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
95
- rescue
96
- #
97
- 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
98
95
  end
99
96
  if row[@mainshelf]
100
- if row[@mainshelf] == 'read'
97
+ case row[@mainshelf]
98
+ when "read"
101
99
  book.redd = true
102
- elsif row[@mainshelf] == 'to-read'
100
+ when "to-read"
103
101
  book.redd = false
104
- book.tags = ['to read']
105
- elsif row[@mainshelf] == 'currently-reading'
102
+ book.tags = ["to read"]
103
+ when "currently-reading"
106
104
  book.redd = false
107
- book.tags = ['currently reading']
105
+ book.tags = ["currently reading"]
108
106
  end
109
107
  end
110
108
  if row[@bookshelves]
111
109
  shelves = normalize(row[@bookshelves]).split
112
110
  shelves.each do |shelf|
113
- tag = shelf.tr('-', ' ')
111
+ tag = shelf.tr("-", " ")
114
112
  book.tags << tag unless book.tags.include? tag
115
113
  end
116
114
  end
117
- puts "Goodreads loading #{book.title}" if $DEBUG
115
+ log.debug { "Goodreads loading #{book.title}" }
118
116
  book
119
117
  end
120
118
  end
@@ -140,9 +138,7 @@ module Alexandria
140
138
  authors << normalize(row[@author])
141
139
  isbn = row[@isbn]
142
140
  if isbn
143
- if isbn =~ /\[([^\]]+)\]/
144
- isbn = Regexp.last_match[1]
145
- end
141
+ isbn = Regexp.last_match[1] if isbn =~ /\[([^\]]+)\]/
146
142
  isbn = Library.canonicalise_ean(isbn)
147
143
  end
148
144
 
@@ -150,14 +146,10 @@ module Alexandria
150
146
  # sometimes "Publisher (YEAR), Edition: NUM, Binding, NUM pages"
151
147
  publisher_info = normalize(row[@publisher_info])
152
148
  publisher = publisher_info
153
- if publisher_info =~ /([^\(]+)\(/
154
- publisher = Regexp.last_match[1]
155
- end
149
+ publisher = Regexp.last_match[1] if publisher_info =~ /([^(]+)\(/
156
150
  edition = publisher_info # binding
157
- edition_info = publisher_info.split(',')
158
- if edition_info.size >= 3
159
- edition = publisher_info.split(',')[-2]
160
- end
151
+ edition_info = publisher_info.split(",")
152
+ edition = publisher_info.split(",")[-2] if edition_info.size >= 3
161
153
 
162
154
  year = row[@publishing_year].to_i
163
155
 
@@ -171,13 +163,13 @@ module Alexandria
171
163
 
172
164
  book.rating = row[@rating].to_i if row[@rating]
173
165
  if row[@tags]
174
- tags = normalize(row[@tags]).split(',')
166
+ tags = normalize(row[@tags]).split(",")
175
167
  tags.each do |tag|
176
168
  book.tags << tag unless book.tags.include? tag
177
169
  end
178
170
  end
179
171
 
180
- puts "LibraryThing loading #{book.title}" if $DEBUG
172
+ log.debug { "LibraryThing loading #{book.title}" }
181
173
  book
182
174
  end
183
175
  end
@@ -191,62 +183,20 @@ module Alexandria
191
183
  is_librarything = false
192
184
  is_goodreads = false
193
185
  header.each do |head|
194
- if head == "'PUBLICATION INFO'"
186
+ case head
187
+ when "'PUBLICATION INFO'"
195
188
  is_librarything = true
196
189
  break
197
- elsif head == 'Year Published'
190
+ when "Year Published"
198
191
  is_goodreads = true
199
192
  break
200
193
  end
201
194
  end
202
- if is_librarything
203
- return LibraryThingCSVImport.new(header)
204
- elsif is_goodreads
205
- return GoodreadsCSVImport.new(header)
206
- end
207
- 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
208
200
  end
209
201
  end
210
-
211
- # def read_csv_file(csv_file)
212
-
213
- # reader = CSV.open(csv_file, 'r')
214
- # # goodreads & librarything now use csv header lines
215
- # header = reader.shift
216
-
217
- # #header.each {|h| puts h }
218
- # importer = identify_csv_type(header)
219
-
220
- # retries = 0
221
- # begin
222
- # reader.each do |row|
223
- # #puts row
224
- # bk = importer.row_to_book(row)
225
- # puts "\"#{bk.title}\" by #{bk.authors.join(', ')}; isbn : #{bk.isbn} #{bk.publisher} | #{bk.publishing_year.inspect} binding:#{bk.edition}"
226
- # if bk.notes.size > 0
227
- # puts " >>> #{bk.notes}"
228
- # end
229
- # end
230
- # rescue
231
- # if retries < 1
232
- # retries += 1
233
- # puts "Retrying..."
234
-
235
- # # probably Goodreads' wonky ISBN fields ,,="043432432X", and such
236
- # # hack to fix up these files
237
- # data = File.read(csv_file)
238
- # data.gsub!(/\,\=\"/, ',"')
239
- # csv_fixed = Tempfile.new("#{csv_file}_fixed")
240
- # csv_fixed.write(data)
241
- # csv_fixed.close
242
- # reader = CSV.open(csv_fixed.path, 'r')
243
- # #puts csv_fixed.path
244
- # header = reader.shift
245
-
246
- # retry
247
- # end
248
-
249
- # end
250
-
251
- # end
252
202
  end