Merge branch 'master' of ssh://git.dcc.fc.up.pt/yap-6.3
This commit is contained in:
commit
a5c09f9187
77
C/atomic.c
77
C/atomic.c
@ -661,26 +661,31 @@ static Int atom_concat3(USES_REGS1) {
|
||||
Term t1;
|
||||
Term t2, t3, ot;
|
||||
Atom at;
|
||||
bool g1, g2, g3;
|
||||
restart_aux:
|
||||
t1 = Deref(ARG1);
|
||||
t2 = Deref(ARG2);
|
||||
t3 = Deref(ARG3);
|
||||
if (Yap_IsGroundTerm(t1) && Yap_IsGroundTerm(t2)) {
|
||||
g1 = Yap_IsGroundTerm(t1);
|
||||
g2 = Yap_IsGroundTerm(t2);
|
||||
g3 = Yap_IsGroundTerm(t3);
|
||||
if (g1 && g2) {
|
||||
at = Yap_ConcatAtoms(t1, t2 PASS_REGS);
|
||||
ot = ARG3;
|
||||
} else if (Yap_IsGroundTerm(t1) && Yap_IsGroundTerm(t3)) {
|
||||
at = Yap_SubtractHeadAtom(Deref(ARG3), t1 PASS_REGS);
|
||||
} else if (g1 && g3) {
|
||||
at = Yap_SubtractHeadAtom(t3, t1 PASS_REGS);
|
||||
ot = ARG2;
|
||||
} else if (Yap_IsGroundTerm(t2) && Yap_IsGroundTerm(t3)) {
|
||||
at = Yap_SubtractTailAtom(Deref(ARG3), t2 PASS_REGS);
|
||||
} else if (g2 && g3) {
|
||||
at = Yap_SubtractTailAtom(t3, t2 PASS_REGS);
|
||||
ot = ARG1;
|
||||
} else if (Yap_IsGroundTerm(t3)) {
|
||||
} else if (g3) {
|
||||
EXTRA_CBACK_ARG(3, 1) = MkIntTerm(0);
|
||||
EXTRA_CBACK_ARG(3, 2) = MkIntTerm(Yap_AtomToLength(t3 PASS_REGS));
|
||||
return cont_atom_concat3(PASS_REGS1);
|
||||
} else {
|
||||
LOCAL_Error_TYPE = INSTANTIATION_ERROR;
|
||||
LOCAL_Error_Term = t1;
|
||||
at = NULL;
|
||||
}
|
||||
if (at) {
|
||||
if (Yap_unify(ot, MkAtomTerm(at)))
|
||||
@ -693,7 +698,7 @@ restart_aux:
|
||||
if (Yap_HandleError("atom_concat/3")) {
|
||||
goto restart_aux;
|
||||
} else {
|
||||
return FALSE;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
cut_fail();
|
||||
@ -751,29 +756,34 @@ static Int atomic_concat3(USES_REGS1) {
|
||||
Term t1;
|
||||
Term t2, t3, ot;
|
||||
Atom at = NULL;
|
||||
bool g1, g2, g3;
|
||||
restart_aux:
|
||||
t1 = Deref(ARG1);
|
||||
t2 = Deref(ARG2);
|
||||
t3 = Deref(ARG3);
|
||||
if (Yap_IsGroundTerm(t1) && Yap_IsGroundTerm(t2)) {
|
||||
at = Yap_ConcatAtoms(CastToAtom(t1), CastToAtom(t2) PASS_REGS);
|
||||
g1 = Yap_IsGroundTerm(t1);
|
||||
g2 = Yap_IsGroundTerm(t2);
|
||||
g3 = Yap_IsGroundTerm(t3);
|
||||
if (g1 && g2) {
|
||||
at = Yap_ConcatAtomics(t1, t2 PASS_REGS);
|
||||
ot = ARG3;
|
||||
} else if (Yap_IsGroundTerm(t1) && Yap_IsGroundTerm(t3)) {
|
||||
at = Yap_SubtractHeadAtom(t3, CastToAtom(t1) PASS_REGS);
|
||||
} else if (g1 && g3) {
|
||||
at = Yap_SubtractHeadAtom(t3, t1 PASS_REGS);
|
||||
ot = ARG2;
|
||||
} else if (Yap_IsGroundTerm(t2) && Yap_IsGroundTerm(t3)) {
|
||||
at = Yap_SubtractTailAtom(t3, CastToAtom(t2) PASS_REGS);
|
||||
ot = ARG1;
|
||||
} else if (Yap_IsGroundTerm(t3)) {
|
||||
} else if (g2 && g3) {
|
||||
at = Yap_SubtractTailAtom(t3, t2 PASS_REGS);
|
||||
ot = ARG1;
|
||||
} else if (g3) {
|
||||
EXTRA_CBACK_ARG(3, 1) = MkIntTerm(0);
|
||||
EXTRA_CBACK_ARG(3, 2) = MkIntTerm(Yap_AtomicToLength(t3 PASS_REGS));
|
||||
return cont_atomic_concat3(PASS_REGS1);
|
||||
} else {
|
||||
LOCAL_Error_TYPE = INSTANTIATION_ERROR;
|
||||
LOCAL_Error_Term = t1;
|
||||
LOCAL_Error_TYPE = INSTANTIATION_ERROR;
|
||||
LOCAL_Error_Term = t1;
|
||||
at = NULL;
|
||||
}
|
||||
if (at) {
|
||||
if (Yap_unify(ot, CastToNumeric(at)))
|
||||
if (Yap_unify(ot, MkAtomTerm(at)))
|
||||
cut_succeed();
|
||||
else
|
||||
cut_fail();
|
||||
@ -783,7 +793,7 @@ restart_aux:
|
||||
if (Yap_HandleError("atomic_concat/3")) {
|
||||
goto restart_aux;
|
||||
} else {
|
||||
return FALSE;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
cut_fail();
|
||||
@ -822,40 +832,45 @@ static Int string_concat3(USES_REGS1) {
|
||||
Term t1;
|
||||
Term t2, t3, ot;
|
||||
Term tf = 0;
|
||||
bool g1, g2, g3;
|
||||
Atom at;
|
||||
restart_aux:
|
||||
t1 = Deref(ARG1);
|
||||
t2 = Deref(ARG2);
|
||||
t3 = Deref(ARG3);
|
||||
if (Yap_IsGroundTerm(t1) && Yap_IsGroundTerm(t2)) {
|
||||
g1 = Yap_IsGroundTerm(t1);
|
||||
g2 = Yap_IsGroundTerm(t2);
|
||||
g3 = Yap_IsGroundTerm(t3);
|
||||
if (g1 && g2) {
|
||||
tf = Yap_ConcatStrings(t1, t2 PASS_REGS);
|
||||
ot = ARG3;
|
||||
} else if (Yap_IsGroundTerm(t1) && Yap_IsGroundTerm(t3)) {
|
||||
} else if (g1 && g3) {
|
||||
tf = Yap_SubtractHeadString(t3, t1 PASS_REGS);
|
||||
ot = ARG2;
|
||||
} else if (Yap_IsGroundTerm(t2) && Yap_IsGroundTerm(t3)) {
|
||||
} else if (g2 && g3) {
|
||||
tf = Yap_SubtractTailString(t3, t2 PASS_REGS);
|
||||
ot = ARG1;
|
||||
} else if (Yap_IsGroundTerm(t3)) {
|
||||
ot = ARG1;
|
||||
} else if (g3) {
|
||||
EXTRA_CBACK_ARG(3, 1) = MkIntTerm(0);
|
||||
EXTRA_CBACK_ARG(3, 2) = MkIntTerm(Yap_StringToLength(t3 PASS_REGS));
|
||||
return cont_string_concat3(PASS_REGS1);
|
||||
} else {
|
||||
LOCAL_Error_TYPE = INSTANTIATION_ERROR;
|
||||
LOCAL_Error_Term = t1;
|
||||
LOCAL_Error_TYPE = INSTANTIATION_ERROR;
|
||||
LOCAL_Error_Term = t1;
|
||||
at = NULL;
|
||||
}
|
||||
if (tf) {
|
||||
if (Yap_unify(ot, tf)) {
|
||||
if (Yap_unify(ot, tf))
|
||||
cut_succeed();
|
||||
} else {
|
||||
else
|
||||
cut_fail();
|
||||
}
|
||||
}
|
||||
/* Error handling */
|
||||
if (LOCAL_Error_TYPE) {
|
||||
if (Yap_HandleError("string_concat/3")) {
|
||||
if (Yap_HandleError("atom_concat/3")) {
|
||||
goto restart_aux;
|
||||
} else {
|
||||
return FALSE;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
cut_fail();
|
||||
|
13
C/text.c
13
C/text.c
@ -104,6 +104,7 @@ static Int SkipListCodes(unsigned char **bufp, Term *l, Term **tailp,
|
||||
*bufp = st = st0;
|
||||
|
||||
if (*l == TermNil) {
|
||||
st[0] = '\0';
|
||||
return 0;
|
||||
}
|
||||
if (IsPairTerm(*l)) {
|
||||
@ -311,7 +312,7 @@ unsigned char *Yap_readText(seq_tv_t *inp, size_t *lengp) {
|
||||
} else if (!IsStringTerm(inp->val.t) &&
|
||||
inp->type == YAP_STRING_STRING) {
|
||||
LOCAL_Error_TYPE = TYPE_ERROR_STRING;
|
||||
} else if (!IsPairTerm(inp->val.t) && !IsStringTerm(inp->val.t) &&
|
||||
} else if (!IsPairOrNilTerm(inp->val.t) && !IsStringTerm(inp->val.t) &&
|
||||
inp->type ==
|
||||
(YAP_STRING_ATOMS_CODES | YAP_STRING_STRING)) {
|
||||
LOCAL_Error_TYPE = TYPE_ERROR_LIST;
|
||||
@ -344,20 +345,20 @@ unsigned char *Yap_readText(seq_tv_t *inp, size_t *lengp) {
|
||||
return UStringOfTerm(inp->val.t);
|
||||
}
|
||||
if (((inp->type & (YAP_STRING_CODES | YAP_STRING_ATOMS)) ==
|
||||
(YAP_STRING_CODES | YAP_STRING_ATOMS)) && IsPairTerm(inp->val.t)) {
|
||||
(YAP_STRING_CODES | YAP_STRING_ATOMS)) && IsPairOrNilTerm(inp->val.t)) {
|
||||
//Yap_DebugPlWriteln(inp->val.t);
|
||||
return inp->val.uc =
|
||||
Yap_ListToBuffer(s0, inp->val.t, inp, &wide, lengp
|
||||
PASS_REGS);
|
||||
// this is a term, extract to a sfer, and representation is wide
|
||||
}
|
||||
if (inp->type & YAP_STRING_CODES && IsPairTerm(inp->val.t)) {
|
||||
if (inp->type & YAP_STRING_CODES && IsPairOrNilTerm(inp->val.t)) {
|
||||
//Yap_DebugPlWriteln(inp->val.t);
|
||||
return inp->val.uc = Yap_ListOfCodesToBuffer(s0, inp->val.t, inp, &wide,
|
||||
lengp PASS_REGS);
|
||||
// this is a term, extract to a sfer, and representation is wide
|
||||
}
|
||||
if (inp->type & YAP_STRING_ATOMS && IsPairTerm(inp->val.t)) {
|
||||
if (inp->type & YAP_STRING_ATOMS && IsPairOrNilTerm(inp->val.t)) {
|
||||
//Yap_DebugPlWriteln(inp->val.t);
|
||||
return inp->val.uc = Yap_ListOfAtomsToBuffer(s0, inp->val.t, inp, &wide,
|
||||
lengp PASS_REGS);
|
||||
@ -1031,7 +1032,7 @@ const char *Yap_TextTermToText(Term t, char *buf, size_t len, encoding_t enc) {
|
||||
CACHE_REGS
|
||||
seq_tv_t inp, out;
|
||||
inp.val.t = t;
|
||||
if (IsAtomTerm(t)) {
|
||||
if (IsAtomTerm(t) && t != TermNil) {
|
||||
inp.type = YAP_STRING_ATOM;
|
||||
if (IsWideAtom(AtomOfTerm(t)))
|
||||
inp.enc = ENC_WCHAR;
|
||||
@ -1040,7 +1041,7 @@ const char *Yap_TextTermToText(Term t, char *buf, size_t len, encoding_t enc) {
|
||||
} else if (IsStringTerm(t)) {
|
||||
inp.type = YAP_STRING_STRING;
|
||||
inp.enc = ENC_ISO_UTF8;
|
||||
} else if (IsPairTerm(t)) {
|
||||
} else if (IsPairOrNilTerm(t)) {
|
||||
inp.type = (YAP_STRING_CODES | YAP_STRING_ATOMS);
|
||||
} else {
|
||||
Yap_Error(TYPE_ERROR_TEXT, t, NULL);
|
||||
|
@ -1,2 +0,0 @@
|
||||
* Threads Support <http://www.gnu.org/software/threads>
|
||||
GNU Threads Library (or similar)
|
@ -1,2 +0,0 @@
|
||||
* GNU libgmp (in some cases MPIR - GNU big integers and rationals
|
||||
* libreadline - Readline line editing library
|
10
H/YapTags.h
10
H/YapTags.h
@ -350,4 +350,14 @@ INLINE_ONLY inline EXTERN void *AddressOfTerm(Term t) {
|
||||
return (void *)(IsIntTerm(t) ? IntOfTerm(t) : LongIntOfTerm(t));
|
||||
}
|
||||
|
||||
|
||||
INLINE_ONLY inline EXTERN Int IsPairTermOrNil (Term);
|
||||
|
||||
INLINE_ONLY inline EXTERN Int
|
||||
IsPairOrNilTerm (Term t)
|
||||
{
|
||||
return IsPairTerm(t) || t == TermNil;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -1,72 +0,0 @@
|
||||
|
||||
## YAPDroid
|
||||
|
||||
This file documents the YAPDroid application. This app was developed in order to
|
||||
test the YAP-Android interface. It provides a simple REPL with a query window and a text
|
||||
viewer.
|
||||
|
||||
### Design
|
||||
|
||||
The YAP interface to Android is based on the SWIG interface generator. SWIG exports the YAP
|
||||
C++ classes as Java classes. In practice there exist two worlds, the native application
|
||||
and the Java application:
|
||||
|
||||
- YAP runs in Android as a native application in a Linux
|
||||
environment. Android does not support glibc. Instead, Android libraries are provided by the Android NDK, and
|
||||
are somewhat limited, i.e., the NDK misses in-memory streams and a `glob`
|
||||
predicate. Moreover, read-only data is kept in a zipped archive that is made to appear as
|
||||
a directory, `~/assets`
|
||||
|
||||
- SWIG generates the glue code: JNI classes that act as Java classes. Callbacks are possible.
|
||||
|
||||
- Java code operates as usual.
|
||||
|
||||
### Compiling YAPDroid
|
||||
|
||||
The current version was compiled using the recent Android Studio `cmake` support. Android Studio uses
|
||||
`gradle` as the build system. `gradle` orchestrates compilation of all Java code. External code is delegated
|
||||
to `cmake` or the Android NDK own builder. The idea fits nicely with YAP; unfortunately, the process currently
|
||||
crashes in middle.
|
||||
|
||||
To install, navigate through these steps:
|
||||
1. Obtain `gmp` for Android. YAP has used the [Rupan repo](https://github.com/Rupan/gmp); place the repo next to
|
||||
the yap-6.3 top directory.
|
||||
|
||||
2. Install [swig](www/swig.org)
|
||||
|
||||
2. Obtain Android Studio. This work used Android Studio 2.2 Beta 3. The IDE includes most everything else you need: the ADK, the NDK, `cmake`, the `ninja` build system, and the debugger.
|
||||
|
||||
3. Set YAPDroid as your directory.
|
||||
|
||||
4. Adapt the `build.gradle` files to your configuration.
|
||||
+ It may be a good idea to first generate an empty configuration and compare.
|
||||
+ In the `lib` directory, please verify wich targets you are generating for.
|
||||
|
||||
4. Build the system, either from the GUI or from a terminal, say as:
|
||||
~~~~~
|
||||
./gradlew assembleDebug
|
||||
~~~~~
|
||||
or
|
||||
~~~~~
|
||||
./gradlew :lib:assembleDebug
|
||||
~~~~~
|
||||
You should see `cmake` being executed, and after some time `gradle` crash.
|
||||
|
||||
5. To actually install the binaries, use:
|
||||
~~~~~
|
||||
cd lib/build/intermediates/cmake/debug/json/armeabi-v7a
|
||||
ninja install
|
||||
~~~~~
|
||||
6. Comment the four lines in `lib/build.gradle` to avoid crashing in the `cmake` task.
|
||||
|
||||
7. use the GUI commands to compile the app or set your directory bak to
|
||||
the YAPDroid top-directory and call `gradle`.
|
||||
|
||||
Enjoy!
|
||||
|
||||
### Limitations and TODO
|
||||
|
||||
- improve error handling.
|
||||
- support `/assets`: the code is written but not tested yet.
|
||||
- network access
|
||||
- sqlite testing and support
|
@ -1,37 +0,0 @@
|
||||
apply plugin: 'com.android.application'
|
||||
|
||||
android {
|
||||
compileSdkVersion 23
|
||||
buildToolsVersion "23.0.3"
|
||||
defaultConfig {
|
||||
applicationId "pt.up.yap.app"
|
||||
minSdkVersion 19
|
||||
targetSdkVersion 23
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
project.gradle.taskGraph.whenReady {
|
||||
connectedDebugAndroidTest {
|
||||
ignoreFailures = true
|
||||
}
|
||||
}
|
||||
dependencies {
|
||||
compile fileTree(dir: 'libs', include: ['*.jar'])
|
||||
compile 'com.android.support:appcompat-v7:23.4.0'
|
||||
testCompile 'junit:junit:4.12'
|
||||
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
|
||||
androidTestCompile 'com.android.support.test:runner:0.5'
|
||||
androidTestCompile 'com.android.support:support-annotations:23.4.0'
|
||||
compile 'com.google.android.gms:play-services-appindexing:8.4.0'
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="pt.up.yap.app"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0">
|
||||
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher"
|
||||
android:theme="@style/AppTheme"
|
||||
>
|
||||
<activity android:name=".YAPDroid"
|
||||
android:label="@string/app_name">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
|
||||
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
|
||||
<meta-data
|
||||
android:name="com.google.android.gms.version"
|
||||
android:value="@integer/google_play_services_version" />
|
||||
</application>
|
||||
</manifest>
|
@ -1,86 +0,0 @@
|
||||
package pt.up.yap.app;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetManager;
|
||||
|
||||
import android.support.v4.content.res.ResourcesCompat;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
|
||||
/**
|
||||
* Created by vsc on 30/05/16.
|
||||
*/
|
||||
|
||||
public class CreateFiles {
|
||||
|
||||
|
||||
|
||||
public static void setupfiles(Context context, AssetManager assets){
|
||||
try {
|
||||
|
||||
context.getExternalFilesDir("Yap").mkdirs();
|
||||
context.getExternalFilesDir("Yap/pl").mkdirs();
|
||||
context.getExternalFilesDir("Yap/os").mkdirs();
|
||||
String list[] = {};
|
||||
list= assets.list("Yap");
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
copy(context, "Yap/"+ list[i]);
|
||||
}
|
||||
list = null;
|
||||
list= assets.list("Yap/pl");
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
copy(context, "Yap/pl/"+ list[i]);
|
||||
}
|
||||
list =null;
|
||||
list= assets.list("Yap/os");
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
copy(context, "Yap/os/"+ list[i]);
|
||||
}
|
||||
Log.d("CreateFiles", context.getFilesDir().getAbsolutePath());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void copy(Context context, String originalName) {
|
||||
try {
|
||||
if (originalName.contains(".")) {
|
||||
File outFile;
|
||||
|
||||
outFile = new File(context.getExternalFilesDir(null), originalName );
|
||||
|
||||
InputStream in = context.getAssets().open(originalName);
|
||||
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));
|
||||
|
||||
|
||||
byte data[] = new byte[1024];
|
||||
int count;
|
||||
|
||||
while ((count = in.read(data)) != -1) {
|
||||
out.write(data, 0, count);
|
||||
}
|
||||
|
||||
out.flush();
|
||||
out.close();
|
||||
in.close();
|
||||
|
||||
in = null;
|
||||
out = null;
|
||||
//Log.d("Copied: ", originalName + " to " + destinationName);
|
||||
//System.out.println("Copied: "+ originalName + " to " + destinationName);
|
||||
//System.out.println();
|
||||
}
|
||||
}catch(IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,330 +0,0 @@
|
||||
package pt.up.yap.app;
|
||||
/****
|
||||
* using sqlite
|
||||
* For example,the following:
|
||||
* <p/>
|
||||
* import android.database.sqlite.SQLiteDatabase;
|
||||
* <p/>
|
||||
* should be replaced with:
|
||||
* <p/>
|
||||
* import org.sqlite.database.sqlite.SQLiteDatabase;
|
||||
* <p/>
|
||||
* As well as replacing all uses of the classes in the android.database.sqlite.* namespace, the application must also be sure to use the following two:
|
||||
* <p/>
|
||||
* org.sqlite.database.SQLException
|
||||
* org.sqlite.database.DatabaseErrorHandler
|
||||
* <p/>
|
||||
* instead of:
|
||||
* <p/>
|
||||
* android.database.SQLException
|
||||
* android.database.DatabaseErrorHandler
|
||||
* <p/>
|
||||
* Aside from namespace changes, there are other differences from the stock Android interface that applications need to be aware of:
|
||||
* <p/>
|
||||
* The SQLiteStatement.simpleQueryForBlobFileDescriptor() API is not available. The collation sequence "UNICODE" is not available. The collation sequence "LOCALIZED", which normally changes with the system's current locale, is always equivalent to SQLite's built in collation BINARY.
|
||||
****/
|
||||
|
||||
import android.content.Context;
|
||||
import android.app.Activity;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.content.res.AssetManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import android.text.method.ScrollingMovementMethod;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ScrollView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.google.android.gms.appindexing.Action;
|
||||
import com.google.android.gms.appindexing.AppIndex;
|
||||
import com.google.android.gms.appindexing.Thing;
|
||||
import com.google.android.gms.common.api.GoogleApiClient;
|
||||
|
||||
import org.sqlite.database.DatabaseErrorHandler;
|
||||
import org.sqlite.database.sqlite.SQLiteDatabase;
|
||||
|
||||
import pt.up.yap.lib.*;
|
||||
|
||||
class DoNotDeleteErrorHandler implements DatabaseErrorHandler {
|
||||
private static final String TAG = "DoNotDeleteErrorHandler";
|
||||
|
||||
public void onCorruption(SQLiteDatabase dbObj) {
|
||||
Log.e(TAG, "Corruption reported by sqlite on database: " + dbObj.getPath());
|
||||
}
|
||||
}
|
||||
|
||||
public class YAPDroid extends Activity {
|
||||
|
||||
private static final String TAG = "YAPDroid";
|
||||
|
||||
TextView outputText = null;
|
||||
ScrollView scroller = null;
|
||||
YAPEngine eng = null;
|
||||
EditText text;
|
||||
String str;
|
||||
String buf;
|
||||
YAPQuery q;
|
||||
Boolean running = false, compute = true;
|
||||
int i = 1;
|
||||
YAPListTerm vs0;
|
||||
private AssetManager mgr;
|
||||
/**
|
||||
* ATTENTION: This was auto-generated to implement the App Indexing API.
|
||||
* See https://g.co/AppIndexing/AndroidStudio for more information.
|
||||
*/
|
||||
private GoogleApiClient client;
|
||||
|
||||
// private static native void load(AssetManager mgr);
|
||||
|
||||
|
||||
void runQuery(String str, Boolean more) {
|
||||
try {
|
||||
// check if at initial query
|
||||
if (running) {
|
||||
if (q != null) q.close();
|
||||
}
|
||||
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.i(TAG, "query " + str);
|
||||
}
|
||||
|
||||
q = eng.query(str);
|
||||
// get the uninstantiated query variables.
|
||||
vs0 = q.namedVars();
|
||||
running = true;
|
||||
// start computing
|
||||
compute = true;
|
||||
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.i(TAG, "onQueryButtonClick called");
|
||||
}
|
||||
|
||||
Boolean rc = true;
|
||||
|
||||
text.setText("?- ");
|
||||
if (vs0.nil()) {
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.i(TAG, "q0=\n");
|
||||
}
|
||||
if (compute && (rc = q.next())) {
|
||||
outputText.append("yes\n");
|
||||
running = compute = more;
|
||||
} else {
|
||||
outputText.append("no\n");
|
||||
running = false;
|
||||
compute = false;
|
||||
}
|
||||
} else {
|
||||
// if (BuildConfig.DEBUG) {
|
||||
// Log.i(TAG, "q1= " + vs0.text() + "\n");
|
||||
// }
|
||||
while (compute && (rc = q.next())) {
|
||||
YAPListTerm vs = q.namedVars();
|
||||
while (!vs.nil()) {
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.i(TAG, "q= " + vs.text() + "\n");
|
||||
}
|
||||
YAPTerm eq = vs.car();
|
||||
//outputText.append(Integr.toString(i) + ": " + eq.text() );
|
||||
outputText.append(Integer.toString(i));
|
||||
outputText.append(":\t" + eq.getArg(1).text() + " = " + eq.getArg(2).text() + "\n");
|
||||
vs = vs.cdr();
|
||||
}
|
||||
compute = more;
|
||||
}
|
||||
}
|
||||
if (!rc) {
|
||||
outputText.append("no\n");
|
||||
if (q != null)
|
||||
q.close();
|
||||
q = null;
|
||||
compute = false;
|
||||
running = false;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
outputText.append("Exception thrown :" + e);
|
||||
if (q != null)
|
||||
q.close();
|
||||
compute = true;
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the activity is first created.
|
||||
*/
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
String s = null;
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.main);
|
||||
|
||||
try {
|
||||
PackageManager
|
||||
m = getPackageManager();
|
||||
s = getPackageName();
|
||||
PackageInfo p = m.getPackageInfo(s, 0);
|
||||
//s = p.applicationInfo.dataDir;
|
||||
mgr = this.getAssets();
|
||||
|
||||
/** static constructor */
|
||||
// follow this order carefully.
|
||||
System.loadLibrary("gmp");
|
||||
System.loadLibrary("Yap");
|
||||
System.loadLibrary("Yapsqlite3");
|
||||
System.loadLibrary("Yap++");
|
||||
System.loadLibrary("YAPDroid");
|
||||
CreateFiles.setupfiles(this, mgr);
|
||||
|
||||
} catch (NameNotFoundException e) {
|
||||
Log.e(TAG, "Couldn't find package information in PackageManager", e);
|
||||
}
|
||||
Log.i(TAG, "mgr=" + mgr);
|
||||
|
||||
text = (EditText) findViewById(R.id.EditText01);
|
||||
outputText = (TextView) findViewById(R.id.OutputText);
|
||||
outputText.setText("Application " + s + "\nPress 'First' or 'All' to query...\n");
|
||||
outputText.setMovementMethod(new ScrollingMovementMethod());
|
||||
scroller = (ScrollView) findViewById(R.id.Scroller);
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.i(TAG, "window making done");
|
||||
}
|
||||
eng = new YAPEngine(null, this.getExternalFilesDir("/Yap/pl/boot.yap").getAbsolutePath());
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.i(TAG, "engine done");
|
||||
}
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.i(TAG, "onClearButtonClick called");
|
||||
}
|
||||
JavaCallback callback = new JavaCallback(outputText);
|
||||
// set the Java Callback
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.i(TAG, "before setting callback");
|
||||
}
|
||||
eng.setYAPCallback(callback);
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.i(TAG, "callback done");
|
||||
}
|
||||
// ATTENTION: This was auto-generated to implement the App Indexing API.
|
||||
// See https://g.co/AppIndexing/AndroidStudio for more information.
|
||||
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
|
||||
}
|
||||
|
||||
public void onClearButtonClick(View view) {
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.i(TAG, "onClearButtonClick called");
|
||||
}
|
||||
// Ensure scroll to end of text
|
||||
scroller.post(new Runnable() {
|
||||
public void run() {
|
||||
scroller.fullScroll(ScrollView.FOCUS_DOWN);
|
||||
if (running) {
|
||||
if (q != null)
|
||||
q.close();
|
||||
q = null;
|
||||
}
|
||||
running = false;
|
||||
text.setText("");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void onFirstButtonClick(View view) {
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.i(TAG, "onQueryButtonClick called");
|
||||
}
|
||||
// Ensure scroll to end of text
|
||||
scroller.post(new Runnable() {
|
||||
public void run() {
|
||||
scroller.fullScroll(ScrollView.FOCUS_DOWN);
|
||||
str = text.getText().toString();
|
||||
//outputText.append("?- " + str+"\n\n");
|
||||
Log.i(TAG, "onQueryAnyButtonClick " + str + "\n");
|
||||
runQuery(str, false);
|
||||
scroller.fullScroll(ScrollView.FOCUS_DOWN);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void onAllButtonClick(View view) {
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.i(TAG, "onQueryButtonClick called");
|
||||
}
|
||||
// Ensure scroll to end of text
|
||||
scroller.post(new Runnable() {
|
||||
public void run() {
|
||||
str = text.getText().toString();
|
||||
scroller.fullScroll(ScrollView.FOCUS_DOWN);
|
||||
outputText.append("?- " + str + "\n\n");
|
||||
Log.i(TAG, "onAllButtonClick " + str + "\n");
|
||||
runQuery(str, true);
|
||||
scroller.fullScroll(ScrollView.FOCUS_DOWN);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* ATTENTION: This was auto-generated to implement the App Indexing API.
|
||||
* See https://g.co/AppIndexing/AndroidStudio for more information.
|
||||
*/
|
||||
public Action getIndexApiAction() {
|
||||
Thing object = new Thing.Builder()
|
||||
.setName("YAPDroid Page") // TODO: Define a title for the content shown.
|
||||
// TODO: Make sure this auto-generated URL is correct.
|
||||
.setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
|
||||
.build();
|
||||
return new Action.Builder(Action.TYPE_VIEW)
|
||||
.setObject(object)
|
||||
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
|
||||
// ATTENTION: This was auto-generated to implement the App Indexing API.
|
||||
// See https://g.co/AppIndexing/AndroidStudio for more information.
|
||||
client.connect();
|
||||
AppIndex.AppIndexApi.start(client, getIndexApiAction());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
|
||||
// ATTENTION: This was auto-generated to implement the App Indexing API.
|
||||
// See https://g.co/AppIndexing/AndroidStudio for more information.
|
||||
AppIndex.AppIndexApi.end(client, getIndexApiAction());
|
||||
client.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
class JavaCallback extends YAPCallback {
|
||||
private static final String TAG = "JavaCallback";
|
||||
TextView output;
|
||||
|
||||
public JavaCallback(TextView outputText) {
|
||||
super();
|
||||
output = outputText;
|
||||
Log.i(TAG, "java callback init");
|
||||
}
|
||||
|
||||
public void run(String s) {
|
||||
Log.i(TAG, "java callback ");
|
||||
output.append(s);
|
||||
}
|
||||
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
android:orientation="vertical"
|
||||
tools:context=".YAPDroid">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/EditText01"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1.0"
|
||||
android:lines="6" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="fill_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<Button
|
||||
android:id="@+id/FirstButton"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:onClick="onFirstButtonClick"
|
||||
android:text="First..." />
|
||||
|
||||
<Button
|
||||
android:id="@+id/AllButton"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:onClick="onAllButtonClick"
|
||||
android:text="All..." />
|
||||
|
||||
<Button
|
||||
android:id="@+id/ClearButton"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:onClick="onClearButtonClick"
|
||||
android:text="Reset" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
|
||||
<ScrollView
|
||||
android:id="@+id/Scroller"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/OutputText"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:singleLine="false" />
|
||||
<!-- Set MaxLegth EditText -->
|
||||
</ScrollView>
|
||||
</LinearLayout>
|
@ -1,23 +0,0 @@
|
||||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:2.2.0-alpha7'
|
||||
|
||||
// NOTE: Do not place your application dependencies here; they belong
|
||||
// in the individual module build.gradle files
|
||||
}
|
||||
}
|
||||
|
||||
allprojects {
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
}
|
||||
|
||||
task clean(type: Delete) {
|
||||
delete rootProject.buildDir
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
apply plugin: 'com.android.application'
|
||||
|
||||
android {
|
||||
compileSdkVersion 23
|
||||
buildToolsVersion "23.0.3"
|
||||
defaultConfig {
|
||||
applicationId "pt.up.yap.app"
|
||||
minSdkVersion 19
|
||||
targetSdkVersion 23
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
cmake {
|
||||
// cFlags "-DTEST_C_FLAG1" "-DTEST_C_FLAG2"
|
||||
// cppFlags "-DTEST_CPP_FLAG2" "-DTEST_CPP_FLAG2"
|
||||
abiFilters "armeabi-v7a", "x86"
|
||||
}
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
// externalNativeBuild {
|
||||
// cmake {
|
||||
// path '../../CMakeLists.txt'
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile fileTree(dir: 'libs', include: ['*.jar'])
|
||||
compile 'com.android.support:appcompat-v7:23.4.0'
|
||||
testCompile 'junit:junit:4.12'
|
||||
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
|
||||
androidTestCompile 'com.android.support.test:runner:0.5'
|
||||
androidTestCompile 'com.android.support:support-annotations:23.4.0'
|
||||
}
|
@ -1 +0,0 @@
|
||||
include ':app', ':lib'
|
@ -316,9 +316,9 @@ static YAP_Bool rename_file(void) {
|
||||
static YAP_Bool read_link(void) {
|
||||
char *s1 = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
|
||||
#if HAVE_READLINK
|
||||
char buf[PATH_MAX + 1];
|
||||
char buf[MAXPATHLEN + 1];
|
||||
|
||||
if (readlink(s1, buf, PATH_MAX) < 0)
|
||||
if (readlink(s1, buf, MAXPATHLEN) < 0)
|
||||
return false;
|
||||
|
||||
|
||||
|
@ -5,7 +5,35 @@ main :-
|
||||
Plt = matplotlib.pyplot,
|
||||
:= import( Plt ),
|
||||
:= (
|
||||
Plt.figure(figsize=(10,2.5)),
|
||||
Plt.plot([1,2,3,4]),
|
||||
Plt.ylabel(`some numbers`),
|
||||
Plt.show()
|
||||
).
|
||||
|
||||
|
||||
main2 :-
|
||||
:= ( import( numpy),
|
||||
import( matplotlib.mlab),
|
||||
import( matplotlib.pyplot) ),
|
||||
NP = numpy,
|
||||
Mlab = matplotlib.mlab,
|
||||
Plt = matplotlib.pyplot,
|
||||
|
||||
% example data
|
||||
mu := 100, % mean of distribution,
|
||||
sigma := 15, % standard deviation of distribution,
|
||||
x := mu + sigma * NP.random.randn(10000),
|
||||
|
||||
num_bins := 50,
|
||||
% the histogram of the data
|
||||
(n, bins, patches) := Plt.hist(x, num_bins, normed=1, facecolor= `green`, alpha=0.5),
|
||||
% add a `best fit` line
|
||||
y := Mlab.normpdf(bins, mu, sigma),
|
||||
:= (Plt.plot(bins, y, `r--`),
|
||||
Plt.xlabel(`Smarts`),
|
||||
Plt.ylabel(`Probability`),
|
||||
Plt.title(`Histogram of IQ: $\\mu=100$, $\\sigma=15$`),
|
||||
% Tweak spacing to prevent clipping of ylabel,
|
||||
Plt.subplots_adjust(left=0.15),
|
||||
Plt.show()).
|
||||
|
7
packages/python/pandas.yap
Normal file
7
packages/python/pandas.yap
Normal file
@ -0,0 +1,7 @@
|
||||
|
||||
:- use_module( library(python) ).
|
||||
|
||||
:- := import(pandas)).
|
||||
|
||||
pred2panda(Pred, Obj) :-
|
||||
|
@ -1,26 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "MetaKernel YAP 3",
|
||||
"language": "prolog",
|
||||
"name": "yap_kernel"
|
||||
},
|
||||
"widgets": {
|
||||
"state": {},
|
||||
"version": "1.1.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -1,86 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {
|
||||
"code_folding": [],
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"<ipywidgets.widgets.widget_selection.ToggleButtons object at 0x105e24da0>\n",
|
||||
"No (more) answers\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
":= import(mathplot).\n",
|
||||
"X := plot([1,2,3])"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": " YAP-6.3",
|
||||
"language": "prolog",
|
||||
"name": "yap_kernel"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "prolog",
|
||||
"version": 2
|
||||
},
|
||||
"file_extension": ".yap",
|
||||
"help_links": [
|
||||
{
|
||||
"text": "MetaKernel Magics",
|
||||
"url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md"
|
||||
}
|
||||
],
|
||||
"mimetype": "text/prolog",
|
||||
"name": "text",
|
||||
"pygments_lexer": "prolog",
|
||||
"version": "0.0.1"
|
||||
},
|
||||
"latex_envs": {
|
||||
"bibliofile": "biblio.bib",
|
||||
"cite_by": "apalike",
|
||||
"current_citInitial": 1,
|
||||
"eqLabelWithNumbers": true,
|
||||
"eqNumInitial": 0
|
||||
},
|
||||
"nav_menu": {},
|
||||
"toc": {
|
||||
"navigate_menu": true,
|
||||
"number_sections": true,
|
||||
"sideBar": true,
|
||||
"threshold": 6,
|
||||
"toc_cell": false,
|
||||
"toc_section_display": "block",
|
||||
"toc_window_display": false
|
||||
},
|
||||
"widgets": {
|
||||
"state": {
|
||||
"09b21d398da9424d94cf3c8e51054154": {
|
||||
"views": []
|
||||
},
|
||||
"369de2fecba34468a7c9cf64076bac60": {
|
||||
"views": []
|
||||
},
|
||||
"7f758a42f83c469882761606f7f8133e": {
|
||||
"views": []
|
||||
},
|
||||
"c028bb350ed6424eb48a02762907868d": {
|
||||
"views": []
|
||||
}
|
||||
},
|
||||
"version": "1.1.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
@ -1,3 +1,2 @@
|
||||
"""A Prolog kernel for Jupyter"""
|
||||
|
||||
__version__ = '0.0.1'
|
||||
from ._version import version_info, __version__, kernel_protocol_version_info, kernel_protocol_version
|
||||
from ipykernel.connect import *
|
||||
|
@ -1,6 +1,3 @@
|
||||
try:
|
||||
from ipykernel.kernelapp import IPKernelApp
|
||||
except ImportError:
|
||||
from IPython.kernel.zmq.kernelapp import IPKernelApp
|
||||
from .kernel import YAPKernel
|
||||
IPKernelApp.launch_instance(kernel_class=YAPKernel)
|
||||
if __name__ == '__main__':
|
||||
from yap_kernel import kernelapp as app
|
||||
app.launch_new_instance()
|
||||
|
5
packages/python/yap_kernel/_version.py
Normal file
5
packages/python/yap_kernel/_version.py
Normal file
@ -0,0 +1,5 @@
|
||||
version_info = (4, 4, 1)
|
||||
__version__ = '.'.join(map(str, version_info))
|
||||
|
||||
kernel_protocol_version_info = (5, 0)
|
||||
kernel_protocol_version = '%s.%s' % kernel_protocol_version_info
|
@ -1,48 +0,0 @@
|
||||
import base64
|
||||
import imghdr
|
||||
import os
|
||||
|
||||
#from IPython.
|
||||
|
||||
_TEXT_SAVED_IMAGE = "yap_kernel: saved image data to:"
|
||||
|
||||
image_setup_cmd = """
|
||||
display () {
|
||||
TMPFILE=$(mktemp ${TMPDIR-/tmp}/yap_kernel.XXXXXXXXXX)
|
||||
cat > $TMPFILE
|
||||
echo "%s $TMPFILE" >&2
|
||||
}
|
||||
""" % _TEXT_SAVED_IMAGE
|
||||
|
||||
def display_data_for_image(filename):
|
||||
with open(filename, 'rb') as f:
|
||||
image = f.read()
|
||||
os.unlink(filename)
|
||||
|
||||
image_type = imghdr.what(None, image)
|
||||
if image_type is None:
|
||||
raise ValueError("Not a valid image: %s" % image)
|
||||
|
||||
image_data = base64.b64encode(image).decode('ascii')
|
||||
content = {
|
||||
'data': {
|
||||
'image/' + image_type: image_data
|
||||
},
|
||||
'metadata': {}
|
||||
}
|
||||
return content
|
||||
|
||||
|
||||
def extract_image_filenames(output):
|
||||
output_lines = []
|
||||
image_filenames = []
|
||||
|
||||
for line in output.split("\n"):
|
||||
if line.startswith(_TEXT_SAVED_IMAGE):
|
||||
filename = line.rstrip().split(": ")[-1]
|
||||
image_filenames.append(filename)
|
||||
else:
|
||||
output_lines.append(line)
|
||||
|
||||
output = "\n".join(output_lines)
|
||||
return image_filenames, output
|
@ -1,44 +0,0 @@
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
try:
|
||||
from jupyter_client.kernelspec import install_kernel_spec
|
||||
except ImportError:
|
||||
from IPython.kernel.kernelspec import install_kernel_spec
|
||||
from IPython.utils.tempdir import TemporaryDirectory
|
||||
|
||||
|
||||
kernel_json = {
|
||||
"argv": [sys.executable,
|
||||
"-m", "yap_kernel",
|
||||
"-f", "{connection_file}"],
|
||||
"display_name": "yap",
|
||||
"mimetype": "text/x-prolog",
|
||||
"language": "prolog",
|
||||
"name": "yap",
|
||||
}
|
||||
|
||||
def install_my_kernel_spec(user=False):
|
||||
with TemporaryDirectory() as td:
|
||||
os.chmod(td, 0o755) # Starts off as 700, not user readable
|
||||
with open(os.path.join(td, 'kernel.json'), 'w') as f:
|
||||
json.dump(kernel_json, f, sort_keys=True)
|
||||
# TODO: Copy resources once they're specified
|
||||
|
||||
print('Installing IPython kernel spec')
|
||||
install_kernel_spec(td, 'yap', user=False, replace=True)
|
||||
|
||||
def _is_root():
|
||||
return True
|
||||
try:
|
||||
return os.geteuid() == 0
|
||||
except AttributeError:
|
||||
return False # assume not an admin on non-Unix platforms
|
||||
|
||||
def main(argv=[]):
|
||||
user = '--user' in argv or not _is_root()
|
||||
install_my_kernel_spec(user=user)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(argv=sys.argv)
|
@ -1,5 +1,15 @@
|
||||
:- if( current_prolog_flag(apple, true) ).
|
||||
|
||||
:- putenv( 'LC_CTYPE', 'en_us:UTF-8'),
|
||||
:- putenv( 'LC_CTYPE', 'en_us:UTF-8').
|
||||
|
||||
plot_inline :-
|
||||
X := self.inline_plotting,
|
||||
nb_setval(inline, X ),
|
||||
X = true,
|
||||
!,
|
||||
:= (
|
||||
import( matplotlib ),
|
||||
matplotlib.use( `nbagg` )
|
||||
).
|
||||
|
||||
:- endif.
|
||||
|
488
packages/python/yap_kernel/kernelapp.py
Normal file
488
packages/python/yap_kernel/kernelapp.py
Normal file
@ -0,0 +1,488 @@
|
||||
"""An Application for launching a kernel"""
|
||||
|
||||
# Copyright (c) IPython Development Team.
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import atexit
|
||||
import os
|
||||
import sys
|
||||
import signal
|
||||
import traceback
|
||||
import logging
|
||||
|
||||
from tornado import ioloop
|
||||
import zmq
|
||||
from zmq.eventloop import ioloop as zmq_ioloop
|
||||
from zmq.eventloop.zmqstream import ZMQStream
|
||||
|
||||
from IPython.core.application import (
|
||||
BaseIPythonApplication, base_flags, base_aliases, catch_config_error
|
||||
)
|
||||
from IPython.core.profiledir import ProfileDir
|
||||
from IPython.core.shellapp import (
|
||||
InteractiveShellApp, shell_flags, shell_aliases
|
||||
)
|
||||
from IPython.utils import io
|
||||
from ipython_genutils.path import filefind, ensure_dir_exists
|
||||
from traitlets import (
|
||||
Any, Instance, Dict, Unicode, Integer, Bool, DottedObjectName, Type, default
|
||||
)
|
||||
from ipython_genutils.importstring import import_item
|
||||
from jupyter_core.paths import jupyter_runtime_dir
|
||||
from jupyter_client import write_connection_file
|
||||
from jupyter_client.connect import ConnectionFileMixin
|
||||
|
||||
# local imports
|
||||
from ipykernel.iostream import IOPubThread
|
||||
from ipykernel.heartbeat import Heartbeat
|
||||
from .yap_kernel import YAPKernel
|
||||
from ipykernel.parentpoller import ParentPollerUnix, ParentPollerWindows
|
||||
from jupyter_client.session import (
|
||||
Session, session_flags, session_aliases,
|
||||
)
|
||||
from ipykernel.zmqshell import ZMQInteractiveShell
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Flags and Aliases
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
kernel_aliases = dict(base_aliases)
|
||||
kernel_aliases.update({
|
||||
'ip' : 'YAPKernelApp.ip',
|
||||
'hb' : 'YAPKernelApp.hb_port',
|
||||
'shell' : 'YAPKernelApp.shell_port',
|
||||
'iopub' : 'YAPKernelApp.iopub_port',
|
||||
'stdin' : 'YAPKernelApp.stdin_port',
|
||||
'control' : 'YAPKernelApp.control_port',
|
||||
'f' : 'YAPKernelApp.connection_file',
|
||||
'transport': 'YAPKernelApp.transport',
|
||||
})
|
||||
|
||||
kernel_flags = dict(base_flags)
|
||||
kernel_flags.update({
|
||||
'no-stdout' : (
|
||||
{'YAPKernelApp' : {'no_stdout' : True}},
|
||||
"redirect stdout to the null device"),
|
||||
'no-stderr' : (
|
||||
{'YAPKernelApp' : {'no_stderr' : True}},
|
||||
"redirect stderr to the null device"),
|
||||
'pylab' : (
|
||||
{'YAPKernelApp' : {'pylab' : 'auto'}},
|
||||
"""Pre-load matplotlib and numpy for interactive use with
|
||||
the default matplotlib backend."""),
|
||||
})
|
||||
|
||||
# inherit flags&aliases for any IPython shell apps
|
||||
kernel_aliases.update(shell_aliases)
|
||||
kernel_flags.update(shell_flags)
|
||||
|
||||
# inherit flags&aliases for Sessions
|
||||
kernel_aliases.update(session_aliases)
|
||||
kernel_flags.update(session_flags)
|
||||
|
||||
_ctrl_c_message = """\
|
||||
NOTE: When using the `ipython kernel` entry point, Ctrl-C will not work.
|
||||
|
||||
To exit, you will have to explicitly quit this process, by either sending
|
||||
"quit" from a client, or using Ctrl-\\ in UNIX-like environments.
|
||||
|
||||
To read more about this, see https://github.com/ipython/ipython/issues/2049
|
||||
|
||||
"""
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Application class for starting an IPython Kernel
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
class YAPKernelApp(BaseIPythonApplication, InteractiveShellApp,
|
||||
ConnectionFileMixin):
|
||||
name='YAP-kernel'
|
||||
aliases = Dict(kernel_aliases)
|
||||
flags = Dict(kernel_flags)
|
||||
classes = [YAPKernel, ZMQInteractiveShell, ProfileDir, Session]
|
||||
# the kernel class, as an importstring
|
||||
kernel_class = Type('yap_kernel.yap_kernel.YAPKernel',
|
||||
klass='ipykernel.kernelbase.Kernel',
|
||||
help="""The Kernel subclass to be used.
|
||||
|
||||
This should allow easy re-use of the IPKernelApp entry point
|
||||
to configure and launch kernels other than IPython's own.
|
||||
""").tag(config=True)
|
||||
kernel = Any()
|
||||
poller = Any() # don't restrict this even though current pollers are all Threads
|
||||
heartbeat = Instance(Heartbeat, allow_none=True)
|
||||
ports = Dict()
|
||||
|
||||
subcommands = {
|
||||
'install': (
|
||||
'.kernelspec.InstallYAPKernelSpecApp',
|
||||
'Install the YAP kernel'
|
||||
),
|
||||
}
|
||||
|
||||
# connection info:
|
||||
connection_dir = Unicode()
|
||||
|
||||
@default('connection_dir')
|
||||
def _default_connection_dir(self):
|
||||
return jupyter_runtime_dir()
|
||||
|
||||
@property
|
||||
def abs_connection_file(self):
|
||||
if os.path.basename(self.connection_file) == self.connection_file:
|
||||
return os.path.join(self.connection_dir, self.connection_file)
|
||||
else:
|
||||
return self.connection_file
|
||||
|
||||
# streams, etc.
|
||||
no_stdout = Bool(False, help="redirect stdout to the null device").tag(config=True)
|
||||
no_stderr = Bool(False, help="redirect stderr to the null device").tag(config=True)
|
||||
outstream_class = DottedObjectName('ipykernel.iostream.OutStream',
|
||||
help="The importstring for the OutStream factory").tag(config=True)
|
||||
displayhook_class = DottedObjectName('ipykernel.displayhook.ZMQDisplayHook',
|
||||
help="The importstring for the DisplayHook factory").tag(config=True)
|
||||
|
||||
# polling
|
||||
parent_handle = Integer(int(os.environ.get('JPY_PARENT_PID') or 0),
|
||||
help="""kill this process if its parent dies. On Windows, the argument
|
||||
specifies the HANDLE of the parent process, otherwise it is simply boolean.
|
||||
""").tag(config=True)
|
||||
interrupt = Integer(int(os.environ.get('JPY_INTERRUPT_EVENT') or 0),
|
||||
help="""ONLY USED ON WINDOWS
|
||||
Interrupt this process when the parent is signaled.
|
||||
""").tag(config=True)
|
||||
|
||||
def init_crash_handler(self):
|
||||
sys.excepthook = self.excepthook
|
||||
|
||||
def excepthook(self, etype, evalue, tb):
|
||||
# write uncaught traceback to 'real' stderr, not zmq-forwarder
|
||||
traceback.print_exception(etype, evalue, tb, file=sys.__stderr__)
|
||||
|
||||
def init_poller(self):
|
||||
if sys.platform == 'win32':
|
||||
if self.interrupt or self.parent_handle:
|
||||
self.poller = ParentPollerWindows(self.interrupt, self.parent_handle)
|
||||
elif self.parent_handle:
|
||||
self.poller = ParentPollerUnix()
|
||||
|
||||
def _bind_socket(self, s, port):
|
||||
iface = '%s://%s' % (self.transport, self.ip)
|
||||
if self.transport == 'tcp':
|
||||
if port <= 0:
|
||||
port = s.bind_to_random_port(iface)
|
||||
else:
|
||||
s.bind("tcp://%s:%i" % (self.ip, port))
|
||||
elif self.transport == 'ipc':
|
||||
if port <= 0:
|
||||
port = 1
|
||||
path = "%s-%i" % (self.ip, port)
|
||||
while os.path.exists(path):
|
||||
port = port + 1
|
||||
path = "%s-%i" % (self.ip, port)
|
||||
else:
|
||||
path = "%s-%i" % (self.ip, port)
|
||||
s.bind("ipc://%s" % path)
|
||||
return port
|
||||
|
||||
def write_connection_file(self):
|
||||
"""write connection info to JSON file"""
|
||||
cf = self.abs_connection_file
|
||||
self.log.debug("Writing connection file: %s", cf)
|
||||
write_connection_file(cf, ip=self.ip, key=self.session.key, transport=self.transport,
|
||||
shell_port=self.shell_port, stdin_port=self.stdin_port, hb_port=self.hb_port,
|
||||
iopub_port=self.iopub_port, control_port=self.control_port)
|
||||
|
||||
def cleanup_connection_file(self):
|
||||
cf = self.abs_connection_file
|
||||
self.log.debug("Cleaning up connection file: %s", cf)
|
||||
try:
|
||||
os.remove(cf)
|
||||
except (IOError, OSError):
|
||||
pass
|
||||
|
||||
self.cleanup_ipc_files()
|
||||
|
||||
def init_connection_file(self):
|
||||
if not self.connection_file:
|
||||
self.connection_file = "kernel-%s.json"%os.getpid()
|
||||
try:
|
||||
self.connection_file = filefind(self.connection_file, ['.', self.connection_dir])
|
||||
except IOError:
|
||||
self.log.debug("Connection file not found: %s", self.connection_file)
|
||||
# This means I own it, and I'll create it in this directory:
|
||||
ensure_dir_exists(os.path.dirname(self.abs_connection_file), 0o700)
|
||||
# Also, I will clean it up:
|
||||
atexit.register(self.cleanup_connection_file)
|
||||
return
|
||||
try:
|
||||
self.load_connection_file()
|
||||
except Exception:
|
||||
self.log.error("Failed to load connection file: %r", self.connection_file, exc_info=True)
|
||||
self.exit(1)
|
||||
|
||||
def init_sockets(self):
|
||||
# Create a context, a session, and the kernel sockets.
|
||||
self.log.info("Starting the kernel at pid: %i", os.getpid())
|
||||
context = zmq.Context.instance()
|
||||
# Uncomment this to try closing the context.
|
||||
# atexit.register(context.term)
|
||||
|
||||
self.shell_socket = context.socket(zmq.ROUTER)
|
||||
self.shell_socket.linger = 1000
|
||||
self.shell_port = self._bind_socket(self.shell_socket, self.shell_port)
|
||||
self.log.debug("shell ROUTER Channel on port: %i" % self.shell_port)
|
||||
|
||||
self.stdin_socket = context.socket(zmq.ROUTER)
|
||||
self.stdin_socket.linger = 1000
|
||||
self.stdin_port = self._bind_socket(self.stdin_socket, self.stdin_port)
|
||||
self.log.debug("stdin ROUTER Channel on port: %i" % self.stdin_port)
|
||||
|
||||
self.control_socket = context.socket(zmq.ROUTER)
|
||||
self.control_socket.linger = 1000
|
||||
self.control_port = self._bind_socket(self.control_socket, self.control_port)
|
||||
self.log.debug("control ROUTER Channel on port: %i" % self.control_port)
|
||||
|
||||
self.init_iopub(context)
|
||||
|
||||
def init_iopub(self, context):
|
||||
self.iopub_socket = context.socket(zmq.PUB)
|
||||
self.iopub_socket.linger = 1000
|
||||
self.iopub_port = self._bind_socket(self.iopub_socket, self.iopub_port)
|
||||
self.log.debug("iopub PUB Channel on port: %i" % self.iopub_port)
|
||||
self.configure_tornado_logger()
|
||||
self.iopub_thread = IOPubThread(self.iopub_socket, pipe=True)
|
||||
self.iopub_thread.start()
|
||||
# backward-compat: wrap iopub socket API in background thread
|
||||
self.iopub_socket = self.iopub_thread.background_socket
|
||||
|
||||
def init_heartbeat(self):
|
||||
"""start the heart beating"""
|
||||
# heartbeat doesn't share context, because it mustn't be blocked
|
||||
# by the GIL, which is accessed by libzmq when freeing zero-copy messages
|
||||
hb_ctx = zmq.Context()
|
||||
self.heartbeat = Heartbeat(hb_ctx, (self.transport, self.ip, self.hb_port))
|
||||
self.hb_port = self.heartbeat.port
|
||||
self.log.debug("Heartbeat REP Channel on port: %i" % self.hb_port)
|
||||
self.heartbeat.start()
|
||||
|
||||
def log_connection_info(self):
|
||||
"""display connection info, and store ports"""
|
||||
basename = os.path.basename(self.connection_file)
|
||||
if basename == self.connection_file or \
|
||||
os.path.dirname(self.connection_file) == self.connection_dir:
|
||||
# use shortname
|
||||
tail = basename
|
||||
else:
|
||||
tail = self.connection_file
|
||||
lines = [
|
||||
"To connect another client to this kernel, use:",
|
||||
" --existing %s" % tail,
|
||||
]
|
||||
# log connection info
|
||||
# info-level, so often not shown.
|
||||
# frontends should use the %connect_info magic
|
||||
# to see the connection info
|
||||
for line in lines:
|
||||
self.log.info(line)
|
||||
# also raw print to the terminal if no parent_handle (`ipython kernel`)
|
||||
# unless log-level is CRITICAL (--quiet)
|
||||
if not self.parent_handle and self.log_level < logging.CRITICAL:
|
||||
io.rprint(_ctrl_c_message)
|
||||
for line in lines:
|
||||
io.rprint(line)
|
||||
|
||||
self.ports = dict(shell=self.shell_port, iopub=self.iopub_port,
|
||||
stdin=self.stdin_port, hb=self.hb_port,
|
||||
control=self.control_port)
|
||||
|
||||
def init_blackhole(self):
|
||||
"""redirects stdout/stderr to devnull if necessary"""
|
||||
if self.no_stdout or self.no_stderr:
|
||||
blackhole = open(os.devnull, 'w')
|
||||
if self.no_stdout:
|
||||
sys.stdout = sys.__stdout__ = blackhole
|
||||
if self.no_stderr:
|
||||
sys.stderr = sys.__stderr__ = blackhole
|
||||
|
||||
def init_io(self):
|
||||
"""Redirect input streams and set a display hook."""
|
||||
if self.outstream_class:
|
||||
outstream_factory = import_item(str(self.outstream_class))
|
||||
sys.stdout = outstream_factory(self.session, self.iopub_thread, u'stdout')
|
||||
sys.stderr = outstream_factory(self.session, self.iopub_thread, u'stderr')
|
||||
if self.displayhook_class:
|
||||
displayhook_factory = import_item(str(self.displayhook_class))
|
||||
self.displayhook = displayhook_factory(self.session, self.iopub_socket)
|
||||
sys.displayhook = self.displayhook
|
||||
|
||||
self.patch_io()
|
||||
|
||||
def patch_io(self):
|
||||
"""Patch important libraries that can't handle sys.stdout forwarding"""
|
||||
try:
|
||||
import faulthandler
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
# Warning: this is a monkeypatch of `faulthandler.enable`, watch for possible
|
||||
# updates to the upstream API and update accordingly (up-to-date as of Python 3.5):
|
||||
# https://docs.python.org/3/library/faulthandler.html#faulthandler.enable
|
||||
|
||||
# change default file to __stderr__ from forwarded stderr
|
||||
faulthandler_enable = faulthandler.enable
|
||||
def enable(file=sys.__stderr__, all_threads=True, **kwargs):
|
||||
return faulthandler_enable(file=file, all_threads=all_threads, **kwargs)
|
||||
|
||||
faulthandler.enable = enable
|
||||
|
||||
if hasattr(faulthandler, 'register'):
|
||||
faulthandler_register = faulthandler.register
|
||||
def register(signum, file=sys.__stderr__, all_threads=True, chain=False, **kwargs):
|
||||
return faulthandler_register(signum, file=file, all_threads=all_threads,
|
||||
chain=chain, **kwargs)
|
||||
faulthandler.register = register
|
||||
|
||||
def init_signal(self):
|
||||
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
||||
|
||||
def init_kernel(self):
|
||||
"""Create the Kernel object itself"""
|
||||
shell_stream = ZMQStream(self.shell_socket)
|
||||
control_stream = ZMQStream(self.control_socket)
|
||||
|
||||
kernel_factory = self.kernel_class.instance
|
||||
|
||||
kernel = kernel_factory(parent=self, session=self.session,
|
||||
shell_streams=[shell_stream, control_stream],
|
||||
iopub_thread=self.iopub_thread,
|
||||
iopub_socket=self.iopub_socket,
|
||||
stdin_socket=self.stdin_socket,
|
||||
log=self.log,
|
||||
profile_dir=self.profile_dir,
|
||||
user_ns=self.user_ns,
|
||||
)
|
||||
kernel.record_ports({
|
||||
name + '_port': port for name, port in self.ports.items()
|
||||
})
|
||||
self.kernel = kernel
|
||||
|
||||
# Allow the displayhook to get the execution count
|
||||
self.displayhook.get_execution_count = lambda: kernel.execution_count
|
||||
|
||||
def init_gui_pylab(self):
|
||||
"""Enable GUI event loop integration, taking pylab into account."""
|
||||
|
||||
# Register inline backend as default
|
||||
# this is higher priority than matplotlibrc,
|
||||
# but lower priority than anything else (mpl.use() for instance).
|
||||
# This only affects matplotlib >= 1.5
|
||||
if not os.environ.get('MPLBACKEND'):
|
||||
os.environ['MPLBACKEND'] = 'module://ipykernel.pylab.backend_inline'
|
||||
|
||||
# Provide a wrapper for :meth:`InteractiveShellApp.init_gui_pylab`
|
||||
# to ensure that any exception is printed straight to stderr.
|
||||
# Normally _showtraceback associates the reply with an execution,
|
||||
# which means frontends will never draw it, as this exception
|
||||
# is not associated with any execute request.
|
||||
|
||||
shell = self.shell
|
||||
_showtraceback = shell._showtraceback
|
||||
try:
|
||||
# replace error-sending traceback with stderr
|
||||
def print_tb(etype, evalue, stb):
|
||||
print ("GUI event loop or pylab initialization failed",
|
||||
file=sys.stderr)
|
||||
print (shell.InteractiveTB.stb2text(stb), file=sys.stderr)
|
||||
shell._showtraceback = print_tb
|
||||
InteractiveShellApp.init_gui_pylab(self)
|
||||
finally:
|
||||
shell._showtraceback = _showtraceback
|
||||
|
||||
def init_shell(self):
|
||||
self.shell = getattr(self.kernel, 'shell', None)
|
||||
if self.shell:
|
||||
self.shell.configurables.append(self)
|
||||
|
||||
def init_extensions(self):
|
||||
super(YAPKernelApp, self).init_extensions()
|
||||
# BEGIN HARDCODED WIDGETS HACK
|
||||
# Ensure ipywidgets extension is loaded if available
|
||||
extension_man = self.shell.extension_manager
|
||||
if 'ipywidgets' not in extension_man.loaded:
|
||||
try:
|
||||
extension_man.load_extension('ipywidgets')
|
||||
except ImportError as e:
|
||||
self.log.debug('ipywidgets package not installed. Widgets will not be available.')
|
||||
# END HARDCODED WIDGETS HACK
|
||||
|
||||
def configure_tornado_logger(self):
|
||||
""" Configure the tornado logging.Logger.
|
||||
|
||||
Must set up the tornado logger or else tornado will call
|
||||
basicConfig for the root logger which makes the root logger
|
||||
go to the real sys.stderr instead of the capture streams.
|
||||
This function mimics the setup of logging.basicConfig.
|
||||
"""
|
||||
logger = logging.getLogger('tornado')
|
||||
handler = logging.StreamHandler()
|
||||
formatter = logging.Formatter(logging.BASIC_FORMAT)
|
||||
handler.setFormatter(formatter)
|
||||
logger.addHandler(handler)
|
||||
|
||||
@catch_config_error
|
||||
def initialize(self, argv=None):
|
||||
super(YAPKernelApp, self).initialize(argv)
|
||||
if self.subapp is not None:
|
||||
return
|
||||
# register zmq IOLoop with tornado
|
||||
zmq_ioloop.install()
|
||||
self.init_blackhole()
|
||||
self.init_connection_file()
|
||||
self.init_poller()
|
||||
self.init_sockets()
|
||||
self.init_heartbeat()
|
||||
# writing/displaying connection info must be *after* init_sockets/heartbeat
|
||||
self.write_connection_file()
|
||||
# Log connection info after writing connection file, so that the connection
|
||||
# file is definitely available at the time someone reads the log.
|
||||
self.log_connection_info()
|
||||
self.init_io()
|
||||
self.init_signal()
|
||||
self.init_kernel()
|
||||
# shell init steps
|
||||
self.init_path()
|
||||
self.init_shell()
|
||||
if self.shell:
|
||||
self.init_gui_pylab()
|
||||
self.init_extensions()
|
||||
self.init_code()
|
||||
# flush stdout/stderr, so that anything written to these streams during
|
||||
# initialization do not get associated with the first execution request
|
||||
sys.stdout.flush()
|
||||
sys.stderr.flush()
|
||||
|
||||
def start(self):
|
||||
if self.subapp is not None:
|
||||
return self.subapp.start()
|
||||
if self.poller is not None:
|
||||
self.poller.start()
|
||||
self.kernel.start()
|
||||
try:
|
||||
ioloop.IOLoop.instance().start()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
launch_new_instance = YAPKernelApp.launch_instance
|
||||
|
||||
def main():
|
||||
"""Run an IPKernel as an application"""
|
||||
app = YAPKernelApp.instance()
|
||||
app.initialize()
|
||||
app.start()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
188
packages/python/yap_kernel/kernelspec.py
Normal file
188
packages/python/yap_kernel/kernelspec.py
Normal file
@ -0,0 +1,188 @@
|
||||
"""The IPython kernel spec for Jupyter"""
|
||||
|
||||
# Copyright (c) IPython Development Team.
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import errno
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
from jupyter_client.kernelspec import KernelSpecManager
|
||||
|
||||
pjoin = os.path.join
|
||||
|
||||
KERNEL_NAME = 'YAP%i' % sys.version_info[0]
|
||||
|
||||
# path to kernelspec resources
|
||||
RESOURCES = pjoin(os.path.dirname(__file__), 'resources')
|
||||
|
||||
|
||||
def make_ipkernel_cmd(mod='ipykernel', executable=None, extra_arguments=None, **kw):
|
||||
"""Build Popen command list for launching an IPython kernel.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
mod : str, optional (default 'ipykernel')
|
||||
A string of an IPython module whose __main__ starts an IPython kernel
|
||||
|
||||
executable : str, optional (default sys.executable)
|
||||
The Python executable to use for the kernel process.
|
||||
|
||||
extra_arguments : list, optional
|
||||
A list of extra arguments to pass when executing the launch code.
|
||||
|
||||
Returns
|
||||
-------
|
||||
|
||||
A Popen command list
|
||||
"""
|
||||
if executable is None:
|
||||
executable = sys.executable
|
||||
extra_arguments = extra_arguments or []
|
||||
arguments = [executable, '-m', mod, '-f', '{connection_file}']
|
||||
arguments.extend(extra_arguments)
|
||||
|
||||
return arguments
|
||||
|
||||
|
||||
def get_kernel_dict(extra_arguments=None):
|
||||
"""Construct dict for kernel.json"""
|
||||
return {
|
||||
'argv': make_ipkernel_cmd(extra_arguments=extra_arguments),
|
||||
'display_name': 'Python %i' % sys.version_info[0],
|
||||
'language': 'python',
|
||||
}
|
||||
|
||||
|
||||
def write_kernel_spec(path=None, overrides=None, extra_arguments=None):
|
||||
"""Write a kernel spec directory to `path`
|
||||
|
||||
If `path` is not specified, a temporary directory is created.
|
||||
If `overrides` is given, the kernelspec JSON is updated before writing.
|
||||
|
||||
The path to the kernelspec is always returned.
|
||||
"""
|
||||
if path is None:
|
||||
path = os.path.join(tempfile.mkdtemp(suffix='_kernels'), KERNEL_NAME)
|
||||
|
||||
# stage resources
|
||||
shutil.copytree(RESOURCES, path)
|
||||
# write kernel.json
|
||||
kernel_dict = get_kernel_dict(extra_arguments)
|
||||
|
||||
if overrides:
|
||||
kernel_dict.update(overrides)
|
||||
with open(pjoin(path, 'kernel.json'), 'w') as f:
|
||||
json.dump(kernel_dict, f, indent=1)
|
||||
|
||||
return path
|
||||
|
||||
|
||||
def install(kernel_spec_manager=None, user=False, kernel_name=KERNEL_NAME, display_name=None,
|
||||
prefix=None, profile=None):
|
||||
"""Install the IPython kernelspec for Jupyter
|
||||
|
||||
Parameters
|
||||
----------
|
||||
|
||||
kernel_spec_manager: KernelSpecManager [optional]
|
||||
A KernelSpecManager to use for installation.
|
||||
If none provided, a default instance will be created.
|
||||
user: bool [default: False]
|
||||
Whether to do a user-only install, or system-wide.
|
||||
kernel_name: str, optional
|
||||
Specify a name for the kernelspec.
|
||||
This is needed for having multiple IPython kernels for different environments.
|
||||
display_name: str, optional
|
||||
Specify the display name for the kernelspec
|
||||
profile: str, optional
|
||||
Specify a custom profile to be loaded by the kernel.
|
||||
prefix: str, optional
|
||||
Specify an install prefix for the kernelspec.
|
||||
This is needed to install into a non-default location, such as a conda/virtual-env.
|
||||
|
||||
Returns
|
||||
-------
|
||||
|
||||
The path where the kernelspec was installed.
|
||||
"""
|
||||
if kernel_spec_manager is None:
|
||||
kernel_spec_manager = KernelSpecManager()
|
||||
|
||||
if (kernel_name != KERNEL_NAME) and (display_name is None):
|
||||
# kernel_name is specified and display_name is not
|
||||
# default display_name to kernel_name
|
||||
display_name = kernel_name
|
||||
overrides = {}
|
||||
if display_name:
|
||||
overrides["display_name"] = display_name
|
||||
if profile:
|
||||
extra_arguments = ["--profile", profile]
|
||||
if not display_name:
|
||||
# add the profile to the default display name
|
||||
overrides["display_name"] = 'Python %i [profile=%s]' % (sys.version_info[0], profile)
|
||||
else:
|
||||
extra_arguments = None
|
||||
path = write_kernel_spec(overrides=overrides, extra_arguments=extra_arguments)
|
||||
dest = kernel_spec_manager.install_kernel_spec(
|
||||
path, kernel_name=kernel_name, user=user, prefix=prefix)
|
||||
# cleanup afterward
|
||||
shutil.rmtree(path)
|
||||
return dest
|
||||
|
||||
# Entrypoint
|
||||
|
||||
from traitlets.config import Application
|
||||
|
||||
|
||||
class InstallYAPKernelSpecApp(Application):
|
||||
"""Dummy app wrapping argparse"""
|
||||
name = 'ipython-kernel-install'
|
||||
|
||||
def initialize(self, argv=None):
|
||||
if argv is None:
|
||||
argv = sys.argv[1:]
|
||||
self.argv = argv
|
||||
|
||||
def start(self):
|
||||
import argparse
|
||||
parser = argparse.ArgumentParser(prog=self.name,
|
||||
description="Install the YAP kernel spec.")
|
||||
parser.add_argument('--user', action='store_true',
|
||||
help="Install for the current user instead of system-wide")
|
||||
parser.add_argument('--name', type=str, default=KERNEL_NAME,
|
||||
help="Specify a name for the kernelspec."
|
||||
" This is needed to have multiple IPython kernels at the same time.")
|
||||
parser.add_argument('--display-name', type=str,
|
||||
help="Specify the display name for the kernelspec."
|
||||
" This is helpful when you have multiple IPython kernels.")
|
||||
parser.add_argument('--profile', type=str,
|
||||
help="Specify an IPython profile to load. "
|
||||
"This can be used to create custom versions of the kernel.")
|
||||
parser.add_argument('--prefix', type=str,
|
||||
help="Specify an install prefix for the kernelspec."
|
||||
" This is needed to install into a non-default location, such as a conda/virtual-env.")
|
||||
parser.add_argument('--sys-prefix', action='store_const', const=sys.prefix, dest='prefix',
|
||||
help="Install to Python's sys.prefix."
|
||||
" Shorthand for --prefix='%s'. For use in conda/virtual-envs." % sys.prefix)
|
||||
opts = parser.parse_args(self.argv)
|
||||
try:
|
||||
dest = install(user=opts.user, kernel_name=opts.name, profile=opts.profile,
|
||||
prefix=opts.prefix, display_name=opts.display_name)
|
||||
except OSError as e:
|
||||
if e.errno == errno.EACCES:
|
||||
print(e, file=sys.stderr)
|
||||
if opts.user:
|
||||
print("Perhaps you want `sudo` or `--user`?", file=sys.stderr)
|
||||
self.exit(1)
|
||||
raise
|
||||
print("Installed kernelspec %s in %s" % (opts.name, dest))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
InstallYAPKernelSpecApp.launch_instance()
|
@ -1,60 +1,81 @@
|
||||
from distutils.command.install import install
|
||||
from distutils.core import setup
|
||||
from distutils import log
|
||||
import json
|
||||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
# Copyright (c) IPython Development Team.
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
# the name of the package
|
||||
name = 'yap_kernel'
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Minimal Python version sanity check
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
import sys
|
||||
|
||||
v = sys.version_info
|
||||
if v[:2] < (2,7) or (v[0] >= 3 and v[:2] < (3,3)):
|
||||
error = "ERROR: %s requires Python version 2.7 or 3.3 or above." % name
|
||||
print(error, file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
PY3 = (sys.version_info[0] >= 3)
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# get on with it
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
import os
|
||||
from glob import glob
|
||||
|
||||
PY3 = sys.version_info[0] >= 3
|
||||
from distutils.core import setup
|
||||
|
||||
kernel_json = {
|
||||
"argv": [sys.executable,
|
||||
"-m", "yap_kernel",
|
||||
"-f", "{connection_file}"],
|
||||
"display_name": " YAP-6.3" ,
|
||||
"language": "prolog",
|
||||
"name": "yap_kernel",
|
||||
packages = ["${CMAKE_CURRENT_SOURCE_DIR}"]
|
||||
|
||||
version_ns = {}
|
||||
setup_args = dict(
|
||||
name = 'yap_kernel',
|
||||
version = '0.0.1',
|
||||
packages = ["yap_kernel"],
|
||||
package_dir = {'': '${CMAKE_SOURCE_DIR}/packages/python' },
|
||||
description = "YAP Kernel for Jupyter",
|
||||
long_description="A simple YAP kernel for Jupyter/IPython",
|
||||
url="https://github.com/vscosta/yap-6.3",
|
||||
author='Vitor Santos Costa, based on the the IPython',
|
||||
author_email='vsc@dcc.fc.up.pt',
|
||||
license = 'BSD',
|
||||
platforms = "Linux, Mac OS X, Windows",
|
||||
keywords = ['Interactive', 'Interpreter', 'Shell', 'Web'],
|
||||
classifiers = [
|
||||
'Intended Audience :: Developers',
|
||||
'Intended Audience :: System Administrators',
|
||||
'Intended Audience :: Science/Research',
|
||||
'License :: OSI Approved :: BSD License',
|
||||
'Programming Language :: Prolog',
|
||||
'Programming Language :: Python :: 2.7',
|
||||
'Programming Language :: Python :: 3',
|
||||
],
|
||||
)
|
||||
|
||||
if 'develop' in sys.argv or any(a.startswith('bdist') for a in sys.argv):
|
||||
import setuptools
|
||||
|
||||
setuptools_args = {}
|
||||
install_requires = setuptools_args['install_requires'] = [
|
||||
'ipython>=4.0.0',
|
||||
'traitlets>=4.1.0',
|
||||
'jupyter_client',
|
||||
'tornado>=4.0',
|
||||
]
|
||||
|
||||
extras_require = setuptools_args['extras_require'] = {
|
||||
'test:python_version=="2.7"': ['mock', 'nose_warnings_filters'],
|
||||
}
|
||||
|
||||
if 'setuptools' in sys.modules:
|
||||
setup_args.update(setuptools_args)
|
||||
|
||||
class install_with_kernelspec(install):
|
||||
def run(self):
|
||||
install.run(self)
|
||||
from jupyter_client.kernelspec import install_kernel_spec
|
||||
from IPython.utils.tempdir import TemporaryDirectory
|
||||
with TemporaryDirectory() as td:
|
||||
os.chmod(td, 0o755) # Starts off as 700, not user readable
|
||||
with open(os.path.join(td, 'kernel.json'), 'w') as f:
|
||||
json.dump(kernel_json, f, sort_keys=True)
|
||||
log.info('Installing kernel spec')
|
||||
try:
|
||||
install_kernel_spec(td, 'yap_kernel', user=self.user,
|
||||
replace=True)
|
||||
except:
|
||||
install_kernel_spec(td, 'yap_kernel', user=not self.user,
|
||||
replace=True)
|
||||
|
||||
svem_flag = '--single-version-externally-managed'
|
||||
if svem_flag in sys.argv:
|
||||
# Die, setuptools, die.
|
||||
sys.argv.remove(svem_flag)
|
||||
|
||||
setup(name='yap_kernel',
|
||||
version='0.0.1',
|
||||
package_dir = {'': '${CMAKE_SOURCE_DIR}/packages/python/yap_kernel' },
|
||||
description='A simple YAP kernel for Jupyter/IPython',
|
||||
long_description="A simple YAP kernel for Jupyter/IPython, based on MetaKernel",
|
||||
url="https://github.com/vscosta/yap-6.3",
|
||||
author='Vitor Santos Costa, based on the metakernel from Douglas Blank',
|
||||
author_email='vsc@dcc.fc.up.pt',
|
||||
py_modules=['yap_kernel'],
|
||||
install_requires=["metakernel","yap"],
|
||||
cmdclass={'install': install_with_kernelspec},
|
||||
classifiers = [
|
||||
'Framework :: IPython',
|
||||
'License :: OSI Approved :: BSD License',
|
||||
'Programming Language :: YAP :: 6.3',
|
||||
'Programming Language :: Python :: 3',
|
||||
'Topic :: System :: Shells',
|
||||
]
|
||||
)
|
||||
if __name__ == '__main__':
|
||||
setup(**setup_args)
|
||||
|
@ -1,44 +1,59 @@
|
||||
from __future__ import print_function
|
||||
|
||||
from metakernel import MetaKernel
|
||||
from ipykernel.ipkernel import IPythonKernel
|
||||
|
||||
import sys
|
||||
import signal
|
||||
import yap
|
||||
import yapex
|
||||
|
||||
import ipywidgets as widgets
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger()
|
||||
handler = logging.StreamHandler()
|
||||
formatter = logging.Formatter(
|
||||
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
|
||||
handler.setFormatter(formatter)
|
||||
logger.addHandler(handler)
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
logger.debug('often makes a very good meal of %s', 'visiting tourists')
|
||||
|
||||
kernel_json = {
|
||||
"argv": [sys.executable,
|
||||
"-m", "yap_kernel",
|
||||
"-f", "{connection_file}"],
|
||||
"display_name": " YAP-6.3" ,
|
||||
"language": "prolog",
|
||||
"name": "yap_kernel",
|
||||
}
|
||||
|
||||
|
||||
def eprint(*args, **kwargs):
|
||||
print(*args, file=sys.stderr, **kwargs)
|
||||
|
||||
|
||||
class MetaKernelyap(MetaKernel):
|
||||
class YAPKernel(IPythonKernel):
|
||||
implementation = 'MetaKernel YAP'
|
||||
implementation_version = '1.0'
|
||||
language = 'text'
|
||||
language_version = '0.1'
|
||||
banner = "MetaKernel YAP"
|
||||
banner = "YAP-6.3"
|
||||
language_info = {
|
||||
'mimetype': 'text/prolog',
|
||||
'name': 'text',
|
||||
# ------ If different from 'language':
|
||||
'codemirror_mode': {
|
||||
'codemirror_mode': {
|
||||
"version": 2,
|
||||
"name": "prolog"
|
||||
},
|
||||
'pygments_lexer': 'prolog',
|
||||
'version' : "0.0.1",
|
||||
},
|
||||
'pygments_lexer': 'prolog',
|
||||
'version' : "0.0.1",
|
||||
'file_extension': '.yap',
|
||||
'help_links': MetaKernel.help_links,
|
||||
}
|
||||
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
MetaKernel.__init__(self, **kwargs)
|
||||
self._start_yap(**kwargs)
|
||||
self.olines = ""
|
||||
self.ask = True
|
||||
super(YAPKernel, self).__init__( **kwargs)
|
||||
_start_yap( **kwargs )
|
||||
|
||||
def _start_yap(self, **kwargs):
|
||||
# Signal handlers are inherited by forked processes, and we can't easily
|
||||
@ -51,18 +66,16 @@ class MetaKernelyap(MetaKernel):
|
||||
self.q = None
|
||||
self.engine.query("load_files(library(python), [])").command()
|
||||
self.engine.query("load_files(library(jupyter), [])").command()
|
||||
banner = "YAP {0} Kernel".format(self.engine.version())
|
||||
banner = "YAP6-3 Kernel"
|
||||
self.olines = banner
|
||||
finally:
|
||||
signal.signal(signal.SIGINT, sig)
|
||||
|
||||
# Register Yap function to write image data to temporary file
|
||||
#self.yapwrapper.run_command(image_setup_cmd)
|
||||
|
||||
def get_usage(self):
|
||||
return "This is the YAP kernel."
|
||||
|
||||
def query_prolog(self, s):
|
||||
|
||||
def run_cell(self, s, store_history=False, silent=False, shell_futures=True):
|
||||
|
||||
if not self.q:
|
||||
self.q = self.engine.query(s)
|
||||
@ -94,118 +107,3 @@ class MetaKernelyap(MetaKernel):
|
||||
if self.q:
|
||||
self.q.close()
|
||||
self.q = None
|
||||
|
||||
def do_execute_direct(self, code):
|
||||
if not code.strip():
|
||||
return ""
|
||||
lines = code.split("\n")
|
||||
interrupted = False
|
||||
self.doReset = True
|
||||
nlines = ""
|
||||
try:
|
||||
for line in lines:
|
||||
line = line.strip()
|
||||
if line.startswith('#'):
|
||||
# wait
|
||||
print( "comment")
|
||||
elif line.startswith('%'):
|
||||
# wait
|
||||
call_magic( line )
|
||||
elif line.endswith(';'):
|
||||
nlines += line.rstrip(';').rstrip()
|
||||
self.doReset = False
|
||||
break
|
||||
elif line.endswith('!'):
|
||||
nlines += line.rstrip('!').rstrip()
|
||||
self.ask = False
|
||||
self.doReset = False
|
||||
break
|
||||
else:
|
||||
line = line.rstrip()
|
||||
if line:
|
||||
nlines += line + "\n"
|
||||
if nlines != self.olines:
|
||||
self.closeq( )
|
||||
self.olines = nlines
|
||||
elif self.doReset:
|
||||
opt = widgets.ToggleButtons(
|
||||
description='Query Solutions:',
|
||||
options=['First', 'Next', 'All'],
|
||||
)
|
||||
print( opt )
|
||||
if opt == 'First':
|
||||
self.closeq( )
|
||||
elif opt == 'Next':
|
||||
self.doReset = False
|
||||
else:
|
||||
self.ask = False
|
||||
self.doReset = False
|
||||
self.query_prolog( nlines )
|
||||
while not self.ask and self.q:
|
||||
self.query_prolog( nlines )
|
||||
|
||||
except SyntaxError as err:
|
||||
print("Syntax Error error: {0}".format(err))
|
||||
except EOFError:
|
||||
return
|
||||
except RuntimeError as err:
|
||||
print("YAP Execution Error: {0}".format(err))
|
||||
except ValueError:
|
||||
print("Could not convert data to an integer.")
|
||||
except KeyboardInterrupt:
|
||||
return 'stopped by user'
|
||||
except:
|
||||
print("Unexpected error:", sys.exc_info()[0])
|
||||
raise
|
||||
|
||||
def do_complete(self, code, cursor_pos):
|
||||
print(code)
|
||||
print(cursor_pos)
|
||||
eprint( code, " -- ", str(cursor_pos ) )
|
||||
# code = code[:cursor_pos]
|
||||
# default = {'matches': [], 'cursor_start': 0,
|
||||
# 'cursor_end': cursor_pos, 'metadata': dict(),
|
||||
# 'status': 'ok'}
|
||||
|
||||
# if not code or code[-1] == ' ':
|
||||
# return default
|
||||
|
||||
# tokens = code.replace(';', ' ').split()
|
||||
# if not tokens:
|
||||
# return default
|
||||
|
||||
# matches = []
|
||||
# token = tokens[-1]
|
||||
# start = cursor_pos - len(token)
|
||||
|
||||
# if token[0] == '$':
|
||||
# # complete variables
|
||||
# cmd = 'compgen -A arrayvar -A export \
|
||||
# -A variable %s' % token[1:] # strip leading $
|
||||
# output = self.bashwrapper.run_command(cmd).rstrip()
|
||||
# completions = set(output.split())
|
||||
# # append matches including leading $
|
||||
# matches.extend(['$'+c for c in completions])
|
||||
# else:
|
||||
# # complete functions and builtins
|
||||
# cmd = 'compgen -cdfa %s' % token
|
||||
# output = self.bashwrapper.run_command(cmd).rstrip()
|
||||
# matches.extend(output.split())
|
||||
|
||||
# if not matches:
|
||||
# return default
|
||||
# matches = [m for m in matches if m.startswith(token)]
|
||||
|
||||
# return {'matches': sorted(matches), 'cursor_start': start,
|
||||
# 'cursor_end': cursor_pos, 'metadata': dict(),
|
||||
# 'status': 'ok'}
|
||||
|
||||
def repr(self, data):
|
||||
return repr(data)
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
from ipykernel.kernelapp import IPKernelApp
|
||||
except ImportError:
|
||||
from jupyter_client.zmq.kernelapp import IPKernelApp
|
||||
IPKernelApp.launch_instance(kernel_class=MetaKernelyap)
|
||||
|
@ -19,6 +19,7 @@ if (PYTHONLIBS_FOUND)
|
||||
SET_SOURCE_FILES_PROPERTIES(../yap.i PROPERTY SWIG_MODULE_NAME libPy2YAP )
|
||||
|
||||
configure_file ("setup.py.cmake" "setup.py" )
|
||||
configure_file ("../yap.i" "yap.i" )
|
||||
|
||||
|
||||
#SET( CMAKE_SWIG_OUTDIR "yap" )
|
||||
|
@ -11,12 +11,12 @@ else:
|
||||
setup(
|
||||
name = "yap",
|
||||
version = "0.1",
|
||||
ext_modules=[Extension('_yap', ['${CMAKE_SOURCE_DIR}/packages/swig/yap.i'],
|
||||
ext_modules=[Extension('_yap', ['yap.i'],
|
||||
define_macros = [('MAJOR_VERSION', '1'),
|
||||
('MINOR_VERSION', '0'),
|
||||
('_YAP_NOT_INSTALLED_', '1')],
|
||||
runtime_library_dirs=['${dlls}'],
|
||||
swig_opts=['-modern', '-c++', '-py3','-I${CMAKE_SOURCE_DIR}/CXX'],
|
||||
swig_opts=['-modern','-outcurrentdir', '-c++', '-py3','-I${CMAKE_SOURCE_DIR}/CXX'],
|
||||
library_dirs=['../../..','../../../CXX',
|
||||
'../../python',
|
||||
'.'],
|
||||
|
Reference in New Issue
Block a user