<commit_before>import sys
import logging
import os
import tempfile
import time
import urllib
import urllib2
from ftplib import FTP
from xml.sax.saxutils import escape

from lxml import etree as ET


OUTPATH = tempfile.gettempdir() + '/'

logger = logging.getLogger(__name__)
if sys.version_info < (2, 7):
    class NullHandler(logging.Handler):
        def emit(self, record):
            pass
else:
    from logging import NullHandler

logger.addHandler(NullHandler())


class Engage(object):
    def __init__(self, api_url, username=None, password=<PASSWORD>, ftp_url=None, *args, **kwargs):

        self.api_url = api_url
        self.username = username
        self.password = password
        self.ftp_url = ftp_url
        self.jsessionid = None

    def login(self, username=None, password=<PASSWORD>):
        self.username = username if username else self.username
        self.password = password if password else self.password

        if not (self.username and self.password):
            raise ValueError("Engage.login() requires username and password be set.")

        xml_login = """
        <Envelope>
            <Body>
                <Login>
                    <USERNAME>{0}</USERNAME>
                    <PASSWORD>{1}</PASSWORD>
                </Login>
            </Body>
        </Envelope>
        """.format(self.username, escape(self.password))

        xml_resp = self._xml_request(self.api_url, xml_login)

        tree = ET.ElementTree(ET.fromstring(xml_resp))
        root = tree.getroot()

        res = root.xpath('/Envelope/Body/RESULT/SUCCESS/text()')[0]
        res = False if res.lower() == 'false' else True

        if res:
            self.jsessionid = root.xpath('/Envelope/Body/RESULT/SESSIONID/text()')[0]
            logger.info("Engage logged in, jsessionid:{0}".format(self.jsessionid))

        return res

    def logout(self):
        xml_logout = """
        <Envelope>
            <Body>
                <Logout/>
            </Body>
        </Envelope>
        """

        resp = self.xml_engage_request(xml_logout)

        if resp.SUCCESS:
            logger.info("Engage logged out, jsessionid:{0}".format(self.jsessionid))
            self.jsessionid = None

        return resp.SUCCESS

    def xml_engage_request(self, xml_req):
        if not self.jsessionid:
            self.login()

        api_url_jsid = self.api_url + ';jsessionid={0}'.format(self.jsessionid)

        tree = ET.ElementTree(ET.fromstring(xml_req))
        root = tree.getroot()

        callname = root.xpath('name(/Envelope/Body/*[1])')

        logger.debug("XML request for API call to '{0}':\n{1}".format(callname, xml_req))

        xml_resp = self._xml_request(api_url_jsid, xml_req)

        logger.debug("XML response for API call to '{0}':\n{1}".format(callname, xml_resp))

        return EngageResponse(xml_resp, callname, self)

    def ftp_putfile(self, filename):
        if not os.path.isfile(filename):
            logger.error("ftp_putfile: {0} not found.".format(filename))
            return False

        with open(filename, 'r') as f:
            if self.ftp_url:
                ftp = FTP(self.ftp_url)
            else:
                raise ValueError("Engage.ftp_putfile() requires ftp_url be set.")

            ftp.login(self.username, self.password)
            ftp.cwd('upload')
            fname = filename[((filename.rfind('/')) + 1):]
            ftp.storlines('STOR ' + fname, f)
            ftp.quit()

        logger.debug("ftp_putfile: stored {0}".format(filename))

        return True

    def ftp_getfile(self, filename, outfilepath):
        if self.ftp_url:
            ftp = FTP(self.ftp_url)
        else:
            raise ValueError("Engage.ftp_getfile() requires ftp_url be set.")

        with open(outfilepath, 'w') as of:
            ftp = FTP(self.ftp_url)
            ftp.login(self.username, self.password)
            #ftp.cwd('upload')
            #fname = filename[((filename.rfind('/'))+1):]<commit_msg>Add parameter for dest directory to ftp_putfile()<commit_after>import sys
import logging
import os
import tempfile
import time
import urllib
import urllib2
from ftplib import FTP
from xml.sax.saxutils import escape

from lxml import etree as ET


OUTPATH = tempfile.gettempdir() + '/'

logger = logging.getLogger(__name__)
if sys.version_info < (2, 7):
    class NullHandler(logging.Handler):
        def emit(self, record):
            pass
else:
    from logging import NullHandler

logger.addHandler(NullHandler())


class Engage(object):
    def __init__(self, api_url, username=None, password=<PASSWORD>, ftp_url=None, *args, **kwargs):

        self.api_url = api_url
        self.username = username
        self.password = password
        self.ftp_url = ftp_url
        self.jsessionid = None

    def login(self, username=None, password=None):
        self.username = username if username else self.username
        self.password = password if password else self.password

        if not (self.username and self.password):
            raise ValueError("Engage.login() requires username and password be set.")

        xml_login = """
        <Envelope>
            <Body>
                <Login>
                    <USERNAME>{0}</USERNAME>
                    <PASSWORD>{1}</PASSWORD>
                </Login>
            </Body>
        </Envelope>
        """.format(self.username, escape(self.password))

        xml_resp = self._xml_request(self.api_url, xml_login)

        tree = ET.ElementTree(ET.fromstring(xml_resp))
        root = tree.getroot()

        res = root.xpath('/Envelope/Body/RESULT/SUCCESS/text()')[0]
        res = False if res.lower() == 'false' else True

        if res:
            self.jsessionid = root.xpath('/Envelope/Body/RESULT/SESSIONID/text()')[0]
            logger.info("Engage logged in, jsessionid:{0}".format(self.jsessionid))

        return res

    def logout(self):
        xml_logout = """
        <Envelope>
            <Body>
                <Logout/>
            </Body>
        </Envelope>
        """

        resp = self.xml_engage_request(xml_logout)

        if resp.SUCCESS:
            logger.info("Engage logged out, jsessionid:{0}".format(self.jsessionid))
            self.jsessionid = None

        return resp.SUCCESS

    def xml_engage_request(self, xml_req):
        if not self.jsessionid:
            self.login()

        api_url_jsid = self.api_url + ';jsessionid={0}'.format(self.jsessionid)

        tree = ET.ElementTree(ET.fromstring(xml_req))
        root = tree.getroot()

        callname = root.xpath('name(/Envelope/Body/*[1])')

        logger.debug("XML request for API call to '{0}':\n{1}".format(callname, xml_req))

        xml_resp = self._xml_request(api_url_jsid, xml_req)

        logger.debug("XML response for API call to '{0}':\n{1}".format(callname, xml_resp))

        return EngageResponse(xml_resp, callname, self)

    def ftp_putfile(self, filename, to='upload', as_filename=None):
        result = None
        if not os.path.isfile(filename):
            logger.error("ftp_putfile: {0} not found.".format(filename))
            return result

        with open(filename, 'r') as f:
            if self.ftp_url:
                ftp = FTP(self.ftp_url)
            else:
                raise ValueError("Engage.ftp_putfile() requires ftp_url be set.")

            ftp.login(self.username, self.password)

            ftp.cwd(to)

            if not as_filename:
                fname = filename[((filename.rfind('/')) + 1):]
            else:
                fname = as_filename

            result = ftp.storlines('STOR ' + fname, f)
            ftp.quit()

        logger.debug("ftp_putfile: stored {0}".format(filename))

        return result

    def ftp_getfile(self, filename, outfilepath):
        if self.ftp_url:
            ftp = FTP(self.ftp_url)
        else:
            raise ValueError("Engage.ftp_getfile() requires ftp_url be set.")

        with open(outfilepath, 'w') as of:
            ftp = FTP(self.ftp_url)
            ftp.login(self.username, self.password)
            #ftp.cwd('upload')
            #fname = filename[((filename.rfind('/'))+1):]
{'fraction_non_alphanumeric': 0.12120841487279843, 'fraction_numerical': 0.004647749510763209, 'mean_word_length': 2.1620262954369682, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 38}, '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': '5130292', 'n_tokens_mistral': 2566, 'n_tokens_neox': 2449, 'n_words': 542}