def import_course(course_dir, verbose=True): course_dir = path(course_dir) data_dir = course_dir.dirname() course_dirs = [course_dir.basename()] # No default class--want to complain if it doesn't find plugins for any # module. modulestore = XMLModuleStore(data_dir, default_class=None, course_dirs=course_dirs) def str_of_err(tpl): (msg, exc_str) = tpl return '{msg}\n{exc}'.format(msg=msg, exc=exc_str) courses = modulestore.get_courses() n = len(courses) if n != 1: sys.stderr.write('ERROR: Expect exactly 1 course. Loaded {n}: {lst}\n'.format( n=n, lst=courses)) return None course = courses[0] errors = modulestore.get_item_errors(course.location) if len(errors) != 0: sys.stderr.write('ERRORs during import: {0}\n'.format( '\n'.join(map(str_of_err, errors)))) return course
def test_colon_in_url_name(self): """Ensure that colons in url_names convert to file paths properly""" print("Starting import") # Not using get_courses because we need the modulestore object too afterward modulestore = XMLModuleStore(DATA_DIR, course_dirs=['toy']) courses = modulestore.get_courses() self.assertEquals(len(courses), 1) course = courses[0] course_id = course.id print("course errors:") for (msg, err) in modulestore.get_item_errors(course.location): print(msg) print(err) chapters = course.get_children() self.assertEquals(len(chapters), 2) ch2 = chapters[1] self.assertEquals(ch2.url_name, "secret:magic") print("Ch2 location: ", ch2.location) also_ch2 = modulestore.get_instance(course_id, ch2.location) self.assertEquals(ch2, also_ch2) print("making sure html loaded") cloc = course.location loc = Location(cloc.tag, cloc.org, cloc.course, 'html', 'secret:toylab') html = modulestore.get_instance(course_id, loc) self.assertEquals(html.display_name, "Toy lab")
def test_colon_in_url_name(self): """Ensure that colons in url_names convert to file paths properly""" print("Starting import") # Not using get_courses because we need the modulestore object too # afterward modulestore = XMLModuleStore(DATA_DIR, course_dirs=['toy']) courses = modulestore.get_courses() self.assertEquals(len(courses), 1) course = courses[0] course_id = course.id print("course errors:") for (msg, err) in modulestore.get_item_errors(course.location): print(msg) print(err) chapters = course.get_children() self.assertEquals(len(chapters), 2) ch2 = chapters[1] self.assertEquals(ch2.url_name, "secret:magic") print("Ch2 location: ", ch2.location) also_ch2 = modulestore.get_instance(course_id, ch2.location) self.assertEquals(ch2, also_ch2) print("making sure html loaded") cloc = course.location loc = Location(cloc.tag, cloc.org, cloc.course, 'html', 'secret:toylab') html = modulestore.get_instance(course_id, loc) self.assertEquals(html.display_name, "Toy lab")
def test_unicode(self): """Check that courses with unicode characters in filenames and in org/course/name import properly. Currently, this means: (a) Having files with unicode names does not prevent import; (b) if files are not loaded because of unicode filenames, there are appropriate exceptions/errors to that effect.""" print("Starting import") modulestore = XMLModuleStore(DATA_DIR, course_dirs=['test_unicode']) courses = modulestore.get_courses() self.assertEquals(len(courses), 1) course = courses[0] print("course errors:") # Expect to find an error/exception about characters in "®esources" expect = "Invalid characters" errors = [(msg.encode("utf-8"), err.encode("utf-8")) for msg, err in modulestore.get_item_errors(course.location)] self.assertTrue(any(expect in msg or expect in err for msg, err in errors)) chapters = course.get_children() self.assertEqual(len(chapters), 3)
def import_course(course_dir, verbose=True): course_dir = path(course_dir) data_dir = course_dir.dirname() course_dirs = [course_dir.basename()] # No default class--want to complain if it doesn't find plugins for any # module. modulestore = XMLModuleStore(data_dir, default_class=None, course_dirs=course_dirs) def str_of_err(tpl): (msg, exc_str) = tpl return '{msg}\n{exc}'.format(msg=msg, exc=exc_str) courses = modulestore.get_courses() n = len(courses) if n != 1: sys.stderr.write( 'ERROR: Expect exactly 1 course. Loaded {n}: {lst}\n'.format( n=n, lst=courses)) return None course = courses[0] errors = modulestore.get_item_errors(course.location) if len(errors) != 0: sys.stderr.write('ERRORs during import: {0}\n'.format('\n'.join( map(str_of_err, errors)))) return course
def test_unicode_chars_in_xml_content(self): # edX/full/6.002_Spring_2012 has non-ASCII chars, and during # uniquification of names, would raise a UnicodeError. It no longer does. # Ensure that there really is a non-ASCII character in the course. with open( os.path.join( DATA_DIR, "full/sequential/Administrivia_and_Circuit_Elements.xml") ) as xmlf: xml = xmlf.read() with assert_raises(UnicodeDecodeError): xml.decode('ascii') # Load the course, but don't make error modules. This will succeed, # but will record the errors. modulestore = XMLModuleStore(DATA_DIR, course_dirs=['full'], load_error_modules=False) # Look up the errors during load. There should be none. location = CourseDescriptor.id_to_location( "edX/full/6.002_Spring_2012") errors = modulestore.get_item_errors(location) assert errors == []
def import_with_checks(course_dir, verbose=True): all_ok = True print "Attempting to load '{0}'".format(course_dir) course_dir = path(course_dir) data_dir = course_dir.dirname() course_dirs = [course_dir.basename()] # No default class--want to complain if it doesn't find plugins for any # module. modulestore = XMLModuleStore(data_dir, default_class=None, course_dirs=course_dirs) def str_of_err(tpl): (msg, exc_str) = tpl return '{msg}\n{exc}'.format(msg=msg, exc=exc_str) courses = modulestore.get_courses() n = len(courses) if n != 1: print 'ERROR: Expect exactly 1 course. Loaded {n}: {lst}'.format( n=n, lst=courses) return (False, None) course = courses[0] errors = modulestore.get_item_errors(course.location) if len(errors) != 0: all_ok = False print '\n' print "=" * 40 print 'ERRORs during import:' print '\n'.join(map(str_of_err, errors)) print "=" * 40 print '\n' #print course validators = ( traverse_tree, ) print "=" * 40 print "Running validators..." for validate in validators: print 'Running {0}'.format(validate.__name__) all_ok = validate(course) and all_ok if all_ok: print 'Course passes all checks!' else: print "Course fails some checks. See above for errors." return all_ok, course
def test_unicode_chars_in_xml_content(self): # edX/full/6.002_Spring_2012 has non-ASCII chars, and during # uniquification of names, would raise a UnicodeError. It no longer does. # Ensure that there really is a non-ASCII character in the course. with open(os.path.join(DATA_DIR, "toy/sequential/vertical_sequential.xml")) as xmlf: xml = xmlf.read() with assert_raises(UnicodeDecodeError): xml.decode('ascii') # Load the course, but don't make error modules. This will succeed, # but will record the errors. modulestore = XMLModuleStore(DATA_DIR, course_dirs=['toy'], load_error_modules=False) # Look up the errors during load. There should be none. location = CourseDescriptor.id_to_location("edX/toy/2012_Fall") errors = modulestore.get_item_errors(location) assert errors == []