Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 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_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_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')
def is_text(srcpath):
# xdg.Mime considers .lo as text, which is technically true but useless
if srcpath[-3:] == '.lo': return False
import xdg.Mime
mimetype = str(xdg.Mime.get_type (srcpath))
for valid in ['text', 'xml', 'shellscript', 'perl', 'm4', 'xbel', 'javascript']:
if valid in mimetype:
return True
# Force indexing of nsis files
if srcpath[-4:] == '.nsh' or srcpath[-4:] == '.nsi':
return True
return False
if not is_text(srcpath):
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:
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]
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 :
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