Skip to content

ST6RI-836 Add option that allows changing the API basePath in a running Jupyter kernel #644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: ST6RI-178
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ public void setApiBasePath(String apiBasePath) {
this.apiBasePath = apiBasePath;
}

public String getApiBasePath() {
return apiBasePath;
}

public int next() {
this.resource = (XtextResource)this.createResource(counter + SYSML_EXTENSION);
this.addInputResource(this.resource);
Expand Down Expand Up @@ -249,6 +253,18 @@ public String help(String command) {
help(command, Collections.emptyList());
}

public String apiBasePath(String apiBasePath, List<String> help) {
if (!help.isEmpty()) {
return SysMLInteractiveHelp.getApiBasePathHelp();
}

if (!Strings.isNullOrEmpty(apiBasePath)) {
setApiBasePath(apiBasePath);
}

return getApiBasePath();
}

public String eval(String input, String targetName, List<String> help) {
if (Strings.isNullOrEmpty(input)) {
this.counter++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class SysMLInteractiveHelp {
private static final String GENERAL_HELP_STRING =
"The following SysML v2 magic commands are available.\n"
+ "For help on a specific command, use \"%help <COMMAND>\" or \"%<cmd> -h\".\n\n"
+ "%api-base-path\t Sets and prints the current api base path"
+ "%eval\t\tEvaluate a given expression.\n"
+ "%export\t\tSave a file of the JSON representation of the abstract syntax tree rooted in the named element.\n"
+ "%help\t\tGet a list of available commands or help on a specific command\n"
Expand Down Expand Up @@ -115,6 +116,11 @@ public class SysMLInteractiveHelp {
"Usage: %export <NAME>\n\n"
+ "Save a file containing the complete JSON representation of the abstract syntax tree rooted in <NAME>.\n"
+ "<NAME> must be fully qualified.\n";

private static final String API_BASE_PATH_HELP_STRING =
"Usage: %api-base-path [<BASE PATH>]\n\n"
+ "Sets the current api base path\n"
+ "If no argument is passed it prints the current api base path";

public static String getGeneralHelp() {
return GENERAL_HELP_STRING;
Expand Down Expand Up @@ -152,6 +158,10 @@ public static String getExportHelp() {
return EXPORT_HELP_STRING;
}

public static String getApiBasePathHelp() {
return API_BASE_PATH_HELP_STRING;
}

private static Map<String, String> commandHelpMap = createCommandHelpMap();

private static Map<String, String> createCommandHelpMap() {
Expand All @@ -170,5 +180,4 @@ private static Map<String, String> createCommandHelpMap() {
public static String getHelpString(String command) {
return commandHelpMap.get(command);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public SysMLKernel() {
this.magics.registerMagics(Viz.class);
this.magics.registerMagics(View.class);
this.magics.registerMagics(Export.class);
this.magics.registerMagics(ApiBasePath.class);

this.magicParser = new MyMagicParser();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* SysML 2 Pilot Implementation
* Copyright (C) 2025 Model Driven Solutions, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license LGPL-3.0-or-later <http://spdx.org/licenses/LGPL-3.0-or-later>
*
* Contributors:
* Laszlo Gati, MDS
*/
package org.omg.sysml.jupyter.kernel.magic;

import java.util.List;
import java.util.Map;

import org.omg.sysml.interactive.SysMLInteractive;
import org.omg.sysml.jupyter.kernel.ISysML;

import io.github.spencerpark.jupyter.kernel.magic.registry.LineMagic;
import io.github.spencerpark.jupyter.kernel.magic.registry.MagicsArgs;

public class ApiBasePath {

private static final MagicsArgs SHOW_ARGS = MagicsArgs.builder().onlyKnownKeywords().onlyKnownFlags()
.optional("basePath")
.flag("help", 'h', "true")
.build();

@LineMagic("api-base-path")
public static String apiBasePath(List<String> args) {
Map<String, List<String>> vals = SHOW_ARGS.parse(args);
List<String> basePaths = vals.get("basePath");
String basePath = basePaths.isEmpty()? null: basePaths.get(0);

SysMLInteractive interactive = ISysML.getKernelInstance().getInteractive();

if (basePath != null) {
interactive.setApiBasePath(basePath);
}

return "Api base path is: " + ISysML.getKernelInstance().getInteractive().getApiBasePath();
}
}