nnnotes/bin/nnnote
2016-03-16 14:30:32 +01:00

84 lines
2.5 KiB
Python
Executable File

#!/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('..')