use Inventory class, partly instead of list[Thing]

additionally allow creating location if it does not exist yet
This commit is contained in:
flukx 2024-08-21 20:07:06 +02:00
parent 04690f0f4d
commit b74912e45d
2 changed files with 12 additions and 5 deletions

@ -1 +1 @@
Subproject commit 436944abb59e999526374875557e76f08346b5de Subproject commit 941296f90c7c7e4ccbc6565bdec3cb4a26128d78

View file

@ -11,10 +11,11 @@ from nicegui import ui
import location import location
from thing import Thing from thing import Thing
import inventory_io import inventory_io
from inventory_io import Inventory
"""Global (module-wide) variables.""" """Global (module-wide) variables."""
gl_options: argparse.Namespace gl_options: argparse.Namespace
gl_things: list[Thing] = [] gl_inventory: Inventory = Inventory([])
def get_options() -> argparse.Namespace: def get_options() -> argparse.Namespace:
@ -43,7 +44,7 @@ def load_data():
Then an argument force would be useful to reload. Then an argument force would be useful to reload.
""" """
start = time.monotonic() start = time.monotonic()
gl_things[:] = inventory_io.get_things(gl_options) gl_inventory.replace_data(Inventory.from_json_files(gl_options))
end = time.monotonic() end = time.monotonic()
print(f"Data loaded in {end-start} seconds.") print(f"Data loaded in {end-start} seconds.")
ui.notify(f"Data loaded in {end-start} seconds.", position="top", type="positive") ui.notify(f"Data loaded in {end-start} seconds.", position="top", type="positive")
@ -54,7 +55,7 @@ load_data()
def save_data() -> None: def save_data() -> None:
"""Save things to files.""" """Save things to files."""
inventory_io.save_things(gl_things, gl_options.input_file, gl_options.locations_file) gl_inventory.save(gl_options.things_file, gl_options.locations_file)
ui.notify( ui.notify(
message="Data saved", message="Data saved",
position="top", position="top",
@ -229,7 +230,7 @@ def search_page() -> None:
except asyncio.exceptions.CancelledError: except asyncio.exceptions.CancelledError:
# the next letter was already typed, do not search and rerender for this query # the next letter was already typed, do not search and rerender for this query
return return
query = asyncio.create_task(find_things(gl_things, event.value)) query = asyncio.create_task(find_things(gl_inventory, event.value))
running_queries.append(query) running_queries.append(query)
try: try:
start = time.monotonic() start = time.monotonic()
@ -324,6 +325,12 @@ def show_thing_changer(ui_element: nicegui.ui.element, thing: Thing) -> None:
None if no focus should be set None if no focus should be set
""" """
location_ui_element.clear() location_ui_element.clear()
if gl_inventory.schema is None and thing.location is None:
with location_ui_element:
ui.label("No location schema and no location existing. Sorry.")
return
if thing.location is None:
thing.location = location.Location(gl_inventory.schema, {}, gl_inventory.seperator)
with location_ui_element: with location_ui_element:
location_info = thing.location.json location_info = thing.location.json
schema_hierachy = thing.location.schema.get_schema_hierachy(dict(iter(thing.location))) schema_hierachy = thing.location.schema.get_schema_hierachy(dict(iter(thing.location)))