2007-03-06 10:05:38 -05:00
|
|
|
import os, pwd, regex
|
|
|
|
|
|
|
|
def expand_tilde(path):
|
|
|
|
if not path.startswith('~'):
|
|
|
|
return path
|
|
|
|
parts = path.split('/', 1)
|
|
|
|
if parts[0] == '~':
|
|
|
|
parts[0] = os.getenv('HOME')
|
|
|
|
elif parts[0].startswith('~'):
|
|
|
|
users = [x[0] for x in pwd.getpwall()]
|
|
|
|
if parts[0][1:] in users:
|
|
|
|
home = pwd.getpwnam(parts[0][1:])[5]
|
|
|
|
parts[0] = home
|
|
|
|
if len(parts) > 1:
|
|
|
|
s = os.path.join(parts[0], parts[1])
|
|
|
|
else:
|
|
|
|
s = parts[0]
|
|
|
|
s = os.path.realpath(s)
|
|
|
|
if os.path.isdir(s):
|
|
|
|
s += '/'
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
def cleanse(s):
|
|
|
|
s2 = s.replace("\n", "")
|
|
|
|
return s2
|
|
|
|
|
|
|
|
def padtrunc(s, i, c=' '):
|
2007-08-19 21:58:02 -04:00
|
|
|
return s.ljust(i, c)[:i]
|
2007-03-06 10:05:38 -05:00
|
|
|
def pad(s, i, c=' '):
|
2007-08-19 21:58:02 -04:00
|
|
|
return s.ljust(i, c)
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
def count_leading_whitespace(s):
|
|
|
|
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
|
|
|
|
|
|
|
def dump(x):
|
|
|
|
d = {}
|
|
|
|
for name in dir(x):
|
|
|
|
d[name] = getattr(x, name)
|
|
|
|
return '%s: %r' % (x, d)
|
2008-11-10 11:45:41 -05:00
|
|
|
|
|
|
|
def get_margin_limit(w, def_limit=80):
|
|
|
|
lname = '%s.margin' % w.mode.name()
|
|
|
|
if lname in w.application.config:
|
|
|
|
return w.application.config[lname]
|
|
|
|
else:
|
|
|
|
return w.application.config.get('margin', def_limit)
|