Open a BibTex file and do some initial parsing. The path to the file has to be provided, otherwise an exception is raised. Optionally, you can provide an array of keys to reduce the amount of processed entries immediately. By default, the Bibtex file gets scanned wi
(self, path, keys=[], encoding="latex")
| 35 | self.loadLibrary(fName) |
| 36 | |
| 37 | def loadLibrary(self, path, keys=[], encoding="latex"): |
| 38 | """Open a BibTex file and do some initial parsing. |
| 39 | The path to the file has to be provided, otherwise an exception is raised. |
| 40 | |
| 41 | Optionally, you can provide an array of keys to reduce the amount of |
| 42 | processed entries immediately. |
| 43 | |
| 44 | By default, the Bibtex file gets scanned with the artificial \"latex\" |
| 45 | encoding, which translates Latex commands to their unicode equivalents. |
| 46 | If you need Latex output, you can skip this step by passing another |
| 47 | codec to the \"encoding\" parameter, for example \"ascii\" or \"utf-8\". |
| 48 | """ |
| 49 | |
| 50 | if path is None: |
| 51 | raise ValueError("You have to provide a path to a bibtex file.") |
| 52 | |
| 53 | # Create the parser object |
| 54 | if len(keys) > 0: |
| 55 | bib_parser = pybtex.database.input.bibtex.Parser(wanted_entries=keys) |
| 56 | else: |
| 57 | bib_parser = pybtex.database.input.bibtex.Parser() |
| 58 | |
| 59 | # TODO: not needed anymore? |
| 60 | oldLatexCodec = False |
| 61 | if oldLatexCodec: |
| 62 | # Do not print that many warnings |
| 63 | pybtex.errors.set_strict_mode(enable=False) |
| 64 | |
| 65 | # TODO: Remove empty lines to keep Pybtex from choking |
| 66 | with open(path, "r") as f: |
| 67 | lines = f.readlines() |
| 68 | cleaned = [l.strip() for l in lines if l.strip()] |
| 69 | |
| 70 | path = path + ".filtered.bib" |
| 71 | with open(path, "w") as f: |
| 72 | f.writelines('\n'.join(cleaned)) |
| 73 | |
| 74 | # Open the file and convert it according to the encoding |
| 75 | with codecs.open(path, encoding=encoding) as stream: |
| 76 | self.library = bib_parser.parse_stream(stream) |
| 77 | |
| 78 | # os.remove(path) |
| 79 | |
| 80 | # Do some post-processing if encoding was latex |
| 81 | if encoding == "latex": |
| 82 | for tag in self.library.entries: |
| 83 | entry = self.library.entries[tag] |
| 84 | for key, value in entry.fields.items(): |
| 85 | entry.fields[key] = self.stripCurls(value) |
| 86 | if key == 'Title': |
| 87 | entry.fields[key] = u'{' + entry.fields[key] + '}' |
| 88 | for key in entry.persons.keys(): |
| 89 | for i in range(len(entry.persons[key])): |
| 90 | entry.persons[key][i]._first = self.stripCurls(entry.persons[key][i].first()) |
| 91 | entry.persons[key][i]._middle = self.stripCurls(entry.persons[key][i].middle()) |
| 92 | entry.persons[key][i]._prelast = self.stripCurls(entry.persons[key][i].prelast()) |
| 93 | entry.persons[key][i]._last = self.stripCurls(entry.persons[key][i].last()) |
| 94 | entry.persons[key][i]._lineage = self.stripCurls(entry.persons[key][i].lineage()) |