def create_and_send_event(self, page, event_type): if not self.zeitgeist_client: return if not hasattr(page, 'source') \ or not isinstance(page.source, File): return uri = page.source.uri origin = Gio.File(uri).get_parent().get_uri() text = _('Wiki page: %s') % page.name # T: label for how zim pages show up in the recent files menu, %s is the page name subject = Subject.new_for_values( mimetype='text/x-zim-wiki', uri=uri, origin=origin, interpretation=Interpretation.TEXT_DOCUMENT, manifestation=Manifestation.FILE_DATA_OBJECT, text=text) event = Event.new_for_values(actor='application://zim.desktop', interpretation=event_type, manifestation=Manifestation.USER_ACTIVITY, subjects=[ subject, ]) self.zeitgeist_client.insert_event(event)
def file_exists(file): "Checks if a file exists" if file is None: return False return Gio.File(file).query_exists()
def get_language(self): if 'http' in self.uicore.core.filename: language = 'http' else: if os.path.isabs(self.uicore.core.filename): path = self.uicore.core.filename else: path = os.path.abspath(self.uicore.core.filename) f = Gio.File(path) path = f.get_path() info = f.query_info("*") mime_type = info.get_content_type() language = None if mime_type: language = self.get_language_for_mime_type(mime_type) if not language: print 'No language found for mime type "%s"' % mime_type else: print 'Couldn\'t get mime type for file "%s"' % self.uicore.core.filename #self.buffer.set_language(language) return language
def open_file(buffer, filename): # get the new language for the file mimetype if os.path.isabs(filename): path = filename else: path = os.path.abspath(filename) f = Gio.File(path) path = f.get_path() info = f.query_info("*") mime_type = info.get_content_type() language = None if mime_type: language = get_language_for_mime_type(mime_type) if not language: print('No language found for mime type "%s"' % mime_type) else: print('Couldn\'t get mime type for file "%s"' % filename) buffer.set_language(language) buffer.set_highlight_syntax(True) remove_all_marks(buffer) load_file(buffer, path) # TODO: check return return True
def file_is_local(file): "Checks if a file is on a local filesystem" if file is None: return False return Gio.File(file).get_uri_scheme() == 'file'
def test_invalid(self): loc = '/tmp/foo' self.mox.StubOutWithMock(Gio, 'File') f_anything = self.get_anything('n') Gio.File(loc).AndReturn(f_anything) self.mox.ReplayAll() assert xl.trax.util.get_tracks_from_uri(loc) == [] self.mox.VerifyAll()
def test_invalid(self): if SkipTest is not None: raise SkipTest("Test is borken because of moxing out error") loc = '/tmp/foo' self.mox.StubOutWithMock(gio, 'File') f_anything = self.get_anything('n') Gio.File(loc).AndReturn(f_anything) self.mox.ReplayAll() self.assertEqual(xl.trax.util.get_tracks_from_uri(loc), []) self.mox.VerifyAll()
def _on_sections_synchronized (self, sections_model, *args): # Column0: display name # Column1: GIcon in string format sections_model.clear () for device in g15devices.find_all_devices(): if device.model_id == 'virtual': icon_file = g15icontools.get_icon_path(["preferences-system-window", "preferences-system-windows", "gnome-window-manager", "window_fullscreen"]) else: icon_file = g15icontools.get_app_icon(self._gconf_client, device.model_id) icon = Gio.FileIcon(Gio.File(icon_file)) sections_model.append (device.model_fullname, icon.to_string())
def __init__(self, bundle_update): GObject.GObject.__init__(self) self.bundle_update = bundle_update self._input_stream = None self._output_stream = None self._pending_buffers = [] self._input_file = Gio.File(bundle_update.link) self._output_file = None self._downloaded_size = 0 self._cancelling = False self._input_file.read_async(self.__file_read_async_cb)
def file_write(file, data): "Writes data to file" try: if file is None: raise IOError if data is None: data = "" return Gio.File(file).replace_contents(data) except GLib.GError as error: raise IOError
def move(self): self.is_trashed = False file = Gio.File(self.filename) access = file.query_info('access::*') can_trash = access.get_attribute_boolean('access::can-trash') try: if can_trash: self.is_trashed = file.trash() except Gio.Error, error: print error if error.code == Gio.ERROR_NOT_SUPPORTED: print "not supported."
def __file_read_async_cb(self, gfile, result): if self._cancelling: return try: self._input_stream = self._input_file.read_finish(result) except: self.emit('error', traceback.format_exc()) return temp_file_path = self._get_temp_file_path(self.bundle_update.link) self._output_file = Gio.File(temp_file_path) self._output_stream = self._output_file.create() self._input_stream.read_async(self._CHUNK_SIZE, self.__read_async_cb, GObject.PRIORITY_LOW)
def test_directory(self): loc = '/tmp/foo' retval = ['foo', 'bar', 'baz'] # Gio call to find type self.mox.StubOutWithMock(Gio, 'File') d_anything = self.get_anything('d') Gio.File(loc).AndReturn(d_anything) # scanning self.mox.StubOutWithMock(xl.collection.Library, 'rescan') xl.collection.Library.rescan() self.mox.StubOutWithMock(xl.collection.Collection, 'get_tracks') xl.collection.Collection.get_tracks().AndReturn(retval) self.mox.ReplayAll() xl.trax.util.get_tracks_from_uri(loc) self.mox.VerifyAll()
def __notify_state_cb(self, file_transfer, pspec): logging.debug('__notify_state_cb %r', self.props.state) if self.props.state == FT_STATE_OPEN: # Need to hold a reference to the socket so that python doesn't # closes the fd when it goes out of scope self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self._socket.connect(self._socket_address) output_stream = Gio.unix.OutputStream(self._socket.fileno(), True) logging.debug('opening %s for reading', self._file_name) input_stream = Gio.File(self._file_name).read() if self.initial_offset > 0: input_stream.skip(self.initial_offset) # TODO: Use splice_async when it gets implemented self._splicer = StreamSplicer(input_stream, output_stream) self._splicer.start()
def __notify_state_cb(self, file_transfer, pspec): logging.debug('__notify_state_cb %r', self.props.state) if self.props.state == FT_STATE_OPEN: # Need to hold a reference to the socket so that python doesn't # close the fd when it goes out of scope self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self._socket.connect(self._socket_address) input_stream = Gio.unix.InputStream(self._socket.fileno(), True) destination_file = Gio.File(self.destination_path) if self.initial_offset == 0: output_stream = destination_file.create() else: output_stream = destination_file.append_to() # TODO: Use splice_async when it gets implemented self._splicer = StreamSplicer(input_stream, output_stream) self._splicer.start()
def test_directory(self): if SkipTest is not None: raise SkipTest("Test is borken because of moxing out error") loc = '/tmp/foo' retval = ['foo', 'bar', 'baz'] # Gio call to find type self.mox.StubOutWithMock(gio, 'File') d_anything = self.get_anything('d') Gio.File(loc).AndReturn(d_anything) # scanning self.mox.StubOutWithMock(xl.collection.Library, 'rescan') xl.collection.Library.rescan() self.mox.StubOutWithMock(xl.collection.Collection, 'get_tracks') xl.collection.Collection.get_tracks().AndReturn(retval) self.mox.ReplayAll() xl.trax.util.get_tracks_from_uri(loc) self.mox.VerifyAll()
properties_iface = connection[dbus.PROPERTIES_IFACE] properties = properties_iface.GetAll(CONNECTION_INTERFACE_REQUESTS) classes = properties['RequestableChannelClasses'] for prop, allowed_prop in classes: channel_type = prop.get(CHANNEL + '.ChannelType', '') target_handle_type = prop.get(CHANNEL + '.TargetHandleType', '') if len(prop) == 2 and \ channel_type == CHANNEL_TYPE_FILE_TRANSFER and \ target_handle_type == CONNECTION_HANDLE_TYPE_CONTACT: return True return False if __name__ == '__main__': import tempfile test_file_name = '/home/tomeu/isos/Soas2-200904031934.iso' test_input_stream = Gio.File(test_file_name).read() test_output_stream = Gio.File(tempfile.mkstemp()[1]).append_to() # TODO: Use splice_async when it gets implemented splicer = StreamSplicer(test_input_stream, test_output_stream) splicer.start() loop = GObject.MainLoop() loop.run()
def on_item_tree_drag_data_received(self, treeview, context, x, y, selection, info, etime): items = treeview.get_model() types_before = (Gtk.TreeViewDropPosition.BEFORE, Gtk.TreeViewDropPosition.INTO_OR_BEFORE) types_into = (Gtk.TreeViewDropPosition.INTO_OR_BEFORE, Gtk.TreeViewDropPosition.INTO_OR_AFTER) types_after = (Gtk.TreeViewDropPosition.AFTER, Gtk.TreeViewDropPosition.INTO_OR_AFTER) if str(selection.get_target()) == 'MOZO_ITEM_ROW': drop_info = treeview.get_dest_row_at_pos(x, y) before = None after = None if self.drag_data is None: return False item = self.drag_data # by default we assume, that the items stays in the same menu destination = item.get_parent() if drop_info: path, position = drop_info target = items[path][3] # move the item to the directory, if the item was dropped into it if isinstance(target, MateMenu.TreeDirectory) and (position in types_into): # append the selected item to the choosen menu destination = target elif position in types_before: before = target elif position in types_after: after = target else: # this does not happen pass else: path = (len(items) - 1,) after = items[path][3] if isinstance(item, MateMenu.TreeEntry): self.editor.moveItem(item, destination, before, after) elif isinstance(item, MateMenu.TreeDirectory): if not self.editor.moveMenu(item, destination, before, after): self.loadUpdates() elif isinstance(item, MateMenu.TreeSeparator): self.editor.moveSeparator(item, destination, before, after) context.finish(True, True, etime) elif str(selection.get_target()) == 'text/plain': if selection.data is None: return False menus, iter = self.tree.get_object('menu_tree').get_selection().get_selected() parent = menus[iter][2] drop_info = treeview.get_dest_row_at_pos(x, y) before = None after = None if drop_info: path, position = drop_info if position in types_before: before = items[path][3] else: after = items[path][3] else: path = (len(items) - 1,) after = items[path][3] file_path = urllib.unquote(selection.data).strip() if not file_path.startswith('file:'): return myfile = Gio.File(uri=file_path) file_info = myfile.query_info(Gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE) content_type = file_info.get_content_type() if content_type == 'application/x-desktop': input_stream = myfile.read() keyfile = GLib.KeyFile() keyfile.load_from_data(input_stream.read()) self.editor.createItem(parent, before, after, KeyFile=keyfile) elif content_type in ('application/x-shellscript', 'application/x-executable'): self.editor.createItem(parent, before, after, Name=os.path.split(file_path)[1].strip(), Exec=file_path.replace('file://', '').strip(), Terminal=False) self.drag_data = None
def import_image(self, file_uri, ocrlang): img_fp = Gio.File(file_uri).read() img = Image.open(img_fp) self.__add_img(img, ocrlang) self.drop_cache()