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):
system = waypoint.system
print(f'systems/{system}/waypoints/{waypoint}/market')
data = self.request('get', f'systems/{system}/waypoints/{waypoint}/market')
return self.store.update(Marketplace, data)

View File

@ -13,7 +13,7 @@ class Reference:
return o
def resolve(self):
self.store.get(self.typ, self.symbol)
return self.store.get(self.typ, self.symbol)
def __repr__(self):
return f'*REF*{self.symbol}.{self.typ.ext()}'
@ -81,14 +81,6 @@ class Base:
self.__dict__.update(d)
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):
return self.__class__.__name__

View File

@ -21,7 +21,8 @@ class Waypoint(Base):
def set_system(self):
parts = self.symbol.split('-')
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
def ext(self):

View File

@ -8,6 +8,22 @@ from time import time
import pickle
from struct import unpack, pack
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:
def __init__(self):
@ -61,10 +77,15 @@ class Store:
self.dirty_objects.add(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):
obj = pickle.loads(data)
buf = BytesIO(data)
p = StoreUnpickler(buf, self)
obj = p.load()
obj.file_offset = offset
obj.disable_dirty = False
self.hold(obj)
@ -91,7 +112,7 @@ class Store:
slack = sz * self.slack
slack = min(slack, self.slack_max)
slack = max(slack, self.slack_min)
sz += slack
sz += int(slack)
self.fil.seek(0, 2)
offset = self.fil.tell()
h = ChunkHeader()