diff --git a/README.md b/README.md index 988a1a5..112c364 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/main.py b/src/main.py index ccacee0..9948688 100755 --- a/src/main.py +++ b/src/main.py @@ -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__) @@ -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) diff --git a/src/qti_parser/assessment_meta/__init__.py b/src/qti_parser/assessment_meta/__init__.py index 2cdaf1f..ea350b0 100644 --- a/src/qti_parser/assessment_meta/__init__.py +++ b/src/qti_parser/assessment_meta/__init__.py @@ -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,