Changeset 325

Show
Ignore:
Timestamp:
07/03/09 10:26:36 (3 years ago)
Author:
jon
Message:

Adding back old imports without transform

Location:
collective.imstransport/trunk/collective/imstransport/utilities
Files:
9 added
6 modified

Legend:

Unmodified
Added
Removed
  • collective.imstransport/trunk/collective/imstransport/utilities/bb/bbreader.py

    r177 r325  
    1 # from zope.interface import implements 
    2 # from collective.imstransport.utilities.interfaces import IIMSManifestReader, IIMSTransportUtility, IIMSObjectCreator 
    3 from collective.imstransport import IMSTransportMessageFactory as _ 
    4 # from zipfile import ZipFile 
    5 # from zope.component import getUtility 
    6 # from Products.CMFCore.interfaces import ISiteRoot 
    7 # from elementtree import ElementTree 
    8 # import re 
    9 from zope.component import getUtility 
    10 from collective.imstransport.utilities.imsinterchange import IMSInterchangeReader 
     1from xml.dom import minidom 
     2from collective.imstransport.IMS_exceptions import ManifestError 
     3from configbb import LOM_BB_namespace, XML_namespace 
     4 
     5class BBReader(object): 
     6 
     7    def parseManifest(self, manifest): 
     8        """ parse the manifest """ 
     9        return self.parseDataFile(manifest) 
     10 
     11    def parseDataFile(self, dataxml): 
     12        """ Parse the datafile """ 
     13        return minidom.parseString(dataxml) 
     14 
     15    def readOrganizations(self, manifest): 
     16        """ Read the organizations for the manifest """ 
     17        orgs = {} 
     18        organizations = manifest.getElementsByTagName('organizations') 
     19        if organizations: 
     20            organization_nodes = organizations[0].getElementsByTagName('organization') 
     21            if organization_nodes: 
     22                organization_node = organization_nodes[0] 
     23                item_nodes = organization_nodes[0].getElementsByTagName('item') 
     24                for item in item_nodes: 
     25                    idref = item.getAttribute('identifierref') 
     26                    titlenodes = item.getElementsByTagName('title') 
     27                    if titlenodes: 
     28                        title = self.getTextValue(titlenodes[0]) 
     29                        orgs[idref] =  title 
     30        return orgs 
     31 
     32    def readResources(self, manifest): 
     33        """ Read all resources. """ 
     34        reslist = [] 
     35        resources = manifest.getElementsByTagName('resources') 
     36        if resources: 
     37            reslist =  resources[0].getElementsByTagName('resource') 
     38        return reslist 
     39 
     40    def getTextValue(self, node): 
     41        """ Removes the text from the text_node of a node """ 
     42        for x in node.childNodes: 
     43            if x.nodeType == x.TEXT_NODE: 
     44                return x.nodeValue.strip() 
     45        return None 
     46 
     47    def readResourceAttributes(self, resource): 
     48        """ Return attributes on resource node. """ 
     49        return (resource.getAttribute('identifier'), 
     50                resource.getAttribute('type'), 
     51                resource.getAttributeNS(LOM_BB_namespace, 'file'), 
     52                resource.getAttributeNS(LOM_BB_namespace, 'title'), 
     53                resource.getAttributeNS(XML_namespace, 'base')) 
     54 
     55    def readFiles(self, resource): 
     56        files = [] 
     57        flns = resource.getElementsByTagName('file') 
     58        if flns: 
     59            for fln in flns: 
     60                file = fln.getAttribute('href') 
     61                files.append(file) 
     62        return files 
     63 
     64    def readMetadata(self, content): 
     65        """ Read metadata from data files """ 
     66        md = {} 
     67        self.readContentMetadata(content, md) 
     68        return md 
     69 
     70    def readContentMetadata(self, metadata, md): 
     71        """ Read the metadata from a content file """ 
     72        content_nodes = metadata.getElementsByTagName('CONTENT') 
     73        if content_nodes: 
     74            content_node = content_nodes[0] 
     75            title_nodes = content_node.getElementsByTagName('TITLE')         
     76            if title_nodes: 
     77                title = title_nodes[0].getAttribute('value') 
     78                md['title'] = title     
     79            body_nodes = content_node.getElementsByTagName('BODY') 
     80            if body_nodes: 
     81                text_nodes = body_nodes[0].getElementsByTagName('TEXT') 
     82                if text_nodes: 
     83                    md['text'] = self.getTextValue(text_nodes[0]) 
     84            date_nodes = content_node.getElementsByTagName('DATES') 
     85            if date_nodes: 
     86                created_nodes = date_nodes[0].getElementsByTagName('CREATED') 
     87                if created_nodes: 
     88                    md['creation_date'] = created_nodes[0].getAttribute('value') 
     89            flag_nodes = content_node.getElementsByTagName('FLAGS') 
     90            if flag_nodes: 
     91                isfolder_nodes = flag_nodes[0].getElementsByTagName('ISFOLDER') 
     92                if isfolder_nodes: 
     93                    value = isfolder_nodes[0].getAttribute('value') 
     94                    if value ==  'true': 
     95                        md['type'] = 'Folder' 
     96 
     97    def readTocItem(self, manifest, resid): 
     98        """ Read the toc page and find child nodes """ 
     99        tocitems = [] 
     100        organizations = manifest.getElementsByTagName('organizations') 
     101        if organizations: 
     102            organization_nodes = organizations[0].getElementsByTagName('organization') 
     103            if organization_nodes: 
     104                organization_node = organization_nodes[0] 
     105                item_nodes = organization_nodes[0].getElementsByTagName('item') 
     106                for item in item_nodes: 
     107                    idref = item.getAttribute('identifierref') 
     108                    if idref == resid: 
     109                        childitems = item.getElementsByTagName('item') 
     110                        for x in childitems: 
     111                            itemid = x.getAttribute('identifierref') 
     112                            tocitems.append(itemid) 
     113        return tocitems 
     114 
     115         
     116                 
     117                 
     118             
     119 
     120             
     121             
     122                 
    11123 
    12124 
    13 class IMSBBReader(IMSInterchangeReader): 
    14     """ Read an IMS CC package """ 
    15125 
    16  
    17     name = _(u'Blackboard') 
    18  
    19     BBNS = 'http://www.blackboard.com/content-packaging/' 
    20     XML = 'http://www.w3.org/XML/1998/namespace' 
    21  
    22     def getPackageName(self): 
    23         return self.name 
    24  
    25     def readPackage(self, file): 
    26         self.zf = ZipFile(file, 'r') 
    27         imsdoc = self.zf.read('imsmanifest.xml') 
    28126         
    29         # Read in resources section 
    30         self.tree = ElementTree.XML(imsdoc) 
    31  
    32         # Enter resource data into a dictionary 
    33         resource_dictionary = self.readResources({}, self.tree) 
    34  
    35         self.zf.close() 
    36  
    37         return resource_dictionary 
    38  
    39     def readResources(self, resDict, tree): 
    40  
    41         resourceNodes = tree.findall('resources/resource') 
    42          
    43         for resourceNode in resourceNodes: 
    44             self.readFiles(resDict, resourceNode) 
    45  
    46         return  resDict 
    47  
    48     def readFiles(self, resDict, resourceNode): 
    49  
    50         # Get information about the resource. 
    51         # 
    52         res_title = resourceNode.get('{%s}title' %(self.BBNS)) 
    53         # Determining which section it is in (e.g., Assignments, CourseInformation, ExternalLinks...) 
    54         res_title_parts = res_title.split(".") 
    55         if len(res_title_parts) == 4: 
    56             if res_title_parts[0] == 'COURSE_DEFAULT' and res_title_parts[2] == 'CONTENT_LINK' and res_title_parts[3] == 'label': 
    57                 # Converts camel case section title (e.g., CourseInformation) to its non-camelcase alternative and sets as title 
    58                 res_title = re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', ' \\1', res_title_parts[1]).strip(' ') 
    59         # Hash datfile information 
    60         datfile = resourceNode.get('{%s}file' %(self.BBNS)) 
    61         xmlbase = resourceNode.get('{%s}base' %(self.XML)) 
    62         # Hash the blackboard type information for the resource 
    63         bbtype = resourceNode.get('type') 
    64         # Hash the file information 
    65         fileNodes = resourceNode.findall('file') 
    66         id = resourceNode.get('identifier') 
    67          
    68      
    69         # Parse dat file 
    70         if datfile: 
    71             datText = self.readContentFile(datfile) 
    72         else: 
    73             datText = '' 
    74  
    75  
    76         # Show in navigation? 
    77         item_nodes_all = self.tree.findall("organizations//item") 
    78         targetItems = [node for node in item_nodes_all if node.get("identifierref") == id] 
    79         if targetItems: 
    80             excludeFromNav = False 
    81             # Are there any children? 
    82             child_nodes = targetItems[0].findall('item') 
    83         else: 
    84             excludeFromNav = True 
    85             child_nodes = [] 
    86  
    87  
    88         if len(fileNodes): 
    89             # Process resource with file nodes 
    90             files = [] 
    91             for fileNode in fileNodes: 
    92                 ## create the file object based on mimetype 
    93                 fileid = fileNode.get('href') 
    94                 files.append(('%s/%s/view' %(id, fileid),fileid)) 
    95                 type = self.determineFileMimetype(fileid) 
    96                 resDict[fileid] = {'file':fileid, 'path':id, 'title':fileid, 'id':fileid, 'type':type} 
    97             resText = self.TocPage('Table of Contents', files) 
    98         elif len(child_nodes): 
    99             # Create table of contents pages. 
    100             if not datText: 
    101                 items = [] 
    102                 for child_node in child_nodes: 
    103                     title_nodes = child_node.findall('title') 
    104                     if title_nodes: 
    105                         title = title_nodes[0].text 
    106                     else: 
    107                         title = '' 
    108                     items.append(('%s.html' %(child_node.get('identifierref')), title)) 
    109                 resText = self.TocPage('Table of Contents', items) 
    110             else: 
    111                 resText = datText 
    112         elif datText: 
    113             resText = datText 
    114         else: 
    115             resText = '' 
    116  
    117         resDict[id] = {'text':resText, 'path':'', 'title':res_title, 'id':'%s.html' %id, 'type':'Document'} 
    118  
    119  
    120     def TocPage(self, tabletitle, tocitems): 
    121          
    122         text = '' 
    123         text += '<table class="documentTable" style="width: 499px;" border="0" cellpadding="0" cellspacing="0">' 
    124         text += '<thead>' 
    125         text += '   <tr>' 
    126         text += '     <td>%s</td>' %tabletitle 
    127         text += '   </tr>' 
    128         text += '</thead>' 
    129         text += '<tbody>' 
    130         for tocitem in tocitems: 
    131             text += '     <tr tal:define="oddrow repeat/item/odd;" ' 
    132             text += '         tal:attributes="class oddrow)">' 
    133             text += '       <td ><a href="%s"' %tocitem[0]  
    134             text += '       >%s</a></td>' %tocitem[1] 
    135             text += '     </tr>' 
    136         text += '  </tbody>' 
    137         text += '</table>' 
    138  
    139         return text 
    140  
    141     def readContentFile(self, datfile): 
    142  
    143         # Read the proprietary blackboard.dat file 
    144         datdoc = self.zf.read(datfile)             
    145  
    146         # Parsing the dat file 
    147         contenttree = ElementTree.XML(datdoc) 
    148         text_nodes = contenttree.findall("BODY/TEXT") 
    149         resText = '' 
    150  
    151         # Get text information 
    152         if text_nodes: 
    153             resText = text_nodes[0].text 
    154  
    155         return resText 
    156  
    157  
    158     def determineFileMimetype(self, name): 
    159  
    160         portal = getUtility(ISiteRoot) 
    161         registry = portal.mimetypes_registry 
    162         mimetype = registry.lookupExtension(name) 
    163  
    164         if mimetype.major() == 'image': 
    165             return 'Image' 
    166         else: 
    167             return 'File' 
    168  
    169      
  • collective.imstransport/trunk/collective/imstransport/utilities/bb/imsbbreader.py

    r177 r325  
    1 from zope.interface import implements 
    2 from collective.imstransport.utilities.interfaces import IIMSManifestReader, IIMSTransportUtility, IIMSObjectCreator 
    3 from collective.imstransport import IMSTransportMessageFactory as _ 
    4 from zipfile import ZipFile 
     1from collective.imstransport.utilities.imsinterchange import IMSReader 
     2from collective.imstransport.utilities.packagingio import ZipfileReader 
     3from collective.imstransport.utilities.bb.bbreader import BBReader 
    54from zope.component import getUtility 
    6 from Products.CMFCore.interfaces import ISiteRoot 
    7 from elementtree import ElementTree 
     5from collective.imstransport.utilities.interfaces import IIMSObjectCreator 
     6from xml.parsers.expat import ExpatError 
    87import re 
    9 from zope.component import getUtility 
    10 from collective.imstransport.utilities.packagingio import ZipfileReader 
    11 from collective.imstransport.utilities.imsinterchange import IMSInterchangeReader 
    128 
     9class IMSBBReader(IMSReader): 
     10    """ Create objects from IMS manifest. """ 
    1311 
    14 class IMSBBReader(IMSInterchangeReader): 
    15     """ Read an IMS CC package """ 
     12    def readPackage(self, file, context): 
     13        """ Read the manifest """ 
     14        source = ZipfileReader(file) 
     15        objDict = {} 
     16        if not source: 
     17             return False, 'Internal error. No source object specified' 
     18        bbreader = BBReader() 
     19        manifest = source.readManifest() 
     20        if not manifest: 
     21            raise ManifestError, 'Could not locate manifest file "imsmanifest.xml" in the zip archive.' 
     22        try: 
     23            doc = bbreader.parseManifest(manifest) 
     24        except ExpatError, e: 
     25            raise ManifestError, str(e) 
     26        tocpages = [] 
     27        orgs = bbreader.readOrganizations(doc) 
     28        resources = bbreader.readResources(doc) 
     29        for x in resources: 
     30            resid, restype, bbfile, bbtitle, bbase = bbreader.readResourceAttributes(x) 
     31            if restype == 'resource/x-bb-document': 
     32                metadata = {} 
     33                # read the data file 
     34                if bbfile: 
     35                    dataxml = source.readFile(bbfile) 
     36                    resnode = bbreader.parseDataFile(dataxml) 
     37                    metadata = bbreader.readMetadata(resnode) 
     38                files = bbreader.readFiles(x) 
     39                # If the resource is a file 
     40                if files: 
     41                    hash = resid 
     42                    objDict[hash] = metadata 
     43                    excludeFromNav = True 
     44                    file = '%s/%s' %(bbase, files[0]) 
     45                    type = self.determineType(objDict[hash], files[0]) 
     46                    id = self.createIdFromFile(files[0]) 
     47                    path = '%s' %bbase 
     48                    if orgs.has_key(resid): 
     49                        title = orgs[resid] 
     50                    else: 
     51                        title = id 
     52                    self.applyCoreMetadata(objDict, hash, id, path, excludeFromNav, type, title, file=file) 
     53                # If the resource is a document 
     54                else: 
     55                    hash = resid  
     56                    objDict[hash] = metadata 
     57                    excludeFromNav = True 
     58                    type = 'Document' 
     59                    id = resid 
     60                    path = '' 
     61                    if orgs.has_key(resid): 
     62                        title = orgs[resid] 
     63                    else: 
     64                        title = id 
     65                    self.applyCoreMetadata(objDict, hash, id, path, excludeFromNav, type, title) 
     66            # The resource is a table of contents page. 
     67            elif restype == 'course/x-bb-coursetoc': 
     68                hash = resid 
     69                objDict[hash] = {} 
     70                excludeFromNav = False 
     71                tocpages.append(resid) 
     72                type = 'Document' 
     73                path = '' 
     74                id = resid 
     75                if orgs.has_key(resid): 
     76                    title = re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', ' \\1', orgs[resid].split('.')[1]) 
     77                else: 
     78                    title = id 
     79                self.applyCoreMetadata(objDict, hash, id, path, excludeFromNav, type, title) 
    1680 
     81            # Build table of contents pages 
     82        for z in tocpages: 
     83            text = '<table>' 
     84            tocitems = bbreader.readTocItem(doc, z) 
     85            for titem in tocitems: 
     86                met = objDict[titem] 
     87                path = met['path'] 
     88                if path: 
     89                    linkpath = '%s/%s' %(path, objDict[titem]['id']) 
     90                else: 
     91                    linkpath = objDict[titem]['id'] 
     92                text += "<tr><td><a href='%s/view'>%s</a></td></tr>" %(linkpath, objDict[titem]['title']) 
     93            text += '</table>' 
     94            objDict[z]['text'] = text 
     95                 
     96        objcreator = getUtility(IIMSObjectCreator) 
     97        objcreator.createObjects(objDict, context, source) 
    1798 
    18     name = _(u'Blackboard') 
     99    def applyCoreMetadata(self, objDict, hash, id, path, excludeFromNav, type, title, file=None, text=None): 
     100        """ Helper function for applying metadata """ 
     101        objDict[hash]['id'] = id 
     102        objDict[hash]['path'] = path 
     103        objDict[hash]['excludeFromNav'] = excludeFromNav 
     104        if not (objDict[hash].has_key('type') and objDict[hash]['type']): 
     105            objDict[hash]['type'] = type 
     106        if not (objDict[hash].has_key('title') and objDict[hash]['title']): 
     107            objDict[hash]['title'] = title 
     108        if file: 
     109            objDict[hash]['file'] =  file 
     110        if text: 
     111            objDict[hash]['text'] = text 
    19112 
    20     BBNS = 'http://www.blackboard.com/content-packaging/' 
    21     XML = 'http://www.w3.org/XML/1998/namespace' 
    22  
    23     def getPackageName(self): 
    24         return self.name 
    25  
    26     def readPackage(self, context, input): 
    27         self.zf = ZipFile(input, 'r') 
    28         imsdoc = self.zf.read('imsmanifest.xml') 
    29          
    30         # Read in resources section 
    31         self.tree = ElementTree.XML(imsdoc) 
    32  
    33         # Enter resource data into a dictionary 
    34         resource_dictionary = self.readResources({}, self.tree) 
    35  
    36         self.zf.close() 
    37  
    38         objcreator = getUtility(IIMSObjectCreator) 
    39         objcreator.createObjects(resource_dictionary, context, ZipfileReader(input)) 
    40  
    41  
    42     def readResources(self, resDict, tree): 
    43  
    44         resourceNodes = tree.findall('resources/resource') 
    45          
    46         for resourceNode in resourceNodes: 
    47             self.readFiles(resDict, resourceNode) 
    48  
    49         return  resDict 
    50  
    51     def readFiles(self, resDict, resourceNode): 
    52  
    53         # Get information about the resource. 
    54         # 
    55         res_title = resourceNode.get('{%s}title' %(self.BBNS)) 
    56         # Determining which section it is in (e.g., Assignments, CourseInformation, ExternalLinks...) 
    57         res_title_parts = res_title.split(".") 
    58         if len(res_title_parts) == 4: 
    59             if res_title_parts[0] == 'COURSE_DEFAULT' and res_title_parts[2] == 'CONTENT_LINK' and res_title_parts[3] == 'label': 
    60                 # Converts camel case section title (e.g., CourseInformation) to its non-camelcase alternative and sets as title 
    61                 res_title = re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', ' \\1', res_title_parts[1]).strip(' ') 
    62         # Hash datfile information 
    63         datfile = resourceNode.get('{%s}file' %(self.BBNS)) 
    64         xmlbase = resourceNode.get('{%s}base' %(self.XML)) 
    65         # Hash the blackboard type information for the resource 
    66         bbtype = resourceNode.get('type') 
    67         # Hash the file information 
    68         fileNodes = resourceNode.findall('file') 
    69         id = resourceNode.get('identifier') 
    70          
    71      
    72         # Parse dat file 
    73         if datfile: 
    74             datText = self.readContentFile(datfile) 
    75         else: 
    76             datText = '' 
    77  
    78  
    79         # Show in navigation? 
    80         item_nodes_all = self.tree.findall("organizations//item") 
    81         targetItems = [node for node in item_nodes_all if node.get("identifierref") == id] 
    82         if targetItems: 
    83             excludeFromNav = False 
    84             # Are there any children? 
    85             child_nodes = targetItems[0].findall('item') 
    86         else: 
    87             excludeFromNav = True 
    88             child_nodes = [] 
    89  
    90  
    91         if len(fileNodes): 
    92             # Process resource with file nodes 
    93             files = [] 
    94             for fileNode in fileNodes: 
    95                 ## create the file object based on mimetype 
    96                 fileid = fileNode.get('href') 
    97                 files.append(('%s/%s/view' %(id, fileid),fileid)) 
    98                 type = self.determineFileMimetype(fileid) 
    99                 resDict[fileid] = {'file':fileid, 'path':id, 'title':fileid, 'id':fileid, 'type':type} 
    100             resText = self.TocPage('Table of Contents', files) 
    101         elif len(child_nodes): 
    102             # Create table of contents pages. 
    103             if not datText: 
    104                 items = [] 
    105                 for child_node in child_nodes: 
    106                     title_nodes = child_node.findall('title') 
    107                     if title_nodes: 
    108                         title = title_nodes[0].text 
    109                     else: 
    110                         title = '' 
    111                     items.append(('%s.html' %(child_node.get('identifierref')), title)) 
    112                 resText = self.TocPage('Table of Contents', items) 
    113             else: 
    114                 resText = datText 
    115         elif datText: 
    116             resText = datText 
    117         else: 
    118             resText = '' 
    119  
    120         resDict[id] = {'text':resText, 'path':'', 'title':res_title, 'id':'%s.html' %id, 'type':'Document'} 
    121  
    122  
    123     def TocPage(self, tabletitle, tocitems): 
    124          
    125         text = '' 
    126         text += '<table class="documentTable" style="width: 499px;" border="0" cellpadding="0" cellspacing="0">' 
    127         text += '<thead>' 
    128         text += '   <tr>' 
    129         text += '     <td>%s</td>' %tabletitle 
    130         text += '   </tr>' 
    131         text += '</thead>' 
    132         text += '<tbody>' 
    133         for tocitem in tocitems: 
    134             text += '     <tr tal:define="oddrow repeat/item/odd;" ' 
    135             text += '         tal:attributes="class oddrow)">' 
    136             text += '       <td ><a href="%s"' %tocitem[0]  
    137             text += '       >%s</a></td>' %tocitem[1] 
    138             text += '     </tr>' 
    139         text += '  </tbody>' 
    140         text += '</table>' 
    141  
    142         return text 
    143  
    144     def readContentFile(self, datfile): 
    145  
    146         # Read the proprietary blackboard.dat file 
    147         datdoc = self.zf.read(datfile)             
    148  
    149         # Parsing the dat file 
    150         contenttree = ElementTree.XML(datdoc) 
    151         text_nodes = contenttree.findall("BODY/TEXT") 
    152         resText = '' 
    153  
    154         # Get text information 
    155         if text_nodes: 
    156             resText = text_nodes[0].text 
    157  
    158         return resText 
    159  
    160      
  • collective.imstransport/trunk/collective/imstransport/utilities/configure.zcml

    r313 r325  
    2323     factory=".imscc.imsccwriter.IMSCCWriter" 
    2424     name="Common Cartridge" 
    25      />   
     25     />  
    2626 
    2727  <utility 
     
    3737     /> 
    3838 
    39   <!-- utility 
     39  <utility 
    4040     provides=".interfaces.IIMSManifestReader" 
    4141     factory=".bb.imsbbreader.IMSBBReader" 
    42      name="Blackboard" 
    43      / -- 
     42     name="Blackboard Content Package" 
     43     / 
    4444 
    4545  <utility 
     
    5151  <utility 
    5252     provides=".interfaces.IIMSManifestReader" 
     53     factory=".webct.imswebctreader.IMSWebCTReader" 
     54     name="WebCT Content Package" 
     55     />   
     56 
     57  <utility 
     58     provides=".interfaces.IIMSManifestReader" 
    5359     factory=".moodle.moodlebackupreader.MoodleBackupReader" 
    5460     name="Moodle Backup" 
    5561     /> 
    5662 
     63  <utility 
     64     provides=".interfaces.IIMSManifestReader" 
     65     factory=".webctvista.imswebctvistareader.IMSWebCTVistaReader" 
     66     name="WebCT Vista Package" 
     67     /> 
     68 
    5769</configure> 
  • collective.imstransport/trunk/collective/imstransport/utilities/imsinterchange.py

    r307 r325  
    1212    implements(IIMSManifestReader) 
    1313 
    14     def getPackageName(self): 
    15         """ Return the desciptive name of the package type. """ 
    16         return None 
    17  
    1814    def readPackage(self, file, context): 
    1915        """ Read IMS manifest. Override this to read specific package info. """ 
    2016 
    2117    # Helper functions for readPackage 
    22  
    2318    def createIdFromFile(self, file): 
    2419        """ Get Id from file path """ 
  • collective.imstransport/trunk/collective/imstransport/utilities/packagingio.py

    r160 r325  
    88        self.fullpath = package_path 
    99 
    10  
    11     def readManifest(self): 
     10    def readManifest(self, manifestfile='imsmanifest.xml'): 
    1211        """ Get the manifest file if it exists. """ 
    1312        manifest_path = os.path.join(self.fullpath, 'imsmanifest.xml') 
     
    3534        self.fullpath = '' 
    3635 
    37     def readManifest(self): 
     36    def readManifest(self, manifestfile='imsmanifest.xml'): 
    3837        """ Get the maifest file if it exists. """ 
    3938        for x in self.files.namelist(): 
    40             index = x.find('imsmanifest.xml') 
     39            index = x.find(manifestfile) 
    4140            if index != -1: 
    4241                self.fullpath = x[:index] 
  • collective.imstransport/trunk/collective/imstransport/utilities/webct/imswebctreader.py

    r177 r325  
    1 from zope.interface import implements 
     1from collective.imstransport.utilities.imsinterchange import IMSReader 
     2from collective.imstransport.utilities.packagingio import ZipfileReader 
     3from collective.imstransport.utilities.webct.webctreader import WebCTReader 
     4from zope.component import getUtility 
    25from collective.imstransport.utilities.interfaces import IIMSObjectCreator 
    3 from zope.component import getUtility 
    4 from collective.imstransport.utilities.imsinterchange import IMSInterchangeReader 
    5 from collective.imstransport.utilities.packagingio import ZipfileReader 
    6 from collective.imstransport.utilities.imscp.cpreader import CPReader 
    7 from collective.imstransport.utilities.imscp.cpresourcereader import CPResourceReader 
    8 import re 
    9 from collective.imstransport import IMSTransportMessageFactory as _ 
    106 
    11  
    12 LOM_namespace = 'http://ltsc.ieee.org/xsd/LOM' 
    13  
    14 class IMSCPReader(IMSInterchangeReader): 
     7class IMSWebCTReader(IMSReader): 
    158    """ Create objects from IMS manifest. """ 
    169 
    17     name = _(u'IMS Content Package') 
    18  
    19     XML = 'http://www.w3.org/XML/1998/namespace' 
    20     IMSCP = 'http://www.imsglobal.org/xsd/imscc/imscp_v1p1' 
    21     LOM = 'http://ltsc.ieee.org/xsd/LOM' 
    22  
    23     def readPackage(self, context, input): 
     10    def readPackage(self, file, context): 
    2411        """ Read the manifest """ 
    2512 
    26         source = ZipfileReader(input) 
     13        source = ZipfileReader(file) 
    2714        objDict = {} 
    28  
    2915        if not source: 
    3016             return False, 'Internal error. No source object specified' 
    31  
    32         cpreader = CPReader(context, source) 
    33               
     17        webctreader = WebCTReader() 
    3418        manifest = source.readManifest() 
    3519        if not manifest: 
    36             return False, \ 
    37                    'Manifest', \ 
    38                    'Could not locate manifest file "imsmanifest.xml" in the zip archive.' 
    39  
    40         cpreader.parseManifest(manifest) 
    41         orgdata = cpreader.readOrganizations() 
    42         resourceids = cpreader.readResources() 
    43  
    44         for resourceid in resourceids: 
    45             cpresourcereader = CPResourceReader(cpreader, resourceid, {})             
    46             resdata = self.parseResourceMetadata(cpresourcereader, resourceid, context) 
    47             reshref = cpresourcereader.getHref() 
    48             files = cpresourcereader.readFiles() 
    49  
    50             hashref = '' 
    51             if files: 
    52                 for file in files: 
    53                     hashref = '%s%s' %(resourceid, file) 
    54                     id = self.createIdFromFile(file) 
    55                     path = self.createPathFromFile(file) 
    56  
    57                     if reshref == file or len(files) == 1: 
    58                         hashref = resourceid 
    59                         objDict[hashref] = resdata 
    60                          
    61                         # Check if item is in organizations section 
    62                         if hashref in [org for org in orgdata if org]: 
    63                             objDict[hashref]['excludeFromNav'] = False 
    64  
    65                     self.parseFile(context, file, objDict, hashref, id, path) 
    66  
     20            raise ManifestError, 'Could not locate manifest file "imsmanifest.xml" in the zip archive.' 
     21        try: 
     22            doc = webctreader.parseManifest(manifest) 
     23        except ExpatError, e: 
     24            raise ManifestError, str(e) 
     25        manifests = webctreader.readManifests(doc) 
    6726         
     27        for manifest in manifests: 
     28            orgs =[] 
     29            manifestmetadata = webctreader.readMetadata(manifest) 
     30            if manifestmetadata.has_key('webcttype') and manifestmetadata['webcttype'] == 'Course': 
     31                objDict['package'] = manifestmetadata 
     32            else: 
     33                orgs = webctreader.readOrganizations(manifest) 
     34                resources = webctreader.readResources(manifest) 
     35                for x in resources: 
     36                    resid, restype, reshref = webctreader.readResourceAttributes(x) 
     37                    files = webctreader.readFiles(x) 
     38                    # If the type is a link 
     39                    if manifestmetadata.has_key('webcttype') and manifestmetadata['webcttype'] == 'URL': 
     40                        for y in files: 
     41                            hash = resid + y 
     42                            objDict[hash] = manifestmetadata 
     43                            id = self.createIdFromFile(y) 
     44                            objDict[hash]['id'] = id 
     45                            objDict[hash]['path'] = '' 
     46                            objDict[hash]['type'] = 'Link' 
     47                            objDict[hash]['remoteUrl'] = y 
     48                    elif restype == 'webcontent': 
     49                        if not files and reshref: 
     50                            files = [reshref,] 
     51                        for y in files: 
     52                            hash = resid + y 
     53                            objDict[hash] = {} 
     54                            # Can apply manifest metadata if single file and single resource 
     55                            if len(resources) == 1 and len(files) == 1: 
     56                                objDict[hash] = manifestmetadata 
     57                            if len(files) == 1: 
     58                                # If it is listed in the org section 
     59                                if orgs.has_key(resid): 
     60                                    objDict[hash]['excludeFromNav'] = False 
     61                                    # Use 'and' as opposed to 'or' to avoid KeyError 
     62                                    if not (objDict[hash].has_key('title') and objDict[hash]['title']): 
     63                                        objDict[hash]['title'] = orgs[resid] 
     64                                else: 
     65                                    objDict[hash]['excludeFromNav'] = True 
     66                                objDict[hash]['file'] = y 
     67                                objDict[hash]['type'] = self.determineType(objDict[hash], y) 
     68                            # If it is just a lowly file 
     69                            else: 
     70                                objDict[hash]['excludeFromNav'] = True 
     71                                objDict[hash]['file'] = y 
     72                                objDict[hash]['type'] = self.determineType(objDict[hash], y) 
     73                            # Add to all files 
     74                            id = self.createIdFromFile(y) 
     75                            objDict[hash]['id'] = id 
     76                            if not (objDict[hash].has_key('title') and objDict[hash]['title']): 
     77                                objDict[hash]['title'] = id 
     78                            objDict[hash]['path'] = self.createPathFromFile(y) 
    6879        objcreator = getUtility(IIMSObjectCreator) 
    6980        objcreator.createObjects(objDict, context, source) 
    70         return 
    71  
    72  
    73  
    74  
    75