+def was_file_written_today(filename: str) -> bool:
+ """Returns True if filename was written today."""
+
+ if not file_utils.does_file_exist(filename):
+ return False
+
+ mtime = file_utils.get_file_mtime_as_datetime(filename)
+ now = datetime.datetime.now()
+ return (
+ mtime.month == now.month and
+ mtime.day == now.day and
+ mtime.year == now.year
+ )
+
+
+def was_file_written_within_n_seconds(
+ filename: str,
+ limit_seconds: int,
+) -> bool:
+ """Returns True if filename was written within the pas limit_seconds
+ seconds.
+
+ """
+ if not file_utils.does_file_exist(filename):
+ return False
+
+ mtime = file_utils.get_file_mtime_as_datetime(filename)
+ now = datetime.datetime.now()
+ return (now - mtime).total_seconds() <= limit_seconds
+
+
+class PersistAtShutdown(enum.Enum):
+ """
+ An enum to describe the conditions under which state is persisted
+ to disk. See details below.
+
+ """
+ NEVER = 0,
+ IF_NOT_LOADED = 1,
+ ALWAYS = 2,
+
+
+class persistent_autoloaded_singleton(object):
+ """A decorator that can be applied to a Persistent subclass (i.e. a
+ class with a save() and load() method. It will intercept attempts
+ to instantiate the class via it's c'tor and, instead, invoke the
+ class' load() method to give it a chance to read state from
+ somewhere persistent.
+
+ If load() fails (returns None), the c'tor is invoked with the
+ original args as a fallback.
+
+ Based upon the value of the optional argument persist_at_shutdown,
+ (NEVER, IF_NOT_LOADED, ALWAYS), the save() method of the class will
+ be invoked just before program shutdown to give the class a chance
+ to save its state somewhere.
+
+ The implementations of save() and load() and where the class
+ persists its state are details left to the Persistent
+ implementation.
+
+ """
+ def __init__(
+ self,
+ *,
+ persist_at_shutdown: PersistAtShutdown = PersistAtShutdown.IF_NOT_LOADED):
+ self.persist_at_shutdown = persist_at_shutdown