Skip to content

Commit e716b08

Browse files
authored
Merge pull request #17 from ebresie/nbPythonCodeIntegration
Merging updates associated with Python Discovery functionality - #3 Python Execution functionality - #4
2 parents b74107d + a012d36 commit e716b08

49 files changed

Lines changed: 7361 additions & 43 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/target/
2+
/nbproject/

SECURITY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Security Policy
22

33

4-
Python4NB is in early development so limited sercurity support is available at
4+
Python4NB is in early development so limited security support is available at
55
the moment.
66

77
The Python4NB project leverages github workflow dependabot checks

pom.xml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>org.apache.netbeans.modules.python4nb</groupId>
55
<artifactId>python</artifactId>
6-
<version>0.1-SNAPSHOT</version>
6+
<version>0.2-SNAPSHOT</version>
77
<packaging>nbm</packaging>
88
<build>
99
<plugins>
@@ -34,6 +34,11 @@
3434
</archive>
3535
</configuration>
3636
</plugin>
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-surefire-plugin</artifactId>
40+
<version>2.15</version>
41+
</plugin>
3742
</plugins>
3843
</build>
3944
<dependencies>
@@ -82,7 +87,7 @@
8287
<groupId>org.netbeans.api</groupId>
8388
<artifactId>org-openide-windows</artifactId>
8489
<version>RELEASE126</version>
85-
<type>jar</type>
90+
<!--<type>jar</type>-->
8691
<scope>provided</scope>
8792
</dependency>
8893
<dependency>
@@ -304,6 +309,17 @@
304309
<version>3.0.2</version>
305310
<scope>provided</scope>
306311
</dependency>
312+
<dependency>
313+
<groupId>org.netbeans.api</groupId>
314+
<artifactId>org-netbeans-modules-nbjunit</artifactId>
315+
<version>RELEASE126</version>
316+
<scope>test</scope>
317+
</dependency>
318+
<dependency>
319+
<groupId>org.netbeans.api</groupId>
320+
<artifactId>org-netbeans-modules-project-ant</artifactId>
321+
<version>RELEASE126</version>
322+
</dependency>
307323
</dependencies>
308324
<properties>
309325
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2022 ebres.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.netbeans.modules.python4nb.api;
17+
18+
/**
19+
*
20+
* @author ebres
21+
*/
22+
public class PythonException extends Exception {
23+
24+
public PythonException(Throwable arg0) {
25+
super(arg0);
26+
}
27+
28+
public PythonException(String arg0, Throwable arg1) {
29+
super(arg0, arg1);
30+
}
31+
32+
public PythonException(String arg0) {
33+
super(arg0);
34+
}
35+
36+
public PythonException() {
37+
}
38+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* To change this template, choose Tools | Templates
3+
* and open the template in the editor.
4+
*/
5+
6+
package org.apache.netbeans.modules.python4nb.api;
7+
8+
import java.io.IOException;
9+
//import org.netbeans.api.extexecution.input.InputProcessor;
10+
import org.netbeans.api.extexecution.base.input.InputProcessor;
11+
12+
/** TODO: This code is derived from nbPython codebase.
13+
* Need to consider proper handling of code.
14+
*/
15+
public class PythonOutputProcessor implements InputProcessor {
16+
StringBuilder builder = new StringBuilder();
17+
@Override
18+
public void processInput(char[] input) throws IOException {
19+
builder.append(input);
20+
}
21+
22+
@Override
23+
public void reset() throws IOException {
24+
//builder = new StringBuilder();
25+
}
26+
27+
@Override
28+
public void close() throws IOException {
29+
30+
}
31+
public String getData(){
32+
return builder.toString();
33+
}
34+
35+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* To change this template, choose Tools | Templates
3+
* and open the template in the editor.
4+
*/
5+
6+
/* TODO: This is based on nbPython code and needs to be reconciled as
7+
applicable.
8+
*/
9+
package org.apache.netbeans.modules.python4nb.api;
10+
11+
import java.util.List;
12+
import javax.swing.AbstractListModel;
13+
//import org.netbeans.modules.python.api.PythonPlatform;
14+
//import org.netbeans.modules.python.api.PythonPlatformManager;
15+
import org.apache.netbeans.modules.python4nb.platform.PythonPlatform;
16+
import org.apache.netbeans.modules.python4nb.platform.PythonPlatformManager;
17+
18+
public class PythonPlatformListModel extends AbstractListModel {
19+
private PythonPlatformManager manager = PythonPlatformManager.getInstance();
20+
private List<PythonPlatform> model = manager.getPlatforms();
21+
22+
@Override
23+
public int getSize() {
24+
return model.size();
25+
}
26+
27+
@Override
28+
public Object getElementAt(int index) {
29+
if (index >= 0 && index < model.size()) {
30+
return model.get(index);
31+
} else {
32+
return null;
33+
}
34+
}
35+
36+
public void refresh(){
37+
model = manager.getPlatforms();
38+
fireContentsChanged(this, 0, model.size() -1);
39+
}
40+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* Copyright 2022 ebres.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.apache.netbeans.modules.python4nb.api;
18+
19+
import java.io.ByteArrayOutputStream;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.Map;
25+
import java.util.prefs.Preferences;
26+
import org.apache.netbeans.modules.python4nb.platform.PythonPlatformManager;
27+
import org.openide.filesystems.FileObject;
28+
import org.openide.filesystems.FileUtil;
29+
import org.openide.util.NbPreferences;
30+
31+
public final class Util {
32+
private static final String USE_PROXY_AUTHENTICATION = "useProxyAuthentication"; // NOI18N
33+
private static final String PROXY_AUTHENTICATION_USERNAME = "proxyAuthenticationUsername"; // NOI18N
34+
private static final String PROXY_AUTHENTICATION_PASSWORD = "proxyAuthenticationPassword"; // NOI18N
35+
36+
private Util() {
37+
throw new IllegalStateException("Utility class");
38+
}
39+
40+
public static String readAsString(final InputStream is) throws IOException {
41+
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
42+
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
43+
FileUtil.copy(is, baos);
44+
return baos.toString("UTF-8"); // NOI18N
45+
} finally {
46+
is.close();
47+
}
48+
}
49+
public static void adjustProxy(final ProcessBuilder pb) {
50+
String proxy = Util.getNetBeansHttpProxy();
51+
if (proxy != null) {
52+
Map<String, String> env = pb.environment();
53+
if ((env.get("HTTP_PROXY") == null) && (env.get("http_proxy") == null)) { // NOI18N
54+
env.put("HTTP_PROXY", proxy); // NOI18N
55+
env.put("http_proxy", proxy); // NOI18N
56+
}
57+
// PENDING - what if proxy was null so the user has TURNED off
58+
// proxies while there is still an environment variable set - should
59+
// we honor their environment, or honor their NetBeans proxy
60+
// settings (e.g. unset HTTP_PROXY in the environment before
61+
// launching plugin?
62+
}
63+
}
64+
65+
/**
66+
* FIXME: get rid of the whole method as soon as some NB Proxy API is
67+
* available.
68+
*/
69+
private static String getNetBeansHttpProxy() {
70+
String host = System.getProperty("http.proxyHost"); // NOI18N
71+
72+
if (host == null) {
73+
return null;
74+
}
75+
76+
String portHttp = System.getProperty("http.proxyPort"); // NOI18N
77+
int port;
78+
79+
try {
80+
port = Integer.parseInt(portHttp);
81+
} catch (NumberFormatException e) {
82+
port = 8080;
83+
}
84+
85+
Preferences prefs = NbPreferences.root().node("org/netbeans/core"); // NOI18N
86+
boolean useAuth = prefs.getBoolean(USE_PROXY_AUTHENTICATION, false);
87+
String auth = "";
88+
if (useAuth) {
89+
auth = prefs.get(PROXY_AUTHENTICATION_USERNAME, "") + ":" + prefs.get(PROXY_AUTHENTICATION_PASSWORD, "") + '@'; // NOI18N
90+
}
91+
92+
// Gem requires "http://" in front of the port name if it's not already there
93+
if (host.indexOf(':') == -1) {
94+
host = "http://" + auth + host; // NOI18N
95+
}
96+
97+
return host + ":" + port; // NOI18N
98+
}
99+
100+
private static final String FIRST_TIME_KEY = "platform-manager-called-first-time"; // NOI18N
101+
102+
public static Preferences getPythonPreferences() {
103+
return NbPreferences.forModule(PythonPlatformManager.class);
104+
}
105+
106+
public static boolean isFirstPlatformTouch() {
107+
return getPythonPreferences().getBoolean(FIRST_TIME_KEY, true);
108+
}
109+
110+
public static void setFirstPlatformTouch(boolean b) {
111+
getPythonPreferences().putBoolean(FIRST_TIME_KEY, b);
112+
}
113+
114+
// Tested in PythonUtilsTest
115+
public static List<FileObject> findUniqueRoots(List<FileObject> originalRoots) {
116+
List<FileObject> roots = new ArrayList<>(originalRoots);
117+
int n = roots.size();
118+
if (n > 1) {
119+
for (int i = 0; i < n; i++) {
120+
FileObject f1 = roots.get(i);
121+
if (f1 == null) {
122+
continue;
123+
}
124+
for (int j = 0; j < n; j++) {
125+
if (i == j) {
126+
continue;
127+
}
128+
FileObject f2 = roots.get(j);
129+
if (f1 == f2 || f2 == null) {
130+
continue;
131+
}
132+
if (FileUtil.isParentOf(f1, f2)) {
133+
roots.set(j, null);
134+
}
135+
}
136+
}
137+
}
138+
139+
List<FileObject> uniqueRoots = new ArrayList<>();
140+
for (int i = 0; i < n; i++) {
141+
FileObject fo = roots.get(i);
142+
if (fo != null) {
143+
uniqueRoots.add(fo);
144+
}
145+
}
146+
147+
return uniqueRoots;
148+
}
149+
}
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
/*
2-
* Licensed to the Apache Software Foundation (ASF) under one
3-
* or more contributor license agreements. See the NOTICE file
4-
* distributed with this work for additional information
5-
* regarding copyright ownership. The ASF licenses this file
6-
* to you under the Apache License, Version 2.0 (the
7-
* "License"); you may not use this file except in compliance
8-
* with the License. You may obtain a copy of the License at
2+
* Copyright 2022 Eric Bresie and friends. All rights reserved.
93
*
10-
* http://www.apache.org/licenses/LICENSE-2.0
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
117
*
12-
* Unless required by applicable law or agreed to in writing,
13-
* software distributed under the License is distributed on an
14-
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15-
* KIND, either express or implied. See the License for the
16-
* specific language governing permissions and limitations
17-
* under the License.
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
1815
*/
1916
package org.apache.netbeans.modules.python4nb.editor.file;
2017

@@ -35,4 +32,7 @@ private MIMETypes() {
3532
public static final String PY_EXT = "py";
3633
public static final String PYC_EXT = "pyc";
3734
public static final String PYO_EXT = "pyo";
35+
36+
public static final String PYTHON_MIME_TYPE = "text/x-python"; // NOI18N
37+
public static final String PYTHON_EXTENSION ="py";
3838
}

0 commit comments

Comments
 (0)