Improves parser selection.
This commit is contained in:
parent
42569f7f23
commit
49821eab51
@ -27,6 +27,11 @@ except ImportError:
|
|||||||
_papersdir = None
|
_papersdir = None
|
||||||
|
|
||||||
BIB_EXTENSIONS = ['.bib', '.bibyaml', '.bibml', '.yaml']
|
BIB_EXTENSIONS = ['.bib', '.bibyaml', '.bibml', '.yaml']
|
||||||
|
FORMATS = {'bib': pybtex.database.input.bibtex,
|
||||||
|
'xml': pybtex.database.input.bibtexml,
|
||||||
|
'yml': pybtex.database.input.bibyaml,
|
||||||
|
'yaml': pybtex.database.input.bibyaml,
|
||||||
|
'bibyaml': pybtex.database.input.bibyaml}
|
||||||
|
|
||||||
|
|
||||||
def clean_path(path):
|
def clean_path(path):
|
||||||
@ -129,35 +134,34 @@ def load_externalbibfile(fullbibpath):
|
|||||||
check_file(fullbibpath)
|
check_file(fullbibpath)
|
||||||
|
|
||||||
filename, ext = os.path.splitext(os.path.split(fullbibpath)[1])
|
filename, ext = os.path.splitext(os.path.split(fullbibpath)[1])
|
||||||
if ext == '.bib':
|
if ext[1:] in FORMATS.keys():
|
||||||
parser = pybtex.database.input.bibtex.Parser()
|
with open(fullbibpath) as f:
|
||||||
bib_data = parser.parse_file(fullbibpath)
|
return parse_bibdata(f, ext[1:])
|
||||||
elif ext == '.xml' or ext == '.bibtexml':
|
|
||||||
parser = pybtex.database.input.bibtexml.Parser()
|
|
||||||
bib_data = parser.parse_file(fullbibpath)
|
|
||||||
elif ext == '.yaml' or ext == '.bibyaml':
|
|
||||||
parser = pybtex.database.input.bibyaml.Parser()
|
|
||||||
bib_data = parser.parse_file(fullbibpath)
|
|
||||||
else:
|
else:
|
||||||
print(colored('error', 'error')
|
print(colored('error', 'error')
|
||||||
+ ': {} not recognized format for bibliography'.format(
|
+ ': {} not recognized format for bibliography'.format(
|
||||||
colored(ext, 'cyan')))
|
colored(ext, 'cyan')))
|
||||||
exit(-1)
|
exit(-1)
|
||||||
|
|
||||||
return bib_data
|
|
||||||
|
def parse_bibdata(content, format_):
|
||||||
|
"""Parse bib data from string.
|
||||||
|
|
||||||
|
:content: stream
|
||||||
|
:param format_: (bib|xml|yml)
|
||||||
|
"""
|
||||||
|
parser = FORMATS[format_].Parser()
|
||||||
|
return parser.parse_stream(content)
|
||||||
|
|
||||||
|
|
||||||
def editor_input(editor, initial=""):
|
def editor_input(editor, initial=""):
|
||||||
"""Use an editor to get input"""
|
"""Use an editor to get input"""
|
||||||
|
|
||||||
with tempfile.NamedTemporaryFile(suffix=".tmp", delete=False) as temp_file:
|
with tempfile.NamedTemporaryFile(suffix=".tmp", delete=False) as temp_file:
|
||||||
tfile_name = temp_file.name
|
tfile_name = temp_file.name
|
||||||
temp_file.write(initial)
|
temp_file.write(initial)
|
||||||
temp_file.flush()
|
temp_file.flush()
|
||||||
subprocess.call([editor, tfile_name])
|
subprocess.call([editor, tfile_name])
|
||||||
|
|
||||||
with open(tfile_name) as temp_file:
|
with open(tfile_name) as temp_file:
|
||||||
content = temp_file.read()
|
content = temp_file.read()
|
||||||
os.remove(tfile_name)
|
os.remove(tfile_name)
|
||||||
|
|
||||||
return content
|
return content
|
||||||
|
Loading…
x
Reference in New Issue
Block a user