Skip to content

Commit

Permalink
references are better (perhaps fully) curated, informations directly …
Browse files Browse the repository at this point in the history
…appended from the json format to the xml format
  • Loading branch information
Mazztok45 committed Dec 5, 2023
1 parent ca49c75 commit 2ab6965
Showing 1 changed file with 61 additions and 62 deletions.
123 changes: 61 additions & 62 deletions src/zbmath_rest2oai/getWithSwagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
doc = res.result
xmld = create_document(doc)

#print(res.result)

ron = xmld.createElement("oai_zb_preview:zbmath")
ron.setAttributeNS(
Expand Down Expand Up @@ -35,70 +34,70 @@ def append_text_child(xmld, parent, name, value):
:param name:
:param value:
"""
x_elem = xmld.createElement(f"zbmath:{name}")
string_name = name
if "zbmath:" not in name:
string_name = f"zbmath:{name}"
x_elem = xmld.createElement(string_name)
text = xmld.createTextNode(str(value))
x_elem.appendChild(text)
parent.appendChild(x_elem)
return parent


final_xml = append_text_child(xmld, ron, "id", res.result.id)
#print(final_xml)

def func_json_keys(json_part, l):

if json_part is None:
print(json_part)
return l

elif str(json_part).startswith('{'):
try:
print('test_dict')
index = json_part.__dict__.keys()
json_part = json_part.__dict__
except:
print('test1')
index = json_part

if type(json_part) == dict:
for elem in index:
#print('call func')
if type(json_part[elem]) in [str, int, float, None]:
l.append([str(elem), json_part[elem]])
#elif json_part[elem] == list():
#continue

elif str(json_part[elem]).startswith('{'):
func_json_keys(json_part[elem], l)
else:
print('UNKNOWN TYPE')
print(type(json_part[elem]))
print(json_part[elem])
func_json_keys(json_part[elem], l)
return l

elif str(json_part).startswith('['):
try:
print('test_list')
#json_part = str(json_part).split(',')
except:
print('test_list_except')

for i in range(len(json_part)):
# print('call func')
if type(json_part[i]) in [str, int, float, None]:
l.append(json_part[i])
elif str(json_part[i]).startswith('{'):
func_json_keys(json_part[i], l)
return l

else:
return l

l0=[]
infos_list = func_json_keys(res, l0)
#print(infos_list)
for j in infos_list:
final_xml = append_text_child(xmld, final_xml, j[0], j[1])

print(final_xml.toprettyxml())
def func_get_doc_to_xml(obj, xml):
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,
swagger_client.models.zbmath_api_data_models_display_documents_submodels_author.ZbmathApiDataModelsDisplayDocumentsSubmodelsAuthor,
swagger_client.models.editorial_contribution.EditorialContribution,
swagger_client.models.all_of_editorial_contribution_reviewer.AllOfEditorialContributionReviewer,
swagger_client.models.all_of_document_language.AllOfDocumentLanguage,
swagger_client.models.link.Link,
swagger_client.models.msc.MSC,
swagger_client.models.reference.Reference,
swagger_client.models.all_of_reference_zbmath.AllOfReferenceZbmath,
swagger_client.models.all_of_document_source.AllOfDocumentSource,
swagger_client.models.series.Series,
swagger_client.models.all_of_document_title.AllOfDocumentTitle,
swagger_client.models.zbmath_api_data_models_display_documents_submodels_issn.ZbmathApiDataModelsDisplayDocumentsSubmodelsISSN]

all_iter_list = [list, dict]
all_iter_list.extend(swagger_client_dicttype_list)

if type(obj) in swagger_client_dicttype_list:
obj = obj.__dict__

if type(obj) == list:
for i in range(len(obj)):
if obj[i]==[]:
xml = append_text_child(xmld, xml, xml.lastChild.nodeName, 'missing')
elif obj[i] is None:
xml = append_text_child(xmld, xml, xml.lastChild.nodeName, 'missing')
elif type(obj[i]) in [str,int]:
xml = append_text_child(xmld, xml, xml.lastChild.nodeName, obj[i])
elif type(obj[i]) in all_iter_list:
func_get_doc_to_xml(obj[i], xml)
else:
print("WARNING")
print(obj[i])


if type(obj) == dict:
for key in obj.keys():
if obj[key]==[]:
xml = append_text_child(xmld, xml, key, 'missing')
elif obj[key] is None:
xml = append_text_child(xmld, xml, key, 'missing')
elif type(obj[key]) in [str,int]:
xml = append_text_child(xmld, xml, key, obj[key])
elif type(obj[key]) in all_iter_list:
xml = append_text_child(xmld, xml, key, "")
func_get_doc_to_xml(obj[key], xml)
return xml

print(func_get_doc_to_xml(res.result, ron).toprettyxml())






0 comments on commit 2ab6965

Please sign in to comment.