Update api.py, base.py and two other files

This commit is contained in:
Richard Bronkhorst 2023-07-11 07:17:13 +02:00
parent ea34bcfab7
commit 269b5cf537
4 changed files with 28 additions and 15 deletions

View File

@ -92,7 +92,6 @@ class Api:
def marketplace(self, waypoint): def marketplace(self, waypoint):
system = waypoint.system system = waypoint.system
print(f'systems/{system}/waypoints/{waypoint}/market')
data = self.request('get', f'systems/{system}/waypoints/{waypoint}/market') data = self.request('get', f'systems/{system}/waypoints/{waypoint}/market')
return self.store.update(Marketplace, data) return self.store.update(Marketplace, data)

View File

@ -6,14 +6,14 @@ class Reference:
self.typ = typ self.typ = typ
self.symbol = symbol self.symbol = symbol
self.store = store self.store = store
@classmethod @classmethod
def create(cls, obj): def create(cls, obj):
o = cls(type(obj), obj.symbol, obj.store) o = cls(type(obj), obj.symbol, obj.store)
return o return o
def resolve(self): def resolve(self):
self.store.get(self.typ, self.symbol) return self.store.get(self.typ, self.symbol)
def __repr__(self): def __repr__(self):
return f'*REF*{self.symbol}.{self.typ.ext()}' return f'*REF*{self.symbol}.{self.typ.ext()}'
@ -80,14 +80,6 @@ class Base:
self.disable_dirty = True self.disable_dirty = True
self.__dict__.update(d) self.__dict__.update(d)
self.disable_dirty = False self.disable_dirty = False
def __getstate__(self):
r = {}
for k,v in self.__dict__.items():
if k in ['store','file_offset', 'disable_dirty', 'file_offset']:
continue
r[k] = deepcopy(v)
return r
def type(self): def type(self):
return self.__class__.__name__ return self.__class__.__name__

View File

@ -21,7 +21,8 @@ class Waypoint(Base):
def set_system(self): def set_system(self):
parts = self.symbol.split('-') parts = self.symbol.split('-')
system_str = f'{parts[0]}-{parts[1]}' system_str = f'{parts[0]}-{parts[1]}'
self.system = self.store.get(System, system_str, create=True) system = self.store.get(System, system_str, create=True)
self.system = system
@classmethod @classmethod
def ext(self): def ext(self):

View File

@ -8,7 +8,23 @@ from time import time
import pickle import pickle
from struct import unpack, pack from struct import unpack, pack
from functools import partial from functools import partial
from io import BytesIO
class StorePickler(pickle.Pickler):
def persistent_id(self, obj):
return "STORE" if type(obj) == Store else None
class StoreUnpickler(pickle.Unpickler):
def __init__(self, stream, store):
self.store = store
super().__init__(stream)
def persistent_load(self, pers_id):
if pers_id == "STORE":
return self.store
raise pickle.UnpicklingError("I don know the persid!")
class ChunkHeader: class ChunkHeader:
def __init__(self): def __init__(self):
self.in_use = True self.in_use = True
@ -61,10 +77,15 @@ class Store:
self.dirty_objects.add(obj) self.dirty_objects.add(obj)
def dump_object(self, obj): def dump_object(self, obj):
return pickle.dumps(obj) buf = BytesIO()
p = StorePickler(buf)
p.dump(obj)
return buf.getvalue()
def load_object(self, data, offset): def load_object(self, data, offset):
obj = pickle.loads(data) buf = BytesIO(data)
p = StoreUnpickler(buf, self)
obj = p.load()
obj.file_offset = offset obj.file_offset = offset
obj.disable_dirty = False obj.disable_dirty = False
self.hold(obj) self.hold(obj)
@ -91,7 +112,7 @@ class Store:
slack = sz * self.slack slack = sz * self.slack
slack = min(slack, self.slack_max) slack = min(slack, self.slack_max)
slack = max(slack, self.slack_min) slack = max(slack, self.slack_min)
sz += slack sz += int(slack)
self.fil.seek(0, 2) self.fil.seek(0, 2)
offset = self.fil.tell() offset = self.fil.tell()
h = ChunkHeader() h = ChunkHeader()