def translate_text_ast_to_zip(self, code: str) -> bytes: """Translate a text ast into a zip file as a memory stream Arguments: code Text `qastle` version of the input ast generated by func_adl target_backend Which backend to generate code for? xAOD or Uproot Returns bytes Data that if written as a binary output would be a zip file. """ if len(code) == 0: raise GenerateCodeException("Requested codegen for an empty string.") body = text_ast_to_python_ast(code).body if len(body) != 1: raise GenerateCodeException( f'Requested codegen for "{code}" yielded no code statements (or too many).') # noqa: E501 a = body[0].value # Generate the python code with TemporaryDirectory() as tempdir: r = self.get_generated_uproot(a, tempdir) # Zip up everything in the directory - we are going to ship it as back as part # of the message. z_filename = os.path.join(str(tempdir), 'joined.zip') zip_h = zipfile.ZipFile(z_filename, 'w', zipfile.ZIP_DEFLATED) self.zipdir(r.output_dir, zip_h) zip_h.close() with open(z_filename, 'rb') as b_in: return b_in.read()
async def exe_from_qastle(q: str): 'Dummy executor that will return the ast properly rendered. If qastle_roundtrip is true, then we will round trip the ast via qastle first.' # Round trip qastle if requested. import qastle a = qastle.text_ast_to_python_ast(q).body[0].value # Setup the rep for this filter from func_adl import find_EventDataset file = find_EventDataset(a) iterator = cpp_variable("bogus-do-not-use", top_level_scope(), cpp_type=None) set_rep(file, cpp_sequence(iterator, iterator, top_level_scope())) # Use the dummy executor to process this, and return it. exe = atlas_xaod_dummy_executor() exe.evaluate(a) return exe
async def execute_result_async(self, a: ast.AST, title: str) -> Any: 'Dummy executor that will return the ast properly rendered. If qastle_roundtrip is true, then we will round trip the ast via qastle first.' # Round trip qastle if requested. if self._q_roundtrip: import qastle print(f'before: {ast.dump(a)}') a_text = qastle.python_ast_to_text_ast(a) a = qastle.text_ast_to_python_ast(a_text).body[0].value print(f'after: {ast.dump(a)}') # Setup the rep for this dataset from func_adl import find_EventDataset file = find_EventDataset(a) iterator = cpp_variable("bogus-do-not-use", top_level_scope(), cpp_type=None) set_rep(file, cpp_sequence(iterator, iterator, top_level_scope())) # Use the dummy executor to process this, and return it. exe = self.get_dummy_executor_obj() exe.evaluate(a) return exe
def translate_text_ast_to_zip(self, code: str) -> bytes: """Translate a text ast into a zip file as a memory stream Arguments: code Text `qastle` version of the input ast generated by func_adl Returns bytes Data that if written as a binary output would be a zip file. """ if len(code) == 0: raise GenerateCodeException( "Requested codegen for an empty string.") body = text_ast_to_python_ast(code).body print("------>", code, body) if len(body) != 1: raise GenerateCodeException( f'Requested codegen for "{code}" yielded no code statements (or too many).' ) # noqa: E501 a = body[0].value # Generate the C++ code with TemporaryDirectory() as tempdir: loc = Path(tempdir) / 'hash' self.get_generated_xAOD(a, loc) # Zip up everything in the directory - we are going to ship it as back as part # of the message. z_filename = Path(tempdir) / 'joined.zip' zip_h = zipfile.ZipFile(z_filename, 'w', zipfile.ZIP_DEFLATED) self._zipdir(loc, zip_h) zip_h.close() with z_filename.open('rb') as b_in: return b_in.read()