Improved error handling and ran pylint

parent b1adbfb5
......@@ -6,20 +6,27 @@ main_writer.py and req_writer.py.
import re
import csv
def get_courses():
with open('course.lp', 'r') as fil:
valid_courses = re.findall('[a-z]+[0-9][0-9V][0-9]{2}', fil.read())
return valid_courses
def get_required_courses(degree, major, year):
filename = '{0}/{1}/{2}/{1}{2}req.lp'.format(degree, major, year)
contents = open(filename).read()
try:
contents = open(filename).read()
except IOError:
raise IOError('error: could not read {}'.format(filename))
req_courses = re.findall('[a-z]+[0-9][0-9V][0-9]{2}', contents)
return req_courses
def parse_transcript(transcript):
if type(transcript) is str:
transcript = trascript.splitlines()
if isinstance(transcript, str) is str:
transcript = transcript.splitlines()
csvr = list(csv.reader(transcript))
student = csvr[0][0]
......@@ -28,21 +35,23 @@ def parse_transcript(transcript):
return (student, degree, major, year, spec, courses)
def gen_main(student, degree, major, year, spec, courses):
student_fact = "student({0},{0}).".format(student)
if spec != 'none':
spec_string = '_specialization({0},{1}).\n'.format(student,spec)
spec_string = '_specialization({0},{1}).\n'.format(student, spec)
else:
spec_string = str()
taken_list = '_taken({},['.format(student) # the _taken(<student>,[c1,c2...cn]). atom
has_taken = str() # series of _hasTaken/2 atoms
# the _taken(<student>,[c1,c2...cn]). atom
taken_list = '_taken({},['.format(student)
has_taken = str() # series of _hasTaken/2 atoms
try:
req = get_required_courses(degree, major, year)
except IOError:
print('requirements file not found', file=sys.stderr)
except IOError as err:
raise err
for course in courses:
# include only those courses that could possibly
......@@ -50,7 +59,7 @@ def gen_main(student, degree, major, year, spec, courses):
# in the 'taken list'
if not '_req(%s,required).' % course in req:
taken_list += course + ','
has_taken += '_hasTaken({0},{1}).\n'.format(student, course)
# strip trailing comma
......@@ -58,9 +67,10 @@ def gen_main(student, degree, major, year, spec, courses):
taken_list += ']).\n'
postamble = "#include 'counting.lp'.\n"\
"#include '{0}/{1}/{2}/{1}{2}rules.lp'.\n\n"\
'?- _main({3}).\n'.format(degree, major, year, student)
"#include '{0}/{1}/{2}/{1}{2}rules.lp'.\n\n"\
'?- _main({3}).\n'.format(degree, major, year, student)
mainlp = '\n'.join([student_fact,spec_string,has_taken,taken_list,postamble])
mainlp = '\n'.join(
[student_fact, spec_string, has_taken, taken_list, postamble])
return mainlp
......@@ -8,6 +8,7 @@ from __future__ import print_function
import sys
import gradaudit
def main():
"""
This program writes 'main.lp'.
......@@ -24,15 +25,17 @@ def main():
try:
transcript = open(transcript)
except IOError:
print("transcript not found", file=sys.stderr)
print("error: {} not found".format(transcript), file=sys.stderr)
sys.exit(1)
student_data = gradaudit.parse_transcript(transcript)
valid_courses = gradaudit.get_courses()
main_contents = gradaudit.gen_main(*student_data)
try:
main_contents = gradaudit.gen_main(*student_data)
except IOError as e:
print(str(e), file=sys.stderr)
sys.exit(1)
with open('main.lp', 'w') as main_file:
main_file.write(main_contents)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment