def _validate_write_file(self, body): """Validate 'write_file' statements. The body of a 'write_file' statement is a two-element dict with known keys: 'filename' and 'contents'. 'filename' maps to a list of strings which constitute a format string and variable-based rvalues defining the fields. 'contents' maps to a single variable-based rvalue. Raises: MismatchedFormatSpecifier: If the filename formatstring is badly formed. UndefinedVariableReference: If any of the formatstring variables or the file contents variable are undefined. InvalidStatement: If either 'filename' or 'contents' are absent keys. """ fname_fmt = body.get("filename") if not fname_fmt: raise types.InvalidStatement( "Missing key in 'write_file' statement: 'filename'") self._validate_format(fname_fmt) contents_var = body.get("contents") if not contents_var: raise types.InvalidStatement( "Missing key in 'write_file' statement: 'contents'") self.validate_expression(contents_var)
def validate_response(self, response): """Validates a "response" block from a sample config. A full description of the response block is outside the scope of this code; refer to the samplegen documentation. Dispatches statements to sub-validators. Args: response: list[dict{str:Any}]: The structured data representing the sample's response. Raises: InvalidStatement: If an unexpected key is found in a statement dict or a statement dict has more than or less than one key. """ for statement in response: if len(statement) != 1: raise types.InvalidStatement( "Invalid statement: {}".format(statement)) keyword, body = next(iter(statement.items())) validater = self.STATEMENT_DISPATCH_TABLE.get(keyword) if not validater: raise types.InvalidStatement( "Invalid statement keyword: {}".format(keyword)) validater(self, body)