def parse_data_commands(data=None, filename=None, debug=0, outputdir=None): global debugging global dat_lexer global dat_yaccer if outputdir is None: # Try and write this into the module source... outputdir = os.path.dirname(getfile( currentframe() )) # Ideally, we would pollute a per-user configuration directory # first -- something like ~/.pyomo. if not os.access(outputdir, os.W_OK): outputdir = os.getcwd() # if the lexer/yaccer haven't been initialized, do so. if dat_lexer is None: # # Always remove the parser.out file, which is generated to # create debugging # if os.path.exists("parser.out"): #pragma:nocover os.remove("parser.out") if debug > 0: #pragma:nocover # # Remove the parsetab.py* files. These apparently need to # be removed to ensure the creation of a parser.out file. # if os.path.exists(tabmodule+".py"): os.remove(tabmodule+".py") if os.path.exists(tabmodule+".pyc"): os.remove(tabmodule+".pyc") debugging=True dat_lexer = lex.lex() # tmpsyspath = sys.path sys.path.append(outputdir) dat_yaccer = yacc.yacc(debug=debug, tabmodule=tabmodule, outputdir=outputdir, optimize=True) sys.path = tmpsyspath # # Initialize parse object # global _parse_info _parse_info = {} _parse_info[None] = [] # # Parse the file # global _parsedata if not data is None: _parsedata=data ply_init(_parsedata) dat_yaccer.parse(data, lexer=dat_lexer, debug=debug) elif not filename is None: f = open(filename, 'r') try: data = f.read() except Exception: e = sys.exc_info()[1] f.close() del f raise e f.close() del f _parsedata=data ply_init(_parsedata) dat_yaccer.parse(data, lexer=dat_lexer, debug=debug) else: _parse_info = None # # Disable parsing I/O # debugging=False #print(_parse_info) return _parse_info
def parse_data_commands(data=None, filename=None, debug=0, outputdir=None): global debugging global ampl_dat_lexer global ampl_dat_yaccer if outputdir is None: outputdir = os.path.dirname(os.path.abspath(__file__))+os.sep # if the lexer/yaccer haven't been initialized, do so. if ampl_dat_lexer is None: # # Always remove the parser.out file, which is generated to create debugging # if os.path.exists("parser.out"): #pragma:nocover os.remove("parser.out") if debug > 0: #pragma:nocover # # Remove the parsetab.py* files. These apparently need to be removed # to ensure the creation of a parser.out file. # if os.path.exists("parsetab.py"): os.remove("parsetab.py") if os.path.exists("parsetab.pyc"): os.remove("parsetab.pyc") debugging=True ampl_dat_lexer = lex.lex() # tmpsyspath = sys.path sys.path.append(outputdir) ampl_dat_yaccer = yacc.yacc(debug=debug, tabmodule=tabmodule, outputdir=outputdir) sys.path = tmpsyspath # # Initialize parse object # global _parse_info _parse_info = {} _parse_info[None] = [] # # Parse the file # global _parsedata if not data is None: _parsedata=data ply_init(_parsedata) ampl_dat_yaccer.parse(data, lexer=ampl_dat_lexer, debug=debug) elif not filename is None: f = open(filename, 'r') try: data = f.read() except Exception: f.close() del f raise e f.close() del f _parsedata=data ply_init(_parsedata) ampl_dat_yaccer.parse(data, lexer=ampl_dat_lexer, debug=debug) else: _parse_info = None # # Disable parsing I/O # debugging=False return _parse_info