Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create bibTexParsing.py #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions bibTexParsing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from pybtex.database.input import bibtex


def read_bibtex(path):
parser = bibtex.Parser()
sources = parser.parse_file(path)
return sources

def read_bibTexKeys(path):
bibtexKeys = []
# read in bib tex keys as list (code needs to be altered for dicts and df´s to be processed)
return bibtexKeys

def extract_sources(bibTexKeys, sources):
sourcesList = {}
for source in bibTexKeys:
if source not in list(sources.entries._keys.values()):
print('Source ' + source + ' not found in sources list.')
newSource = dict()
newSource["title"] = source
newSource["description"] = ""
newSource["path"] = ""
newSource["licenses"] = []
else:
newSource = dict()
if 'subtitle' in sources.entries[source].fields:
if not sources.entries[source].fields['subtitle'].empty:
newSource["title"] = sources.entries[source].fields['title'] + ' - ' + sources.entries[source].fields['subtitle']
else:
newSource["title"] = sources.entries[source].fields['title']
if 'abstract' in sources.entries[source].fields:
newSource["description"] = sources.entries[source].fields['abstract']
else:
newSource["description"] = ''
if 'url' in sources.entries[source].fields:
newSource["path"] = sources.entries[source].fields['url']
else:
newSource["path"] = ''
if 'licenses_name' in sources.entries[source].fields:
newSource["licenses"] = [
{"name": sources.entries[source].fields['licenses_name'],
"title": sources.entries[source].fields['licenses_title'],
"path": sources.entries[source].fields['licenses_path'],
"instruction": sources.entries[source].fields['licenses_instruction'],
"attribution": sources.entries[source].fields['licenses_attribution'] + ' ' + sources.entries[source].fields['author'],
}
]
else:
newSource["licenses"] = []
sourcesList.append(newSource)


if __name__ == "__main__":
sources = read_bibtex('O:/ESY/06_Projekte-ST/BMWi SEDOS/06_Literatur/20231018_SEDOS.bib')
bibTexKeys = read_bibTexKeys()
extract_sources(bibTexKeys, sources)