<commit_before>import os
import hashlib
import json
from getpass import getpass

from .. import api
from ..api.errors import AuthenticationFailed

from .utils import mkdir

import logging
log = logging.getLogger(__name__)


class Authenticator(object):
    def __init__(self, token_dir):
        mkdir(token_dir)
        url_hash = hashlib.md5(api.base_url()).hexdigest()
        self.token_file = os.path.join(token_dir, url_hash)

    def authenticate(self):
        loaded_data = self.load_user_data()

        if loaded_data:
            try:
                client = api.with_token(loaded_data["token"])

                if "username" not in loaded_data:
                    log.debug("Found API token but no other data - requesting")
                    extra_data = client.customer_data()
                    loaded_data.update(extra_data)
                    self.store_user_data(loaded_data)

                client.username = loaded_data["username"]

                return client
            except AuthenticationFailed:
                log.error("Oh dear, looks like your API token has expired. We'll need to log you in again.")

        return self.login()

    def login(self, prompt=None):
        if prompt is None:
            prompt = 'Orchard username: '

        username = raw_input(prompt)
<commit_msg>Use ORCHARD_API_KEY environment variable to authenticate, if present<commit_after>import os
import hashlib
import json
from getpass import getpass

from .. import api
from ..api.errors import AuthenticationFailed

from .utils import mkdir

import logging
log = logging.getLogger(__name__)


class Authenticator(object):
    def __init__(self, token_dir):
        mkdir(token_dir)
        url_hash = hashlib.md5(api.base_url()).hexdigest()
        self.token_file = os.path.join(token_dir, url_hash)

    def authenticate(self):
        if 'ORCHARD_API_KEY' in os.environ:
            return api.with_token(os.environ['ORCHARD_API_KEY'])

        loaded_data = self.load_user_data()

        if loaded_data:
            try:
                client = api.with_token(loaded_data["token"])

                if "username" not in loaded_data:
                    log.debug("Found API token but no other data - requesting")
                    extra_data = client.customer_data()
                    loaded_data.update(extra_data)
                    self.store_user_data(loaded_data)

                client.username = loaded_data["username"]

                return client
            except AuthenticationFailed:
                log.error("Oh dear, looks like your API token has expired. We'll need to log you in again.")

        return self.login()

    def login(self, prompt=None):
        if prompt is None:
            prompt = 'Orchard username: '

        username = raw_input(prompt)

{'fraction_non_alphanumeric': 0.08069676501955209, 'fraction_numerical': 0.0007109847138286527, 'mean_word_length': 2.20501138952164, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '7292694', 'n_tokens_mistral': 796, 'n_tokens_neox': 743, 'n_words': 209}