minor updates
This commit is contained in:
parent
701dcdbf94
commit
72a3073a5b
|
@ -1,6 +1,6 @@
|
||||||
import json, re
|
import json, re
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime, timedelta
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from functools import update_wrapper
|
from functools import update_wrapper
|
||||||
from hashlib import sha1
|
from hashlib import sha1
|
||||||
|
@ -13,7 +13,10 @@ def parse_ttl(ttl):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
if type(ttl) == int:
|
if type(ttl) == int:
|
||||||
return ttl * 60
|
return ttl
|
||||||
|
|
||||||
|
if type(ttl) == timedelta:
|
||||||
|
return timedelta.seconds
|
||||||
|
|
||||||
m = re.match(r'^(\d+)([smhdw]?)$', ttl)
|
m = re.match(r'^(\d+)([smhdw]?)$', ttl)
|
||||||
|
|
||||||
|
@ -49,45 +52,25 @@ class DefaultValue(object):
|
||||||
class BaseCache(OrderedDict):
|
class BaseCache(OrderedDict):
|
||||||
_get = OrderedDict.get
|
_get = OrderedDict.get
|
||||||
_items = OrderedDict.items
|
_items = OrderedDict.items
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, maxsize=1024, ttl=None):
|
def __init__(self, maxsize=1024, ttl=None):
|
||||||
self.ttl = parse_ttl(ttl)
|
self.ttl = parse_ttl(ttl)
|
||||||
self.maxsize = maxsize
|
self.maxsize = maxsize
|
||||||
|
self.get = self.fetch
|
||||||
self.set = self.store
|
self.set = self.store
|
||||||
|
|
||||||
self.deco = lambda *args: CacheDecorator(self, *args)
|
self.deco = lambda *args: CacheDecorator(self, *args)
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
data = ', '.join([f'{k}="{v}"' for k,v in self.items()])
|
return f'BaseCache(maxsize={self.maxsize}, ttl={self.ttl})'
|
||||||
return f'BaseCache({data})'
|
|
||||||
|
|
||||||
|
|
||||||
def items(self):
|
def items(self):
|
||||||
return [[k, v.data] for k,v in self._items()]
|
return [[k, v.data] for k,v in self._items()]
|
||||||
|
|
||||||
|
|
||||||
def get(self, key):
|
|
||||||
while len(self) >= self.maxsize and self.maxsize > 0:
|
|
||||||
self.popitem(last=False)
|
|
||||||
|
|
||||||
item = self._get(key)
|
|
||||||
|
|
||||||
if not item:
|
|
||||||
return
|
|
||||||
|
|
||||||
if self.ttl > 0:
|
|
||||||
timestamp = int(datetime.timestamp(datetime.now()))
|
|
||||||
|
|
||||||
if timestamp >= self[key].timestamp:
|
|
||||||
del self[key]
|
|
||||||
return
|
|
||||||
|
|
||||||
self[key].timestamp = timestamp + self.ttl
|
|
||||||
|
|
||||||
self.move_to_end(key)
|
|
||||||
return item['data']
|
|
||||||
|
|
||||||
|
|
||||||
def remove(self, key):
|
def remove(self, key):
|
||||||
if self.get(key):
|
if self.get(key):
|
||||||
del self[key]
|
del self[key]
|
||||||
|
@ -107,13 +90,14 @@ class BaseCache(OrderedDict):
|
||||||
|
|
||||||
|
|
||||||
def fetch(self, key):
|
def fetch(self, key):
|
||||||
item = self.get(key)
|
item = self._get(key)
|
||||||
timestamp = int(datetime.timestamp(datetime.now()))
|
|
||||||
|
|
||||||
if not item:
|
if not item:
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.ttl:
|
if self.ttl:
|
||||||
|
timestamp = int(datetime.timestamp(datetime.now()))
|
||||||
|
|
||||||
if timestamp >= self[key].timestamp:
|
if timestamp >= self[key].timestamp:
|
||||||
del self[key]
|
del self[key]
|
||||||
return
|
return
|
||||||
|
@ -121,21 +105,7 @@ class BaseCache(OrderedDict):
|
||||||
self[key]['timestamp'] = timestamp + self.ttl
|
self[key]['timestamp'] = timestamp + self.ttl
|
||||||
|
|
||||||
self.move_to_end(key)
|
self.move_to_end(key)
|
||||||
return item
|
return item.data
|
||||||
|
|
||||||
|
|
||||||
def pop(self, key, default=DefaultValue):
|
|
||||||
try:
|
|
||||||
item = self.get(key)
|
|
||||||
del self[key]
|
|
||||||
|
|
||||||
return item
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
if default == DefaultValue:
|
|
||||||
raise e from None
|
|
||||||
|
|
||||||
return default
|
|
||||||
|
|
||||||
|
|
||||||
## This doesn't work for some reason
|
## This doesn't work for some reason
|
||||||
|
|
|
@ -24,6 +24,7 @@ class Path(str, metaclass=PathMeta):
|
||||||
#else:
|
#else:
|
||||||
#str.__new__(Path, path)
|
#str.__new__(Path, path)
|
||||||
|
|
||||||
|
## todo: move these to direct properties of Path
|
||||||
self.config = {
|
self.config = {
|
||||||
'missing': missing,
|
'missing': missing,
|
||||||
'parents': parents,
|
'parents': parents,
|
||||||
|
@ -112,9 +113,10 @@ class Path(str, metaclass=PathMeta):
|
||||||
def backup(self, ext='backup', overwrite=False):
|
def backup(self, ext='backup', overwrite=False):
|
||||||
target = f'{self.parent}.{ext}'
|
target = f'{self.parent}.{ext}'
|
||||||
self.copy(target, overwrite)
|
self.copy(target, overwrite)
|
||||||
|
return Path(target)
|
||||||
|
|
||||||
|
|
||||||
def chmod(self, mode=None):
|
def chmod(self, mode):
|
||||||
os.chmod(self, mode)
|
os.chmod(self, mode)
|
||||||
|
|
||||||
|
|
||||||
|
@ -127,6 +129,7 @@ class Path(str, metaclass=PathMeta):
|
||||||
|
|
||||||
self.__check_dir(path)
|
self.__check_dir(path)
|
||||||
|
|
||||||
|
if target.exists():
|
||||||
if overwrite:
|
if overwrite:
|
||||||
try:
|
try:
|
||||||
target.delete()
|
target.delete()
|
||||||
|
@ -134,10 +137,17 @@ class Path(str, metaclass=PathMeta):
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
elif not self.config.exist:
|
||||||
|
raise FileExistsError(target)
|
||||||
|
|
||||||
shutil.copy2(self, target)
|
shutil.copy2(self, target)
|
||||||
|
return target
|
||||||
|
|
||||||
|
|
||||||
def delete(self):
|
def delete(self):
|
||||||
|
if not self.exists() and not self.config.exist:
|
||||||
|
raise FileNotFoundError(self)
|
||||||
|
|
||||||
if self.isdir:
|
if self.isdir:
|
||||||
shutil.rmtree(self, ignore_errors=True)
|
shutil.rmtree(self, ignore_errors=True)
|
||||||
|
|
||||||
|
@ -155,12 +165,13 @@ class Path(str, metaclass=PathMeta):
|
||||||
return Path(os.path.expanduser(self))
|
return Path(os.path.expanduser(self))
|
||||||
|
|
||||||
|
|
||||||
def glob(self, pattern='*'):
|
def glob(self, pattern='*', recursive=True):
|
||||||
return tuple(sorted(self.join(path) for path in PyPath(self).rglob(pattern)))
|
paths = PyPath(self).rglob(pattern) if recursive else PyPath(self).glob(pattern)
|
||||||
|
return tuple(sorted(self.join(path) for path in paths))
|
||||||
|
|
||||||
|
|
||||||
def join(self, new_path):
|
def join(self, path):
|
||||||
return Path(os.path.join(self, new_path))
|
return Path(os.path.join(self, path))
|
||||||
|
|
||||||
|
|
||||||
def json_load(self):
|
def json_load(self):
|
||||||
|
@ -178,10 +189,14 @@ class Path(str, metaclass=PathMeta):
|
||||||
|
|
||||||
self.__check_dir(path)
|
self.__check_dir(path)
|
||||||
|
|
||||||
if target.exists:
|
if target.exists():
|
||||||
|
if not self.config.exist:
|
||||||
|
raise FileExistsError(target)
|
||||||
|
|
||||||
target.delete()
|
target.delete()
|
||||||
|
|
||||||
self.symlink_to(path, target.isdir)
|
self.symlink_to(path, target.isdir)
|
||||||
|
return target
|
||||||
|
|
||||||
|
|
||||||
def listdir(self, recursive=True):
|
def listdir(self, recursive=True):
|
||||||
|
@ -192,11 +207,11 @@ class Path(str, metaclass=PathMeta):
|
||||||
|
|
||||||
|
|
||||||
def mkdir(self, mode=0o755):
|
def mkdir(self, mode=0o755):
|
||||||
if self.config['parents']:
|
if self.config.parents:
|
||||||
os.makedirs(self, mode, exist_ok=self.config['exist'])
|
os.makedirs(self, mode, exist_ok=self.config.exist)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
os.makedir(self, mode, exist_ok=self.config['exist'])
|
os.makedir(self, mode, exist_ok=self.config.exist)
|
||||||
|
|
||||||
return self.exists
|
return self.exists
|
||||||
|
|
||||||
|
@ -228,8 +243,8 @@ class Path(str, metaclass=PathMeta):
|
||||||
return Path(os.path.abspath(self))
|
return Path(os.path.abspath(self))
|
||||||
|
|
||||||
|
|
||||||
def touch(self, mode=0o644, utime=None):
|
def touch(self, mode=0o644, mtime=None):
|
||||||
timestamp = utime or datetime.now().timestamp()
|
timestamp = mtime or datetime.now().timestamp()
|
||||||
|
|
||||||
with self.open('w+') as fd:
|
with self.open('w+') as fd:
|
||||||
os.utime(self, (timestamp, timestamp))
|
os.utime(self, (timestamp, timestamp))
|
||||||
|
|
Loading…
Reference in a new issue