WIP mediaWikiSystem

This commit is contained in:
2021-08-01 18:27:58 -06:00
parent 2623b5d543
commit f443ca0867
12 changed files with 96 additions and 12 deletions

View File

@@ -2,6 +2,7 @@
-lib uuid -lib uuid
-lib tink_macro -lib tink_macro
-lib tink_json -lib tink_json
-lib requests-externs
-cp src -cp src
--main nat.CLI --main nat.CLI
--interp --interp

View File

@@ -0,0 +1 @@
-lib requests-externs

View File

@@ -0,0 +1,2 @@
import requests_externs.Response;
import requests_externs.Requests;

View File

@@ -0,0 +1 @@
requests==2.26.0

View File

@@ -1,9 +1,9 @@
(function eval [:String expStr :Array<String> activeConditions] (function eval [:String expStr :Array<String> activeConditions]
(let [hscriptExp (let [hscript (Prelude.convertToHScript expStr)
(.parseString (new Parser) parser (new Parser)
(Prelude.convertToHScript expStr)) hscriptExp (parser.parseString hscript)
interp interp (new BoolExpInterp)]
(new BoolExpInterp)]
(doFor condition activeConditions (doFor condition activeConditions
(interp.variables.set condition true)) (interp.variables.set condition true))
?(interp.execute hscriptExp))) ?(interp.execute hscriptExp)))

View File

@@ -0,0 +1,9 @@
package nat.systems;
import kiss.Prelude;
import kiss.List;
import requests_externs.Response;
import nat.System;
@:build(kiss.Kiss.build())
class MediaWikiSystem extends System {}

View File

@@ -0,0 +1,32 @@
(load "../Lib.kiss")
(defNew [&prop :String mediaWikiUrl
// TODO make a &super annotation that passes an argument to the super constructor
:EntryChecker canProcess
:EntryProcessor processor
&prop :Null<Float> maxLag]
(super
canProcess
processor))
// TODO make this an externMethod --
// but mediaWikiUrl, headers, and maxLag will still have to be specified, not as args
// unless all vars and props are passed to externmethods by default
(method :Response queryProp [:Array<String> titles :Array<String> props]
(#extern Response python
(object
hxmlFile "extern-files/python/args.hxml"
importHxFile "extern-files/python/import.hx"
langProjectFile "extern-files/python/requirements.txt")
[:Array<String> titles _ :Array<String> props _ :String mediaWikiUrl _ :Float maxLag (or maxLag 1) :Map<String,String> headers _]
(Requests.get mediaWikiUrl
[
=>"action" "query"
=>"titles" (titles.join "|")
=>"prop" (props.join "|")
=>"maxlag" (Std.string maxLag)
=>"format" "json"
]
(object headers headers))))
(var headers [=>"User-Agent" "NatArchiveTool/0.0.0 (https://github.com/NQNStudios/kisslang/tree/main/projects/nat-archive-tool; natquaylenelson@gmail.com) Requests/2.26.0"])

View File

@@ -6,6 +6,7 @@ import nat.BoolExpInterp;
import nat.Archive; import nat.Archive;
import nat.Lib; import nat.Lib;
import nat.components.*; import nat.components.*;
import nat.systems.*;
@:build(kiss.Kiss.build()) @:build(kiss.Kiss.build())
class TestMain {} class TestMain {}

View File

@@ -1,12 +1,21 @@
// External programs can load Lib.kiss with (loadFrom "nat-archive-tool" "src/nat/Lib.kiss") // External programs can load Lib.kiss with (loadFrom "nat-archive-tool" "src/nat/Lib.kiss")
(load "../nat/Lib.kiss") (load "../nat/Lib.kiss")
(print "dicks")
(assert (BoolExpInterp.eval "true" [])) (assert (BoolExpInterp.eval "true" []))
(print "dicks")
(assert !(BoolExpInterp.eval "false" [])) (assert !(BoolExpInterp.eval "false" []))
(print "dicks")
(assert !(BoolExpInterp.eval "flag" [])) (assert !(BoolExpInterp.eval "flag" []))
(print "dicks")
(assert (BoolExpInterp.eval "flag" ["flag"])) (assert (BoolExpInterp.eval "flag" ["flag"]))
(print "dicks")
(assert !(BoolExpInterp.eval "(and flag false)" ["flag"])) (assert !(BoolExpInterp.eval "(and flag false)" ["flag"]))
(print "dicks")
(assert (BoolExpInterp.eval "(or flag otherFlag)" ["otherFlag"])) (assert (BoolExpInterp.eval "(or flag otherFlag)" ["otherFlag"]))
(print "dicks")
(let [archive (let [archive
(new Archive "src/test/example-archive") (new Archive "src/test/example-archive")
song1 song1
@@ -14,6 +23,7 @@
song2 song2
(dictGet archive.entries "song2")] (dictGet archive.entries "song2")]
(print "dicks")
(assert (hasComponent song1 Tags)) (assert (hasComponent song1 Tags))
(assert (hasComponent song2 Tags)) (assert (hasComponent song2 Tags))
(assert (componentsMatch song1 "(and Name Author)")) (assert (componentsMatch song1 "(and Name Author)"))
@@ -22,9 +32,17 @@
(assert !(tagsMatch archive song1 "(and song religious)")) (assert !(tagsMatch archive song1 "(and song religious)"))
(assert (tagsMatch archive song2 "(and song religious)")) (assert (tagsMatch archive song2 "(and song religious)"))
(assert !(tagsMatch archive song2 "(and song western)")) (assert !(tagsMatch archive song2 "(and song western)"))
(print "dicks")
(withWritableComponents archive song1 (withWritableComponents archive song1
[author Author [author Author
name Name] name Name]
(assert (= author "Rafael Krux")) (assert (= author "Rafael Krux"))
(assert (= name "Adventure")))) (assert (= name "Adventure"))))
(print "dicks")
(let [wikipedia (new MediaWikiSystem "https://en.wikipedia.org/w/api.php" null null 1)]
//~(wikipedia.queryProp ["Phoenix Wright"] ["images"])
0
)

View File

@@ -1,6 +1,8 @@
-D test -D test
-lib kiss -lib kiss
-lib nat-archive-tool -lib nat-archive-tool
-lib requests-externs
-cp test -cp test
--main test.TestMain --main test.TestMain
--interp --python bin/main.py
--cmd python bin/main.py

View File

@@ -5,21 +5,36 @@ import python.Dict;
import python.KwArgs; import python.KwArgs;
import requests_externs.Response; import requests_externs.Response;
typedef NativeRequestKwArgs = {
?headers:Dict<String, String>
}
typedef RequestKwArgs = { typedef RequestKwArgs = {
?headers:Map<String, String> ?headers:Map<String, String>
} }
@:pythonImport("requests") @:pythonImport("requests")
extern class NativeRequests { extern class NativeRequests {
public static function get(url:String, params:Dict<String, String>, ?kwArgs:KwArgs<RequestKwArgs>):Response; public static function get(url:String, params:Dict<String, String>, ?kwArgs:KwArgs<RequestKwArgs>):NativeResponse;
} }
class Requests { class Requests {
public static function get(url:String, params:Map<String, String>, ?kwArgs:KwArgs<RequestKwArgs>):Response { public static function get(url:String, params:Map<String, String>, ?kwArgs:KwArgs<RequestKwArgs>):NativeResponse {
return NativeRequests.get(url, mapToDict(params), kwArgs);
}
static function mapToDict(?map:Map<String,String>) {
if (map == null) return null;
var dict = new Dict<String, String>(); var dict = new Dict<String, String>();
for (param => value in params) { for (key => value in map) {
dict.set(param, value); dict.set(key, value);
} }
return NativeRequests.get(url, dict, kwArgs); return dict;
}
static function kwArgsToNativeKwArgs(kwArgs:RequestKwArgs) {
return {
headers: mapToDict(kwArgs.headers)
};
} }
} }

View File

@@ -1,4 +1,6 @@
package requests_externs; package requests_externs;
@:pythonImport("requests.Response") @:pythonImport("requests.Response")
extern class Response {} extern class NativeResponse {}
typedef Response = {};