Example #1
0
def test_open_save():
    fname = "tests/data/USGS_ASC_PolarBears_FGDC.xml"
    md = xml_utils.XMLRecord(fname)
    assert md.metadata.idinfo.citation.citeinfo.geoform.text == 'Tabular Digital Data'
    md.metadata.idinfo.citation.citeinfo.geoform.text = 'testing'
    md.save()
    md = xml_utils.XMLRecord(fname)
    new_geoform = md.metadata.idinfo.citation.citeinfo.geoform.text
    md.metadata.idinfo.citation.citeinfo.geoform.text = 'Tabular Digital Data'
    md.save()

    assert new_geoform == 'testing'
Example #2
0
    def generate_review_doc(self):
        if self.cur_fname:
            out_fname = self.cur_fname[:-4] + '_REVIEW.docx'

            if self.metadata_root.schema == 'bdp':
                which = 'bdp'
            else:
                which = 'fgdc'

            if time.time() - self.last_updated > 4:
                msg = "Would you like to save the current file before continuing?"
                alert = QDialog()
                self.last_updated = time.time()
                confirm = QMessageBox.question(self, "File save", msg,
                                               QMessageBox.Yes | QMessageBox.No
                                               | QMessageBox.Cancel)
                if confirm == QMessageBox.Yes:
                    self.save_file()
                elif confirm == QMessageBox.Cancel:
                    return
            try:
                cur_content = xml_utils.XMLRecord(self.cur_fname)
                review_utils.generate_review_report(cur_content, out_fname,
                                                    which=which)

                import subprocess
                os.startfile('"{}"'.format(out_fname))
                msg = 'Review document available at: {}'.format(out_fname)
                msg += '\n\nReview document now opening in default application...'
                QMessageBox.information(self, "Review finished", msg)
            except BaseException:
                import traceback
                msg = "Problem encountered generating review document:\n{}".format(traceback.format_exc())
                QMessageBox.warning(self, "Problem encountered", msg)
Example #3
0
def md_to_docx(xml_fname, docx_fname):
    ''' Converts FGDC xml metadata to Microsoft Word docx '''
    document = Document()
    #Set narrow margins:
    sections = document.sections
    section = sections[0]
    section.left_margin = Inches(0.5)
    section.right_margin = Inches(0.5)
    section.top_margin = Inches(0.8)
    section.bottom_margin = Inches(0.8)

    DOCX = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
    #This prevents Word from ignoring spelling errors:
    element = document.settings.element.find(DOCX + 'proofState')
    element.attrib[DOCX + 'grammar'] = 'dirty'
    element.attrib[DOCX + 'spelling'] = 'dirty'

    #My tweaked custom styles, in my tweaked fort-pymwizard (mmf branch)
    review_utils._load_mmf_styles(document)

    md = xml_utils.XMLRecord(xml_fname)
    #Set the citation title as the document title
    md_title = md.metadata.idinfo.citation.citeinfo.title.text
    title1 = document.add_heading(md_title, level=1)
    title1.style = document.styles['fgdc title']
    title2 = document.add_heading('Metadata:', level=2)
    title2.style = document.styles['fgdc heading 2']

    #Populate a 'table of contents' (of sorts) at beginning of doc
    for child in md.metadata.children:
        long_name = _get_longname(child.tag)
        #If there is no long name, skip
        if long_name != child.tag:
            link = document.add_paragraph(style='fgdc link')
            add_hyperlink(link,
                          long_name.replace(' ', '_'),
                          long_name,
                          is_external=False)
            link.paragraph_format.left_indent = Inches(0.25)
            link.paragraph_format.line_spacing = 1

    #Set a heading for each top child, then recursively populate with all children
    for child in md.metadata.children:
        long_name = _get_longname(child.tag)
        #Don't make a heading if there is no long name
        if long_name != child.tag:
            #Separate sections with a vertical line:
            document.add_paragraph('_' * 72, style='fgdc bar')
            #Add section heading and link to toc:
            section_title = document.add_heading(long_name + ':', level=3)
            add_bookmark(section_title, long_name.replace(' ', '_'))
            section_title.style = document.styles['fgdc heading 3']
            section_title.paragraph_format.space_after = Inches(.15)
        #Add remaining tags and content
        _add_abbrev_child_content(document, child, indent=0.1)

    document.save(docx_fname)
Example #4
0
def test_find_replace():
    fname = "tests/data/USGS_ASC_PolarBears_FGDC.xml"
    md = xml_utils.XMLRecord(fname)
    assert len(md.metadata.find_string('asc', ignorecase=True)) == 7
    assert len(md.metadata.find_string('asc', ignorecase=False)) == 0
    assert len(md.metadata.find_string('ASC')) == 7

    assert md.metadata.replace_string('Polar Bear', "Honey Badger",
                                      deep=False) == 0
    assert md.metadata.replace_string('Polar Bear', "Honey Badger") == 4
    assert 'Honey Badger' in md.metadata.idinfo.citation.citeinfo.title.text
    assert md.metadata.idinfo.descript.abstract.replace_string(
        'polar', 'big white') == 4
    assert md.metadata.idinfo.descript.abstract.text.count('polar') == 0

    md = xml_utils.XMLRecord(fname)
    assert md.metadata.idinfo.descript.abstract.replace_string(
        'polar', 'big white', maxreplace=2) == 2
    assert md.metadata.idinfo.descript.abstract.text.count('polar') == 2
Example #5
0
def test_find_replace():
    fname = "tests/data/USGS_ASC_PolarBears_FGDC.xml"
    md = xml_utils.XMLRecord(fname)
    assert len(md.metadata.find_string("asc", ignorecase=True)) == 7
    assert len(md.metadata.find_string("asc", ignorecase=False)) == 0
    assert len(md.metadata.find_string("ASC")) == 7

    assert md.metadata.replace_string("Polar Bear", "Honey Badger",
                                      deep=False) == 0
    assert md.metadata.replace_string("Polar Bear", "Honey Badger") == 4
    assert "Honey Badger" in md.metadata.idinfo.citation.citeinfo.title.text
    assert (md.metadata.idinfo.descript.abstract.replace_string(
        "polar", "big white") == 4)
    assert md.metadata.idinfo.descript.abstract.text.count("polar") == 0

    md = xml_utils.XMLRecord(fname)
    assert (md.metadata.idinfo.descript.abstract.replace_string(
        "polar", "big white", maxreplace=2) == 2)
    assert md.metadata.idinfo.descript.abstract.text.count("polar") == 2
Example #6
0
    def generate_review_doc(self):
        if self.cur_fname:
            out_fname = self.cur_fname[:-4] + "_REVIEW.docx"

            if self.metadata_root.schema == "bdp":
                which = "bdp"
            else:
                which = "fgdc"

            if time.time() - self.last_updated > 4:
                msg = "Would you like to save the current file before continuing?"
                #alert = QDialog()
                self.last_updated = time.time()
                confirm = QMessageBox.question(
                    self,
                    "File save",
                    msg,
                    QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel,
                )
                if confirm == QMessageBox.Yes:
                    self.save_file()
                elif confirm == QMessageBox.Cancel:
                    return
            try:
                cur_content = xml_utils.XMLRecord(self.cur_fname)
                review_utils.generate_review_report(cur_content,
                                                    out_fname,
                                                    which=which)

                import os, sys, subprocess

                def open_file(filename):
                    if platform.system() == "Windows":
                        os.startfile('"{}"'.format(filename))
                    elif sys.platform == "Darwin":
                        opener = "open"
                        subprocess.call([opener, filename])

                open_file(out_fname)

                msg = "Review document available at: {}".format(out_fname)
                msg += "\n\nReview document now opening in default application..."
                QMessageBox.information(self, "Review finished", msg)
            except BaseException:
                import traceback

                msg = "Problem encountered generating review document:\n{}".format(
                    traceback.format_exc())
                QMessageBox.warning(self, "Problem encountered", msg)
Example #7
0
def test_url_read():
    url = "https://www2.usgs.gov/datamanagement/documents/USGS_ASC_PolarBears_FGDC.xml"
    md = xml_utils.XMLRecord(url)
    assert md.metadata.idinfo.citation.citeinfo.geoform.text == 'Tabular Digital Data'
Example #8
0
def test_url_read():
    url = "https://www.sciencebase.gov/catalog/file/get/57d8779de4b090824ff9acfb?f=__disk__e1%2F7c%2Fa7%2Fe17ca734bf9ffd9ae0abeaaf0da208d457f72b3c&allowOpen=true"
    md = xml_utils.XMLRecord(url)
    assert md.metadata.idinfo.citation.citeinfo.geoform.text == "Raster Digital Data Set"
Example #9
0
    def generate_review_doc(self):
        if self.cur_fname:
            out_fname = self.cur_fname[:-4] + "_REVIEW.docx"

            if self.metadata_root.schema == "bdp":
                which = "bdp"
            else:
                which = "fgdc"

            if time.time() - self.last_updated > 4:
                msg = "Would you like to save the current file before continuing?"
                exists_msg = "File already exists, would you like to overwrite it? Selecting 'No' "
                exists_msg += "will allow you to SaveAs."
                alert = QDialog()
                self.last_updated = time.time()
                confirm = QMessageBox.question(
                    self,
                    "File save",
                    msg,
                    QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel,
                )
                if confirm == QMessageBox.Yes:
                    self.save_as()

                elif confirm == QMessageBox.Cancel:
                    return
            try:
                cur_content = xml_utils.XMLRecord(self.cur_fname)
                import os
                if os.path.exists(out_fname):
                    confirm2 = QMessageBox.question(self,
                        "File Overwrite",
                        exists_msg,
                        QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel ,
                    )
                    if confirm2 == QMessageBox.Yes:
                        self.save_file()
                    elif confirm2 == QMessageBox.No:
                        out_fname = QFileDialog.getSaveFileName(
                            self, "Save As", out_fname, filter="Document (*.docx)"
                        )[0]
                    elif confirm2 == QMessageBox.Cancel:
                        return

                review_utils.generate_review_report(cur_content, out_fname, which=which)

                import os, sys, subprocess

                def open_file(filename):
                    if sys.platform == "win32":
                        os.startfile('"{}"'.format(filename))
                    elif sys.platform == "darwin":
                        opener = "open"
                        subprocess.call([opener, filename])

                open_file(out_fname)

                msg = "Review document available at: {}".format(out_fname)
                msg += "\n\nReview document now opening in default application..."
                QMessageBox.information(self, "Review finished", msg)
            except BaseException:
                import traceback

                msg = "Problem encountered generating review document:\n{}".format(
                    traceback.format_exc()
                )
                QMessageBox.warning(self, "Problem encountered", msg)