-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update python examples #529
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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_column) | ||
from sqlalchemy.dialects.postgresql import BYTEA | ||
from acrawriter import create_acrastruct | ||
from common import get_engine, get_default, get_zone, register_common_cli_params | ||
|
@@ -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_column("'{}'".format(zone_id)), test_table])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about to avoid manual escaping values, use sqlalchemy's engine and specify There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good, let me check it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used sqlalchemy's |
||
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): | ||
|
@@ -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__': | ||
|
@@ -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), | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mention decoded value of base64 string to understand what we should expect: "test-data"