python refactoring
This commit is contained in:
Executable
+27
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import os, sys, glob
|
||||
|
||||
from nnnotes import parse
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
indexfile = '.indx'
|
||||
|
||||
notes = glob.glob('*.mmd')
|
||||
if len(notes) > 1:
|
||||
sys.exit('More or less *.mmd files than expected.')
|
||||
elif len(notes) < 1:
|
||||
sys.exit('No *.mmd in current directory.')
|
||||
|
||||
try:
|
||||
note = open(notes[0], 'r+')
|
||||
except:
|
||||
sys.exit('Cannot open ' + notes[0])
|
||||
|
||||
with open(indexfile, 'w') as indx:
|
||||
parse.run(note, indx)
|
||||
|
||||
|
||||
|
||||
|
||||
Executable
+40
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import os, sys, glob, json
|
||||
|
||||
from nnnotes import compare, inject
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
indexfile = '.indx'
|
||||
|
||||
pdfs = glob.glob('*.pdf')
|
||||
if len(pdfs) > 1:
|
||||
sys.exit('More than one pdf in current directory. No obvious choice. Aborting.')
|
||||
|
||||
if len(pdfs) == 1:
|
||||
pdf = pdfs[0]
|
||||
if os.path.isfile(indexfile):
|
||||
tmp = '.tmp'
|
||||
os.system('plfr -json ' + pdf + ' > ' + tmp) ### relying on plfr
|
||||
diff = compare.run(indexfile, tmp) ### new highlights in pdf?
|
||||
|
||||
with open(tmp, 'w') as fptmp:
|
||||
json.dump(diff, fptmp)
|
||||
indexfile = tmp
|
||||
|
||||
else:
|
||||
os.system('plfr -json ' + pdf + ' > ' + indexfile)
|
||||
|
||||
|
||||
notes = glob.glob('*.mmd')
|
||||
if len(notes) > 1:
|
||||
sys.exit('More or less *.mmd files than expected.')
|
||||
elif len(notes) < 1:
|
||||
sys.exit('No *.mmd in current directory.')
|
||||
|
||||
note = notes[0]
|
||||
inject.run(note, indexfile)
|
||||
|
||||
if os.path.isfile('.tmp'):
|
||||
os.remove('.tmp')
|
||||
Executable
+83
@@ -0,0 +1,83 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import os, sys, argparse, shutil, subprocess
|
||||
import getpass, time
|
||||
|
||||
from nnnotes import TEMPLATE_PATH
|
||||
|
||||
def yes_no(question):
|
||||
|
||||
sys.stdout.write(question)
|
||||
|
||||
yes = set(['yes','y', 'ye', ''])
|
||||
no = set(['no','n'])
|
||||
|
||||
choice = raw_input().lower()
|
||||
if choice in yes:
|
||||
return True
|
||||
elif choice in no:
|
||||
return False
|
||||
else:
|
||||
sys.stdout.write("Please respond with 'yes' or 'no'")
|
||||
|
||||
def check_note_exists(title):
|
||||
|
||||
if os.path.isdir(title):
|
||||
q = yes_no('The note already exists in the current directory.\nDo you want to proceed and erase the current note? [y/n] ')
|
||||
if not q:
|
||||
sys.exit('Notes already exists. Aborting.')
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
p = argparse.ArgumentParser()
|
||||
p.add_argument('-t', '--title', action="store", help="title of the note", required=True)
|
||||
p.add_argument('-f', '--file', action="store", help="(pdf) file to extract notes from")
|
||||
p.add_argument('-b', '--bibtex', action="store", help="bibtex file containing bibliographical information")
|
||||
p.add_argument('-i', '--bibtexitem', action="store", help="name of the item in the bibtex file")
|
||||
p.add_argument('-s', '--style', action="store", help="bibliographic style")
|
||||
|
||||
args = p.parse_args()
|
||||
|
||||
if not check_note_exists(args.title):
|
||||
# make directory for the note
|
||||
os.makedirs(args.title)
|
||||
|
||||
# copy template files -- this needs to change....
|
||||
#home = os.path.dirname(os.path.realpath(__file__))
|
||||
os.system('cp -aR ' + TEMPLATE_PATH + '/* ' + args.title)
|
||||
|
||||
os.chdir(args.title)
|
||||
|
||||
# copy note file if it exists
|
||||
if args.file is not None and os.path.isfile(args.file):
|
||||
shutil.copy2(args.file, os.path.basename(args.file))
|
||||
|
||||
# style
|
||||
style_arg = ''
|
||||
if args.style is not None:
|
||||
os.system('sed -i.bak "s#CSL\ :=#CSL\ := ' + args.style + '#g" Makefile')
|
||||
style_arg = '-s ' + args.style
|
||||
|
||||
# bibtex item
|
||||
bibtexitem_arg = ''
|
||||
if args.bibtexitem is not None:
|
||||
bibtexitem_arg = '-i ' + args.bibtexitem
|
||||
|
||||
# generate bibliographic element
|
||||
if args.bibtex is not None and os.path.isfile(args.bibtex):
|
||||
os.system('sed -i.bak "s#BIB\ :=#BIB\ := ' + args.bibtex + '#g" Makefile')
|
||||
ref = '> ' + subprocess.check_output('csl_unsorted ' + args.bibtex + ' ' + bibtexitem_arg + ' ' + style_arg + ' -f md', shell=True).rstrip()
|
||||
os.system('sed -i.bak "s~*MACHINE-REF*~' + ref + '~g ; s~%\ title~%\ ' + ref + '~g" notes.mmd')
|
||||
|
||||
author = getpass.getuser()
|
||||
date = time.strftime("%d/%m/%Y")
|
||||
|
||||
os.system('sed -i.bak "s~%\ author~%\ ' + author + '~g ; s~%\ date~%\ ' + date + '~g" notes.mmd')
|
||||
|
||||
# cleanup
|
||||
os.system('rm *.bak')
|
||||
os.chdir('..')
|
||||
|
||||
Reference in New Issue
Block a user