def test_add_remove(self): filepath = os.path.join(TESTER_NOTES_PATH, 'zen' + NOTE_EXTENSION) zen_note = NoteModel(filepath) # file doesn't exist yet self.assertFalse(os.path.exists(filepath)) self.assertEqual(zen_note.content, '') self.assertEqual(zen_note.timestamp, -1) self.assertEqual(zen_note.metadata, dict()) self.assertEqual(zen_note.history, list()) self.assertEqual(zen_note.wordset, '') self.assertFalse(zen_note.recorded) self.assertFalse(zen_note.pinned) # add content content = NoteModel.enc_read(ZEN_TEXT_FILE) zen_note.content = content self.assertTrue(os.path.exists(filepath)) self.assertNotEqual(zen_note.metadata, dict()) self.assertNotEqual(zen_note.timestamp, -1) # remove note zen_note.remove() self.assertFalse(os.path.exists(filepath)) self.assertEqual(zen_note.content, '') self.assertEqual(zen_note.timestamp, -1) self.assertEqual(zen_note.metadata, dict()) self.assertEqual(zen_note.history, list()) self.assertEqual(zen_note.wordset, '') self.assertFalse(zen_note.recorded) self.assertFalse(zen_note.pinned)
def test_get_changed_content(self): notename = list(self.db_notes.keys())[0] note = self.db_notes[notename] filepath = note.filepath # Read data from file, not using NoteModel raw_data = NoteModel.enc_read(filepath) content, metadata = NoteModel.parse_note_content(raw_data) timestamp = os.stat(filepath).st_mtime self.assertEqual(note.content, content) self.assertEqual(note.timestamp, timestamp) # Make a change new_content = content + '\nNew line\n' self.assertNotEqual(note.content, new_content) # Write changed data not from NoteModel filedata = new_content + END_OF_TEXT + '\n' for key, value in list(metadata.items()): filedata = filedata + '{0}:{1}\n'.format(key, value) NoteModel.enc_write(filepath, filedata) # Change happened? self.assertNotEqual(note.timestamp, timestamp) # And the content automatically updates when accessed self.assertEqual(note.content, new_content) # Reset file filedata = content + END_OF_TEXT + '\n' for key, value in list(metadata.items()): filedata = filedata + '{0}:{1}\n'.format(key, value) NoteModel.enc_write(filepath, filedata)
def load_styles(self): if 'style' in list(self.conf.keys()): self.style_dir = os.path.join(self.app_data_dir, 'styles', self.conf['style']) else: self.style_dir = os.path.join(APP_DIR, 'styles', 'default') editor_path = os.path.join(self.style_dir, 'editor.css') preview_path = os.path.join(self.style_dir, 'preview.css') diff_path = os.path.join(self.style_dir, 'diff.css') if os.path.exists(editor_path): editor_style = NoteModel.enc_read(editor_path) self.noteEditor.setStyleSheet(editor_style) self.noteEditor.document().setDefaultStyleSheet(editor_style) if os.path.exists(preview_path): preview_style = NoteModel.enc_read(preview_path) self.ui.notePreview.document().setDefaultStyleSheet(preview_style) if os.path.exists(diff_path): diff_style = NoteModel.enc_read(diff_path) self.ui.noteDiff.document().setDefaultStyleSheet(diff_style)
def test_add_record(self): filepath = os.path.join(TESTER_NOTES_PATH, 'zen' + NOTE_EXTENSION) zen_note = NoteModel(filepath) # add content content = NoteModel.enc_read(ZEN_TEXT_FILE) zen_note.content = content # record self.assertFalse(os.path.exists(zen_note.historypath)) self.assertEqual(zen_note.load_old_note(0), (None, None)) zen_note.record(TESTER_NOTES_PATH) self.assertTrue(os.path.exists(zen_note.historypath)) self.assertNotEqual(zen_note.history, list()) self.assertNotEqual(zen_note.load_old_note(0), (None, None))
def test_add_rename(self): filepath = os.path.join(TESTER_NOTES_PATH, 'zen' + NOTE_EXTENSION) zen_note = NoteModel(filepath) # add content content = NoteModel.enc_read(ZEN_TEXT_FILE) zen_note.content = content self.assertTrue(os.path.exists(filepath)) self.assertNotEqual(zen_note.metadata, dict()) self.assertNotEqual(zen_note.timestamp, -1) # rename filepath2 = os.path.join(TESTER_NOTES_PATH, 'zen2' + NOTE_EXTENSION) zen_note.notename = 'zen2' self.assertTrue(os.path.exists(filepath2)) self.assertEqual(zen_note.notename, 'zen2')
def load_conf(self): filepath = os.path.join(self.app_data_dir, 'conf.yml') try: data = NoteModel.enc_read(filepath) try: self.conf = yaml.safe_load(data) except yaml.YAMLError: self.conf = {} if not 'conf_notesLocation' in list(self.conf.keys()): self.first_run = True else: self.notes_dir = self.conf['conf_notesLocation'] self.notes_data_dir = os.path.join(self.notes_dir, NOTE_DATA_DIR) except IOError: # no conf file self.first_run = True
def transition_versions(notes_dir): """ Change notes from the old metadata style to the new (0.1.0 - 0.2.0) :param notes_dir: :return: """ notepaths = set(glob.glob(notes_dir + '/*' + '.txt')) for notepath in notepaths: try: data = NoteModel.enc_read(notepath) c, m = parse_note_content_old(data) if len(list(m.keys())) == 0: new_data = c else: new_data = c + YAML_BRACKET + '\n' + yaml.safe_dump(m, default_flow_style=False) + YAML_BRACKET NoteModel.enc_write(notepath, new_data) except Exception as e: logging.error('[transition_versions] %r' % e)
def transition_versions(notes_dir): """ Change notes from the old metadata style to the new (0.1.0 - 0.2.0) :param notes_dir: :return: """ notepaths = set(glob.glob(notes_dir + '/*' + '.txt')) for notepath in notepaths: try: data = NoteModel.enc_read(notepath) c, m = parse_note_content_old(data) if len(m.keys()) == 0: new_data = c else: new_data = c + YAML_BRACKET + '\n' + yaml.safe_dump(m, default_flow_style=False) + YAML_BRACKET NoteModel.enc_write(notepath, new_data) except Exception as e: logging.error('[transition_versions] %r' % e)
def open_and_parse_note(filepath): data = NoteModel.enc_read(filepath) return NoteModel.parse_note_content(data)
def setup_mainwindow(self): # load main window style main_style_path = os.path.join(APP_DIR, 'styles', 'main_window.css') main_style = NoteModel.enc_read(main_style_path) self.setStyleSheet(main_style)