def on_export_button(self): """ Method called when the export button in the toolbar is pressed. """ filename = tkFileDialog.asksaveasfilename(filetypes=[("Word List", ".tldr")]) if not filename: return try: with open(filename, "w") as f: words = sorted(list(self.view.right_list.model.values()), key=lambda x: x.word.lower()) f.write("# Autogenerated by Spellorama Teacher Interface\n") f.write("# {0}\n".format(datetime.datetime.now().isoformat())) f.write("# {0} words\n".format(len(words))) for line in gen_tldr(words): f.write(line) except IOError as e: tkMessageBox.showwarning( "Export Error", "Could not save {0}".format(filename) ) logging.warn(e) tkMessageBox.showinfo( "Export", "Exported successfully to {0}".format(filename) ) lid = database.add_list(filename) for w in words: wid = database.get_word_id(w.word, w.definition, w.example, w.difficulty).fetchone()[0] database.add_wordlist_mappings(lid, wid) self.populate()
def on_import_button(self): """ Method called when the import button in the toolbar is pressed. """ filenames = tkFileDialog.askopenfilenames(filetypes=[("Word List", ".tldr")]) for filename in filenames: try: with open(filename, "r") as f: list_Id = database.add_list(filename) data = list(parse_tldr(f)) for word in data: try: word_Id = database.add_word(word.word, word.definition, word.example, word.difficulty) except Exception: pass else: database.add_wordlist_mappings(list_Id, word_Id) except IOError as e: tkMessageBox.showwarning( "Import Error", "Could not open {0}".format(filename) ) logging.warn(e) else: self.view.left_list.model[os.path.relpath(filename)] = data self.populate()
def on_create_button(self): """ Method called when the create new word button in the toolbar is pressed. """ word = new_word_prompt() if word is not None: try: word_Id = database.add_word(word.word, word.definition, word.example, word.difficulty) except Exception: pass else: database.add_wordlist_mappings(1, word_Id) self.populate() self.on_left_list_select()