ó
dòôdc           @` sþ  d  Z  d d l m Z m Z m Z m Z d d l Td d l Z d d l Z d d l	 Z	 d d l	 m
 Z
 d d l Z d d l m Z d d l m Z d d l m Z d d	 l m Z d
 e f d „  ƒ  YZ e	 j d ƒ d e j d e e e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z  d „  Z! d „  Z" d „  Z# d „  Z$ d „  Z% d „  Z& d „  Z' d „  Z( d „  Z) d „  Z* d  „  Z+ d! „  Z, d" „  Z- d# „  Z. d$ „  Z/ d% e f d& „  ƒ  YZ0 d' e f d( „  ƒ  YZ1 d) e f d* „  ƒ  YZ2 d S(+   u6   Built-in procedure to export a given item as an image.i    (   t   absolute_importt   divisiont   print_functiont   unicode_literals(   t   *N(   t   pdb(   t	   pygimplib(   t
   exceptions(   t   renamer(   t
   uniquifiert   ExportModesc           B` s   e  Z d Z \ Z Z Z RS(   i    i   i   (   i    i   i   (   t   __name__t
   __module__t   EXPORT_MODESt
   EACH_LAYERt   EACH_TOP_LEVEL_LAYER_OR_GROUPt   ENTIRE_IMAGE_AT_ONCE(    (    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR
      s   i   u   pngc         c` s¼  t  j ƒ  } t ƒ  }	 t ƒ  }
 | } | t j k rQ | d  k	 rQ t j | ƒ } n d  } | t j	 k r° |  j
 r° t j j |  j ƒ } t j | ƒ |  j j t d g | g ƒ n d  } |  j r	|  j
 r	t j j |  j ƒ } t j | ƒ |  j j t d g | g ƒ n	 |  j } x£t r·|  j } | } | } |  j } |  j r`|  j
 r`t | | | ƒ } n  | d  k ru| } n | } | t j k rM|  j
 rºt |  | | ƒ } t | | | ƒ } n  |  j j | d t ƒd  k	 rðt |  | ƒ d  Vqqãt j j  | j! t j j" g  g  d  d  ƒ } | d  k	 r>| j# |  | ƒ | _$ qã| j$ | _$ n– | t j% k rã|  j
 rŒt |  | | ƒ } t | | | ƒ } n  t& | ƒ } t& |  j j | d t ƒƒ } | | k rÚt |  | ƒ d  Vqqã| } n  | rö| j' ƒ  n  |  j( r^| rt) | | |	 ƒ } n  | r/| j* ƒ  } n  t+ | | |
 ƒ t, |  | | | | d t ƒn  |  j
 ri| t j	 k r‹t |  | | ƒ } n  t- |  | | | | | |	 ƒ \ } } | t. j/ k r|  j( ræt, |  | | | | d t ƒn  |  j
 rt- |  | | | | | |	 ƒ \ } } qn  | t j0 j1 j2 k ri|	 t j3 j4 | j$ ƒ j5 d 7_5 |  j6 j7 | j! ƒ qin  | r|| j8 ƒ  n  t9 |  | ƒ | d  k	 r¢t: | ƒ n  t |  | ƒ d  VqWd  S(   Nu   cleanup_contentst   with_folderst   force_default_file_extensioni   (;   R	   t   ItemUniquifiert   _FileExtensionPropertiest   setR
   R   t   Nonet   renamer_t   ItemRenamerR   t   process_exportt   pgt   pdbutilst   create_image_from_metadatat   input_imageR   t   gimp_image_undo_freezet   invokert   addt   _delete_image_on_cleanupt	   edit_modet   current_imaget   Truet   current_itemt   current_raw_itemt   _copy_layert   _merge_and_resize_imaget	   item_treet   nextt   Falset!   _refresh_image_copy_for_edit_modet   itemtreet   Itemt   rawt	   TYPE_ITEMt   renamet   nameR   t   _get_top_level_itemt
   push_statet   process_namest   _get_current_file_extensiont   lowert   _process_parent_folder_namest   _process_item_namet   _export_itemt   ExportStatusest   USE_DEFAULT_FILE_EXTENSIONt	   overwritet   OverwriteModest   SKIPt   patht   get_file_extensiont   processed_countt   _exported_raw_itemst   appendt	   pop_statet   _sync_raw_item_namet   _refresh_image(   t   batchert   output_directoryt   file_extensiont   export_modet   single_image_filename_patternt   use_file_extension_in_item_namet#   convert_file_extension_to_lowercaset    preserve_layer_name_after_exportt   item_uniquifiert   file_extension_propertiest   processed_parent_namest   default_file_extensiont   renamer_for_imaget   multi_layer_imaget
   image_copyt   itemt   current_file_extensiont   item_to_processt   raw_item_to_processt   image_to_processt   current_top_level_itemt   next_top_level_itemt   overwrite_modet   export_statust   unused_(    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyt   export   s°    									*								c         C` s/   |  j  r+ | d  k	 r+ t j j | ƒ q+ n  d  S(   N(   R   R   R   R   t   try_delete_image(   RH   t   image(    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR!   £   s    	c         C` s(   |  d  k	 r  |  j r  |  j d S|  Sd  S(   Ni    (   R   t   parents(   RW   (    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR3   ©   s    c         C` sK   xD |  j  D]9 } | | k r
 t | ƒ | j | ƒ | j | ƒ q
 q
 Wd  S(   N(   Rd   t   _validate_namet   uniquifyR    (   RW   RP   RR   t   parent(    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR8   °   s
    
c         C` s©   | sL | | k r( | j  d | 7_  qm t j j | j  | d t ƒ| _  n! t j j | j  | d t ƒ| _  t | ƒ | j | d t | j  t j j | j  ƒ ƒ ƒd  S(   Nu   .t   keep_extra_trailing_periodst   position(	   R2   R   R@   t$   get_filename_with_new_file_extensionR$   Re   Rf   t   _get_unique_substring_positionRA   (   RH   RW   RP   RX   RS   R   (    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR9   ¹   s    		
c         C` s4   t  j j |  j ƒ } | r, | | j r, | S| Sd  S(   N(   R   R@   RA   R2   t   is_valid(   RW   RS   RQ   t   item_file_extension(    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR6   Ì   s    c         C` sV   | j  } t j | t j ƒ } t j | ƒ | | _  | | _ |  j sR | |  _ n  | S(   u>  Merges all layers in the current image into one.
  
  Merging is necessary for:
  * custom procedures inserting layers (background, foreground). Some file
    formats may discard all but one layer.
  * multi-layer images, with each layer containing background or foreground
    which are originally separate layers.
  (	   R2   R   t   gimp_image_merge_visible_layerst	   gimpenumst   EXPAND_AS_NECESSARYt   gimp_layer_resize_to_image_sizet   active_layerR"   R&   (   RH   Rc   t   raw_itemt   raw_item_namet   raw_item_merged(    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR(   Õ   s    					c         C` sD   t  j j |  | d  t | j ƒ t t t ƒ } t j | | j	 ƒ | S(   N(
   R   R   t   copy_and_paste_layerR   t   lent   layersR$   R   t   gimp_item_set_nameR2   (   Rs   t
   dest_imageRW   t   raw_item_copy(    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR'   ì   s    	$c         C` s   t  j j j |  j ƒ |  _ d  S(   N(   R   R@   t   FilenameValidatort   validateR2   (   RW   (    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyRe   ó   s    c         C` s   t  |  ƒ t  d | ƒ S(   Nu   .(   Rw   (   t   str_RJ   (    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyRk   ÷   s    c      
   C` sP  t  | | ƒ } t j j | j ƒ } t j }	 |  j j t	 d ƒ j
 | ƒ ƒ t j j | |  j t | | ƒ ƒ \ }
 } |
 t j j j k r t j d ƒ ‚ n  |
 t j j j k rFt | t j j | ƒ | ƒ t |  t | ƒ t |  | | ƒ | | | | | | ƒ	 }	 |	 t j k rFt |  t | ƒ t j | | | | | | ƒ	 }	 qFn  |
 |	 f S(   Nu   Saving "{}"u	   cancelled(   t   _get_item_filepathR   R@   RA   R2   R;   t   NOT_EXPORTED_YETt   progress_updatert   update_textt   _t   formatR=   t   handle_overwritet   overwrite_chooserRk   R>   t   CANCELR   t   BatcherCancelErrorR?   t
   _make_dirst   ost   dirnamet   _export_item_once_wrappert   _get_export_funct   _get_run_modet   FORCE_INTERACTIVERo   t   RUN_INTERACTIVE(   RH   RW   Rc   Rs   RI   RS   RQ   t   output_filepathRJ   R_   R^   (    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR:   û   sB    						c         C` s†   | d k r d } n  t j j | ƒ } g  |  j D] } | j ^ q1 } | rp t j j | t j j | Œ  ƒ } n  t j j | |  j ƒ S(   u§  Returns a file path based on the specified directory path and the name of
  the item and its parents.
  
  The file path created has the following format:
    
    <directory path>/<item path components>/<item name>
  
  If the directory path is not an absolute path or is `None`, the
  current working directory is prepended.
  
  Item path components consist of parents' item names, starting with the
  topmost parent.
  u    N(   R   RŠ   R@   t   abspathRd   R2   t   join(   RW   t   dirpathR@   Rg   t   path_components(    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR   (  s    	$c         C` s¥   y t  j j | ƒ WnŠ t k
 r  } y9 | j d } | j d  k	 r^ | d j | j ƒ 7} n  Wn# t t	 f k
 r„ t
 | ƒ } n Xt j | |  j | ƒ ‚ n Xd  S(   Ni   u   : "{}"(   R   R@   t	   make_dirst   OSErrort   argst   filenameR   R„   t
   IndexErrort   AttributeErrort   strR   t   InvalidOutputDirectoryErrorR2   (   RW   R”   RS   t   et   message(    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR‰   B  s    c	   
      C` sS   |  j  | | | | |  j |  j Ž ) t |  | | | | | | | | ƒ	 }	 Wd  QX|	 S(   N(   t   export_context_managert   export_context_manager_argst   export_context_manager_kwargst   _export_item_once(
   RH   t   export_funct   run_modeRc   Rs   R‘   RJ   RS   RQ   R_   (    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyRŒ   P  s    c         C` s4   | | } | j  r) | j d k r) t j S|  j Sd  S(   Ni    (   Rl   RB   Ro   t   RUN_WITH_LAST_VALSt   initial_run_mode(   RH   RJ   RQ   t   file_extension_property(    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyRŽ   ]  s    
c         C` s   t  j j |  ƒ S(   N(   R   t   fileformatst   get_save_procedure(   RJ   (    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR   e  s    c	         C` sû   t  j }	 y> | | | | t j j | ƒ t j j t j j | ƒ ƒ ƒ Wn¤ t k
 rí }
 t	 t
 |
 ƒ ƒ rƒ t j t
 |
 ƒ ƒ ‚ q÷ t t
 |
 ƒ | ƒ r¤ t  j }	 q÷ t | | ƒ rÌ t | | _ t  j }	 q÷ t j t
 |
 ƒ | j | ƒ ‚ n
 Xt  j }	 |	 S(   N(   R;   R€   R   t   utilst   safe_encode_gimpRŠ   R@   t   basenamet   RuntimeErrort   _was_export_canceled_by_userRœ   R   Rˆ   t.   _should_export_again_with_interactive_run_modeR   t0   _should_export_again_with_default_file_extensionR+   Rl   R<   t   ExportErrorR2   t   EXPORT_SUCCESSFUL(   RH   R¤   R¥   Rc   Rs   R‘   RJ   RS   RQ   R_   Rž   (    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR£   i  s&    	#"	c         ` s    t  ‡  f d †  d d g Dƒ ƒ S(   Nc         3` s!   |  ] } | ˆ  j  ƒ  k Vq d  S(   N(   R7   (   t   .0RŸ   (   t   exception_message(    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pys	   <genexpr>ˆ  s    u	   cancelledu   canceled(   t   any(   Rµ   (    (   Rµ   s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR¯   ‡  s    c         C` s(   d |  j  ƒ  k o' | t j t j g k S(   Nu   calling error(   R7   Ro   R¦   t   RUN_NONINTERACTIVE(   Rµ   t   current_run_mode(    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR°   ‹  s    c         C` s
   |  | k S(   N(    (   RJ   RS   (    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR±   ‘  s    c         C` s;   |  j  | k r7 |  j r7 |  j r7 |  j  j |  j _ n  d  S(   N(   R%   R5   t
   is_previewR2   R&   (   RH   RY   (    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyRF   •  s    "c         C` s#   |  j  r |  j r t | ƒ n  d  S(   N(   R"   R   RG   (   RH   RV   (    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR,   š  s    c         C` s(   x! |  j  D] } t j |  | ƒ q
 Wd  S(   N(   Rx   R   t   gimp_image_remove_layer(   Rc   t   layer(    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyRG   Ÿ  s    t   _FileExtensionc           B` s   e  Z d  Z d „  Z RS(   u:  
  This class defines additional properties for a file extension.
  
  Attributes:
  
  * `is_valid` - If `True`, file extension is valid and can be used in filenames
    for file export procedures.
  
  * `processed_count` - Number of items with the specific file extension that
    have already been exported.
  c         C` s   t  |  _ d |  _ d  S(   Ni    (   R$   Rl   RB   (   t   self(    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyt   __init__±  s    	(   R   R   t   __doc__R¾   (    (    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR¼   ¤  s   R   c           B` s    e  Z d  Z d „  Z d „  Z RS(   u§   Mapping of file extensions from `pygimplib.fileformats.file_formats` to
  `_FileExtension` instances.
  
  File extension as a key is always converted to lowercase.
  c         C` s]   t  j t ƒ |  _ xD t j j D]6 } t ƒ  } x$ | j D] } | |  j | j ƒ  <q8 Wq Wd  S(   N(	   t   collectionst   defaultdictR¼   t   _propertiesR   R©   t   file_formatst   file_extensionsR7   (   R½   t   file_formatt   extension_propertiesRJ   (    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR¾   ¼  s
    	c         C` s   |  j  | j ƒ  S(   N(   RÂ   R7   (   R½   t   key(    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyt   __getitem__Ç  s    (   R   R   R¿   R¾   RÈ   (    (    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR   ¶  s   	R;   c           B` s   e  Z d Z \ Z Z Z Z RS(   i    i   i   i   (   i    i   i   i   (   R   R   t   EXPORT_STATUSESR€   R³   R   R<   (    (    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyR;   Ë  s   (3   R¿   t
   __future__R    R   R   R   t   future.builtinsRÀ   RŠ   t   gimpR   Ro   t   export_layersR   R   R   R   R   R	   t   objectR
   t   user_directoryR   R   R+   Ra   R!   R3   R8   R9   R6   R(   R'   Re   Rk   R:   R   R‰   RŒ   RŽ   R   R£   R¯   R°   R±   RF   R,   RG   R¼   R   R;   (    (    (    s>   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/export.pyt   <module>   sX   "
|												-												                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      # -*- coding: utf-8 -*-

"""Placeholder objects replaced with real GIMP objects when calling GIMP PDB
procedures during batch processing.

The following placeholder objects are defined:

* `PLACEHOLDERS['current_image']` - The image currently being processed.

* `PLACEHOLDERS['current_layer']` - The layer currently being processed in the
  current image. This placeholder is used for PDB procedures containing
  `gimp.Layer`, `gimp.Drawable` or `gimp.Item` parameters.

* `PLACEHOLDERS['background_layer']` - The layer positioned immediately after
  the currently processed layer.

* `PLACEHOLDERS['foreground_layer']` - The layer positioned immediately before
  the currently processed layer.
"""

from __future__ import absolute_import, division, print_function, unicode_literals
from future.builtins import *

import collections

import gimpenums

from export_layers import pygimplib as pg

from export_layers import background_foreground
from export_layers.gui import placeholders as gui_placeholders


class _GimpObjectPlaceholder(object):
  
  def __init__(self, display_name, replacement_func):
    self._display_name = display_name
    self._replacement_func = replacement_func
  
  @property
  def display_name(self):
    return self._display_name
  
  def replace_args(self, *args):
    return self._replacement_func(*args)


def _get_current_image(batcher):
  return batcher.current_image


def _get_current_layer(batcher):
  return batcher.current_raw_item


_PLACEHOLDERS = collections.OrderedDict([
  ('current_image', _GimpObjectPlaceholder(_('Current Image'), _get_current_image)),
  ('current_layer', _GimpObjectPlaceholder(_('Current Layer'), _get_current_layer)),
  ('background_layer',
   _GimpObjectPlaceholder(_('Background Layer'), background_foreground.get_background_layer)),
  ('foreground_layer',
   _GimpObjectPlaceholder(_('Foreground Layer'), background_foreground.get_foreground_layer)),
])


def get_replaced_arg(arg, batcher):
  """If `arg` is a placeholder object, returns a real object replacing the
  placeholder. Otherwise, `arg` is returned.
  
  Arguments after `args` are required arguments for actions and are used to
  determine the real object that replaces the placeholder.
  """
  try:
    placeholder = _PLACEHOLDERS[arg]
  except KeyError:
    raise ValueError('invalid placeholder value "{}"'.format(arg))
  else:
    return placeholder.replace_args(batcher)


#===============================================================================


class PlaceholderSetting(pg.setting.Setting):
   
  _ALLOWED_GUI_TYPES = [gui_placeholders.GimpObjectPlaceholdersComboBoxPresenter]
  _ALLOWED_PLACEHOLDERS = []
  
  @classmethod
  def get_allowed_placeholder_names(cls):
    """Returns a list of allowed names of placeholders for this setting class.
    """
    return list(cls._ALLOWED_PLACEHOLDERS)
  
  @classmethod
  def get_allowed_placeholders(cls):
    """Returns a list of allowed placeholder objects for this setting class.
    """
    return [
      placeholder for placeholder_name, placeholder in _PLACEHOLDERS.items()
      if placeholder_name in cls._ALLOWED_PLACEHOLDERS]
  
  def _init_error_messages(self):
    self.error_messages['invalid_value'] = _('Invalid placeholder.')
  
  def _validate(self, value):
    if value not in self._ALLOWED_PLACEHOLDERS:
      raise pg.setting.SettingValueError(
        pg.setting.value_to_str_prefix(value) + self.error_messages['invalid_value'])


class PlaceholderImageSetting(PlaceholderSetting):
  
  _DEFAULT_DEFAULT_VALUE = 'current_image'
  _ALLOWED_PLACEHOLDERS = ['current_image']


class PlaceholderDrawableSetting(PlaceholderSetting):
  
  _DEFAULT_DEFAULT_VALUE = 'current_layer'
  _ALLOWED_PLACEHOLDERS = ['current_layer', 'background_layer', 'foreground_layer']


class PlaceholderLayerSetting(PlaceholderSetting):
  
  _DEFAULT_DEFAULT_VALUE = 'current_layer'
  _ALLOWED_PLACEHOLDERS = ['current_layer', 'background_layer', 'foreground_layer']


class PlaceholderItemSetting(PlaceholderSetting):
  
  _DEFAULT_DEFAULT_VALUE = 'current_layer'
  _ALLOWED_PLACEHOLDERS = ['current_layer', 'background_layer', 'foreground_layer']


PDB_TYPES_TO_PLACEHOLDER_SETTING_TYPES_MAP = {
  gimpenums.PDB_IMAGE: 'placeholder_image',
  gimpenums.PDB_ITEM: 'placeholder_item',
  gimpenums.PDB_DRAWABLE: 'placeholder_drawable',
  gimpenums.PDB_LAYER: 'placeholder_layer',
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                