Beispiel #1
0
def cli(output):
    """Return the longest streak of all currently tracked habits."""
    try:
        # Open habits database
        habits_db = shelve.open(os.path.join(conf.data_dir, conf.db_name))
        # Load habits
        habits = [habit for habit in habits_db.items()]
        # Close habits database
        habits_db.close()
        # Retrieve longest streak
        longest_streak = analytics.get_longest_streak(habits)
        return_value = {"longest_streak": longest_streak}
        # Return longest streak
        if output == "JSON":
            click.echo(get_json_out(return_value))
        else:
            click.echo(get_human_out(return_value))
    except ValueError as e:
        # Inform user: Return error if unexpected error occurred and exit application
        click.secho("################# ERROR #################", bg="red", fg="white", bold=True)
        click.secho("! An error occurred !", bg="red", fg="white", bold=True)
        click.secho(f"{type(e).__name__}: {e}", bg="red", fg="white", bold=True)
        click.secho("########################################", bg="red", fg="white", bold=True)
        sys.exit(1)
    except Exception as e:
        # Inform user: Return error if unexpected error occurred and exit application
        click.secho("################# ERROR #################", bg="red", fg="white", bold=True)
        click.secho("! An unexpected error occurred !", bg="red", fg="white", bold=True)
        click.secho(f"{type(e).__name__}: {e}", bg="red", fg="white", bold=True)
        click.secho("########################################", bg="red", fg="white", bold=True)
        sys.exit(1)
Beispiel #2
0
 def test_get_longest_streak_for_all(self):
     streak_days = 0
     streak_days_sum = 45
     streak_list = an.get_longest_streak("", 1)
     for x in streak_list:
         streak_days += x[1]
     self.assertIsNotNone(streak_list)
     self.assertEqual(streak_days, streak_days_sum)
Beispiel #3
0
    def test_get_longest_streak_for_sleep_habit(self):
        habit_name = "Sleep early"
        longest_streak = 2
        streak_unit = "week(s)"
        streak_list = an.get_longest_streak(habit_name, 1)

        self.assertIsNotNone(streak_list)
        self.assertEqual(streak_list[0][0], habit_name)
        self.assertEqual(streak_list[0][1], longest_streak)
        self.assertEqual(streak_list[0][2], streak_unit)
Beispiel #4
0
    def test_get_longest_streak_for_walk_habit(self):
        habit_name = "Walk a lot"
        longest_streak = 13
        streak_unit = "day(s)"
        streak_list = an.get_longest_streak(habit_name, 1)

        self.assertIsNotNone(streak_list)
        self.assertEqual(streak_list[0][0], habit_name)
        self.assertEqual(streak_list[0][1], longest_streak)
        self.assertEqual(streak_list[0][2], streak_unit)
Beispiel #5
0
 def test_get_longest_streak_valid_option_all(self, sample_habits_objects):
     expected_longest_streak = 11
     actual = analytics.get_longest_streak(sample_habits_objects)
     assert actual == expected_longest_streak
Beispiel #6
0
 def test_get_longest_streak_valid_option_all_empty_habits(self):
     expected_longest_streak = 0
     actual = analytics.get_longest_streak(habits=[])
     assert actual == expected_longest_streak
def main(args):
    """
    The main function calls methods from the Habit class depending
    on the selection made by the user.

    Parameters
    ----------
    args : object
        A populated namespace with attributes.

    Returns
    -------
    None.

    """

    # Each time the app is launched, the statistical values
    # for all Habits are updated.
    analytics.update_analytic_fields()

    if args.new:
        name = args.new
        habit = Habit.new(name)
        if habit is not None:
            habit.save(0)

    elif args.copy:
        name = args.copy
        habit = Habit.load(name)
        if habit is not None:
            habit.duplicate()

    elif args.edit:
        name = args.edit
        habit = Habit.load(name)
        if habit is not None:
            habit.edit()

    elif args.fav:
        name = args.fav
        habit = Habit.load(name)
        if habit is not None:
            habit.set_favorite()

    elif args.delete:
        name = args.delete
        habit = Habit.load(name)
        if habit is not None:
            habit.delete()

    elif args.show:
        name = args.show
        habit = Habit.load(name)
        if habit is not None:
            habit.get_summary()
            habit.streak.get_summary()

    elif args.check:
        name = args.check
        habit = Habit.load(name)
        if habit is not None:
            habit.streak.check()

    elif args.stats:
        choice = args.stats
        if choice == 1:
            print("\nUrgent habits that have to checked today at the latest")
            analytics.get_urgent_habits()
        elif choice == 2:
            print("\nThe longest streaks for all habits")
            analytics.get_longest_streak()
        elif choice == 3:
            print("\nAll active habits")
            analytics.get_active_habits()

    return None