2009-08-07 09:21:37 +00:00
|
|
|
#!/usr/bin/python
|
2009-08-09 23:10:08 +00:00
|
|
|
# Terminator.util - misc utility functions
|
2010-01-04 23:56:28 +00:00
|
|
|
# Copyright (C) 2006-2010 cmsj@tenshu.net
|
2009-08-07 09:21:37 +00:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, version 2 only.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2015-09-01 20:50:09 +00:00
|
|
|
"""Terminator.util - misc utility functions"""
|
2009-08-07 09:21:37 +00:00
|
|
|
|
|
|
|
import sys
|
2015-07-12 20:06:36 +00:00
|
|
|
from gi.repository import Gtk, Gdk
|
|
|
|
import cairo
|
2009-09-06 21:54:52 +00:00
|
|
|
import os
|
|
|
|
import pwd
|
2010-01-14 13:15:05 +00:00
|
|
|
import inspect
|
2012-10-18 18:39:28 +00:00
|
|
|
import uuid
|
2013-08-28 21:09:17 +00:00
|
|
|
import subprocess
|
2009-08-07 09:21:37 +00:00
|
|
|
|
|
|
|
# set this to true to enable debugging output
|
2010-01-11 10:10:19 +00:00
|
|
|
DEBUG = False
|
2010-01-14 13:15:05 +00:00
|
|
|
# set this to true to additionally list filenames in debugging
|
|
|
|
DEBUGFILES = False
|
2010-02-17 20:15:33 +00:00
|
|
|
# list of classes to show debugging for. empty list means show all classes
|
|
|
|
DEBUGCLASSES = []
|
|
|
|
# list of methods to show debugging for. empty list means show all methods
|
|
|
|
DEBUGMETHODS = []
|
2009-08-07 09:21:37 +00:00
|
|
|
|
2009-08-18 11:52:06 +00:00
|
|
|
def dbg(log = ""):
|
2009-08-09 23:10:08 +00:00
|
|
|
"""Print a message if debugging is enabled"""
|
|
|
|
if DEBUG:
|
2010-01-14 13:15:05 +00:00
|
|
|
stackitem = inspect.stack()[1]
|
|
|
|
parent_frame = stackitem[0]
|
|
|
|
method = parent_frame.f_code.co_name
|
|
|
|
names, varargs, keywords, local_vars = inspect.getargvalues(parent_frame)
|
|
|
|
try:
|
|
|
|
self_name = names[0]
|
|
|
|
classname = local_vars[self_name].__class__.__name__
|
|
|
|
except IndexError:
|
|
|
|
classname = "noclass"
|
|
|
|
if DEBUGFILES:
|
|
|
|
line = stackitem[2]
|
|
|
|
filename = parent_frame.f_code.co_filename
|
|
|
|
extra = " (%s:%s)" % (filename, line)
|
|
|
|
else:
|
|
|
|
extra = ""
|
2010-02-17 20:15:33 +00:00
|
|
|
if DEBUGCLASSES != [] and classname not in DEBUGCLASSES:
|
|
|
|
return
|
|
|
|
if DEBUGMETHODS != [] and method not in DEBUGMETHODS:
|
|
|
|
return
|
2010-04-15 11:59:19 +00:00
|
|
|
try:
|
|
|
|
print >> sys.stderr, "%s::%s: %s%s" % (classname, method, log, extra)
|
|
|
|
except IOError:
|
|
|
|
pass
|
2009-08-07 09:21:37 +00:00
|
|
|
|
2009-08-18 11:52:06 +00:00
|
|
|
def err(log = ""):
|
2009-08-09 23:10:08 +00:00
|
|
|
"""Print an error message"""
|
2010-04-15 11:59:19 +00:00
|
|
|
try:
|
|
|
|
print >> sys.stderr, log
|
|
|
|
except IOError:
|
|
|
|
pass
|
2009-08-18 11:52:06 +00:00
|
|
|
|
|
|
|
def gerr(message = None):
|
|
|
|
"""Display a graphical error. This should only be used for serious
|
|
|
|
errors as it will halt execution"""
|
|
|
|
|
2014-09-19 14:08:08 +00:00
|
|
|
dialog = Gtk.MessageDialog(None, Gtk.DialogFlags.MODAL,
|
|
|
|
Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, message)
|
2009-08-18 11:52:06 +00:00
|
|
|
dialog.run()
|
2010-06-15 13:54:24 +00:00
|
|
|
dialog.destroy()
|
2009-08-18 11:52:06 +00:00
|
|
|
|
2009-09-06 21:54:52 +00:00
|
|
|
def has_ancestor(widget, wtype):
|
2009-09-04 23:34:09 +00:00
|
|
|
"""Walk up the family tree of widget to see if any ancestors are of type"""
|
|
|
|
while widget:
|
|
|
|
widget = widget.get_parent()
|
2009-09-06 21:54:52 +00:00
|
|
|
if isinstance(widget, wtype):
|
2009-09-04 23:34:09 +00:00
|
|
|
return(True)
|
|
|
|
return(False)
|
|
|
|
|
2015-09-01 20:50:09 +00:00
|
|
|
def manual_lookup():
|
|
|
|
'''Choose the manual to open based on LANGUAGE'''
|
|
|
|
prefix = os.path.join(os.sep, 'usr', 'share', 'doc', 'terminator')
|
|
|
|
if 'LANGUAGE' in os.environ:
|
|
|
|
languages = os.environ['LANGUAGE'].split(':')
|
|
|
|
for language in languages:
|
|
|
|
full_path = os.path.join(prefix, 'html_%s' % (language), 'index.html')
|
|
|
|
if os.path.isfile(full_path):
|
|
|
|
dbg('Found %s manual' % (language))
|
|
|
|
return full_path
|
|
|
|
dbg('Couldn\'t find manual for %s language' % (language))
|
|
|
|
|
|
|
|
full_path = os.path.join(prefix, 'html', 'index.html')
|
|
|
|
if os.path.isfile(full_path):
|
|
|
|
dbg('Falling back to the default manual')
|
|
|
|
return full_path
|
|
|
|
else:
|
|
|
|
dbg('I can\'t find any suitable manual')
|
|
|
|
return None
|
|
|
|
|
2009-09-06 21:54:52 +00:00
|
|
|
def path_lookup(command):
|
|
|
|
'''Find a command in our path'''
|
|
|
|
if os.path.isabs(command):
|
|
|
|
if os.path.isfile(command):
|
|
|
|
return(command)
|
|
|
|
else:
|
|
|
|
return(None)
|
|
|
|
elif command[:2] == './' and os.path.isfile(command):
|
|
|
|
dbg('path_lookup: Relative filename %s found in cwd' % command)
|
|
|
|
return(command)
|
|
|
|
|
|
|
|
try:
|
|
|
|
paths = os.environ['PATH'].split(':')
|
|
|
|
if len(paths[0]) == 0:
|
|
|
|
raise(ValueError)
|
|
|
|
except (ValueError, NameError):
|
|
|
|
dbg('path_lookup: PATH not set in environment, using fallbacks')
|
|
|
|
paths = ['/usr/local/bin', '/usr/bin', '/bin']
|
|
|
|
|
2010-01-18 22:56:43 +00:00
|
|
|
dbg('path_lookup: Using %d paths: %s' % (len(paths), paths))
|
2009-09-06 21:54:52 +00:00
|
|
|
|
|
|
|
for path in paths:
|
|
|
|
target = os.path.join(path, command)
|
|
|
|
if os.path.isfile(target):
|
|
|
|
dbg('path_lookup: found %s' % target)
|
|
|
|
return(target)
|
|
|
|
|
|
|
|
dbg('path_lookup: Unable to locate %s' % command)
|
|
|
|
|
|
|
|
def shell_lookup():
|
|
|
|
"""Find an appropriate shell for the user"""
|
2011-05-03 16:49:00 +00:00
|
|
|
try:
|
|
|
|
usershell = pwd.getpwuid(os.getuid())[6]
|
|
|
|
except KeyError:
|
|
|
|
usershell = None
|
2013-02-27 17:47:35 +00:00
|
|
|
shells = [usershell, 'bash', 'zsh', 'tcsh', 'ksh', 'csh', 'sh']
|
2009-09-06 21:54:52 +00:00
|
|
|
|
|
|
|
for shell in shells:
|
|
|
|
if shell is None:
|
|
|
|
continue
|
|
|
|
elif os.path.isfile(shell):
|
|
|
|
return(shell)
|
|
|
|
else:
|
|
|
|
rshell = path_lookup(shell)
|
|
|
|
if rshell is not None:
|
|
|
|
dbg('shell_lookup: Found %s at %s' % (shell, rshell))
|
|
|
|
return(rshell)
|
|
|
|
dbg('shell_lookup: Unable to locate a shell')
|
|
|
|
|
2009-10-27 21:03:11 +00:00
|
|
|
def widget_pixbuf(widget, maxsize=None):
|
|
|
|
"""Generate a pixbuf of a widget"""
|
2015-07-12 20:06:36 +00:00
|
|
|
# FIXME: Can this be changed from using "import cairo" to "from gi.repository import cairo"?
|
|
|
|
window = widget.get_window()
|
|
|
|
width, height = window.get_width(), window.get_height()
|
2009-10-27 21:03:11 +00:00
|
|
|
|
|
|
|
longest = max(width, height)
|
|
|
|
|
|
|
|
if maxsize is not None:
|
|
|
|
factor = float(maxsize) / float(longest)
|
|
|
|
|
|
|
|
if not maxsize or (width * factor) > width or (height * factor) > height:
|
|
|
|
factor = 1
|
|
|
|
|
2015-07-12 20:06:36 +00:00
|
|
|
preview_width, preview_height = int(width * factor), int(height * factor)
|
2009-10-27 21:03:11 +00:00
|
|
|
|
2015-07-12 20:06:36 +00:00
|
|
|
preview_surface = Gdk.Window.create_similar_surface(window,
|
|
|
|
cairo.CONTENT_COLOR, preview_width, preview_height)
|
|
|
|
|
|
|
|
cairo_context = cairo.Context(preview_surface)
|
|
|
|
cairo_context.scale(factor, factor)
|
|
|
|
Gdk.cairo_set_source_window(cairo_context, window, 0, 0)
|
|
|
|
cairo_context.paint()
|
|
|
|
|
|
|
|
scaledpixbuf = Gdk.pixbuf_get_from_surface(preview_surface, 0, 0, preview_width, preview_height);
|
|
|
|
|
2009-10-27 21:03:11 +00:00
|
|
|
return(scaledpixbuf)
|
|
|
|
|
2009-12-24 21:35:07 +00:00
|
|
|
def get_config_dir():
|
|
|
|
"""Expand all the messy nonsense for finding where ~/.config/terminator
|
|
|
|
really is"""
|
|
|
|
try:
|
|
|
|
configdir = os.environ['XDG_CONFIG_HOME']
|
|
|
|
except KeyError:
|
|
|
|
configdir = os.path.join(os.path.expanduser('~'), '.config')
|
|
|
|
|
2010-10-15 11:40:29 +00:00
|
|
|
dbg('Found config dir: %s' % configdir)
|
2009-12-24 21:35:07 +00:00
|
|
|
return(os.path.join(configdir, 'terminator'))
|
|
|
|
|
2009-12-26 01:19:42 +00:00
|
|
|
def dict_diff(reference, working):
|
|
|
|
"""Examine the values in the supplied working set and return a new dict
|
|
|
|
that only contains those values which are different from those in the
|
2015-09-01 20:50:09 +00:00
|
|
|
reference dictionary
|
|
|
|
|
|
|
|
>>> a = {'foo': 'bar', 'baz': 'bjonk'}
|
|
|
|
>>> b = {'foo': 'far', 'baz': 'bjonk'}
|
|
|
|
>>> dict_diff(a, b)
|
|
|
|
{'foo': 'far'}
|
|
|
|
"""
|
2009-12-26 01:19:42 +00:00
|
|
|
|
|
|
|
result = {}
|
|
|
|
|
|
|
|
for key in reference:
|
|
|
|
if reference[key] != working[key]:
|
|
|
|
result[key] = working[key]
|
|
|
|
|
|
|
|
return(result)
|
2010-01-20 00:54:35 +00:00
|
|
|
|
|
|
|
# Helper functions for directional navigation
|
|
|
|
def get_edge(allocation, direction):
|
|
|
|
"""Return the edge of the supplied allocation that we will care about for
|
|
|
|
directional navigation"""
|
|
|
|
if direction == 'left':
|
|
|
|
edge = allocation.x
|
2015-06-20 19:02:41 +00:00
|
|
|
p1, p2 = allocation.y, allocation.y + allocation.height
|
2010-01-20 00:54:35 +00:00
|
|
|
elif direction == 'up':
|
|
|
|
edge = allocation.y
|
2015-06-20 19:02:41 +00:00
|
|
|
p1, p2 = allocation.x, allocation.x + allocation.width
|
2010-01-20 00:54:35 +00:00
|
|
|
elif direction == 'right':
|
|
|
|
edge = allocation.x + allocation.width
|
2015-06-20 19:02:41 +00:00
|
|
|
p1, p2 = allocation.y, allocation.y + allocation.height
|
2010-01-20 00:54:35 +00:00
|
|
|
elif direction == 'down':
|
|
|
|
edge = allocation.y + allocation.height
|
2015-06-20 19:02:41 +00:00
|
|
|
p1, p2 = allocation.x, allocation.x + allocation.width
|
2010-01-20 00:54:35 +00:00
|
|
|
else:
|
|
|
|
raise ValueError('unknown direction %s' % direction)
|
|
|
|
|
2015-06-20 19:02:41 +00:00
|
|
|
return(edge, p1, p2)
|
2010-01-20 00:54:35 +00:00
|
|
|
|
2015-06-20 19:02:41 +00:00
|
|
|
def get_nav_possible(edge, allocation, direction, p1, p2):
|
2010-01-20 00:54:35 +00:00
|
|
|
"""Check if the supplied allocation is in the right direction of the
|
|
|
|
supplied edge"""
|
2015-06-20 19:02:41 +00:00
|
|
|
x1, x2 = allocation.x, allocation.x + allocation.width
|
|
|
|
y1, y2 = allocation.y, allocation.y + allocation.height
|
2010-01-20 00:54:35 +00:00
|
|
|
if direction == 'left':
|
2015-06-20 19:02:41 +00:00
|
|
|
return(x2 <= edge and y1 <= p2 and y2 >= p1)
|
2010-01-20 00:54:35 +00:00
|
|
|
elif direction == 'right':
|
2015-06-20 19:02:41 +00:00
|
|
|
return(x1 >= edge and y1 <= p2 and y2 >= p1)
|
2010-01-20 00:54:35 +00:00
|
|
|
elif direction == 'up':
|
2015-06-20 19:02:41 +00:00
|
|
|
return(y2 <= edge and x1 <= p2 and x2 >= p1)
|
2010-01-20 00:54:35 +00:00
|
|
|
elif direction == 'down':
|
2015-06-20 19:02:41 +00:00
|
|
|
return(y1 >= edge and x1 <= p2 and x2 >= p1)
|
2010-01-20 00:54:35 +00:00
|
|
|
else:
|
|
|
|
raise ValueError('Unknown direction: %s' % direction)
|
|
|
|
|
2010-01-20 13:04:14 +00:00
|
|
|
def get_nav_offset(edge, allocation, direction):
|
|
|
|
"""Work out how far edge is from a particular point on the allocation
|
|
|
|
rectangle, in the given direction"""
|
|
|
|
if direction == 'left':
|
|
|
|
return(edge - (allocation.x + allocation.width))
|
|
|
|
elif direction == 'right':
|
2015-06-20 19:02:41 +00:00
|
|
|
return(allocation.x - edge)
|
2010-01-20 13:04:14 +00:00
|
|
|
elif direction == 'up':
|
2015-06-20 19:02:41 +00:00
|
|
|
return(edge - (allocation.y + allocation.height))
|
2010-01-20 13:04:14 +00:00
|
|
|
elif direction == 'down':
|
2015-06-20 19:02:41 +00:00
|
|
|
return(allocation.y - edge)
|
2010-01-20 13:04:14 +00:00
|
|
|
else:
|
|
|
|
raise ValueError('Unknown direction: %s' % direction)
|
|
|
|
|
|
|
|
def get_nav_tiebreak(direction, cursor_x, cursor_y, rect):
|
|
|
|
"""We have multiple candidate terminals. Pick the closest by cursor
|
|
|
|
position"""
|
|
|
|
if direction in ['left', 'right']:
|
|
|
|
return(cursor_y >= rect.y and cursor_y <= (rect.y + rect.height))
|
|
|
|
elif direction in ['up', 'down']:
|
|
|
|
return(cursor_x >= rect.x and cursor_x <= (rect.x + rect.width))
|
|
|
|
else:
|
|
|
|
raise ValueError('Unknown direction: %s' % direction)
|
|
|
|
|
2010-01-28 13:41:44 +00:00
|
|
|
def enumerate_descendants(parent):
|
|
|
|
"""Walk all our children and build up a list of containers and
|
|
|
|
terminals"""
|
|
|
|
# FIXME: Does having to import this here mean we should move this function
|
|
|
|
# back to Container?
|
|
|
|
from factory import Factory
|
|
|
|
|
|
|
|
containerstmp = []
|
|
|
|
containers = []
|
|
|
|
terminals = []
|
|
|
|
maker = Factory()
|
|
|
|
|
|
|
|
if parent is None:
|
|
|
|
err('no parent widget specified')
|
|
|
|
return
|
|
|
|
|
|
|
|
for descendant in parent.get_children():
|
|
|
|
if maker.isinstance(descendant, 'Container'):
|
|
|
|
containerstmp.append(descendant)
|
|
|
|
elif maker.isinstance(descendant, 'Terminal'):
|
|
|
|
terminals.append(descendant)
|
|
|
|
|
|
|
|
while len(containerstmp) > 0:
|
2011-02-23 21:02:09 +00:00
|
|
|
child = containerstmp.pop(0)
|
2010-01-28 13:41:44 +00:00
|
|
|
for descendant in child.get_children():
|
|
|
|
if maker.isinstance(descendant, 'Container'):
|
|
|
|
containerstmp.append(descendant)
|
|
|
|
elif maker.isinstance(descendant, 'Terminal'):
|
|
|
|
terminals.append(descendant)
|
|
|
|
containers.append(child)
|
|
|
|
|
|
|
|
dbg('%d containers and %d terminals fall beneath %s' % (len(containers),
|
|
|
|
len(terminals), parent))
|
|
|
|
return(containers, terminals)
|
|
|
|
|
2013-10-30 16:07:23 +00:00
|
|
|
def make_uuid(str_uuid=None):
|
2012-10-18 18:39:28 +00:00
|
|
|
"""Generate a UUID for an object"""
|
2013-10-30 16:07:23 +00:00
|
|
|
if str_uuid:
|
|
|
|
return uuid.UUID(str_uuid)
|
2012-10-18 18:39:28 +00:00
|
|
|
return uuid.uuid4()
|
|
|
|
|
|
|
|
def inject_uuid(target):
|
|
|
|
"""Inject a UUID into an existing object"""
|
|
|
|
uuid = make_uuid()
|
|
|
|
if not hasattr(target, "uuid") or target.uuid == None:
|
|
|
|
dbg("Injecting UUID %s into: %s" % (uuid, target))
|
|
|
|
target.uuid = uuid
|
|
|
|
else:
|
|
|
|
dbg("Object already has a UUID: %s" % target)
|
|
|
|
|
2013-08-28 21:09:17 +00:00
|
|
|
def spawn_new_terminator(cwd, args):
|
|
|
|
"""Start a new terminator instance with the given arguments"""
|
|
|
|
cmd = sys.argv[0]
|
|
|
|
|
|
|
|
if not os.path.isabs(cmd):
|
|
|
|
# Command is not an absolute path. Figure out where we are
|
|
|
|
cmd = os.path.join (cwd, sys.argv[0])
|
|
|
|
if not os.path.isfile(cmd):
|
|
|
|
# we weren't started as ./terminator in a path. Give up
|
|
|
|
err('Unable to locate Terminator')
|
|
|
|
return False
|
|
|
|
|
|
|
|
dbg("Spawning: %s" % cmd)
|
|
|
|
subprocess.Popen([cmd]+args)
|