Browse files

use xdotools to save/restore size/pos of window + flake8 fixes

  • Loading branch information...
1 parent 688c57c commit e66ee3082d0f0e1f78ae6bbc9420947f3a55e65d @tshirtman tshirtman committed Jan 15, 2013
Showing with 48 additions and 10 deletions.
  1. +48 −10 reloader.py
View
58 reloader.py
@@ -26,11 +26,18 @@
Or to restart a daemon when configuration or data changes.
Or any other use, the sky is the limit :)
''')
-parser.add_argument('-p', '--path', type=str, default='.', help='set the path to monitor for changes')
-parser.add_argument('-a', '--action', type=str, default='restart', help='what action to perform when changes are detected')
-parser.add_argument('-i', '--ignorelist', type=str, default='', nargs='*', help='files to ignore')
-parser.add_argument('-f', '--focus', type=int, default=0, help='save focus and restore it for the next n seconds after restarting application, require xdotool (linux only)')
-parser.add_argument('-s', '--sleep', type=int, default=0, help='ignore events for n seconds after the last restart')
+parser.add_argument('-p', '--path', type=str, default='.',
+ help='set the path to monitor for changes')
+parser.add_argument('-a', '--action', type=str, default='restart',
+ help='what action to perform when changes are detected')
+parser.add_argument('-i', '--ignorelist', type=str, default='', nargs='*',
+ help='files to ignore')
+parser.add_argument('-f', '--focus', type=int, default=0,
+ help='save focus and restore it for the next n'
+ 'seconds after restarting application, require'
+ 'xdotool (linux only)')
+parser.add_argument('-s', '--sleep', type=int, default=0,
+ help='ignore events for n seconds after the last restart')
parser.add_argument('command', type=str, nargs=REMAINDER)
@@ -54,20 +61,43 @@ def stop(self):
self._process.terminate()
log('red', 'TERMINATED')
- def start(self):
+ def start(self, geometry=None):
self._last_restart = time()
if self.focus:
t = time()
- wid = int(Popen(['/usr/bin/env', 'xdotool', 'getwindowfocus'], stdout=PIPE).communicate()[0])
+ wid = int(Popen(['/usr/bin/env', 'xdotool', 'getwindowfocus'],
+ stdout=PIPE).communicate()[0])
self._process = Popen(self.command)
log('green', 'STARTED %s' % self._process)
+ self.swid = int(Popen(['/usr/bin/env', 'xdotool', 'getwindowfocus'],
+ stdout=PIPE).communicate()[0])
if self.focus:
while time() < t + self.focus:
+ # try to detect a window appeared by tracking focus
+ # change, NOT 100% safe!
+ swid = int(Popen(['/usr/bin/env', 'xdotool', 'getwindowfocus'],
+ stdout=PIPE).communicate()[0])
+ if swid != wid:
+ print "got swid", swid
+ self.swid = swid
+ if geometry:
+ self.replace(geometry)
+
Popen(['/usr/bin/env', 'xdotool', 'windowfocus', str(wid)])
sleep(.01)
+ if geometry:
+ self.replace(geometry)
+
+ def replace(self, geometry):
+ print geometry
+ x, y = geometry.split('\n')[1].strip().split(' ')[1].split(',')
+ w, h = geometry.split('\n')[2].strip().split(' ')[1].split('x')
+ Popen(['/usr/bin/env', 'xdotool', 'windowsize', str(self.swid), w, h])
+ Popen(['/usr/bin/env', 'xdotool', 'windowmove', str(self.swid), x, y])
+
def on_any_event(self, event):
if self.sleep and time() < self._last_restart + self.sleep:
return
@@ -81,13 +111,20 @@ def on_any_event(self, event):
log('blue', '%s RESTARTING' % event)
+ geometry = None
+ if self.swid:
+ geometry = Popen(['/usr/bin/env', 'xdotool', 'getwindowgeometry',
+ str(self.swid)],
+ stdout=PIPE).communicate()[0]
self.stop()
- self.start()
+ self.start(geometry)
def monitor(command, path, action, focus, sleeptime, ignorelist=None):
if action == 'restart':
- ev = RestartHandler(command, path=path, focus=focus, sleeptime=sleeptime, ignorelist=ignorelist)
+ ev = RestartHandler(command, path=path, focus=focus,
+ sleeptime=sleeptime,
+ ignorelist=ignorelist)
else:
raise NotImplementedError('action %s not implemented' % action)
@@ -104,4 +141,5 @@ def monitor(command, path, action, focus, sleeptime, ignorelist=None):
if __name__ == '__main__':
args = parser.parse_args()
log('blue', str(args))
- monitor(args.command, args.path, args.action, args.focus, args.sleep, args.ignorelist)
+ monitor(args.command, args.path, args.action, args.focus, args.sleep,
+ args.ignorelist)

0 comments on commit e66ee30

Please sign in to comment.