diff --git a/pyproject.toml b/pyproject.toml index 9a2fc59..54d6e88 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,6 +25,7 @@ test = [ "requests_mock", "python-dateutil", "pytest-coverage", + "xmldiff" ] [tool.setuptools] diff --git a/src/zbmath_rest2oai/.getWithSwagger.py.swp b/src/zbmath_rest2oai/.getWithSwagger.py.swp deleted file mode 100644 index e28ae8c..0000000 Binary files a/src/zbmath_rest2oai/.getWithSwagger.py.swp and /dev/null differ diff --git a/src/zbmath_rest2oai/getWithSwagger.py b/src/zbmath_rest2oai/getWithSwagger.py index 30a65c5..087dc2e 100644 --- a/src/zbmath_rest2oai/getWithSwagger.py +++ b/src/zbmath_rest2oai/getWithSwagger.py @@ -1,31 +1,12 @@ +import os.path + import swagger_client from zbmath_rest2oai.xml_writer import create_document import json -with open('./output mapping - Copy.json') as f: +with open(os.path.join(os.path.dirname(__file__), './output mapping - Copy.json')) as f: d = json.load(f) -api_instance = swagger_client.DocumentApi(swagger_client.ApiClient()) -res = api_instance.get_document_by_zbmath_id_document_id_get(id="6383667") -doc = res.result -root_doc = create_document(doc) - -ron = root_doc.createElement("oai_zb_preview:zbmath") -ron.setAttributeNS( - "xmls", - "xmlns:oai_zb_preview", - "https://zbmath.org/OAI/2.0/oai_zb_preview/", -) -ron.setAttributeNS( - "xmls", - "xmlns:zbmath", - "https://zbmath.org/zbmath/elements/1.0/", -) -ron.setAttributeNS( - "xmls", - "xmlns:xsi", - "http://www.w3.org/2001/XMLSchema-instance", -) def append_text_child(xmld, parent, name, value): @@ -46,7 +27,7 @@ def append_text_child(xmld, parent, name, value): return parent -def func_get_doc_to_xml(obj, xml): +def func_get_doc_to_xml(obj, xml, root_doc): swagger_client_dicttype_list = [ swagger_client.models.all_ofzbmath_api_data_models_display_documents_result_id_result.AllOfzbmathApiDataModelsDisplayDocumentsResultIDResult, swagger_client.models.all_of_document_contributors.AllOfDocumentContributors, @@ -116,7 +97,7 @@ def func_get_doc_to_xml(obj, xml): print(0) elif type(obj[i]) in all_iter_list: - func_get_doc_to_xml(obj[i], xml) + func_get_doc_to_xml(obj[i], xml, root_doc) if type(obj) is dict: new_obj = {} @@ -240,50 +221,39 @@ def func_get_doc_to_xml(obj, xml): else: xml = append_text_child(root_doc, xml, key, "") - func_get_doc_to_xml(new_obj[key], xml) + func_get_doc_to_xml(new_obj[key], xml, root_doc) return xml -final_xml = func_get_doc_to_xml(res.result, ron) - -l = final_xml.childNodes - -# print([node.nodeName for node in l]) -# print(final_xml.getElementsByTagName('zbmath:references')) -#### - -## STRATEGY TO SOLVE the situation with inner indentation. IF elem in Happening node, append child and recurs func ELSE, happend node normally -## -# list_nodes_remove = [] -# for i in range(len(l)): -# if l[i].localName in ['_contributors','_authors', '_aliases', '_checked', '_author_references', '_reviewer', '_editors', '_author_codes']: -# list_nodes_remove.append(l[i]) - -# for node in list_nodes_remove: -# final_xml.removeChild(node) +def get_final_xml(de: str): + api_instance = swagger_client.DocumentApi(swagger_client.ApiClient()) + res = api_instance.get_document_by_zbmath_id_document_id_get(id=de) + doc = res.result + root_doc = create_document(doc) + + ron = root_doc.createElement("oai_zb_preview:zbmath") + ron.setAttributeNS( + "xmls", + "xmlns:oai_zb_preview", + "https://zbmath.org/OAI/2.0/oai_zb_preview/", + ) + ron.setAttributeNS( + "xmls", + "xmlns:zbmath", + "https://zbmath.org/zbmath/elements/1.0/", + ) + ron.setAttributeNS( + "xmls", + "xmlns:xsi", + "http://www.w3.org/2001/XMLSchema-instance", + ) + return func_get_doc_to_xml( + res.result, + ron, + root_doc + ) + + +final_xml = get_final_xml("6383667") print(final_xml.parentNode.parentNode.toprettyxml()) - -### PART OF THE CODE TO SORT THE FINAL XML -ron1 = root_doc.createElement("oai_zb_preview:zbmath") -ron1.setAttributeNS( - "xmls", - "xmlns:oai_zb_preview", - "https://zbmath.org/OAI/2.0/oai_zb_preview/", -) -ron1.setAttributeNS( - "xmls", - "xmlns:zbmath", - "https://zbmath.org/zbmath/elements/1.0/", -) -ron1.setAttributeNS( - "xmls", - "xmlns:xsi", - "http://www.w3.org/2001/XMLSchema-instance", -) - -# sorted_dict = dict(sorted(dict(zip([i.localName for i in l],l)).items())) -# for key in sorted_dict.keys(): -# ron1.appendChild(sorted_dict[key]) - -# print(ron1.toprettyxml()) diff --git a/tests/data/reference.xml b/tests/data/reference.xml new file mode 100644 index 0000000..997116c --- /dev/null +++ b/tests/data/reference.xml @@ -0,0 +1,172 @@ + + Maynard, James + + maynard.james + + + 11N05 + 11N36 + + 6383667 + Small gaps between primes + j + 10.4007/annals.2015.181.1.7 + + prime number + small gap + sieve method + \(k\)-tuples conjecture + admissible set + Selberg sieve + symmetric polynomial + symmetric matrix + + English + 383-413 + 2015 + Ann. Math. (2) 181, No. 1, 383-413 (2015). + Maynard, James + 2015-01-06T13:15:02Z + 1306.11073 + + English + Jonas Šiaulys (Vilnius) + The prime \(k\)-tuples and small gaps between prime numbers are considered. Using a + refinement of the Goldston-Pintz-Yildirim sieve method [\textit{D. A. Goldston} et al., Ann. Math. (2) 170, + No. 2, 819--862 (2009; Zbl 1207.11096)] the author proves, for instance, the following estimates + \[ + + \liminf_{n\to\infty}\,(p_{n+m}-p_n)\ll m^3\text{{e}}^{4m}, \quad \liminf_{n\to\infty}\,(p_{n+1}-p_n)\leq 600 + + \] + with an absolute constant in sign \(\ll\). Here \(m\) is a natural number, and \(p_{\,l}\) denote the + \(l\)-th prime number. + + review + 11807 + siaulys.jonas + + + Princeton University, Mathematics Department, Princeton, NJ + Annals of Mathematics. Second Series + + + + P. D. T. A. Elliott and H. Halberstam, ''A conjecture in prime number theory,'' in Symposia + Mathematica, Vol. IV, London: Academic Press, 1970, pp. 59-72. + + 3377327 + + 11N35 + 11N13 + + + + J. Friedlander and A. Granville, ''Limitations to the equi-distribution of primes. I,'' Ann. of + Math., vol. 129, iss. 2, pp. 363-382, 1989. + + 4097497 + + 11N05 + 11N13 + 11N35 + + + + D. A. Goldston, S. W. Graham, J. Pintz, and C. Y. Yildirim, ''Small gaps between products of + two primes,'' Proc. Lond. Math. Soc., vol. 98, iss. 3, pp. 741-774, 2009. + + 5551831 + + 11N25 + 11N36 + + + + D. A. Goldston, J. Pintz, and C. Y. Yildirim, ''Primes in tuples. III. On the difference + \(p_{n+\nu}-p_n\),'' Funct. Approx. Comment. Math., vol. 35, pp. 79-89, 2006. + + 5135166 + + 11N05 + 11N13 + + + + D. A. Goldston, J. Pintz, and C. Y. Yildirim, ''Primes in tuples. I,'' Ann. of Math., vol. 170, + iss. 2, pp. 819-862, 2009. + + 5610431 + + 11N05 + 11N36 + 11N13 + + + + D. A. Goldston and C. Y. Yildirim, ''Higher correlations of divisor sums related to primes. + III. Small gaps between primes,'' Proc. Lond. Math. Soc., vol. 95, iss. 3, pp. 653-686, 2007. + + 5170700 + + 11N05 + 11N37 + + + + D. H. J. Polymath, New equidistribution estimates of Zhang type, and bounded gaps between + primes. + + 6587992 + + 11N35 + 11N05 + + + + A. Selberg, Collected Papers. Vol. II, New York: Springer-Verlag, 1991. + 195021 + + 11-03 + 01A75 + 32-03 + 11M06 + 11M41 + 11N35 + 11N36 + 11F72 + 32N05 + 32N15 + + + + Y. Zhang, ''Bounded gaps between primes,'' Ann. of Math., vol. 179, iss. 3, pp. 1121-1174, + 2014. + + 6302171 + + 11N05 + 11N13 + 11N35 + 11N36 + 11L07 + + + + + https://arxiv.org/abs/1311.4600 + + Content generated by zbMATH Open, such as reviews, + classifications, software, or author disambiguation data, + are distributed under CC-BY-SA 4.0. This defines the license for the + whole dataset, which also contains non-copyrighted bibliographic + metadata and reference data derived from I4OC (CC0). Note that the API + only provides a subset of the data in the zbMATH Open Web interface. In + several cases, third-party information, such as abstracts, cannot be + made available under a suitable license through the API. In those cases, + we replaced the data with the string 'zbMATH Open Web Interface contents + unavailable due to conflicting licenses.' + + \ No newline at end of file diff --git a/tests/test_oai_preview.py b/tests/test_oai_preview.py new file mode 100644 index 0000000..f322ccf --- /dev/null +++ b/tests/test_oai_preview.py @@ -0,0 +1,21 @@ +import os +import unittest +from xml.dom.minidom import parse +from xmldiff import main +from zbmath_rest2oai import getWithSwagger + + +class MyTestCase(unittest.TestCase): + def test_something(self): + real = getWithSwagger.get_final_xml("6383667") + real_string = real.parentNode.parentNode.toprettyxml() + ref_location = os.path.join(os.path.dirname(__file__), './data/reference.xml') + with open(ref_location) as f: + dom = parse(f) + expected_string = dom.toprettyxml() + diff = main.diff_texts(expected_string, real_string) + self.assertEqual(len(diff), 0) # add assertion here + + +if __name__ == '__main__': + unittest.main()