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

Merged
merged 12 commits into from
May 22, 2025
Merged
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 @@ -160,6 +160,10 @@ public void setApiBasePath(String apiBasePath) {
this.apiBasePath = apiBasePath;
}

public String getApiBasePath() {
return apiBasePath;
}

public int next(String extension) {
this.resource = this.createResource(counter + extension);
this.addInputResource(this.resource);
Expand Down Expand Up @@ -275,6 +279,25 @@ public String help(String command) {
help(command, Collections.emptyList());
}

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

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

return getApiBasePath() + "\n";
}

public String repo(String command) {
return "-h".equals(command)?
repo(null, Collections.singletonList("true")):
repo(command, Collections.emptyList());
}

public String eval(String input, String targetName, List<String> help) {
if (Strings.isNullOrEmpty(input)) {
this.counter++;
Expand Down Expand Up @@ -466,7 +489,7 @@ protected String publish(String name) {
* @return output of the command
*/
public String load(Map<String, String> parameters) {
counter++;
this.counter++;

if (parameters.containsKey(HELP_KEY)) {
return SysMLInteractiveHelp.getLoadHelp();
Expand All @@ -480,6 +503,9 @@ public String load(Map<String, String> parameters) {
return "ERROR:Branch name and id cannot be provided at the same time\n";
}

System.out.println("API base path: " + apiBasePath);
System.out.println();

final ProjectRepository repository = new ProjectRepository(apiBasePath);
final RemoteProject project;

Expand Down Expand Up @@ -518,10 +544,13 @@ private String load(RemoteBranch branch) {
return "ERROR:Branch doesn't exist\n";
}

System.out.println("API base path: " + apiBasePath);
System.out.println();
System.out.println("Selected branch " + branch.getName() + " (" + branch.getRemoteId().toString() + ")");

Revision headRevision = branch.getHeadRevision();
if (!headRevision.isRemote()) {
return "ERROR:Branch has no head commit\n";
}

if (!tracker.isLibraryTracked()) {
System.out.println("Caching library UUIDs...");
tracker.trackLibraryUUIDs(getLibraryResources());
Expand All @@ -533,9 +562,6 @@ private String load(RemoteBranch branch) {
tracker.trackLocalUUIDs(inputResources);

System.out.println("Downloading model...");

RemoteProject remoteProject = branch.getRemoteProject();
Revision headRevision = branch.getHeadRevision();
APIModel model = headRevision.fetchRemote();

EMFModelRefresher modelRefresher = new EMFModelRefresher(model, tracker);
Expand All @@ -549,6 +575,7 @@ private String load(RemoteBranch branch) {
addResourceToIndex(resource);
});

RemoteProject remoteProject = branch.getRemoteProject();
return "Loaded Project " + remoteProject.getProjectName() + " (" + remoteProject.getRemoteId().toString() + ")\n";
}

Expand Down Expand Up @@ -764,6 +791,8 @@ public void run() {
}
} else if ("%projects".equals(command)) {
System.out.print(this.projects(argument));
} else if ("%repo".equals(command)) {
System.out.print(this.repo(argument));
} else if ("%publish".equals(command)) {
if (!"".equals(argument)) {
System.out.print(this.publish(argument));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class SysMLInteractiveHelp {
+ "%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"
+ "%load\t\tLoad a model from the repository\n"
+ "%repo\t Set the api base path for the repository\n"
+ "%show\t\tPrint the abstract syntax tree rooted in a named element\n"
+ "%projects\tList projects in the repository\n"
+ "%publish\tPublish to the repository the modele elements rooted in a named element\n"
Expand Down Expand Up @@ -139,6 +140,14 @@ public class SysMLInteractiveHelp {
"Usage: %projects\n\n"
+ "Print the name and identifier of all projects in the repository.\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 @@ -183,6 +192,10 @@ public static String getLoadHelp() {
return LOAD_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 @@ -197,6 +210,7 @@ private static Map<String, String> createCommandHelpMap() {
map.put("%export", EXPORT_HELP_STRING);
map.put("%load", LOAD_HELP_STRING);
map.put("%projects", PROJECTS_HELP_STRING);
map.put("%repo", API_BASE_PATH_HELP_STRING);

return map;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public SysMLKernel() {
this.magics.registerMagics(Export.class);
this.magics.registerMagics(Projects.class);
this.magics.registerMagics(Load.class);
this.magics.registerMagics(Repo.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 Repo {

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

@LineMagic("repo")
public static String repo(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.repo(basePath, help);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public static DisplayData view(List<String> args) {
break;
case TEXT:
dd.putText(vr.getText());
default:
break;
}
return dd;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ public RemoteProject getPRojectById(UUID projectId) {
Project projectById = getProjectApi().getProjectById(projectId);
return projectById == null? null: new RemoteProject(this, projectId, projectById.getName());
} catch (ApiException e) {
throw new RemoteException("Error occured while trying to query the project", e);
if (e.getCode() == 404) {
return null;
} else {
throw new RemoteException("Error occured while trying to query the project", e);
}
}
}

Expand Down