<commit_before># -*- coding: utf-8 -*-
# pylint: disable=bad-continuation
""" Security / AuthN / AuthZ helpers.
"""
# Copyright ©  2015 <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import, unicode_literals, print_function

import os
import re
import sys
import errno
import getpass
from netrc import netrc, NetrcParseError

try:
    import keyring
except ImportError:
    keyring = None

from ._compat import urlparse

__all__ = ['Credentials']


class Credentials(object):
    """Look up and provide authN credentials (username / password) from common sources."""

    URL_RE = re.compile(r'^(http|https|ftp|ftps)://')  # covers the common use cases
    NETRC_FILE = None  # use the default, unless changed for test purposes
    AUTH_MEMOIZE_INPUT = None, None  # remember manual auth input across several queries in one run

    def __init__(self, target):
        """``target`` is a representation of the secured object, typically an URL."""
        self.target = target
        self.user = None
        self.password = None

    def auth_valid(self):
        """Return bool indicating whether full credentials were provided."""
        return bool(self.user and self.password)

    def auth_pair(self):
        """Return username/password tuple, possibly prompting the user for them."""
        if not self.auth_valid():
            self._get_auth()
        return (self.user, self.password)

    def _get_auth(self):
        """Try to get login auth from known sources."""
        if self.URL_RE.match(self.target):
            auth_url = urlparse(self.target)
            if auth_url.username:
                self.user = auth_url.username
            if auth_url.password:
                self.password = auth_url.password
            if not self.auth_valid():
                self._get_auth_from_keyring(auth_url.hostname)
            if not self.auth_valid():
                self._get_auth_from_netrc(auth_url.hostname)
            if not self.auth_valid():
                self._get_auth_from_console(auth_url.hostname)

    def _get_auth_from_console(self, realm):
        """Prompt for the user and password."""
        self.user, self.password = self.AUTH_MEMOIZE_INPUT
        if not self.auth_valid():
            login = getpass.getuser()
            self.user = raw_input('Username for {} [{}]: '.format(realm, login)) or login
            self.password = get<PASSWORD>.getpass('Password: ')
            Credentials.AUTH_MEMOIZE_INPUT = self.user, self.password

    def _get_auth_from_netrc(self, hostname):
        """Try to find login auth in ``~/.netrc``."""
        try:
            hostauth = netrc(self.NETRC_FILE)
        except IOError as cause:
            if cause.errno != errno.ENOENT:
                raise
            return
        except NetrcParseError as cause:
            raise  # TODO: Map to common base class, so caller has to handle less error types?

        # Try to find specific `user@host` credentials first, then just `host`
        auth = hostauth.hosts.get('{}@{}'.format(self.user or getpass.getuser(), hostname), None)<commit_msg>Credentials: fall back to console prompt for anything not an URL<commit_after># -*- coding: utf-8 -*-
# pylint: disable=bad-continuation
""" Security / AuthN / AuthZ helpers.
"""
# Copyright ©  2015 <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import, unicode_literals, print_function

import os
import re
import sys
import errno
import getpass
from netrc import netrc, NetrcParseError

try:
    import keyring
except ImportError:
    keyring = None

from ._compat import urlparse

__all__ = ['Credentials']


class Credentials(object):
    """Look up and provide authN credentials (username / password) from common sources."""

    URL_RE = re.compile(r'^(http|https|ftp|ftps)://')  # covers the common use cases
    NETRC_FILE = None  # use the default, unless changed for test purposes
    AUTH_MEMOIZE_INPUT = None, None  # remember manual auth input across several queries in one run

    def __init__(self, target):
        """``target`` is a representation of the secured object, typically an URL."""
        self.target = target
        self.user = None
        self.password = <PASSWORD>

    def auth_valid(self):
        """Return bool indicating whether full credentials were provided."""
        return bool(self.user and self.password)

    def auth_pair(self):
        """Return username/password tuple, possibly prompting the user for them."""
        if not self.auth_valid():
            self._get_auth()
        return (self.user, self.password)

    def _get_auth(self):
        """Try to get login auth from known sources."""
        if self.URL_RE.match(self.target):
            auth_url = urlparse(self.target)
            if auth_url.username:
                self.user = auth_url.username
            if auth_url.password:
                self.password = auth_url.password
            if not self.auth_valid():
                self._get_auth_from_keyring(auth_url.hostname)
            if not self.auth_valid():
                self._get_auth_from_netrc(auth_url.hostname)
            if not self.auth_valid():
                self._get_auth_from_console(auth_url.hostname)
        else:
            self._get_auth_from_console(self.target)

    def _get_auth_from_console(self, realm):
        """Prompt for the user and password."""
        self.user, self.password = self.AUTH_MEMOIZE_INPUT
        if not self.auth_valid():
            login = getpass.getuser()
            self.user = raw_input('Username for {} [{}]: '.format(realm, login)) or login
            self.password = getpass.getpass('Password: ')
            Credentials.AUTH_MEMOIZE_INPUT = self.user, self.password

    def _get_auth_from_netrc(self, hostname):
        """Try to find login auth in ``~/.netrc``."""
        try:
            hostauth = netrc(self.NETRC_FILE)
        except IOError as cause:
            if cause.errno != errno.ENOENT:
                raise
            return
        except NetrcParseError as cause:
            raise  # TODO: Map to common base class, so caller has to handle less error types?

        # Try to find specific `user@host` credentials first, then just `host`
        auth = hostauth.hosts.get('{}@{}'.format(self.user or getpass.getuser(), hostname), None)
{'fraction_non_alphanumeric': 0.09507042253521127, 'fraction_numerical': 0.002437703141928494, 'mean_word_length': 2.7054691419969896, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5799837', 'n_tokens_mistral': 2057, 'n_tokens_neox': 1996, 'n_words': 693}