示例#1
0
文件: numbas.py 项目: sangwinc/Numbas
def makeExam(options):
    data = open(options.source, encoding="utf-8").read()
    if options.xml:
        examXML = data
        options.resources = []
        options.extensions = []
    else:
        try:
            exam = Exam.fromstring(data)
            examXML = exam.tostring()
            options.resources = exam.resources
            options.extensions = exam.extensions
        except examparser.ParseError as err:
            print("Failed to compile exam due to parsing error.")
            raise
        except:
            print("Failed to compile exam")
            raise
    options.examXML = examXML
    options.xmls = xml2js(options)

    if options.zip:
        compileToZip(options)
    else:
        compileToDir(options)
示例#2
0
def makeExam(options):
    data = open(options.source, encoding='utf-8').read()
    if (options.xml):
        examXML = data
        options.resources = []
        options.extensions = []
    else:
        exam = Exam.fromstring(data)
        examXML = exam.tostring()
        options.resources = exam.resources
        options.extensions = exam.extensions
    options.examXML = examXML
    options.xmls = xml2js(options)

    if options.zip:
        compileToZip(options)
    else:
        compileToDir(options)
示例#3
0
def makeExam(options):
	data = open(options.source,encoding='utf-8').read()
	if(options.xml):
		examXML = data
		options.resources=[]
		options.extensions=[]
	else:
		exam = Exam.fromstring(data)
		examXML = exam.tostring()
		options.resources = exam.resources
		options.extensions = exam.extensions
	options.examXML = examXML
	options.xmls = xml2js(options)


	if options.zip:
		compileToZip(options)
	else:
		compileToDir(options)
示例#4
0
def makeExam(options):
    try:
        exam = Exam.fromstring(options.source)
        examXML = exam.tostring()
        options.resources = exam.resources
        options.extensions = exam.extensions
    except ExamError as err:
        raise Exception('Error constructing exam:\n%s' % err)
    except examparser.ParseError as err:
        raise Exception("Failed to compile exam due to parsing error.\n%s" %
                        err)
    except:
        raise Exception('Failed to compile exam.')

    options.examXML = examXML
    options.xmls = xml2js(options)

    files = collectFiles(options)
    files[os.path.join('.', 'settings.js')] = io.StringIO(options.xmls)

    localePath = os.path.join(options.path, 'locales', options.locale + '.js')
    if not os.path.exists(localePath):
        raise Exception("Can't find locale " + options.locale)
    files[os.path.join('.', 'locale.js')] = io.StringIO(
        open(localePath, encoding='utf-8').read())

    if options.scorm:
        IMSprefix = '{http://www.imsglobal.org/xsd/imscp_v1p1}'
        manifest = etree.fromstring(
            open(os.path.join(options.path, 'scormfiles',
                              'imsmanifest.xml')).read())
        manifest.attrib['identifier'] = 'Numbas: %s' % exam.name
        manifest.find('%sorganizations/%sorganization/%stitle' %
                      (IMSprefix, IMSprefix, IMSprefix)).text = exam.name

        def to_relative_url(path):
            path = os.path.normpath(path)
            bits = []
            head, tail = os.path.split(path)
            while head != '':
                bits.insert(0, tail)
                head, tail = os.path.split(head)
            bits.insert(0, tail)
            return '/'.join(bits)

        resource_files = [to_relative_url(x) for x in files.keys()]

        resource_element = manifest.find('%sresources/%sresource' %
                                         (IMSprefix, IMSprefix))
        for filename in resource_files:
            file_element = etree.Element('file')
            file_element.attrib = {'href': filename}
            resource_element.append(file_element)

        files.update(collectFiles(options, [('scormfiles', '.')]))

        manifest_string = etree.tostring(manifest)
        try:
            manifest_string = manifest_string.decode('utf-8')
        except AttributeError:
            pass

        files[os.path.join('.',
                           'imsmanifest.xml')] = io.StringIO(manifest_string)

    stylesheets = [(dst, src) for dst, src in files.items()
                   if os.path.splitext(dst)[1] == '.css']
    for dst, src in stylesheets:
        del files[dst]
    stylesheets = [src for dst, src in stylesheets]
    stylesheets = '\n'.join(
        open(src, encoding='utf-8').read() if isinstance(src, basestring
                                                         ) else src.read()
        for src in stylesheets)
    files[os.path.join('.', 'styles.css')] = io.StringIO(stylesheets)

    javascripts = [(dst, src) for dst, src in files.items()
                   if os.path.splitext(dst)[1] == '.js']
    for dst, src in javascripts:
        del files[dst]

    javascripts = [src for dst, src in javascripts]
    numbas_loader_path = os.path.join(options.path, 'runtime', 'scripts',
                                      'numbas.js')
    javascripts.remove(numbas_loader_path)
    javascripts.insert(0, numbas_loader_path)
    javascripts = '\n'.join(
        open(src, encoding='utf-8').read() if isinstance(src, basestring
                                                         ) else src.read()
        for src in javascripts)
    files[os.path.join('.', 'scripts.js')] = io.StringIO(javascripts)

    if options.minify:
        for dst, src in files.items():
            if isinstance(src,
                          basestring) and os.path.splitext(dst)[1] == '.js':
                p = subprocess.Popen([options.minify, src],
                                     stdin=subprocess.PIPE,
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE)
                out, err = p.communicate()
                code = p.poll()
                if code != 0:
                    raise Exception('Failed to minify %s with minifier %s' %
                                    (src, options.minify))
                else:
                    files[dst] = io.StringIO(out.decode('utf-8'))

    if options.zip:
        compileToZip(exam, files, options)
    else:
        compileToDir(exam, files, options)