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

Adding back old imports without transform

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • 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