def set_uri(self, uri): """Data is safely saved somewhere. Update the document's URI and save_last_stat (for local saves). URI is not escaped. Internal.""" path = get_local_path(escape(uri)) if path is not None: self.document.save_last_stat = os.stat(path) # Record for next time self.document.set_uri(path or uri)
def set_uri(self, uri): """Data is safely saved somewhere. Update the document's URI and save_last_stat (for local saves). URI is not escaped. Internal.""" path = get_local_path(escape(uri)) if path is not None: self.document.save_last_stat = os.stat( path) # Record for next time self.document.set_uri(path or uri)
def store_image(self, img, inname, outname, ow, oh): """Store the thumbnail image it the correct location, adding the extra data required by the thumbnail spec.""" s=os.stat(inname) img.save(outname+self.fname, 'png', {'tEXt::Thumb::Image::Width': str(ow), 'tEXt::Thumb::Image::Height': str(oh), "tEXt::Thumb::Size": str(s.st_size), "tEXt::Thumb::MTime": str(s.st_mtime), 'tEXt::Thumb::URI': rox.escape('file://'+inname), 'tEXt::Software': self.name}) os.rename(outname+self.fname, outname)
def store_image(self, img, inname, outname, ow, oh): """Store the thumbnail image it the correct location, adding the extra data required by the thumbnail spec.""" s = os.stat(inname) img.save( outname + self.fname, 'png', { 'tEXt::Thumb::Image::Width': str(ow), 'tEXt::Thumb::Image::Height': str(oh), "tEXt::Thumb::Size": str(s.st_size), "tEXt::Thumb::MTime": str(s.st_mtime), 'tEXt::Thumb::URI': rox.escape('file://' + inname), 'tEXt::Software': self.name }) os.rename(outname + self.fname, outname)
def save_to_file_in_entry(self): """Call this when the user clicks on an OK button you provide.""" uri = self.entry.get_text() path = get_local_path(escape(uri)) if path: if not self.confirm_new_path(path): return try: self.set_sensitive(False) try: self.document.save_to_file(path) finally: self.set_sensitive(True) self.set_uri(uri) self.save_done() except: _report_save_error() else: rox.info(_("Drag the icon to a directory viewer\n" "(or enter a full pathname)"))
def save_to_file_in_entry(self): """Call this when the user clicks on an OK button you provide.""" uri = self.entry.get_text() path = get_local_path(escape(uri)) if path: if not self.confirm_new_path(path): return try: self.set_sensitive(False) try: self.document.save_to_file(path) finally: self.set_sensitive(True) self.set_uri(uri) self.save_done() except: _report_save_error() else: rox.info( _("Drag the icon to a directory viewer\n" "(or enter a full pathname)"))
def drag_data_get(self, widget, context, selection_data, info, time): if info == TARGET_RAW: try: self.set_sensitive(False) try: self.document.save_to_selection(selection_data) finally: self.set_sensitive(True) except: _report_save_error() _write_xds_property(context, None) return self.data_sent = 1 _write_xds_property(context, None) if self.drag_in_progress: self.destroy_on_drag_end = 1 else: self.save_done() return elif info != TARGET_XDS: _write_xds_property(context, None) alert("Bad target requested!") return # Using XDS: # # Get the path that the destination app wants us to save to. # If it's local, save and return Success # (or Error if save fails) # If it's remote, return Failure (remote may try another method) # If no URI is given, return Error to_send = 'E' uri = _read_xds_property(context, False) if uri: path = get_local_path(escape(uri)) if path: if not self.confirm_new_path(path): to_send = 'E' else: try: self.set_sensitive(False) try: self.document.save_to_file(path) finally: self.set_sensitive(True) self.data_sent = True except: _report_save_error() self.data_sent = False if self.data_sent: to_send = 'S' # (else Error) else: to_send = 'F' # Non-local transfer else: alert("Remote application wants to use " + "Direct Save, but I can't read the " + "XdndDirectSave0 (type text/plain) " + "property.") selection_data.set(selection_data.target, 8, to_send) if to_send != 'E': _write_xds_property(context, None) self.set_uri(uri) if self.data_sent: self.save_done()
def _leaf(fname): path = os.path.abspath(fname) uri = 'file://' + rox.escape(path) return md5hash(uri) + '.png'
class Thumbnailer: """Base class for programs which generate thumbnails. The method run() creates the thumbnail for a source file. This calls the methods get_image(), process_image() and store_image(). process_image() takes the image returned by get_image() and scales it to the correct dimensions then passes it through post_process_image() (which does nothing). You should override the method get_image() to create the image. You can also override post_process_image() if you wish to work on the scaled image.""" def __init__(self, name, fname, use_wdir=False, debug=False): """Initialise the thumbnailer. name - name of the program fname - a string to use in generated temp file names use_wdir - if true then use a temp directory to store files debug - if false then suppress most error messages """ self.name = name self.fname = fname self.use_wdir = use_wdir self.debug = debug self.failed = False def run(self, inname, outname=None, rsize=96): """Generate the thumbnail from the file inname - source file outname - path to store thumbnail image, or None for default location rsize - maximum size of thumbnail (in either axis) """ if not outname: outname = get_path_save(inname) elif not os.path.isabs(outname): outname = os.path.abspath(outname) if self.use_wdir: self.make_working_dir() try: img = self.get_image(inname, rsize) if img: ow = img.get_width() oh = img.get_height() img = self.process_image(img, rsize) self.store_image(img, inname, outname, ow, oh) else: # Thumbnail creation has failed. self.creation_failed(inname, outname, rsize) except: self.report_exception() if self.use_wdir: self.remove_working_dir() def get_image(self, inname, rsize): """Method you must define for your thumbnailer to do anything""" raise _("Thumbnail not implemented") def process_image(self, img, rsize): """Take the raw image and scale it to the correct size. Returns the result of scaling img and passing it to post_process_image()""" ow = img.get_width() oh = img.get_height() if ow > oh: s = float(rsize) / float(ow) else: s = float(rsize) / float(oh) w = int(s * ow) h = int(s * oh) if w != ow or h != oh: img = img.scale_simple(w, h, rox.g.gdk.INTERP_BILINEAR) return self.post_process_image(img, w, h) def post_process_image(self, img, w, h): """Perform some post-processing on the image. img - gdk-pixbuf of the image w - width h - height Return: modified image The default implementation just returns the image unchanged.""" return img def store_image(self, img, inname, outname, ow, oh): """Store the thumbnail image it the correct location, adding the extra data required by the thumbnail spec.""" s = os.stat(inname) img.save( outname + self.fname, 'png', { 'tEXt::Thumb::Image::Width': str(ow), 'tEXt::Thumb::Image::Height': str(oh), "tEXt::Thumb::Size": str(s.st_size), "tEXt::Thumb::MTime": str(s.st_mtime), 'tEXt::Thumb::URI': rox.escape('file://' + inname), 'tEXt::Software': self.name }) os.rename(outname + self.fname, outname) self.created = outname def make_working_dir(self): """Create the temporary directory and change into it.""" try: self.work_dir = tempfile.mkdtemp() except: self.report_exception() self.work_dir = None return self.old_dir = os.getcwd() os.chdir(self.work_dir) def remove_working_dir(self): """Remove our temporary directory, after changing back to the previous one""" if not self.work_dir: return os.chdir(self.old_dir) try: shutil.rmtree(self.work_dir) except: self.report_exception() self.work_dir = None def creation_failed(self, inname, outname, rsize): """Creation of a thumbnail failed. Stores a dummy file to mark it as per the Thumbnail spec.""" self.failed = True s = os.stat(inname) dummy = rox.g.gdk.Pixbuf(rox.g.gdk.COLORSPACE_RGB, False, 8, rsize, rsize) outname = get_path_save(inname, ttype=os.path.join('fail', self.fname)) d = os.path.dirname(outname) try: os.makedirs(d) except OSError, exc: if exc.errno != errno.EEXIST: raise dummy.save( outname + self.fname, 'png', { "tEXt::Thumb::Size": str(s.st_size), "tEXt::Thumb::MTime": str(s.st_mtime), 'tEXt::Thumb::URI': rox.escape('file://' + inname), 'tEXt::Software': self.name }) os.rename(outname + self.fname, outname) self.created = outname
def _leaf(fname): path=os.path.abspath(fname) uri='file://'+rox.escape(path) return md5hash(uri)+'.png'