<commit_before>import kaptan
from sh import tmux, cut
from pprint import pprint
config = kaptan.Kaptan(handler="yaml")

config.import_config("""
    windows:
        - editor:
            layout: main-vertical
            panes:
                - vim
                - guard
        - server: bundle exec rails s
        - logs: tail -f logs/development.log
    """)

print config
pprint(config.__dict__)

windows = list()
# expand inline window configuration
""" expand
    dict({'session_name': { dict })

    to

    dict({ name='session_name', **dict})
"""

for window in config.get('windows'):

    if len(window) == 1:
        name = window.iterkeys().next()  # get window name

        """expand
            window[name] = 'command'

            to

            window[name] = {
                panes=['command']
            }
        """
        if isinstance(window[name], basestring):
            windowoptions = dict(
                panes=[window[name]]
            )
        else:
            windowoptions = window[name]

        window = dict(name=name, **windowoptions)
        if len(window['panes']) > 1:
            pprint('omg multiple panes')

    windows.append(window)


class Session(object):
    __FORMATS__ = [
        'session_attached', 'session_created', 'session_created_string',
        'session_group', 'session_grouped', 'session_height', 'session_id',
        'session_name', 'session_width', 'session_windows']

    def __init__(self, session_name):
        self._session_name = session_name

    @property
    def session_name(self):
        return self._session_name

    @property
    def windows(self):

        if hasattr(self, '_session_name'):
            formats = [
                'session_name', 'window_index', 'pane_index', 'window_width',
                'window_height'
            ]
            tmux_formats = ['#{%s}\t' % format for format in formats]

            windows = cut(tmux('list-windows', '-t%s' % self.session_name, \
                            '-F%s' % ''.join(tmux_formats)), '-f1', '-d:')

            windows = dict(zip(formats, windows.split('\t')))
            windows = [Window(session=self, **windows)]
            return windows
        else:
            return None  # session is not bound to a current session, the user
                         # is using Session imported config file to launch a
                         # new session

    def __repr__(self):
        # todo test without session_name
        return "%s(%s)" % (self.__class__, self.session_name)

    pass


class Window(object):
    def __init__(self, **kwargs):
        if 'session' in kwargs:
            if isinstance(kwargs['session'], Session):
                self._session = kwargs['session']

    @property
    def session(self):
        return self._session if self._session else None

    @property
    def panes(self):

        if hasattr(self.session, '_session_name'):
            formats = [
                'session_name', 'window_index', 'pane_index', 'window_width',
                'window_height', 'pane_width', 'pane_height', 'pane_pid',
                'pane_current_path'
            ]
            tmux_formats = ['#{%s}\t' % format for format in formats]

            panes = cut(tmux('list-panes',
                               '-s', # for sessions
                               '-t%s' % self.session.session_name,  # target session_name<commit_msg>window object, output pane info<commit_after>import kaptan
from sh import tmux, cut
from pprint import pprint
config = kaptan.Kaptan(handler="yaml")

config.import_config("""
    windows:
        - editor:
            layout: main-vertical
            panes:
                - vim
                - guard
        - server: bundle exec rails s
        - logs: tail -f logs/development.log
    """)

print config
pprint(config.__dict__)

windows = list()
# expand inline window configuration
""" expand
    dict({'session_name': { dict })

    to

    dict({ name='session_name', **dict})
"""

for window in config.get('windows'):

    if len(window) == 1:
        name = window.iterkeys().next()  # get window name

        """expand
            window[name] = 'command'

            to

            window[name] = {
                panes=['command']
            }
        """
        if isinstance(window[name], basestring):
            windowoptions = dict(
                panes=[window[name]]
            )
        else:
            windowoptions = window[name]

        window = dict(name=name, **windowoptions)
        if len(window['panes']) > 1:
            pprint('omg multiple panes')

    windows.append(window)


class Session(object):
    __FORMATS__ = [
        'session_attached', 'session_created', 'session_created_string',
        'session_group', 'session_grouped', 'session_height', 'session_id',
        'session_name', 'session_width', 'session_windows']

    def __init__(self, session_name):
        self._session_name = session_name

    @property
    def session_name(self):
        return self._session_name

    @property
    def windows(self):

        if hasattr(self, '_session_name'):
            formats = [
                'session_name', 'window_index', 'pane_index', 'window_width',
                'window_height'
            ]
            tmux_formats = ['#{%s}\t' % format for format in formats]

            windows = cut(tmux('list-windows', '-t%s' % self.session_name, \
                            '-F%s' % ''.join(tmux_formats)), '-f1', '-d:')

            windows = dict(zip(formats, windows.split('\t')))
            windows = [Window(session=self, **windows)]
            return windows
        else:
            return None  # session is not bound to a current session, the user
                         # is using Session imported config file to launch a
                         # new session

    def __repr__(self):
        # todo test without session_name
        return "%s(%s)" % (self.__class__, self.session_name)

    pass


class Window(object):
    def __init__(self, **kwargs):
        if 'session' in kwargs:
            if isinstance(kwargs['session'], Session):
                self._session = kwargs['session']

    @property
    def session(self):
        return self._session if self._session else None

    # todo, @has_session property, or throw an Error
    # todo, Pane
    # todo, __cmd__

    @property
    def panes(self):

        if hasattr(self.session, '_session_name'):
            formats = [
                'session_name', 'window_index', 'pane_index', 'window_width',
                'window_height', 'pane_width', 'pane_height', 'pane_pid',
                'pane_current_path'
            ]
            tmux_formats = ['#{%s}\t' % format for format in formats]

            panes = cut(tmux('list-panes',
                               '-s', # for sessions
                               '-t%s' % self.session.session_name,  # target session_name
{'fraction_non_alphanumeric': 0.11987336307382357, 'fraction_numerical': 0.0008634335875665563, 'mean_word_length': 1.883817427385892, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1124112', 'n_tokens_mistral': 1986, 'n_tokens_neox': 1919, 'n_words': 476}