def import_from_xml_file(self, fname): if os.path.isfile(fname): try: self.data = [] tree = ElementTree() tree.parse(fname) for i in tree.getroot().iter("book"): bdata = {} for child in i: if child.tag == "authors": bdata["authors"] = [] for j in child.iter("name"): bdata["authors"].append(j.text) elif child.tag == "isbn13": bdata["isbn13"] = ISBN.from_string(child.text) elif child.tag == "isbn10": bdata["isbn10"] = ISBN.from_string(child.text) elif child.tag == "categories": bookcats = [] for j in child.iter("item"): newcat = [] for k in j.iter("n"): col = None if 'color' in k.attrib: col = k.attrib['color'] newcat.append([k.text, col]) bookcats.append(self.categories.register(newcat)) bdata["categories"] = set() for i in bookcats: addi = True for j in bookcats: if j == self.categories.sub_category(j, i): addi = False if addi: bdata["categories"].add(i) else: bdata[child.tag] = child.text b = Book(bdata) self.add_book(b, localCover=False) except Exception as e: raise e else: self.data = [] ## TODO: use something more efficient (tree etc.)
def on_add_file(self, widget): dialog = Gtk.FileChooserDialog("Add eBook to Library", self, Gtk.FileChooserAction.OPEN, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK)) self.add_filters(dialog) if self.last_location != None: dialog.set_filename("/home/koe/Documents/Library") response = dialog.run() fn = dialog.get_filename() dialog.destroy() if response == Gtk.ResponseType.OK: self.last_location = os.path.dirname(fn) try: b = self.library.add_local_book(fn) if b != None: self.filtered_list.add_book_item(b, self.library) except Exception as inst: if inst.args[0] == "ExtractionError": validInput = False isbn = [] while validInput == False: inputDialog = Gtk.MessageDialog(self, Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT, Gtk.MessageType.QUESTION, Gtk.ButtonsType.OK_CANCEL, "Insert ISBN") inputDialog.set_title("ISBN") inputDialog.format_secondary_markup("The ISBN automaitc extraction of a book failed.\n"+ "If you want to add the book <a href='" + fn + "'>" + os.path.basename(fn) + "</a> to the library, provide a valid ISBN number:") dialogBox = inputDialog.get_content_area() infoText = Gtk.Label() #infoText.set_markup() inputDialog.get_children()[0].get_children()[0].get_children()[1].get_children()[1].connect('activate-link', self.on_file_link_clicked) userInput = Gtk.Entry() userInput.set_size_request(100,0) dialogBox.pack_end(userInput, False, False, 0) #dialogBox.pack_end(infoText, False, False, 0) inputDialog.show_all() response = inputDialog.run() text = userInput.get_text() inputDialog.destroy() if response == Gtk.ResponseType.OK: validInput = True try: isbn = ISBN.from_string("".join(i for i in text if ord(i)<128)) b = self.library.add_local_book(fn, isbn=isbn) if b != None: self.filtered_list.add_book_item(b, self.library) except TypeError: validInput = False else: validInput = True else: raise inst errorDialog = Gtk.MessageDialog(self, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, "Importing the Book failed.") errorDialog.format_secondary_text("While importing the Book:\n'" + fn + "' the following error occured:\n" + "\n".join(map(str, inst.args))) errorDialog.run() errorDialog.destroy()