def save_conf(self): filepath = os.path.join(self.app_data_dir, 'conf.yml') self.conf['motome_version'] = VERSION self.conf['conf_update'] = time.time() try: data = yaml.safe_dump(self.conf, default_flow_style=False) out = '# Motome configuration values\n---\n' + data + '...' NoteModel.enc_write(filepath, out) except IOError: pass
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 update_ui_views_history(self): if self.old_data is None: self.update_ui_views() else: old_content, old_meta = NoteModel.parse_note_content( self.old_data[0]) # update the note editor self.noteEditor.blockSignals(True) self.noteEditor.set_note_text(old_content) self.noteEditor.blockSignals(False) # update the tag editor self.tagEditor.blockSignals(True) try: self.tagEditor.setText(old_meta['tags']) except (TypeError, KeyError): # no metadata or no tag metadata self.tagEditor.setText('') self.tagEditor.blockSignals(False) # update the preview and diff panes self.update_ui_preview() self.update_ui_diff() # update the window title dt_str = self.old_data[1] dt = history_timestring_to_datetime(dt_str) tab_date = '[' + human_date(dt) + ']' self.setWindowTitle(' '.join( [WINDOW_TITLE, '-', self.current_note.title, tab_date]))
def setUp(self): self.notepaths = set( glob.glob(TESTER_NOTES_PATH + '/*' + NOTE_EXTENSION)) self.db_notes = dict() for filepath in self.notepaths: filename = os.path.basename(filepath) if filename not in list(self.db_notes.keys()): note = NoteModel(filepath) self.db_notes[note.filename] = note
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 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 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 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 new_note(self): tagged_title = self.ui.omniBar.text() if tagged_title == '': return if len(self.notesList.findItems(tagged_title, QtCore.Qt.MatchWildcard)) > 0: return # build new note name self.search.query = tagged_title if len(self.search.use_words) == 0: # no words to use in the title return title = ' '.join(self.search.use_words) tags = ' '.join(self.search.use_tags) # build the new note filename = clean_filename(title) + NOTE_EXTENSION filepath = os.path.join(self.notes_dir, filename) content = title + '\n' new_note = NoteModel(filepath) new_note.metadata['title'] = title new_note.metadata['tags'] = tags new_note.content = content # update new_note.save_to_file() self.notesList.update_list() self.ui.omniBar.setText('') self.query = '' try: item = self.notesList.findItems(tagged_title, QtCore.Qt.MatchWildcard)[0] self.notesList.setCurrentItem(item) except IndexError: for item in self.notesList.all_items[::-1]: if not item.notemodel.pinned: self.notesList.setCurrentItem(item) # set the focus on the editor and move the cursor to the end self.noteEditor.setFocus() cursor = self.noteEditor.textCursor() cursor.movePosition(QtGui.QTextCursor.MoveOperation.End, QtGui.QTextCursor.MoveMode.MoveAnchor) self.noteEditor.setTextCursor(cursor)
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 _update_notemodel_dict(self): if self.notes_dir is None or self.notes_dir == '': return notepaths = set(glob.glob(self.notes_dir + '/*' + NOTE_EXTENSION)) notenames = map(os.path.basename, notepaths) note_keys = set(self.session_notemodel_dict.keys()) keys_missing_notes = note_keys - set(notenames) # remove keys missing notes for filename in keys_missing_notes: del self.session_notemodel_dict[filename] # add notes missing keys for filepath in notepaths: filename = os.path.basename(filepath) if filename not in self.session_notemodel_dict.keys(): note = NoteModel(filepath) self.session_notemodel_dict[note.filename] = note
def update_ui_diff(self): if self.old_data is not None: new_content = self.current_note.content content, __ = NoteModel.parse_note_content(self.old_data[0]) dt_str = self.old_data[1] dt = history_timestring_to_datetime(dt_str) tab_date = '[' + human_date(dt) + ']' fromdesc = ' '.join([self.current_note.title, tab_date]) diff_html = diff_to_html(content, new_content, fromdesc, self.current_note.title) else: try: diff_html = self.current_note.get_status() except AttributeError: # current_note is None diff_html = '' self.ui.noteDiff.setHtml(diff_html) if self.current_note is not None: self.ui.noteDiff.setDocumentTitle( self.current_note.title) # for things like print to pdf self.ui.noteDiff.reload()
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)