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