def set_form_field_value(self, item, value_as_string): label, old = self.GetPyData(item) form = self.get_form(item, label) field = form._get_field(label) value_as_string = field.fix_string(value_as_string) if value_as_string != old: # test-validate the user input (see formField.Field.get) try: if isinstance(field, formField.PixelField): field.get_size(IMAGE_TEST_INFO, 100, 100, label, value_as_string) # 100 is just some dummy value for base, dpi else: field.get(IMAGE_TEST_INFO, label=label, value_as_string=value_as_string, test=True) self.set_dirty(True) except formField.ValidationError, details: reason = exception_to_unicode(details, WX_ENCODING) self.show_error(reason) if formField.Field.safe: return if value_as_string == '': # hack, fix me value_as_string = ' ' self.SetPyData(item, (label, value_as_string)) self.SetItemText(item, self.tree_label(label, value_as_string)) form.set_field_as_string(label, value_as_string)
def get_photo(info_file, info_not_file, result): """Get a :class:`core.pil.Photo` instance from a file. If there is an error opening the file, func:`process_error` will be called. :param info_file: file information :type info_file: dictionary :param info_not_file: image information not related to file :type info_not_file: string :param result: settings to send to progress dialog box (such as ``stop for errors``) :type result: dict :returns: photo, result :rtype: tuple """ try: photo = pil.Photo(info_file, info_not_file) result['skip'] = False result['abort'] = False return photo, result except Exception, details: reason = exception_to_unicode(details) #log error details message = u'%s: %s:\n%s' % (_('Unable to open file'), info_file['path'], reason)
def menu_action(self, program, comment, method, *args, **keyw): try: success = method(*args, **keyw) self.show_info(_("If you restart %s, " "the action will appear in the context menu.") % program + comment) except Exception, details: reason = exception_to_unicode(details, WX_ENCODING) self.show_error(_("Phatch could not install the action in %s:") % program + "\n\n" + reason)
def load_actionlist_data(self, filename): if not os.path.exists(filename): return try: data, warnings = api.open_actionlist(filename) except KeyError, details: self.show_error(ERROR_INSTALL_ACTION\ % exception_to_unicode(details, WX_ENCODING)) return
def menu_action(self, program, comment, method, *args, **keyw): try: success = method(*args, **keyw) self.show_info( _('If you restart %s, ' 'the action will appear in the context menu.') % program + comment) except Exception, details: reason = exception_to_unicode(details, WX_ENCODING) self.show_error(_('Phatch could not install the action in %s:')\ % program + '\n\n' + reason)
def menu_file_export_droplet(self, method, *args, **keyw): folder = self.get_droplet_folder() if folder is None: return try: method(folder=folder, *args) self.show_info(_('Phatch successfully created the droplet.')) except Exception, details: reason = exception_to_unicode(details, WX_ENCODING) self.show_error(_('Phatch could not create the droplet: ')\ + '\n\n' + reason)
def __init__(self, info, info_to_dump, get_pil, image=None): """The ``get_pil`` parameter is necessary for tags as width, height, size and mode. :param info: pil, pyexiv2, ... tag, value info :type info: dict :param get_pil: method to retrieve the pil image :type get_pil: callable """ #parameters self.get_pil = get_pil path = info['path'] #sources if image == None: image = get_pil() sources = { metadata.InfoPil: image, metadata.InfoPexif: image, metadata.InfoZexif: image } #check format -> readable/writable metadata with pyexiv2 if exif and exif.is_readable_format(image.format): self.pyexiv2, pyexiv2_temp = exif.read_metadata_temp(path) if pyexiv2_temp: pyexiv2_temp.close() self.pyexiv2_temp = True self.writable_exif = exif.is_writable_format_exif(image.format) self.writable_iptc = exif.is_writable_format_exif(image.format) self.writable = self.writable_exif or self.writable_iptc if self.writable_exif: self.pyexiv2['Exif.Image.Software'] = CONTEXT['app_title'] sources[metadata.InfoExif] = sources[metadata.InfoIptc] =\ self.pyexiv2 else: self.pyexiv2 = None self.pyexiv2_temp = False self.writable = self.writable_exif = self.writable_iptc = False #retrieve dump info try: info_dumped = info_to_dump.open(path, sources).dump(free=True) except Exception, details: reason = unicoding.exception_to_unicode(details) #log error details message = u'%s:%s:\n%s' % (_('Unable extract variables from file'), path, reason) raise Exception(message)
def init_actions(actions): """Initializes all actions. Shows an error to the user if an action fails to initialize. :param actions: actions :type actions: list of :class:`core.models.Action` :returns: False, if one action fails, True otherwise :rtype: bool """ for action in actions: try: action.init() except Exception, details: reason = exception_to_unicode(details) message = u'%s\n\n%s' % ( _("Can not apply action %(a)s:") \ % {'a': _(action.label)}, reason) send.frame_show_error(message) return False
def apply_action_to_photo(action, photo, read_only_settings, cache, image_file, result): """Apply a single action to a photo. It uses :func:`log_error` for non fatal errors or :func:`process_error` for serious errors. The settings are read only as the actions don't have permission to change them. :param action: action :type action: :class:`core.models.Action` :param photo: photo :type photo: :class:`core.pil.Photo` :param read_only_settings: read only settings :type read_only_settings: :class:`lib.odict.ReadOnlyDict` :param cache: cache for data which is usefull across photos :type cache: dictionary :param image_file: filename reference during error logging :type image_file: string :param result: settings for dialog (eg ``stop_for_errors``) :type result: dictionary """ try: photo = action.apply(photo, read_only_settings, cache) result['skip'] = False result['abort'] = False #log non fatal errors/warnings flush_log(photo, image_file, action) return photo, result except Exception, details: flush_log(photo, image_file, action) folder, image = os.path.split(ensure_unicode(image_file)) reason = exception_to_unicode(details) message = u'%s\n%s\n%s' % ( _("Can not apply action %(a)s on image '%(i)s' in folder:")\ % {'a': _(action.label), 'i': image}, folder, reason, ) return process_error(photo, message, image_file, action, result, ignore=True)
def __init__(self, info, info_to_dump, get_pil, image=None): """The ``get_pil`` parameter is necessary for tags as width, height, size and mode. :param info: pil, pyexiv2, ... tag, value info :type info: dict :param get_pil: method to retrieve the pil image :type get_pil: callable """ #parameters self.get_pil = get_pil path = info['path'] #sources if image == None: image = get_pil() sources = { metadata.InfoPil: image, metadata.InfoPexif: image, metadata.InfoZexif: image} #check format -> readable/writable metadata with pyexiv2 if exif and exif.is_readable_format(image.format): self.pyexiv2 = pyexiv2.ImageMetadata(path) self.pyexiv2.read() self.writable_exif = exif.is_writable_format_exif(image.format) self.writable_iptc = exif.is_writable_format_exif(image.format) self.writable = self.writable_exif or self.writable_iptc if self.writable_exif: self.pyexiv2['Exif.Image.Software'] = TITLE sources[metadata.InfoExif] = sources[metadata.InfoIptc] =\ self.pyexiv2 else: self.pyexiv2 = None self.writable = self.writable_exif = self.writable_iptc = False #retrieve dump info try: info_dumped = info_to_dump.open(path, sources).dump(free=True) except Exception, details: reason = unicoding.exception_to_unicode(details) #log error details message = u'%s:%s:\n%s' % (_('Unable extract variables from file'), path, reason) raise Exception(message)
def apply_action_to_photo(action, photo, read_only_settings, cache, image_file, result): """Apply a single action to a photo. It uses :func:`log_error` for non fatal errors or :func:`process_error` for serious errors. The settings are read only as the actions don't have permission to change them. :param action: action :type action: :class:`core.models.Action` :param photo: photo :type photo: :class:`core.pil.Photo` :param read_only_settings: read only settings :type read_only_settings: :class:`lib.odict.ReadOnlyDict` :param cache: cache for data which is usefull across photos :type cache: dictionary :param image_file: filename reference during error logging :type image_file: string :param result: settings for dialog (eg ``stop_for_errors``) :type result: dictionary """ try: photo = action.apply(photo, read_only_settings, cache) result['skip'] = False result['abort'] = False #log non fatal errors/warnings flush_log(photo, image_file, action) return photo, result except Exception, details: flush_log(photo, image_file, action) folder, image = os.path.split(ensure_unicode(image_file)) reason = exception_to_unicode(details) message = u'%s\n%s\n\n%s' % ( _("Can not apply action %(a)s on image '%(i)s' in folder:")\ % {'a': _(action.label), 'i': image}, folder, reason, ) return process_error(photo, message, image_file, action, result, ignore=True)
def assert_safe(actions): test_info = metadata.InfoTest() geek = False warning = '' for action in actions: warning_action = '' if action.label == 'Geek': geek = True for label, field in action._get_fields().items(): if label.startswith('_') \ or isinstance(field, formField.BooleanField)\ or isinstance(field, formField.ChoiceField)\ or isinstance(field, formField.SliderField): continue try: field.assert_safe(label, test_info) except Exception, details: warning_action += ' %s: %s\n'\ % (label, exception_to_unicode(details)) if warning_action: warning += '%s %s:\n%s' % (_(action.label), _('Action'), warning_action)