Skip to content
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

fix formatting as per pep8 in xml_marshal.py #892

Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 32 additions & 26 deletions minio/xml_marshal.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@

_S3_NAMESPACE = 'http://s3.amazonaws.com/doc/2006-03-01/'


def xml_to_dict(in_xml):
# Converts xml to dict
elem = s3_xml.XML(in_xml)
return etree_to_dict(elem)


def etree_to_dict(elem):
# Converts ElementTree object to dict
ns = '{' + _S3_NAMESPACE + '}'
Expand All @@ -55,7 +57,7 @@ def etree_to_dict(elem):
for dc in map(etree_to_dict, children):
for k, v in dc.items():
dd[k].append(v)
d = {elem.tag: {k:v[0] if len(v) == 1 else v for k, v in dd.items()}}
d = {elem.tag: {k: v[0] if len(v) == 1 else v for k, v in dd.items()}}
if elem.attrib:
d[elem.tag].update(('@' + k, v) for k, v in elem.attrib.items())
if elem.text:
Expand All @@ -67,40 +69,38 @@ def etree_to_dict(elem):
d[elem.tag] = text
return d


def xml_marshal_bucket_encryption(rules):
kms_key_val = ''
sse_alg_val = 'AES256'
root = s3_xml.Element('ServerSideEncryptionConfiguration')
for r in rules:
rule_xml = s3_xml.SubElement(root, 'Rule')
apply_xml = s3_xml.SubElement(rule_xml, 'ApplyServerSideEncryptionByDefault')
if 'KMSMasterKeyID' in r['ApplyServerSideEncryptionByDefault'].keys():
kms_key_val = r['ApplyServerSideEncryptionByDefault']['KMSMasterKeyID']
if 'SSEAlgorithm' in r['ApplyServerSideEncryptionByDefault'].keys():
sse_alg_val = r['ApplyServerSideEncryptionByDefault']['SSEAlgorithm']
if kms_key_val != '':
kms = s3_xml.SubElement(apply_xml, 'KMSMasterKeyID')
kms.text = kms_key_val

if rules:
# As server supports only one rule, the first rule is taken due to
# no validation is done at server side.
apply_xml = s3_xml.SubElement(s3_xml.SubElement(root, 'Rule'),
'ApplyServerSideEncryptionByDefault')
sse = s3_xml.SubElement(apply_xml, 'SSEAlgorithm')
sse.text = sse_alg_val
# 'break' the loop as soon as the first entry in the list is
# processed. That is because, 'Rule" list cannot have more than
# one entry for the time-being, even if there are more entries
# in the list. This is a server side restriction and it is
# validated on the server side.
break
sse.text = rules[0]['ApplyServerSideEncryptionByDefault'].get(
'SSEAlgorithm', 'AES256')
kms_text = rules[0]['ApplyServerSideEncryptionByDefault'].get(
'KMSMasterKeyID')
if kms_text:
kms = s3_xml.SubElement(apply_xml, 'KMSMasterKeyID')
kms.text = kms_text

data = io.BytesIO()
s3_xml.ElementTree(root).write(data, encoding=None, xml_declaration=False)
return data.getvalue()


def xml_marshal_bucket_constraint(region):
"""
Marshal's bucket constraint based on *region*.

:param region: Region name of a given bucket.
:return: Marshalled XML data.
"""
root = s3_xml.Element('CreateBucketConfiguration', {'xmlns': _S3_NAMESPACE})
root = s3_xml.Element('CreateBucketConfiguration',
{'xmlns': _S3_NAMESPACE})
location_constraint = s3_xml.SubElement(root, 'LocationConstraint')
location_constraint.text = region
data = io.BytesIO()
Expand All @@ -117,7 +117,8 @@ def xml_marshal_select(opts):
input_serialization = s3_xml.SubElement(root, 'InputSerialization')

if opts.in_ser.csv_input is not None:
compression_type = s3_xml.SubElement(input_serialization, 'CompressionType')
compression_type = s3_xml.SubElement(
input_serialization, 'CompressionType')
compression_type.text = opts.in_ser.compression_type
csv = s3_xml.SubElement(input_serialization, 'CSV')
file_header_info = s3_xml.SubElement(csv, 'FileHeaderInfo')
Expand All @@ -132,18 +133,21 @@ def xml_marshal_select(opts):
quote_escape_character.text = opts.in_ser.csv_input.QuoteEscapeCharacter
comments = s3_xml.SubElement(csv, 'Comments')
comments.text = opts.in_ser.csv_input.Comments
allow_quoted_record_delimiter = s3_xml.SubElement(csv, 'AllowQuotedRecordDelimiter')
allow_quoted_record_delimiter = s3_xml.SubElement(
csv, 'AllowQuotedRecordDelimiter')
allow_quoted_record_delimiter.text = opts.in_ser.csv_input.AllowQuotedRecordDelimiter.lower()

if opts.in_ser.json_input is not None:
compression_type = s3_xml.SubElement(input_serialization, 'CompressionType')
compression_type = s3_xml.SubElement(
input_serialization, 'CompressionType')
compression_type.text = opts.in_ser.compression_type
json = s3_xml.SubElement(input_serialization, 'JSON')
type_input = s3_xml.SubElement(json, 'Type')
type_input.text = opts.in_ser.json_input.Type

if opts.in_ser.parquet_input is not None:
compression_type = s3_xml.SubElement(input_serialization, 'CompressionType')
compression_type = s3_xml.SubElement(
input_serialization, 'CompressionType')
compression_type.text = opts.in_ser.compression_type
s3_xml.SubElement(input_serialization, 'Parquet')

Expand Down Expand Up @@ -264,7 +268,8 @@ def xml_marshal_bucket_notifications(notifications):

:return: Marshalled XML data
"""
root = s3_xml.Element('NotificationConfiguration', {'xmlns': _S3_NAMESPACE})
root = s3_xml.Element('NotificationConfiguration',
{'xmlns': _S3_NAMESPACE})
_add_notification_config_to_xml(
root,
'TopicConfiguration',
Expand All @@ -285,6 +290,7 @@ def xml_marshal_bucket_notifications(notifications):
s3_xml.ElementTree(root).write(data, encoding=None, xml_declaration=False)
return data.getvalue()


NOTIFICATIONS_ARN_FIELDNAME_MAP = {
'TopicConfiguration': 'Topic',
'QueueConfiguration': 'Queue',
Expand Down