esturdivant-usgs / science-base-automation

Automating large USGS ScienceBase data releases
4 stars 2 forks source link

Find and replace didn't work (find_and_replace, **dr_doi** value not replaced) #63

Closed esturdivant-usgs closed 5 years ago

esturdivant-usgs commented 5 years ago

The HTML versions of the XMLs viewed from SB still have dr_doi placeholders. The find_and_replace dictionary should have been created, but somewhere along the line it didn't work.

Here are the

find_and_replace = {'**dr_doi**': dr_doi,
    'xxx-ofr doi-***': 'xxx-ofr doi-***',
    # 'http:': 'https:',
    'dx.doi.org': 'doi.org'
    }
if update_XML:
     update_xml(xml_file, new_values, verbose=verbose) # new_values['pubdate']
def update_xml(xml_file, new_values, verbose=False):
#####
    if "find_and_replace" in new_values:
        find_and_replace_from_dict(xml_file, new_values['find_and_replace'])
def find_and_replace_from_dict(fname, find_dict):
    # Takes dictionary of {find_value: replace_value}
    ct = 0
    with io.open(fname, 'r', encoding='utf-8') as f:
        s = f.read()
    # Iterate through find:replace pairs
    for fstr, rstr in find_dict.items():
        s = s.replace(fstr, rstr)
    with io.open(fname, 'w', encoding='utf-8') as f:
        f.write(s)
    return(fname)
esturdivant-usgs commented 5 years ago

find_and_replace_from_dict() worked in testing. update_xml() did not work in testing. When I ran it line by line, it did update the XML.


find_and_replace_from_dict does not update the tree. The tree is the last thing to get saved as the XML file.


Moved the call to find_and_replace_from_dict() to the very end of update_xml().