
ddc           @` 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
 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 d	 l m Z d d
 l m Z d d l m Z d d l m Z d Z d Z d   Z d e f d     YZ d S(   u@   Batch-processing layers and exporting layers as separate images.i    (   t   absolute_importt   divisiont   print_functiont   unicode_literals(   t   *N(   t   pdb(   t	   pygimplib(   t   actions(   t   builtin_constraints(   t   builtin_procedures(   t
   exceptions(   t   export(   t   placeholdersu   namec         ` s"   t  j      f d    } | S(   Nc         ` s  t  |  d j   j     t j    } | j t | j  t | j   } y | j d  Wn t	 k
 rr n Xi  } x' t
 | |  D] \ } } | | | <q W| j t | j  t | j  } x* t
 | | j  D] \ }	 }
 |
 | |	 <q W| j |  |  j |     |  |  d  S(   Nu   _orig_{}u   self(   t   setattrt   formatt   __name__t   inspectt
   getargspect   argst   lent   defaultst   removet
   ValueErrort   zipt   updatet   _init_attributes(   t   selfR   t   kwargst   argspect	   arg_namest   full_kwargst   arg_namet	   arg_valuet   kwarg_namest
   kwarg_namet   kwarg_value(   t   func(    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   func_wrapper    s     ##(   t	   functoolst   wraps(   R$   R%   (    (   R$   s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   _set_attributes_on_init   s    t   Batcherc           B` sh  e  Z d  Z e e e j d  d d e j j	 j
 dD dD dD e e e e dD dD dD d   Z e d    Z e d    Z e d    Z e d    Z e d	    Z e d
    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z  e d    Z! e d    Z" e d    Z# e# j$ d    Z# e d    Z% e d    Z& e d    Z' e d    Z( e d    Z) e d     Z* e d!    Z+ e d"    Z, e d#    Z- e d$  Z. d%   Z/ d&   Z0 d'   Z1 d(   Z2 d)   Z3 dD dD d*  Z4 d+   Z5 d,   Z6 d-   Z7 d.   Z8 d/   Z9 d0   Z: d1   Z; dD d d2  Z< d3   Z= d4   Z> d5   Z? d6   Z@ d7   ZA d8   ZB d9   ZC d:   ZD d;   ZE d<   ZF e d=  ZG eH d>    ZI d?   ZJ d@   ZK dA   ZL dB   ZM dC   ZN RS(E   uw   Class for batch-processing layers in the specified image with a sequence of
  actions (resize, rename, export, ...).
  i   u    u   pngc         C` s   d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ g  |  _ t j	 t
  |  _ t j	 t
  |  _ t j	 t
  |  _ t j	 t
  |  _ t |  _ d  |  _ t j j   |  _ d  S(   N(   t   Nonet   _current_itemt   _current_raw_itemt   _current_proceduret   _current_constraintt   _current_imaget   _orig_active_layert   _exported_raw_itemst   collectionst   defaultdictt   listt   _skipped_procedurest   _skipped_constraintst   _failed_procedurest   _failed_constraintst   Falset   _should_stopt   _invokert   pgt   invokert   Invokert   _initial_invoker(   R   t   initial_run_modet   input_imaget
   procedurest   constraintst	   edit_modet   output_directoryt   layer_filename_patternt   file_extensiont   overwrite_modet   overwrite_choosert   progress_updatert	   item_treet
   is_previewt   process_contentst   process_namest   process_exportt   export_context_managert   export_context_manager_argst   export_context_manager_kwargs(    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   __init__D   s    									c         C` s   |  j  S(   u  The run mode to use for the first layer.
    
    For subsequent layers, `gimpenums.RUN_WITH_LAST_VALS` is used.
    This usually has effect when exporting layers - if `initial_run_mode` is
    `gimpenums.RUN_INTERACTIVE`, a native file format GUI is displayed for the
    first layer and the same settings are then applied to subsequent layers.
    If the file format in which the layer is exported to cannot handle
    `gimpenums.RUN_WITH_LAST_VALS`, `gimpenums.RUN_INTERACTIVE` is forced.
    (   t   _initial_run_mode(   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR@   m   s    c         C` s   |  j  S(   u)   Input `gimp.Image` to process layers for.(   t   _input_image(   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRA   z   s    c         C` s   |  j  S(   um   If `True`, batch-edit layers directly in `input_image`. If `False`,
    batch-process and export layers.
    (   t
   _edit_mode(   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRD      s    c         C` s   |  j  S(   u/   `setting.Group` instance containing procedures.(   t   _procedures(   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRB      s    c         C` s   |  j  S(   u1   `setting.Group` instance containing constraints..(   t   _constraints(   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRC      s    c         C` s   |  j  S(   u1   Output directory path to save exported layers to.(   t   _output_directory(   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRE      s    c         C` s   |  j  S(   u+   Filename pattern for layers to be exported.(   t   _layer_filename_pattern(   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRF      s    c         C` s   |  j  S(   u-   Filename extension for layers to be exported.(   t   _file_extension(   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRG      s    c         C` s   |  j  S(   um   One of the `pygimplib.overwrite.OverwriteModes` values indicating how to
    handle files with the same name.(   t   _overwrite_mode(   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRH      s    c         C` s   |  j  S(   u   `pygimplib.overwrite.OverwriteChooser` instance that is invoked during
    export if a file with the same name already exists.
    
    If `None`, then `pygimplib.overwrite.NoninteractiveOverwriteChooser` is
    used.
    (   t   _overwrite_chooser(   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRI      s    c         C` s   |  j  S(   u   `pygimplib.progres.ProgressUpdater` instance indicating the number of
    layers processed so far.
    
    If no progress update is desired, pass `None`.
    (   t   _progress_updater(   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRJ      s    c         C` s   |  j  S(   u  `pygimplib.itemtree.ItemTree` instance containing layers to be processed.
    
    If `None` (the default), an item tree is automatically created at the start
    of processing. If `item_tree` has filters (constraints) set, they will be
    reset on each call to `run()`.
    (   t
   _item_tree(   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRK      s    c         C` s   |  j  S(   u   If `True`, only procedures and constraints that are marked as
    "enabled for previews" will be applied for previews. If `False`, this
    property has no effect (and effectively allows performing real processing).
    (   t   _is_preview(   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRL      s    c         C` s   |  j  S(   u   If `True`, invoke procedures on layers.
    
    Setting this to `False` is useful if you require only layer names to be
    processed.
    (   t   _process_contents(   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRM      s    c         C` s   |  j  S(   u,  If `True`, process layer names before export to be suitable to save to
    disk (in particular to remove characters invalid for a file system).
    
    If `is_preview` is `True` and `process_names` is `True`, also invoke
    built-in procedures modifying item names only (e.g. renaming layers).
    (   t   _process_names(   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRN      s    c         C` s   |  j  S(   u   If `True`, perform export of layers.
    
    Setting this to `False` is useful to preview the processed contents of a
    layer without saving it to a file.
    (   t   _process_export(   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRO      s    c         C` s   |  j  S(   u   Context manager that wraps exporting a single layer.
    
    This can be used to perform GUI updates before and after export.
    Required parameters: current run mode, current image, layer to export,
    output filename of the layer.
    (   t   _export_context_manager(   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRP      s    c         C` s   |  j  S(   uC   Additional positional arguments passed to `export_context_manager`.(   t   _export_context_manager_args(   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRQ      s    c         C` s   |  j  S(   u@   Additional keyword arguments passed to `export_context_manager`.(   t   _export_context_manager_kwargs(   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRR      s    c         C` s   |  j  S(   u?   A `pygimplib.itemtree.Item` instance currently being processed.(   R+   (   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   current_item   s    c         C` s   |  j  S(   u2   Raw item (`gimp.Layer`) currently being processed.(   R,   (   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   current_raw_item   s    c         C` s   | |  _  d  S(   N(   R,   (   R   t   value(    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRh     s    c         C` s   |  j  S(   u8   The procedure currently being applied to `current_item`.(   R-   (   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   current_procedure  s    c         C` s   |  j  S(   u5   The most recent (last) constraint that was evaluated.(   R.   (   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   current_constraint  s    c         C` s   |  j  S(   u   The current `gimp.Image` containing layer(s) being processed.
    
    If `edit_mode` is `True`, this is equivalent to `input_image`.
    
    If `edit_mode` is `False`, this is a copy of `input_image` to avoid
    modifying original layers.
    (   R/   (   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   current_image  s    	c         C` s   t  |  j  S(   u   List of layers that were successfully exported.
    
    Does not include layers skipped by the user (when files with the same names
    already exist).
    (   R4   R1   (   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   exported_raw_items  s    c         C` s   t  |  j  S(   u   Procedures that were skipped during processing.
    
    A skipped procedure was not applied to one or more items and causes no
    adverse effects further during processing.
    (   t   dictR5   (   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   skipped_procedures&  s    c         C` s   t  |  j  S(   u   Constraints that were skipped during processing.
    
    A skipped constraint was not evaluated for one or more items and causes no
    adverse effects further during processing.
    (   Rn   R6   (   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   skipped_constraints/  s    c         C` s   t  |  j  S(   u   Procedures that caused an error during processing.
    
    Failed procedures indicate a problem with the procedure parameters or
    potentially a bug in the plug-in.
    (   Rn   R7   (   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   failed_procedures8  s    c         C` s   t  |  j  S(   u   Constraints that caused an error during processing.
    
    Failed constraints indicate a problem with the constraint parameters or
    potentially a bug in the plug-in.
    (   Rn   R8   (   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   failed_constraintsA  s    c         C` s   |  j  S(   u   `pygimplib.invoker.Invoker` instance to manage procedures and constraints
    applied on layers.
    
    This property is reset on each call of `run()`.
    (   R;   (   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR=   J  s    c         K` s   |  j  |   |  j |  j |  t } |  j r< |  j   n  z/ y |  j   Wn t k
 ri t }   n XWd |  j r |  j	 |  n  X|  j r |  j
 r |  j Sd Sd S(   u  Batch-processes and exports layers as separate images.
    
    A copy of the image and the layers to be processed are created so that the
    original image and its soon-to-be processed layers are left intact. The
    image copy is automatically destroyed once processing is done. To keep the
    image copy, pass `True` to `keep_image_copy`. In that case, this method
    returns the image copy. If an exception was raised or if no layer was
    exported, this method returns `None` and the image copy will be destroyed.
    
    `**kwargs` can contain arguments that can be passed to `Batcher.__init__()`.
    Arguments in `**kwargs` overwrite the corresponding `Batcher` properties.
    See the properties for details.
    N(   R   t   _prepare_for_processingR_   R9   Ra   t   _setup_contentst   _process_itemst	   Exceptiont   Truet   _cleanup_contentst   _keep_image_copyt   _image_copyR*   (   R   t   keep_image_copyR   t   exception_occurred(    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   runS  s     	 	c         C` s   t  |  _ d S(   u}   Terminates batch processing prematurely.
    
    The termination occurs after the current item is processed completely.
    N(   Rw   R:   (   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   stopv  s    c         O` s   |  j  j | |   S(   u	  Adds a procedure to be applied during `run()`.
    
    The signature is the same as for `pygimplib.invoker.Invoker.add()`.
    
    Procedures added by this method are placed before procedures added by
    `actions.add()`.
    
    Procedures are added immediately before the start of processing. Thus,
    calling this method during processing will have no effect.
    
    Unlike `actions.add()`, procedures added by this method do not act as
    settings, i.e. they are merely functions without GUI, are not saved
    persistently and are always enabled.
    
    This class recognizes several action groups that are invoked at certain
    places when `run()` is called:
    * `'before_process_items'` - invoked before starting processing the first
      item. Only one argument is accepted - instance of this class.
    * `'before_process_items_contents'` - same as `'before_process_items'`, but
      applied only if `process_contents` is `True`.
    * `'after_process_items'` - invoked after finishing processing the last
      item. Only one argument is accepted - instance of this class.
    * `'after_process_items_contents'` - same as `'after_process_items'`, but
      applied only if `process_contents` is `True`.
    * `'before_process_item'` - invoked immediately before applying procedures
      on the layer.
      Three arguments are accepted:
        * instance of this class
        * the current `pygimplib.itemtree.Item` to be processed
        * the current GIMP item to be processed
    * `'before_process_item_contents'` - same as `'before_process_item'`, but
      applied only if `process_contents` is `True`.
    * `'after_process_item'` - invoked immediately after all procedures have
      been applied to the layer.
      Three arguments are accepted:
        * instance of this class
        * the current `pygimplib.itemtree.Item` that has been processed
        * the current GIMP item that has been processed
    * `'after_process_item_contents'` - same as `'after_process_item'`, but
      applied only if `process_contents` is `True`.
    * `'cleanup_contents'` - invoked after processing is finished and cleanup is
      commenced (e.g. removing temporary internal images). Use this if you
      create temporary images or items of your own. While you may also achieve
      the same effect with `'after_process_items_contents'`, using
      `'cleanup_contents'` is safer as it is also invoked when an exception is
      raised. Only one argument is accepted - instance of this class.
    (   R?   t   add(   R   R   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   add_procedure}  s    0c         O` s   |  j  j |  j |  | |  S(   u   Adds a constraint to be applied during `run()`.
    
    The first argument is the function to act as a filter (returning `True` or
    `False`). The rest of the signature is the same as for
    `pygimplib.invoker.Invoker.add()`.
    
    For more information, see `add_procedure()`.
    (   R?   R   t   _get_constraint_func(   R   R$   R   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   add_constraint  s    	c         O` s   |  j  j | |   d S(   u   Removes an action originally scheduled to be applied during `run()`.
    
    The signature is the same as for `pygimplib.invoker.Invoker.remove()`.
    N(   R?   R   (   R   R   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   remove_action  s    c         O` s   |  j  j | |   d S(   u   Reorders an action to be applied during `run()`.
    
    The signature is the same as for `pygimplib.invoker.Invoker.reorder()`.
    N(   R?   t   reorder(   R   R   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   reorder_action  s    c         ` s]    d j  d  r d   j k r9 t j   d j } qd   j k r_ t j   d j } qd j   j  } t	 j
 |   d d   n&  d j  d  r~t j   d j  r t t j j   d j  } q  d	 j rwd
 j   d j  } d   j k r*|  j   j j d | d f  n  d   j k r\|  j   j j d | d f  n  t	 j
 |   d d   qd Sn4 d j   d j   j  } t	 j
 |   d d   | d k rd S| d k	 rt   f d   | D  rd S|  j    } |  j |    } | d k r,  d j } n  t   d  | g } |  j j | | |  d S(   u  Adds an action and wraps/processes the action's function according to the
    action's settings.
    
    For PDB procedures, the function name is converted to a proper function
    object. For constraints, the function is wrapped to act as a proper filter
    rule for `item_tree.filter`. Any placeholder objects (e.g. "current image")
    as function arguments are replaced with real objects during processing of
    each item.
    
    If `tags` is not `None`, the action will not be added if it does not contain
    any of the specified tags.
    
    If `action_groups` is not `None`, the action will be added to the specified
    action groups instead of the groups defined in `action['action_groups']`.
    u   originu   builtinu	   procedureu	   orig_nameu
   constraintuF   invalid action "{}" - must contain "procedure" or "constraint" in tagsu   gimp_pdbu   functionu   enabledu   PDB procedure "{}" not foundNu!   invalid origin {} for action "{}"c         3` s   |  ] } |   j  k Vq d  S(   N(   t   tags(   t   .0t   tag(   t   action(    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pys	   <genexpr>  s    u   action_groupsu	   arguments(   t   is_itemR   R	   t   BUILTIN_PROCEDURES_FUNCTIONSRi   R   t   BUILTIN_CONSTRAINTS_FUNCTIONSR   t   nameR
   t   ActionErrorR*   R   t   gimp_procedural_db_proc_existsR<   t   utilst   safe_encode_gimpR7   t   appendR8   t   anyt   _get_processed_functiont   _handle_exceptions_from_actionR4   R;   R   (   R   R   R   t   action_groupst   functiont   messaget   processed_functiont   invoker_args(    (   R   s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   _add_action_from_settings  s>     ##)c         ` s      f d   } | S(   Nc          ` s   |  d  |  d } }  j     s( t S j    | }  j   | |  \ } } d   j k r  j |    }  j | |   d j  } n  | | |   S(   Niu
   constraintu	   orig_name(   t   _is_enabledR9   t%   _set_current_procedure_and_constraintt   _get_action_args_and_kwargsR   t    _set_apply_constraint_to_foldersR   Ri   (   t   action_args_and_functiont   action_argsR   t   orig_functionR   R   (   R   R   (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   _function_wrapper  s    (    (   R   R   R   (    (   R   R   s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR     s    c         C` s?   |  j  r* | d j o  | d j s; t Sn | d j s; t St S(   Nu   enabledu   enabled_for_previews(   R`   Ri   R9   Rw   (   R   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR     s    	c         C` s:   d | j  k r | |  _ n  d | j  k r6 | |  _ n  d  S(   Nu	   procedureu
   constraint(   R   R-   R.   (   R   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR   %  s    c         C` so   |  j  |  } i  } | d j d  re | j t  |  j |  re i | d d 6} | d } qe n  | | f S(   Nu   originu   gimp_pdbi    t   run_modei   (   t   _get_replaced_argsR   t   popt    _BATCHER_ARG_POSITION_IN_ACTIONSt   _has_run_mode_param(   R   R   R   R   R   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR   ,  s    c         C` s   g  } xt | D]l } t  | t j  rD | j t j | j |    q t  | t j j  rl | j | j  q | j |  q W| S(   uZ   Returns a list of action arguments, replacing any placeholder values with
    real values.(	   t
   isinstanceR   t   PlaceholderSettingR   t   get_replaced_argRi   R<   t   settingt   Setting(   R   t   action_argumentst   replaced_argst   argument(    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR   9  s    c         C` s   | j  o | j  d d d k S(   Ni    i   u   run-mode(   t   params(   R   t   pdb_procedure(    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR   I  s    c         ` s(   | d j  r    f d   } | S  Sd  S(   Nu   also_apply_to_parent_foldersc          ` sb   |  d } t  } xK | g | j d  d  d  D], } | oM   | |  d |  } | s. Pq. q. W| S(   Ni    ii   (   Rw   t   parents(   R   t   action_kwargst   itemt   resultt   item_or_parent(   R   (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR   O  s    
$(   Ri   (   R   R   R   R   (    (   R   s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR   L  s    
c         ` s        f d   } | S(   Nc          ` sG    j   d  k	 r  n   |   }  j j j   | | d  d  S(   NR   (   t   _get_args_for_constraint_funcR*   R_   t   filterR   (   R   R   t	   func_args(   R$   R   t	   orig_funcR   (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR   _  s    	(    (   R   R$   R   R   R   (    (   R$   R   R   R   s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR   ]  s    c         C` s   y t  j |  j j d  } Wn t k
 r8 d  } n X| d  k	 rN | } n7 t |  d k ri t } n d } | |  | | d } | S(   Nu   batcheri   i    (   R   R   R   t   indexR   R*   R   R   (   R   R$   R   t   batcher_arg_positionR   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR   h  s    
		c         ` s       f d   } | S(   Nc          ` sS  y  |  |   SWn;t  j k
 r } d   j k r^  j   j j  j t |  f  n  d   j k rO j   j j  j t |  f  qOn t	 k
 rN} t
 j   } d   j k r  j   j j  j t |  | f  n  d   j k r* j   j j  j t |  | f  n  t  j t |     j |   n Xd  S(   Nu	   procedureu
   constraint(   R
   t
   SkipActionR   R5   R   R   R+   t   strR6   Rv   t	   tracebackt
   format_excR7   R8   R   (   R   R   t   et   trace(   R   R   R   (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   _handle_exceptions{  s    ),,,(    (   R   R   R   R   (    (   R   R   R   s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR   z  s    c         K` s2  t  t j |  j  j  } | j d  x[ | j   D]M \ } } | | k rd t |  d | |  q5 t d j	 | t
 |     q5 W|  j d  k r t j j |  j  |  _ n  |  j d  k r t j j d   |  _ n  |  j d  k r t j j |  _ n  |  j d  k rd |  _ n  |  j d  k r.i  |  _ n  d  S(   Nu   selfu   _u4   invalid argument "{}" encountered; must be one of {}(    (   t   setR   R   t   _orig___init__R   t   discardt   itemsR   R   R   R4   R]   R*   R<   t	   overwritet   NoninteractiveOverwriteChooserR\   R^   t   progresst   ProgressUpdaterRd   R   t   EmptyContextRe   Rf   (   R   R   t   init_argspec_namesR   Ri   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR     s&    	c         C` s>  | d  k	 r | |  _ n$ t j j |  j d t j j |  _ |  j j rX |  j j	   n  | |  _
 d  |  _ d  |  _ d  |  _ d  |  _ |  j |  _ d  |  _ d  |  _ t |  _ g  |  _ t j t  |  _ t j t  |  _ t j t  |  _ t j t  |  _ t j j   |  _ |  j   |  j    |  j!   |  j" j#   d  S(   NR   ($   R*   R_   R<   t   itemtreet	   LayerTreeRU   t   configt   SOURCE_NAMER   t   reset_filterRy   R+   R,   R-   R.   R/   Rz   R0   R9   R:   R1   R2   R3   R4   R5   R6   R7   R8   R=   R>   R;   t   _add_actionst   _add_name_only_actionst   _set_constraintsR^   t   reset(   R   RK   R{   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRs     s0    $									


c         C` s1  |  j  j t j t j g  |  j  j t j t j g d t |  j  j t j t j g d t |  j	 r |  j  j t j
 t j g d t n  |  j  j |  j |  j j d t   |  j t j g  x' t j |  j  D] } |  j |  q W|  j t j g  x' t j |  j  D] } |  j |  qWd  S(   Nt   foreacht   include_empty_groups(   R;   R   R	   t   set_active_and_current_layerR   t   DEFAULT_PROCEDURES_GROUPt)   set_active_and_current_layer_after_actionRw   t    sync_item_name_and_raw_item_nameRV   t   preserve_locks_between_actionsR?   t   list_groupst   _add_default_rename_proceduret   walkRW   R   t   _add_default_export_procedureRX   (   R   t	   proceduret
   constraint(    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR     s0    					
	c         C` s   |  j  t g  x6 t j |  j  D]" } |  j | t j g t g  q# W|  j t g  x6 t j |  j	  D]" } |  j | t j g t g  ql Wd  S(   N(
   R   t   _NAME_ONLY_ACTION_GROUPR   R   RW   R   R	   t   NAME_ONLY_TAGR   RX   (   R   R   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR     s    c         C` sY   |  j  rU t d   t j |  j  D  rU |  j j t j d | d |  j	 g n  d  S(   Nc         s` s/   |  ]% } | d  j  d k o& | d j  Vq d S(   u	   orig_nameu   renameu   enabledN(   Ri   (   R   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pys	   <genexpr>  s   t   groupsR   (
   RV   R   R   R   RW   R;   R   R	   t   rename_layerRZ   (   R   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR     s    
		c         C` sh   |  j  rd t d   t j |  j  D  rd |  j j t j d | d |  j	 |  j
 t j j g n  d  S(   Nc         s` s/   |  ]% } | d  j  d k o& | d j  Vq d S(   u	   orig_nameu   exportu   enabledN(   Ri   (   R   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pys	   <genexpr>  s   R   R   (   RV   R   R   R   RW   R;   R   t   export_R   RY   R[   t   ExportModest
   EACH_LAYER(   R   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR   	  s    
		c         C` s&   |  j  j t j g |  g d t d  S(   Nt   additional_args_position(   R;   t   invokeR   t   DEFAULT_CONSTRAINTS_GROUPR   (   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR     s    			c         C` s   t  j   |  j s |  j rx t j j |  j  |  _ |  j |  _	 t  j
 |  j	  t j j r t  j |  j	  |  _ q n |  j |  _	 t  j |  j	  |  j	 j |  _ d  S(   N(   R   t   gimp_context_pushRV   R`   R<   t   pdbutilst   create_image_from_metadataRU   Rz   R/   t   gimp_image_undo_freezeR   t   DEBUG_IMAGE_PROCESSINGt   gimp_display_newt   _display_idt   gimp_image_undo_group_startt   active_layerR0   (   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRt     s    
c         C` s  |  j  j d g |  g d t |  j s2 |  j r |  j |  j |  j  t j	 |  j  t
 j j rw t j |  j  n  |  j s | r t
 j j |  j  q n> t j |  j  r |  j |  j _ n  t j |  j  t j   t j   d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  S(   Nu   cleanup_contentsR   (   R;   R   R   RV   R`   t   _copy_non_modifying_parasitesR/   RU   R   t   gimp_image_undo_thawR<   R   R   t   gimp_display_deleteR   Ry   R   t   try_delete_imaget   gimp_item_is_validR0   R   t   gimp_image_undo_group_endt   gimp_displays_flusht   gimp_context_popR*   R+   R,   R-   R.   (   R   R|   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRx   *  s*    		

				c         C` sp   t  j |   \ } } xT | D]L } | j |  d  k r |  j |  } | j d k rh | j |  qh q q Wd  S(   Ni    (   R   t   gimp_image_get_parasite_listt   parasite_findR*   t   flagst   parasite_attach(   t	   src_imaget
   dest_imaget   unused_t   parasite_namest   parasite_namet   parasite(    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR   H  s    c         C` s  t  |  j  |  j _ |  j j d g |  g d t |  j r_ |  j j d g |  g d t n  xg |  j D]\ } |  j r t	 j
 d   n  |  j r |  j j t d  j | j   n  |  j |  qi W|  j r |  j j d g |  g d t n  |  j j d g |  g d t d  S(   Nu   before_process_itemsR   u   before_process_items_contentsu   stopped by useru   Processing "{}"u   after_process_items_contentsu   after_process_items(   R   R_   R^   t   num_total_tasksR;   R   R   Ra   R:   R
   t   BatcherCancelErrorRV   t   update_textt   _R   t	   orig_namet   _process_item(   R   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyRu   S  s2    					
		%			
		c         C` st   | |  _  | j |  _ |  j r4 |  j r4 |  j   n  |  j rc |  j | |  j  |  j |  j  n  |  j	 j
   d  S(   N(   R+   t   rawR,   R`   Rb   t$   _process_item_with_name_only_actionsRa   t   _process_item_with_actionst   _refresh_current_imageR^   t   update_tasks(   R   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR  u  s    		c         C` sy   |  j  j d g |  |  j |  j g d t |  j  j t g |  g d t |  j  j d g |  |  j |  j g d t d  S(   Nu   before_process_itemR   u   after_process_item(   R;   R   R+   R,   R   R   (   R   (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR    s    				c         C` s  |  j  s |  j ra t j j | |  j d  t |  j j  t	 t	 t	  } | |  _
 | j |  j
 _ n  |  j  r |  j r t j |  r t j j | |  j | j t j |  j |  d t	 t	 t	  } | |  _
 | j |  j
 _ n  |  j j d g |  |  j |  j
 g d t |  j r=|  j j d g |  |  j |  j
 g d t n  |  j j t j g |  g d t |  j r|  j j d g |  |  j |  j
 g d t n  |  j j d g |  |  j |  j
 g d t d  S(   Ni   u   before_process_itemR   u   before_process_item_contentsu   after_process_item_contentsu   after_process_item(   RV   R`   R<   R   t   copy_and_paste_layerR/   R*   R   t   layersRw   R,   R   R   t   gimp_item_is_groupt   parentt   gimp_image_get_item_positionR;   R   R+   R   Ra   R   R   (   R   R   t   raw_itemt   raw_item_copy(    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR    sD    	*	"					
					
	c         C` sE   |  j  rA |  j rA x* |  j j D] } t j |  j |  q! Wn  d  S(   N(   RV   Ry   R/   R  R   t   gimp_image_remove_layer(   R   R  t   layer(    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR    s    N(O   R   t
   __module__t   __doc__R(   R9   t   gimpt   user_directoryR<   R   t   OverwriteModest   SKIPR*   Rw   RS   t   propertyR@   RA   RD   RB   RC   RE   RF   RG   RH   RI   RJ   RK   RL   RM   RN   RO   RP   RQ   RR   Rg   Rh   t   setterRj   Rk   Rl   Rm   Ro   Rp   Rq   Rr   R=   R}   R~   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   Rs   R   R   R   R   R   Rt   Rx   t   staticmethodR   Ru   R  R  R  R  (    (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyR)   ?   s   
	
	
	
						#		2			=		
									$	"		
	
			"			.(   R  t
   __future__R    R   R   R   t   future.builtinsR2   R&   R   R   R   R   t   export_layersR   R<   R   R   R	   R
   R   R   R   R   R   R(   t   objectR)   (    (    (    s?   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/batcher.pyt   <module>   s&   "
	!                                                                                                                                                                                                                                                                                                                                                                                                                                                                         # -*- coding: utf-8 -*-

"""Built-in plug-in constraints."""

from __future__ import absolute_import, division, print_function, unicode_literals
from future.builtins import *

import collections

from gimp import pdb

from export_layers import pygimplib as pg


def is_layer(item):
  return item.type == pg.itemtree.TYPE_ITEM


def is_nonempty_group(item):
  return item.type == pg.itemtree.TYPE_GROUP and pdb.gimp_item_get_children(item.raw)[1]


def has_matching_file_extension(item, file_extension):
  return pg.path.get_file_extension(item.name).lower() == file_extension.lower()


def has_matching_default_file_extension(item, batcher):
  return pg.path.get_file_extension(item.name).lower() == batcher.file_extension.lower()


def is_item_in_selected_items(item, selected_items):
  return item.raw.ID in selected_items


def is_top_level(item):
  return item.depth == 0


def is_visible(item):
  return item.raw.visible


def has_tags(item, tags=None):
  if tags:
    return any(tag for tag in tags if tag in item.tags)
  else:
    return bool(item.tags)


def has_no_tags(item, tags=None):
  return not has_tags(item, tags)


_BUILTIN_CONSTRAINTS_LIST = [
  {
    'name': 'layers',
    'type': 'constraint',
    'function': is_layer,
    'display_name': _('Layers'),
  },
  {
    'name': 'layer_groups',
    'type': 'constraint',
    'function': is_nonempty_group,
    'display_name': _('Layer groups'),
  },
  {
    'name': 'matching_file_extension',
    'type': 'constraint',
    'function': has_matching_default_file_extension,
    # FOR TRANSLATORS: Think of "Only layers matching file extension" when translating this
    'display_name': _('Matching file extension'),
  },
  {
    'name': 'selected_in_preview',
    'type': 'constraint',
    'function': is_item_in_selected_items,
    # FOR TRANSLATORS: Think of "Only layers selected in preview" when translating this
    'display_name': _('Selected in preview'),
    'arguments': [
      {
        'type': 'set',
        'name': 'selected_layers',
        'display_name': _('Selected layers'),
        'gui_type': None,
      },
    ],
  },
  {
    'name': 'top_level',
    'type': 'constraint',
    'function': is_top_level,
    # FOR TRANSLATORS: Think of "Only top-level layers" when translating this
    'display_name': _('Top-level'),
  },
  {
    'name': 'visible',
    'type': 'constraint',
    'function': is_visible,
    # FOR TRANSLATORS: Think of "Only visible layers" when translating this
    'display_name': _('Visible'),
  },
  {
    'name': 'with_tags',
    'type': 'constraint',
    'function': has_tags,
    # FOR TRANSLATORS: Think of "Only layers with tags" when translating this
    'display_name': _('With tags'),
    'arguments': [
      {
        'type': 'array',
        'name': 'tags',
        'display_name': _('Tags'),
        'element_type': 'string',
        'default_value': (),
      },
    ],
  },
  {
    'name': 'without_tags',
    'type': 'constraint',
    'function': has_no_tags,
    # FOR TRANSLATORS: Think of "Only layers without tags" when translating this
    'display_name': _('Without tags'),
    'arguments': [
      {
        'type': 'array',
        'name': 'tags',
        'display_name': _('Tags'),
        'element_type': 'string',
        'default_value': (),
      },
    ],
  },
]

# Create a separate dictionary for functions since objects cannot be saved
# to a persistent source. Saving them as strings would not be reliable as
# function names and paths may change when refactoring or adding/modifying features.
# The 'function' setting is set to an empty value as the function can be inferred
# via the action's 'orig_name' setting.
BUILTIN_CONSTRAINTS = collections.OrderedDict()
BUILTIN_CONSTRAINTS_FUNCTIONS = collections.OrderedDict()

for action_dict in _BUILTIN_CONSTRAINTS_LIST:
  function = action_dict['function']
  action_dict['function'] = ''
  
  BUILTIN_CONSTRAINTS[action_dict['name']] = action_dict
  BUILTIN_CONSTRAINTS_FUNCTIONS[action_dict['name']] = function
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         