2007-03-06 10:05:38 -05:00
|
|
|
import os, pwd, regex
|
|
|
|
|
2008-12-04 12:16:41 -05:00
|
|
|
def normal_path(path):
|
2009-03-06 09:21:52 -05:00
|
|
|
#path = os.path.realpath(path)
|
|
|
|
path = os.path.normpath(path)
|
2008-12-04 12:16:41 -05:00
|
|
|
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('/'):
|
2008-12-04 12:16:41 -05:00
|
|
|
return path + '/'
|
|
|
|
else:
|
|
|
|
return path
|
|
|
|
|
2007-03-06 10:05:38 -05:00
|
|
|
def expand_tilde(path):
|
2009-03-06 09:21:52 -05:00
|
|
|
isd = 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('~'):
|
|
|
|
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)
|
2009-03-06 09:21:52 -05:00
|
|
|
if os.path.isdir(s) and isd:
|
2007-03-06 10:05:38 -05:00
|
|
|
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):
|
2008-11-10 15:04:14 -05: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'):
|
|
|
|
lname = '%s.margin_color' % w.mode.name().lower()
|
|
|
|
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,
|
|
|
|
return type(self), args, None, None, self.items()
|
|
|
|
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,
|
|
|
|
copy.deepcopy(self.items()))
|
|
|
|
def __repr__(self):
|
|
|
|
return 'defaultdict(%s, %s)' % (self.default_factory,
|
|
|
|
dict.__repr__(self))
|