From d786e86ba367a022696205e1cbdea164795c973e Mon Sep 17 00:00:00 2001 From: tholzheim Date: Sat, 4 May 2024 12:04:46 +0200 Subject: [PATCH] updated scholia query parser --- tests/test_import_queries.py | 47 ++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/tests/test_import_queries.py b/tests/test_import_queries.py index a8d5e50..0c66fd5 100644 --- a/tests/test_import_queries.py +++ b/tests/test_import_queries.py @@ -1,35 +1,40 @@ import unittest +import urllib.request +import zipfile from pathlib import Path import yaml from nqapi.query import NamedQuery +from tempfile import TemporaryDirectory class MyTestCase(unittest.TestCase): @unittest.skip("Only execute if the queries should be regenerated") def test_something(self): - path = Path("/home/holzheim/Documents/projects/scholia/scholia/app/templates") - exclude = [ - "author_topics", - "event_proceedings", - "publisher_editors", - "works_topics", - ] - for entry in path.iterdir(): - if entry.is_file(): - if entry.suffix == ".sparql": - name = entry.stem - if name in exclude: - continue - with open(entry, mode="r") as f: - query_str = f.read() - query = NamedQuery(name=name, query=query_str) - target = Path(__file__).parent.parent.joinpath( - "nqapi", "resources", "queries", f"{name}.yaml" - ) - with open(target, mode="w") as f: - f.write(yaml.dump({"name": name, "query": query_str})) + with TemporaryDirectory() as tmpdir: + git_archive = "https://github.com/WDscholia/scholia/archive/refs/heads/master.zip" + path = Path(tmpdir) + zip_file = path.joinpath("master.zip") + urllib.request.urlretrieve(git_archive, path.joinpath("master.zip")) + with zipfile.ZipFile(zip_file, 'r') as zip_ref: + zip_ref.extractall(tmpdir) + query_path = path.joinpath("scholia-master", "scholia/app/templates") + exclude = ["author_topics", "event_proceedings", "publisher_editors", "works_topics", ] + for entry in query_path.iterdir(): + if entry.is_file(): + if entry.suffix == ".sparql": + name = entry.stem + if name in exclude: + continue + with open(entry, mode="r") as f: + query_str = f.read() + query = NamedQuery(name=name, query=query_str) + target = Path(__file__).parent.parent.joinpath( + "nqapi", "resources", "queries", f"{name}.yaml" + ) + with open(target, mode="w") as f: + f.write(yaml.dump({"name": name, "query": query_str})) if __name__ == "__main__":