def previews(self):
        '''
            Return preview on archive file / single file content as dictionary.
            In order to avoid browser and web server trashing by the students,
            there is a size limit for the single files shown.
        '''
        MAX_PREVIEW_SIZE = 1000000

        def sanitize(bytes):
            return bytes.decode('utf-8', 'ignore')

        def is_code(fname):
            code_endings = ['.c', '.cpp', 'Makefile',
                            '.java', '.py', '.rb', '.js']
            for ending in code_endings:
                if fname.endswith(ending):
                    return True
            return False

        result = []
        try:
            assert(self.attachment.path is not None)
        except ValueError:
            # The file behind the given path does not exist
            return None
        if zipfile.is_zipfile(self.attachment.path):
            zf = zipfile.ZipFile(self.attachment.path, 'r')
            for zipinfo in zf.infolist():
                if zipinfo.file_size < MAX_PREVIEW_SIZE:
                    try:
                        result.append({'name': zipinfo.filename, 'is_code': is_code(
                            zipinfo.filename), 'preview': sanitize(zf.read(zipinfo))})
                    except NotImplementedError as e:
                        result.append(
                                {'name': zipinfo.filename, 'is_code': False, 'preview': '(NotImplementedError: %s)' % (e)})

                    result.append({'name': zipinfo.filename, 'is_code': is_code(
                        zipinfo.filename), 'preview': sanitize(zf.read(zipinfo))})
                else:
                    result.append(
                        {'name': zipinfo.filename, 'is_code': False, 'preview': '(maximum size exceeded)'})
        elif tarfile.is_tarfile(self.attachment.path):
            tf = tarfile.open(self.attachment.path, 'r')
            for tarinfo in tf.getmembers():
                if tarinfo.isfile():
                    if tarinfo.size < MAX_PREVIEW_SIZE:
                        result.append({'name': tarinfo.name, 'is_code': is_code(
                            tarinfo.name), 'preview': sanitize(tf.extractfile(tarinfo).read())})
                    else:
                        result.append(
                            {'name': tarinfo.name, 'is_code': False, 'preview': '(maximum size exceeded)'})
        else:
            # single file
            f = open(self.attachment.path, 'rb')
            fname = f.name[f.name.rfind(os.sep) + 1:]
            result = [{'name': fname, 'is_code': is_code(
                fname), 'preview': sanitize(f.read())}, ]
        return result
def previews ( self ) : MAX_PREVIEW_SIZE = 1000000 def sanitize ( bytes ) : return bytes . decode ( 'utf-8' , 'ignore' ) def is_code ( fname ) : code_endings = [ '.c' , '.cpp' , 'Makefile' , '.java' , '.py' , '.rb' , '.js' ] for ending in code_endings : if fname . endswith ( ending ) : return True return False result = [ ] try : assert ( self . attachment . path is not None ) except ValueError : return None if zipfile . is_zipfile ( self . attachment . path ) : zf = zipfile . ZipFile ( self . attachment . path , 'r' ) for zipinfo in zf . infolist ( ) : if zipinfo . file_size < MAX_PREVIEW_SIZE : try : result . append ( { 'name' : zipinfo . filename , 'is_code' : is_code ( zipinfo . filename ) , 'preview' : sanitize ( zf . read ( zipinfo ) ) } ) except NotImplementedError as e : result . append ( { 'name' : zipinfo . filename , 'is_code' : False , 'preview' : '(NotImplementedError: %s)' % ( e ) } ) result . append ( { 'name' : zipinfo . filename , 'is_code' : is_code ( zipinfo . filename ) , 'preview' : sanitize ( zf . read ( zipinfo ) ) } ) else : result . append ( { 'name' : zipinfo . filename , 'is_code' : False , 'preview' : '(maximum size exceeded)' } ) elif tarfile . is_tarfile ( self . attachment . path ) : tf = tarfile . open ( self . attachment . path , 'r' ) for tarinfo in tf . getmembers ( ) : if tarinfo . isfile ( ) : if tarinfo . size < MAX_PREVIEW_SIZE : result . append ( { 'name' : tarinfo . name , 'is_code' : is_code ( tarinfo . name ) , 'preview' : sanitize ( tf . extractfile ( tarinfo ) . read ( ) ) } ) else : result . append ( { 'name' : tarinfo . name , 'is_code' : False , 'preview' : '(maximum size exceeded)' } ) else : f = open ( self . attachment . path , 'rb' ) fname = f . name [ f . name . rfind ( os . sep ) + 1 : ] result = [ { 'name' : fname , 'is_code' : is_code ( fname ) , 'preview' : sanitize ( f . read ( ) ) } , ] return result
Return preview on archive file / single file content as dictionary.
            In order to avoid browser and web server trashing by the students,
            there is a size limit for the single files shown.