database: add close function, path: add copy and move

This commit is contained in:
Izalia Mae 2021-03-07 15:59:11 -05:00
parent c4e5c9b6b4
commit 26156682c8
3 changed files with 29 additions and 6 deletions

View file

@ -21,12 +21,15 @@ class DataBase():
self.engine_string = self.__engine_string(dbtype, kwargs) self.engine_string = self.__engine_string(dbtype, kwargs)
self.db = create_engine(self.engine_string) self.db = create_engine(self.engine_string)
self.table = Tables(self, tables) self.table = Tables(self, tables)
self.cache = DotDict({table: LRUCache() for table in tables.keys()}) self.table_names = tables.keys()
self.classes = kwargs.get('row_classes', CustomRows()) self.classes = kwargs.get('row_classes', CustomRows())
self.cache = None
session_class = kwargs.get('session_class', Session) session_class = kwargs.get('session_class', Session)
self.session = lambda trans=True: session_class(self, trans) self.session = lambda trans=True: session_class(self, trans)
self.SetupCache()
def __engine_string(self, dbtype, kwargs): def __engine_string(self, dbtype, kwargs):
if not kwargs.get('database'): if not kwargs.get('database'):
@ -63,6 +66,14 @@ class DataBase():
return engine_string return engine_string
def close(self):
self.SetupCache()
def SetupCache(self):
self.cache = DotDict({table: LRUCache() for table in self.table_names})
def CreateDatabase(self): def CreateDatabase(self):
if self.engine_string.startswith('postgresql'): if self.engine_string.startswith('postgresql'):
predb = create_engine(db.engine_string.replace(config.db.name, 'postgres', -1)) predb = create_engine(db.engine_string.replace(config.db.name, 'postgres', -1))

View file

@ -5,6 +5,7 @@ from os import environ as env
from datetime import datetime from datetime import datetime
from getpass import getpass from getpass import getpass
from pathlib import Path as Pathlib from pathlib import Path as Pathlib
from shutil import copyfile
from . import logging from . import logging
@ -324,6 +325,10 @@ class LowerDotDict(DotDict):
class Path(object): class Path(object):
def __init__(self, path, exist=True, missing=True, parents=True): def __init__(self, path, exist=True, missing=True, parents=True):
self.__path = Pathlib(str(path)) self.__path = Pathlib(str(path))
if str(path).startswith('~'):
self.__path == self.__path.expanduser()
self.json = DotDict({}) self.json = DotDict({})
self.exist = exist self.exist = exist
self.missing = missing self.missing = missing
@ -382,14 +387,21 @@ class Path(object):
return self return self
def move(self, path): def copy(self, path, overwrite=False):
target = Path(path) target = Path(path)
self.__check_dir(path) self.__check_dir(path)
if target.exists() and not target.isdir(): if target.exists() and overwrite:
target.delete() target.delete()
copyfile(self.str(), target.str())
def move(self, path, overwrite=False):
self.copy(path, overwrite=overwrite)
self.delete()
def join(self, path, new=True): def join(self, path, new=True):
new_path = self.__path.joinpath(path) new_path = self.__path.joinpath(path)
@ -487,10 +499,10 @@ class Path(object):
# This needs to be extended to handle dirs with files/sub-dirs # This needs to be extended to handle dirs with files/sub-dirs
def delete(self): def delete(self):
if self.isdir(): if self.isdir():
self.__path.rmdir(self.missing) self.__path.rmdir()
else: else:
self.__path.unlink(self.missing) self.__path.unlink()
return not self.exists() return not self.exists()

View file

@ -100,7 +100,7 @@ class Template(Environment):
self.filters.update(data) self.filters.update(data)
def render(self, tplfile, request=None, context={}, headers={}, cookies={}, pprint=False, **kwargs): def render(self, tplfile, context={}, headers={}, cookies={}, request=None, pprint=False, **kwargs):
if not isinstance(context, dict): if not isinstance(context, dict):
raise TypeError(f'context for {tplfile} not a dict: {type(context)} {context}') raise TypeError(f'context for {tplfile} not a dict: {type(context)} {context}')