forked from snowflakedb/snowflake-connector-python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsfbinaryformat.py
38 lines (29 loc) · 1.08 KB
/
sfbinaryformat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012-2019 Snowflake Computing Inc. All right reserved.
#
from base64 import b16decode, b16encode, standard_b64encode
from .errors import InternalError
# Converts a Snowflake binary value into a "bytes" object.
binary_to_python = b16decode
def binary_to_snowflake(binary_value):
"""Encodes a "bytes" object for passing to Snowflake."""
result = b16encode(binary_value)
if isinstance(binary_value, bytearray):
return bytearray(result)
return result
class SnowflakeBinaryFormat(object):
"""Formats binary values ("bytes" objects) in hex or base64."""
def __init__(self, name):
name = name.upper()
if name == 'HEX':
self._encode = b16encode
elif name == 'BASE64':
self._encode = standard_b64encode
else:
raise InternalError(
'Unrecognized binary format {}'.format(name))
def format(self, binary_value):
"""Formats a "bytes" object, returning a string."""
return self._encode(binary_value).decode('ascii')