def get_xml_from_textmarc(recid, textmarc_record, uid=None): """ Convert textmarc to marcxml and return the result of the conversion @param recid: id of the record that is being converted @type: int @param textmarc_record: record content in textmarc format @type: string @return: dictionary with the following keys: * resultMsg: message describing conversion status * resultXML: xml resulting from conversion * parse_error: in case of error, a description of it @rtype: dict """ response = {} # Let's remove empty lines textmarc_record = os.linesep.join( [s for s in textmarc_record.splitlines() if s]) # Create temp file with textmarc to be converted by textmarc2xmlmarc (file_descriptor, file_name) = tempfile.mkstemp() f = os.fdopen(file_descriptor, "w") # If there is a cache file, add the controlfields if cache_exists(recid, uid): record = get_cache_contents(recid, uid)[2] for tag in record: if tag.startswith("00") and tag != "001": # It is a controlfield f.write( "%09d %s %s\n" % (recid, tag + "__", record_get_field_value(record, tag))) # Write content appending sysno at beginning for line in textmarc_record.splitlines(): f.write("%09d %s\n" % (recid, re.sub(r"\s+", " ", line.strip()))) f.close() old_stdout = sys.stdout try: # Redirect output, transform, restore old references new_stdout = StringIO() sys.stdout = new_stdout try: transform_file(file_name) response['resultMsg'] = 'textmarc_parsing_success' response['resultXML'] = new_stdout.getvalue() except ParseError, e: # Something went wrong, notify user response['resultXML'] = "" response['resultMsg'] = 'textmarc_parsing_error' response['parse_error'] = [ e.lineno, " ".join(e.linecontent.split()[1:]), e.message ] finally: sys.stdout = old_stdout return response
def get_xml_from_textmarc(recid, textmarc_record, uid=None): """ Convert textmarc to marcxml and return the result of the conversion @param recid: id of the record that is being converted @type: int @param textmarc_record: record content in textmarc format @type: string @return: dictionary with the following keys: * resultMsg: message describing conversion status * resultXML: xml resulting from conversion * parse_error: in case of error, a description of it @rtype: dict """ response = {} # Let's remove empty lines textmarc_record = os.linesep.join([s for s in textmarc_record.splitlines() if s]) # Create temp file with textmarc to be converted by textmarc2xmlmarc (file_descriptor, file_name) = tempfile.mkstemp() f = os.fdopen(file_descriptor, "w") # If there is a cache file, add the controlfields if cache_exists(recid, uid): record = get_cache_contents(recid, uid)[2] for tag in record: if tag.startswith("00") and tag != "001": # It is a controlfield f.write("%09d %s %s\n" % (recid, tag + "__", record_get_field_value(record, tag))) # Write content appending sysno at beginning for line in textmarc_record.splitlines(): f.write("%09d %s\n" % (recid, re.sub(r"\s+", " ", line.strip()))) f.close() old_stdout = sys.stdout try: # Redirect output, transform, restore old references new_stdout = StringIO() sys.stdout = new_stdout try: transform_file(file_name) response['resultMsg'] = 'textmarc_parsing_success' response['resultXML'] = new_stdout.getvalue() except ParseError, e: # Something went wrong, notify user response['resultXML'] = "" response['resultMsg'] = 'textmarc_parsing_error' response['parse_error'] = [e.lineno, " ".join(e.linecontent.split()[1:]), e.message] finally: sys.stdout = old_stdout return response
def transform_input_to_marcxml(filename, file_input=""): """ Takes the filename or input of text-marc and transforms it to MARCXML. """ if not filename: # Create temporary file to read from tmp_fd, filename = mkstemp() os.write(tmp_fd, file_input) os.close(tmp_fd) try: # Redirect output, transform, restore old references old_stdout = sys.stdout new_stdout = StringIO() sys.stdout = new_stdout transform_file(filename) finally: sys.stdout = old_stdout return new_stdout.getvalue()
def _transform_input_to_marcxml(file_input=""): """ Takes text-marc as input and transforms it to MARCXML. """ # Create temporary file to read from tmp_fd, filename = tempfile.mkstemp(dir=CFG_TMPSHAREDDIR) os.write(tmp_fd, file_input) os.close(tmp_fd) try: # Redirect output, transform, restore old references old_stdout = sys.stdout new_stdout = StringIO() sys.stdout = new_stdout transform_file(filename) finally: sys.stdout = old_stdout return new_stdout.getvalue()
def main(): '''Run the script.''' filename_1 = 'tmp_' + __file__ filename_1 = re.sub('.py', '_1.out', filename_1) output = open(filename_1, 'w') new_records = [] for book in BOOKS: new_records += process_references(book) for record in new_records: for line in record: output.write(line) output.close() filename = 'tmp_' + __file__ filename = re.sub('.py', '_correct.out', filename) with open(filename, "w") as final_output: with stdout_redirected(final_output): transform_file(filename_1) final_output.close() os.unlink(filename_1) print filename