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.
51 lines
1.3 KiB
51 lines
1.3 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__()
|
|
print('initiated', path)
|
|
self.path = path
|
|
self.timeout = timeout or timedelta(days=1)
|
|
self.get_object()
|
|
|
|
def __repr__(self):
|
|
return self.__str__()
|
|
|
|
def __str__(self):
|
|
return self.object
|
|
# .encode('utf-8')
|
|
|
|
def __unicode__(self):
|
|
return self.object.decode('utf-8')
|
|
|
|
def get_object(self):
|
|
self.timeto = datetime.now() + self.timeout
|
|
try:
|
|
with open(self.path, 'r') as f:
|
|
self._object = f.read()
|
|
except:
|
|
self._object = ''
|
|
|
|
@property
|
|
def object(self):
|
|
if self.timeto < datetime.now():
|
|
self.get_object()
|
|
return self._object
|
|
|