Created
February 25, 2022 15:33
-
-
Save dmadisetti/c20a31b376abfed39437987677fc02e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import re | |
import sys | |
import argparse | |
from pybtex.database.input import bibtex | |
from pybtex.database import BibliographyData | |
def arg_is_file(path): | |
try: | |
if not os.path.isfile(path): | |
raise | |
except: | |
msg = '{0!r} is not a file'.format(path) | |
raise argparse.ArgumentTypeError(msg) | |
return path | |
def main(): | |
import argparse | |
parser = argparse.ArgumentParser( | |
formatter_class = argparse.ArgumentDefaultsHelpFormatter) | |
parser.add_argument('bib_path', | |
metavar = 'BIB_PATH', | |
type = arg_is_file, | |
help = ('Path to bibtex-formatted file.')) | |
args = parser.parse_args() | |
bib_parser = bibtex.Parser() | |
bib_data = bib_parser.parse_file(args.bib_path) | |
keywords = sum([re.split(r"\s*,\s*", keyword.strip()) for keyword in sys.stdin], []) | |
filtered_bib_data = BibliographyData() | |
for key, entry in bib_data.entries.items(): | |
if key in keywords: | |
filtered_bib_data.add_entry(entry.key, entry) | |
s = filtered_bib_data.to_string("bibtex") | |
s = s.replace("= \"", "= {") | |
s = s.replace("\",\n", "},\n") | |
s = s.replace("\"\n", "}\n") | |
sys.stdout.write(s) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment