def set_contents(self, contents): if self.exists(): h = gnomevfs.Handle(self._URI, open_mode=gnomevfs.OPEN_WRITE) else: h = gnomevfs.create(self._URI, open_mode=gnomevfs.OPEN_WRITE) h.write(contents) h.close() self.close()
def create_file_in(self, uri): """Launches a new file dialog box set to create a new file within the URI specified.""" try: file_uri = dialogs.retrieve_new_file_name(uri) if file_uri is not None: newfile = gnomevfs.create(file_uri, open_mode=gnomevfs.OPEN_WRITE) newfile.truncate(0) newfile.close() except IOError, e: dialog.error(e)
def file_write(file, data): "Writes data to file" try: if file is None: raise IOError if data is None: data = "" if file_exists(file) == True: f = gnomevfs.open(file, gnomevfs.OPEN_WRITE) else: f = gnomevfs.create(file, gnomevfs.OPEN_WRITE) f.write(data) f.close() except gnomevfs.Error: raise IOError
def save_hashfile(self, uri): for format in formats: regex = format.filename_regex result = regex.search(uri) if result: self.format = format log("Saving with format:", format.name) break self.update_hashfile() if self.abort: return list = [] base = os.path.dirname(uri) remove = len(base) + 1 for ff in self.files: # convert to a path relative to hashfile dest = self.get_relative_filename(ff.filename, base) if not dest: self.set_status(_("Cannot save hashfile")) log("Cannot save hashfile '%s'" % ff.filename) return file = gnomevfs.unescape_string_for_display(dest) hash = ff.real_hash list.append((hash, file)) u = gnomevfs.URI(uri) log("saving to:", u) f = gnomevfs.create(u, gnomevfs.OPEN_WRITE) self.format.write_file(f, list) f.close() self.modified = False self.filename = uri self.update_title() self.set_status(_("Hashfile Saved"))
def save_hashfile(self, uri): for format in formats: regex = format.filename_regex result = regex.search(uri) if result: self.format = format log("Saving with format:", format.name) break self.update_hashfile() if self.abort: return list=[] base = os.path.dirname(uri) remove = len(base)+1 for ff in self.files: # convert to a path relative to hashfile dest = self.get_relative_filename(ff.filename, base) if not dest: self.set_status(_("Cannot save hashfile")) log("Cannot save hashfile '%s'" % ff.filename) return file = gnomevfs.unescape_string_for_display(dest) hash = ff.real_hash list.append( (hash,file) ) u = gnomevfs.URI(uri) log("saving to:", u) f = gnomevfs.create(u , gnomevfs.OPEN_WRITE) self.format.write_file(f, list) f.close() self.modified=False self.filename=uri self.update_title() self.set_status(_("Hashfile Saved"))
def open_for_write(filename, mode=0666): try: return gnomevfs.create(filename, gnomevfs.OPEN_WRITE, mode) except gnomevfs.FileExistsError: return gnomevfs.Handle(filename, gnomevfs.OPEN_WRITE)
def new_file(self, widget): """ Create a new file. @param widget: Reference to the button @type widget: A Button object. """ sel_model, sel_iter = self.treeview.get_selection().get_selected() dir_iter = None new_file_dir = self.flist.get_current_dir() path = None # If some selection exists if sel_iter != None: path = self.treestore.get_path(sel_iter) if len(path) == 1: # New file will be created in current folder dir_iter = None else: dir_iter = self.treestore.get_iter(path[0:len(path) - 1]) for i in range(1, len(path)): dir_iter = self.treestore.get_iter(path[0:i]) iter_value = self.treestore.get_value(dir_iter, 1) new_file_dir = os.path.join(new_file_dir, iter_value) #print new_file_dir # Create file i = 1 new_file_name = _("New File") new_file_uri = os.path.join(new_file_dir, new_file_name) while gnomevfs.exists(new_file_uri): new_file_uri = \ os.path.join(new_file_dir, new_file_name + " " + str(i)) i += 1 old_file_dir = self.flist.get_current_dir() self.flist.set_current_dir(new_file_dir) try: files = self.flist.get_files(False) new_file = gnomevfs.create(new_file_uri, gnomevfs.OPEN_WRITE) new_file.close() except gnomevfs.AccessDeniedError: main_text = _("<b>You can't create a file.</b>") secondary_text = _("You don't have the required permissions to create a file in this directory.") error = ErrorDialog(main_text, secondary_text) error_return = error.run() if error_return == gtk.RESPONSE_DELETE_EVENT: error.destroy() return elif error_return == 1: error.destroy() return # Verifying if the directory is empty to delete "(Empty)" string. if len(files) == 0: if dir_iter == None: empty_path = (0,) else: empty_path = list(path) empty_path[len(path)-1] = 0 empty_path = tuple(empty_path) empty_iter = self.treestore.get_iter(empty_path) self.treestore.remove(empty_iter) theme = gtk.icon_theme_get_default() info = theme.lookup_icon("text-x-generic", gtk.ICON_SIZE_MENU, 0) if info == None: # text-x-generic icon doesn't exist icon = self.treeview.render_icon(gtk.STOCK_FILE, gtk.ICON_SIZE_MENU) else: icon = info.load_icon() name = gnomevfs.URI(new_file_uri).short_name new_iter = self.treestore.append(dir_iter, [icon, name]) if len(files) == 0 and dir_iter != None: self.treeview.expand_row(path[0:len(path) - 1], False) self.flist.set_current_dir(old_file_dir) path = self.treestore.get_path(new_iter) self.cell2.set_property('editable', True) self.treeview.set_cursor_on_cell(path, self.tvcolumn, self.cell2, start_editing=True) self.cell2.set_property('editable', False)