def action_odf2odf(self, resource, context, form): odf_file_name, odf_file_mime_type, odf_file_data = form['tr_odf_file'] srx_file = form['tr_srx_file'] trash, input_file_mime_type, input_file_data = form['tr_input'] # Not a too big file if check_size(odf_file_data, context): return # Get the good "translate" odf_handler = get_handler_class_by_mimetype(odf_file_mime_type) try: translate = odf_handler(string=odf_file_data).translate except AttributeError: context.message = ERROR(u'malformed ODF file') return # a SRX file ? if srx_file is not None: srx_file_data = srx_file[2] try: srx_handler = SRXFile(string=srx_file_data) except XMLError: context.message = ERROR(u'unexpected error, please verify ' u'your input files') return else: srx_handler = None # Get the catalog and test it input_handler = get_handler_class_by_mimetype(input_file_mime_type) catalog = input_handler(string=input_file_data) try: catalog.gettext except AttributeError: context.message = ERROR(u'unexpected error, please verify ' u'your input files') return # Make the translation! data = translate(catalog, srx_handler=srx_handler) # Return the result context.set_content_type(odf_file_mime_type) context.set_content_disposition('inline', odf_file_name) return data
def _get_handler_from_value(self, value): if type(value) is tuple: filename, mimetype, value = value if type(value) is str: cls = self.class_handler if cls is None: mimetype = magic_from_buffer(value) cls = get_handler_class_by_mimetype(mimetype) return cls(string=value) return value
def get_handler_class(self, key): mimetype = self.get_mimetype(key) try: return get_handler_class_by_mimetype(mimetype) except ValueError: log_warning('unknown handler class "{0}"'.format(mimetype)) if fs.is_file(key): from itools.handlers import File return File elif fs.is_folder(key): from itools.handlers import Folder return Folder raise ValueError
def action_odf2tr(self, resource, context, form): odf_file_name, odf_file_mime_type, odf_file_data = form['odf_file'] srx_file = form['srx_file'] output_type = form['output_type'] # Not a too big file if check_size(odf_file_data, context): return # Get the good "get_units" odf_handler = get_handler_class_by_mimetype(odf_file_mime_type) try: get_units = odf_handler(string=odf_file_data).get_units except AttributeError: context.message = ERROR(u'malformed ODF file') return # a SRX file ? if srx_file is not None: srx_file_data = srx_file[2] try: srx_handler = SRXFile(string=srx_file_data) except XMLError: context.message = ERROR(u'unexpected error, please verify ' u'your input files') return else: srx_handler = None # The good handler for the output if output_type == 'PO': extension = 'po' out_handler = POFile() else: extension = 'xlf' out_handler = XLFFile() name = FileName.decode(odf_file_name)[0] out_filename = FileName.encode((name, extension, None)) # Make the output for source, source_context, line in get_units(srx_handler=srx_handler): out_handler.add_unit(odf_file_name, source, source_context, line) # Return the result context.set_content_type(out_handler.class_mimetypes[0]) context.set_content_disposition('inline', out_filename) return out_handler.to_str()
def action(self, resource, context, form): odf_file_name, odf_file_mimetype, odf_file_data = form['odf_file'] # Not a too big file if check_size(odf_file_data, context): return # Load the handler odf_handler = get_handler_class_by_mimetype(odf_file_mimetype) odf_handler = odf_handler(string=odf_file_data) # Greek output = odf_handler.greek() # Add '-greek' to the filename name, extension, language = FileName.decode(odf_file_name) out_filename = FileName.encode((name + '-greek', extension, language)) # Return the result context.set_content_type(odf_file_mimetype) context.set_content_disposition('inline', out_filename) return output
def action(self, resource, context, form): skip = set([ 'application/javascript', 'application/octet-stream', 'text/css', 'text/plain' ]) keep = set(['application/pdf', 'image/png']) language = form['language'] def rewrite(value): if value[0] == '#': return value ref = get_reference(value) if ref.scheme: return value name = ref.path.get_name() name, extension, langage = FileName.decode(name) if extension in ('png', 'pdf'): name = '%s/;download' % name ref.path[-1] = name return '../%s' % ref def filter(path, mimetype, body): # HTML if mimetype == 'text/html': source = XHTMLFile(string=body) target = XHTMLFile() elem = get_element(source.events, 'div', **{'class': 'body'}) if not elem: print 'E', path return None elements = elem.get_content_elements() elements = rewrite_uris(elements, rewrite) elements = list(elements) target.set_body(elements) return target.to_str() # Skip elif mimetype in skip: return None # Keep elif mimetype in keep: return body # Unknown else: print 'X', path, mimetype return body def postproc(file): # Share file.set_value('share', ['everybody']) # Title if file.class_id != 'webpage': return handler = file.get_handler() events = handler.events elem = get_element(events, 'h1') if elem: title = [ unicode(x[1], 'utf8') for x in elem.get_content_elements() if x[0] == TEXT ] if title[-1] == u'¶': title.pop() title = u''.join(title) file.set_property('title', title, language) handler.events = events[:elem.start] + events[elem.end + 1:] # 1. Make the '/docs/' folder docs = resource.get_resource('docs', soft=True) if not docs: docs = resource.make_resource('docs', Folder) # 2. Extract filename, mimetype, body = form['file'] cls = get_handler_class_by_mimetype(mimetype) handler = cls(string=body) docs.extract_archive(handler, language, filter, postproc, True) # Ok message = MSG(u'Documentation updated.') return context.come_back(message, goto='./docs')
def get_handler_class(self, key): mimetype = self.get_mimetype(key) return get_handler_class_by_mimetype(mimetype)
def action(self, resource, context, form): skip = set(['application/javascript', 'application/octet-stream', 'text/css', 'text/plain']) keep = set(['application/pdf', 'image/png']) language = form['language'] def rewrite(value): if value[0] == '#': return value ref = get_reference(value) if ref.scheme: return value name = ref.path.get_name() name, extension, langage = FileName.decode(name) if extension in ('png', 'pdf'): name = '%s/;download' % name ref.path[-1] = name return '../%s' % ref def filter(path, mimetype, body): # HTML if mimetype == 'text/html': source = XHTMLFile(string=body) target = XHTMLFile() elem = get_element(source.events, 'div', **{'class': 'body'}) if not elem: print 'E', path return None elements = elem.get_content_elements() elements = rewrite_uris(elements, rewrite) elements = list(elements) target.set_body(elements) return target.to_str() # Skip elif mimetype in skip: return None # Keep elif mimetype in keep: return body # Unknown else: print 'X', path, mimetype return body def postproc(file): # Share file.set_value('share', ['everybody']) # Title if file.class_id != 'webpage': return handler = file.get_handler() events = handler.events elem = get_element(events, 'h1') if elem: title = [ unicode(x[1], 'utf8') for x in elem.get_content_elements() if x[0] == TEXT ] if title[-1] == u'¶': title.pop() title = u''.join(title) file.set_property('title', title, language) handler.events = events[:elem.start] + events[elem.end+1:] # 1. Make the '/docs/' folder docs = resource.get_resource('docs', soft=True) if not docs: docs = resource.make_resource('docs', Folder) # 2. Extract filename, mimetype, body = form['file'] cls = get_handler_class_by_mimetype(mimetype) handler = cls(string=body) docs.extract_archive(handler, language, filter, postproc, True) # Ok message = MSG(u'Documentation updated.') return context.come_back(message, goto='./docs')
def action(self, resource, context, form): skip = set(["application/javascript", "application/octet-stream", "text/css", "text/plain"]) keep = set(["application/pdf", "image/png"]) language = form["language"] def rewrite(value): if value[0] == "#": return value ref = get_reference(value) if ref.scheme: return value name = ref.path.get_name() name, extension, langage = FileName.decode(name) if extension in ("png", "pdf"): name = "%s/;download" % name ref.path[-1] = name return "../%s" % ref def filter(path, mimetype, body): # HTML if mimetype == "text/html": source = XHTMLFile(string=body) target = XHTMLFile() elem = get_element(source.events, "div", **{"class": "body"}) if not elem: print "E", path return None elements = elem.get_content_elements() elements = rewrite_uris(elements, rewrite) elements = list(elements) target.set_body(elements) return target.to_str() # Skip elif mimetype in skip: return None # Keep elif mimetype in keep: return body # Unknown else: print "X", path, mimetype return body def postproc(file): # Share file.set_value("share", ["everybody"]) # Title if file.class_id != "webpage": return handler = file.get_handler() events = handler.events elem = get_element(events, "h1") if elem: title = [unicode(x[1], "utf8") for x in elem.get_content_elements() if x[0] == TEXT] if title[-1] == u"¶": title.pop() title = u"".join(title) file.set_property("title", title, language) handler.events = events[: elem.start] + events[elem.end + 1 :] # 1. Make the '/docs/' folder docs = resource.get_resource("docs", soft=True) if not docs: docs = resource.make_resource("docs", Folder) # 2. Extract filename, mimetype, body = form["file"] cls = get_handler_class_by_mimetype(mimetype) handler = cls(string=body) docs.extract_archive(handler, language, filter, postproc, True) # Ok message = MSG(u"Documentation updated.") return context.come_back(message, goto="./docs")