def import_notes(self): fichier = askopenfilename(defaultextension=".backup", filetypes=[(_("Notes (.notes)"), "*.notes"), (_("All files"), "*")], initialdir=LOCAL_PATH, initialfile="", title=_('Import')) if fichier: try: with open(fichier, "rb") as fich: dp = pickle.Unpickler(fich) note_data = dp.load() for i, key in enumerate(note_data): data = note_data[key] note_id = "%i" % (i + self.nb) self.note_data[note_id] = data cat = data["category"] if not CONFIG.has_option("Categories", cat): CONFIG.set("Categories", cat, data["color"]) self.hidden_notes[cat] = {} if data["visible"]: self.notes[note_id] = Sticky(self, note_id, **data) self.nb = int(max(self.note_data.keys(), key=lambda x: int(x))) + 1 self.update_menu() self.update_notes() except Exception: message = _("The file {file} is not a valid .notes file." ).format(file=fichier) showerror(_("Error"), message, traceback.format_exc())
def add_image(self): fichier = askopenfilename(defaultextension=".png", filetypes=[("PNG", "*.png")], initialdir="", initialfile="", title=_('Select PNG image')) if os.path.exists(fichier): self.images.append(PhotoImage(master=self.txt, file=fichier)) self.txt.image_create("current", image=self.images[-1], name=fichier) elif fichier: showerror("Erreur", "L'image %s n'existe pas" % fichier)
def backup(self): """Create a backup at the location indicated by user.""" initialdir, initialfile = os.path.split(PATH_DATA_BACKUP % 0) fichier = asksaveasfilename(defaultextension=".backup", filetypes=[], initialdir=initialdir, initialfile="notes.backup0", title=_('Backup Notes')) if fichier: try: with open(fichier, "wb") as fich: dp = pickle.Pickler(fich) dp.dump(self.note_data) except Exception as e: report_msg = e.strerror != 'Permission denied' showerror(_("Error"), _("Backup failed."), traceback.format_exc(), report_msg)
def restore(self, fichier=None, confirmation=True): """Restore notes from backup.""" if confirmation: rep = askokcancel( _("Warning"), _("Restoring a backup will erase the current notes."), icon="warning") else: rep = True if rep: if fichier is None: fichier = askopenfilename(defaultextension=".backup", filetypes=[], initialdir=LOCAL_PATH, initialfile="", title=_('Restore Backup')) if fichier: try: keys = list(self.note_data.keys()) for key in keys: self.delete_note(key) if not os.path.samefile(fichier, PATH_DATA): copy(fichier, PATH_DATA) with open(PATH_DATA, "rb") as myfich: dp = pickle.Unpickler(myfich) note_data = dp.load() for i, key in enumerate(note_data): data = note_data[key] note_id = "%i" % i self.note_data[note_id] = data cat = data["category"] if not CONFIG.has_option("Categories", cat): CONFIG.set("Categories", cat, data["color"]) if data["visible"]: self.notes[note_id] = Sticky(self, note_id, **data) self.nb = len(self.note_data) self.update_menu() self.update_notes() except FileNotFoundError: showerror( _("Error"), _("The file {filename} does not exists.").format( filename=fichier)) except Exception as e: showerror(_("Error"), str(e), traceback.format_exc(), True)
def ok(event): latex = r'%s' % text.get() if latex: if img_name is None: l = [ int(os.path.splitext(f)[0]) for f in os.listdir(PATH_LATEX) ] l.sort() if l: i = l[-1] + 1 else: i = 0 img = "%i.png" % i self.txt.tag_bind(img, '<Double-Button-1>', lambda e: self.add_latex(img)) self.latex[img] = latex else: img = img_name im = os.path.join(PATH_LATEX, img) try: math_to_image(latex, im, fontsize=CONFIG.getint("Font", "text_size") - 2) self.images.append(PhotoImage(file=im, master=self)) if self.txt.tag_ranges("sel"): index = self.txt.index("sel.first") self.txt.delete('sel.first', 'sel.last') else: index = self.txt.index("current") self.txt.image_create(index, image=self.images[-1], name=im) self.txt.tag_add(img, index) top.destroy() except Exception as e: showerror(_("Error"), str(e))
def export_notes(self): export = Export(self) self.wait_window(export) categories_to_export, only_visible = export.get_export() if categories_to_export: initialdir, initialfile = os.path.split(PATH_DATA_BACKUP % 0) fichier = asksaveasfilename(defaultextension=".html", filetypes=[ (_("HTML file (.html)"), "*.html"), (_("Text file (.txt)"), "*.txt"), (_("All files"), "*") ], initialdir=initialdir, initialfile="", title=_('Export Notes As')) if fichier: try: if os.path.splitext(fichier)[-1] == ".html": # --- html export cats = {cat: [] for cat in categories_to_export} for key in self.note_data: cat = self.note_data[key]["category"] if cat in cats and ( (not only_visible) or self.note_data[key]["visible"]): cats[cat].append( (self.note_data[key]["title"], cst.note_to_html(self.note_data[key], self))) text = "" for cat in cats: cat_txt = "<h1 style='text-align:center'>" + _( "Category: {category}").format( category=cat) + "<h1/>\n" text += cat_txt text += "<br>" for title, txt in cats[cat]: text += "<h2 style='text-align:center'>%s</h2>\n" % title text += txt text += "<br>\n" text += "<hr />" text += "<br>\n" text += '<hr style="height: 8px;background-color:grey" />' text += "<br>\n" with open(fichier, "w") as fich: fich.write('<body style="max-width:30em">\n') fich.write( text.encode( 'ascii', 'xmlcharrefreplace').decode("utf-8")) fich.write("\n</body>") # if os.path.splitext(fichier)[-1] == ".txt": else: # --- txt export # export notes to .txt: all formatting is lost cats = {cat: [] for cat in categories_to_export} for key in self.note_data: cat = self.note_data[key]["category"] if cat in cats and ( (not only_visible) or self.note_data[key]["visible"]): cats[cat].append( (self.note_data[key]["title"], cst.note_to_txt(self.note_data[key]))) text = "" for cat in cats: cat_txt = _("Category: {category}").format( category=cat) + "\n" text += cat_txt text += "=" * len(cat_txt) text += "\n\n" for title, txt in cats[cat]: text += title text += "\n" text += "-" * len(title) text += "\n\n" text += txt text += "\n\n" text += "-" * 30 text += "\n\n" text += "#" * 30 text += "\n\n" with open(fichier, "w") as fich: fich.write(text) # else: # # --- pickle export # note_data = {} # for key in self.note_data: # if self.note_data[key]["category"] in categories_to_export: # if (not only_visible) or self.note_data[key]["visible"]: # note_data[key] = self.note_data[key] # # with open(fichier, "wb") as fich: # dp = pickle.Pickler(fich) # dp.dump(note_data) except Exception as e: report_msg = e.strerror != 'Permission denied' showerror(_("Error"), str(e), traceback.format_exc(), report_msg)