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 from os.path import isfile, dirname, isdir import os import json from .util import * from time import time class Store: def __init__(self, data_dir): self.data_dir = data_dir self.data = {} self.dirty_objects = set() def dirty(self, obj): self.dirty_objects.add(obj) 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 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): oid = f'{symbol}.{typ.ext()}' if oid in self.data: return self.data[oid] obj = typ(symbol, self) self.load(obj) self.data[oid] = obj return obj 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] 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) def flush(self): 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}')