Commit 591f9023 by Piotr Mitros

Content parser moved to lxml

parent 95842602
...@@ -4,10 +4,10 @@ try: ...@@ -4,10 +4,10 @@ try:
except: except:
settings = None settings = None
from xml.dom.minidom import parse, parseString
from lxml import etree from lxml import etree
import json
''' This file will eventually form an abstraction layer between the ''' This file will eventually form an abstraction layer between the
course XML file and the rest of the system. course XML file and the rest of the system.
...@@ -56,7 +56,6 @@ def item(l, default="", process=lambda x:x): ...@@ -56,7 +56,6 @@ def item(l, default="", process=lambda x:x):
else: else:
raise Exception('Malformed XML') raise Exception('Malformed XML')
def course_file(user): def course_file(user):
# TODO: Cache. Also, return the libxml2 object. # TODO: Cache. Also, return the libxml2 object.
return settings.DATA_DIR+UserProfile.objects.get(user=user).courseware return settings.DATA_DIR+UserProfile.objects.get(user=user).courseware
...@@ -84,26 +83,26 @@ def module_xml(coursefile, module, id_tag, module_id): ...@@ -84,26 +83,26 @@ def module_xml(coursefile, module, id_tag, module_id):
#return result_set[0].serialize() #return result_set[0].serialize()
def toc_from_xml(coursefile, active_chapter, active_section): def toc_from_xml(coursefile, active_chapter, active_section):
dom=parse(coursefile) dom2 = etree.parse(coursefile)
name = dom2.xpath('//course/@name')[0]
course = dom.getElementsByTagName('course')[0] chapters = dom2.xpath('//course[@name=$name]/chapter', name=name)
name=course.getAttribute("name")
chapters = course.getElementsByTagName('chapter')
ch=list() ch=list()
for c in chapters: for c in chapters:
if c.getAttribute("name") == 'hidden': if c.get('name') == 'hidden':
continue continue
sections=list() sections=list()
for s in c.getElementsByTagName('section'): for s in dom2.xpath('//course[@name=$name]/chapter[@name=$chname]/section', name=name, chname=c.get('name')):
sections.append({'name':s.getAttribute("name"), sections.append({'name':s.get("name") or "",
'time':s.getAttribute("time"), 'time':s.get("time") or "",
'format':s.getAttribute("format"), 'format':s.get("format") or "",
'due':s.getAttribute("due"), 'due':s.get("due") or "",
'active':(c.getAttribute("name")==active_chapter and \ 'active':(c.get("name")==active_chapter and \
s.getAttribute("name")==active_section)}) s.get("name")==active_section)})
ch.append({'name':c.getAttribute("name"), ch.append({'name':c.get("name"),
'sections':sections, 'sections':sections,
'active':(c.getAttribute("name")==active_chapter)}) 'active':(c.get("name")==active_chapter)})
return ch return ch
def dom_select(dom, element_type, element_name): def dom_select(dom, element_type, element_name):
......
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