<commit_before># -*- coding: utf-8 -*-

import sys

import requests

from lxml import etree
from django_lithium_api import settings as lithium_settings
from django_lithium_api import exceptions, types


# supported API methods

API_PATHS = {
    'GET': {
        'auth_user_id': 'authentication/sessions/current/id',
        'auth_user': 'authentication/sessions/current/user',

        'boards': 'boards',
        'boards_nested': 'boards/nested',
        'messages_linear': '/boards/id/%(board_id)s/messages/linear',

        'events': 'events',
        'events_types': 'events/types',
        'events_subscriptions': 'events/subscriptions',

    },
    'POST': {
        'auth_login': 'authentication/sessions/login',
        'auth_logout': 'authentication/sessions/logout',

        'events_subscribe': 'events/subscriptions/events/name/%(event_type)s/subscribe',
        'events_unsubscribe': 'events/subscriptions/token/%(subscription_token)s/unsubscribe',
        'events_unsubscribe_all': 'events/subscriptions/users/self/unsubscribe',
    }
}



class LithiumApi(object):
    def __init__(self, entry_point=None):
        self.entry_point = entry_point or lithium_settings.get('ENTRY_POINT')
        self.session_key = ''
        if lithium_settings.get('HTTP_USER'):
            auth = (lithium_settings.get('HTTP_USER'), lithium_settings.get('HTTP_PASSWORD'))
        else:
            auth = ()
        if lithium_settings.get('DEBUG'):
            config =  {'verbose': sys.stderr}
        else:
            config = {}
        self.session = requests.session(auth=auth, config=config)

    def authenticate(self, username, password):
        self.session_key = self('auth_login', {'user.login': username, 'user.password': password})

    def logout(self):
        if self.session_key:
            self('auth_logout')
            self.session_key = None

    def __call__(self, method, data=None, **kwargs):
        if data is None:
            data = {}
        if self.session_key:
            data['restapi.session_key'] = self.session_key
        if method in API_PATHS['GET']:
            handler = self.session.get
            request_kwargs = {'params': data}
            path = API_PATHS['GET'][method]
        elif method in API_PATHS['POST']:
            handler = self.session.post
            request_kwargs = {'data': data}
            path = API_PATHS['POST'][method]
        else:
            raise exceptions.MethodNotSupported(method)
        try:
            path = path % kwargs
        except KeyError as e:
            raise exceptions.ArgumentsMissing(e.message)
        url = self.entry_point + path
        response = handler(url, **request_kwargs)
        return self.handle_response(response.content)

    def get_obj_from_url(self, url):
        params = {}
        if self.session_key:
            params['restapi.session_key'] = self.session_key
        url = self.entry_point[:-1] + url
        response = self.session.get(url, params=params)
        return self.handle_response(response.content)

    def handle_response(self, response):
        if lithium_settings.get('DEBUG'):
            print response
        if response:
            tree = etree.fromstring(response)
            if tree.get('status') == 'success':
                elem = tree.getchildren()
                if not elem:
                    return None
                elem = elem[0]
                return types.xml_to_type(elem, self)
            else:
                raise exceptions.RemoteException(response)

api = LithiumApi()<commit_msg>added ability to change debug mode during runtime<commit_after># -*- coding: utf-8 -*-

import sys

import requests

from lxml import etree
from django_lithium_api import settings as lithium_settings
from django_lithium_api import exceptions, types


# supported API methods

API_PATHS = {
    'GET': {
        'auth_user_id': 'authentication/sessions/current/id',
        'auth_user': 'authentication/sessions/current/user',

        'boards': 'boards',
        'boards_nested': 'boards/nested',
        'messages_linear': '/boards/id/%(board_id)s/messages/linear',

        'events': 'events',
        'events_types': 'events/types',
        'events_subscriptions': 'events/subscriptions',

    },
    'POST': {
        'auth_login': 'authentication/sessions/login',
        'auth_logout': 'authentication/sessions/logout',

        'events_subscribe': 'events/subscriptions/events/name/%(event_type)s/subscribe',
        'events_unsubscribe': 'events/subscriptions/token/%(subscription_token)s/unsubscribe',
        'events_unsubscribe_all': 'events/subscriptions/users/self/unsubscribe',
    }
}


class LithiumApi(object):
    def __init__(self, entry_point=None, debug=None):
        self.entry_point = entry_point or lithium_settings.get('ENTRY_POINT')
        self.session_key = ''
        if lithium_settings.get('HTTP_USER'):
            auth = (lithium_settings.get('HTTP_USER'), lithium_settings.get('HTTP_PASSWORD'))
        else:
            auth = ()
        self.session = requests.session(auth=auth, config={})
        self.set_debug(lithium_settings.get('DEBUG') if debug is None else debug)

    def authenticate(self, username, password):
        self.session_key = self('auth_login', {'user.login': username, 'user.password': password})

    def logout(self):
        if self.session_key:
            self('auth_logout')
            self.session_key = None

    def __call__(self, method, data=None, **kwargs):
        if data is None:
            data = {}
        if self.session_key:
            data['restapi.session_key'] = self.session_key
        if method in API_PATHS['GET']:
            handler = self.session.get
            request_kwargs = {'params': data}
            path = API_PATHS['GET'][method]
        elif method in API_PATHS['POST']:
            handler = self.session.post
            request_kwargs = {'data': data}
            path = API_PATHS['POST'][method]
        else:
            raise exceptions.MethodNotSupported(method)
        try:
            path = path % kwargs
        except KeyError as e:
            raise exceptions.ArgumentsMissing(e.message)
        url = self.entry_point + path
        response = handler(url, **request_kwargs)
        return self.handle_response(response.content)

    def get_obj_from_url(self, url):
        params = {}
        if self.session_key:
            params['restapi.session_key'] = self.session_key
        url = self.entry_point[:-1] + url
        response = self.session.get(url, params=params)
        return self.handle_response(response.content)

    def handle_response(self, response):
        if self._debug:
            sys.stderr.write(response)
        if response:
            tree = etree.fromstring(response)
            if tree.get('status') == 'success':
                elem = tree.getchildren()
                if not elem:
                    return None
                elem = elem[0]
                return types.xml_to_type(elem, self)
            else:
                raise exceptions.RemoteException(response)

    def set_debug(self, debug):
        """
        Changes the ``debug`` mode of the API. When set to ``True``, HTTP
        requests and XML responses are printed to STDERR
        """
        self._debug = debug
        if debug:
            self.session.config['verbose'] = sys.stderr
        else:
            self.session.config['verbose'] = None

api = LithiumApi()
{'fraction_non_alphanumeric': 0.115678136296693, 'fraction_numerical': 0.000803320390949257, 'mean_word_length': 2.35278276481149, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '6479068', 'n_tokens_mistral': 2133, 'n_tokens_neox': 2078, 'n_words': 442}