Skip to content

Commit

Permalink
Merge pull request #1 from olearydj/main
Browse files Browse the repository at this point in the history
feat: fully pathed inputs
  • Loading branch information
rolfis authored Sep 21, 2023
2 parents df69733 + 05a1c1b commit 7e0812d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ This Python code is provided as-is and enhancements are welcome. The goal is to

Uses pipenv for package dependencies. Install pipenv with `pip install pipenv` then run `pipenv install` to install needed packages in virtual environment.

Note: This code has only been tested with Canvas export packages. They need to be unzipped first. All paths are relative, so `cd` to the export directory first, then run main.py to get all paths correct. This will eventually be fixed in the future.
Note: This code has only been tested with Canvas export packages. They need to be unzipped first.

Specify the input file using either a relative or absolute path. Ensure the path is correctly set based on your current working directory.


## Examples
Expand Down
7 changes: 6 additions & 1 deletion src/formats/docx.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ def write_file(data, outfile):

for assessment in data['assessment']:
doc.add_heading(assessment['metadata']['title'], 0)
html_parser.add_html_to_document(assessment['metadata']['description'], doc)

# handle error from missing description
description = assessment['metadata'].get('description') or ''
logger.info("Writing assessment: " + assessment['metadata']['title'])
logger.info("with description: " + description)
html_parser.add_html_to_document(description, doc)

for question in assessment['question']:
if 'title' in question:
Expand Down
24 changes: 18 additions & 6 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@
"""

import argparse
import hashlib
import json
import re
import hashlib
from pathlib import Path

from logzero import logger
from lxml import etree
from qti_parser import assessment_meta, item

import config
import formats
from qti_parser import assessment_meta, item

__author__ = config.__author__
__description__ = config.__description__
__license__ = config.__license__
__version__ = config.__version__


def main(args):
logger.info(__description__)

Expand All @@ -29,16 +33,24 @@ def main(args):
}

for xml_resource in xml_doc.getroot().findall(".//{http://www.imsglobal.org/xsd/imsccv1p1/imscp_v1p1}resource[@type='imsqti_xmlv1p2']"):
# allow fully pathed input files using pathlib's Path objects
input_path = Path(args.input)
metadata_path = (input_path.parent / xml_resource.get("identifier") / "assessment_meta.xml")

this_assessment = {
'id': xml_resource.get("identifier"),
'metadata': assessment_meta.get_metadata(xml_resource.get("identifier") + "/" + "assessment_meta.xml"),
'metadata': assessment_meta.get_metadata(metadata_path),
'question': []
}

# TODO: Should be prefixed with PATH part of input filename since paths in XML are relative
this_assessment_xml = this_assessment['id'] + "/" + this_assessment['id'] + ".xml"
this_assessment_xml = (input_path.parent / this_assessment['id'] / (this_assessment['id'] + ".xml"))
logger.info(f"this assessment: {this_assessment_xml}")

for xml_item in etree.parse(this_assessment_xml).getroot().findall(".//{http://www.imsglobal.org/xsd/ims_qtiasiv1p2}item"):
for xml_item in (
etree.parse(str(this_assessment_xml))
.getroot()
.findall(".//{http://www.imsglobal.org/xsd/ims_qtiasiv1p2}item")
):
this_assessment['question'].append(item.get_question(xml_item))

qti_resource['assessment'].append(this_assessment)
Expand Down
5 changes: 3 additions & 2 deletions src/qti_parser/assessment_meta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
from lxml import etree
from logzero import logger

def get_metadata(file):

def get_metadata(file_path):
""" Extracts basic metadata """
metadata = {}

try:
xml = etree.parse(file).getroot()
xml = etree.parse(str(file_path)).getroot()
metadata = {
'title': xml.find("./{http://canvas.instructure.com/xsd/cccv1p0}title").text,
'description': xml.find("./{http://canvas.instructure.com/xsd/cccv1p0}description").text,
Expand Down

0 comments on commit 7e0812d

Please sign in to comment.