def run_server(host, port, log_path=None): global scriptpath grammar_src = os.path.abspath(__file__).replace('Server.py', '.ebnf') dhparserdir = os.path.abspath(os.path.join(scriptpath, '../..')) if scriptpath not in sys.path: sys.path.append(scriptpath) if dhparserdir not in sys.path: sys.path.append(dhparserdir) # from tst_neu_grammar import recompile_grammar # recompile_grammar(os.path.join(scriptpath, 'neu.ebnf'), force=False) from DHParser.dsl import recompile_grammar if not recompile_grammar( grammar_src, force=False, notify=lambda: print('recompiling ' + grammar_src)): print('\nErrors while recompiling "%s":' % grammar_src + '\n--------------------------------------\n\n') with open('neu_ebnf_ERRORS.txt', encoding='utf-8') as f: print(f.read()) sys.exit(1) from neuParser import compile_src from DHParser.server import Server, gen_lsp_table config_filename = get_config_filename() try: with open(config_filename, 'w') as f: f.write(host + ' ' + str(port)) except PermissionError: print('PermissionError: Could not write temporary config file: ' + config_filename) print('Starting server on %s:%i' % (host, port)) neu_lsp = neuLanguageServerProtocol() lsp_table = neu_lsp.lsp_fulltable.copy() lsp_table.setdefault('default', compile_src) neu_server = Server(rpc_functions=lsp_table, cpu_bound=neu_lsp.cpu_bound.lsp_table.keys(), blocking=neu_lsp.blocking.lsp_table.keys(), connection_callback=neu_lsp.connect, server_name="neuServer", strict_lsp=True) if log_path is not None: neu_server.echo_log = True print(neu_server.start_logging(log_path)) try: neu_server.run_server(host, port) # returns only after server has stopped except OSError as e: print(e) print('Could not start server. Shutting down!') sys.exit(1) finally: cfg_filename = get_config_filename() print('Server on %s:%i stopped' % (host, port)) try: os.remove(cfg_filename) print('Removing temporary config file: "{}".'.format(cfg_filename)) except FileNotFoundError: print('Config file "{}" does not exist any more.'.format( cfg_filename))
def recompile_grammar(grammar_src, force): # recompiles Grammar only if it has changed if not dsl.recompile_grammar(grammar_src, force=force): print('\nErrors while recompiling "%s":' % grammar_src + '\n--------------------------------------\n\n') with open('XML_ebnf_ERRORS.txt') as f: print(f.read()) sys.exit(1)
def recompile_grammar(grammar_src, force): # recompiles Grammar only if it has changed grammar_path = os.path.join(fullpath, grammar_src) if not dsl.recompile_grammar(grammar_path, force=force, notify=lambda :print('grammar changed: recompling')): print('\nErrors while recompiling "%s":' % grammar_src + '\n--------------------------------------\n\n') with open('LaTeX_ebnf_ERRORS.txt') as f: print(f.read()) sys.exit(1)
def recompile_grammar(grammar_src, force): grammar_tests_dir = os.path.join(scriptpath, 'test_grammar') testing.create_test_templates(grammar_src, grammar_tests_dir) # recompiles Grammar only if it has changed if not dsl.recompile_grammar(grammar_src, force=force, notify=lambda: print('recompiling ' + grammar_src)): print('\nErrors while recompiling "%s":' % grammar_src + '\n--------------------------------------\n\n') with open('yaml_ebnf_ERRORS.txt') as f: print(f.read()) sys.exit(1)
def recompile_grammar(grammar_src, force): grammar_tests_dir = os.path.join(scriptpath, 'test_grammar') create_test_templates(grammar_src, grammar_tests_dir) # recompiles Grammar only if it has changed name = os.path.splitext(os.path.basename(grammar_src))[0] if not dsl.recompile_grammar( grammar_src, force=force, notify=lambda: print('recompiling ' + grammar_src)): print('\nErrors while recompiling "{}":'.format(grammar_src) + '\n--------------------------------------\n\n') with open('{}_ebnf_ERRORS.txt'.format(name)) as f: print(f.read()) sys.exit(1)
def recompile_grammar(grammar_src, force): grammar_tests_dir = os.path.join(scriptpath, TEST_DIRNAME) testing.create_test_templates(grammar_src, grammar_tests_dir) # recompiles Grammar only if it has changed if not dsl.recompile_grammar( grammar_src, force=force, notify=lambda: print('recompiling ' + grammar_src)): print('\nErrors while recompiling "%s":' % grammar_src + '\n--------------------------------------\n\n') with open('ts2dataclass_ebnf_ERRORS.txt', encoding='utf-8') as f: print(f.read()) sys.exit(1) dsl.restore_server_script(grammar_src)
def recompile_grammar(grammar_src, force): grammar_tests_dir = os.path.join(scriptpath, 'test_grammar') if not os.path.exists(grammar_tests_dir) \ or not any(os.path.isfile(os.path.join(grammar_tests_dir, entry)) for entry in os.listdir(grammar_tests_dir)): print('No grammar-tests found, generating test templates.') create_test_templates(grammar_src, grammar_tests_dir) DHParser.log.start_logging(LOGGING) # recompiles Grammar only if it has changed name = os.path.splitext(os.path.basename(grammar_src))[0] if not dsl.recompile_grammar(grammar_src, force=force): print('\nErrors while recompiling "{}":'.format(grammar_src) + '\n--------------------------------------\n\n') with open('{}_ebnf_ERRORS.txt'.format(name)) as f: print(f.read()) sys.exit(1)
def recompile_grammar(grammar_src, force): grammar_tests_dir = os.path.join(scriptpath, 'test_grammar') testing.create_test_templates(grammar_src, grammar_tests_dir) DHParser.log.start_logging('LOGS') # recompiles Grammar only if it has changed saved_syntax_variant = get_config_value('syntax_variant') set_config_value('syntax_variant', 'heuristic') if not dsl.recompile_grammar( grammar_src, force=force, notify=lambda: print('recompiling ' + grammar_src)): print('\nErrors while recompiling "%s":' % grammar_src + '\n--------------------------------------\n\n') if is_filename(grammar_src): err_name = grammar_src.replace('.', '_') + '_ERRORS.txt' else: err_name = 'EBNF_ebnf_ERRORS.txt' with open(err_name, encoding='utf-8') as f: print(f.read()) sys.exit(1) set_config_value('syntax_variant', saved_syntax_variant)
def run_server(host, port, log_path=None): """ Starts a new atfServer. If `port` is already occupied, different ports will be tried. """ global KNOWN_HOST, KNOWN_PORT global scriptpath, servername from multiprocessing import set_start_method # 'forkserver' or 'spawn' required to avoid broken process pools if sys.platform.lower().startswith('linux'): set_start_method('forkserver') else: set_start_method('spawn') grammar_src = os.path.abspath(__file__).replace('Server.py', '.ebnf') dhparserdir = os.path.abspath(os.path.join(scriptpath, '../..')) if scriptpath not in sys.path: sys.path.append(scriptpath) if dhparserdir not in sys.path: sys.path.append(dhparserdir) # from tst_atf_grammar import recompile_grammar # recompile_grammar(os.path.join(scriptpath, 'atf.ebnf'), force=False) from DHParser.dsl import recompile_grammar if not recompile_grammar( grammar_src, force=False, notify=lambda: print('recompiling ' + grammar_src)): print('\nErrors while recompiling "%s":' % grammar_src + '\n--------------------------------------\n\n') with open('atf_ebnf_ERRORS.txt', encoding='utf-8') as f: print(f.read()) sys.exit(1) from atfParser import compile_src from DHParser.server import Server, probe_tcp_server, StreamReaderProxy, StreamWriterProxy from DHParser.lsp import gen_lsp_table atf_lsp = atfLanguageServerProtocol() lsp_table = atf_lsp.lsp_fulltable.copy() lsp_table.setdefault('default', compile_src) atf_server = Server(rpc_functions=lsp_table, cpu_bound=atf_lsp.cpu_bound.lsp_table.keys(), blocking=atf_lsp.blocking.lsp_table.keys(), connection_callback=atf_lsp.connect, server_name="atfServer", strict_lsp=True) if log_path is not None: # echoing does not work with stream connections! atf_server.echo_log = True if port >= 0 and host else False msg = atf_server.start_logging(log_path.strip('" \'')) if atf_server.echo_log: echo(msg) if port < 0 or not host: # communication via streams instead of tcp server reader = StreamReaderProxy(sys.stdin) writer = StreamWriterProxy(sys.stdout) atf_server.run_stream_server(reader, writer) return cfg_filename = get_config_filename() overwrite = not os.path.exists(cfg_filename) ports = ALTERNATIVE_PORTS.copy() if port == DEFAULT_PORT else [] if port in ports: ports.remove(port) ports.append(port) while ports: port = ports.pop() if (host, port) == (KNOWN_HOST, KNOWN_PORT): ident = asyncio_run( probe_tcp_server(host, port, SERVER_REPLY_TIMEOUT)) if ident: if ident.endswith(servername): echo( 'A server of type "%s" already exists on %s:%i.' % (servername, host, port) + ' Use --port option to start a secondary server on a different port.' ) sys.exit(1) if ports: echo('"%s" already occupies %s:%i. Trying port %i' % (ident, host, port, ports[-1])) continue else: echo('"%s" already occupies %s:%i. No more ports to try.' % (ident, host, port)) sys.exit(1) if overwrite: try: with open(cfg_filename, 'w') as f: debug('Storing host and port value %s:%i in file "%s".' % (host, port, cfg_filename)) f.write(host + ' ' + str(port)) except (PermissionError, IOError) as e: echo('%s: Could not write temporary config file: "%s"' % (str(e), cfg_filename)) ports = [] else: echo( 'Configuration file "%s" already existed and was not overwritten. ' 'Use option "--port %i" to stop this server!' % (cfg_filename, port)) try: debug('Starting server on %s:%i' % (host, port)) atf_server.run_tcp_server( host, port) # returns only after server has stopped! ports = [] except OSError as e: if not (ports and e.errno == 98): echo(e) echo('Could not start server. Shutting down!') sys.exit(1) elif ports: echo('Could not start server on %s:%i. Trying port %s' % (host, port, ports[-1])) else: echo('Could not start server on %s:%i. No more ports to try.' % (host, port)) finally: if not ports: echo('Server on %s:%i stopped' % (host, port)) if overwrite: try: os.remove(cfg_filename) debug('removing temporary config file: ' + cfg_filename) except FileNotFoundError: pass
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ import os import sys sys.path.extend([os.path.join('..', '..'), '..', '.']) flag = os.path.exists('LyrikParser.py') from DHParser.dsl import recompile_grammar if not recompile_grammar('.', force=True): with open('Lyrik_ebnf_ERRORS.txt') as f: print(f.read()) sys.exit(1) # Not needed if DHParser was installed on the system. Just a little # service for those who have merely checked out the git repository, # in particular for those reading the Tutorial in the Introduction if not flag: with open('LyrikParser.py', 'r') as f: script = f.read() i = script.find('import sys') + 10 script = script[:i] + "\nsys.path.extend([os.path.join('..', '..'), '..', '.'])\n" + script [i:] with open('LyrikParser.py', 'w') as f: f.write(script)
def run_server(host, port, log_path=None): global scriptpath grammar_src = os.path.abspath(__file__).replace('Server.py', '.ebnf') dhparserdir = os.path.abspath( os.path.join(scriptpath, os.path.join('..', '..'))) if scriptpath not in sys.path: sys.path.append(scriptpath) if dhparserdir not in sys.path: sys.path.append(dhparserdir) from DHParser.dsl import recompile_grammar if not recompile_grammar( grammar_src, force=False, notify=lambda: print('recompiling ' + grammar_src)): print('\nErrors while recompiling "%s":' % grammar_src + '\n--------------------------------------\n\n') with open('LaTeX_ebnf_ERRORS.txt', encoding='utf-8') as f: print(f.read()) sys.exit(1) recompile_grammar(os.path.join(scriptpath, 'LaTeX.ebnf'), force=False) from LaTeXCompiler import compile_src from DHParser.server import Server from DHParser.lsp import gen_lsp_table config_filename = get_config_filename() try: with open(config_filename, 'w') as f: f.write(host + ' ' + str(port)) except PermissionError: print('PermissionError: Could not write temporary config file: ' + config_filename) print('Starting server on %s:%i' % (host, port)) LaTeX_lsp = LaTeXLanguageServerProtocol() lsp_table = gen_lsp_table(LaTeX_lsp, prefix='lsp_') lsp_table.update({'default': compile_src}) non_blocking = frozenset(('initialize', 'initialized', 'shutdown', 'exit')) LaTeX_server = Server(rpc_functions=lsp_table, cpu_bound=set(lsp_table.keys() - non_blocking), blocking=frozenset()) if log_path is not None: LaTeX_server.echo_log = True print(LaTeX_server.start_logging(log_path)) try: LaTeX_server.run_tcp_server( host, port) # returns only after server has stopped except OSError as e: print(e) print('Could not start server. Shutting down!') return 1 except KeyboardInterrupt: print('Keyboard interrupt received.') return 1 finally: print('Server at host: {} port: {} has been stopped.'.format( host, port)) cfg_filename = get_config_filename() try: os.remove(cfg_filename) print('Removing temporary config file: "{}".'.format(cfg_filename)) except FileNotFoundError: print('Config file "{}" does not exist any more.'.format( cfg_filename)) return 0