def tranAssignParam(self): from pycropml.transpiler.main import Main snip = Main(self.assignParam(), "cs") a = snip.parse() g = snip.to_ast(self.assignParam()) snip.dictAst return snip.to_source()
def transpile_package(package, language): # translate from crop2ml package sourcef = package pkg = Path(sourcef) models = model_parser(pkg) # parse xml files and create python model object output = pkg/'src' dir_test= pkg/'test' m=[model.name for model in models] # Generate packages if the directories does not exists. if not output.isdir(): output.mkdir() if not dir_test.isdir(): dir_test.mkdir() m2p = render_cyml.Model2Package(models, dir=output) m2p.generate_package() # generate cyml models in "pyx" directory tg_rep = Path(output/"%s"%(language)) # target language models directory in output dir_test_lang = Path(dir_test/"%s"%(language)) if not tg_rep.isdir(): tg_rep.mkdir() if not dir_test_lang.isdir() : #Create if it doesn't exist dir_test_lang.mkdir() # generate cyml_rep = Path(output/'pyx') # cyml model directory in output for k, file in enumerate(cyml_rep.files()): #print(file) with open(file, 'r') as fi: source = fi.read() name = os.path.split(file)[1].split(".")[0] for model in models: # in the case we have'nt the same order if name == model.name.lower(): test=Main(file, language, model) test.parse() test.to_ast(source) code=test.to_source() filename = tg_rep/"%s.%s"%(name, language) with open(filename, "wb") as tg_file: tg_file.write(code.encode('utf-8')) # writeTest test = WriteTest(models,language,dir_test_lang) test.write() status = 0 return status
def compotranslate(self, language): d = self.algo2cyml() test = Main(d, language, self.model, self.model.name) test.parse() test.to_ast(d) code = test.translate() return code
def test_if_elif_else(): test = Main(source, "cs") test.parse() test.to_ast(source) code = test.to_source() print(code) assert (code == output_cs)
def test_for_range(): for lang in languages: test = Main(source, lang) test.parse() test.to_ast(source) code = test.to_source() print(code) assert (code == output[lang])
def testDeclaration(): test = Main(source, "py") test.parse() test.to_ast(source) code = test.to_source() print(code) assert (str(code) == output_cs)
def transpile_file(source, language): sourcef = source file = Path(sourcef) with open(file, 'r') as fi: source = fi.read() name = sourcef.split(".")[0] test = Main(file, language) test.parse() test.to_ast(source) code = test.to_source() filename = "%s.%s" % (name, language) with open(filename, "wb") as tg_file: tg_file.write(code.encode('utf-8')) return 0
} else { return a; } } }""" def test_if_elif_else(): test = Main(source, "cs") test.parse() test.to_ast(source) code = test.to_source() print(code) assert (code == output_cs) if __name__ == '__main__': test_if_elif_else() # coding: utf8 from pycropml.transpiler.main import Main source = u"""def test(int a): cdef int x=1, y=2 x=2 return a""" test = Main(source, "cs") test.parse() test.to_ast(source)
# coding: utf-8 from __future__ import absolute_import from __future__ import print_function from pycropml.transpiler.main import Main source = u"""import numpy as np from math import * def initshootnumber_(float sowingDensity=288.0): cdef float x x=23*(4/5*2.0) return x """ test = Main(source, 'f90') a = test.parse() g = test.to_ast(source) test.dictAst code = test.to_source() print(code)
def transpile_package(package, language): # translate from crop2ml package sourcef = package namep = sourcef.split(".")[0] pkg = Path(sourcef) models = model_parser( pkg) # parse xml files and create python model object output = Path(os.path.join(pkg, 'src')) dir_test = Path(os.path.join(pkg, 'test')) # Generate packages if the directories does not exists. if not output.isdir(): output.mkdir() if not dir_test.isdir(): dir_test.mkdir() m2p = render_cyml.Model2Package(models, dir=output) m2p.generate_package() # generate cyml models in "pyx" directory tg_rep = Path(os.path.join( output, language)) # target language models directory in output dir_test_lang = Path(os.path.join(dir_test, language)) if not tg_rep.isdir(): tg_rep.mkdir() if not dir_test_lang.isdir(): #Create if it doesn't exist dir_test_lang.mkdir() m2p.write_tests() # generate cyml_rep = Path(os.path.join(output, 'pyx')) # cyml model directory in output T = Topology(namep, package) T_pyx = T.algo2cyml() namep = T.model.name.lower() fileT = Path(os.path.join(cyml_rep, "%s.pyx" % namep)) with open(fileT, "wb") as tg_file: tg_file.write(T_pyx.encode('utf-8')) if language in ("cs", "java"): getattr( getattr(pycropml.transpiler.generators, '%sGenerator' % NAMES[language]), 'to_struct_%s' % language)([T.model], tg_rep, namep) to_wrapper_cs(T.model, tg_rep, namep) if language == "cs" else "" filename = Path( os.path.join(tg_rep, "%s.%s" % (namep.capitalize(), language))) with open(filename, "wb") as tg_file: tg_file.write(T.compotranslate(language).encode('utf-8')) for k, file in enumerate(cyml_rep.files()): #print(file) with open(file, 'r') as fi: source = fi.read() name = os.path.split(file)[1].split(".")[0] for model in models: # in the case we have'nt the same order if name.lower() == model.name.lower( ) and prefix(model) != "function": test = Main(file, language, model, T.model.name) test.parse() test.to_ast(source) code = test.to_source() filename = Path( os.path.join(tg_rep, "%s.%s" % (name.capitalize(), language))) with open(filename, "wb") as tg_file: tg_file.write(code.encode('utf-8')) Model2Nb(model, code, name, dir_test_lang).generate_nb(language, tg_rep, namep) #code2nbk.generate_notebook(code, name, dir_nb_lang) # writeTest '''TODO''' #test = WriteTest(models,language,dir_test_lang) #test.write() status = 0 return status
def transpile_package(package, language): """ translate from crop2ml package""" sourcef = package namep = sourcef.split(os.path.sep)[-1] pkg = Path(sourcef) models = model_parser( pkg) # parse xml files and create python model object output = Path(os.path.join(pkg, 'src')) dir_test = Path(os.path.join(pkg, 'test')) # Generate packages if the directories does not exists. if not output.isdir(): output.mkdir() if not dir_test.isdir(): dir_test.mkdir() m2p = render_cyml.Model2Package(models, dir=output) m2p.generate_package() # generate cyml models in "pyx" directory tg_rep1 = Path(os.path.join( output, language)) # target language models directory in output dir_test_lang = Path(os.path.join(dir_test, language)) if not tg_rep1.isdir(): tg_rep1.mkdir() tg_rep = Path(os.path.join(tg_rep1, namep)) if not tg_rep.isdir(): tg_rep.mkdir() if not dir_test_lang.isdir(): #Create if it doesn't exist dir_test_lang.mkdir() m2p.write_tests() # generate cyml functions cyml_rep = Path(os.path.join(output, 'pyx')) # cyml model directory in output # cretae topology of composite model T = Topology(namep, package) namep = T.model.name # Record VPZ if language == "record": vpz = Crop2ML_Vpz(T) print(vpz.create()) # domain class if language in domain_class: getattr( getattr(pycropml.transpiler.generators, '%sGenerator' % NAMES[language]), 'to_struct_%s' % language)([T.model], tg_rep, namep) # wrapper if language in wrapper: getattr( getattr(pycropml.transpiler.generators, '%sGenerator' % NAMES[language]), 'to_wrapper_%s' % language)(T.model, tg_rep, namep) # Transform model unit to languages and platforms for k, file in enumerate(cyml_rep.files()): with open(file, 'r') as fi: source = fi.read() name = os.path.split(file)[1].split(".")[0] for model in models: # in the case we have'nt the same order if name.lower() == model.name.lower( ) and prefix(model) != "function": test = Main(file, language, model, T.model.name) test.parse() test.to_ast(source) code = test.to_source() filename = Path( os.path.join(tg_rep, "%s.%s" % (name.capitalize(), ext[language]))) with open(filename, "wb") as tg_file: tg_file.write(code.encode('utf-8')) Model2Nb(model, code, name, dir_test_lang).generate_nb(language, tg_rep, namep) #code2nbk.generate_notebook(code, name, dir_nb_lang) # Create Cyml Composite model T_pyx = T.algo2cyml() fileT = Path(os.path.join(cyml_rep, "%sComponent.pyx" % namep)) with open(fileT, "wb") as tg_file: tg_file.write(T_pyx.encode('utf-8')) filename = Path( os.path.join(tg_rep, "%sComponent.%s" % (namep, ext[language]))) with open(filename, "wb") as tg_file: tg_file.write(T.compotranslate(language).encode('utf-8')) ## create computing algorithm if language == "py": simulation = PythonSimulation(T.model) simulation.generate() code = ''.join(simulation.result) filename = Path(os.path.join(tg_rep, "simulation.py")) initfile = Path(os.path.join(tg_rep, "__init__.py")) with open(filename, "wb") as tg_file: tg_file.write(code.encode("utf-8")) with open(initfile, "wb") as tg_file: tg_file.write("".encode("utf-8")) setup = PythonSimulation(T.model) setup.generate_setup() code = ''.join(setup.result) setupfile = Path(os.path.join(tg_rep1, "setup.py")) with open(setupfile, "wb") as tg_file: tg_file.write(code.encode("utf-8")) status = 0 return status