Esempio n. 1
0
    def process(self, zip_filename):
        try:
            self.zip_archive = zipfile.ZipFile(zip_filename, 'r')

            # Unzip into transform directory
            self.zip_archive.extractall(path=self.save_dir)

            # Rename ZIP file so that the user can download it again
            os.rename(zip_filename,
                      os.path.join(self.save_dir, 'upload.zip'))

            # with open(os.path.join(self.save_dir, 'index.html'), 'r') as index:
            #     html = index.read()

        except ConversionError as e:
            return render_conversionerror(self.request, e.msg)

        except Exception:
            tb = traceback.format_exc()
            # self.write_traceback_to_zipfile(tb, zip_filename) # TODO: This fails, unicode error. Also untested in other processors like e.g. ZipFileProcessor
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath, response, request=self.request)

        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url(self.nextStep()))
Esempio n. 2
0
    def process(self, form):
        try:
            processor = None
            packaging = self.get_type()
            if packaging == ZIP_PACKAGING:
                processor = ZipFileProcessor(self.request, form)
                return processor.process(self.original_filename)
            elif packaging == HTML_TEST_PACKAGING:
                processor = HtmlTestFileProcessor(self.request, form)
                return processor.process(self.original_filename)
            elif packaging == LATEX_PACKAGING:
                processor = LatexProcessor(self.request)
                return processor.process(form)

        except UnknownPackagingError as e:
            return render_conversionerror(self.request, e.msg)

        except Exception:
            tb = traceback.format_exc()
            self.write_traceback_to_zipfile(tb, form)
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if ('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath,
                                      response,
                                      request=self.request)

        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url(self.nextStep()))
    def process(self):
        try:
            f = open(self.original_filename)
            latex_archive = f.read()

            # LaTeX 2 CNXML transformation
            cnxml, objects = latex_to_cnxml(latex_archive, self.original_filename)

            cnxml = clean_cnxml(cnxml)
            save_cnxml(self.save_dir, cnxml, objects.items())
            validate_cnxml(cnxml)

        except ConversionError as e:
            return render_conversionerror(self.request, e.msg)

        except Exception:
            tb = traceback.format_exc()
            self.write_traceback_to_zipfile(tb)
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath, response, request=self.request)

        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url(self.nextStep()))
    def process(self):
        try:
            processor = None
            packaging = self.get_type()
            if packaging == ZIP_PACKAGING:
                processor = ZipFileProcessor(self.request, self.form)
                return processor.process(self.original_filename)
            elif packaging == LATEX_PACKAGING:
                processor = LatexProcessor(self.request, self.form)
                return processor.process()

        except UnknownPackagingError as e:
            return render_conversionerror(self.request, e.msg)

        except Exception:
            tb = traceback.format_exc()
            self.write_traceback_to_zipfile(tb)
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath, response, request=self.request)

        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url(self.nextStep()))
Esempio n. 5
0
    def process(self, form):
        try:
            # Convert from other office format to odt if needed
            filename, extension = os.path.splitext(self.original_filename)
            odt_filename = str(filename) + '.odt'

            if (extension != '.odt'):
                self._convert_to_odt(filename)
            # Convert and save all the resulting files.

            tree, files, errors = transform(odt_filename)
            cnxml = clean_cnxml(etree.tostring(tree))

            save_cnxml(self.save_dir, cnxml, files.items())

            # now validate with jing
            validate_cnxml(cnxml)

        except ConversionError as e:
            return render_conversionerror(self.request, e.msg)

        except Exception:
            tb = traceback.format_exc()
            self.write_traceback_to_zipfile(tb, form)
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if ('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath,
                                      response,
                                      request=self.request)

        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url(self.nextStep()))
    def process(self):
        try:
            # Convert from other office format to odt if needed
            filename, extension = os.path.splitext(self.original_filename)
            odt_filename = str(filename) + '.odt'

            if(extension != '.odt'):
                self._convert_to_odt(filename)        
            # Convert and save all the resulting files.

            tree, files, errors = transform(odt_filename)
            cnxml = clean_cnxml(etree.tostring(tree))

            save_cnxml(self.save_dir, cnxml, files.items())

            # now validate with jing
            validate_cnxml(cnxml)

        except ConversionError as e:
            return render_conversionerror(self.request, e.msg)

        except Exception:
            tb = traceback.format_exc()
            self.write_traceback_to_zipfile(tb)
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath, response, request=self.request)

        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url(self.nextStep()))
Esempio n. 7
0
    def process(self, form):
        try:
            f = open(self.original_filename)
            latex_archive = f.read()

            # LaTeX 2 CNXML transformation
            cnxml, objects = latex_to_cnxml(latex_archive,
                                            self.original_filename)

            cnxml = clean_cnxml(cnxml)
            save_cnxml(self.save_dir, cnxml, objects.items())
            validate_cnxml(cnxml)

        except ConversionError as e:
            return render_conversionerror(self.request, e.msg)

        except Exception:
            tb = traceback.format_exc()
            self.write_traceback_to_zipfile(tb, form)
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if ('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath,
                                      response,
                                      request=self.request)

        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url(self.nextStep()))
Esempio n. 8
0
    def process(self, zip_filename):
        try:
            self.zip_archive = zipfile.ZipFile(zip_filename, 'r')

            # Unzip into transform directory
            self.zip_archive.extractall(path=self.save_dir)

            # Rename ZIP file so that the user can download it again
            os.rename(zip_filename, os.path.join(self.save_dir, 'upload.zip'))

            # with open(os.path.join(self.save_dir, 'index.html'), 'r') as index:
            #     html = index.read()

        except ConversionError as e:
            return render_conversionerror(self.request, e.msg)

        except Exception:
            tb = traceback.format_exc()
            # self.write_traceback_to_zipfile(tb, zip_filename) # TODO: This fails, unicode error. Also untested in other processors like e.g. ZipFileProcessor
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if ('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath,
                                      response,
                                      request=self.request)

        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url(self.nextStep()))
    def process(self):
        try:
            gdocs_resource_id = self.form.data['gdocs_resource_id']
            gdocs_access_token = self.form.data['gdocs_access_token']

            self.form.data['gdocs_resource_id'] = None
            self.form.data['gdocs_access_token'] = None

            title, filename = self.process_gdocs_resource(
                self.save_dir, gdocs_resource_id)

            self.request.session['title'] = title
            self.request.session['filename'] = filename
        except ConversionError as e:
            return render_conversionerror(self.request, e.msg)

        except Exception:
            tb = traceback.format_exc()
            self.write_traceback_to_zipfile(tb)
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath, response, request=self.request)

        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url(self.nextStep()))
    def process(self):
        try:
            if self.form.data.get('newmodule'):
                self.set_source('new')
                self.set_target('new')
                # save empty cnxml and html files
                cnxml = self.empty_cnxml()
                files = []
                save_cnxml(self.save_dir, cnxml, files)
            
            elif self.form.data.get('existingmodule'):
                self.set_source('existingmodule')
                self.set_target('existingmodule')
                return HTTPFound(
                    location=self.request.route_url('choose-module'))

        except ConversionError as e:
            return render_conversionerror(self.request, e.msg)
        
        # TODO: add a process decorator that has this bit of error handling
        except Exception:
            tb = traceback.format_exc()
            self.write_traceback_to_zipfile(tb)
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath, response, request=self.request)

        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url(self.nextStep()))
Esempio n. 11
0
    def process(self, form):
        try:
            if form.data.get('newmodule'):
                self.set_source('new')
                self.set_target('new')
                # save empty cnxml and html files
                cnxml = self.empty_cnxml()
                files = []
                save_cnxml(self.save_dir, cnxml, files)

            elif form.data.get('existingmodule'):
                self.set_source('existingmodule')
                self.set_target('existingmodule')
                return HTTPFound(
                    location=self.request.route_url('choose-module'))

        except ConversionError as e:
            return render_conversionerror(self.request, e.msg)

        # TODO: add a process decorator that has this bit of error handling
        except Exception:
            tb = traceback.format_exc()
            self.write_traceback_to_zipfile(tb, form)
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if ('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath,
                                      response,
                                      request=self.request)

        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url(self.nextStep()))
    def process(self):
        try:
            url = self.form.data['url_text']

            # Build a regex for Google Docs URLs
            regex = re.compile(
                "^https:\/\/docs\.google\.com\/.*document\/[^\/]\/([^\/]+)\/")
            r = regex.search(url)

            # Take special action for Google Docs URLs
            if r:
                gdocs_resource_id = r.groups()[0]
                doc_id = "document:" + gdocs_resource_id
                title, filename = self.process_gdocs_resource(self.save_dir,
                                                              doc_id)

                self.request.session['title'] = title
                self.request.session['filename'] = filename
            else:
                # download html:
                # Simple urlopen() will fail on mediawiki websites eg. Wikipedia!
                import_opener = urllib2.build_opener()
                import_opener.addheaders = [('User-agent', 'Mozilla/5.0')]
                import_request = import_opener.open(url)
                html = import_request.read()

                # transformation
                cnxml, objects, html_title = htmlsoup_to_cnxml(
                        html, bDownloadImages=True, base_or_source_url=url)
                self.request.session['title'] = html_title

                cnxml = clean_cnxml(cnxml)
                save_cnxml(self.save_dir, cnxml, objects.items())

                # Keep the info we need for next uploads.  Note that
                # this might kill the ability to do multiple tabs in
                # parallel, unless it gets offloaded onto the form
                # again.
                self.request.session['filename'] = "HTML Document"

                validate_cnxml(cnxml)

        except ConversionError as e:
            return render_conversionerror(self.request, e.msg)

        except Exception:
            tb = traceback.format_exc()
            self.write_traceback_to_zipfile(tb)
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath, response, request=self.request)

        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url(self.nextStep()))
Esempio n. 13
0
    def process(self):
        try:
            LOG.info("Inside presentation form")
            zipped_filepath = os.path.join(self.save_dir, "cnxupload.zip")
            self.request.session['userfilepath'] = zipped_filepath
            zip_archive = zipfile.ZipFile(zipped_filepath, 'w')
            zip_archive.write(self.original_filename, self.uploaded_filename)
            zip_archive.close()
            self.request.session['uploaded_filename'] = self.uploaded_filename
            self.request.session['original_filename'] = self.original_filename
            LOG.info("Original filename ", self.original_filename)
            self.request.session['title'] = self.uploaded_filename.split(
                ".")[0]
            metadata = {}
            metadata['dcterms:title'] = self.uploaded_filename.split(".")[0]
            cnxml_now_string = datetime.datetime.now().strftime(
                '%Y/%m/%d %H:%M:%S')
            cnxml = self.slide_importer_cnxml(cnxml_now_string, self.username)
            self.request.session['cnxml'] = cnxml

            # do a little metadata cleanup before we go to the metadata view
            for key in metadata.keys():
                if metadata[key] == '':
                    del metadata[key]
            self.request.session['metadata'] = metadata

        except ConversionError as e:
            return render_conversionerror(self.request, e.msg)

        except Exception:
            tb = traceback.format_exc()
            self.write_traceback_to_zipfile(tb, form)
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if ('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath,
                                      response,
                                      request=self.request)

        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url('importer'))
Esempio n. 14
0
    def process_gdocs_resource(self, html, title, form=None, kix=None):
        try:
            filename = self._process_gdocs_resource(
                self.save_dir, html, kix)
        except ConversionError as e:
            return render_conversionerror(self.request, e.msg)

        except Exception:
            tb = traceback.format_exc()
            self.write_traceback_to_zipfile(tb, form)
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath, response, request=self.request)

        self.request.session['title'] = title
        self.request.session['filename'] = filename
        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url(self.nextStep()))
Esempio n. 15
0
    def process_gdocs_resource(self, html, title, form=None, kix=None):
        try:
            filename = self._process_gdocs_resource(self.save_dir, html, kix)
        except ConversionError as e:
            return render_conversionerror(self.request, e.msg)

        except Exception:
            tb = traceback.format_exc()
            self.write_traceback_to_zipfile(tb, form)
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if ('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath,
                                      response,
                                      request=self.request)

        self.request.session['title'] = title
        self.request.session['filename'] = filename
        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url(self.nextStep()))
    def process(self):
        try:
            LOG.info("Inside presentation form")
            zipped_filepath = os.path.join(self.save_dir, "cnxupload.zip")
            LOG.info("Zipped filepath", zipped_filepath)
            self.request.session['userfilepath'] = zipped_filepath
            zip_archive = zipfile.ZipFile(zipped_filepath, 'w')
            zip_archive.write(self.original_filename, self.uploaded_filename)
            zip_archive.close()
            self.request.session['uploaded_filename'] = self.uploaded_filename
            self.request.session['original_filename'] = self.original_filename
            LOG.info("Original filename ", self.original_filename)
            self.request.session['title'] = self.uploaded_filename.split(".")[0]
            metadata = {}
            metadata['dcterms:title'] = self.uploaded_filename.split(".")[0]
            cnxml_now_string = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
            cnxml = self.slide_importer_cnxml(cnxml_now_string, self.username)
            self.request.session['cnxml'] = cnxml

            # do a little metadata cleanup before we go to the metadata view
            for key in metadata.keys():
                if metadata[key] == '':
                    del metadata[key]
            self.request.session['metadata'] = metadata

        except ConversionError as e:
            return render_conversionerror(self.request, e.msg)

        except Exception:
            tb = traceback.format_exc()
            self.write_traceback_to_zipfile(tb)
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath, response, request=self.request)

        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url(self.nextStep()))
Esempio n. 17
0
    def process(self, zip_filename):
        try:
            self.zip_archive = zipfile.ZipFile(zip_filename, 'r')

            # Unzip into transform directory
            self.zip_archive.extractall(path=self.save_dir)

            # Rename ZIP file so that the user can download it again
            os.rename(zip_filename, os.path.join(self.save_dir, 'upload.zip'))

            # Read CNXML
            with open(os.path.join(self.save_dir, 'index.cnxml'), 'rt') as fp:
                cnxml = fp.read()

            # Convert the CNXML to XHTML for preview
            html = cnxml_to_htmlpreview(cnxml)
            with open(os.path.join(self.save_dir, 'index.xhtml'),
                      'w') as index:
                index.write(html)

            cnxml = clean_cnxml(cnxml)
            validate_cnxml(cnxml)

        except ConversionError as e:
            return render_conversionerror(self.request, e.msg)

        except Exception:
            tb = traceback.format_exc()
            self.write_traceback_to_zipfile(tb, form)
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if ('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath,
                                      response,
                                      request=self.request)

        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url(self.nextStep()))
    def process(self, zip_filename):
        try:
            self.zip_archive = zipfile.ZipFile(zip_filename, 'r')

            # Unzip into transform directory
            self.zip_archive.extractall(path=self.save_dir)

            # Rename ZIP file so that the user can download it again
            os.rename(zip_filename,
                      os.path.join(self.save_dir, 'upload.zip'))

            # Read CNXML
            with open(os.path.join(self.save_dir, 'index.cnxml'), 'rt') as fp:
                cnxml = fp.read()

            # Convert the CNXML to XHTML for preview
            html = cnxml_to_htmlpreview(cnxml)
            with open(os.path.join(self.save_dir, 'index.xhtml'), 'w') as index:
                index.write(html)

            cnxml = clean_cnxml(cnxml)
            validate_cnxml(cnxml)

        except ConversionError as e:
            return render_conversionerror(self.request, e.msg)

        except Exception:
            tb = traceback.format_exc()
            self.write_traceback_to_zipfile(tb)
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath, response, request=self.request)

        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url(self.nextStep()))
Esempio n. 19
0
    def process(self, form):
        try:
            url = form.data['url_text']

            # Build a regex for Google Docs URLs
            regex = re.compile(
                "^https:\/\/docs\.google\.com\/.*document\/[^\/]\/([^\/]+)\/")
            r = regex.search(url)

            # Take special action for Google Docs URLs
            if r is not None:
                gdocs_resource_id = r.groups()[0]
                http = httplib2.Http()
                http.follow_redirects = False
                try:
                    resp, html = http.request(
                        'https://docs.google.com/document/d/%s/export?format=html&confirm=no_antivirus'
                        % gdocs_resource_id)
                    resp2, kix = http.request(
                        'https://docs.google.com/feeds/download/documents/export/Export?id=%s&exportFormat=kix'
                        % gdocs_resource_id)
                except HttpError:
                    pass
                else:
                    # Check that status was OK, google docs sends a redirect to a login
                    # page if not.
                    if resp.status / 100 == 2:
                        # Get the title
                        title = 'Untitled Google Document'

                        # Process it
                        P = GoogleDocProcessor(self.request)
                        return P.process_gdocs_resource(html, title, form, kix)
                self.request.session.flash('Failed to convert google document')
                return HTTPFound(location=self.request.route_url('choose'))
            else:
                # download html:
                # Simple urlopen() will fail on mediawiki websites eg. Wikipedia!
                import_opener = urllib2.build_opener()
                import_opener.addheaders = [('User-agent', 'Mozilla/5.0')]
                import_request = import_opener.open(url)
                html = import_request.read()

                # transformation
                cnxml, objects, html_title = htmlsoup_to_cnxml(
                    html, bDownloadImages=True, base_or_source_url=url)
                self.request.session['title'] = html_title

                cnxml = clean_cnxml(cnxml)
                save_cnxml(self.save_dir, cnxml, objects.items())

                # Keep the info we need for next uploads.  Note that
                # this might kill the ability to do multiple tabs in
                # parallel, unless it gets offloaded onto the form
                # again.
                self.request.session['filename'] = "HTML Document"

                validate_cnxml(cnxml)

        except ConversionError as e:
            return render_conversionerror(self.request, e.msg)

        except Exception:
            tb = traceback.format_exc()
            self.write_traceback_to_zipfile(tb, form)
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if ('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath,
                                      response,
                                      request=self.request)

        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url(self.nextStep()))
    def process(self, form):
        try:
            url = form.data['url_text']

            # Build a regex for Google Docs URLs
            regex = re.compile(
                "^https:\/\/docs\.google\.com\/.*document\/[^\/]\/([^\/]+)\/")
            r = regex.search(url)

            # Take special action for Google Docs URLs
            if r is not None:
                gdocs_resource_id = r.groups()[0]
                http = httplib2.Http()
                http.follow_redirects = False
                try:
                    resp, html = http.request(
                        'https://docs.google.com/document/d/%s/export?format=html&confirm=no_antivirus' % gdocs_resource_id)
                except HttpError:
                    pass
                else:
                    # Check that status was OK, google docs sends a redirect to a login
                    # page if not.
                    if resp.status / 100 == 2:
                        # Get the title (does not work anymore)
                        title = 'Untitled Google Document'

                        # Process it
                        P = GoogleDocProcessor(self.request)
                        return P.process_gdocs_resource(html, title, form)
                self.request.session.flash('Failed to convert google document')
                return HTTPFound(location=self.request.route_url('choose'))
            else:
                # download html:
                # Simple urlopen() will fail on mediawiki websites eg. Wikipedia!
                import_opener = urllib2.build_opener()
                import_opener.addheaders = [('User-agent', 'Mozilla/5.0')]
                import_request = import_opener.open(url)
                html = import_request.read()

                # transformation
                cnxml, objects, html_title = htmlsoup_to_cnxml(
                        html, bDownloadImages=True, base_or_source_url=url)
                self.request.session['title'] = html_title

                cnxml = clean_cnxml(cnxml)
                save_cnxml(self.save_dir, cnxml, objects.items())

                # Keep the info we need for next uploads.  Note that
                # this might kill the ability to do multiple tabs in
                # parallel, unless it gets offloaded onto the form
                # again.
                self.request.session['filename'] = "HTML Document"

                validate_cnxml(cnxml)

        except ConversionError as e:
            return render_conversionerror(self.request, e.msg)

        except Exception:
            tb = traceback.format_exc()
            self.write_traceback_to_zipfile(tb, form)
            templatePath = 'templates/error.pt'
            response = {'traceback': tb}
            if('title' in self.request.session):
                del self.request.session['title']
            return render_to_response(templatePath, response, request=self.request)

        self.request.session.flash(self.message)
        return HTTPFound(location=self.request.route_url(self.nextStep()))