You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

40 lines
1.0 KiB

# -*- coding: utf-8 -*-
# from string import Template
import string
from datetime import timedelta, datetime
class DeltaTemplate(string.Template):
delimiter = "%"
def strfdelta(tdelta, fmt):
d = {"D": tdelta.days}
hours, rem = divmod(tdelta.seconds, 3600)
minutes, seconds = divmod(rem, 60)
d["H"] = '{:02d}'.format(hours)
d["M"] = '{:02d}'.format(minutes)
d["S"] = '{:02d}'.format(seconds)
t = DeltaTemplate(fmt)
return t.substitute(**d)
class CachedSting(object):
def __init__(self, path, timeout=None):
super(CachedSting, self).__init__()
self.path = path
self.timeout = timeout or timedelta(days=1)
self.get_object()
def __repr__(self):
return self.__str__()
def __str__(self):
if self.timeto > datetime.now():
return self.object
self.get_object()
return self.object
def get_object(self):
self.timeto = datetime.now() + self.timeout
with open(self.path, 'r') as f:
self.object = f.read()