Mercurial > hg > Members > aka > jupyter_CbC_kernel
changeset 54:7857f8eea835
Add args magic to provide cli-args to user program
author | Ben Spoor <ben.spoor@altran.com> |
---|---|
date | Thu, 13 Apr 2017 08:09:16 +0000 |
parents | 82b0505f1156 |
children | 4a03cede4763 |
files | jupyter_c_kernel/kernel.py resources/master.c |
diffstat | 2 files changed, 13 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/jupyter_c_kernel/kernel.py Wed Apr 12 20:59:16 2017 +0200 +++ b/jupyter_c_kernel/kernel.py Thu Apr 13 08:09:16 2017 +0000 @@ -2,6 +2,7 @@ from threading import Thread from ipykernel.kernelbase import Kernel +import re import subprocess import tempfile import os @@ -118,7 +119,9 @@ def _filter_magics(self, code): - magics = {'cflags': [], 'ldflags': []} + magics = {'cflags': [], + 'ldflags': [], + 'args': []} for line in code.splitlines(): if line.startswith('//%'): @@ -128,6 +131,10 @@ if key in ['ldflags', 'cflags']: for flag in value.split(): magics[key] += [flag] + elif key == "args": + # Split arguments respecting quotes + for argument in re.findall(r'(?:[^\s,"]|"(?:\\.|[^"])*")+', value): + magics['args'] += [argument.strip('"')] return magics @@ -151,7 +158,7 @@ return {'status': 'ok', 'execution_count': self.execution_count, 'payload': [], 'user_expressions': {}} - p = self.create_jupyter_subprocess([self.master_path, binary_file.name]) + p = self.create_jupyter_subprocess([self.master_path, binary_file.name] + magics['args']) while p.poll() is None: p.write_contents() p.write_contents()
--- a/resources/master.c Wed Apr 12 20:59:16 2017 +0200 +++ b/resources/master.c Thu Apr 13 08:09:16 2017 +0000 @@ -25,5 +25,7 @@ fprintf(stderr, "%s: %s\n", argv[0], error); return EXIT_FAILURE; } - return usermain(argc, argv, envp); -} \ No newline at end of file + + /* Call Users main, but make master.c invisible by removing first argument */ + return usermain(argc-1, argv+1, envp); +}