def guess_mimetype(filename, default): """Override itools function 'guess_type' to intercept the encoding. """ name, extension, language = FileName.decode(filename) filename = FileName.encode((name, extension, None)) mimetype, encoding = guess_type(filename) return encoding_map.get(encoding, mimetype or default)
def get_new_resource_name(self, form): name = super(File_NewInstance, self).get_new_resource_name(form) if name: return name filename, mimetype, body = form['data'] name, extension, language = FileName.decode(filename) if mimetype not in ('application/xhtml+xml', 'text/html'): name = FileName.encode((name, extension, None)) return name
def test_FileName(self): map = { 'index': ('index', None, None), 'index.html': ('index', 'html', None), 'index.html.en': ('index', 'html', 'en'), 'index.html.en.gz': ('index.html.en', 'gz', None), 'itools.tar': ('itools', 'tar', None), 'itools.tar.gz': ('itools.tar', 'gz', None), 'toto.en': ('toto', None, 'en'), 'toto.gz': ('toto', 'gz', None), 'toto.Z': ('toto', 'Z', None), } for name, result in map.iteritems(): self.assertEqual(FileName.decode(name), result) self.assertEqual(FileName.encode(result), name)
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 extract_archive(self, handler, default_language, filter=None, postproc=None, update=False): change_resource = self.database.change_resource for path_str in handler.get_contents(): # 1. Skip folders path = Path(path_str) if path.endswith_slash: continue # Skip the owner file (garbage produced by microsoft) filename = path[-1] if filename.startswith('~$'): continue # 2. Create parent folders if needed folder = self for name in path[:-1]: name, title = process_name(name) subfolder = folder.get_resource(name, soft=True) if subfolder is None: folder = folder.make_resource(name, Folder) folder.set_value('title', title, default_language) elif not isinstance(subfolder, Folder): raise RuntimeError, MSG_NAME_CLASH else: folder = subfolder # 3. Find out the resource name and title, the file mimetype and # language mimetype = guess_mimetype(filename, 'application/octet-stream') name, extension, language = FileName.decode(filename) name, title = process_name(name) language = language or default_language # Keep the filename extension (except in webpages) if mimetype not in ('application/xhtml+xml', 'text/html'): name = FileName.encode((name, extension, None)) # 4. The body body = handler.get_file(path_str) if filter: body = filter(path_str, mimetype, body) if body is None: continue # 5. Update or make file file = folder.get_resource(name, soft=True) if file: if update is False: msg = 'unexpected resource at {path}' raise RuntimeError, msg.format(path=path_str) if mimetype == 'text/html': body = tidy_html(body) file_handler = file.get_handler(language) else: file_handler = file.get_handler() old_body = file.handler.to_str() file_handler.load_state_from_string(body) if postproc: postproc(file) # FIXME Comparing the bytes does not work for XML, so we use # this weak heuristic if len(old_body) != len(file.handler.to_str()): change_resource(file) else: # Case 1: the resource does not exist file = folder._make_file(name, filename, mimetype, body, language) file.set_value('title', title, language=language) if postproc: postproc(file)
def extract_archive(self, handler, default_language, filter=None, postproc=None, update=False): change_resource = self.database.change_resource for path_str in handler.get_contents(): # 1. Skip folders clean_path = "/".join([ checkid(x) or 'file' if x else 'file' for x in path_str.split("/")]) path = Path(clean_path) if path.endswith_slash: continue # Skip the owner file (garbage produced by microsoft) filename = path[-1] if filename.startswith('~$'): continue # 2. Create parent folders if needed folder = self for name in path[:-1]: name, title = process_name(name) subfolder = folder.get_resource(name, soft=True) if subfolder is None: folder = folder.make_resource(name, Folder) folder.set_value('title', title, default_language) elif not isinstance(subfolder, Folder): raise RuntimeError, MSG_NAME_CLASH else: folder = subfolder # 3. Find out the resource name and title, the file mimetype and # language mimetype = guess_mimetype(filename, 'application/octet-stream') name, extension, language = FileName.decode(filename) name, title = process_name(name) language = language or default_language # Keep the filename extension (except in webpages) if mimetype not in ('application/xhtml+xml', 'text/html'): name = FileName.encode((name, extension, None)) # 4. The body body = handler.get_file(path_str) if filter: body = filter(path_str, mimetype, body) if body is None: continue # 5. Update or make file file = folder.get_resource(name, soft=True) if file: if update is False: msg = 'unexpected resource at {path}' raise RuntimeError, msg.format(path=path_str) if mimetype == 'text/html': body = tidy_html(body) file_handler = file.get_handler(language) else: file_handler = file.get_handler() old_body = file.handler.to_str() file_handler.load_state_from_string(body) if postproc: postproc(file) # FIXME Comparing the bytes does not work for XML, so we use # this weak heuristic if len(old_body) != len(file.handler.to_str()): change_resource(file) else: # Case 1: the resource does not exist file = folder._make_file(name, filename, mimetype, body, language) file.set_value('title', title, language=language) if postproc: postproc(file)