<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function , with_statement , unicode_literals <EOL> import logging <EOL> import os <EOL> from . import formats , exc <EOL> from . session import Session <EOL> from . util import tmux_cmd , TmuxRelationalObject <EOL> from . common import EnvironmentMixin <EOL> logger = logging . getLogger ( __name__ ) <EOL> class Server ( TmuxRelationalObject , EnvironmentMixin ) : <EOL> """<STR_LIT>""" <EOL> socket_name = None <EOL> socket_path = None <EOL> config_file = None <EOL> colors = None <EOL> childIdAttribute = '<STR_LIT>' <EOL> def __init__ ( <EOL> self , <EOL> socket_name = None , <EOL> socket_path = None , <EOL> config_file = None , <EOL> colors = None , <EOL> ** kwargs <EOL> ) : <EOL> EnvironmentMixin . __init__ ( self , '<STR_LIT>' ) <EOL> self . _windows = [ ] <EOL> self . _panes = [ ] <EOL> if socket_name : <EOL> self . socket_name = socket_name <EOL> if socket_path : <EOL> self . socket_path = socket_path <EOL> if config_file : <EOL> self . config_file = config_file <EOL> if colors : <EOL> self . colors = colors <EOL> def cmd ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> args = list ( args ) <EOL> if self . socket_name : <EOL> args . insert ( <NUM_LIT:0> , '<STR_LIT>' . format ( self . socket_name ) ) <EOL> if self . socket_path : <EOL> args . insert ( <NUM_LIT:0> , '<STR_LIT>' . format ( self . socket_path ) ) <EOL> if self . config_file : <EOL> args . insert ( <NUM_LIT:0> , '<STR_LIT>' . format ( self . config_file ) ) <EOL> if self . colors : <EOL> if self . colors == <NUM_LIT> : <EOL> args . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> elif self . colors == <NUM_LIT> : <EOL> args . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return tmux_cmd ( * args , ** kwargs ) <EOL> def _list_sessions ( self ) : <EOL> """<STR_LIT>""" <EOL> sformats = formats . SESSION_FORMATS <EOL> tmux_formats = [ '<STR_LIT>' % f for f in sformats ] <EOL> tmux_args = ( <EOL> '<STR_LIT>' % '<STR_LIT:\t>' . join ( tmux_formats ) , <EOL> ) <EOL> proc = self . cmd ( <EOL> '<STR_LIT>' , <EOL> * tmux_args <EOL> ) <EOL> if proc . stderr : <EOL> raise exc . TmuxpException ( proc . stderr ) <EOL> else : <EOL> session_info = proc . stdout [ <NUM_LIT:0> ] <EOL> if proc . stderr : <EOL> raise exc . TmuxpException ( proc . stderr ) <EOL> sformats = formats . SESSION_FORMATS <EOL> tmux_formats = [ '<STR_LIT>' % format for format in sformats ] <EOL> sessions = proc . stdout <EOL> sessions = [ dict ( zip ( <EOL> sformats , session . split ( '<STR_LIT:\t>' ) ) ) for session in sessions ] <EOL> sessions = [ <EOL> dict ( ( k , v ) for k , v in session . items ( ) if v ) <EOL> for session in sessions <EOL> ] <EOL> return sessions <EOL> @ property <EOL> def _sessions ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _list_sessions ( ) <EOL> def list_sessions ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ <EOL> Session ( server = self , ** s ) for s in self . _sessions <EOL> ] <EOL> @ property <EOL> def sessions ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . list_sessions ( ) <EOL> children = sessions <EOL> def _list_windows ( self ) : <EOL> """<STR_LIT>""" <EOL> wformats = [ '<STR_LIT>' , '<STR_LIT>' ] + formats . WINDOW_FORMATS <EOL> tmux_formats = [ '<STR_LIT>' % format for format in wformats ] <EOL> proc = self . cmd ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % '<STR_LIT:\t>' . join ( tmux_formats ) , <EOL> ) <EOL> if proc . stderr : <EOL> raise exc . TmuxpException ( proc . stderr ) <EOL> windows = proc . stdout <EOL> wformats = [ '<STR_LIT>' , '<STR_LIT>' ] + formats . WINDOW_FORMATS <EOL> windows = [ dict ( zip ( <EOL> wformats , window . split ( '<STR_LIT:\t>' ) ) ) for window in windows ] <EOL> windows = [ <EOL> dict ( ( k , v ) for k , v in window . items ( ) if v ) for window in windows <EOL> ] <EOL> for w in windows : <EOL> if not '<STR_LIT>' in w : <EOL> w [ '<STR_LIT>' ] = w [ '<STR_LIT>' ] <EOL> if self . _windows : <EOL> self . _windows [ : ] = [ ] <EOL> self . _windows . extend ( windows ) <EOL> return self . _windows <EOL> def _update_windows ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _list_windows ( ) <EOL> return self <EOL> def _list_panes ( self ) : <EOL> """<STR_LIT>""" <EOL> pformats = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] + formats . PANE_FORMATS <EOL> tmux_formats = [ '<STR_LIT>' % f for f in pformats ] <EOL> proc = self . cmd ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % '<STR_LIT>' . join ( tmux_formats ) , <EOL> ) <EOL> if proc . stderr : <EOL> raise exc . TmuxpException ( proc . stderr ) <EOL> panes = proc . stdout <EOL> pformats = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] + formats . PANE_FORMATS <EOL> panes = [ dict ( zip ( <EOL> pformats , window . split ( '<STR_LIT:\t>' ) ) ) for window in panes ] <EOL> panes = [ <EOL> dict ( ( k , v ) for k , v in window . items ( ) if v ) for window in panes <EOL> ] <EOL> if self . _panes : <EOL> self . _panes [ : ] = [ ] <EOL> self . _panes . extend ( panes ) <EOL> return self . _panes <EOL> def _update_panes ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _list_panes ( ) <EOL> return self <EOL> def attached_sessions ( self ) : <EOL> """<STR_LIT>""" <EOL> sessions = self . _sessions <EOL> attached_sessions = list ( ) <EOL> for session in sessions : <EOL> if '<STR_LIT>' in session : <EOL> if session . get ( '<STR_LIT>' ) == '<STR_LIT:1>' : <EOL> logger . debug ( '<STR_LIT>' , session . get ( <EOL> '<STR_LIT>' ) ) <EOL> attached_sessions . append ( session ) <EOL> else : <EOL> continue <EOL> return [ <EOL> Session ( server = self , ** s ) for s in attached_sessions <EOL> ] or None <EOL> def has_session ( self , target_session ) : <EOL> """<STR_LIT>""" <EOL> proc = self . cmd ( '<STR_LIT>' , '<STR_LIT>' % target_session ) <EOL> if proc . stdout == [ ] : <EOL> return True <EOL> if any ( <EOL> x in proc . stdout for x in <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> ) : <EOL> return False <EOL> elif '<STR_LIT>' in proc . stdout : <EOL> return False <EOL> elif '<STR_LIT>' in proc . stdout : <EOL> return False <EOL> elif '<STR_LIT>' in proc . stdout : <EOL> return False <EOL> else : <EOL> return True <EOL> def kill_server ( self ) : <EOL> """<STR_LIT>""" <EOL> self . cmd ( '<STR_LIT>' ) <EOL> def kill_session ( self , target_session = None ) : <EOL> """<STR_LIT>""" <EOL> proc = self . cmd ( '<STR_LIT>' , '<STR_LIT>' % target_session ) <EOL> if proc . stderr : <EOL> raise exc . TmuxpException ( proc . stderr ) <EOL> return self <EOL> def switch_client ( self , target_session ) : <EOL> """<STR_LIT>""" <EOL> proc = self . cmd ( '<STR_LIT>' , '<STR_LIT>' % target_session ) <EOL> if proc . stderr : <EOL> raise exc . TmuxpException ( proc . stderr ) <EOL> def attach_session ( self , target_session = None ) : <EOL> """<STR_LIT>""" <EOL> tmux_args = tuple ( ) <EOL> if target_session : <EOL> tmux_args += ( '<STR_LIT>' % target_session , ) <EOL> proc = self . cmd ( '<STR_LIT>' , * tmux_args ) <EOL> if proc . stderr : <EOL> raise exc . TmuxpException ( proc . stderr ) <EOL> def new_session ( self , <EOL> session_name = None , <EOL> kill_session = False , <EOL> attach = False , <EOL> * args , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . has_session ( session_name ) : <EOL> if kill_session : <EOL> self . cmd ( '<STR_LIT>' , '<STR_LIT>' % session_name ) <EOL> logger . info ( '<STR_LIT>' % session_name ) <EOL> else : <EOL> raise exc . TmuxSessionExists ( <EOL> '<STR_LIT>' % session_name <EOL> ) <EOL> logger . debug ( '<STR_LIT>' % session_name ) <EOL> sformats = formats . SESSION_FORMATS <EOL> tmux_formats = [ '<STR_LIT>' % f for f in sformats ] <EOL> env = os . environ . get ( '<STR_LIT>' ) <EOL> if env : <EOL> del os . environ [ '<STR_LIT>' ] <EOL> tmux_args = ( <EOL> '<STR_LIT>' % session_name , <EOL> '<STR_LIT>' , '<STR_LIT>' % '<STR_LIT:\t>' . join ( tmux_formats ) , <EOL> ) <EOL> if not attach : <EOL> tmux_args += ( '<STR_LIT>' , ) <EOL> proc = self . cmd ( <EOL> '<STR_LIT>' , <EOL> * tmux_args <EOL> ) <EOL> if proc . stderr : <EOL> raise exc . TmuxpException ( proc . stderr ) <EOL> session = proc . stdout [ <NUM_LIT:0> ] <EOL> if env : <EOL> os . environ [ '<STR_LIT>' ] = env <EOL> session = dict ( zip ( sformats , session . split ( '<STR_LIT:\t>' ) ) ) <EOL> session = dict ( ( k , v ) for k , v in session . items ( ) if v ) <EOL> session = Session ( server = self , ** session ) <EOL> return session </s>
