Exemplo n.º 1
0
def test_dumps_multiple_unknown_tags_ris(tmp_path):

    fp = tmp_path / "test_dump_unknown_tags.ris"

    results = [{"title": "my-title", "abstract": "my-abstract", "does_not_exists": "test"}]

    # check that we get a warning
    with pytest.warns(UserWarning, match="label `does_not_exists` not exported"):
        with open(fp, "w") as f:
            rispy.dump(results, f)

    # check that we get everything back except missing key
    text = Path(fp).read_text()
    entries = rispy.loads(text)
    assert entries[0] == {
        "type_of_reference": "JOUR",
        "title": "my-title",
        "abstract": "my-abstract",
    }

    # check file looks as expected
    lines = text.splitlines()
    assert lines[0] == "1."
    assert lines[1] == "TY  - JOUR"
    assert lines[4] == "ER  - "
    assert len(lines) == 5
Exemplo n.º 2
0
def test_file_implementation_write():
    class CustomParser(rispy.RisParser):
        DEFAULT_IGNORE = ["JF", "ID", "KW"]

    class CustomWriter(rispy.RisWriter):
        DEFAULT_IGNORE = ["JF", "ID", "KW"]

    list_tags = ["SN", "T1", "A1", "UR"]

    fn = DATA_DIR / "example_full.ris"
    with open(fn, "r") as f:
        entries = rispy.load(f, implementation=CustomParser, list_tags=list_tags)

    fn_write = DATA_DIR / "example_full_write.ris"

    with open(fn_write, "w") as f:
        rispy.dump(entries, f, implementation=CustomWriter, list_tags=list_tags)

    with open(fn_write, "r") as f:
        reload = rispy.load(f, implementation=CustomParser, list_tags=list_tags)

    assert reload == entries
Exemplo n.º 3
0
    def export(self):

        # Open StringIO to grab in-memory file contents
        file = io.StringIO()

        entries = list()

        for paper in self._papers:
            entry = {
                'primary_title':
                paper.title,
                'first_authors': [
                    ", ".join([author.last_name, author.first_name])
                    for author in paper.authors.all()
                ],
                'abstract':
                paper.abstract,
                'doi':
                paper.doi,
                'publication_year':
                paper.published_at.year,
                'publisher':
                paper.host.name
            }

            if paper.pdf_url:
                entry['url'] = paper.pdf_url

            if paper.journal:
                entry['journal_name'] = paper.journal.displayname
                entry['type_of_reference'] = 'JOUR'
            else:
                entry['type_of_reference'] = 'GEN'

            entries.append(entry)

        rispy.dump(entries, file)

        return file
Exemplo n.º 4
0
def main():

    # Get input arguments
    parser = argparse.ArgumentParser(
        description=
        'Given a list of accession numbers in a file, extract records that match those ids'
    )

    parser.add_argument(
        "-i",
        "--input_file",
        type=str,
        help=
        "The path to the file that contains the accession numbers to search for",
        required=True)
    parser.add_argument(
        "-d",
        "--data_file",
        type=str,
        help="The path to the data file. Should be in .ris format",
        required=True)
    parser.add_argument(
        "-o",
        "--output_file",
        type=str,
        help=
        "The path to the output file for records taht match. Will be in .ris format",
        required=True)

    input_file = ''
    data_file = ''
    output_file = ''

    try:
        args = parser.parse_args()

        input_file = args.input_file
        data_file = args.data_file
        output_file = args.output_file

    except:
        parser.print_help()
        sys.exit(0)

    # Get list of accession ids from input file
    accession_no_list = []
    try:
        accession_no_list = [line.strip() for line in open(input_file)]
    except:
        print(f'Unable to open input file {input_file}')
        sys.exit(0)

    # Interate over data file and if ids is one specificed in the id file add to list
    selected_records = []
    try:
        with open(data_file, 'r') as bibliography_file:
            entries = rispy.load(bibliography_file)
            for entry in entries:
                if entry['accession_number'] in accession_no_list:
                    selected_records.append(entry)
    except OSError as err:
        print("OS error: {0}".format(err))
        sys.exit(0)
    except:
        print("Unexpected error:", sys.exc_info()[0])
        raise

    # This is pretty inefficient. We are storing the selected records to a list
    # and this list could get huge!
    try:
        with open(output_file, 'w') as output_file:
            rispy.dump(selected_records, output_file)
    except OSError as err:
        print("OS error: {0}".format(err))
        sys.exit(0)
    except:
        print("Unexpected error:", sys.exc_info()[0])
        raise
Exemplo n.º 5
0
import os
import rispy

entries = [{
    'type_of_reference': 'JOUR',
    'id': '42',
    'primary_title': 'The title of the reference',
    'first_authors': ['Marxus, Karlus', 'Lindgren, Astrid']
}, {
    'type_of_reference': 'JOUR',
    'id': '43',
    'primary_title': 'Reference 43',
    'abstract': 'Lorem ipsum'
}]
filepath = 'export.ris'
with open(filepath, 'w') as bibliography_file:
    rispy.dump(entries, bibliography_file)
Exemplo n.º 6
0
def write_entries_to_ris(entries, wpath, mode="w"):
    if not isinstance(entries, list):
        raise AssertionError("entries must be dictionary.")
    with open(wpath, mode) as write:
        rispy.dump(entries, write)