2012-04-29 22:09:16 +00:00
|
|
|
import os, re
|
2012-04-29 00:05:38 +00:00
|
|
|
|
2012-07-23 03:03:56 +00:00
|
|
|
SIZE_UNITS = dict(
|
|
|
|
b = 1024**0,
|
|
|
|
k = 1024**1,
|
|
|
|
m = 1024**2,
|
|
|
|
g = 1024**3,
|
|
|
|
t = 1024**4,
|
|
|
|
)
|
|
|
|
|
2013-01-05 02:25:09 +00:00
|
|
|
|
|
|
|
class MemBool:
|
|
|
|
"""
|
|
|
|
Truth-checking with a memory, for use in chained if statements.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self.v = None
|
|
|
|
|
|
|
|
def __call__(self, v):
|
|
|
|
self.v = v
|
|
|
|
return bool(v)
|
|
|
|
|
|
|
|
|
2012-07-23 03:03:56 +00:00
|
|
|
def parse_size(s):
|
|
|
|
try:
|
|
|
|
return int(s)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
for i in SIZE_UNITS.keys():
|
|
|
|
if s.endswith(i):
|
|
|
|
try:
|
|
|
|
return int(s[:-1]) * SIZE_UNITS[i]
|
|
|
|
except ValueError:
|
|
|
|
break
|
|
|
|
raise ValueError("Invalid size specification.")
|
|
|
|
|
|
|
|
|
2012-06-24 05:47:55 +00:00
|
|
|
def get_header(val, headers):
|
|
|
|
"""
|
|
|
|
Header keys may be Values, so we have to "generate" them as we try the match.
|
|
|
|
"""
|
2012-10-27 01:00:50 +00:00
|
|
|
for h in headers:
|
|
|
|
k = h.key.get_generator({})
|
2012-09-23 22:08:18 +00:00
|
|
|
if len(k) == len(val) and k[:].lower() == val.lower():
|
2012-10-27 01:00:50 +00:00
|
|
|
return h
|
2012-06-24 05:47:55 +00:00
|
|
|
return None
|
|
|
|
|
2012-04-29 00:05:38 +00:00
|
|
|
|
2012-06-24 04:38:32 +00:00
|
|
|
def parse_anchor_spec(s):
|
2012-04-29 00:05:38 +00:00
|
|
|
"""
|
2012-06-24 04:38:32 +00:00
|
|
|
Return a tuple, or None on error.
|
2012-04-29 00:05:38 +00:00
|
|
|
"""
|
|
|
|
if not "=" in s:
|
2012-06-24 04:38:32 +00:00
|
|
|
return None
|
|
|
|
return tuple(s.split("=", 1))
|
2012-04-29 00:05:38 +00:00
|
|
|
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-07-23 12:00:55 +00:00
|
|
|
def xrepr(s):
|
|
|
|
return repr(s)[1:-1]
|
|
|
|
|
|
|
|
|
2012-07-24 22:44:21 +00:00
|
|
|
def inner_repr(s):
|
|
|
|
"""
|
|
|
|
Returns the inner portion of a string or unicode repr (i.e. without the
|
|
|
|
quotes)
|
|
|
|
"""
|
|
|
|
if isinstance(s, unicode):
|
|
|
|
return repr(s)[2:-1]
|
|
|
|
else:
|
|
|
|
return repr(s)[1:-1]
|
|
|
|
|
|
|
|
|
2012-07-23 04:39:25 +00:00
|
|
|
def escape_unprintables(s):
|
2012-07-24 22:44:21 +00:00
|
|
|
"""
|
|
|
|
Like inner_repr, but preserves line breaks.
|
|
|
|
"""
|
2012-07-23 04:39:25 +00:00
|
|
|
s = s.replace("\r\n", "PATHOD_MARKER_RN")
|
|
|
|
s = s.replace("\n", "PATHOD_MARKER_N")
|
2012-07-24 22:44:21 +00:00
|
|
|
s = inner_repr(s)
|
2012-07-23 04:39:25 +00:00
|
|
|
s = s.replace("PATHOD_MARKER_RN", "\n")
|
|
|
|
s = s.replace("PATHOD_MARKER_N", "\n")
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
2012-04-28 00:42:03 +00:00
|
|
|
class Data:
|
|
|
|
def __init__(self, name):
|
|
|
|
m = __import__(name)
|
|
|
|
dirname, _ = os.path.split(m.__file__)
|
|
|
|
self.dirname = os.path.abspath(dirname)
|
|
|
|
|
|
|
|
def path(self, path):
|
|
|
|
"""
|
|
|
|
Returns a path to the package data housed at 'path' under this
|
|
|
|
module.Path can be a path to a file, or to a directory.
|
|
|
|
|
|
|
|
This function will raise ValueError if the path does not exist.
|
|
|
|
"""
|
|
|
|
fullpath = os.path.join(self.dirname, path)
|
|
|
|
if not os.path.exists(fullpath):
|
|
|
|
raise ValueError, "dataPath: %s does not exist."%fullpath
|
|
|
|
return fullpath
|
|
|
|
|
|
|
|
|
|
|
|
data = Data(__name__)
|