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)
def _load(self): """Load data from a file saved earlier or directly from entries.""" try: with open(self.get_path()) as f: loaded = pickle.load(f) self.data, self.tags = loaded['data'], loaded['tags'] except IOError: for day in range(1, lastday(self.year, self.month) + 1): self.load_day(day) self._load_tags()
def __init__(self, year, month, init_day=None, is_active=lambda date: False, *args, **kwargs): """Initialize calendar by year and month. init_day: day of the month that is initially selected is_active: function that takes a datetime.date instance and returns True if that date is "active" """ super(Calendar, self).__init__(*args, **kwargs) self.window = curses.newwin(*ScreenManager.get_coords(self.area_id)) self.window.keypad(1) self.year, self.month = year, month self.selected = () last = lastday(year, month) if not init_day: init_day = datetime.date.today().day if init_day > last: init_day = last self.init_day = init_day self.is_active = is_active first = datetime.date(self.year, self.month, 1) x, y = 0, 1 week = [None] * first.weekday() x += 3 * first.weekday() last = lastday(first) day = first.day data = [] while day <= last: date = datetime.date(self.year, self.month, day) week.append((x, y, is_active(date), day)) if len(week) == 7: data.append(week) week = [] y += 1 x = 0 else: x += 3 day += 1 if week: data.append(week + [None] * (7 - len(week))) self.data = data self.miny = len(self.data) + 1
def _check_entries(self, year, month, metadata, should_fail=[]): start = datetime.date(year, month, 1) stop = datetime.date(year, month, lastday(year, month)) month_dir = os.path.join(conf['entries_dir'], start.strftime('%Y-%m')) while start <= stop: path = os.path.join(month_dir, start.strftime('%d')) with open(path) as f: text = f.read() lines = [line for line in text.split('\n') if line.strip()] data = metadata.get_data_for_day(start.day) test = (self.assertNotEqual if start.day in should_fail else self.assertEqual) lines_from_data, words, tags, size, edits = data test(len(lines), lines_from_data) test(sum(len(line.split()) for line in lines), words) start += datetime.timedelta(days=1)
def setUp(self): cd = os.path.dirname(os.path.abspath(__file__)) test_dir = cd while os.path.exists(test_dir): test_dir = os.path.join(cd, self._gen_name()) self.orig_conf = conf.copy() conf['data_dir'] = test_dir conf['entries_dir'] = os.path.join(test_dir, 'entries') conf['metadata_dir'] = os.path.join(test_dir, 'metadata') today = datetime.date.today() start = datetime.date(today.year, today.month, 1) stop = datetime.date(today.year, today.month, lastday(today)) month_dir = os.path.join(conf['entries_dir'], today.strftime('%Y-%m')) os.makedirs(month_dir) while start <= stop: path = os.path.join(month_dir, start.strftime('%d')) with open(path, 'w') as f: f.write(self._gen_text()) start += datetime.timedelta(days=1)
def test_current_month_cal(self): today = datetime.date.today() Calendar(today.year, today.month, today.day) ScreenManager.draw_all() # check the first line screen = get_screen() lines = screen.lines days = 'Mo Tu We Th Fr Sa Su' self.assertEquals(lines[0], list(days + ' ' * (screen.getmaxyx()[1] - len(days)))) # check the location of the first day first = datetime.date(today.year, today.month, 1) x = first.weekday() * 3 self.assertEquals(lines[1][x:x + 2], list(' 1')) # check the location of the last day last = lastday(today) x = datetime.date(today.year, today.month, last).weekday() * 3 weeks = int(math.ceil((last - (7 - first.weekday())) / 7.0)) + 1 self.assertEquals(lines[weeks][x:x + 2], list(str(last)))
def test_moving(self): today = datetime.date.today() day = 1 last = lastday(today) cal = Calendar(today.year, today.month, day) ScreenManager.draw_all() # moving from the first day to the last while day < last: cal.move_right() day += 1 self.assertEquals(cal.get_current_day(), day) # moving from the last day to the 7th while day > 7: cal.move_left() day -= 1 self.assertEquals(cal.get_current_day(), day) # moving up and down cal.move_down() self.assertEquals(cal.get_current_day(), 14) cal.move_up() self.assertEquals(cal.get_current_day(), 7)
def get_previous_calendar(self, day=None): """Return a Calendar instance representing the previous month.""" year, month = (self.year if self.month != 1 else self.year - 1, self.month - 1 if self.month != 1 else 12) return Calendar(year, month, day or lastday(year, month), self.is_active, self.area_id)