Sud: server always starts empty; sude/*.json is purely a client concern
Several sude/*.json schedules can exist on disk; only one runs at a time, chosen by a client Load. Sud no longer takes a path at all - it starts empty (EMPTY_SUD) and the server config no longer names a specific schedule file, removing the implicit link to sud_0010.json in particular. Reading/writing sude/*.json is now entirely the client's job (e.g. the GUI's Load/Save file dialogs); the server's Sud only ever holds whatever was last Load()ed, in memory, until replaced or the server restarts. Updates demo_sud.py/demo_sud_save_load.py to construct an empty Sud() and load() a schedule explicitly, matching the new constructor.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import json
|
||||
import numpy as np
|
||||
from matplotlib.pyplot import plot, step, figure, subplot, grid, show, legend, yticks
|
||||
from components.sud import Sud, SudState
|
||||
@@ -33,7 +34,9 @@ if __name__ == '__main__':
|
||||
}
|
||||
}
|
||||
|
||||
sud = Sud("sude/sud_0010.json")
|
||||
sud = Sud()
|
||||
with open("sude/sud_0010.json") as f:
|
||||
sud.load(json.load(f))
|
||||
|
||||
first_step = sud.schedule[0]
|
||||
plant_params = {
|
||||
|
||||
@@ -1,45 +1,44 @@
|
||||
import json
|
||||
import shutil
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
from components.sud import Sud, SudState
|
||||
|
||||
# Console-only walkthrough of Sud.save()/load() (no plot - there's no
|
||||
# numeric data here, just JSON plumbing). Runs against a scratch copy of a
|
||||
# real sud.json so sude/sud_0010.json itself is never touched.
|
||||
# numeric data here, just JSON plumbing). load()/save() are purely
|
||||
# in-memory - Sud itself never reads or writes anything under sude/.
|
||||
|
||||
if __name__ == '__main__':
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "sud.json"
|
||||
shutil.copy("sude/sud_0010.json", path)
|
||||
sud = Sud()
|
||||
print(f"Starts out empty: '{sud.name}', {len(sud.schedule)} steps")
|
||||
|
||||
sud = Sud(str(path))
|
||||
print(f"Loaded '{sud.name}', {len(sud.schedule)} steps")
|
||||
with open("sude/sud_0010.json") as f:
|
||||
doc = json.load(f)
|
||||
assert sud.load(doc) is True
|
||||
print(f"Loaded '{sud.name}', {len(sud.schedule)} steps")
|
||||
|
||||
doc = sud.save()
|
||||
assert doc['Name'] == sud.name
|
||||
print("save() returned the on-disk document")
|
||||
saved = sud.save()
|
||||
assert saved['Name'] == sud.name
|
||||
print("save() returned the running document")
|
||||
|
||||
sud.state = SudState.RAMPING
|
||||
assert sud.load(doc) is False
|
||||
print("load() refused while RAMPING")
|
||||
sud.state = SudState.RAMPING
|
||||
assert sud.load(saved) is False
|
||||
print("load() refused while RAMPING")
|
||||
|
||||
sud.state = SudState.IDLE
|
||||
doc['Name'] = 'Sud-Renamed'
|
||||
doc['steps'][0]['descr'] = 'Edited step'
|
||||
assert sud.load(doc) is True
|
||||
assert sud.name == 'Sud-Renamed'
|
||||
assert sud.schedule[0]['descr'] == 'Edited step'
|
||||
assert sud.state == SudState.IDLE
|
||||
print("load() applied the edit and reset to IDLE")
|
||||
sud.state = SudState.IDLE
|
||||
saved['Name'] = 'Sud-Renamed'
|
||||
saved['steps'][0]['descr'] = 'Edited step'
|
||||
assert sud.load(saved) is True
|
||||
assert sud.name == 'Sud-Renamed'
|
||||
assert sud.schedule[0]['descr'] == 'Edited step'
|
||||
assert sud.state == SudState.IDLE
|
||||
print("load() applied the edit, purely in memory")
|
||||
|
||||
on_disk = json.loads(path.read_text())
|
||||
assert on_disk['Name'] == 'Sud-Renamed'
|
||||
print("load() persisted the edit to disk")
|
||||
with open("sude/sud_0010.json") as f:
|
||||
on_disk = json.load(f)
|
||||
assert on_disk['Name'] != 'Sud-Renamed'
|
||||
print("sude/sud_0010.json on disk is untouched")
|
||||
|
||||
assert sud.load({'Name': 'broken'}) is False
|
||||
assert sud.name == 'Sud-Renamed'
|
||||
print("load() rejected malformed data, left schedule untouched")
|
||||
assert sud.load({'Name': 'broken'}) is False
|
||||
assert sud.name == 'Sud-Renamed'
|
||||
print("load() rejected malformed data, left schedule untouched")
|
||||
|
||||
print("All sud save/load checks passed")
|
||||
|
||||
Reference in New Issue
Block a user