summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWouter Bolsterlee <uws@xs4all.nl>2016-03-28 20:13:42 +0200
committerWouter Bolsterlee <wouter@bolsterl.ee>2016-07-27 19:08:46 +0200
commitc788d830b1a9e1ad777fd97b59d990fbc4c0285d (patch)
tree46ca9960c265d457577895487ee3a05f55dc2ea0
parent76ed812f5f8efa90dd41d51de7952f4f15891ff2 (diff)
downloadhappybase-c788d830b1a9e1ad777fd97b59d990fbc4c0285d.tar.gz
Allow table names to be specified as binary or text
In HBase, the name is a byte string, but transparently encoding text using UTF-8 allows for cleaner code on the Python side. This makes code like connection.table("sometable") work on both Python 3 and 2.
-rw-r--r--happybase/connection.py17
1 files changed, 10 insertions, 7 deletions
diff --git a/happybase/connection.py b/happybase/connection.py
index 208c599..1dad7d6 100644
--- a/happybase/connection.py
+++ b/happybase/connection.py
@@ -13,7 +13,7 @@ from thriftpy.protocol import TBinaryProtocol, TCompactProtocol
from .Hbase_thrift import Hbase, ColumnDescriptor
from .table import Table
-from .util import pep8_to_camel_case
+from .util import ensure_bytes, pep8_to_camel_case
logger = logging.getLogger(__name__)
@@ -106,19 +106,21 @@ class Connection(object):
"""
def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, timeout=None,
autoconnect=True, table_prefix=None,
- table_prefix_separator='_', compat=DEFAULT_COMPAT,
+ table_prefix_separator=b'_', compat=DEFAULT_COMPAT,
transport=DEFAULT_TRANSPORT, protocol=DEFAULT_PROTOCOL):
if transport not in THRIFT_TRANSPORTS:
raise ValueError("'transport' must be one of %s"
% ", ".join(THRIFT_TRANSPORTS.keys()))
- if table_prefix is not None \
- and not isinstance(table_prefix, STRING_OR_BINARY):
- raise TypeError("'table_prefix' must be a string")
+ if table_prefix is not None:
+ if not isinstance(table_prefix, STRING_OR_BINARY):
+ raise TypeError("'table_prefix' must be a string")
+ table_prefix = ensure_bytes(table_prefix)
if not isinstance(table_prefix_separator, STRING_OR_BINARY):
raise TypeError("'table_prefix_separator' must be a string")
+ table_prefix_separator = ensure_bytes(table_prefix_separator)
if compat not in COMPAT_MODES:
raise ValueError("'compat' must be one of %s"
@@ -158,9 +160,9 @@ class Connection(object):
def _table_name(self, name):
"""Construct a table name by optionally adding a table name prefix."""
+ name = ensure_bytes(name)
if self.table_prefix is None:
return name
-
return self.table_prefix + self.table_prefix_separator + name
def open(self):
@@ -220,6 +222,7 @@ class Connection(object):
:return: Table instance
:rtype: :py:class:`Table`
"""
+ name = ensure_bytes(name)
if use_prefix:
name = self._table_name(name)
return Table(name, self)
@@ -241,7 +244,7 @@ class Connection(object):
# Filter using prefix, and strip prefix from names
if self.table_prefix is not None:
- prefix = self._table_name('')
+ prefix = self._table_name(b'')
offset = len(prefix)
names = [n[offset:] for n in names if n.startswith(prefix)]