forked from snowflakedb/snowflake-connector-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfbinaryformat.py
44 lines (35 loc) · 1.13 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
39
40
41
42
43
44
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012-2018 Snowflake Computing Inc. All right reserved.
#
from base64 import (b16encode, standard_b64encode, b16decode)
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 == u'HEX':
self._encode = b16encode
elif name == u'BASE64':
self._encode = standard_b64encode
else:
raise InternalError(
u'Unrecognized binary format {}'.format(name))
def format(self, binary_value):
"""
Formats a "bytes" object, returning a string.
"""
return self._encode(binary_value).decode('ascii')