# -*- coding: utf8 - *-
"""
    tmuxp.config
    ~~~~~~~~~~~~

    tmuxp helps you manage tmux workspaces.

    :copyright: Copyright 2013 Tony Narlock.
    :license: BSD, see LICENSE for details
"""


def _expand_shell_command(c):
    '''any config section, session, window, pane that can
    contain the 'shell_command' value
    '''
    if ('shell_command' in c and
            isinstance(c['shell_command'], basestring)):
            c['shell_command'] = [c['shell_command']]

    if ('shell_command_before' in c and
            isinstance(c['shell_command_before'], basestring)):
            c['shell_command_before'] = [c['shell_command_before']]

    return c


def expand_config(config):
    '''Expand configuration into full form. Enables shorthand forms for tmuxp
    config.

    This is necessary to keep the code in the :class:`Builder` clean and also
    allow for neat, short-hand configurations.

    As a simple example, internally, tmuxp expects that config options
    like ``shell_command`` are a list (array)::

        'shell_command': ['htop']

    tmuxp configs allow for it to be simply a string::

        'shell_command': 'htop'

    Kaptan will load JSON/YAML/INI files into python dicts for you.
    :param config: the configuration for the session
    :type config: dict

    iterate through session, windows, and panes for ``shell_command``, if
    it is a string, turn to list.

    shell_command: 'string' => shell_command: list('string')

    :param config: the session configuration
    :type config: dict
    '''

    config = _expand_shell_command(config)
    for window in config['windows']:
        window = _expand_shell_command(window)
        window['panes'] = [_expand_shell_command(
            pane) for pane in window['panes']]

    return config


def trickledown_config(config):
    '''Trickle down / inherit config values

    This will only work if config has been expand with ConfigExpand()

    tmuxp allows certain commands to be default at the session, window
    level. shell_command_before trickles down and prepends the
    ``shell_command`` for the pane.

    :param config: the session configuration
    :type config: dict
    '''

    '''
    prepends a pane's ``shell_command`` list with the window and sessions'
    ``shell_command_before``.
    '''

    for windowconfig in config['windows']:
        for paneconfig in windowconfig['panes']:
            commands_before = config['shell_command_before'] if 'shell_command_before' in config else []
            commands_before.extend(windowconfig['shell_command_before']) if 'shell_command_before' in windowconfig else None
            commands_before.extend(paneconfig['shell_command_before']) if 'shell_command_before' in paneconfig else None

            if 'shell_command' not in paneconfig:
                paneconfig['shell_command'] = list()

            commands_before.extend(paneconfig['shell_command']) if paneconfig['shell_command'] else None
            paneconfig['shell_command'] = commands_before

    return config

@@ -199,475 +199,8 @@
 %22%0A%0A%0A
-def _expand_shell_command(c):%0A    '''any config section, session, window, pane that can%0A    contain the 'shell_command' value%0A    '''%0A    if ('shell_command' in c and%0A            isinstance(c%5B'shell_command'%5D, basestring)):%0A            c%5B'shell_command'%5D = %5Bc%5B'shell_command'%5D%5D%0A%0A    if ('shell_command_before' in c and%0A            isinstance(c%5B'shell_command_before'%5D, basestring)):%0A            c%5B'shell_command_before'%5D = %5Bc%5B'shell_command_before'%5D%5D%0A%0A    return c%0A%0A%0A
 def 
@@ -218,24 +218,24 @@
 ig(config):%0A
+
     '''Expan
@@ -1084,193 +1084,690 @@
     
-config = _expand_shell_command(config)%0A    for window in config%5B'windows'%5D:%0A        window = _expand_shell_command(window)%0A        window%5B'pane
+'''any config section, session, window, pane that can%0A    contain the 'shell_command' value%0A    '''%0A    if ('shell_command' in config and isinstance(config%5B'shell_command'%5D, basestring)):%0A        config%5B'shell_command'%5D = %5Bconfig%5B'shell_command'%5D%5D%0A%0A    if ('shell_command_before' in config and isinstance(config%5B'shell_command_before'%5D, basestring)):%0A        config%5B'shell_command_before'%5D = %5Bconfig%5B'shell_command_before'%5D%5D%0A%0A    # recurse into window and pane config items%0A    if 'windows' in config:%0A        config%5B'window
 s'%5D = %5B
-_
 expand_
-shell_command(%0A            
+config(window)%0A                             for window in config%5B'windows'%5D%5D%0A    if 'panes' in config:%0A        config%5B'panes'%5D = %5Bexpand_config(
 pane
@@ -1780,22 +1780,22 @@
 pane in 
-window
+config
 %5B'panes'
@@ -2457,32 +2457,49 @@
 before = config%5B
+%0A                
 'shell_command_b
@@ -2594,24 +2594,60 @@
 indowconfig%5B
+%0A                                   
 'shell_comma
@@ -2749,32 +2749,68 @@
 tend(paneconfig%5B
+%0A                                   
 'shell_command_b
@@ -2969,24 +2969,24 @@
 %5D = list()%0A%0A
-
             
@@ -3011,32 +3011,68 @@
 tend(paneconfig%5B
+%0A                                   
 'shell_command'%5D

simplify config parser more