2015-11-12 01:41:42 +00:00
|
|
|
from __future__ import absolute_import, print_function, division
|
2015-06-23 10:16:03 +00:00
|
|
|
import os.path
|
2015-08-05 19:32:53 +00:00
|
|
|
import re
|
2016-02-15 15:34:38 +00:00
|
|
|
import importlib
|
|
|
|
import inspect
|
2016-02-08 03:16:58 +00:00
|
|
|
|
2015-04-24 03:09:21 +00:00
|
|
|
|
|
|
|
def setbit(byte, offset, value):
|
|
|
|
"""
|
|
|
|
Set a bit in a byte to 1 if value is truthy, 0 if not.
|
|
|
|
"""
|
|
|
|
if value:
|
|
|
|
return byte | (1 << offset)
|
|
|
|
else:
|
|
|
|
return byte & ~(1 << offset)
|
|
|
|
|
|
|
|
|
|
|
|
def getbit(byte, offset):
|
|
|
|
mask = 1 << offset
|
2015-09-17 13:16:12 +00:00
|
|
|
return bool(byte & mask)
|
2015-04-29 21:04:22 +00:00
|
|
|
|
|
|
|
|
2015-06-17 11:10:27 +00:00
|
|
|
class BiDi(object):
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2015-04-29 21:04:22 +00:00
|
|
|
"""
|
|
|
|
A wee utility class for keeping bi-directional mappings, like field
|
2015-04-30 00:10:08 +00:00
|
|
|
constants in protocols. Names are attributes on the object, dict-like
|
|
|
|
access maps values to names:
|
2015-04-29 21:04:22 +00:00
|
|
|
|
|
|
|
CONST = BiDi(a=1, b=2)
|
|
|
|
assert CONST.a == 1
|
2015-04-30 00:10:08 +00:00
|
|
|
assert CONST.get_name(1) == "a"
|
2015-04-29 21:04:22 +00:00
|
|
|
"""
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2015-04-29 21:04:22 +00:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self.names = kwargs
|
|
|
|
self.values = {}
|
2015-11-12 01:41:42 +00:00
|
|
|
for k, v in kwargs.items():
|
2015-04-29 21:04:22 +00:00
|
|
|
self.values[v] = k
|
|
|
|
if len(self.names) != len(self.values):
|
|
|
|
raise ValueError("Duplicate values not allowed.")
|
|
|
|
|
|
|
|
def __getattr__(self, k):
|
|
|
|
if k in self.names:
|
|
|
|
return self.names[k]
|
|
|
|
raise AttributeError("No such attribute: %s", k)
|
|
|
|
|
2015-04-30 00:10:08 +00:00
|
|
|
def get_name(self, n, default=None):
|
|
|
|
return self.values.get(n, default)
|
|
|
|
|
|
|
|
|
2015-06-23 10:16:03 +00:00
|
|
|
class Data(object):
|
2015-08-10 18:44:36 +00:00
|
|
|
|
2015-06-23 10:16:03 +00:00
|
|
|
def __init__(self, name):
|
2016-02-15 15:34:38 +00:00
|
|
|
m = importlib.import_module(name)
|
|
|
|
dirname = os.path.dirname(inspect.getsourcefile(m))
|
2015-06-23 10:16:03 +00:00
|
|
|
self.dirname = os.path.abspath(dirname)
|
|
|
|
|
2016-07-14 04:20:27 +00:00
|
|
|
def push(self, subpath):
|
|
|
|
"""
|
|
|
|
Change the data object to a path relative to the module.
|
|
|
|
"""
|
|
|
|
self.dirname = os.path.join(self.dirname, subpath)
|
|
|
|
return self
|
|
|
|
|
2015-06-23 10:16:03 +00:00
|
|
|
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.
|
|
|
|
"""
|
2016-02-15 15:34:38 +00:00
|
|
|
fullpath = os.path.join(self.dirname, path)
|
2015-06-23 10:16:03 +00:00
|
|
|
if not os.path.exists(fullpath):
|
|
|
|
raise ValueError("dataPath: %s does not exist." % fullpath)
|
|
|
|
return fullpath
|
2015-07-29 09:27:43 +00:00
|
|
|
|
|
|
|
|
2015-09-15 17:12:15 +00:00
|
|
|
_label_valid = re.compile(b"(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE)
|
2015-08-01 08:39:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_valid_host(host):
|
2016-07-07 04:03:17 +00:00
|
|
|
# type: (bytes) -> bool
|
2015-09-20 17:40:09 +00:00
|
|
|
"""
|
|
|
|
Checks if a hostname is valid.
|
|
|
|
"""
|
2015-08-01 08:39:14 +00:00
|
|
|
try:
|
|
|
|
host.decode("idna")
|
|
|
|
except ValueError:
|
|
|
|
return False
|
2015-09-15 17:12:15 +00:00
|
|
|
if len(host) > 255:
|
|
|
|
return False
|
2015-09-17 13:16:12 +00:00
|
|
|
if host[-1] == b".":
|
2015-09-15 17:12:15 +00:00
|
|
|
host = host[:-1]
|
|
|
|
return all(_label_valid.match(x) for x in host.split(b"."))
|
|
|
|
|
|
|
|
|
|
|
|
def is_valid_port(port):
|
|
|
|
return 0 <= port <= 65535
|