Beispiel #1
0
    def _filter_records_fields(self, records_xml, output_fields):
        """Leaves in the records only fields that are necessary.
        All the other fields are removed from the records.

        @param records_xml: MARC XML containing all the information about the records
        @param output_fields: list of fields that should remain in the records

        @return: MARC XML with records containing only fields that are
        in output_fields list.
        """
        # Add 001/970 to the output fields. 970 is necessary for system number
        # extraction when exporting in aleph marc. When we add more formats,
        # we can add it optionally only when exporting aleph marc.
        output_fields.append("001")
        output_fields.append("970")

        records = bibrecord.create_records(records_xml)
        output_records = []

        for (record, status_code, list_of_errors) in records:
            record = self._filter_fields(record, output_fields)
            # do not return empty records
            if not self._is_record_empty(record):
                output_records.append(record)

        output_xml = bibrecord.print_recs(output_records)

        return output_xml
Beispiel #2
0
    def _filter_records_fields(self, records_xml, output_fields):
        """Leaves in the records only fields that are necessary.
        All the other fields are removed from the records.

        @param records_xml: MARC XML containing all the information about the records
        @param output_fields: list of fields that should remain in the records

        @return: MARC XML with records containing only fields that are
        in output_fields list.
        """
        # Add 001/970 to the output fields. 970 is necessary for system number
        # extraction when exporting in aleph marc. When we add more formats,
        # we can add it optionally only when exporting aleph marc.
        output_fields.append("001")
        output_fields.append("970")

        records = bibrecord.create_records(records_xml)
        output_records = []

        for (record, status_code, list_of_errors) in records:
            record = self._filter_fields(record, output_fields)
            # do not return empty records
            if not self._is_record_empty(record):
                output_records.append(record)

        output_xml = bibrecord.print_recs(output_records)

        return output_xml
Beispiel #3
0
def _save_records_xml(records, file_path, upload_mode, tag_list):
    """Saves records in a file in XML format

    @param records: list of records (record structures)
    @param file_path: path to the file where the XML will be saved."""

    output_file = None
    try:
        output_file = open(file_path, "w")

        if upload_mode == "-c":
            for record in records:
                for tag in record.keys():
                    if tag not in tag_list:
                        del(record[tag])

        records_xml = bibrecord.print_recs(records)

        output_file.write(records_xml)
    finally:
        if not output_file is None:
            output_file.close()
Beispiel #4
0
def main():
    cmdusage = """Usage: %s [options] <marcxmlfile>
    General options:
      -h, --help            Print this help.
      -V, --version         Print version information.
      -v, --verbose=LEVEL   Verbose level (from 0 to 9, default 0).
    Description: checks the validity of MARCXML file.
    """ % (sys.argv[0])

    verbose = 0
    badrecords = []
    listofrecs = []

    try:
        opts, args = getopt.getopt(sys.argv[1:], "hVv:", ["help", "version", "verbose="])
    except getopt.GetoptError:
        print(cmdusage)
        sys.exit(2)

    for opt in opts:
        if opt[0] in ("-V","--version"):
            print(__revision__)
            sys.exit(0)
        elif opt[0] in ("-h","--help"):
            sys.stderr.write(cmdusage)
            sys.exit(0)
        elif opt[0] in ("-v", "--verbose"):
            try:
                verbose = string.atoi(opt[1])
            except ValueError:
                print("[ERROR] verbose must be an integer.")
                sys.exit(2)

    try:
        xmlfile = args[0]
    except IndexError:
        sys.stderr.write(cmdusage)
        sys.exit(0)

    try:
        xmltext = open(xmlfile,'r').read()
    except IOError:
        print("[ERROR] File %s not found." % xmlfile)
        import sys
        sys.exit(1)

    listofrecs = create_records(xmltext, 0, 1)
    badr = filter((lambda x: x[1]==0), listofrecs)
    badrecords = map((lambda x:x[0]), badr)

    s = ''
    errors = []

    if xmltext and not listofrecs:
        print("[ERROR] No valid record detected.")
        sys.exit(1)

    if verbose:
        if verbose <= 3:
            errors.extend(map((lambda x:x[2]), listofrecs))
        else:
            s = print_recs(badrecords)
            errors.extend(map((lambda x:x[2]), listofrecs))
    else:
        if badrecords:
            print("[ERROR] Bad records detected.  For more information, increase verbosity.")
            print("\n[INFO] You may also want to run `xmllint %s' to help " \
                  "localise errors in the input file." % xmlfile)
            sys.exit(1)

    errors = [error for error in errors if error]

    if s or errors:
        if s:
            print(s)
        for error in errors:
            print("[ERROR]", error)
        print("[INFO] You may also want to run `xmllint %s' to help " \
              "localise errors in the input file." % xmlfile)
        sys.exit(1)
Beispiel #5
0
def main():
    """Execute script."""
    import sys

    cmdusage = """Usage: %s [options] <marcxmlfile>
    General options:
      -h, --help            Print this help.
      -v, --verbose=LEVEL   Verbose level (from 0 to 9, default 0).
    Description: checks the validity of MARCXML file.
    """ % (sys.argv[0])

    verbose = 0
    badrecords = []
    listofrecs = []

    try:
        opts, args = getopt.getopt(sys.argv[1:], "hv:", ["help", "verbose="])
    except getopt.GetoptError:
        print(cmdusage)
        sys.exit(2)

    for opt in opts:
        if opt[0] in ("-h", "--help"):
            sys.stderr.write(cmdusage)
            sys.exit(0)
        elif opt[0] in ("-v", "--verbose"):
            try:
                verbose = string.atoi(opt[1])
            except ValueError:
                print("[ERROR] verbose must be an integer.")
                sys.exit(2)

    try:
        xmlfile = args[0]
    except IndexError:
        sys.stderr.write(cmdusage)
        sys.exit(0)

    try:
        xmltext = open(xmlfile, 'r').read()
    except IOError:
        print("[ERROR] File %s not found." % xmlfile)
        import sys
        sys.exit(1)

    listofrecs = create_records(xmltext, 0, 1)
    badr = filter((lambda x: x[1] == 0), listofrecs)
    badrecords = map((lambda x: x[0]), badr)

    s = ''
    errors = []

    if xmltext and not listofrecs:
        print("[ERROR] No valid record detected.")
        sys.exit(1)

    if verbose:
        if verbose <= 3:
            errors.extend(map((lambda x: x[2]), listofrecs))
        else:
            s = print_recs(badrecords)
            errors.extend(map((lambda x: x[2]), listofrecs))
    else:
        if badrecords:
            print(
                "[ERROR] Bad records detected.  For more information, increase verbosity."
            )
            print("\n[INFO] You may also want to run `xmllint %s' to help "
                  "localise errors in the input file." % xmlfile)
            sys.exit(1)

    errors = [error for error in errors if error]

    if s or errors:
        if s:
            print(s)
        for error in errors:
            print("[ERROR]", error)
        print("[INFO] You may also want to run `xmllint %s' to help "
              "localise errors in the input file." % xmlfile)
        sys.exit(1)