"""Offline intake shared by Windows and Linux desktops. No code is executed.""" import json import os import stat from pathlib import Path from samsara.core.privacy import minimize_inventory from samsara.core.storage import require_local_path from samsara.models.inventory import validate_inventory MAX_IMPORT_BYTES = 10 * 1024 * 1024 def load_inventory(path): local = require_local_path(path) flags=os.O_RDONLY|getattr(os,'O_NOFOLLOW',0)|getattr(os,'O_NONBLOCK',0)|getattr(os,'O_BINARY',0) with os.fdopen(os.open(local,flags),'rb') as source: if not stat.S_ISREG(os.fstat(source.fileno()).st_mode): raise ValueError('Only regular local inventory files may be imported.') raw = source.read(MAX_IMPORT_BYTES + 1) if len(raw) > MAX_IMPORT_BYTES: raise ValueError('Inventory exceeds the 10 MiB import limit.') try: data = json.loads(raw.decode('utf-8-sig')) except (ValueError, UnicodeError, RecursionError): raise ValueError('The selected file is not valid UTF-8 inventory JSON.') from None # A report wrapper is accepted, but its precomputed assessment is never trusted. if isinstance(data, dict) and 'inventory_result' in data: data = data['inventory_result'] validate_inventory(data) return validate_inventory(minimize_inventory(data))