2023-06-11 18:51:03 +00:00
|
|
|
from nullptr.models.base import Base
|
|
|
|
from nullptr.models.waypoint import Waypoint
|
|
|
|
from nullptr.models.sector import Sector
|
|
|
|
from nullptr.models.system import System
|
|
|
|
from nullptr.models.agent import Agent
|
2023-06-09 20:20:46 +00:00
|
|
|
from os.path import isfile, dirname, isdir
|
|
|
|
import os
|
|
|
|
import json
|
2023-06-11 18:51:03 +00:00
|
|
|
from .util import *
|
2023-06-12 08:51:01 +00:00
|
|
|
from time import time
|
2023-06-09 11:19:47 +00:00
|
|
|
|
2023-06-07 19:48:28 +00:00
|
|
|
class Store:
|
2023-06-09 20:20:46 +00:00
|
|
|
def __init__(self, data_dir):
|
|
|
|
self.data_dir = data_dir
|
|
|
|
self.data = {}
|
2023-06-12 08:51:01 +00:00
|
|
|
self.dirty_objects = set()
|
|
|
|
|
|
|
|
def dirty(self, obj):
|
|
|
|
self.dirty_objects.add(obj)
|
2023-06-09 20:20:46 +00:00
|
|
|
|
|
|
|
def path(self, obj):
|
|
|
|
return os.path.join(self.data_dir, obj.path())
|
|
|
|
|
|
|
|
def load(self, obj):
|
|
|
|
path = self.path(obj)
|
|
|
|
if not isfile(path):
|
|
|
|
return obj
|
|
|
|
with open(path) as f:
|
|
|
|
data = json.load(f)
|
|
|
|
data['store'] = self
|
|
|
|
obj.__dict__ = data
|
2023-06-08 16:49:00 +00:00
|
|
|
|
2023-06-09 20:20:46 +00:00
|
|
|
def store(self, obj):
|
|
|
|
path = self.path(obj)
|
|
|
|
path_dir = dirname(path)
|
|
|
|
data = obj.dict()
|
|
|
|
if not isdir(path_dir):
|
|
|
|
os.makedirs(path_dir, exist_ok=True)
|
|
|
|
with open(path, 'w') as f:
|
|
|
|
json.dump(data, f, indent=2)
|
|
|
|
|
|
|
|
def get(self, typ, symbol):
|
2023-06-12 08:51:01 +00:00
|
|
|
oid = f'{symbol}.{typ.ext()}'
|
|
|
|
if oid in self.data:
|
|
|
|
return self.data[oid]
|
2023-06-09 20:20:46 +00:00
|
|
|
obj = typ(symbol, self)
|
|
|
|
self.load(obj)
|
2023-06-12 08:51:01 +00:00
|
|
|
self.data[oid] = obj
|
2023-06-09 20:20:46 +00:00
|
|
|
return obj
|
|
|
|
|
2023-06-10 18:49:50 +00:00
|
|
|
def update(self, typ, symbol, data):
|
|
|
|
obj = self.get(typ, symbol)
|
|
|
|
obj.update(data)
|
|
|
|
return obj
|
|
|
|
|
|
|
|
def update_list(self, typ, lst):
|
|
|
|
return [self.update(typ, mg(d, 'symbol'), d) for d in lst]
|
|
|
|
|
2023-06-10 17:39:32 +00:00
|
|
|
def all(self, path, typ):
|
|
|
|
if hasattr(path, 'path'):
|
|
|
|
path = path.path()
|
|
|
|
path = os.path.join(self.data_dir, path)
|
|
|
|
if not isdir(path):
|
|
|
|
return
|
|
|
|
ext = '.' + typ.ext()
|
|
|
|
for f in os.listdir(path):
|
|
|
|
fil = os.path.join(path, f)
|
|
|
|
if not isfile(fil):
|
|
|
|
continue
|
|
|
|
if not fil.endswith(ext):
|
|
|
|
continue
|
|
|
|
symbol = f[:-len(ext)]
|
|
|
|
yield self.get(typ, symbol)
|
|
|
|
|
|
|
|
|
2023-06-09 20:20:46 +00:00
|
|
|
def flush(self):
|
2023-06-12 08:51:01 +00:00
|
|
|
it = 0
|
|
|
|
start_time = time()
|
|
|
|
for obj in self.dirty_objects:
|
|
|
|
it += 1
|
|
|
|
self.store(obj)
|
|
|
|
self.dirty_objects = set()
|
|
|
|
dur = time() - start_time
|
|
|
|
print(f'flush done {it} items {dur:.2f}')
|