def test_loader_local(): # create a loader loader = Loader() path_to_this_file, this_file_name = os.path.split(__file__) found_file = loader.find_file(file_name=this_file_name, mixed_path=['./', path_to_this_file]) assert os.path.realpath(__file__) == os.path.realpath(found_file)
def __init__(self, manifest_filename_or_mapping, loader=None): """ :param manifest_filename_or_mapping: file name of project manifest or a mapping containing the same information :param project_root_or :param loader: loader object that resolves files in a search path """ if loader: self.loader = loader else: self.loader = Loader() if isinstance(manifest_filename_or_mapping, string_types): # we got a manifest file name, one of # local filename # github path in the form of https://github.com/owner/repo/some/path/to/project.yml mapping = self._load_manifest(manifest_filename_or_mapping) else: # we got a mapping mapping = manifest_filename_or_mapping # get path, title, etc. from mapping assert isinstance(mapping, dict) self.title = mapping['title'] self.code_path = mapping['code_path'] self.concept_path = mapping['concept_path'] self.template_path = mapping['template_path']
def test_loader(): local_root_dir = mkdtemp() # create a loader with a new temporary directory loader1 = Loader(local_root_dir=local_root_dir) # locate requirements.txt, after cloning the repo local_path = loader1.find_file("requirements", ["https://github.com/imodels/simgen.git"], implicit_ext=['', '.txt']) assert local_path is not None assert os.path.realpath(local_path) == os.path.realpath( os.path.join(local_root_dir, 'imodels', 'simgen', 'requirements.txt')) # create a new loader with the same temp dir loader2 = Loader(local_root_dir=local_root_dir) # locate requirements.txt, after pulling the repo local_path = loader2.find_file("requirements", ["https://github.com/imodels/simgen.git"], implicit_ext=['', '.txt']) assert local_path is not None assert os.path.realpath(local_path) == os.path.realpath( os.path.join(local_root_dir, 'imodels', 'simgen', 'requirements.txt')) # create a new offline loader with explicit github url to local directory association loader3 = Loader() loader3.add_repo( "https://github.com/imodels/simgen.git", os.path.realpath(os.path.join(local_root_dir, 'imodels', 'simgen'))) # locate requirements.txt locally local_path = loader3.find_file("requirements", ["https://github.com/iModels/simgen.git"], implicit_ext=['', '.txt']) assert local_path is not None assert os.path.realpath(local_path) == os.path.realpath( os.path.join(local_root_dir, 'imodels', 'simgen', 'requirements.txt'))
def test_ast(): # create a new offline loader with explicit github url to local directory association loader = Loader() loader.add_repo("https://github.com/imodels/simgen.git", os.path.split(os.path.dirname(__file__))[0]) ast_node = AstNode( file_name='prg', loader=loader, search_path=['https://github.com/imodels/simgen/res/ast_test']) assert ast_node is not None assert ast_node.nodetype_name == 'add' assert ast_node.mapping['add'] is not None assert ast_node.mapping['add']['expr1'] is not None assert ast_node.mapping['add']['expr2'] is not None ast_node.validate()
def run(): # # configure logging # logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=0) # create a simulated online loader with explicit github url to local directory association loader = Loader() local_repo_dir = os.path.join(os.path.dirname(__file__), '..', '..') loader.add_repo("https://github.com/imodels/simgen.git", local_repo_dir) # # initialize a project project = Project( 'https://github.com/imodels/simgen/res/binary_lj_sim/online_project.yaml', loader) generated_code = project.render('binary_lj_sim_prg', output_dir='./generated_code') print("Generated code:\n {}".format(generated_code)) print("Additional files have been saved to: ./generated_code")
def test_render_local(): # create a new offline loader with explicit github url to local directory association res_dir = os.path.join(dirname(__file__), '..', 'res', 'ast_test') loader = Loader() manifest = { 'title': 'adder', 'code_path': [os.path.join(res_dir, 'code')], 'concept_path': [os.path.join(res_dir, 'concepts')], 'template_path': [os.path.join(res_dir, 'templates')] } ast_node = AstNode(file_name='prg', loader=loader, **manifest) renderer = Renderer(loader, **manifest) rendered_code = renderer.render_ast(ast_node) assert rendered_code == '1 + 2 + 3' rendered_code = renderer.render_file('prg') assert rendered_code == '1 + 2 + 3'