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 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 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 __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 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 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 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 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 _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')
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 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 get_mime_type(path): return str(Mime.get_type(path))