""" Warehouse API """ import os from dataclasses import dataclass # # Dataclasses # @dataclass class WarehouseLocation: """ Location inside warehouse """ # human readable name of location within warehouse name: str # description on how to find the location how_to_find: str = 'Somewhere inside the warehouse' # remarks remarks: str = '' @dataclass class WarehouseItem: """ Item stored within warehouse """ # human readable name of item name: str # human readable name of measurement unit unit: str = 'units' # count of item in units count: int = 1 # remarks remarks: str = '' # location ID where the item is stored location_id: int = 0 # # Private # _warehouse_name = None _warehouse_path = None _locations = {} _entries = {} async def _read_file(path: str) -> str: """ Read file content and strip it. Empty string on error. """ try: with open(path, 'r') as f: return f.read().strip() except: return '' async def _write_file(path: str, content: str) -> bool: """ Write file content. """ try: with open(path, 'w') as f: f.write(content) return True except: return False async def _fsck_warehouse(path: str) -> bool: """ Check warehouse for errors and fix them if possible. Returns True if warehouse is usable. """ if not os.path.isdir(path): return False async def _create_warehouse(path: str) -> bool: """ Create warehouse. Returns True if warehouse is usable. """ if os.path.isdir(path): return await _fsck_warehouse(path) locations[0] = WarehouseLocation( name='Unsorted', how_to_find='', remarks='Pseudolocation for unsorted items' ) # # Public # async def init(name: str) -> bool: """ Initialize warehouse API. """ global _warehouse_name, _warehouse_path _warehouse_name = name _warehouse_path = f'warehouse_{name}' return await _create_warehouse(_warehouse_path)