def index(): files = [] for f in os.listdir(current_app.config['FILES_DIR']): # ignore dotfiles if f.startswith('.'): continue fp = open(os.path.join(current_app.config['FILES_DIR'], f)) # List of all modules for this file type handlers = mfr.detect(fp, many=True) renderer_handlers = [mfr.get_namespace(handler) for handler in handlers if handler.renderers] exporter_handlers = [handler for handler in handlers if handler.exporters] # Generate a list of modules and their file extensions export_options = [] for handler in exporter_handlers: available_file_extensions = mfr.get_file_exporters(handler) for export_file_type in available_file_extensions: export_options.append((export_file_type, mfr.get_namespace(handler))) files.append((f, renderer_handlers, export_options)) return render_template('main/index.html', files=files)
def test_render_return_type(self): for filename in os.listdir(self.FILES_DIR): fp = open(os.path.join(self.FILES_DIR, filename)) renderer = mfr.detect(fp, many=False) if renderer: # src = url_for('render.serve_file', filename=filename) result = mfr.render(fp) assert type(result) == mfr.RenderResult
def test_render_request(self): for filename in os.listdir(self.FILES_DIR): fp = open(os.path.join(self.FILES_DIR, filename)) status_code = self.app.get('/render/{filename}'.format(filename=filename)).status_code if mfr.detect(fp, many=False): assert status_code == 200 else: assert status_code == 400
def export(export_file_type, filename, exporter_name=None): """ Convert a file to another type and download that file. :param export_file_type: the type to export a file as :param filename: file to be exported :param exporter_name: optional name of the specific exporter module """ try: fp = open(os.path.join(current_app.config['FILES_DIR'], filename)) except IOError as err: flash(err, 'error') abort(404) # If handler name is not specified, choose the first that will work if exporter_name is None: exporter = mfr.detect(fp, many=False) exported_content = mfr.export(fp, exporter, exporter=export_file_type) else: handlers = get_registry() for handler in handlers: if mfr.get_namespace(handler) == exporter_name: exported_content = mfr.export(fp, handler=handler, exporter=export_file_type) if not exported_content: raise NameError("A matching exporter was not found") short_name, _ = os.path.splitext(filename) export_name = short_name + '.' + export_file_type return send_file( StringIO(exported_content), as_attachment=True, attachment_filename=export_name, )
def render(filename, renderer_name=None): """Make a file viewable in HTML :param filename: file to be rendered :param renderer_name: optional name of the specific renderer module :return: html representation of the file """ try: fp = open(os.path.join(current_app.config['FILES_DIR'], filename)) except IOError as err: flash(err, 'error') abort(404) renderer = None if renderer_name is None: renderer = mfr.detect(fp, many=False) # return the first valid filehandler else: handlers = get_registry() for available_handler in handlers: if mfr.get_namespace(available_handler) == renderer_name: renderer = available_handler() break if renderer is None: raise IOError('Could not load a matching renderer') src = url_for('render.serve_file', filename=filename) if renderer is None: return ("A matching renderer cannot be found", 400) return render_template( "main/view_file.html", render_result=mfr.render(fp, handler=renderer, src=src) )