FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-bibtexparser/bibtexparser/bibdatabase.py at master · bcbnz/python-bibtexparser · GitHub
bcbnz
/
python-bibtexparser
Public
forked from
sciunto-org/python-bibtexparser
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-bibtexparser
/
bibtexparser
/
bibdatabase.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
122 lines (102 loc) · 3.33 KB
Breadcrumbs
python-bibtexparser
/
bibtexparser
/
bibdatabase.py
Copy path
File metadata and controls
122 lines (102 loc) · 3.33 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from
collections
import
OrderedDict
import
sys
if
sys
.
version_info
.
major
==
2
:
TEXT_TYPE
=
unicode
else
:
TEXT_TYPE
=
str
STANDARD_TYPES
=
set
([
'article'
,
'book'
,
'booklet'
,
'conference'
,
'inbook'
,
'incollection'
,
'inproceedings'
,
'manual'
,
'mastersthesis'
,
'misc'
,
'phdthesis'
,
'proceedings'
,
'techreport'
,
'unpublished'
])
COMMON_STRINGS
=
{
'jan'
:
'January'
,
'feb'
:
'February'
,
'mar'
:
'March'
,
'apr'
:
'April'
,
'may'
:
'May'
,
'jun'
:
'June'
,
'jul'
:
'July'
,
'aug'
:
'August'
,
'sep'
:
'September'
,
'oct'
:
'October'
,
'nov'
:
'November'
,
'dec'
:
'December'
,
}
class
BibDatabase
(
object
):
"""
Bibliographic database object that follows the data structure of a BibTeX file.
"""
def
__init__
(
self
):
#: List of BibTeX entries, for example `@book{...}`, `@article{...}`, etc. Each entry is a simple dict with
#: BibTeX field-value pairs, for example `'author': 'Bird, R.B. and Armstrong, R.C. and Hassager, O.'` Each
#: entry will always have the following dict keys (in addition to other BibTeX fields):
#:
#: * `ID` (BibTeX key)
#: * `ENTRYTYPE` (entry type in lowercase, e.g. `book`, `article` etc.)
self
.
entries
=
[]
self
.
_entries_dict
=
{}
#: List of BibTeX comment (`@comment{...}`) blocks.
self
.
comments
=
[]
#: OrderedDict of BibTeX string definitions (`@string{...}`). In order of definition.
self
.
strings
=
OrderedDict
()
# Not sure if order is import, keep order just in case
#: List of BibTeX preamble (`@preamble{...}`) blocks.
self
.
preambles
=
[]
def
load_common_strings
(
self
):
self
.
strings
.
update
(
COMMON_STRINGS
)
def
get_entry_list
(
self
):
"""Get a list of bibtex entries.
:returns: BibTeX entries
:rtype: list
.. deprecated:: 0.5.6
Use :attr:`entries` instead.
"""
return
self
.
entries
@
staticmethod
def
entry_sort_key
(
entry
,
fields
):
result
=
[]
for
field
in
fields
:
result
.
append
(
TEXT_TYPE
(
entry
.
get
(
field
,
''
)).
lower
())
# Sorting always as string
return
tuple
(
result
)
def
get_entry_dict
(
self
):
"""Return a dictionary of BibTeX entries.
The dict key is the BibTeX entry key
"""
# If the hash has never been made, make it
if
not
self
.
_entries_dict
:
for
entry
in
self
.
entries
:
self
.
_entries_dict
[
entry
[
'ID'
]]
=
entry
return
self
.
_entries_dict
entries_dict
=
property
(
get_entry_dict
)
def
expand_string
(
self
,
name
):
try
:
return
self
.
strings
[
name
]
except
KeyError
:
raise
(
KeyError
(
"Unknown string: {}."
.
format
(
name
)))
class
BibDataString
(
object
):
"""
Represents a bibtex string.
This object enables mainting string expressions as list of strings
and BibDataString. Can be interpolated from Bibdatabase.
"""
def
__init__
(
self
,
bibdatabase
,
name
):
self
.
_bibdatabase
=
bibdatabase
self
.
name
=
name
.
lower
()
def
__repr__
(
self
):
return
"BibDataString({})"
.
format
(
self
.
name
.
__repr__
())
def
get_value
(
self
):
"""
Query value from string name.
:returns: string
"""
return
self
.
_bibdatabase
.
expand_string
(
self
.
name
)
Back
|
FazBrowse Home
|
New Git URL