Skip to content

Commit

Permalink
Implementing data reload on intervals
Browse files Browse the repository at this point in the history
Now there is a new parameter that determines how often data is reloaded. This addresses the issue #97
  • Loading branch information
anufrievroman committed Apr 3, 2024
1 parent acd4a58 commit 0300c5a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 36 deletions.
15 changes: 10 additions & 5 deletions calcure/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
import getopt
import sys
import importlib
import logging
import threading

from calcure.calendars import Calendar
from calcure.errors import error
from calcure.configuration import cf
from calcure.errors import Error
from calcure.configuration import Config
from calcure.weather import Weather
from calcure.importers import Importer
from calcure.dialogues import clear_line
Expand All @@ -24,6 +23,10 @@
from calcure.controls import *


# Initialise config:
cf = Config()
error = Error(cf.config_folder / "info.log")

# Language:
if cf.LANG == "fr":
from calcure.translations.fr import *
Expand Down Expand Up @@ -593,6 +596,7 @@ def __init__(self, stdscr, y, x, screen):
self.screen = screen
self.error = error


def render(self):
"""Render this view on the screen"""
if self.error.has_occured:
Expand Down Expand Up @@ -1072,12 +1076,13 @@ def main(stdscr) -> None:
if user_tasks.changed:
task_saver_csv.save()
screen.refresh_now = True
if screen.reload_data:

# If needed, reload the data:
if screen.is_time_to_reload:
user_events = event_loader_csv.load()
user_tasks = task_loader_csv.load()
user_ics_events = event_loader_ics.load()
user_ics_tasks = task_loader_ics.load()
screen.reload_data = False

# Cleaning up before quitting:
curses.echo()
Expand Down
24 changes: 12 additions & 12 deletions calcure/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import getopt
import logging
import datetime
from pathlib import Path

from calcure.data import AppState
Expand All @@ -19,6 +20,15 @@ def __init__(self):
self.config_file = self.config_folder / "config.ini"
self.is_first_run= True

# Create config folder:
self.config_folder.mkdir(exist_ok=True)

# Read config file:
self.create_config_file()
self.read_config_file_from_user_arguments()
self.read_config_file()
self.read_parameters_from_user_arguments()


def shorten_path(self, path):
"""Replace home part of paths with tilde"""
Expand Down Expand Up @@ -66,6 +76,7 @@ def create_config_file(self):
"start_week_day": "1",
"weekend_days": "6,7",
"refresh_interval": "1",
"data_reload_interval": "0",
"split_screen": "Yes",
"right_pane_percentage": "25",
"journal_header": "JOURNAL",
Expand Down Expand Up @@ -233,6 +244,7 @@ def read_config_file(self):
self.TODO_ICON = conf.get("Parameters", "todo_icon", fallback="•") if self.DISPLAY_ICONS else "·"
self.IMPORTANT_ICON = conf.get("Parameters", "important_icon", fallback="‣") if self.DISPLAY_ICONS else "!"
self.REFRESH_INTERVAL = int(conf.get("Parameters", "refresh_interval", fallback=1))
self.DATA_RELOAD_INTERVAL = int(conf.get("Parameters", "data_reload_interval", fallback=0))
self.RIGHT_PANE_PERCENTAGE = int(conf.get("Parameters", "right_pane_percentage", fallback=25))
self.ONE_TIMER_AT_A_TIME = conf.getboolean("Parameters", "one_timer_at_a_time", fallback=False)

Expand Down Expand Up @@ -360,15 +372,3 @@ def read_parameters_from_user_arguments(self):
logging.error("Invalid user arguments. %s", e_message)
pass


# Initialise config:
cf = Config()

# Create config folder:
cf.config_folder.mkdir(exist_ok=True)

# Read config file:
cf.create_config_file()
cf.read_config_file_from_user_arguments()
cf.read_config_file()
cf.read_parameters_from_user_arguments()
4 changes: 3 additions & 1 deletion calcure/controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import importlib

# Modules:
from calcure.configuration import cf
from calcure.data import *
from calcure.dialogues import *
from calcure.configuration import Config

cf = Config()

# Language:
if cf.LANG == "fr":
Expand Down
1 change: 0 additions & 1 deletion calcure/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import time
import enum
import logging

from calcure.calendars import Calendar

Expand Down
28 changes: 11 additions & 17 deletions calcure/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
import logging
import io

from calcure.configuration import cf


class Error():
"""Error messages displayed to the user.
Different errors have different purposes:
ERROR type is reserved for loading errors;
WARNING type is reserved for incorrect user input;"""

def __init__(self):
def __init__(self, file):
self.buffer = io.StringIO()
self.file = f"{cf.config_folder}/info.log"

self.file = file

# Start logging errors:
logging.basicConfig(level=logging.INFO,
format="[%(levelname)s] %(message)s",
# encoding='utf-8',
handlers=[logging.FileHandler(self.file, 'w'),
#logging.StreamHandler(),
logging.StreamHandler(self.buffer),
])
@property
def has_occured(self):
"""Has any errors occured?"""
Expand All @@ -41,15 +47,3 @@ def clear_buffer(self):
self.buffer.seek(0)
self.buffer.truncate(0)


# Initialise error:
error = Error()

# Start logging:
logging.basicConfig(level=logging.INFO,
format="[%(levelname)s] %(message)s",
# encoding='utf-8',
handlers=[logging.FileHandler(error.file, 'w'),
logging.StreamHandler(),
logging.StreamHandler(error.buffer),
])
17 changes: 17 additions & 0 deletions calcure/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def __init__(self, stdscr, cf):
self.day = self.today.day
self.month = self.today.month
self.year = self.today.year
self.reload_interval = cf.DATA_RELOAD_INTERVAL
self.last_data_reload_time = datetime.datetime.now()

@property
def is_active_pane(self):
Expand Down Expand Up @@ -87,6 +89,21 @@ def number_of_weeks(self) -> int:
"""Calculate how many weeks are in this month"""
return len(Calendar(0, self.use_persian_calendar).monthdayscalendar(self.year, self.month))

@property
def is_time_to_reload(self):
"""Check if enough time passed since last data reload or it was requested"""
if self.reload_data:
self.reload_data = False
return True
if self.reload_interval == 0:
return False
reload_interval = datetime.timedelta(minutes=self.reload_interval)
if datetime.datetime.now() - self.last_data_reload_time > reload_interval:
self.last_data_reload_time = datetime.datetime.now()
return True
else:
return False

def next_month(self):
"""Switches to the next month"""
if self.month < 12:
Expand Down

0 comments on commit 0300c5a

Please sign in to comment.