tmux.py
Go to the documentation of this file.
1 #Copyright (c) 2016, Allgeyer Tobias, Aumann Florian, Borella Jocelyn, Karrenbauer Oliver, Marek Felix, Meissner Pascal, Stroh Daniel, Trautmann Jeremias
2 #All rights reserved.
3 #
4 #Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5 #
6 #1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7 #
8 #2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other #materials provided with the distribution.
9 #
10 #3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific #prior written permission.
11 #
12 #THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED #WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, #INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR #PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) #ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13 import subprocess
14 import time
15 from string_util import *
16 import libtmux
17 
18 server = libtmux.Server()
19 session = server.sessions[0]
20 curWindow = session.attached_window
21 curPane = session.attached_pane
22 
23 # tmux api
24 # windows
25 
26 def getPaneOut(pane):
27  return "\n".join(pane.cmd("capturep", "-p").stdout)
28 
30  return getPaneOut(session.attached_pane)
31 
32 def getTmuxOut(windowNameOrId, paneId):
33  window = getWindow(windowNameOrId)
34  if window is None:
35  return
36  paneId = paneId % len(window.panes)
37  return getPaneOut(window.panes[paneId])
38 
39 def getWindow(windowNameOrId):
40  if type(windowNameOrId) is int:
41  windowNameOrId = "@" + str(windowNameOrId)
42  window = session.get_by_id(windowNameOrId)
43  if window is not None:
44  return window
45  # otherwise we have to find the window
46  # [(name, window)]
47  windowTuples = map(lambda x : (x._info["window_name"], x), session.windows)
48  filteredWindows = filter (lambda (name, window): name == windowNameOrId, windowTuples)
49  if len(filteredWindows) > 0:
50  return filteredWindows[0][1]
51 
52 def selectWindow(windowNameOrId):
53  window = getWindow(windowNameOrId)
54  if window is not None:
55  window.select_window()
56  else:
57  print("window not found")
58 
60  return len(session.windows)
61 
63  windows = session.windows
64  validWindowIds = map(lambda x: x._window_id, windows)
65  return validWindowIds
66 
68  selectWindow(str(i))
69  nPanes = getNumberOfPanes()
70  for i in range(0, nPanes):
71  selectPane(i)
73 
75  selectWindow(str(i))
76  nPanes = getNumberOfPanes()
77  for i in range(0, nPanes):
78  selectPane(i)
80 
82  for i in getValidWindowIds():
83  restartWindow(i)
84 
86  for i in getValidWindowIds():
88 
89 # panes
91  return len(session.attached_window.panes)
92 
93 def selectPane(paneId):
94  nPanes = getNumberOfPanes()
95  paneId = paneId % nPanes
96  session.attached_window.panes[paneId].select_pane()
97 
99  session.attached_pane.cmd("send-keys", "C-C")
100 
102  terminatePane()
103  # wait for application to finish
104  while True:
105  lastLine = getLastLinesFrom(getTmuxCurOut(), -1)
106  lastSymbol = lastLine[-1] if len(lastLine) > 0 else ""
107  if "$" == lastSymbol:
108  break
109  terminatePane()
110  # to terminate python process, we first remove all input characters before and after and then use Ctrl + d
111  #session.attached_pane.cmd("send-keys", "C-k")
112  #session.attached_pane.cmd("send-keys", "C-u")
113  #session.attached_pane.cmd("send-keys", "C-d")
114  time.sleep(1)
115  # restart last command
116  session.attached_pane.cmd("send-keys", "Up")
117  session.attached_pane.enter()
118 
120  terminatePane()
121  # wait for application to finish
122  while True:
123  lastLine = getLastLinesFrom(getTmuxCurOut(), -1)
124  lastSymbol = lastLine[-1] if len(lastLine) > 0 else ""
125  if "$" == lastSymbol:
126  break
127  terminatePane()
128  time.sleep(1)
129 
130 def restartPanes(windowAndPanes):
131  oldPane = session.attached_pane
132  if type(windowAndPanes) is not list:
133  windowAndPanes = [windowAndPanes]
134  for (windowNameOrId, paneId) in windowAndPanes:
135  selectWindow(windowNameOrId)
136  selectPane(paneId)
138  oldPane.window.select_window()
139  oldPane.select_pane()
140 
141 def terminatePanes(windowAndPanes):
142  oldPane = session.attached_pane
143  if type(windowAndPanes) is not list:
144  windowAndPanes = [windowAndPanes]
145  for (windowNameOrId, paneId) in windowAndPanes:
146  selectWindow(windowNameOrId)
147  selectPane(paneId)
149  oldPane.window.select_window()
150  oldPane.select_pane()
151 
152 def selectWindowAndPane(windowNameOrId, paneId):
153  selectWindow(windowNameOrId)
154  selectPane(paneId)
155 
156 def tmuxKill():
157  subprocess.call("tmux kill-server", shell=True)
def restartPanes(windowAndPanes)
Definition: tmux.py:130
def restartCurrentPane()
Definition: tmux.py:101
def getNumberOfPanes()
Definition: tmux.py:90
def restartWindow(i)
Definition: tmux.py:67
def tmuxKill()
Definition: tmux.py:156
def getTmuxOut(windowNameOrId, paneId)
Definition: tmux.py:32
def getValidWindowIds()
Definition: tmux.py:62
def terminateAllWindows()
Definition: tmux.py:85
def terminatePanes(windowAndPanes)
Definition: tmux.py:141
def terminatePane()
Definition: tmux.py:98
def terminateWindow(i)
Definition: tmux.py:74
def restartAllWindows()
Definition: tmux.py:81
def getWindow(windowNameOrId)
Definition: tmux.py:39
def getPaneOut(pane)
Definition: tmux.py:26
def selectWindow(windowNameOrId)
Definition: tmux.py:52
def selectWindowAndPane(windowNameOrId, paneId)
Definition: tmux.py:152
def selectPane(paneId)
Definition: tmux.py:93
def getTmuxCurOut()
Definition: tmux.py:29
def getNumberOfWindows()
Definition: tmux.py:59
def terminateCurrentPane()
Definition: tmux.py:119
def getLastLinesFrom(str, pos)
Definition: string_util.py:18


asr_resources_for_active_scene_recognition
Author(s): Allgeyer Tobias, Aumann Florian, Borella Jocelyn, Karrenbauer Oliver, Marek Felix, Meißner Pascal, Stroh Daniel, Trautmann Jeremias
autogenerated on Tue Feb 18 2020 03:14:15