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 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -32,18 +32,20 @@
import java.util.Map;

import org.omg.sysml.plantuml.SysML2PlantUMLStyle;
import org.omg.sysml.util.traversal.facade.impl.ApiElementProcessingFacade;

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"
+ "%repo\t Set the api base path for the repository"
+ "%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"
+ "%list\t\tList loaded library packages or the results of a given query\n"
+ "%show\t\tPrint the abstract syntax tree rooted in a named element\n"
+ "%publish\tPublish to the repository the modele elements rooted in a named element\n"
+ "%publish\tPublish to the repository the model elements rooted in a named element\n"
+ "%view\t\tRender the view specified by the named view usage\n"
+ "%viz\t\tVisualize the name model elements\n";

Expand Down Expand Up @@ -115,6 +117,14 @@ 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: %repo [<BASE PATH>]\n\n"
+ "If <BASE PATH> is not given, print the current repository base path.\r\n"
+ "If <BASE PATH> is given, set the repository base path.\r\n"
+ "\r\n"
+ "<BASE PATH> is a URL giving the API base path for the repository access by the %projects, %publish and %load commands. \r\n"
+ "For example: https://my.domain.com/sysml_repo";

public static String getGeneralHelp() {
return GENERAL_HELP_STRING;
Expand Down Expand Up @@ -152,6 +162,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 @@ -163,12 +177,12 @@ private static Map<String, String> createCommandHelpMap() {
map.put("%publish", PUBLISH_HELP_STRING);
map.put("%viz", VIZ_HELP_STRING);
map.put("%view", VIEW_HELP_STRING);
map.put("%export", EXPORT_HELP_STRING);
map.put("%export", EXPORT_HELP_STRING);
map.put("%repo", API_BASE_PATH_HELP_STRING);
return map;
}

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,51 @@
/**
* 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 REPO_ARGS = MagicsArgs.builder().onlyKnownKeywords().onlyKnownFlags()
.optional("basePath")
.flag("help", 'h', "true")
.build();

@LineMagic("repo")
public static String apiBasePath(List<String> args) {
Map<String, List<String>> vals = REPO_ARGS.parse(args);
List<String> basePaths = vals.get("basePath");
List<String> help = vals.get("help");
String basePath = basePaths.isEmpty()? null: basePaths.get(0);

SysMLInteractive interactive = ISysML.getKernelInstance().getInteractive();
return interactive.apiBasePath(basePath, help);
}
}