Skip to content

Commit

Permalink
Update python examples (#529)
Browse files Browse the repository at this point in the history
* zhars/update_python_examples

Update python examples
  • Loading branch information
Zhaars authored Apr 28, 2022
1 parent a728f75 commit 1f2d55a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
10 changes: 10 additions & 0 deletions examples/python/encryptor_config_with_zone.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,13 @@ schemas:
encrypted:
- column: email

- table: test_example_with_zone
columns:
- id
- data
- raw_data
encrypted:
- column: data
data_type: bytes
# base64 bytes value `test-data`
default_data_value: dGVzdC1kYXRhCg==
13 changes: 12 additions & 1 deletion examples/python/encryptor_config_without_zone.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,15 @@ schemas:
- id
- email
encrypted:
- column: email
- column: email

- table: test_example_without_zone
columns:
- id
- data
- raw_data
encrypted:
- column: data
data_type: bytes
# base64 bytes value `test-data`
default_data_value: dGVzdC1kYXRhCg
14 changes: 5 additions & 9 deletions examples/python/example_with_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# coding: utf-8
import argparse
import ssl
from sqlalchemy import (Table, Column, Integer, MetaData, select, LargeBinary, Text, cast)
from sqlalchemy import (Table, Column, Integer, MetaData, select, LargeBinary, Text, literal)
from sqlalchemy.dialects.postgresql import BYTEA
from acrawriter import create_acrastruct
from common import get_engine, get_default, get_zone, register_common_cli_params
Expand All @@ -25,16 +25,16 @@ def print_data(zone_id, connection):
console"""
result = connection.execute(
# explicitly pass zone id before related data
select([cast(zone_id.encode('utf-8'), BYTEA), test_table]))
select([literal(zone_id), test_table]))
result = result.fetchall()
ZONE_ID_INDEX = 0
print("use zone_id: ", zone_id)
print("{:<3} - {} - {} - {}".format("id", 'zone', "data", "raw_data"))
for row in result:
print(
"{:<3} - {} - {} - {}\n".format(
row['id'], row[ZONE_ID_INDEX].decode('utf-8'),
row['data'].decode('utf-8', errors='ignore'), row['raw_data']))
row['id'], row[ZONE_ID_INDEX],
row['data'].decode('utf-8', errors='ignore').rstrip(), row['raw_data']))


def write_data(data, connection, sslcontext=None):
Expand All @@ -45,10 +45,7 @@ def write_data(data, connection, sslcontext=None):
encrypted_data = create_acrastruct(
data.encode('utf-8'), key, zone_id.encode('utf-8'))

connection.execute(
test_table.insert(), data=encrypted_data,
zone_id=zone_id.encode('utf-8'),
raw_data=data)
connection.execute(test_table.insert(), data=encrypted_data, raw_data=data)


if __name__ == '__main__':
Expand All @@ -67,7 +64,6 @@ def write_data(data, connection, sslcontext=None):
test_table = Table(
'test_example_with_zone', metadata,
Column('id', Integer, primary_key=True, nullable=False),
Column('zone_id', LargeBinary, nullable=True),
Column('data', LargeBinary, nullable=False),
Column('raw_data', Text, nullable=False),
)
Expand Down

0 comments on commit 1f2d55a

Please sign in to comment.