2009-05-09 22:57:05 -04:00
|
|
|
import os
|
|
|
|
import pwd
|
|
|
|
import re
|
|
|
|
import regex
|
2009-08-26 10:07:48 -04:00
|
|
|
from subprocess import Popen, PIPE, STDOUT
|
2009-05-09 22:57:05 -04:00
|
|
|
|
2009-07-23 16:26:59 -04:00
|
|
|
# character buffers use sequences like [r:d]foo[d:d] to indicate that foo
|
|
|
|
# should be fg=red,bg=default. as such, we have to escape \, [ and ].
|
2009-05-09 22:57:05 -04:00
|
|
|
cbuf_re = re.compile(r'[\[\]\\]')
|
|
|
|
def cbuf_escape(s):
|
2009-07-23 10:04:12 -04:00
|
|
|
'''escape characters which have special meaning in color buffers'''
|
2009-05-09 22:57:05 -04:00
|
|
|
return cbuf_re.sub(lambda m: '\\' + m.group(0), s)
|
2007-03-06 10:05:38 -05:00
|
|
|
|
2009-05-14 23:59:13 -04:00
|
|
|
def flatzip(a, b):
|
2009-07-23 10:04:12 -04:00
|
|
|
'''interleave two lists, e.g. ((a,b),(1,2)) -> (a,1,b,2)'''
|
2009-05-14 23:59:13 -04:00
|
|
|
l = []
|
|
|
|
for x, y in zip(a, b):
|
|
|
|
l.append(x)
|
|
|
|
l.append(y)
|
|
|
|
return l
|
|
|
|
|
2009-06-09 02:12:07 -04:00
|
|
|
def should_ignore_path(path, suffixes):
|
2009-07-23 10:04:12 -04:00
|
|
|
'''whether file (or any parent) should be ignored based on suffixes '''
|
2009-06-09 02:12:07 -04:00
|
|
|
for name in path.split('/'):
|
|
|
|
for suffix in suffixes:
|
|
|
|
if name.endswith(suffix):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2009-07-23 10:04:12 -04:00
|
|
|
def literal_path(path):
|
|
|
|
'''return the system path for the given user path'''
|
|
|
|
path = os.path.realpath(expand_tilde(path))
|
|
|
|
return os.path.abspath(path)
|
|
|
|
|
2008-12-04 12:16:41 -05:00
|
|
|
def normal_path(path):
|
2009-07-23 10:04:12 -04:00
|
|
|
'''return the user path for the given system path '''
|
|
|
|
path = os.path.normpath(path)
|
|
|
|
home = os.getenv('HOME')
|
2008-12-10 00:54:53 -05:00
|
|
|
isdir = os.path.isdir(path)
|
2008-12-04 12:16:41 -05:00
|
|
|
if path.startswith(home):
|
|
|
|
path = path.replace(home, '~', 1)
|
2008-12-10 00:54:53 -05:00
|
|
|
if isdir and not path.endswith('/'):
|
2009-07-23 10:04:12 -04:00
|
|
|
path += '/'
|
|
|
|
return path
|
2008-12-04 12:16:41 -05:00
|
|
|
|
2007-03-06 10:05:38 -05:00
|
|
|
def expand_tilde(path):
|
2009-07-23 10:04:12 -04:00
|
|
|
'''correctly expand the tilde in the provided path'''
|
|
|
|
isdir = path.endswith('/')
|
2007-03-06 10:05:38 -05:00
|
|
|
if not path.startswith('~'):
|
|
|
|
return path
|
|
|
|
parts = path.split('/', 1)
|
|
|
|
if parts[0] == '~':
|
|
|
|
parts[0] = os.getenv('HOME')
|
|
|
|
elif parts[0].startswith('~'):
|
2009-07-23 10:04:12 -04:00
|
|
|
try:
|
|
|
|
parts[0] = pwd.getpwnam(parts[0][1:])[5]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2007-03-06 10:05:38 -05:00
|
|
|
if len(parts) > 1:
|
|
|
|
s = os.path.join(parts[0], parts[1])
|
|
|
|
else:
|
|
|
|
s = parts[0]
|
|
|
|
s = os.path.realpath(s)
|
2009-07-23 10:04:12 -04:00
|
|
|
if os.path.isdir(s) and isdir:
|
2007-03-06 10:05:38 -05:00
|
|
|
s += '/'
|
|
|
|
return s
|
|
|
|
|
|
|
|
def count_leading_whitespace(s):
|
2009-07-23 10:04:12 -04:00
|
|
|
'''return the amount of leading whitespace of the provided string'''
|
2007-03-06 10:05:38 -05:00
|
|
|
m = regex.leading_whitespace.match(s)
|
|
|
|
assert m, "count leading whitespace failed somehow"
|
|
|
|
return m.end() - m.start()
|
2007-07-14 10:21:22 -04:00
|
|
|
|
2009-07-24 00:07:03 -04:00
|
|
|
def make_name(name, testf):
|
|
|
|
'''generate a new name, using testf() to see which names are taken'''
|
|
|
|
if testf(name):
|
|
|
|
i = 1
|
|
|
|
auxname = '%s/%d' % (name, i)
|
|
|
|
while testf(auxname):
|
|
|
|
i += 1
|
|
|
|
auxname = '%s/%d' % (name, i)
|
|
|
|
name = auxname
|
|
|
|
return name
|
|
|
|
|
2009-07-23 16:26:59 -04:00
|
|
|
# TODO: move these somewhere more sensible. they aren't really utilities.
|
2008-11-10 11:45:41 -05:00
|
|
|
def get_margin_limit(w, def_limit=80):
|
2009-03-17 15:24:10 -04:00
|
|
|
lname = '%s.margin' % w.mode.name.lower()
|
2008-11-10 11:45:41 -05:00
|
|
|
if lname in w.application.config:
|
|
|
|
return w.application.config[lname]
|
|
|
|
else:
|
|
|
|
return w.application.config.get('margin', def_limit)
|
2008-11-12 00:00:40 -05:00
|
|
|
|
|
|
|
def get_margin_color(w, def_color='blue'):
|
2009-03-17 15:24:10 -04:00
|
|
|
lname = '%s.margin_color' % w.mode.name.lower()
|
2008-11-12 00:00:40 -05:00
|
|
|
if lname in w.application.config:
|
|
|
|
return w.application.config[lname]
|
|
|
|
else:
|
|
|
|
return w.application.config.get('margin_color', def_color)
|
2009-03-09 23:46:49 -04:00
|
|
|
|
|
|
|
# emulate defaultdict for 2.4
|
|
|
|
try:
|
|
|
|
from collections import defaultdict
|
|
|
|
except:
|
|
|
|
class defaultdict(dict):
|
|
|
|
def __init__(self, default_factory=None, *a, **kw):
|
|
|
|
if (default_factory is not None and
|
|
|
|
not hasattr(default_factory, '__call__')):
|
|
|
|
raise TypeError('first argument must be callable')
|
|
|
|
dict.__init__(self, *a, **kw)
|
|
|
|
self.default_factory = default_factory
|
|
|
|
def __getitem__(self, key):
|
|
|
|
try:
|
|
|
|
return dict.__getitem__(self, key)
|
|
|
|
except KeyError:
|
|
|
|
return self.__missing__(key)
|
|
|
|
def __missing__(self, key):
|
|
|
|
if self.default_factory is None:
|
|
|
|
raise KeyError(key)
|
|
|
|
self[key] = value = self.default_factory()
|
|
|
|
return value
|
|
|
|
def __reduce__(self):
|
|
|
|
if self.default_factory is None:
|
|
|
|
args = tuple()
|
|
|
|
else:
|
|
|
|
args = self.default_factory,
|
2020-08-31 20:58:27 -04:00
|
|
|
return type(self), args, None, None, list(self.items())
|
2009-03-09 23:46:49 -04:00
|
|
|
def copy(self):
|
|
|
|
return self.__copy__()
|
|
|
|
def __copy__(self):
|
|
|
|
return type(self)(self.default_factory, self)
|
|
|
|
def __deepcopy__(self, memo):
|
|
|
|
import copy
|
|
|
|
return type(self)(self.default_factory,
|
2020-08-31 20:58:27 -04:00
|
|
|
copy.deepcopy(list(self.items())))
|
2009-03-09 23:46:49 -04:00
|
|
|
def __repr__(self):
|
|
|
|
return 'defaultdict(%s, %s)' % (self.default_factory,
|
|
|
|
dict.__repr__(self))
|
2009-08-26 10:07:48 -04:00
|
|
|
|
|
|
|
def decode(s):
|
2020-09-04 00:03:00 -04:00
|
|
|
return s
|
|
|
|
#for coding in ('utf-8', 'latin-1'):
|
|
|
|
# try:
|
|
|
|
# return s.decode(coding)
|
|
|
|
# except:
|
|
|
|
# pass
|
|
|
|
#return s.decode('ascii', 'replace')
|
2009-08-26 10:07:48 -04:00
|
|
|
|
2009-11-10 16:30:35 -05:00
|
|
|
def communicate(cmd, stdin=None, stderr=True, shell=False):
|
2009-08-26 10:07:48 -04:00
|
|
|
if stderr:
|
|
|
|
pipe = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=shell)
|
|
|
|
else:
|
|
|
|
pipe = Popen(cmd, stdout=PIPE, stderr=STDOUT, shell=shell)
|
|
|
|
out, err = pipe.communicate(input=stdin)
|
|
|
|
result = pipe.wait()
|
|
|
|
return result, decode(out or ''), decode(err or '')
|
2009-10-01 20:06:40 -04:00
|
|
|
|
|
|
|
def wrap_lines(s, limit):
|
|
|
|
'''given a string, return an iterator of wrapped lines'''
|
|
|
|
i = 0
|
|
|
|
while i + limit < len(s):
|
|
|
|
j = limit
|
|
|
|
while j > 0 and s[i + j] != ' ':
|
|
|
|
j -= 1
|
|
|
|
if j == 0:
|
|
|
|
yield s[i:i + limit]
|
|
|
|
i += limit
|
|
|
|
else:
|
|
|
|
yield s[i:i + j]
|
|
|
|
i += j + 1
|
|
|
|
if s[i:]:
|
|
|
|
yield s[i:]
|