Skip to content

Commit 7808b57

Browse files
authored
Merge pull request #644 from Systems-Modeling/ST6RI-836
ST6RI-836 Add option that allows changing the API basePath in a running Jupyter kernel
2 parents f2dfa92 + e46defe commit 7808b57

6 files changed

Lines changed: 108 additions & 7 deletions

File tree

org.omg.sysml.interactive/src/org/omg/sysml/interactive/SysMLInteractive.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ public void setApiBasePath(String apiBasePath) {
160160
this.apiBasePath = apiBasePath;
161161
}
162162

163+
public String getApiBasePath() {
164+
return apiBasePath;
165+
}
166+
163167
public int next(String extension) {
164168
this.resource = this.createResource(counter + extension);
165169
this.addInputResource(this.resource);
@@ -275,6 +279,25 @@ public String help(String command) {
275279
help(command, Collections.emptyList());
276280
}
277281

282+
public String repo(String apiBasePath, List<String> help) {
283+
this.counter++;
284+
if (!help.isEmpty()) {
285+
return SysMLInteractiveHelp.getApiBasePathHelp();
286+
}
287+
288+
if (!Strings.isNullOrEmpty(apiBasePath)) {
289+
setApiBasePath(apiBasePath);
290+
}
291+
292+
return getApiBasePath() + "\n";
293+
}
294+
295+
public String repo(String command) {
296+
return "-h".equals(command)?
297+
repo(null, Collections.singletonList("true")):
298+
repo(command, Collections.emptyList());
299+
}
300+
278301
public String eval(String input, String targetName, List<String> help) {
279302
if (Strings.isNullOrEmpty(input)) {
280303
this.counter++;
@@ -466,7 +489,7 @@ protected String publish(String name) {
466489
* @return output of the command
467490
*/
468491
public String load(Map<String, String> parameters) {
469-
counter++;
492+
this.counter++;
470493

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

506+
System.out.println("API base path: " + apiBasePath);
507+
System.out.println();
508+
483509
final ProjectRepository repository = new ProjectRepository(apiBasePath);
484510
final RemoteProject project;
485511

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

521-
System.out.println("API base path: " + apiBasePath);
522-
System.out.println();
523547
System.out.println("Selected branch " + branch.getName() + " (" + branch.getRemoteId().toString() + ")");
524548

549+
Revision headRevision = branch.getHeadRevision();
550+
if (!headRevision.isRemote()) {
551+
return "ERROR:Branch has no head commit\n";
552+
}
553+
525554
if (!tracker.isLibraryTracked()) {
526555
System.out.println("Caching library UUIDs...");
527556
tracker.trackLibraryUUIDs(getLibraryResources());
@@ -533,9 +562,6 @@ private String load(RemoteBranch branch) {
533562
tracker.trackLocalUUIDs(inputResources);
534563

535564
System.out.println("Downloading model...");
536-
537-
RemoteProject remoteProject = branch.getRemoteProject();
538-
Revision headRevision = branch.getHeadRevision();
539565
APIModel model = headRevision.fetchRemote();
540566

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

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

@@ -764,6 +791,8 @@ public void run() {
764791
}
765792
} else if ("%projects".equals(command)) {
766793
System.out.print(this.projects(argument));
794+
} else if ("%repo".equals(command)) {
795+
System.out.print(this.repo(argument));
767796
} else if ("%publish".equals(command)) {
768797
if (!"".equals(argument)) {
769798
System.out.print(this.publish(argument));

org.omg.sysml.interactive/src/org/omg/sysml/interactive/SysMLInteractiveHelp.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class SysMLInteractiveHelp {
4343
+ "%help\t\tGet a list of available commands or help on a specific command\n"
4444
+ "%list\t\tList loaded library packages or the results of a given query\n"
4545
+ "%load\t\tLoad a model from the repository\n"
46+
+ "%repo\t Set the api base path for the repository\n"
4647
+ "%show\t\tPrint the abstract syntax tree rooted in a named element\n"
4748
+ "%projects\tList projects in the repository\n"
4849
+ "%publish\tPublish to the repository the modele elements rooted in a named element\n"
@@ -139,6 +140,14 @@ public class SysMLInteractiveHelp {
139140
"Usage: %projects\n\n"
140141
+ "Print the name and identifier of all projects in the repository.\n";
141142

143+
private static final String API_BASE_PATH_HELP_STRING =
144+
"Usage: %repo [<BASE PATH>]\n\n"
145+
+ "If <BASE PATH> is not given, print the current repository base path.\r\n"
146+
+ "If <BASE PATH> is given, set the repository base path.\r\n"
147+
+ "\r\n"
148+
+ "<BASE PATH> is a URL giving the API base path for the repository access by the %projects, %publish and %load commands. \r\n"
149+
+ "For example: https://my.domain.com/sysml_repo";
150+
142151
public static String getGeneralHelp() {
143152
return GENERAL_HELP_STRING;
144153
}
@@ -183,6 +192,10 @@ public static String getLoadHelp() {
183192
return LOAD_HELP_STRING;
184193
}
185194

195+
public static String getApiBasePathHelp() {
196+
return API_BASE_PATH_HELP_STRING;
197+
}
198+
186199
private static Map<String, String> commandHelpMap = createCommandHelpMap();
187200

188201
private static Map<String, String> createCommandHelpMap() {
@@ -197,6 +210,7 @@ private static Map<String, String> createCommandHelpMap() {
197210
map.put("%export", EXPORT_HELP_STRING);
198211
map.put("%load", LOAD_HELP_STRING);
199212
map.put("%projects", PROJECTS_HELP_STRING);
213+
map.put("%repo", API_BASE_PATH_HELP_STRING);
200214

201215
return map;
202216
}

org.omg.sysml.jupyter.kernel/src/main/java/org/omg/sysml/jupyter/kernel/SysMLKernel.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public SysMLKernel() {
8282
this.magics.registerMagics(Export.class);
8383
this.magics.registerMagics(Projects.class);
8484
this.magics.registerMagics(Load.class);
85+
this.magics.registerMagics(Repo.class);
8586

8687
this.magicParser = new MyMagicParser();
8788
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* SysML 2 Pilot Implementation
3+
* Copyright (C) 2025 Model Driven Solutions, Inc.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*
18+
* @license LGPL-3.0-or-later <http://spdx.org/licenses/LGPL-3.0-or-later>
19+
*
20+
* Contributors:
21+
* Laszlo Gati, MDS
22+
*/
23+
package org.omg.sysml.jupyter.kernel.magic;
24+
25+
import java.util.List;
26+
import java.util.Map;
27+
28+
import org.omg.sysml.interactive.SysMLInteractive;
29+
import org.omg.sysml.jupyter.kernel.ISysML;
30+
31+
import io.github.spencerpark.jupyter.kernel.magic.registry.LineMagic;
32+
import io.github.spencerpark.jupyter.kernel.magic.registry.MagicsArgs;
33+
34+
public class Repo {
35+
36+
private static final MagicsArgs REPO_ARGS = MagicsArgs.builder().onlyKnownKeywords().onlyKnownFlags()
37+
.optional("basePath")
38+
.flag("help", 'h', "true")
39+
.build();
40+
41+
@LineMagic("repo")
42+
public static String repo(List<String> args) {
43+
Map<String, List<String>> vals = REPO_ARGS.parse(args);
44+
List<String> basePaths = vals.get("basePath");
45+
List<String> help = vals.get("help");
46+
String basePath = basePaths.isEmpty()? null: basePaths.get(0);
47+
48+
SysMLInteractive interactive = ISysML.getKernelInstance().getInteractive();
49+
return interactive.repo(basePath, help);
50+
}
51+
}

org.omg.sysml.jupyter.kernel/src/main/java/org/omg/sysml/jupyter/kernel/magic/View.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public static DisplayData view(List<String> args) {
6363
break;
6464
case TEXT:
6565
dd.putText(vr.getText());
66+
default:
67+
break;
6668
}
6769
return dd;
6870
}

org.omg.sysml/src/org/omg/sysml/util/repository/ProjectRepository.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,11 @@ public RemoteProject getPRojectById(UUID projectId) {
185185
Project projectById = getProjectApi().getProjectById(projectId);
186186
return projectById == null? null: new RemoteProject(this, projectId, projectById.getName());
187187
} catch (ApiException e) {
188-
throw new RemoteException("Error occured while trying to query the project", e);
188+
if (e.getCode() == 404) {
189+
return null;
190+
} else {
191+
throw new RemoteException("Error occured while trying to query the project", e);
192+
}
189193
}
190194
}
191195

0 commit comments

Comments
 (0)