def start_xfst(**kwargs): """ Start interactive xfst compiler. Parameters ---------- * `kwargs` : Arguments recognized are: type, quit_on_fail. * `quit_on_fail` : Whether the compiler exits on any error, defaults to False. * `type` : Implementation type of the compiler, defaults to hfst.get_default_fst_type(). """ import sys idle = 'idlelib' in sys.modules if idle: print('It seems that you are running python in in IDLE. Note that all output from xfst will be buffered.') print('This means that all warnings, e.g. about time-consuming operations, will be printed only after the operation is carried out.') print('Consider running python from shell, for example command prompt, if you wish to see output with no delays.') type = get_default_fst_type() quit_on_fail = 'OFF' to_console=get_output_to_console() for k,v in kwargs.items(): if k == 'type': type = v elif k == 'output_to_console': to_console=v elif k == 'quit_on_fail': if v == True: quit_on_fail='ON' else: print('Warning: ignoring unknown argument %s.' % (k)) comp = XfstCompiler(type) comp.setReadInteractiveTextFromStdin(True) comp.setReadline(False) # do not mix python and c++ readline if to_console and idle: print('Cannot output to console when running libhfst from IDLE.') to_console=False comp.setOutputToConsole(to_console) comp.set('quit-on-fail', quit_on_fail) rl_length_1 = 0 rl_found = False try: import readline rl_found = True rl_length_1 = readline.get_current_history_length() except ImportError: pass import sys expression="" while True: expression += input(comp.get_prompt()).rstrip().lstrip() if len(expression) == 0: continue if expression[-1] == '\\': expression = expression[:-2] + '\n' continue retval = -1 if idle: retval = libhfst.hfst_compile_xfst_to_string_one(comp, expression) stdout.write(libhfst.get_hfst_xfst_string_one()) else: # interactive command if (expression == "apply down" or expression == "apply up") and rl_found: rl_length_2 = readline.get_current_history_length() while True: try: line = input().rstrip().lstrip() except EOFError: break if expression == "apply down": comp.apply_down(line) elif expression == "apply up": comp.apply_up(line) for foo in range(readline.get_current_history_length() - rl_length_2): readline.remove_history_item(rl_length_2) retval = 0 elif expression == "inspect" or expression == "inspect net": print('inspect net not supported') retval = 0 else: retval = comp.parse_line(expression + "\n") if retval != 0: print("expression '%s' could not be parsed" % expression) if comp.get("quit-on-fail") == "ON": return if comp.quit_requested(): break expression = "" if rl_found: for foo in range(readline.get_current_history_length() - rl_length_1): readline.remove_history_item(rl_length_1)
def start_xfst(**kvargs): """ Start interactive xfst compiler. Parameters ---------- * `kvargs` : Arguments recognized are: type, quit_on_fail. * `quit_on_fail` : Whether the compiler exits on any error, defaults to False. * `type` : Implementation type of the compiler, defaults to hfst.get_default_fst_type(). """ import sys idle = 'idlelib' in sys.modules if idle: print('It seems that you are running python in in IDLE. Note that all output from xfst will be buffered.') print('This means that all warnings, e.g. about time-consuming operations, will be printed only after the operation is carried out.') print('Consider running python from shell, for example command prompt, if you wish to see output with no delays.') type = get_default_fst_type() quit_on_fail = 'OFF' to_console=get_output_to_console() for k,v in kvargs.items(): if k == 'type': type = v elif k == 'output_to_console': to_console=v elif k == 'quit_on_fail': if v == True: quit_on_fail='ON' else: print('Warning: ignoring unknown argument %s.' % (k)) comp = XfstCompiler(type) comp.setReadInteractiveTextFromStdin(True) comp.setReadline(False) # do not mix python and c++ readline if to_console and idle: print('Cannot output to console when running libhfst from IDLE.') to_console=False comp.setOutputToConsole(to_console) comp.set('quit-on-fail', quit_on_fail) rl_length_1 = 0 rl_found = False try: import readline rl_found = True rl_length_1 = readline.get_current_history_length() except ImportError: pass import sys expression="" while True: expression += input(comp.get_prompt()).rstrip().lstrip() if len(expression) == 0: continue if expression[-1] == '\\': expression = expression[:-2] + '\n' continue retval = -1 if idle: retval = libhfst.hfst_compile_xfst_to_string_one(comp, expression) stdout.write(libhfst.get_hfst_xfst_string_one()) else: # interactive command if (expression == "apply down" or expression == "apply up") and rl_found: rl_length_2 = readline.get_current_history_length() while True: try: line = input().rstrip().lstrip() except EOFError: break if expression == "apply down": comp.apply_down(line) elif expression == "apply up": comp.apply_up(line) for foo in range(readline.get_current_history_length() - rl_length_2): readline.remove_history_item(rl_length_2) retval = 0 elif expression == "inspect" or expression == "inspect net": print('inspect net not supported') retval = 0 else: retval = comp.parse_line(expression + "\n") if retval != 0: print("expression '%s' could not be parsed" % expression) if comp.get("quit-on-fail") == "ON": return if comp.quit_requested(): break expression = "" if rl_found: for foo in range(readline.get_current_history_length() - rl_length_1): readline.remove_history_item(rl_length_1)
def compile_xfst_file(filename, **kwargs): """ Compile (run) xfst file *filename*. Parameters ---------- * `filename` : The name of the xfst file. * `kwargs` : Arguments recognized are: verbosity, quit_on_fail, output, type. * `verbosity` : The verbosity of the compiler, defaults to 0 (silent). Possible values are: 0, 1, 2. * `quit_on_fail` : Whether the script is exited on any error, defaults to True. * `output` : Where output is printed. Possible values are sys.stdout, sys.stderr, a StringIO, sys.stderr being the default? * `type` : Implementation type of the compiler, defaults to hfst.get_default_fst_type(). Returns ------- On success 0, else an integer greater than 0. """ if int(version[0]) > 2: pass else: raise RuntimeError('hfst.compile_xfst_file not supported for python version 2') verbosity=0 quit_on_fail='ON' type = get_default_fst_type() output=None error=None to_console=get_output_to_console() for k,v in kwargs.items(): if k == 'verbosity': verbosity=v elif k == 'quit_on_fail': if v == False: quit_on_fail='OFF' elif k == 'output': output=v elif k == 'error': error=v elif k == 'output_to_console': to_console=v else: print('Warning: ignoring unknown argument %s.' % (k)) if verbosity > 1: print('Compiling with %s implementation...' % fst_type_to_string(type)) xfstcomp = XfstCompiler(type) xfstcomp.setOutputToConsole(to_console) xfstcomp.setVerbosity(verbosity > 0) xfstcomp.set('quit-on-fail', quit_on_fail) if verbosity > 1: print('Opening xfst file %s...' % filename) f = open(filename, 'r', encoding='utf-8') data = f.read() f.close() if verbosity > 1: print('File closed...') retval=-1 import sys from io import StringIO # check special case if isinstance(output, StringIO) and isinstance(error, StringIO) and output == error: retval = libhfst.hfst_compile_xfst_to_string_one(xfstcomp, data) output.write(unicode(libhfst.get_hfst_xfst_string_one(), 'utf-8')) else: arg1 = "" arg2 = "" if output == None or output == sys.stdout: arg1 = "cout" if output == sys.stderr: arg1 == "cerr" if error == None or error == sys.stderr: arg2 = "cerr" if error == sys.stdout: arg2 == "cout" retval = libhfst.hfst_compile_xfst(xfstcomp, data, arg1, arg2) if isinstance(output, StringIO): output.write(unicode(libhfst.get_hfst_xfst_string_one(), 'utf-8')) if isinstance(error, StringIO): error.write(unicode(libhfst.get_hfst_xfst_string_two(), 'utf-8')) if verbosity > 1: print('Parsed file with return value %i (0 indicating succesful parsing).' % retval) return retval
def compile_xfst_file(filename, **kvargs): """ Compile (run) xfst file *filename*. Parameters ---------- * `filename` : The name of the xfst file. * `kvargs` : Arguments recognized are: verbosity, quit_on_fail, output, type. * `verbosity` : The verbosity of the compiler, defaults to 0 (silent). Possible values are: 0, 1, 2. * `quit_on_fail` : Whether the script is exited on any error, defaults to True. * `output` : Where output is printed. Possible values are sys.stdout, sys.stderr, a StringIO, sys.stderr being the default? * `type` : Implementation type of the compiler, defaults to hfst.get_default_fst_type(). Returns ------- On success 0, else an integer greater than 0. """ if int(version[0]) > 2: pass else: raise RuntimeError('hfst.compile_xfst_file not supported for python version 2') verbosity=0 quit_on_fail='ON' type = get_default_fst_type() output=None error=None to_console=get_output_to_console() for k,v in kvargs.items(): if k == 'verbosity': verbosity=v elif k == 'quit_on_fail': if v == False: quit_on_fail='OFF' elif k == 'output': output=v elif k == 'error': error=v elif k == 'output_to_console': to_console=v else: print('Warning: ignoring unknown argument %s.' % (k)) if verbosity > 1: print('Compiling with %s implementation...' % fst_type_to_string(type)) xfstcomp = XfstCompiler(type) xfstcomp.setOutputToConsole(to_console) xfstcomp.setVerbosity(verbosity > 0) xfstcomp.set('quit-on-fail', quit_on_fail) if verbosity > 1: print('Opening xfst file %s...' % filename) f = open(filename, 'r', encoding='utf-8') data = f.read() f.close() if verbosity > 1: print('File closed...') retval=-1 import sys from io import StringIO # check special case if isinstance(output, StringIO) and isinstance(error, StringIO) and output == error: retval = libhfst.hfst_compile_xfst_to_string_one(xfstcomp, data) output.write(unicode(libhfst.get_hfst_xfst_string_one(), 'utf-8')) else: arg1 = "" arg2 = "" if output == None or output == sys.stdout: arg1 = "cout" if output == sys.stderr: arg1 == "cerr" if error == None or error == sys.stderr: arg2 = "cerr" if error == sys.stdout: arg2 == "cout" retval = libhfst.hfst_compile_xfst(xfstcomp, data, arg1, arg2) if isinstance(output, StringIO): output.write(unicode(libhfst.get_hfst_xfst_string_one(), 'utf-8')) if isinstance(error, StringIO): error.write(unicode(libhfst.get_hfst_xfst_string_two(), 'utf-8')) if verbosity > 1: print('Parsed file with return value %i (0 indicating succesful parsing).' % retval) return retval