ó
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
 Z
 d d l Z d d l m Z d e f d „  ƒ  YZ e d	 d
 ƒ Z e d	 d ƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d „  Z d „  Z d d „ Z d S(   u/   Metaclasses for settings and mappings of types.i    (   t   absolute_importt   divisiont   print_functiont   unicode_literals(   t   *Ni   (   t   utilst   _TypeMapc           B` sA   e  Z d d  „ Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C` s.   | |  _  t j ƒ  |  _ t j t ƒ |  _ d  S(   N(   t   _descriptiont   collectionst   OrderedDictt   _name_to_type_mapt   defaultdictt   listt   _type_to_names_map(   t   selft   description(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyt   __init__   s    	c         C` sä   t  | t j ƒ rM y |  j | SWqà t k
 rI t |  j | ƒ ƒ ‚ qà Xn“ t } t  | t t	 f ƒ r” | d } t
 | ƒ d k rš | d } qš n | } | |  j k rÁ t |  j | ƒ ƒ ‚ n  |  j | } | rØ | S| d Sd  S(   Ni    i   (   t
   isinstancet   typest   StringTypesR
   t   KeyErrort	   TypeErrort   _get_error_messaget   FalseR   t   tuplet   lenR   (   R   t   type_or_namet   return_all_namest   type_t   names(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyt   __getitem__   s"    
c         C` s0   t  | t j ƒ r | |  j k S| |  j k Sd  S(   N(   R   R   R   R
   R   (   R   t   key(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyt   __contains__3   s    c         C` s<   y |  j  | SWn& t k
 r7 t |  j | ƒ ƒ ‚ n Xd  S(   N(   R
   R   R   R   (   R   t   name(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyt   __getattr__9   s    c         C` s   | |  j  k S(   N(   R
   (   R   R!   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyt   __hasattr__?   s    c         C` s5   d j  | ƒ } |  j r1 | d j  |  j ƒ 7} n  | S(   Nu   unrecognized type "{}"u   ; are you sure this is a {}?(   t   formatR   (   R   t   valuet   error_message(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyR   B   s    	N(	   t   __name__t
   __module__t   NoneR   R   R    R"   R#   R   (    (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyR      s   				R   u   setting typeu   setting GUI typet   SettingMetac           B` s&   e  Z d  Z d „  Z e d „  ƒ Z RS(   u  Metaclass for the `setting.Setting` class and its subclasses.
  
  The metaclass is responsible for the following:
  
  * Creating a mapping of `Setting` subclasses and human-readable names for
    easier specification of the `'type'` field when creating settings via
    `setting.Group.add()`.
  
  * Tracking names and values of arguments passed to instantiation of a setting.
    The names and values are then passed to `Setting.to_dict()` to allow
    persisting the setting with the arguments it was instantiated with.
  
  * Ensuring that `Setting` classes documented as abstract cannot be initialized
    (`TypeError` is raised on `__init__()`).
  c         C` sR   t  | ƒ t |  | ƒ t t |  ƒ j |  | | | ƒ } t | | | t d ƒ | S(   Nu   Setting(   t   _handle_abstract_attributet   _set_init_wrappert   superR*   t   __new__t   _register_type_and_aliasest   SettingTypes(   t   mclsR!   t   basest	   namespacet   cls(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyR.   _   s
    
!c         ` s"   t  j ˆ  ƒ ‡  f d †  ƒ } | S(   Nc         ` sà   t  |  d t ƒ r0 t d j |  j j ƒ ƒ ‚ n  t |  d ƒ sÌ t | ƒ |  _ t	 j
 ˆ  ƒ d d } x* t | | ƒ D] \ } } | |  j | <qu Wt	 j
 ˆ  ƒ d d  k	 rÌ t d j |  j j ƒ ƒ ‚ qÌ n  ˆ  |  | | Ž d  S(   Nu	   _ABSTRACTu-   cannot initialize abstract setting class "{}"u   _dict_on_initi    i   uZ   __init__ in Setting subclasses cannot accept variable positional arguments (found in "{}")(   t   getattrR   R   R$   t	   __class__R'   t   hasattrt   dictt   _dict_on_initt   inspectt
   getargspect   zipR)   (   R   t   argst   kwargst	   arg_namest   arg_namet   arg(   t	   orig_init(    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyt   init_wrapperm   s    	(   t	   functoolst   wraps(   RB   RC   (    (   RB   sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyt   _get_init_wrapperj   s    (   R'   R(   t   __doc__R.   t   staticmethodRF   (    (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyR*   N   s   	t	   GroupMetac           B` s&   e  Z d  Z d „  Z e d „  ƒ Z RS(   u?  Metaclass for the `setting.Group` class.
  
  The metaclass is responsible for the following:
  
  * Tracking names and values of arguments passed to instantiation of a group.
    The names and values are then passed to `Group.to_dict()` to allow
    persisting the group with the arguments it was instantiated with.
  c         C` s2   t  |  | ƒ t t |  ƒ j |  | | | ƒ } | S(   N(   R,   R-   RI   R.   (   R1   R!   R2   R3   R4   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyR.      s    !c         ` s"   t  j ˆ  ƒ ‡  f d †  ƒ } | S(   Nc         ` s¡   t  |  d ƒ s t | ƒ |  _ t j ˆ  ƒ d d } x* t | | ƒ D] \ } } | |  j | <qE Wt j ˆ  ƒ d d  k	 r t d ƒ ‚ q n  ˆ  |  | | Ž d  S(   Nu   _dict_on_initi    i   u<   Group.__init__() cannot accept variable positional arguments(   R7   R8   R9   R:   R;   R<   R)   R   (   R   R=   R>   R?   R@   RA   (   RB   (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyRC   š   s    (   RD   RE   (   RB   RC   (    (   RB   sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyRF   —   s    (   R'   R(   RG   R.   RH   RF   (    (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyRI   †   s   	t   PresenterMetac           B` s&   e  Z d  Z d „  Z e d „  ƒ Z RS(   u´  Metaclass for the `setting.Presenter` class and its subclasses.
  
  The metaclass is responsible for the following:
  
  * Creating a mapping of `Presenter` subclasses and human-readable names for
    easier specification of the `'gui_type'` field when creating settings via
    `setting.Group.add()`.
  
  * Ensuring that `Presenter` classes documented as abstract cannot be
    initialized (`TypeError` is raised on `__init__()`).
  c         C` sR   t  | ƒ t |  | ƒ t t |  ƒ j |  | | | ƒ } t | | | t d ƒ | S(   Nu	   Presenter(   R+   R,   R-   RJ   R.   R/   t   SettingGuiTypes(   R1   R!   R2   R3   R4   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyR.   º   s
    
!c         ` s"   t  j ˆ  ƒ ‡  f d †  ƒ } | S(   Nc         ` sD   t  |  d t ƒ r0 t d j |  j j ƒ ƒ ‚ n  ˆ  |  | | Ž d  S(   Nu	   _ABSTRACTu/   cannot initialize abstract presenter class "{}"(   R5   R   R   R$   R6   R'   (   R   R=   R>   (   RB   (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyRC   È   s    	(   RD   RE   (   RB   RC   (    (   RB   sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyRF   Å   s    (   R'   R(   RG   R.   RH   RF   (    (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyRJ   ­   s   	c         C` s*   d | k r& |  j  | d ƒ | d <n  d  S(   Nu   __init__(   RF   (   R1   R3   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyR,   Ó   s    c         C` s   d |  k r t  |  d <n  d  S(   Nu	   _ABSTRACT(   R   (   R3   (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyR+   Û   s    c         C` sö   t  j | d ƒ } t | | ƒ } | | j k rÐ |  d sò | | j | <| j | j | ƒ d |  k rÍ x` |  d D]Q } | | j k r— | | j | <n t d j | | ƒ ƒ ‚ | j | j | ƒ qr WqÍ qò n" t d j | j | j | ƒ ƒ ‚ d  S(   Nu   utf-8u	   _ABSTRACTu   _ALIASESu:   alias "{}" matches a {} class name or is already specifiedu7   Setting subclass with the name "{}" already exists ({})(	   t   pgutilst   safe_decodet   _get_human_readable_class_nameR
   R   t   appendR   R$   R'   (   R3   R4   t	   type_namet   type_mapt   base_class_namet   processed_type_namet   human_readable_namet   alias(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyR/   à   s"    
!c         C` sN   |  } | r/ | j  | ƒ r/ | t | ƒ  } n  t j d d | ƒ j ƒ  } | S(   Nu   (?<!^)(?=[A-Z])u   _(   t   endswithR   t   ret   subt   lower(   R!   t   suffix_to_stript   processed_name(    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyRN   ù   s
    (   RG   t
   __future__R    R   R   R   t   future.builtinsR   RD   R:   RW   R   t    R   RL   t   objectR   R0   RK   t   typeR*   RI   RJ   R,   R+   R/   R)   RN   (    (    (    sN   /home/josie/.config/GIMP/2.10/plug-ins/export_layers/pygimplib/setting/meta.pyt   <module>   s$   "
98'&			                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          # -*- coding: utf-8 -*-

"""Class to generate GIMP PDB parameters out of settings and parse GIMP
procedure arguments to assign them as values to settings.
"""

from __future__ import absolute_import, division, print_function, unicode_literals
from future.builtins import *

from . import group as group_
from . import settings as settings_

__all__ = [
  'create_params',
  'iter_args',
  'list_param_values',
]


def create_params(*settings_or_groups):
  """
  Return a list of GIMP PDB parameters from the specified `Setting` or `Group`
  instances.
  """
  settings = _list_settings(settings_or_groups)
  
  params = []
  
  for setting in settings:
    if setting.can_be_registered_to_pdb():
      params.extend(setting.get_pdb_param())
  
  return params


def iter_args(args, settings):
  """
  Iterate over arguments (`args`) passed to a GIMP PDB procedure.
  
  `settings` is a list of `Setting` instances that may modify the iteration. For
  example, if an argument is matched by a setting of type `ArraySetting`, the
  array argument causes the preceding argument to be skipped. The preceding
  argument is the array length and does not need to exist as a separate setting
  because the length can be obtained from the array itself in Python.
  
  If there are more settings than non-skipped arguments, the remaining settings
  will be ignored.
  """
  indexes_of_array_length_settings = set()
  index = 0
  
  for setting in settings:
    if isinstance(setting, settings_.ArraySetting):
      index += 1
      indexes_of_array_length_settings.add(index - 1)
    
    index += 1
  
  for arg_index in range(min(len(args), index)):
    if arg_index not in indexes_of_array_length_settings:
      yield args[arg_index]


def list_param_values(settings_or_groups, ignore_run_mode=True):
  """
  Return a list of values of settings registrable to PDB.
  
  If `ignore_run_mode` is `True`, ignore setting(s) named `'run_mode'`. This
  makes it possible to call PDB functions with the setting values without
  manually omitting the `'run_mode'` setting.
  """
  settings = _list_settings(settings_or_groups)
  
  if ignore_run_mode:
    for i, setting in enumerate(settings):
      if setting.name == 'run_mode':
        del settings[i]
        break
  
  return [setting.value for setting in settings if setting.can_be_registered_to_pdb()]


def _list_settings(settings_or_groups):
  settings = []
  for setting_or_group in settings_or_groups:
    if isinstance(setting_or_group, settings_.Setting):
      settings.append(setting_or_group)
    elif isinstance(setting_or_group, group_.Group):
      settings.extend(setting_or_group.walk())
    else:
      raise TypeError(
        '{} is not an object of type {} or {}'.format(
          setting_or_group, settings_.Setting, group_.Group))
  
  return settings
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 