예제 #1
0
def delete(text=None, preview=None):
    """ Delete lines containing given text
    """
    if text is None:
        log.error(ERROR_MISSING_TEXT)
        return

    OPTION_PREVIEW = "preview"

    no_updates = True
    with open(FILE, 'r+') as f:
        lines = f.read().splitlines()
        new_lines = []
        for line in lines:
            if text in line:
                if preview == OPTION_PREVIEW:
                    log.info("To be deleted: {}".format(line))
                    new_lines.append(line)
                else:
                    log.info("Deleted: {}".format(line))
                    no_updates = False
            else:
                new_lines.append(line)

    with open(FILE, 'w') as f:
        f.write("\n".join(new_lines))

    if (no_updates):
        log.info(INFO_NO_CHANGES)
예제 #2
0
def edit(old_line=None, new_line=None):
    """ Replace old line with new one
    """
    if (old_line is None) or (new_line is None):
        log.error(ERROR_MISSING_LINE)
        return

    no_updates = True
    with open(FILE, 'r+') as f:
        lines = f.read().splitlines()
        new_lines = []
        for line in lines:
            updated_line = line
            if old_line in line:
                # So, it's working with parts of an old line, too.
                updated_line = new_line
                no_updates = False
                log.info("Replaced with: {}".format(new_line))
            new_lines.append(updated_line)
        new_lines.append("")

    with open(FILE, 'w') as f:
        f.write("\n".join(new_lines))

    if (no_updates):
        log.info(INFO_NO_CHANGES)
예제 #3
0
def total(tag_name=None, start_date=None):
    """ Show total value for a given tag
    """
    if tag_name is None:
        log.error(ERROR_MISSING_TAG_NAME)
        return

    if tag_name not in get_tags():
        log.error(ERROR_UNKNOWN_TAG_NAME)
        return

    if start_date is not None:
        if start_date == OPTION_TODAY:
            start_date = get_date(DEFAULT_DATE)
        else:
            start_date = get_date(start_date)

    total = DEFAULT_TOTAL

    with open(FILE, 'r') as f:
        lines = f.read().splitlines()
        for line in lines:
            parts = line.split(" ")
            if parts[2] == tag_name:
                if start_date is not None:
                    if get_date(parts[0].split("/")[1]) >= start_date:
                        print nice(line)
                        total += float(parts[3])
                else:
                    print nice(line)
                    total += float(parts[3])

    log.info("TOTAL: {}".format(total))
예제 #4
0
def new(tag_name=None, start_value=None):
    """ Create new tag and assign a start value
    """
    if tag_name is None:
        log.error(ERROR_MISSING_TAG_NAME)
        return

    if tag_name in get_tags():
        log.error(ERROR_EXISTING_TAG_NAME)
        return

    if not is_valid(tag_name):
        log.error(ERROR_WRONG_TAG_NAME)
        return

    if start_value is None:
        start_value = DEFAULT_START_VALUE_NEW

    else:
        try:
            start_value = float(start_value)
        except Exception:
            log.error(ERROR_WRONG_VALUE)
            return

    line = '{} {} {} {} \n'.format(now(), ACTION_NEW, tag_name,
                                   str(start_value))
    write(line)
    log.info('{} {}'.format(ACTION_NEW, line))
예제 #5
0
def get_date(ddmmyyyy=None):
    """ Return date object
    """
    if ddmmyyyy is None:
        log.error(ERROR_MISSING_DATE_USE_DEFAULT)
        ddmmyyyy = DEFAULT_DATE

    parts = ddmmyyyy.split(".")
    try:
        res = date(int(parts[2]), int(parts[1]), int(parts[0]))
    except Exception:
        log.error(ERROR_WRONG_DATE_USE_DEFAULT)
        parts = DEFAULT_DATE.split(".")
        res = date(int(parts[2]), int(parts[1]), int(parts[0]))

    return res
예제 #6
0
def add(tag_name=None, value=None, story=None):
    """ Add value for given tag
    """
    if value is None:
        value = DEFAULT_UNIT

    if tag_name is None:
        log.error(ERROR_MISSING_TAG_NAME)
        return

    if tag_name not in get_tags():
        log.error(ERROR_UNKNOWN_TAG_NAME)
        return

    try:
        value = float(value)
    except Exception:
        log.error(ERROR_WRONG_VALUE)
        return

    if story is None:
        story = SEPARATOR
    else:
        story = "{}{}".format(SEPARATOR, story)

    line = '{} {} {} {} {} \n'.format(now(), ACTION_ADD, tag_name, str(value),
                                      story)
    write(line)
    log.info('{} {}'.format(ACTION_ADD, line))
예제 #7
0
def list(tag_name=None, start_date=None):
    """ List records for a given tag (optional: starting from a given date)
    """
    if tag_name is not None:
        if tag_name not in get_tags():
            log.error(ERROR_UNKNOWN_TAG_NAME)
            return

        if start_date is not None:
            if start_date == OPTION_TODAY:
                start_date = get_date(DEFAULT_DATE)
            else:
                start_date = get_date(start_date)

            with open(FILE, 'r') as f:
                lines = f.read().splitlines()
                for line in lines:
                    parts = line.split(" ")
                    if parts[2] == tag_name:
                        if start_date is not None:
                            if get_date(parts[0].split("/")[1]) >= start_date:
                                print nice(line)

        else:
            with open(FILE, 'r') as f:
                lines = f.read().splitlines()
                for line in lines:
                    parts = line.split(" ")
                    if parts[2] == tag_name:
                        print nice(line)

    else:
        with open(FILE, 'r') as f:
            lines = f.read().splitlines()
            for line in lines:
                print line
예제 #8
0
def rename(tag_name=None, new_tag_name=None):
    """ Replace tag_name with new_tag_name
    """
    if tag_name is None:
        log.error(ERROR_MISSING_TAG_NAME)
        return

    tags = get_tags()

    if tag_name not in tags:
        log.error(ERROR_UNKNOWN_TAG_NAME)
        return

    if new_tag_name is None:
        log.error(ERROR_MISSING_TAG_NAME)
        return
    else:
        if not is_valid(new_tag_name):
            log.error(ERROR_WRONG_TAG_NAME)
            return

        if new_tag_name in tags:
            log.warning(WARNING_RENAME_WITH_EXISTING_TAG_NAME)

    with open(FILE, 'r+') as f:
        lines = f.read().splitlines()
        new_lines = []
        for line in lines:
            new_line = line
            parts = line.split(" ")
            if parts[2] == tag_name:
                parts[2] = new_tag_name
                new_line = " ".join(parts)
                log.info("Renamed: {}".format(new_line))
            new_lines.append(new_line)

    with open(FILE, 'w') as f:
        f.write("\n".join(new_lines))