"""IOContext: per-scenario state for the io module (files, JSON, CSV)."""from__future__importannotationsimportcsvfromdataclassesimportdataclass,fieldfromtypingimportAny,TextIO
[docs]@dataclassclassIOContext:"""Holds all io-module state for a scenario. Lives at ``context.steplib.io`` and is reset between scenarios. Attributes: variables: User-defined variables stored by file steps. _last_json: The last parsed JSON value (dict or list). _csv_writer: Active CSV writer instance, if any. _csv_file: Underlying file handle for the CSV writer, if any. """variables:dict[str,Any]=field(default_factory=dict)_last_json:Any=None_csv_writer:csv.DictWriter[str]|None=None_csv_file:TextIO|None=None
[docs]defreset(self)->None:"""Reset per-scenario state, closing any open CSV writer."""self.variables={}self._last_json=Noneself._close_csv()self._csv_writer=Noneself._csv_file=None
[docs]defcleanup(self)->None:"""Clean up resources after the scenario."""self._close_csv()
def_close_csv(self)->None:"""Close the CSV file handle if open."""ifself._csv_fileisnotNoneandnotself._csv_file.closed:self._csv_file.close()