Skip to content
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

Nb python code integration #17

Merged
merged 3 commits into from
Jun 4, 2022
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target/
/nbproject/
2 changes: 1 addition & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Security Policy


Python4NB is in early development so limited sercurity support is available at
Python4NB is in early development so limited security support is available at
the moment.

The Python4NB project leverages github workflow dependabot checks
Expand Down
20 changes: 18 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.netbeans.modules.python4nb</groupId>
<artifactId>python</artifactId>
<version>0.1-SNAPSHOT</version>
<version>0.2-SNAPSHOT</version>
<packaging>nbm</packaging>
<build>
<plugins>
Expand Down Expand Up @@ -34,6 +34,11 @@
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
</plugin>
</plugins>
</build>
<dependencies>
Expand Down Expand Up @@ -82,7 +87,7 @@
<groupId>org.netbeans.api</groupId>
<artifactId>org-openide-windows</artifactId>
<version>RELEASE126</version>
<type>jar</type>
<!--<type>jar</type>-->
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -304,6 +309,17 @@
<version>3.0.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-nbjunit</artifactId>
<version>RELEASE126</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-project-ant</artifactId>
<version>RELEASE126</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2022 ebres.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.netbeans.modules.python4nb.api;

/**
*
* @author ebres
*/
public class PythonException extends Exception {

public PythonException(Throwable arg0) {
super(arg0);
}

public PythonException(String arg0, Throwable arg1) {
super(arg0, arg1);
}

public PythonException(String arg0) {
super(arg0);
}

public PythonException() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package org.apache.netbeans.modules.python4nb.api;

import java.io.IOException;
//import org.netbeans.api.extexecution.input.InputProcessor;
import org.netbeans.api.extexecution.base.input.InputProcessor;

/** TODO: This code is derived from nbPython codebase.
* Need to consider proper handling of code.
*/
public class PythonOutputProcessor implements InputProcessor {
StringBuilder builder = new StringBuilder();
@Override
public void processInput(char[] input) throws IOException {
builder.append(input);
}

@Override
public void reset() throws IOException {
//builder = new StringBuilder();
}

@Override
public void close() throws IOException {

}
public String getData(){
return builder.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/* TODO: This is based on nbPython code and needs to be reconciled as
applicable.
*/
package org.apache.netbeans.modules.python4nb.api;

import java.util.List;
import javax.swing.AbstractListModel;
//import org.netbeans.modules.python.api.PythonPlatform;
//import org.netbeans.modules.python.api.PythonPlatformManager;
import org.apache.netbeans.modules.python4nb.platform.PythonPlatform;
import org.apache.netbeans.modules.python4nb.platform.PythonPlatformManager;

public class PythonPlatformListModel extends AbstractListModel {
private PythonPlatformManager manager = PythonPlatformManager.getInstance();
private List<PythonPlatform> model = manager.getPlatforms();

@Override
public int getSize() {
return model.size();
}

@Override
public Object getElementAt(int index) {
if (index >= 0 && index < model.size()) {
return model.get(index);
} else {
return null;
}
}

public void refresh(){
model = manager.getPlatforms();
fireContentsChanged(this, 0, model.size() -1);
}
}
149 changes: 149 additions & 0 deletions src/main/java/org/apache/netbeans/modules/python4nb/api/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright 2022 ebres.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.netbeans.modules.python4nb.api;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.prefs.Preferences;
import org.apache.netbeans.modules.python4nb.platform.PythonPlatformManager;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.NbPreferences;

public final class Util {
private static final String USE_PROXY_AUTHENTICATION = "useProxyAuthentication"; // NOI18N
private static final String PROXY_AUTHENTICATION_USERNAME = "proxyAuthenticationUsername"; // NOI18N
private static final String PROXY_AUTHENTICATION_PASSWORD = "proxyAuthenticationPassword"; // NOI18N

private Util() {
throw new IllegalStateException("Utility class");
}

public static String readAsString(final InputStream is) throws IOException {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileUtil.copy(is, baos);
return baos.toString("UTF-8"); // NOI18N
} finally {
is.close();
}
}
public static void adjustProxy(final ProcessBuilder pb) {
String proxy = Util.getNetBeansHttpProxy();
if (proxy != null) {
Map<String, String> env = pb.environment();
if ((env.get("HTTP_PROXY") == null) && (env.get("http_proxy") == null)) { // NOI18N
env.put("HTTP_PROXY", proxy); // NOI18N
env.put("http_proxy", proxy); // NOI18N
}
// PENDING - what if proxy was null so the user has TURNED off
// proxies while there is still an environment variable set - should
// we honor their environment, or honor their NetBeans proxy
// settings (e.g. unset HTTP_PROXY in the environment before
// launching plugin?
}
}

/**
* FIXME: get rid of the whole method as soon as some NB Proxy API is
* available.
*/
private static String getNetBeansHttpProxy() {
String host = System.getProperty("http.proxyHost"); // NOI18N

if (host == null) {
return null;
}

String portHttp = System.getProperty("http.proxyPort"); // NOI18N
int port;

try {
port = Integer.parseInt(portHttp);
} catch (NumberFormatException e) {
port = 8080;
}

Preferences prefs = NbPreferences.root().node("org/netbeans/core"); // NOI18N
boolean useAuth = prefs.getBoolean(USE_PROXY_AUTHENTICATION, false);
String auth = "";
if (useAuth) {
auth = prefs.get(PROXY_AUTHENTICATION_USERNAME, "") + ":" + prefs.get(PROXY_AUTHENTICATION_PASSWORD, "") + '@'; // NOI18N
}

// Gem requires "http://" in front of the port name if it's not already there
if (host.indexOf(':') == -1) {
host = "http://" + auth + host; // NOI18N
}

return host + ":" + port; // NOI18N
}

private static final String FIRST_TIME_KEY = "platform-manager-called-first-time"; // NOI18N

public static Preferences getPythonPreferences() {
return NbPreferences.forModule(PythonPlatformManager.class);
}

public static boolean isFirstPlatformTouch() {
return getPythonPreferences().getBoolean(FIRST_TIME_KEY, true);
}

public static void setFirstPlatformTouch(boolean b) {
getPythonPreferences().putBoolean(FIRST_TIME_KEY, b);
}

// Tested in PythonUtilsTest
public static List<FileObject> findUniqueRoots(List<FileObject> originalRoots) {
List<FileObject> roots = new ArrayList<>(originalRoots);
int n = roots.size();
if (n > 1) {
for (int i = 0; i < n; i++) {
FileObject f1 = roots.get(i);
if (f1 == null) {
continue;
}
for (int j = 0; j < n; j++) {
if (i == j) {
continue;
}
FileObject f2 = roots.get(j);
if (f1 == f2 || f2 == null) {
continue;
}
if (FileUtil.isParentOf(f1, f2)) {
roots.set(j, null);
}
}
}
}

List<FileObject> uniqueRoots = new ArrayList<>();
for (int i = 0; i < n; i++) {
FileObject fo = roots.get(i);
if (fo != null) {
uniqueRoots.add(fo);
}
}

return uniqueRoots;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* Copyright 2022 Eric Bresie and friends. All rights reserved.
*
* http://www.apache.org/licenses/LICENSE-2.0
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.netbeans.modules.python4nb.editor.file;

Expand All @@ -35,4 +32,7 @@ private MIMETypes() {
public static final String PY_EXT = "py";
public static final String PYC_EXT = "pyc";
public static final String PYO_EXT = "pyo";

public static final String PYTHON_MIME_TYPE = "text/x-python"; // NOI18N
public static final String PYTHON_EXTENSION ="py";
}
Loading