示例#1
0
    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']
示例#2
0
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)
示例#3
0
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'))
示例#4
0
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")
示例#6
0
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()
示例#7
0
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")
示例#8
0
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'
示例#9
0
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)
示例#10
0
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'))
示例#11
0
class Project(object):
    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 _load_manifest(self, manifest_filename):
        implicit_exts = ['', '.yaml', '.yml']
        log.debug("Loading project manifest from {} {}".format(manifest_filename, implicit_exts))
        resolved_manifest_filename = self.loader.find_file(manifest_filename, implicit_ext=implicit_exts)
        if resolved_manifest_filename is None:
            raise Error('Cannot find manifest file {}'.format(manifest_filename))
        with open(resolved_manifest_filename, 'r') as f:
            mapping = marked_load(f)

        return mapping

    def load_ast(self, file_name, inject_dict=None, validation=True):
        # find file
        log.debug('Loading ast from file: {}'.format(file_name))

        # load ast
        ast = AstNode(file_name=file_name, loader=self.loader, code_path=self.code_path, concept_path=self.concept_path)
        log.debug("Ast loaded: {}".format(ast))

        # inject values from inject_dict to replace placeholders in ast
        if inject_dict:
            ast.inject(inject_dict)

        # validate
        if validation:
            ast.validate()

        return ast

    def render(self, ast_or_filename, output_dir='', inject_dict=None, validation=True):
        if isinstance(ast_or_filename, string_types):
            ast = self.load_ast(ast_or_filename, inject_dict=inject_dict, validation=validation)
        else:
            ast = ast_or_filename
            assert isinstance(ast, AstNode)
            ast.inject(inject_dict)
            ast.validate()

        self.renderer = Renderer(self.loader, output_dir=output_dir, code_path=self.code_path, concept_path=self.concept_path, template_path=self.template_path)
        rendered_code = self.renderer.render_ast(ast)
        return rendered_code


    def render_tasks(self, ast_or_filename, output_dir='', inject_dict=None, validation=True):

        gen_code = self.render(ast_or_filename, output_dir, inject_dict, validation)
        run_script = []
        for line in gen_code.split("\n"):
            if line.strip().startswith("#"):
                continue
            elif not line.strip():
                continue
            else:
                run_script.append(line.strip())

        return run_script