def test_inheritance(self): text_python = Mime.lookup('text/x-python') self.check_mimetype(text_python, 'text', 'x-python') text_plain = Mime.lookup('text/plain') app_executable = Mime.lookup('application/x-executable') self.assertEqual(text_python.inherits_from(), set([text_plain, app_executable]))
def test_lookup(self): pdf1 = Mime.lookup("application/pdf") pdf2 = Mime.lookup("application", "pdf") self.assertEqual(pdf1, pdf2) self.check_mimetype(pdf1, 'application', 'pdf') pdf1.get_comment()
def test_canonical(self): text_xml = Mime.lookup('text/xml') self.check_mimetype(text_xml, 'text', 'xml') self.check_mimetype(text_xml.canonical(), 'application', 'xml') # Already is canonical python = Mime.lookup('text/x-python') self.check_mimetype(python.canonical(), 'text', 'x-python')
def test_by_name(self): dot_c = Mime.get_type_by_name('foo.c') self.check_mimetype(dot_c, 'text', 'x-csrc') dot_C = Mime.get_type_by_name('foo.C') self.check_mimetype(dot_C, 'text', 'x-c++src') # But most names should be case insensitive dot_GIF = Mime.get_type_by_name('IMAGE.GIF') self.check_mimetype(dot_GIF, 'image', 'gif')
def get_mime_type(content): mime = Mime.get_type_by_data(content) """try harder to guess mime type""" """sometimes helps to trim leading newlines""" """FIXME: try with smaller chunk of content""" if mime is None or mime == "text/plain": tmpmime = Mime.get_type_by_data(content.strip("\n")) if tmpmime and tmpmime != "text/plain": mime = tmpmime else: mime = "text/x-csrc" return str(mime)
def test_parsing(self): self.assertEqual(len(self.magic.bytype), 9) # Check repr() doesn't throw an error repr(self.magic) prio, png = self.magic.bytype[Mime.lookup('image', 'png')][0] self.assertEqual(prio, 50) assert isinstance(png, Mime.MagicRule), type(png) repr(png) # Check this doesn't throw an error. self.assertEqual(png.start, 0) self.assertEqual(png.value, b'\x89PNG') self.assertEqual(png.mask, None) self.assertEqual(png.also, None) prio, jpeg = self.magic.bytype[Mime.lookup('image', 'jpeg')][0] assert isinstance(jpeg, Mime.MagicMatchAny), type(jpeg) self.assertEqual(len(jpeg.rules), 2) self.assertEqual(jpeg.rules[0].value, b'\xff\xd8\xff') prio, ora = self.magic.bytype[Mime.lookup('image', 'openraster')][0] assert isinstance(ora, Mime.MagicRule), type(ora) self.assertEqual(ora.value, b'PK\x03\x04') ora1 = ora.also assert ora1 is not None self.assertEqual(ora1.start, 30) ora2 = ora1.also assert ora2 is not None self.assertEqual(ora2.start, 38) self.assertEqual(ora2.value, b'image/openraster') prio, svg = self.magic.bytype[Mime.lookup('image', 'svg+xml')][0] self.assertEqual(len(svg.rules), 2) self.assertEqual(svg.rules[0].value, b'<!DOCTYPE svg') self.assertEqual(svg.rules[0].range, 257) prio, psd = self.magic.bytype[Mime.lookup('image', 'vnd.adobe.photoshop')][0] self.assertEqual(psd.value, b'8BPS \0\0\0\0') self.assertEqual(psd.mask, b'\xff\xff\xff\xff\0\0\xff\xff\xff\xff') prio, elf = self.magic.bytype[Mime.lookup('application', 'x-executable')][0] self.assertEqual(elf.value, b'\x01\x11') self.assertEqual(elf.word, 2) # Test that a newline within the value doesn't break parsing. prio, madeup = self.magic.bytype[Mime.lookup('application', 'madeup')][0] self.assertEqual(madeup.rules[0].value, b'ab\ncd') self.assertEqual(madeup.rules[1].mask, b'\xff\xff\n\xff\xff') prio, replaced = self.magic.bytype[Mime.lookup('application', 'tobereplaced')][0] self.assertEqual(replaced.value, b'jkl') addedrules = self.magic.bytype[Mime.lookup('application', 'tobeaddedto')] self.assertEqual(len(addedrules), 2) self.assertEqual(addedrules[1][1].value, b'pqr')
def test_create_mimetype(self): mt1 = Mime.MIMEtype('application', 'pdf') mt2 = Mime.MIMEtype('application', 'pdf') self.assertEqual(id(mt1), id(mt2)) # Check caching amr = Mime.MIMEtype('audio', 'AMR') self.check_mimetype(amr, 'audio', 'amr') # Check lowercase ogg = Mime.MIMEtype('audio/ogg') self.check_mimetype(ogg, 'audio', 'ogg') # Check split on / self.assertRaises(Exception, Mime.MIMEtype, 'audio/foo/bar')
def _reload(self): icons = [] mime_type = mime.get_type(self.file_path) if mime_type != None: icons.append(str(mime_type).replace("/","-")) icons.append("text-plain") icons.append("panel-searchtool") icons.append("gnome-searchtool") icon = g15icontools.get_icon_path(icons, size=self.plugin._screen.height) if icon is None: self._icon_surface = None self._icon_embedded = None else: try : icon_surface = g15cairo.load_surface_from_file(icon) self._icon_surface = icon_surface self._icon_embedded = g15icontools.get_embedded_image_url(icon_surface) except Exception as e: logger.warning("Failed to get icon %s", str(icon), exc_info = e) self._icon_surface = None self._icon_embedded = None self._stop() if os.path.exists(self.file_path): self._subtitle = time.strftime('%Y-%m-%d %H:%M', time.localtime(os.path.getmtime(self.file_path))) self._message = "" self.thread = G15TailThread(self) self.thread.start() else: self._subtitle = "" self._message = "File does not exist"
def _filltree(self, root=''): self._pending_filltree = None self.treeView.heading('#0', text=os.path.basename(self.currentPath)) while len(self.treeView.get_children(root)): self.treeView.delete(self.treeView.get_children(root)[0]) if not root: image = self.module._get_icon_for_mime(("folder",)) self.insert_child(self.currentPath, '..', image) for current, dirs, files in os.walk(os.path.join(self.currentPath, root)): for dir_ in sorted(dirs): if not self.module.apply_exclude(dir_): continue image = self.module._get_icon_for_mime(("folder",)) self.insert_child(current, dir_, image) self.insert_child(os.path.join(current, dir_), '.', image=None) dirs[:] = [] for file_ in sorted(files): if not self.module.apply_exclude(file_): continue image = None if hasImageSupport: fullPath = os.path.join(current, file_) mime = str(Mime.get_type_by_name(fullPath)).split('/') image = self.module._get_icon_for_mime(mime) self.insert_child(current, file_, image)
def get_type_optimized(path, follow=False): mtypes = sorted(Mime.globs.all_matches(path), key=(lambda x: x[1]), reverse=True) if mtypes: max_weight = mtypes[0][1] i = 1 for mt, w in mtypes[1:]: if w < max_weight: break i += 1 mtypes = mtypes[:i] if len(mtypes) == 1: return mtypes[0][0] possible = [mt for mt, w in mtypes] else: possible = None # Break if we have a named-based candidate if mtypes: return mtypes[0][0] t = None try: t = Mime.magic.match(path, possible=possible) except IOError: t = None if t: return t elif mtypes: return mtypes[0][0] else: return Mime.text if Mime.is_text_file(path) else Mime.octet_stream
def test_match_file(self): png_file = os.path.join(self.tmpdir, 'image') with open(png_file, 'wb') as f: f.write(resources.png_data) res = self.magic.match(png_file) self.check_mimetype(res, 'image', 'png') # With list of options options = [Mime.lookup('image','png'), Mime.lookup('image', 'jpeg'), Mime.lookup('image', 'nonexistant')] # Missing MIMEtype should be dropped res = self.magic.match(png_file, possible=options) self.check_mimetype(res, 'image', 'png') # Nonexistant file path = os.path.join(self.tmpdir, 'nonexistant') self.assertRaises(IOError, self.magic.match, path)
def get(request): realpath = __url2path(request.path_info) file = open(realpath) response = HttpResponse(content = file.read(), content_type = str(Mime.get_type(realpath))) response['Last-Modified'] = datetime.datetime.fromtimestamp(os.path.getmtime(realpath)).strftime('%a, %d %b %Y %H:%M:%S GMT') response['Accept-Ranges'] = 'bytes' response['Content-Length'] = str(os.path.getsize(realpath)) return response
def test_get_type(self): # File that doesn't exist - get type by name imgpng = Mime.get_type(example_file("test.gif")) self.check_mimetype(imgpng, 'image', 'gif') # File that does exist - get type by contents imgpng = Mime.get_type(example_file("png_file")) self.check_mimetype(imgpng, 'image', 'png') # Directory - special case inodedir = Mime.get_type(example_file("subdir")) self.check_mimetype(inodedir, 'inode', 'directory') # Mystery files mystery_text = Mime.get_type(example_file('mystery_text')) self.check_mimetype(mystery_text, 'text', 'plain') mystery_exe = Mime.get_type(example_file('mystery_exe')) self.check_mimetype(mystery_exe, 'application', 'executable') # Symlink self.check_mimetype(Mime.get_type(example_file("png_symlink")), 'image', 'png') self.check_mimetype( Mime.get_type(example_file("png_symlink"), follow=False), 'inode', 'symlink')
def test_match_data(self): res = self.magic.match_data(resources.png_data) self.check_mimetype(res, 'image', 'png') # Denied by min or max priority notpng_max40 = self.magic.match_data(resources.png_data, max_pri=40) assert notpng_max40 is None, notpng_max40 notpng_min60 = self.magic.match_data(resources.png_data, min_pri=60) assert notpng_min60 is None, notpng_min60 # With list of options options = [Mime.lookup('image', 'nonexistant'), # Missing MIMEtype should be dropped Mime.lookup('image','png'), Mime.lookup('image', 'jpeg')] res = self.magic.match_data(resources.png_data, possible=options) self.check_mimetype(res, 'image', 'png') # Non matching res = self.magic.match_data(b'oiejgoethetrkjgnwefergoijekngjekg') assert res is None, res
def get(request): realpath = __url2path(request.path_info) file = open(realpath) response = HttpResponse(content=file.read(), content_type=str(Mime.get_type(realpath))) response['Last-Modified'] = datetime.datetime.fromtimestamp( os.path.getmtime(realpath)).strftime('%a, %d %b %Y %H:%M:%S GMT') response['Accept-Ranges'] = 'bytes' response['Content-Length'] = str(os.path.getsize(realpath)) return response
def get_good_mime(music_file): mime = Mime.get_type_by_contents(music_file) if mime == None: # try magic instead mime = magic.open(magic.MAGIC_MIME) mime.load() mime = mime.file(music_file) mime = mime.split(";")[0] else: mime = str(mime) return mime
def __fill_propfind(props, url): ''' Fills response by real file/dir attributes @param props:dict - requested properties @param url:strl - relative url @return lxml.etree.Element ''' realpath = __url2path(url) response = etree.Element('response') etree.SubElement(response, 'href').text = url # 1. usable values propstat = etree.SubElement(response, 'propstat') prop = etree.SubElement(propstat, 'prop') if ('creationdate' in props): etree.SubElement( prop, 'creationdate').text = datetime.datetime.fromtimestamp( os.path.getctime(realpath)).strftime('%Y-%m-%dT%H:%M:%SZ') del props['creationdate'] if ('getlastmodified' in props): # 'Mon, 11 Apr 2011 04:03:09 GMT' # FIXME: GMT etree.SubElement( prop, 'getlastmodified').text = datetime.datetime.fromtimestamp( os.path.getmtime(realpath)).strftime( '%a, %d %b %Y %H:%M:%S GMT') del props['getlastmodified'] if (os.path.isdir(realpath)): if ('getcontenttype' in props): etree.SubElement(prop, 'getcontenttype').text = 'httpd/unix-directory' del props['getcontenttype'] if ('resourcetype' in props): etree.SubElement(etree.SubElement(prop, 'resourcetype'), 'collection') del props['resourcetype'] else: if ('getcontentlength' in props): etree.SubElement(prop, 'getcontentlength').text = str( os.path.getsize(realpath)) del props['getcontentlength'] if ('getcontenttype' in props): etree.SubElement(prop, 'getcontenttype').text = str( Mime.get_type(realpath)) del props['getcontenttype'] if ('resourcetype' in props): etree.SubElement(prop, 'resourcetype') del props['resourcetype'] etree.SubElement(propstat, 'status').text = 'HTTP/1.1 200 OK' # 2. unusable values propstat = etree.SubElement(response, 'propstat') prop = etree.SubElement(propstat, 'prop') for i in props: etree.SubElement(prop, i) etree.SubElement(propstat, 'status').text = 'HTTP/1.1 404 Not Found' return response
def test_match_file(self): png_file = os.path.join(self.tmpdir, 'image') with open(png_file, 'wb') as f: f.write(resources.png_data) res = self.magic.match(png_file) self.check_mimetype(res, 'image', 'png') # With list of options options = [ Mime.lookup('image', 'png'), Mime.lookup('image', 'jpeg'), Mime.lookup('image', 'nonexistant') ] # Missing MIMEtype should be dropped res = self.magic.match(png_file, possible=options) self.check_mimetype(res, 'image', 'png') # Nonexistant file path = os.path.join(self.tmpdir, 'nonexistant') self.assertRaises(IOError, self.magic.match, path)
def test_get_type_by_contents(self): tmpdir = tempfile.mkdtemp() try: test_file = os.path.join(tmpdir, "test") with open(test_file, "wb") as f: f.write(resources.png_data) imgpng = Mime.get_type_by_contents(test_file) self.check_mimetype(imgpng, 'image', 'png') finally: shutil.rmtree(tmpdir)
def delete(request): ''' ''' realpath = __url2path(request.path_info) response = HttpResponse(status = 204, content_type = str(Mime.get_type(realpath))) response['Location'] = request.path_info response['Content-Length'] = '0' if (os.path.isdir(realpath)): shutil.rmtree(realpath) else: os.remove(realpath) return response
def delete(request): ''' ''' realpath = __url2path(request.path_info) response = HttpResponse(status=204, content_type=str(Mime.get_type(realpath))) response['Location'] = request.path_info response['Content-Length'] = '0' if (os.path.isdir(realpath)): shutil.rmtree(realpath) else: os.remove(realpath) return response
def test_get_type(self): tmpdir = tempfile.mkdtemp() try: # File that doesn't exist - get type by name path = os.path.join(tmpdir, "test.png") imgpng = Mime.get_type(path) self.check_mimetype(imgpng, 'image', 'png') # File that does exist - get type by contents path = os.path.join(tmpdir, "test") with open(path, "wb") as f: f.write(resources.png_data) imgpng = Mime.get_type(path) self.check_mimetype(imgpng, 'image', 'png') # Directory - special case path = os.path.join(tmpdir, "test2") os.mkdir(path) inodedir = Mime.get_type(path) self.check_mimetype(inodedir, 'inode', 'directory') finally: shutil.rmtree(tmpdir)
def test_match_data(self): res = self.magic.match_data(resources.png_data) self.check_mimetype(res, 'image', 'png') # Denied by min or max priority notpng_max40 = self.magic.match_data(resources.png_data, max_pri=40) assert notpng_max40 is None, notpng_max40 notpng_min60 = self.magic.match_data(resources.png_data, min_pri=60) assert notpng_min60 is None, notpng_min60 # With list of options options = [ Mime.lookup('image', 'nonexistant'), # Missing MIMEtype should be dropped Mime.lookup('image', 'png'), Mime.lookup('image', 'jpeg') ] res = self.magic.match_data(resources.png_data, possible=options) self.check_mimetype(res, 'image', 'png') # Non matching res = self.magic.match_data(b'oiejgoethetrkjgnwefergoijekngjekg') assert res is None, res
def test_is_text(self): assert Mime._is_text(b'abcdef \n') assert not Mime._is_text(b'abcdef\x08') assert not Mime._is_text(b'abcdef\x0e') assert not Mime._is_text(b'abcdef\x1f') assert not Mime._is_text(b'abcdef\x7f') # Check nonexistant file. assert not Mime.is_text_file('/fwoijorij')
def put(request): ''' Location Statuscode: 201 Content-Length Content-Type ''' realpath = __url2path(request.path_info) file = open(realpath, "w") file.write(request.read()) file.close() response = HttpResponse(status = 201, content_type = str(Mime.get_type(realpath))) response['Location'] = request.path_info response['Content-Length'] = str(os.path.getsize(realpath)) return response
def setUp(self): self.tmpdir = tempfile.mkdtemp() self.path = os.path.join(self.tmpdir, 'mimemagic') with open(self.path, 'wb') as f: f.write(resources.mime_magic_db) self.path2 = os.path.join(self.tmpdir, 'mimemagic2') with open(self.path2, 'wb') as f: f.write(resources.mime_magic_db2) # Read the files self.magic = Mime.MagicDB() self.magic.merge_file(self.path) self.magic.merge_file(self.path2) self.magic.finalise()
def put(request): ''' Location Statuscode: 201 Content-Length Content-Type ''' realpath = __url2path(request.path_info) file = open(realpath, "w") file.write(request.read()) file.close() response = HttpResponse(status=201, content_type=str(Mime.get_type(realpath))) response['Location'] = request.path_info response['Content-Length'] = str(os.path.getsize(realpath)) return response
def icon(self, fi): if fi.isDir(): return QtGui.QIcon.fromTheme("inode-directory") mime = Mime.get_type(fi.absoluteFilePath()) mimestr = str(mime).replace("/", "-") icon = G.icon_cache.get(mimestr) if icon: return icon ipath = IconTheme.getIconPath(mimestr, theme=G.icon_theme) if not ipath: ipath = IconTheme.getIconPath(mime.media + "-x-generic", theme=G.icon_theme) qi = QtGui.QIcon(ipath) if qi.isNull(): qi = QtGui.QIcon.fromTheme("text-plain") G.icon_cache[mimestr] = qi return qi
def __fill_propfind(props, url): ''' Fills response by real file/dir attributes @param props:dict - requested properties @param url:strl - relative url @return lxml.etree.Element ''' realpath = __url2path(url) response = etree.Element('response') etree.SubElement(response, 'href').text = url # 1. usable values propstat = etree.SubElement(response, 'propstat') prop = etree.SubElement(propstat, 'prop') if ('creationdate' in props): etree.SubElement(prop, 'creationdate').text = datetime.datetime.fromtimestamp(os.path.getctime(realpath)).strftime('%Y-%m-%dT%H:%M:%SZ') del props['creationdate'] if ('getlastmodified' in props): # 'Mon, 11 Apr 2011 04:03:09 GMT' # FIXME: GMT etree.SubElement(prop, 'getlastmodified').text = datetime.datetime.fromtimestamp(os.path.getmtime(realpath)).strftime('%a, %d %b %Y %H:%M:%S GMT') del props['getlastmodified'] if (os.path.isdir(realpath)): if ('getcontenttype' in props): etree.SubElement(prop, 'getcontenttype').text = 'httpd/unix-directory' del props['getcontenttype'] if ('resourcetype' in props): etree.SubElement(etree.SubElement(prop, 'resourcetype'), 'collection') del props['resourcetype'] else: if ('getcontentlength' in props): etree.SubElement(prop, 'getcontentlength').text = str(os.path.getsize(realpath)) del props['getcontentlength'] if ('getcontenttype' in props): etree.SubElement(prop, 'getcontenttype').text = str(Mime.get_type(realpath)) del props['getcontenttype'] if ('resourcetype' in props): etree.SubElement(prop, 'resourcetype') del props['resourcetype'] etree.SubElement(propstat, 'status').text = 'HTTP/1.1 200 OK' # 2. unusable values propstat = etree.SubElement(response, 'propstat') prop = etree.SubElement(propstat, 'prop') for i in props: etree.SubElement(prop, i) etree.SubElement(propstat, 'status').text = 'HTTP/1.1 404 Not Found' return response
def get_default_cover(self): mime_type = mime.get_type(self.playing_uri) new_cover_uri = None if mime_type != None: mime_icon = g15icontools.get_icon_path(str(mime_type).replace("/","-"), size=self.screen.height) if mime_icon != None: new_cover_uri = mime_icon if new_cover_uri != None: try : new_cover_uri = "file://" + urllib.pathname2url(new_cover_uri) except Exception as e: logger.debug("Error getting default cover, using None", exc_info = e) new_cover_uri = None if new_cover_uri == None: new_cover_uri = g15icontools.get_icon_path(["audio-player", "applications-multimedia" ], size=self.screen.height) return new_cover_uri
def TestOneInput(input_bytes): # We need to make the file an absolute path testfile_path = os.path.join(os.getcwd(), "testfile.tmp") with open(testfile_path, "wb") as f: f.write(input_bytes) # Test basic Mime API Mime.get_type2(testfile_path) Mime.get_type_by_contents(testfile_path) Mime.get_type_by_data(input_bytes) # Test GlobDB globs = Mime.GlobDB() try: globs.merge_file(testfile_path) globs.merge_file(testfile_path) except UnicodeError as e: pass except ValueError as e: if ("not enough values to unpack" in str(e) or "invalid literal for int" in str(e)): pass else: raise e # Test MagicDB magic = Mime.MagicDB() try: magic.merge_file(testfile_path) magic.finalise() except UnicodeDecodeError: pass except (OSError, ValueError) as e: msg = str(e) if ("Not a MIME magic file" in msg or "Malformed section heading" in msg): pass else: raise e # Cleanup os.remove(testfile_path)
def get_mimetype(self): '''Get the mime-type for this file. Will use the XDG mimetype system if available, otherwise fallsback to the standard library C{mimetypes}. @returns: the mimetype as a string, e.g. "text/plain" ''' if xdgmime: mimetype = xdgmime.get_type(self.path, name_pri=80) return str(mimetype) else: mimetype, encoding = mimetypes.guess_type(self.path, strict=False) if encoding == 'gzip': return 'application/x-gzip' elif encoding == 'bzip': return 'application/x-bzip' elif encoding == 'compress': return 'application/x-compress' else: return mimetype or 'application/octet-stream'
def test_parsing(self): p1 = os.path.join(self.tmpdir, 'globs2a') with open(p1, 'w') as f: f.write(resources.mime_globs2_a) p2 = os.path.join(self.tmpdir, 'globs2b') with open(p2, 'w') as f: f.write(resources.mime_globs2_b) globs = Mime.GlobDB() globs.merge_file(p1) globs.merge_file(p2) ag = globs.allglobs self.assertEqual(ag[_l('text', 'x-diff')], set([(55, '*.patch', ()), (50, '*.diff', ())])) self.assertEqual(ag[_l('text', 'x-c++src')], set([(50, '*.C', ('cs', ))])) self.assertEqual(ag[_l('text', 'x-readme')], set([(20, 'RDME', ('cs', ))])) assert _l('text', 'x-python') not in ag, ag
def _reload(self): icons = [] mime_type = mime.get_type(self.file_path) if mime_type != None: icons.append(str(mime_type).replace("/", "-")) icons.append("text-plain") icons.append("panel-searchtool") icons.append("gnome-searchtool") icon = g15icontools.get_icon_path(icons, size=self.plugin._screen.height) if icon is None: self._icon_surface = None self._icon_embedded = None else: try: icon_surface = g15cairo.load_surface_from_file(icon) self._icon_surface = icon_surface self._icon_embedded = g15icontools.get_embedded_image_url( icon_surface) except Exception as e: logger.warning("Failed to get icon %s", str(icon), exc_info=e) self._icon_surface = None self._icon_embedded = None self._stop() if os.path.exists(self.file_path): self._subtitle = time.strftime( '%Y-%m-%d %H:%M', time.localtime(os.path.getmtime(self.file_path))) self._message = "" self.thread = G15TailThread(self) self.thread.start() else: self._subtitle = "" self._message = "File does not exist"
def test_get_type(self): # File that doesn't exist - get type by name imgpng = Mime.get_type(example_file("test.png")) self.check_mimetype(imgpng, 'image', 'png') # File that does exist - get type by contents imgpng = Mime.get_type(example_file("png_file")) self.check_mimetype(imgpng, 'image', 'png') # Directory - special case inodedir = Mime.get_type(example_file("subdir")) self.check_mimetype(inodedir, 'inode', 'directory') # Mystery files mystery_text = Mime.get_type(example_file('mystery_text')) self.check_mimetype(mystery_text, 'text', 'plain') mystery_exe = Mime.get_type(example_file('mystery_exe')) self.check_mimetype(mystery_exe, 'application', 'executable') # Symlink self.check_mimetype(Mime.get_type(example_file("png_symlink")), 'image', 'png') self.check_mimetype(Mime.get_type(example_file("png_symlink"), follow=False), 'inode', 'symlink')
#!/usr/bin/env python """Run this manually to test xdg.Mime.get_type2 against all files in a directory. Syntax: ./fuzz-mime.py /dir/to/test/ """ from __future__ import print_function import sys, os from xdg import Mime testdir = sys.argv[1] files = os.listdir(testdir) for f in files: f = os.path.join(testdir, f) try: print(f, Mime.get_type2(f)) except: print(f) raise
def test_canonical(self): text_xml = Mime.lookup('text/xml') self.check_mimetype(text_xml, 'text', 'xml') self.check_mimetype(text_xml.canonical(), 'application', 'xml')
def load_surface_from_file(filename, size = None): type = None if filename == None: logger.warning("Empty filename requested") return None if filename.startswith("http:") or filename.startswith("https:"): full_cache_path = get_image_cache_file(filename, size) if full_cache_path: meta_fileobj = open(full_cache_path + "m", "r") type = meta_fileobj.readline() meta_fileobj.close() if type == "image/svg+xml" or filename.lower().endswith(".svg"): return load_svg_as_surface(filename, size) else: return pixbuf_to_surface(gtk.gdk.pixbuf_new_from_file(full_cache_path), size) if is_url(filename): type = None try: file = urllib.urlopen(filename) data = file.read() type = file.info().gettype() if filename.startswith("file://"): type = str(mime.get_type(filename)) if filename.startswith("http:") or filename.startswith("https:"): full_cache_path = get_cache_filename(filename, size) cache_fileobj = open(full_cache_path, "w") cache_fileobj.write(data) cache_fileobj.close() meta_fileobj = open(full_cache_path + "m", "w") meta_fileobj.write(type + "\n") meta_fileobj.close() if type == "image/svg+xml" or filename.lower().endswith(".svg"): svg = rsvg.Handle() try: if not svg.write(data): raise Exception("Failed to load SVG") svg_size = svg.get_dimension_data()[2:4] if size == None: size = svg_size surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(size[0]) if not isinstance(size, int) else size, int(size[1]) if not isinstance(size, int) else size) context = cairo.Context(surface) if size != svg_size: scale = get_scale(size, svg_size) context.scale(scale, scale) svg.render_cairo(context) surface.flush() return surface finally: svg.close() else: if type == "text/plain": if filename.startswith("file://"): pixbuf = gtk.gdk.pixbuf_new_from_file(filename[7:]) return pixbuf_to_surface(pixbuf, size) raise Exception("Could not determine type") else: pbl = gtk.gdk.pixbuf_loader_new_with_mime_type(type) pbl.write(data) pixbuf = pbl.get_pixbuf() pbl.close() return pixbuf_to_surface(pixbuf, size) return None except Exception as e: logger.warning("Failed to get image %s (%s).", filename, type, exc_info = e) return None else: if os.path.exists(filename): try: if filename.lower().endswith(".svg"): if os.path.islink(filename): filename = os.path.realpath(filename) return load_svg_as_surface(filename, size) else: return pixbuf_to_surface(gtk.gdk.pixbuf_new_from_file(filename), size) except Exception as e: logger.warning("Failed to get image %s (%s).", filename, type, exc_info = e) return None
def test_get_type2(self): # File that doesn't exist - use the name self.check_mimetype(Mime.get_type2(example_file('test.png')), 'image', 'png') # File that does exist - use the contents self.check_mimetype(Mime.get_type2(example_file('png_file')), 'image', 'png') # Does exist - use name before contents self.check_mimetype(Mime.get_type2(example_file('file.png')), 'image', 'png') self.check_mimetype(Mime.get_type2(example_file('word.doc')), 'application', 'msword') # Ambiguous file extension self.check_mimetype(Mime.get_type2(example_file('glade.ui')), 'application', 'x-glade') self.check_mimetype(Mime.get_type2(example_file('qtdesigner.ui')), 'application', 'x-designer') # text/x-python has greater weight than text/x-readme self.check_mimetype(Mime.get_type2(example_file('README.py')), 'text', 'x-python') # Directory - special filesystem object self.check_mimetype(Mime.get_type2(example_file('subdir')), 'inode', 'directory') # Mystery files: mystery_missing = Mime.get_type2(example_file('mystery_missing')) self.check_mimetype(mystery_missing, 'application', 'octet-stream') mystery_binary = Mime.get_type2(example_file('mystery_binary')) self.check_mimetype(mystery_binary, 'application', 'octet-stream') mystery_text = Mime.get_type2(example_file('mystery_text')) self.check_mimetype(mystery_text, 'text', 'plain') mystery_exe = Mime.get_type2(example_file('mystery_exe')) self.check_mimetype(mystery_exe, 'application', 'executable') # Symlink self.check_mimetype(Mime.get_type2(example_file("png_symlink")), 'image', 'png') self.check_mimetype(Mime.get_type2(example_file("png_symlink"), follow=False), 'inode', 'symlink')
def test_lookup(self): pdf1 = Mime.lookup("application/pdf") pdf2 = Mime.lookup("application", "pdf") self.assertEqual(pdf1, pdf2) self.check_mimetype(pdf1, 'application', 'pdf')
def test_get_comment(self): # Check these don't throw an error. One that is likely to exist: Mime.MIMEtype("application", "pdf").get_comment() # And one that's unlikely to exist: Mime.MIMEtype("application", "ierjg").get_comment()
def test_mimetype_repr(self): mt = Mime.lookup('application', 'zip') repr(mt) # Just check that this doesn't throw an error.
def test_get_type_by_data(self): imgpng = Mime.get_type_by_data(resources.png_data) self.check_mimetype(imgpng, 'image', 'png')
def setUp(self): self.globs = Mime.GlobDB() self.globs.allglobs = self.allglobs self.globs.finalise()
def test_get_type_by_name(self): appzip = Mime.get_type_by_name("foo.zip") self.check_mimetype(appzip, 'application', 'zip')
def test_by_name(self): dot_c = Mime.get_type_by_name('foo.c') self.check_mimetype(dot_c, 'text', 'x-csrc') dot_C = Mime.get_type_by_name('foo.C') self.check_mimetype(dot_C, 'text', 'x-c++src')
def test_get_type2(self): # File that doesn't exist - use the name self.check_mimetype(Mime.get_type2(example_file('test.gif')), 'image', 'gif') # File that does exist - use the contents self.check_mimetype(Mime.get_type2(example_file('png_file')), 'image', 'png') # Does exist - use name before contents self.check_mimetype(Mime.get_type2(example_file('file.png')), 'image', 'png') self.check_mimetype(Mime.get_type2(example_file('word.doc')), 'application', 'msword') # Ambiguous file extension glade_mime = Mime.get_type2(example_file('glade.ui')) self.assertEqual(glade_mime.media, 'application') # Grumble, this is still ambiguous on some systems self.assertIn(glade_mime.subtype, {'x-gtk-builder', 'x-glade'}) self.check_mimetype(Mime.get_type2(example_file('qtdesigner.ui')), 'application', 'x-designer') # text/x-python has greater weight than text/x-readme self.check_mimetype(Mime.get_type2(example_file('README.py')), 'text', 'x-python') # Directory - special filesystem object self.check_mimetype(Mime.get_type2(example_file('subdir')), 'inode', 'directory') # Mystery files: mystery_missing = Mime.get_type2(example_file('mystery_missing')) self.check_mimetype(mystery_missing, 'application', 'octet-stream') mystery_binary = Mime.get_type2(example_file('mystery_binary')) self.check_mimetype(mystery_binary, 'application', 'octet-stream') mystery_text = Mime.get_type2(example_file('mystery_text')) self.check_mimetype(mystery_text, 'text', 'plain') mystery_exe = Mime.get_type2(example_file('mystery_exe')) self.check_mimetype(mystery_exe, 'application', 'executable') # Symlink self.check_mimetype(Mime.get_type2(example_file("png_symlink")), 'image', 'png') self.check_mimetype( Mime.get_type2(example_file("png_symlink"), follow=False), 'inode', 'symlink')
def __init__(self, name): self.name = name self.mimetype = Mime.lookup("text", "plain")