C0 code coverage information
Generated on Tue Oct 16 11:40:48 -0400 2007 with rcov 0.8.0
Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
1 # Copyright (C) 2004-2006 Laurent Sansonetti
2 #
3 # Alexandria is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License as
5 # published by the Free Software Foundation; either version 2 of the
6 # License, or (at your option) any later version.
7 #
8 # Alexandria is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public
14 # License along with Alexandria; see the file COPYING. If not,
15 # write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 # Boston, MA 02111-1307, USA.
17 require 'gettext'
18
19 module Alexandria
20 class ImportFilter
21 attr_reader :name, :patterns, :message
22
23 include GetText
24 extend GetText
25 bindtextdomain(Alexandria::TEXTDOMAIN, nil, nil, "UTF-8")
26
27 def self.all
28 [
29 self.new(_("Autodetect"), ['*'], :import_autodetect),
30 self.new(_("Archived Tellico XML (*.bc, *.tc)"),
31 ['*.tc', '*.bc'], :import_as_tellico_xml_archive),
32 self.new(_("ISBN List (*.txt)"), ['*.txt'],
33 :import_as_isbn_list)
34 ]
35 end
36
37 def on_iterate(&on_iterate_cb)
38 @on_iterate_cb = on_iterate_cb
39 end
40
41 def on_error(&on_error_cb)
42 @on_error_cb = on_error_cb
43 end
44
45 def invoke(library_name, filename)
46 puts "Selected: #{@message} -- #{library_name} -- #{filename}"
47 Library.send(@message, library_name, filename,
48 @on_iterate_cb, @on_error_cb)
49 end
50
51 #######
52 private
53 #######
54
55 def initialize(name, patterns, message)
56 @name = name
57 @patterns = patterns
58 @message = message
59 end
60 end
61
62 class Library
63 def self.import_autodetect(*args)
64 puts args.inspect
65 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"
69 self.import_as_isbn_list(*args)
70 elsif [".tc",".bc"].include? filename[-3..-1]
71 begin
72 self.import_as_tellico_xml_archive(*args)
73 rescue => e
74 puts e.message
75 end
76 else
77 puts "Bailing on this import!"
78 raise "Not supported type"
79 end
80 end
81
82 def self.import_as_tellico_xml_archive(name, filename,
83 on_iterate_cb, on_error_cb)
84 puts "Starting import_as_tellico_xml_archive... "
85 return nil unless system("unzip -qqt \"#{filename}\"")
86 tmpdir = File.join(Dir.tmpdir, "tellico_export")
87 FileUtils.rm_rf(tmpdir) if File.exists?(tmpdir)
88 Dir.mkdir(tmpdir)
89 Dir.chdir(tmpdir) do
90 begin
91 system("unzip -qq \"#{filename}\"")
92 file = File.exists?('bookcase.xml') \
93 ? 'bookcase.xml' : 'tellico.xml'
94 xml = REXML::Document.new(File.open(file))
95 raise unless (xml.root.name == 'bookcase' or
96 xml.root.name == 'tellico')
97 # FIXME: handle multiple collections
98 raise unless xml.root.elements.size == 1
99 collection = xml.root.elements[1]
100 raise unless collection.name == 'collection'
101 type = collection.attribute('type').value.to_i
102 raise unless (type == 2 or type == 5)
103
104 content = []
105 entries = collection.elements.to_a('entry')
106 (total = entries.size).times do |n|
107 entry = entries[n]
108 elements = entry.elements
109 #Feed an array in here, tomorrow.
110 keys = ['isbn', 'publisher', 'pub_year', 'binding']
111
112 book_elements = [elements['title'].text]
113 if elements['authors'] != nil
114 book_elements += [elements['authors'].elements.to_a.map \
115 { |x| x.text }]
116 else
117 book_elements += [[]]
118 end
119 book_elements += keys.map {|key|
120 unless elements[key]
121 nil
122 else
123 elements[key].text
124 end
125 }
126 book_elements[2] = Library.canonicalise_ean(book_elements[2]) unless book_elements[2]== nil # isbn
127 book_elements[4] = book_elements[4].to_i unless book_elements[4]== nil # publishing_year
128 puts book_elements.inspect
129 if elements['cover']
130 cover = elements['cover'].text
131 else
132 cover = nil
133 end
134 puts cover
135 book = Book.new(*book_elements)
136 if elements['rating'] and (0..UI::MainApp::MAX_RATING_STARS).map.member? elements['rating'].text.to_i
137 book.rating = elements['rating'].text.to_i
138 end
139 book.notes = elements['comments'].text if elements['comments']
140 content << [ book, cover]
141 on_iterate_cb.call(n+1, total) if on_iterate_cb
142 end
143
144 library = Library.load(name)
145 content.each do |book, cover|
146 unless cover.nil?
147 library.save_cover(book,
148 File.join(Dir.pwd, "images",
149 cover))
150 end
151 library << book
152 library.save(book)
153 end
154 return [library, []]
155 rescue => e
156 puts e.message
157 return nil
158 end
159 end
160 end
161
162 def self.import_as_isbn_list(name, filename, on_iterate_cb,
163 on_error_cb)
164 puts "Starting import_as_isbn_list... "
165 isbn_list = IO.readlines(filename).map do |line|
166 puts "Trying line #{line}" if $DEBUG
167 # Let's preserve the failing isbns so we can report them later.
168 begin
169 [line.chomp, canonicalise_isbn(line.chomp)] unless line == "\n"
170 rescue => e
171 puts e.message
172 [line.chomp, nil]
173 end
174 end
175 puts "Isbn list: #{isbn_list.inspect}"
176 isbn_list.compact!
177 return nil if isbn_list.empty?
178 max_iterations = isbn_list.length * 2
179 current_iteration = 1
180 books = []
181 bad_isbns = []
182 isbn_list.each do |isbn|
183 begin
184 unless isbn[1]
185 bad_isbns << isbn[0]
186 else
187 books << Alexandria::BookProviders.isbn_search(isbn[1])
188 end
189 rescue => e
190 puts e.message
191 return nil unless
192 (on_error_cb and on_error_cb.call(e.message))
193 end
194
195 on_iterate_cb.call(current_iteration += 1,
196 max_iterations) if on_iterate_cb
197 end
198 puts "Bad Isbn list: #{bad_isbns.inspect}" if bad_isbns
199 library = load(name)
200 puts "Going with these #{books.length} books: #{books.inspect}" if $DEBUG
201 books.each do |book, cover_uri|
202 puts "Saving #{book.isbn} cover..." if $DEBUG
203 library.save_cover(book, cover_uri) if cover_uri != nil
204 puts "Saving #{book.isbn}..." if $DEBUG
205 library << book
206 library.save(book)
207 on_iterate_cb.call(current_iteration += 1,
208 max_iterations) if on_iterate_cb
209 end
210 return [library, bad_isbns]
211 end
212 end
213 end
Generated using the rcov code coverage analysis tool for Ruby version 0.8.0.