Deletion working
This commit is contained in:
parent
72e2def380
commit
49f4fb41ee
10
README.md
10
README.md
@ -81,3 +81,13 @@ A list of ideas and TODO. Don't hesitate to give feedback on the ones you really
|
|||||||
* Options to ignore auto check, manual check etc.
|
* Options to ignore auto check, manual check etc.
|
||||||
* anti-duplicates
|
* anti-duplicates
|
||||||
* Split main.py
|
* Split main.py
|
||||||
|
|
||||||
|
## Roadmap
|
||||||
|
|
||||||
|
* Working with local files
|
||||||
|
[x] Import
|
||||||
|
[x] Deletion
|
||||||
|
[ ] Update ?
|
||||||
|
* Get distant files
|
||||||
|
* cf paperbot
|
||||||
|
* Search engine / list
|
||||||
|
79
main.py
79
main.py
@ -19,11 +19,12 @@ except:
|
|||||||
from StringIO import StringIO
|
from StringIO import StringIO
|
||||||
from bibtexparser.bparser import BibTexParser
|
from bibtexparser.bparser import BibTexParser
|
||||||
from bibtexparser.customization import homogeneize_latex_encoding
|
from bibtexparser.customization import homogeneize_latex_encoding
|
||||||
|
from termios import tcflush, TCIOFLUSH
|
||||||
import params
|
import params
|
||||||
|
|
||||||
|
|
||||||
def rawInput(string):
|
def rawInput(string):
|
||||||
sys.stdout.flush()
|
tcflush(sys.stdin, TCIOFLUSH)
|
||||||
return raw_input(string)
|
return raw_input(string)
|
||||||
|
|
||||||
|
|
||||||
@ -35,6 +36,9 @@ def warning(*objs):
|
|||||||
|
|
||||||
|
|
||||||
def parsed2Bibtex(parsed):
|
def parsed2Bibtex(parsed):
|
||||||
|
"""
|
||||||
|
Convert a single bibtex entry dict to bibtex string
|
||||||
|
"""
|
||||||
bibtex = '@'+parsed['type']+'{'+parsed['id']+"\n"
|
bibtex = '@'+parsed['type']+'{'+parsed['id']+"\n"
|
||||||
|
|
||||||
for field in [i for i in sorted(parsed) if i not in ['type', 'id']]:
|
for field in [i for i in sorted(parsed) if i not in ['type', 'id']]:
|
||||||
@ -46,11 +50,22 @@ def parsed2Bibtex(parsed):
|
|||||||
def bibtexAppend(data):
|
def bibtexAppend(data):
|
||||||
"""
|
"""
|
||||||
Append data to the main bibtex file
|
Append data to the main bibtex file
|
||||||
data is a dict as the one from bibtexparser output
|
data is a dict for one entry in bibtex, as the one from bibtexparser output
|
||||||
"""
|
"""
|
||||||
with open(params.folder+'index.bib', 'a') as fh:
|
with open(params.folder+'index.bib', 'a') as fh:
|
||||||
fh.write(parsed2Bibtex(data)+"\n")
|
fh.write(parsed2Bibtex(data)+"\n")
|
||||||
|
|
||||||
|
def bibtexRewrite(data):
|
||||||
|
"""
|
||||||
|
Rewrite the bibtex index file.
|
||||||
|
data is a dict of bibtex entry dict.
|
||||||
|
"""
|
||||||
|
bibtex = ''
|
||||||
|
for entry in data.keys():
|
||||||
|
bibtex += parsed2Bibtex(data[entry])+"\n"
|
||||||
|
with open(params.folder+'index.bib', 'w') as fh:
|
||||||
|
fh.write(bibtex)
|
||||||
|
|
||||||
|
|
||||||
def replaceAll(text, dic):
|
def replaceAll(text, dic):
|
||||||
for i, j in dic.iteritems():
|
for i, j in dic.iteritems():
|
||||||
@ -267,7 +282,6 @@ def addFile(src, filetype):
|
|||||||
for i in authors]))
|
for i in authors]))
|
||||||
|
|
||||||
new_name = params.folder+_slugify(new_name)+getExtension(src)
|
new_name = params.folder+_slugify(new_name)+getExtension(src)
|
||||||
bibtex['file'] = new_name
|
|
||||||
|
|
||||||
while os.path.exists(new_name):
|
while os.path.exists(new_name):
|
||||||
warning("file "+new_name+" already exists.")
|
warning("file "+new_name+" already exists.")
|
||||||
@ -278,18 +292,59 @@ def addFile(src, filetype):
|
|||||||
new_name = default_rename
|
new_name = default_rename
|
||||||
else:
|
else:
|
||||||
new_name = rename
|
new_name = rename
|
||||||
|
bibtex['file'] = new_name
|
||||||
|
|
||||||
try:
|
try:
|
||||||
shutil.copy2(src, new_name)
|
shutil.copy2(src, new_name)
|
||||||
except IOError:
|
except IOError:
|
||||||
sys.exit("Unable to move file to library dir " + params.folder+".")
|
sys.exit("Unable to move file to library dir " + params.folder+".")
|
||||||
|
|
||||||
# TODO
|
|
||||||
bibtexAppend(bibtex)
|
bibtexAppend(bibtex)
|
||||||
print("File " + src + " successfully imported.")
|
print("File " + src + " successfully imported.")
|
||||||
|
|
||||||
|
|
||||||
|
def delete_id(ident):
|
||||||
|
"""
|
||||||
|
Delete a file based on its id in the bibtex file
|
||||||
|
"""
|
||||||
|
with open(params.folder+'index.bib', 'r') as fh:
|
||||||
|
bibtex = BibTexParser(fh).get_entry_dict()
|
||||||
|
|
||||||
|
if ident not in bibtex.keys():
|
||||||
|
return False
|
||||||
|
|
||||||
|
try:
|
||||||
|
os.remove(bibtex[ident]['file'])
|
||||||
|
except:
|
||||||
|
warning("Unable to delete file associated to id "+ident+" : " +
|
||||||
|
bibtex[ident]['file'])
|
||||||
|
del(bibtex[ident])
|
||||||
|
bibtexRewrite(bibtex)
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def delete_file(filename):
|
||||||
|
"""
|
||||||
|
Delete a file based on its filename
|
||||||
|
"""
|
||||||
|
with open(params.folder+'index.bib', 'r') as fh:
|
||||||
|
bibtex = BibTexParser(fh).get_entry_dict()
|
||||||
|
|
||||||
|
found = False
|
||||||
|
for key in bibtex.keys():
|
||||||
|
if bibtex[key]['file'] == filename:
|
||||||
|
found = True
|
||||||
|
try:
|
||||||
|
os.remove(bibtex[key]['file'])
|
||||||
|
except:
|
||||||
|
warning("Unable to delete file associated to id "+key+" : " +
|
||||||
|
bibtex[key]['file'])
|
||||||
|
del(bibtex[key])
|
||||||
|
if found:
|
||||||
|
bibtexRewrite(bibtex)
|
||||||
|
return found
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
try:
|
try:
|
||||||
if len(sys.argv) < 2:
|
if len(sys.argv) < 2:
|
||||||
@ -310,12 +365,24 @@ if __name__ == '__main__':
|
|||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
elif sys.argv[1] == 'delete':
|
elif sys.argv[1] == 'delete':
|
||||||
raise Exception('TODO')
|
if len(sys.argv) < 3:
|
||||||
|
sys.exit("Usage : " + sys.argv[0] + " delete FILE|ID")
|
||||||
|
|
||||||
|
if not delete_id(sys.argv[2]):
|
||||||
|
if not delete_file(sys.argv[2]):
|
||||||
|
warning("Unable to delete "+sys.argv[2])
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
print(sys.argv[2]+" successfully deleted.")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
elif sys.argv[1] == 'list':
|
elif sys.argv[1] == 'list':
|
||||||
raise Exception('TODO')
|
raise Exception('TODO')
|
||||||
|
|
||||||
elif sys.argv[1] == 'search':
|
elif sys.argv[1] == 'search':
|
||||||
raise Exception('TODO')
|
raise Exception('TODO')
|
||||||
|
|
||||||
|
elif sys.argv[1] == 'rebuild':
|
||||||
|
raise Exception('TODO')
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
Loading…
Reference in New Issue
Block a user