<commit_before>#
# s3-lifecycle-editor.py
# Mich, 2014-12-02
# Copyright (c) 2014 Datacratic Inc. All rights reserved.
#

import tempfile
import os
import sys
from functools import partial
from subprocess import call
from xml.etree import ElementTree
from xml.dom import minidom
from boto.exception import S3ResponseError
from boto.s3.connection import S3Connection
from boto.s3.lifecycle import Lifecycle
from boto.s3.lifecycle import Transition

class S3LifecycleEditor(object):
    def reset_xml(self, file_name, cycle_xml):
        f = open(file_name, 'wt')
        to_write = """<?xml version="1.0" ?>
<!--- Example
<LifecycleConfiguration>
    <Rule>
        <ID>sample-rule</ID>
        <Prefix>documents/</Prefix>
        <Status>Enabled</Status>
        <Transition>
            <Days>365</Days>
            <StorageClass>GLACIER</StorageClass>
        </Transition>
        <Expiration>
            <Days>3650</Days>
        </Expiration>
    </Rule>
    <Rule>
        ...
    </Rule>
</LifecycleConfiguration>
-->

"""
        xml_str = minidom.parseString(ElementTree.tostring(cycle_xml)) \
            .toprettyxml()
        to_write += xml_str[xml_str.find("\n") + 1:]
        f.write(to_write)
        f.close()
        return to_write

    def get_parse_error_what_to_do(self):
        reply = None
        while reply not in ["e", "r", "q"]:
            reply = raw_input(
"""Failed to parse XML. What do you want to do?"
[e] Edit
[r] Reset
[q] Quit
Command: """)
        return reply

    def get_transition_from_xml(self, x_transition):
        return Transition(days=x_transition.find("Days").text,
                        storage_class=x_transition.find("StorageClass").text)

    def get_rule_kwargs_from_xml(self, x_rule):
        _id = x_rule.find("ID").text
        if _id == "":
            _id = None
        return dict(
            id=_id,
            prefix=x_rule.find("Prefix").text,
            status=x_rule.find("Status").text,
            expiration=int(x_rule.find("Expiration/Days").text),
            transition=self.get_transition_from_xml(x_rule.find("Transition")))

    def __init__(self, s3_id, s3_key, bucket):
        conn = S3Connection(s3_id, s3_key)
        self.bucket = conn.get_bucket(bucket)
<commit_msg>Support no expiration<commit_after>#
# s3-lifecycle-editor.py
# Mich, 2014-12-02
# Copyright (c) 2014 Datacratic Inc. All rights reserved.
#

import tempfile
import os
import sys
from functools import partial
from subprocess import call
from xml.etree import ElementTree
from xml.dom import minidom
from boto.exception import S3ResponseError
from boto.s3.connection import S3Connection
from boto.s3.lifecycle import Lifecycle
from boto.s3.lifecycle import Transition

class S3LifecycleEditor(object):
    def reset_xml(self, file_name, cycle_xml):
        f = open(file_name, 'wt')
        to_write = """<?xml version="1.0" ?>
<!--- Example
<LifecycleConfiguration>
    <Rule>
        <ID>sample-rule</ID>
        <Prefix>documents/</Prefix>
        <Status>Enabled</Status>
        <Transition>
            <Days>365</Days>
            <StorageClass>GLACIER</StorageClass>
        </Transition>
        <Expiration>
            <Days>3650</Days>
        </Expiration>
    </Rule>
    <Rule>
        ...
    </Rule>
</LifecycleConfiguration>
-->

"""
        xml_str = minidom.parseString(ElementTree.tostring(cycle_xml)) \
            .toprettyxml()
        to_write += xml_str[xml_str.find("\n") + 1:]
        f.write(to_write)
        f.close()
        return to_write

    def get_parse_error_what_to_do(self):
        reply = None
        while reply not in ["e", "r", "q"]:
            reply = raw_input(
"""Failed to parse XML. What do you want to do?"
[e] Edit
[r] Reset
[q] Quit
Command: """)
        return reply

    def get_transition_from_xml(self, x_transition):
        return Transition(days=x_transition.find("Days").text,
                        storage_class=x_transition.find("StorageClass").text)

    def get_rule_kwargs_from_xml(self, x_rule):
        _id = x_rule.find("ID").text
        if _id == "":
            _id = None
        try:
            expiration = int(x_rule.find("Expiration/Days").text)
            if expiration == 0:
                expiration = None
        except AttributeError:
            expiration = None
        return dict(
            id=_id,
            prefix=x_rule.find("Prefix").text,
            status=x_rule.find("Status").text,
            expiration=expiration,
            transition=self.get_transition_from_xml(x_rule.find("Transition")))

    def __init__(self, s3_id, s3_key, bucket):
        conn = S3Connection(s3_id, s3_key)
        self.bucket = conn.get_bucket(bucket)

{'fraction_non_alphanumeric': 0.12085106382978723, 'fraction_numerical': 0.014680851063829787, 'mean_word_length': 2.479644707623982, 'pattern_counts': {'":': 2, '<': 51, '<?xml version=': 2, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 26}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2094425', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1477, 'n_words': 320}