Resync should be working

This commit is contained in:
Phyks 2014-05-14 17:07:57 +02:00
parent 8c0a9e83bc
commit 558946f48d
3 changed files with 28 additions and 16 deletions

View File

@ -46,7 +46,7 @@ Should be almost working and usable now, although still to be considered as **ex
* Open * Open
* working: all * working: all
* Resync * Resync
* Testing * working
* Update * Update
* Testing * Testing

View File

@ -171,6 +171,8 @@ def deleteFile(filename):
pass pass
if found: if found:
bibtexRewrite(bibtex) bibtexRewrite(bibtex)
elif os.path.isfile(filename):
os.remove(filename)
return found return found
@ -182,9 +184,10 @@ def diffFilesIndex():
* only file entry if file with missing bibtex entry * only file entry if file with missing bibtex entry
""" """
files = tools.listDir(params.folder) files = tools.listDir(params.folder)
files = [ i for i in files if tools.getExtension(i) in ['.pdf', '.djvu'] ]
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_diff = index.get_entry_dict() index_diff = index.get_entry_dict()
except: except:
tools.warning("Unable to open index file.") tools.warning("Unable to open index file.")
@ -199,7 +202,7 @@ def diffFilesIndex():
for filename in files: for filename in files:
index_diff[filename] = {'file': filename} index_diff[filename] = {'file': filename}
return index return index.get_entry_dict()
def getBibtex(entry, file_id='both'): def getBibtex(entry, file_id='both'):

35
main.py
View File

@ -272,56 +272,65 @@ def openFile(ident):
def resync(): def resync():
diff = backend.diffFilesIndex() diff = backend.diffFilesIndex()
for entry in diff: if diff is False:
return False
for key in diff:
entry = diff[key]
if entry['file'] == '': if entry['file'] == '':
print("Found entry in index without associated file.") print("\nFound entry in index without associated file: " +
confirm = False entry['id'])
while not confirm: print("Title:\t"+entry['title'])
loop = True
while confirm:
filename = tools.rawInput("File to import for this entry " + filename = tools.rawInput("File to import for this entry " +
"(leave empty to delete the " + "(leave empty to delete the " +
"entry)? ") "entry)? ")
if filename == '': if filename == '':
break break
else: else:
confirm = True
if 'doi' in entry.keys(): if 'doi' in entry.keys():
doi = fetcher.findDOI(filename) doi = fetcher.findDOI(filename)
if doi is not False and doi != entry['doi']: if doi is not False and doi != entry['doi']:
confirm = tools.rawInput("Found DOI does not " + loop = tools.rawInput("Found DOI does not " +
"match bibtex entry " + "match bibtex entry " +
"DOI, continue anyway " + "DOI, continue anyway " +
"? [y/N]") "? [y/N]")
confirm = (confirm.lower() == 'y') loop = (loop.lower() != 'y')
if 'Eprint' in entry.keys(): if 'Eprint' in entry.keys():
arxiv = fetcher.findArXivId(filename) arxiv = fetcher.findArXivId(filename)
if arxiv is not False and arxiv != entry['Eprint']: if arxiv is not False and arxiv != entry['Eprint']:
confirm = tools.rawInput("Found arXiv id does " + loop = tools.rawInput("Found arXiv id does " +
"not match bibtex " + "not match bibtex " +
"entry arxiv id, " + "entry arxiv id, " +
"continue anyway ? [y/N]") "continue anyway ? [y/N]")
confirm = (confirm.lower() == 'y') loop = (loop.lower() != 'y')
elif 'isbn' in entry.keys(): if 'isbn' in entry.keys():
isbn = fetcher.findISBN(filename) isbn = fetcher.findISBN(filename)
if isbn is not False and isbn != entry['isbn']: if isbn is not False and isbn != entry['isbn']:
confirm = tools.rawInput("Found ISBN does not " + loop = tools.rawInput("Found ISBN does not " +
"match bibtex entry " + "match bibtex entry " +
"ISBN, continue anyway " + "ISBN, continue anyway " +
"? [y/N]") "? [y/N]")
confirm = (confirm.lower() == 'y') loop = (loop.lower() != 'y')
continue continue
if filename == '': if filename == '':
backend.deleteId(entry['id']) backend.deleteId(entry['id'])
print("Deleted entry \""+entry['id']+"\".")
else: else:
new_name = backend.getNewName(filename, entry) new_name = backend.getNewName(filename, entry)
try: try:
shutil.copy2(filename, new_name) shutil.copy2(filename, new_name)
print("Imported new file "+filename+" for entry " +
entry['id']+".")
except IOError: except IOError:
new_name = False new_name = False
sys.exit("Unable to move file to library dir " + sys.exit("Unable to move file to library dir " +
params.folder+".") params.folder+".")
backend.bibtexEdit(entry['id'], {'file': filename}) backend.bibtexEdit(entry['id'], {'file': filename})
else: else:
print("Found file without any associated entry in index.") print("Found file without any associated entry in index:")
print(entry['file'])
action = '' action = ''
while action.lower() not in ['import', 'delete']: while action.lower() not in ['import', 'delete']:
action = tools.rawInput("What to do? [import / delete] ") action = tools.rawInput("What to do? [import / delete] ")