def show_item(name): ''' extracts content of a given entry ''' if config.AUTHENTICATE and not authenticator.is_auth(flask.session): return None # shouldn't happen src = store.open_data_source(authenticator.username(flask.session)) first = True title = None is_match = False count = 0 out = [] for line in src: if first: first = False title = line if line.strip('\n') == name: is_match = True count += 1 else: if line.strip('\n') == TERMINATOR: first = True is_match = False if is_match: out.append(line) return ''.join(out)
def remove_item(name): ''' removes specified entry ''' if config.AUTHENTICATE and not authenticator.is_auth(flask.session): return None # shouldn't happen src = store.open_data_source(authenticator.username(flask.session)) target = store.open_data_target(authenticator.username(flask.session)) found = False in_found = False first = True new_entry = '' # write everything except the entry of interest for line in src: if in_found: if line.strip('\n') == TERMINATOR: in_found = False first = True else: new_entry += line else: if first and line.lower().strip('\n') == name.lower(): # case insensitive match new_entry = line found = True in_found = True first = False else: target.write(line) if line.strip('\n') == TERMINATOR: first = True else: first = False target.close() store.move_data_target(authenticator.username(flask.session)) return "removed"
def show_list(filter=None): if config.AUTHENTICATE and not authenticator.is_auth(flask.session): return None # shouldn't happen src = store.open_data_source(authenticator.username(flask.session)) first = True content = None count = 0 result = [] for line in src: if first: # start of entry first = False if not filter or re.search(filter, line.strip('\n'), re.I) is not None: result.append([line.strip('\n'), None]) # title, content count += 1 content = [] elif line.strip('\n') == TERMINATOR: # end of entry first = True if content is not None: result[-1][1] = ''.join(content) content = None else: if content is not None: content.append(line) return flask.jsonify(data=result)
def export(): if config.AUTHENTICATE and not authenticator.is_auth(flask.session): return flask.redirect(flask.url_for('login')) src = store.open_data_source(authenticator.username(flask.session)) response = flask.make_response(src.read()) response.headers['Content-Type'] = 'text/plain' response.headers['Content-Disposition'] = 'attachment; filename=notes.txt' return response
def create_item(name): ''' creates an empty entry ''' if config.AUTHENTICATE and not authenticator.is_auth(flask.session): return None # shouldn't happen src = store.open_data_source(authenticator.username(flask.session)) target = store.open_data_target(authenticator.username(flask.session)) found = False in_found = False first = True new_entry = '' # write everything except the entry of interest for line in src: if in_found: if line.strip('\n') == TERMINATOR: in_found = False first = True else: new_entry += line else: if first and line.lower().strip( '\n') == name.lower(): # case insensitive match new_entry = line found = True in_found = True first = False else: target.write(line) if line.strip('\n') == TERMINATOR: first = True else: first = False if found: return "exists" else: new_entry = '{0}\n{1}\n{2}\n'.format(name, DEFAULT_NOTE, TERMINATOR) target.write(new_entry) target.close() store.move_data_target(authenticator.username(flask.session)) return "created"
def create_item(name): ''' creates an empty entry ''' if config.AUTHENTICATE and not authenticator.is_auth(flask.session): return None # shouldn't happen src = store.open_data_source(authenticator.username(flask.session)) target = store.open_data_target(authenticator.username(flask.session)) found = False in_found = False first = True new_entry = '' # write everything except the entry of interest for line in src: if in_found: if line.strip('\n') == TERMINATOR: in_found = False first = True else: new_entry += line else: if first and line.lower().strip('\n') == name.lower(): # case insensitive match new_entry = line found = True in_found = True first = False else: target.write(line) if line.strip('\n') == TERMINATOR: first = True else: first = False if found: return "exists" else: new_entry = '{0}\n{1}\n{2}\n'.format(name, DEFAULT_NOTE, TERMINATOR) target.write(new_entry) target.close() store.move_data_target(authenticator.username(flask.session)) return "created"
def rename_item(name, target): if config.AUTHENTICATE and not authenticator.is_auth(flask.session): return None # shouldn't happen src_fh = store.open_data_source(authenticator.username(flask.session)) target_fh = store.open_data_target(authenticator.username(flask.session)) found = False in_found = False first = True new_entry = '' # write everything except the entry of interest for line in src_fh: if in_found: if line.strip('\n') == TERMINATOR: in_found = False first = True else: new_entry += line else: if first and line.lower().strip( '\n') == name.lower(): # case insensitive match new_entry = '{}\n'.format(target) found = True in_found = True first = False else: if first and line.lower().strip( '\n') == target.lower(): # case insensitive match return "exists" target_fh.write(line) if line.strip('\n') == TERMINATOR: first = True else: first = False new_entry = '{0}\n{1}\n'.format(new_entry, TERMINATOR) target_fh.write(new_entry) target_fh.close() store.move_data_target(authenticator.username(flask.session)) return "renamed"
def save_item(name): ''' updates entry with new content ''' if config.AUTHENTICATE and not authenticator.is_auth(flask.session): return None # shouldn't happen src = store.open_data_source(authenticator.username(flask.session)) target = store.open_data_target(authenticator.username(flask.session)) found = False in_found = False first = True new_entry = '' # write everything except the entry of interest for line in src: if in_found: if line.strip('\n') == TERMINATOR: in_found = False first = True else: new_entry += line else: if first and line.lower().strip( '\n') == name.lower(): # case insensitive match new_entry = line found = True in_found = True first = False else: target.write(line) if line.strip('\n') == TERMINATOR: first = True else: first = False # make changes or add new target.write('{0}\n'.format(name)) target.write(flask.request.form["content"]) target.write('\n{0}\n'.format(TERMINATOR)) target.close() store.move_data_target(authenticator.username(flask.session)) return "saved"
def save_item(name): ''' updates entry with new content ''' if config.AUTHENTICATE and not authenticator.is_auth(flask.session): return None # shouldn't happen src = store.open_data_source(authenticator.username(flask.session)) target = store.open_data_target(authenticator.username(flask.session)) found = False in_found = False first = True new_entry = '' # write everything except the entry of interest for line in src: if in_found: if line.strip('\n') == TERMINATOR: in_found = False first = True else: new_entry += line else: if first and line.lower().strip('\n') == name.lower(): # case insensitive match new_entry = line found = True in_found = True first = False else: target.write(line) if line.strip('\n') == TERMINATOR: first = True else: first = False # make changes or add new target.write('{0}\n'.format(name)) target.write(flask.request.form["content"]) target.write('\n{0}\n'.format(TERMINATOR)) target.close() store.move_data_target(authenticator.username(flask.session)) return "saved"
def rename_item(name, target): if config.AUTHENTICATE and not authenticator.is_auth(flask.session): return None # shouldn't happen src_fh = store.open_data_source(authenticator.username(flask.session)) target_fh = store.open_data_target(authenticator.username(flask.session)) found = False in_found = False first = True new_entry = '' # write everything except the entry of interest for line in src_fh: if in_found: if line.strip('\n') == TERMINATOR: in_found = False first = True else: new_entry += line else: if first and line.lower().strip('\n') == name.lower(): # case insensitive match new_entry = '{}\n'.format(target) found = True in_found = True first = False else: if first and line.lower().strip('\n') == target.lower(): # case insensitive match return "exists" target_fh.write(line) if line.strip('\n') == TERMINATOR: first = True else: first = False new_entry = '{0}\n{1}\n'.format(new_entry, TERMINATOR) target_fh.write(new_entry) target_fh.close() store.move_data_target(authenticator.username(flask.session)) return "renamed"
def remove_item(name): ''' removes specified entry ''' if config.AUTHENTICATE and not authenticator.is_auth(flask.session): return None # shouldn't happen src = store.open_data_source(authenticator.username(flask.session)) target = store.open_data_target(authenticator.username(flask.session)) found = False in_found = False first = True new_entry = '' # write everything except the entry of interest for line in src: if in_found: if line.strip('\n') == TERMINATOR: in_found = False first = True else: new_entry += line else: if first and line.lower().strip( '\n') == name.lower(): # case insensitive match new_entry = line found = True in_found = True first = False else: target.write(line) if line.strip('\n') == TERMINATOR: first = True else: first = False target.close() store.move_data_target(authenticator.username(flask.session)) return "removed"