exception handling

This commit is contained in:
gauthiier 2014-08-25 19:23:13 +02:00
parent e88d0d1973
commit ccb5616d48
3 changed files with 85 additions and 35 deletions

View File

@ -49,23 +49,41 @@ def open_file(p):
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) < 2: if len(sys.argv) < 2:
sys.exit('No input file... Aborting.') sys.exit('No input file... Aborting.')
fp1 = open_file(sys.argv[1]) try:
fp1 = open_file(sys.argv[1])
except:
sys.exit("Can't open file " + sys.argv[1] + ". Aborting.")
if len(sys.argv) < 3: if len(sys.argv) < 3:
fp2 = sys.stdin fp2 = sys.stdin
else: else:
fp2 = open_file(sys.argv[2]) try:
fp2 = open_file(sys.argv[2])
except:
sys.exit("Can't open file " + sys.argv[2] + ". Aborting.")
data1 = json.load(fp1) try:
data2 = json.load(fp2) sdata = fp1.read()
data1 = json.loads(sdata)
# print "----" except:
# print data1 e = "<compare> Error loading data from" + sys.argv[1] + ". Aborting.\n"
# print "----" if sdata:
# print data2 e += "Traceback: " + sdata1
# print "----" fp2.close()
sys.exit(e)
fp1.close() finally:
fp2.close() fp1.close()
try:
sdata = fp2.read()
data2 = json.loads(sdata)
except:
e = "<compare> Error loading data. Aborting.\n"
if sdata:
e += "Traceback: " + sdata
fp1.close()
sys.exit(e)
finally:
fp2.close()
data = difference(data1, data2) data = difference(data1, data2)

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
from statemachine import StateMachine from statemachine import StateMachine
import sys, os, string, json, shutil, codecs import sys, os, string, json, shutil, codecs, traceback
quote_nbr = sys.maxint quote_nbr = sys.maxint
fileout = '' fileout = ''
@ -170,17 +170,35 @@ if __name__ == '__main__':
if len(sys.argv) < 2: if len(sys.argv) < 2:
sys.exit('No input file... Aborting.') sys.exit('No input file... Aborting.')
# fp1 should be the incoming .mmd file # fp1 should be the incoming .mmd file
fileref = backupfile(sys.argv[1]) try:
fileout = open_fileoutput(sys.argv[1]) fileref = backupfile(sys.argv[1])
fileout.seek(0) fileout = open_fileoutput(sys.argv[1])
fileout.seek(0)
except:
sys.exit("Can't open file " + sys.argv[1] + ". Aborting.")
if len(sys.argv) < 3: if len(sys.argv) < 3:
fp2 = sys.stdin fp2 = sys.stdin
else: else:
fp2 = open_file(sys.argv[2]) try:
fp2 = open_file(sys.argv[2])
except:
sys.exit("Can't open file " + sys.argv[2] + ". Aborting.")
# fp2 should be the incoming (json) data to inject in fp1 # fp2 should be the incoming (json) data to inject in fp1
data = json.load(fp2)
fp2.close() try:
sdata = fp2.read()
data = json.loads(sdata)
except:
e = "<inject> Error loading data. Aborting\n"
if sdata:
e += "Traceback: " + sdata
fileout.close()
fileref.close()
sys.exit(e)
finally:
fp2.close()
if not data['QUOTES'] and not data['NOTES']: if not data['QUOTES'] and not data['NOTES']:
print "Document up-to-date." print "Document up-to-date."
@ -192,18 +210,27 @@ if __name__ == '__main__':
notes_cnt = 0 notes_cnt = 0
m = StateMachine(); try:
m.add_state(parse) m = StateMachine();
m.add_state(NOTES) m.add_state(parse)
m.add_state(QUOTES) m.add_state(NOTES)
m.add_state(process_quote) m.add_state(QUOTES)
m.add_state(process_note) m.add_state(process_quote)
m.add_state(error, end_state=1) m.add_state(process_note)
m.add_state(eof, end_state=1) m.add_state(error, end_state=1)
m.set_start(parse) m.add_state(eof, end_state=1)
m.run((fileref, '')) m.set_start(parse)
m.run((fileref, ''))
fileout.close() except:
fileref.close() trace = "Error injecting.\n\nTrace: \n"
trace += traceback.format_exc()
else:
trace = "Done injection."
finally:
fileout.close()
fileref.close()
sys.exit(trace)

View File

@ -4,8 +4,8 @@ for i in *.pdf; do
f=$i f=$i
done done
if [[ -z '$f' ]]; then if [[ -d '$f' ]]; then
echo "No pdf file in directory... Aborting."; echo "$f No pdf file in directory... Aborting.";
exit; exit;
fi fi
@ -13,6 +13,11 @@ filename="${f%.*}"
skimnotes get -format txt $f skimnotes get -format txt $f
if [[ ! -f '$filename.txt' ]]; then
echo "No skim notes in pdf $f. Aborting.";
exit;
fi
parse-skim.py < "$filename.txt" parse-skim.py < "$filename.txt"
rm $filename.txt rm $filename.txt