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 len(sys.argv) < 2:
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:
fp2 = sys.stdin
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)
data2 = json.load(fp2)
# print "----"
# print data1
# print "----"
# print data2
# print "----"
fp1.close()
fp2.close()
try:
sdata = fp1.read()
data1 = json.loads(sdata)
except:
e = "<compare> Error loading data from" + sys.argv[1] + ". Aborting.\n"
if sdata:
e += "Traceback: " + sdata1
fp2.close()
sys.exit(e)
finally:
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)

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python
from statemachine import StateMachine
import sys, os, string, json, shutil, codecs
import sys, os, string, json, shutil, codecs, traceback
quote_nbr = sys.maxint
fileout = ''
@ -170,17 +170,35 @@ if __name__ == '__main__':
if len(sys.argv) < 2:
sys.exit('No input file... Aborting.')
# fp1 should be the incoming .mmd file
fileref = backupfile(sys.argv[1])
fileout = open_fileoutput(sys.argv[1])
fileout.seek(0)
try:
fileref = backupfile(sys.argv[1])
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:
fp2 = sys.stdin
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
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']:
print "Document up-to-date."
@ -192,18 +210,27 @@ if __name__ == '__main__':
notes_cnt = 0
m = StateMachine();
m.add_state(parse)
m.add_state(NOTES)
m.add_state(QUOTES)
m.add_state(process_quote)
m.add_state(process_note)
m.add_state(error, end_state=1)
m.add_state(eof, end_state=1)
m.set_start(parse)
m.run((fileref, ''))
fileout.close()
fileref.close()
try:
m = StateMachine();
m.add_state(parse)
m.add_state(NOTES)
m.add_state(QUOTES)
m.add_state(process_quote)
m.add_state(process_note)
m.add_state(error, end_state=1)
m.add_state(eof, end_state=1)
m.set_start(parse)
m.run((fileref, ''))
except:
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
done
if [[ -z '$f' ]]; then
echo "No pdf file in directory... Aborting.";
if [[ -d '$f' ]]; then
echo "$f No pdf file in directory... Aborting.";
exit;
fi
@ -13,6 +13,11 @@ filename="${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"
rm $filename.txt