Edit working

This commit is contained in:
Phyks 2014-05-14 14:53:56 +02:00
parent eecdcc93bf
commit 8c0a9e83bc
3 changed files with 34 additions and 20 deletions

View File

@ -37,6 +37,8 @@ Should be almost working and usable now, although still to be considered as **ex
* working: all * working: all
* Delete * Delete
* working: all (by file and by id) * working: all (by file and by id)
* Edit
* working: all
* List * List
* TODO * TODO
* Search * Search

View File

@ -210,7 +210,7 @@ def getBibtex(entry, file_id='both'):
""" """
try: try:
with open(params.folder+'index.bib', 'r', encoding='utf-8') as fh: with open(params.folder+'index.bib', 'r', encoding='utf-8') as fh:
bibtex = BibTexParser(fh.read()) bibtex = BibTexParser(fh.read().encode('utf-8'))
bibtex = bibtex.get_entry_dict() bibtex = bibtex.get_entry_dict()
except: except:
tools.warning("Unable to open index file.") tools.warning("Unable to open index file.")
@ -222,11 +222,12 @@ def getBibtex(entry, file_id='both'):
bibtex_entry = bibtex[entry] bibtex_entry = bibtex[entry]
except KeyError: except KeyError:
pass pass
elif file_id == 'both' or file_id == 'file': if file_id == 'both' or file_id == 'file':
for key in bibtex.keys(): if os.path.isfile(entry):
if os.path.samefile(bibtex[key]['file'], entry): for key in bibtex.keys():
bibtex_entry = bibtex[key] if os.path.samefile(bibtex[key]['file'], entry):
break bibtex_entry = bibtex[key]
break
return bibtex_entry return bibtex_entry

39
main.py
View File

@ -19,18 +19,14 @@ from codecs import open
EDITOR = os.environ.get('EDITOR') if os.environ.get('EDITOR') else 'vim' EDITOR = os.environ.get('EDITOR') if os.environ.get('EDITOR') else 'vim'
def checkBibtex(filename, bibtex): def checkBibtex(filename, bibtex_string):
print("The bibtex entry found for "+filename+" is:") print("The bibtex entry found for "+filename+" is:")
bibtex = BibTexParser(bibtex.encode('utf-8')) bibtex = BibTexParser(bibtex_string.encode('utf-8'))
bibtex = bibtex.get_entry_dict() bibtex = bibtex.get_entry_dict()
if len(bibtex) > 0: bibtex = bibtex[bibtex.keys()[0]]
bibtex_name = bibtex.keys()[0]
bibtex = bibtex[bibtex_name]
bibtex_string = tools.parsed2Bibtex(bibtex)
else:
bibtex_string = ''
print(bibtex_string) print(bibtex_string)
check = tools.rawInput("Is it correct? [Y/n] ") check = tools.rawInput("Is it correct? [Y/n] ")
try: try:
old_filename = bibtex['file'] old_filename = bibtex['file']
@ -46,14 +42,18 @@ def checkBibtex(filename, bibtex):
bibtex = BibTexParser(tmpfile.read().encode('utf-8')+"\n") bibtex = BibTexParser(tmpfile.read().encode('utf-8')+"\n")
bibtex = bibtex.get_entry_dict() bibtex = bibtex.get_entry_dict()
try:
bibtex = bibtex[bibtex.keys()[0]]
except:
tools.warning("Invalid bibtex entry")
bibtex_string = False
if old_filename is not False and 'file' not in bibtex: if old_filename is not False and 'file' not in bibtex:
tools.warning("Invalid bibtex entry. No filename given.") tools.warning("Invalid bibtex entry. No filename given.")
tools.rawInput("Press Enter to go back to editor.") tools.rawInput("Press Enter to go back to editor.")
check = 'n' check = 'n'
else: else:
if len(bibtex) > 0: if bibtex_string is not False:
bibtex_name = bibtex.keys()[0]
bibtex = bibtex[bibtex_name]
bibtex_string = tools.parsed2Bibtex(bibtex) bibtex_string = tools.parsed2Bibtex(bibtex)
else: else:
bibtex_string = '' bibtex_string = ''
@ -62,10 +62,12 @@ def checkBibtex(filename, bibtex):
check = tools.rawInput("Is it correct? [Y/n] ") check = tools.rawInput("Is it correct? [Y/n] ")
if old_filename is not False and old_filename != bibtex['file']: if old_filename is not False and old_filename != bibtex['file']:
try: try:
print("Moving file to new location…")
shutil.move(old_filename, bibtex['file']) shutil.move(old_filename, bibtex['file'])
except: except:
tools.warning("Unable to move file "+old_filename+" to " + tools.warning("Unable to move file "+old_filename+" to " +
bibtex['file']+". You should check it manually.") bibtex['file']+". You should check it manually.")
return bibtex return bibtex
@ -133,7 +135,15 @@ def addFile(src, filetype, manual):
else: else:
bibtex = '' bibtex = ''
bibtex = checkBibtex(src, bibtex) bibtex = BibTexParser(bibtex.encode('utf-8'))
bibtex = bibtex.get_entry_dict()
if len(bibtex) > 0:
bibtex_name = bibtex.keys()[0]
bibtex = bibtex[bibtex_name]
bibtex_string = tools.parsed2Bibtex(bibtex)
else:
bibtex_string = ''
bibtex = checkBibtex(src, bibtex_string)
tag = tools.rawInput("Tag for this paper (leave empty for default) ? ") tag = tools.rawInput("Tag for this paper (leave empty for default) ? ")
bibtex['tag'] = tag bibtex['tag'] = tag
@ -172,12 +182,13 @@ def editEntry(entry, file_id='both'):
bibtex = backend.getBibtex(entry, file_id) bibtex = backend.getBibtex(entry, file_id)
if bibtex is False: if bibtex is False:
tools.warning("Entry "+entry+" does not exist.") tools.warning("Entry "+entry+" does not exist.")
return False
if file_id == 'file': if file_id == 'file':
filename = entry filename = entry
else: else:
filename = bibtex['file'] filename = bibtex['file']
new_bibtex = checkBibtex(filename, bibtex) new_bibtex = checkBibtex(filename, tools.parsed2Bibtex(bibtex))
# Tag update # Tag update
if new_bibtex['tag'] != bibtex['tag']: if new_bibtex['tag'] != bibtex['tag']:
@ -213,7 +224,7 @@ def editEntry(entry, file_id='both'):
try: try:
with open(params.folder+'index.bib', 'r', encoding='utf-8') as fh: with open(params.folder+'index.bib', 'r', encoding='utf-8') as fh:
index = BibTexParser(fh.read()) index = BibTexParser(fh.read().encode('utf-8'))
index = index.get_entry_dict() index = index.get_entry_dict()
except: except:
tools.warning("Unable to open index file.") tools.warning("Unable to open index file.")