diff --git a/projects/nat-archive-tool/cli.hxml b/projects/nat-archive-tool/cli.hxml index 87b80805..def0f7ac 100644 --- a/projects/nat-archive-tool/cli.hxml +++ b/projects/nat-archive-tool/cli.hxml @@ -2,6 +2,7 @@ -lib uuid -lib tink_macro -lib tink_json +-lib requests-externs -cp src --main nat.CLI --interp \ No newline at end of file diff --git a/projects/nat-archive-tool/extern-files/python/args.hxml b/projects/nat-archive-tool/extern-files/python/args.hxml new file mode 100644 index 00000000..c52a3eba --- /dev/null +++ b/projects/nat-archive-tool/extern-files/python/args.hxml @@ -0,0 +1 @@ +-lib requests-externs \ No newline at end of file diff --git a/projects/nat-archive-tool/extern-files/python/import.hx b/projects/nat-archive-tool/extern-files/python/import.hx new file mode 100644 index 00000000..22c82133 --- /dev/null +++ b/projects/nat-archive-tool/extern-files/python/import.hx @@ -0,0 +1,2 @@ +import requests_externs.Response; +import requests_externs.Requests; \ No newline at end of file diff --git a/projects/nat-archive-tool/extern-files/python/requirements.txt b/projects/nat-archive-tool/extern-files/python/requirements.txt new file mode 100644 index 00000000..4f5b8993 --- /dev/null +++ b/projects/nat-archive-tool/extern-files/python/requirements.txt @@ -0,0 +1 @@ +requests==2.26.0 \ No newline at end of file diff --git a/projects/nat-archive-tool/src/nat/BoolExpInterp.kiss b/projects/nat-archive-tool/src/nat/BoolExpInterp.kiss index e11f3cd1..2b2a05d2 100644 --- a/projects/nat-archive-tool/src/nat/BoolExpInterp.kiss +++ b/projects/nat-archive-tool/src/nat/BoolExpInterp.kiss @@ -1,9 +1,9 @@ (function eval [:String expStr :Array activeConditions] - (let [hscriptExp - (.parseString (new Parser) - (Prelude.convertToHScript expStr)) - interp - (new BoolExpInterp)] + (let [hscript (Prelude.convertToHScript expStr) + parser (new Parser) + hscriptExp (parser.parseString hscript) + interp (new BoolExpInterp)] + (doFor condition activeConditions (interp.variables.set condition true)) ?(interp.execute hscriptExp))) diff --git a/projects/nat-archive-tool/src/nat/systems/MediaWikiSystem.hx b/projects/nat-archive-tool/src/nat/systems/MediaWikiSystem.hx new file mode 100644 index 00000000..118f222e --- /dev/null +++ b/projects/nat-archive-tool/src/nat/systems/MediaWikiSystem.hx @@ -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 {} diff --git a/projects/nat-archive-tool/src/nat/systems/MediaWikiSystem.kiss b/projects/nat-archive-tool/src/nat/systems/MediaWikiSystem.kiss new file mode 100644 index 00000000..926b8d0c --- /dev/null +++ b/projects/nat-archive-tool/src/nat/systems/MediaWikiSystem.kiss @@ -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 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 titles :Array props] + (#extern Response python + (object + hxmlFile "extern-files/python/args.hxml" + importHxFile "extern-files/python/import.hx" + langProjectFile "extern-files/python/requirements.txt") + [:Array titles _ :Array props _ :String mediaWikiUrl _ :Float maxLag (or maxLag 1) :Map 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"]) \ No newline at end of file diff --git a/projects/nat-archive-tool/src/test/TestMain.hx b/projects/nat-archive-tool/src/test/TestMain.hx index b6abeb54..1f5fee23 100644 --- a/projects/nat-archive-tool/src/test/TestMain.hx +++ b/projects/nat-archive-tool/src/test/TestMain.hx @@ -6,6 +6,7 @@ import nat.BoolExpInterp; import nat.Archive; import nat.Lib; import nat.components.*; +import nat.systems.*; @:build(kiss.Kiss.build()) class TestMain {} diff --git a/projects/nat-archive-tool/src/test/TestMain.kiss b/projects/nat-archive-tool/src/test/TestMain.kiss index fadca024..e50b2990 100644 --- a/projects/nat-archive-tool/src/test/TestMain.kiss +++ b/projects/nat-archive-tool/src/test/TestMain.kiss @@ -1,12 +1,21 @@ // External programs can load Lib.kiss with (loadFrom "nat-archive-tool" "src/nat/Lib.kiss") (load "../nat/Lib.kiss") +(print "dicks") + (assert (BoolExpInterp.eval "true" [])) +(print "dicks") (assert !(BoolExpInterp.eval "false" [])) +(print "dicks") (assert !(BoolExpInterp.eval "flag" [])) +(print "dicks") (assert (BoolExpInterp.eval "flag" ["flag"])) +(print "dicks") (assert !(BoolExpInterp.eval "(and flag false)" ["flag"])) +(print "dicks") (assert (BoolExpInterp.eval "(or flag otherFlag)" ["otherFlag"])) + +(print "dicks") (let [archive (new Archive "src/test/example-archive") song1 @@ -14,6 +23,7 @@ song2 (dictGet archive.entries "song2")] +(print "dicks") (assert (hasComponent song1 Tags)) (assert (hasComponent song2 Tags)) (assert (componentsMatch song1 "(and Name Author)")) @@ -22,9 +32,17 @@ (assert !(tagsMatch archive song1 "(and song religious)")) (assert (tagsMatch archive song2 "(and song religious)")) (assert !(tagsMatch archive song2 "(and song western)")) +(print "dicks") (withWritableComponents archive song1 [author Author name Name] (assert (= author "Rafael Krux")) (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 + ) \ No newline at end of file diff --git a/projects/nat-archive-tool/test.hxml b/projects/nat-archive-tool/test.hxml index a0d212fc..c4649462 100644 --- a/projects/nat-archive-tool/test.hxml +++ b/projects/nat-archive-tool/test.hxml @@ -1,6 +1,8 @@ -D test -lib kiss -lib nat-archive-tool +-lib requests-externs -cp test --main test.TestMain ---interp \ No newline at end of file +--python bin/main.py +--cmd python bin/main.py \ No newline at end of file diff --git a/projects/requests-externs/src/requests_externs/Requests.hx b/projects/requests-externs/src/requests_externs/Requests.hx index 97491a15..cee784b5 100644 --- a/projects/requests-externs/src/requests_externs/Requests.hx +++ b/projects/requests-externs/src/requests_externs/Requests.hx @@ -5,21 +5,36 @@ import python.Dict; import python.KwArgs; import requests_externs.Response; +typedef NativeRequestKwArgs = { + ?headers:Dict +} + typedef RequestKwArgs = { ?headers:Map } @:pythonImport("requests") extern class NativeRequests { - public static function get(url:String, params:Dict, ?kwArgs:KwArgs):Response; + public static function get(url:String, params:Dict, ?kwArgs:KwArgs):NativeResponse; } class Requests { - public static function get(url:String, params:Map, ?kwArgs:KwArgs):Response { + public static function get(url:String, params:Map, ?kwArgs:KwArgs):NativeResponse { + return NativeRequests.get(url, mapToDict(params), kwArgs); + } + + static function mapToDict(?map:Map) { + if (map == null) return null; var dict = new Dict(); - for (param => value in params) { - dict.set(param, value); + for (key => value in map) { + dict.set(key, value); } - return NativeRequests.get(url, dict, kwArgs); + return dict; + } + + static function kwArgsToNativeKwArgs(kwArgs:RequestKwArgs) { + return { + headers: mapToDict(kwArgs.headers) + }; } } diff --git a/projects/requests-externs/src/requests_externs/Response.hx b/projects/requests-externs/src/requests_externs/Response.hx index fa6b7845..635d4e18 100644 --- a/projects/requests-externs/src/requests_externs/Response.hx +++ b/projects/requests-externs/src/requests_externs/Response.hx @@ -1,4 +1,6 @@ package requests_externs; @:pythonImport("requests.Response") -extern class Response {} +extern class NativeResponse {} + +typedef Response = {};