<commit_before>import requests
import keyring
import getpass
import re
import lxml.html as html
from cStringIO import StringIO

from astropy.table import Table

from ..query import QueryWithLogin

class EsoClass(QueryWithLogin):
    
    def __init__(self):
        self.session = requests.Session()
    
    def _activate_form(self, response, form_index=0, inputs={}):
        root = html.document_fromstring(response.content)
        form = root.forms[form_index]
        #form_dict = dict(form.form_values())
        form_dict = {}
        files_dict = []
        for key in form.inputs.keys():
            if 'type' in form.inputs[key].attrib.keys():
                typ = form.inputs[key].attrib['type']
            else:
                typ = None
            if typ == 'file':
                if form.inputs[key].value is None:
                    files_dict += [(key, ("", "", "application/octet-stream"))]
            else:
                if (form.inputs[key].value != '') and (form.inputs[key].value != None):
                    form_dict[key] = form.inputs[key].value
                if isinstance(form.inputs[key], html.SelectElement):
                    form_dict[key] = form.inputs[key].value_options[0]
        #
        for key in inputs.keys():
            form_dict[key] = inputs[key]
        if "://" in form.action:
            url = form.action
        elif form.action[0] == "/":
            url = '/'.join(response.url.split('/',3)[:3]) + form.action
        else:
            url = response.url.rsplit('/',1)[0] + '/' + form.action
        if form.method == 'GET':
            response = self.session.get(url, params=form_dict)
        elif form.method == 'POST':
            if 'enctype' in form.attrib:
                if form.attrib['enctype'] == 'multipart/form-data':
                    response = self.session.post(url, data=form_dict, files=files_dict)
                elif form.attrib['enctype'] == 'application/x-www-form-urlencoded':
                    response = self.session.post(url, data=form_dict)
                else:
                    raise Exception("Not implemented: enctype={}".format(form.attrib['enctype']))
            else:
                response = self.session.post(url, params=form_dict)
        else:
            raise Exception("Unknown form method: {}".format(form.method))
        return response
    
    def login(self, username):
        #Get password from keyring or prompt
        password_from_keyring = keyring.get_password("astroquery:www.eso.org", username)
        if password_from_keyring is None:
            password = getpass.getpass("{}, enter your ESO password:\n".format(username))
        else:
            password = password_from_keyring
        #Authenticate<commit_msg>Converted form_dict from dictionary to list. Added handling for CheckboGroup.<commit_after>import requests
import keyring
import getpass
import re
import lxml.html as html
from cStringIO import StringIO

from astropy.table import Table

from ..query import QueryWithLogin

class EsoClass(QueryWithLogin):
    
    def __init__(self):
        self.session = requests.Session()
    
    def _activate_form(self, response, form_index=0, inputs={}):
        root = html.document_fromstring(response.content)
        form = root.forms[form_index]
        form_dict = []
        files_dict = []
        for key in form.inputs.keys():
            if isinstance(form.inputs[key], html.CheckboxGroup):
                for item in form.inputs[key]:
                    form_dict += [(key, item.value)]
            else:
                if 'type' in form.inputs[key].attrib.keys():
                    typ = form.inputs[key].attrib['type']
                else:
                    typ = None
                if typ == 'file':
                    if form.inputs[key].value is None:
                        files_dict += [(key, ("", "", "application/octet-stream"))]
                else:
                    if (form.inputs[key].value != '') and (form.inputs[key].value != None):
                        form_dict += [(key, form.inputs[key].value)]
                    if isinstance(form.inputs[key], html.SelectElement):
                        form_dict += [(key, form.inputs[key].value_options[0])]
        #
        for key in inputs.keys():
            form_dict += [(key, inputs[key])]
        if "://" in form.action:
            url = form.action
        elif form.action[0] == "/":
            url = '/'.join(response.url.split('/',3)[:3]) + form.action
        else:
            url = response.url.rsplit('/',1)[0] + '/' + form.action
        if form.method == 'GET':
            response = self.session.get(url, params=form_dict)
        elif form.method == 'POST':
            if 'enctype' in form.attrib:
                if form.attrib['enctype'] == 'multipart/form-data':
                    response = self.session.post(url, data=form_dict, files=files_dict)
                elif form.attrib['enctype'] == 'application/x-www-form-urlencoded':
                    response = self.session.post(url, data=form_dict)
                else:
                    raise Exception("Not implemented: enctype={}".format(form.attrib['enctype']))
            else:
                response = self.session.post(url, params=form_dict)
        else:
            raise Exception("Unknown form method: {}".format(form.method))
        return response
    
    def login(self, username):
        #Get password from keyring or prompt
        password_from_keyring = keyring.get_password("astroquery:www.eso.org", username)
        if password_from_keyring is None:
            password = getpass.getpass("{}, enter your ESO password:\n".format(username))
        else:
            password = password_from_keyring
        #Authenticate
{'fraction_non_alphanumeric': 0.12610427853802184, 'fraction_numerical': 0.002425082279577343, 'mean_word_length': 1.944416114227435, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 2}, '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': '5862908', 'n_tokens_mistral': 1628, 'n_tokens_neox': 1576, 'n_words': 356}