示例#1
0
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)
示例#2
0
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"
示例#3
0
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)
示例#4
0
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)
示例#5
0
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
示例#6
0
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
示例#7
0
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"
示例#8
0
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"
示例#9
0
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"
示例#10
0
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"
示例#11
0
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"
示例#12
0
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"
示例#13
0
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"
示例#14
0
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)