Show
Ignore:
Timestamp:
07/13/09 14:26:21 (3 years ago)
Author:
jon
Message:

Changing blackboard import to accomodate tufts blackboard packages

Files:
1 modified

Legend:

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

    r335 r378  
    66from xml.parsers.expat import ExpatError 
    77from collective.imstransport.IMS_exceptions import ManifestError 
     8from BeautifulSoup import BeautifulSoup 
     9from Products.CMFCore.interfaces import ISiteRoot 
     10import htmlentitydefs 
     11from configbb import EMBEDDED_STRING 
    812import re 
    913 
     
    3034        for x in resources: 
    3135            resid, restype, bbfile, bbtitle, bbase = bbreader.readResourceAttributes(x) 
     36            doctext = filetoc = restoc = '' 
     37            metadata = {} 
    3238            if restype == 'resource/x-bb-document': 
    3339                metadata = {} 
     
    3743                    resnode = bbreader.parseDataFile(dataxml) 
    3844                    metadata = bbreader.readMetadata(resnode) 
    39                 files = bbreader.readFiles(x) 
    40                 # If the resource is a file 
    41                 if files: 
    42                     hash = resid 
    43                     objDict[hash] = metadata 
    44                     excludeFromNav = True 
    45                     file = '%s/%s' %(bbase, files[0]) 
    46                     type = self.determineType(objDict[hash], files[0]) 
    47                     id = self.createIdFromFile(files[0]) 
    48                     path = '%s' %bbase 
    49                     if orgs.has_key(resid): 
    50                         title = orgs[resid] 
     45                if metadata.has_key('text') and metadata['text']: 
     46                    mtext = metadata['text']                     
     47                    if type(mtext) == type(u''): 
     48                        mtext = mtext.encode('utf-8') 
     49                    ptext = unquotehtml(mtext) 
     50                    utils = getUtility(ISiteRoot).plone_utils 
     51                    soup = BeautifulSoup(ptext) 
     52                    doctext = bbreader.runDocumentFilters(utils, soup, [(EMBEDDED_STRING, 'embedded'),], bbase) 
     53                # Handle Files 
     54                files = bbreader.readFiles(x, bbase) 
     55                entries = [] 
     56                for y in files: 
     57                    dhash = resid + y 
     58                    objDict[dhash] = {} 
     59                    dexcludeFromNav = True 
     60                    dfile = y 
     61                    dfileparts = y.split('/') 
     62                    # File is embedded 
     63                    if 'embedded' in y: 
     64                        # Link is encrypted 
     65                        if len(dfileparts) > 0 and dfileparts[-1][0] == '!' and doctext: 
     66                            soup = BeautifulSoup(doctext) 
     67                            embeddedpath = bbreader.readEmbeddedTags(soup) 
     68                            if embeddedpath: 
     69                                dfile = embeddedpath 
     70                    dfilepath = self.createPathFromFile(dfile) 
     71                    did = self.createIdFromFile(dfile) 
     72                    dtype = self.determineType(objDict[dhash], dfile) 
     73                    portal = getUtility(ISiteRoot) 
     74                    utils = portal.plone_utils 
     75                    did = utils.normalizeString(did) 
     76                    dtitle = did 
     77                    if dfilepath: 
     78                        linkpath = '%s/%s' %(dfilepath, did) 
    5179                    else: 
    52                         title = id 
    53                     self.applyCoreMetadata(objDict, hash, id, path, excludeFromNav, type, title, file=file) 
    54                 # If the resource is a document 
    55                 else: 
    56                     hash = resid  
    57                     objDict[hash] = metadata 
    58                     excludeFromNav = True 
    59                     type = 'Document' 
    60                     id = resid 
    61                     path = '' 
    62                     if orgs.has_key(resid): 
    63                         title = orgs[resid] 
    64                     else: 
    65                         title = id 
    66                     self.applyCoreMetadata(objDict, hash, id, path, excludeFromNav, type, title) 
    67             # The resource is a table of contents page. 
    68             elif restype == 'course/x-bb-coursetoc': 
     80                        linkpath = did 
     81                    binfile = y 
     82                    folder_files = source.getFolderFiles(dfilepath) 
     83                    if len(folder_files) == 1: 
     84                        binfile = folder_files[0] 
     85                    entries.append((linkpath, dtitle)) 
     86                    self.applyCoreMetadata(objDict[dhash], did, dfilepath, dexcludeFromNav, dtype, dtitle, file=binfile) 
     87                if entries: 
     88                    filetoc = bbreader.createTocPage(entries) 
     89            # Handle links 
     90            if metadata.has_key('bbtype') and metadata['bbtype'] == 'Link': 
    6991                hash = resid 
    70                 objDict[hash] = {} 
    71                 excludeFromNav = False 
    72                 tocpages.append(resid) 
    73                 type = 'Document' 
    74                 path = '' 
    75                 id = resid 
     92                objDict[hash] = metadata 
     93                filepath = '' 
     94                id = resid + '.link' 
     95                excludeFromNav = True 
     96                ptype = metadata['bbtype'] 
    7697                if orgs.has_key(resid): 
    77                     title = re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', ' \\1', orgs[resid].split('.')[1]) 
     98                    title = orgs[resid] 
    7899                else: 
    79100                    title = id 
    80                 self.applyCoreMetadata(objDict, hash, id, path, excludeFromNav, type, title) 
    81  
    82             # Build table of contents pages 
     101                self.applyCoreMetadata(objDict[hash], id, filepath, excludeFromNav, ptype, title) 
     102            elif restype in ['resource/x-bb-document', 'course/x-bb-coursetoc']: 
     103                # Handle normal bb-documents 
     104                hash = resid 
     105                objDict[hash] = metadata 
     106                filepath = '' 
     107                excludeFromNav = True 
     108                ptype = 'Document' 
     109                id = resid + '.html' 
     110                if orgs.has_key(resid): 
     111                    title = orgs[resid] 
     112                else: 
     113                    title = id 
     114                # It's a folder object: 
     115                isFolder = metadata.has_key('bbtype') and metadata['bbtype'] == 'Folder' 
     116                if isFolder or restype == 'course/x-bb-coursetoc': 
     117                    tocpages.append(resid) 
     118                # It's a table of contents object 
     119                if restype == 'course/x-bb-coursetoc': 
     120                    if orgs.has_key(resid): 
     121                        excludeFromNav = False 
     122                        orgstitle = orgs[resid].split('.') 
     123                        # Rewrite label tag 
     124                        if len(orgstitle) > 1 and orgstitle[-1] == 'label': 
     125                            title = re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', ' \\1', orgs[resid].split('.')[1]) 
     126                text = doctext 
     127                if type(filetoc) == type(u''): 
     128                    text += filetoc.encode('utf-8') 
     129                self.applyCoreMetadata(objDict[hash], id, filepath, excludeFromNav, ptype, title, text=text) 
     130        # Build table of contents pages 
    83131        for z in tocpages: 
    84             text = '<table>' 
    85132            tocitems = bbreader.readTocItem(doc, z) 
     133            entries = [] 
    86134            for titem in tocitems: 
    87                 met = objDict[titem] 
    88                 path = met['path'] 
    89                 if path: 
    90                     linkpath = '%s/%s' %(path, objDict[titem]['id']) 
     135                if objDict.has_key(titem): 
     136                    met = objDict[titem] 
     137                    path = met['path'] 
     138                    if path: 
     139                        linkpath = '%s/%s' %(path, objDict[titem]['id']) 
     140                    else: 
     141                        linkpath = objDict[titem]['id'] 
     142                    entries.append((linkpath, met['title'])) 
     143            if entries: 
     144                if objDict[z].has_key('text') and objDict[z]['text']: 
     145                    gtext = objDict[z]['text'] 
     146                    if type(gtext) == type(u''): 
     147                        gtext = gtext.encode('utf-8') 
     148                    objDict[z]['text'] = bbreader.createTocPage(entries).encode('utf-8') + gtext 
    91149                else: 
    92                     linkpath = objDict[titem]['id'] 
    93                 text += "<tr><td><a href='%s/view'>%s</a></td></tr>" %(linkpath, objDict[titem]['title']) 
    94             text += '</table>' 
    95             objDict[z]['text'] = text 
    96                  
     150                    objDict[z]['text'] = bbreader.createTocPage(entries).encode('utf-8') 
     151            else: 
     152                objDict[z]['excludeFromNav'] = True 
    97153        objcreator = getUtility(IIMSObjectCreator) 
    98154        objcreator.createObjects(objDict, context, source) 
    99155 
    100     def applyCoreMetadata(self, objDict, hash, id, path, excludeFromNav, type, title, file=None, text=None): 
     156    def applyCoreMetadata(self, metadata, id, path, excludeFromNav, type, title, file=None, text=None): 
    101157        """ Helper function for applying metadata """ 
    102         objDict[hash]['id'] = id 
    103         objDict[hash]['path'] = path 
    104         objDict[hash]['excludeFromNav'] = excludeFromNav 
    105         if not (objDict[hash].has_key('type') and objDict[hash]['type']): 
    106             objDict[hash]['type'] = type 
    107         if not (objDict[hash].has_key('title') and objDict[hash]['title']): 
    108             objDict[hash]['title'] = title 
     158        portal = getUtility(ISiteRoot) 
     159        utils = portal.plone_utils 
     160        metadata['id'] = utils.normalizeString(id) 
     161        metadata['path'] = path 
     162        metadata['excludeFromNav'] = excludeFromNav 
     163        metadata['type'] = type 
     164        if not (metadata.has_key('title') and metadata['title']): 
     165            metadata['title'] = title 
    109166        if file: 
    110             objDict[hash]['file'] =  file 
     167            metadata['file'] =  file 
    111168        if text: 
    112             objDict[hash]['text'] = text 
     169            metadata['text'] = text 
    113170 
     171def convertentity(m): 
     172    """Convert a HTML entity into normal string (ISO-8859-1)""" 
     173    if m.group(1)=='#': 
     174        try: 
     175            return chr(int(m.group(2))) 
     176        except ValueError: 
     177            return '&#%s;' % m.group(2) 
     178    try: 
     179        return htmlentitydefs.entitydefs[m.group(2)] 
     180    except KeyError: 
     181        return '&%s;' % m.group(2) 
     182 
     183def unquotehtml(s): 
     184    """Convert a HTML quoted string into normal string (ISO-8859-1). 
     185 
     186    Works with &#XX; and with &nbsp; &gt; etc.""" 
     187    return re.sub(r'&(#?)(.+?);',convertentity,s)