Beispiel #1
0
def edit_single_date(date):
    """Edit a single entry in external editor without initializing screen."""
    date = parse_date(date)
    if not date:
        raise WLError('Unrecognised date format\n')
    edit_date(date)
    metadata = Metadata(date.year, date.month)
    metadata.load_day(date.day)
    metadata.write()
Beispiel #2
0
    def test_loading(self):
        today = datetime.date.today()
        m = Metadata(today.year, today.month)
        m.write()
        self._check_entries(today.year, today.month, m)

        date = datetime.date(today.year, today.month,
                             random.randint(1, lastday(today)))
        month_dir = os.path.join(conf['entries_dir'], date.strftime('%Y-%m'))
        path = os.path.join(month_dir, date.strftime('%d'))
        with open(path) as f:
            lines = f.read().split('\n')
        lines.pop(random.randint(0, len(lines) - 1))
        with open(path, 'w') as f:
            f.write('\n'.join(lines))
        self._check_entries(today.year, today.month, m, [date.day])

        m.load_day(date.day)
        self._check_entries(today.year, today.month, m)

        m1 = Metadata(today.year, today.month)
        self._check_entries(today.year, today.month, m1, [date.day])

        m.write()
        m2 = Metadata(today.year, today.month)
        self._check_entries(today.year, today.month, m2)
Beispiel #3
0
def show_date_list(tag, dates, area_id=None, text_area=None):
    """Show the list of entries for a tag."""
    labels = map(format_date, dates)
    sl = ScrollableList(labels, tag, area_id=area_id)
    sl.draw()
    date = dates[sl.get_current_index()]
    metadata = Metadata.get(date.year, date.month)
    if not text_area:
        text_area = TextArea()
    text_area.set_title()
    text_area.show_text(metadata.text(date.day))
    while 1:
        try:
            kn = curses.keyname(sl.window.getch())
        except KeyboardInterrupt:
            break
        if kn in Config.general_keys['quit']:
            raise WLQuit
        if kn in Config.general_keys['quit_mode']:
            break
        if kn == 'KEY_RESIZE':
            ScreenManager.resize()
        if sl.hidden:
            continue
        if kn in Config.tag_details_keys['edit']:
            date = dates[sl.get_current_index()]
            edit_date(date)
            sl.draw()
            metadata.load_day(date.day)
            text_area.show_text(metadata.text(date.day))
        elif kn in Config.tag_details_keys['edits']:
            date = dates[sl.get_current_index()]
            edits = get_edits(date)
            if edits:
                show_edits(date, edits, text_area.area_id)
                ScreenManager.restore_area(text_area.area_id)
                text_area.show_text(metadata.text(date.day))
        else:
            sl.handle_keypress(kn)
            date = dates[sl.get_current_index()]
            metadata = Metadata.get(date.year, date.month)
            text_area.show_text(metadata.text(date.day))
    Metadata.write_all()
Beispiel #4
0
def show_tag(tag):
    """Find all entries for a tag and call show_date_list."""
    dates = []
    for year, month in get_all_months(conf['data_dir']):
        m = Metadata.get(year, month)
        if tag in m.tags:
            for day in m.tags[tag]:
                dates.append(datetime.date(year, month, day))
    if not dates:
        raise WLError('No entries for tag %s' % tag)
    else:
        show_date_list(tag, dates)
Beispiel #5
0
 def tag_info(index):
     tag, dates = items[index]
     d = dates[-1]
     text_area.show_text(Metadata.get(d.year, d.month).text(d.day))
Beispiel #6
0
def show_tags(area_id=None, text_area=None):
    """Show all tags sorted by entries number as a scrollable list."""
    screen = ScreenManager.screen
    screen.addstr(0, 0, 'loading...')
    screen.refresh()

    tags = {}

    for year, month in get_all_months(conf['data_dir']):
        m = Metadata.get(year, month)
        for tag, days in m.tags.items():
            for day in days:
                date = datetime.date(year, month, day)
                try:
                    tags[tag].append(date)
                except KeyError:
                    tags[tag] = [date]

    if not tags:
        raise WLError('No tags found')
    items = sorted(sorted(tags.items(), key=lambda i: i[0]),
                   key=lambda i: len(i[1]), reverse=True)
    tl = ['%s (%d)' % (item[0], len(item[1])) for item in items]
    sl = ScrollableList(tl, area_id=area_id)
    if text_area:
        text_area.set_title('Last entry:')
    else:
        text_area = TextArea()
        text_area.set_title('Last entry:')
    ScreenManager.draw_all()
    def tag_info(index):
        tag, dates = items[index]
        d = dates[-1]
        text_area.show_text(Metadata.get(d.year, d.month).text(d.day))
    tag_info(sl.get_current_index())
    while 1:
        try:
            kn = curses.keyname(sl.window.getch())
        except KeyboardInterrupt:
            break
        except ValueError:
            continue
        if kn in Config.general_keys['quit']:
            raise WLQuit
        if kn in Config.general_keys['quit_mode']:
            break
        if kn == 'KEY_RESIZE':
            ScreenManager.resize()
        if sl.hidden:
            continue
        elif kn in Config.tags_keys['details']:
            tag, dates = items[sl.get_current_index()]
            show_date_list(tag, dates, sl.area_id, text_area)
            ScreenManager.restore_area(sl.area_id)
            sl.reinit()
            text_area.set_title('Last entry:')
            tag_info(sl.get_current_index())
        else:
            sl.handle_keypress(kn)
            tag_info(sl.get_current_index())
    Metadata.write_all()
Beispiel #7
0
def show_calendar():
    """Show an interactive calendar.

    Show the calendar on the left side of the screen and some metadata about
    the selected date on the right. Any entry can be edited in external editor.
    """
    today = datetime.date.today()
    year, month = today.year, today.month
    cal = Calendar(year, month, today.day, entry_exists)
    metadata = Metadata.get(year, month)
    text_area = TextArea()
    ScreenManager.draw_all()
    d = cal.get_current_date()
    text_area.show_text(metadata.text(d.day))
    keys = Config.calendar_keys
    while 1:
        try:
            kn = curses.keyname(cal.window.getch())
        except KeyboardInterrupt:
            break
        except ValueError:
            continue
        if kn in Config.general_keys['quit']:
            raise WLQuit
        if kn in Config.general_keys['quit_mode']:
            break
        if kn == 'KEY_RESIZE':
            ScreenManager.resize()
        if cal.hidden:
            continue
        if kn in keys['left']:
            moved = cal.move_left()
            if not moved:
                cal = cal.get_previous_calendar()
                cal.draw()
                metadata = Metadata.get(cal.year, cal.month)
            text_area.show_text(metadata.text(cal.get_current_day()))
        elif kn in keys['right']:
            moved = cal.move_right()
            if not moved:
                cal = cal.get_next_calendar()
                cal.draw()
                metadata = Metadata.get(cal.year, cal.month)
            text_area.show_text(metadata.text(cal.get_current_day()))
        elif kn in keys['down']:
            cal.move_down()
            text_area.show_text(metadata.text(cal.get_current_day()))
        elif kn in keys['up']:
            cal.move_up()
            text_area.show_text(metadata.text(cal.get_current_day()))
        elif kn in keys['edit']:
            date = cal.get_current_date()
            edit_date(date)
            metadata.load_day(date.day)
            cal.set_active(entry_exists(date))
            text_area.show_text(metadata.text(date.day))
        elif kn in keys['tags']:
            show_tags(cal.area_id, text_area)
            ScreenManager.restore_area(cal.area_id)
            cal.reinit()
            text_area.set_title()
            text_area.show_text(metadata.text(cal.get_current_day()))
        elif kn in keys['edits']:
            date = cal.get_current_date()
            edits = get_edits(date)
            if edits:
                show_edits(date, edits, text_area.area_id)
                ScreenManager.restore_area(text_area.area_id)
                text_area.show_text(metadata.text(date.day))
        elif kn in keys['prev_month']:
            cal = cal.get_previous_calendar(cal.get_current_day())
            cal.draw()
            metadata = Metadata.get(cal.year, cal.month)
            text_area.show_text(metadata.text(cal.get_current_day()))
        elif kn in keys['next_month']:
            cal = cal.get_next_calendar(cal.get_current_day())
            cal.draw()
            metadata = Metadata.get(cal.year, cal.month)
            text_area.show_text(metadata.text(cal.get_current_day()))
    Metadata.write_all()
    clean_tmp()