def set_uri(self, uri, add_to_history=True, cursor=None, scroll=None): folder = gio.file_parse_name(uri) file_info = folder.query_info('standard::*') ft = file_info.get_file_type() if ft == gio.FILE_TYPE_DIRECTORY: uri = folder.get_path() or folder.get_uri() elif ft in (gio.FILE_TYPE_MOUNTABLE, gio.FILE_TYPE_SHORTCUT): uri = file_info.get_attribute_as_string(gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI) folder = gio.File(uri=uri) else: raise Exception('Unknown file type %s %s' % (ft, folder.get_uri())) self.uri_entry.set_text(uri) if self.current_folder: self.history.update(self.current_folder.get_uri(), self.view.get_cursor(), self.sw.props.hadjustment.value) self.current_folder = folder self.setup_monitor(folder) self.fill() self.view.grab_focus() if cursor: self.view.set_cursor(cursor) else: self.view.set_cursor((0,)) self.view.refresh(False) if add_to_history: self.history.add(folder.get_uri())
def process_archive(self, cfile, fi): from subprocess import Popen, PIPE ct = Popen(["/usr/bin/env", "file", "-b", "--mime-type", cfile.get_path()], stdout=PIPE).communicate()[0].strip() if ct == 'application/x-rar': cmd = ['unrar', 'x', '-y'] elif ct == 'application/zip': cmd = ['unzip', '-o'] elif ct == 'application/x-7z-compressed': cmd = ['7z', 'x', '-y'] else: print ct return False folder = gio.file_parse_name('/tmp/fmd-archive-cache/' + fi.get_display_name()) if not folder.query_exists(): folder.make_directory_with_parents() cmd.append(self.current_folder.get_child(fi.get_name()).get_path()) pid, _, _, _ = glib.spawn_async(cmd, working_directory=folder.get_path(), flags=glib.SPAWN_SEARCH_PATH) self.set_uri(folder.get_uri()) return True
def test_add_item(): rm = gtk.RecentManager() file = gio.file_parse_name(__file__) uri = file.get_uri() assert rm.add_full(uri, dict(mime_type = 'foo', app_type = 'foo', app_name = 'boo', app_exec = 'gedit')) recent_info = rm.lookup_item(uri) assert recent_info.get_uri() == uri
def goto_dir(editor): import gio import os.path f = gio.file_parse_name(os.path.dirname(editor.uri)) ct = f.query_info(gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE).get_content_type() ai = gio.app_info_get_default_for_type(ct, False) if ai: ai.launch([f]) editor.message('File manager started', 1000) else: editor.message('Unknown content type for launch %s' % ct)
def entry_activate(self, widget, event=None): """ Called when the user presses enter in the entry box """ path = self.entry.get_text() if path.startswith('~'): path = os.path.expanduser(path) f = gio.file_parse_name(path) try: ftype = f.query_info('standard::type').get_file_type() except glib.GError, e: logger.error(e) self.entry.set_text(self.current.get_parse_name()) return
def open_mime(self): fname, name, root, top = self.get_selected_file() if fname: import gio self.hide() refresh_gui() f = gio.file_parse_name(fname) ct = f.query_info(gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE).get_content_type() ai = gio.app_info_get_default_for_type(ct, False) if ai: ai.launch([f]) else: self.pwindow().emessage('Unknown content type for launch %s' % ct, 'error')
class WalkerTexasRanger(object): def __init__(self, onResult, onClear=None, onFinish=None): self._onResult = onResult self._onClear = onClear self._onFinish = onFinish self._enumerate = gio.Cancellable() self._userData = None def _result(self, *args, **kwargs): if callable(self._onResult): userData = self._userData and [self._userData] or [None] apply(self._onResult, [self] + list(args) + userData, kwargs) def _clear(self, *args, **kwargs): if callable(self._onClear): userData = self._userData and [self._userData] or [None] apply(self._onClear, [self] + list(args) + userData, kwargs) def _finish(self, *args, **kwargs): if callable(self._onFinish): userData = self._userData and [self._userData] or [None] apply(self._onFinish, [self] + list(args) + userData, kwargs) def cancel(self): """ Cancels a running query. """ self._stamp = None self._enumerate.cancel() def walk(self, query, ignoredot = False, maxdepth = -1, user_data = None): # cancel any existing request self._enumerate.cancel() self._enumerate.reset() self._userData = user_data # call the clear callback self._clear() # consider doing query_info_async to determine if this is # a directory without potential blocking for slow disks. if not query or not os.path.isdir(query): False # build a unique stamp for this query stamp = self._stamp = str(time.time()) + query # build our state and file objects # state => ( # unique query stamp, # dirs to traverse, # ignore dot files/dirs # max depth to traverse # current traversal depth # ) state = [stamp, [], ignoredot, maxdepth, 0] vfs = gio.vfs_get_default() gfile = vfs.get_file_for_path(query) # asynchronously get the list of children attrs = ','.join([_NAME_ATTRIBUTE, _TYPE_ATTRIBUTE]) gfile.enumerate_children_async(attrs, self._walk, 0, 0, self._enumerate, state) return True def _walk(self, gfile, result, state): stamp, todo, ignoredot, maxdepth, curdepth = state # return immediately if we have been End-Of-Lifed if stamp != self._stamp: return try: children = gfile.enumerate_children_finish(result) dirname = gfile.get_path() dirs = [] files = [] # iterate the children found for child in children: childname = child.get_attribute_string(_NAME_ATTRIBUTE) childtype = child.get_attribute_uint32(_TYPE_ATTRIBUTE) # keep track of dirs and files for callback. # add directories to traverse if needed. if childtype == gio.FILE_TYPE_DIRECTORY: if childname.startswith('.') and ignoredot: continue # only add this to the todo list if its within # our depth limit. if maxdepth < 0 or curdepth + 1 <= maxdepth: fullpath = os.path.join(gfile.get_path(), childname) todo.insert(0, (fullpath, curdepth + 1)) dirs.insert(0, childname) elif childtype == gio.FILE_TYPE_REGULAR: if childname.startswith('.') and ignoredot: continue files.insert(0, childname) self._result(dirname, dirs, files) children.close() del children except gio.Error, ex: pass del gfile # we are done if no more dirs are left to traverse. # call finish and return. if not len(todo): self._finish() return # perform our next enumerate which calls this same method nextpath, nextdepth = todo.pop() state[-1] = nextdepth next = gio.file_parse_name(nextpath) attrs = ','.join([_NAME_ATTRIBUTE, _TYPE_ATTRIBUTE]) next.enumerate_children_async(attrs, self._walk, 0, 0, self._enumerate, state)
def get_local_path(url): return gio.file_parse_name(url.strip()).get_path()
def get_local_path(url): return gio.file_parse_name(url.strip()).get_path()
def add(self, uri): info = gio.file_parse_name(uri).query_info('standard::display-name,standard::icon') title = info.get_display_name() pixbuf = self.filelist.get_pixbuf(info) self.model.append((title, uri, pixbuf))
def test_relative_path(): file = gio.file_parse_name('foo/bar') assert file.get_path() == os.path.abspath('foo/bar')