def popup_js(self, callback_name=None, indent=None,<EOL>custom=None, stay_open=False):

custom_callback = """<STR_LIT>""".format(cb=callback_name) if callback_name else '<STR_LIT>'<EOL>return """<STR_LIT>""".format(result=self.to_json(indent),<EOL>custom=json.dumps(custom),<EOL>custom_callback=custom_callback,<EOL>stay_open='<STR_LIT>' if stay_open else '<STR_LIT>')<EOL>

Returns JavaScript that:

#.  Triggers the ``options.onLoginComplete(result, closer)``
    handler set with the :ref:`authomatic.setup() <js_setup>`
    function of :ref:`javascript.js <js>`.
#.  Calls the JavasScript callback specified by :data:`callback_name`
    on the opener of the *login handler popup* and passes it the
    *login result* JSON object as first argument and the `closer`
    function which you should call in your callback to close the popup.

:param str callback_name:
    The name of the javascript callback e.g ``foo.bar.loginCallback``
    will result in ``window.opener.foo.bar.loginCallback(result);``
    in the HTML.

:param int indent:
    The number of spaces to indent the JSON result object.
    If ``0`` or negative, only newlines are added.
    If ``None``, no newlines are added.

:param custom:
    Any JSON serializable object that will be passed to the
    ``result.custom`` attribute.

:param str stay_open:
    If ``True``, the popup will stay open.

:returns:
    :class:`str` with JavaScript.


def popup_html(self, callback_name=None, indent=None,<EOL>title='<STR_LIT>', custom=None, stay_open=False):

return """<STR_LIT>""".format(<EOL>title=title.format(self.provider.name if self.provider else '<STR_LIT>'),<EOL>js=self.popup_js(callback_name, indent, custom, stay_open)<EOL>)<EOL>

Returns a HTML with JavaScript that:

#.  Triggers the ``options.onLoginComplete(result, closer)`` handler
    set with the :ref:`authomatic.setup() <js_setup>` function of
    :ref:`javascript.js <js>`.
#.  Calls the JavasScript callback specified by :data:`callback_name`
    on the opener of the *login handler popup* and passes it the
    *login result* JSON object as first argument and the `closer`
    function which you should call in your callback to close the popup.

:param str callback_name:
    The name of the javascript callback e.g ``foo.bar.loginCallback``
    will result in ``window.opener.foo.bar.loginCallback(result);``
    in the HTML.

:param int indent:
    The number of spaces to indent the JSON result object.
    If ``0`` or negative, only newlines are added.
    If ``None``, no newlines are added.

:param str title:
    The text of the HTML title. You can use ``{0}`` tag inside,
    which will be replaced by the provider name.

:param custom:
    Any JSON serializable object that will be passed to the
    ``result.custom`` attribute.

:param str stay_open:
    If ``True``, the popup will stay open.

:returns:
    :class:`str` with HTML.


return self.provider.user if self.provider else None<EOL>


def __init__(self, httplib_response, content_parser=None):

self.httplib_response = httplib_response<EOL>self.content_parser = content_parser or json_qs_parser<EOL>self._data = None<EOL>self._content = None<EOL>self.msg = httplib_response.msg<EOL>self.version = httplib_response.version<EOL>self.status = httplib_response.status<EOL>self.reason = httplib_response.reason<EOL>

:param httplib_response:
    The wrapped :class:`httplib.HTTPResponse` instance.

:param function content_parser:
    Callable which accepts :attr:`.content` as argument,
    parses it and returns the parsed data as :class:`dict`.


return self.httplib_response.read(amt)<EOL>

Same as :meth:`httplib.HTTPResponse.read`.

:param amt:


def getheader(self, name, default=None):

return self.httplib_response.getheader(name, default)<EOL>

Same as :meth:`httplib.HTTPResponse.getheader`.

:param name:
:param default:


return self.httplib_response.fileno()<EOL>


return self.httplib_response.getheaders()<EOL>


textchars = (bytearray([<NUM_LIT:7>, <NUM_LIT:8>, <NUM_LIT:9>, <NUM_LIT:10>, <NUM_LIT:12>, <NUM_LIT>, <NUM_LIT>]) +<EOL>bytearray(range(<NUM_LIT>, <NUM_LIT>)))<EOL>return bool(content.translate(None, textchars))<EOL>


if not self._content:<EOL><INDENT>content = self.httplib_response.read()<EOL>if self.is_binary_string(content):<EOL><INDENT>self._content = content<EOL><DEDENT>else:<EOL><INDENT>self._content = content.decode('<STR_LIT:utf-8>')<EOL><DEDENT><DEDENT>return self._content<EOL>


if not self._data:<EOL><INDENT>self._data = self.content_parser(self.content)<EOL><DEDENT>return self._data<EOL>

A :class:`dict` of data parsed from :attr:`.content`.


return self[<NUM_LIT:0>]<EOL>


return self[<NUM_LIT:1>]<EOL>


return self[<NUM_LIT:2>]<EOL>


return self[<NUM_LIT:3>]<EOL>


return self[<NUM_LIT:4>]<EOL>

:class:`str` Body of ``POST``, ``PUT`` and ``PATCH`` requests.


return parse.urlencode(self.params)<EOL>


return self.url + '<STR_LIT:?>' + self.query_string<EOL>


def __init__(<EOL>self, config, secret, session_max_age=<NUM_LIT>, secure_cookie=False,<EOL>session=None, session_save_method=None, report_errors=True,<EOL>debug=False, logging_level=logging.INFO, prefix='<STR_LIT>',<EOL>logger=None<EOL>):

self.config = config<EOL>self.secret = secret<EOL>self.session_max_age = session_max_age<EOL>self.secure_cookie = secure_cookie<EOL>self.session = session<EOL>self.session_save_method = session_save_method<EOL>self.report_errors = report_errors<EOL>self.debug = debug<EOL>self.logging_level = logging_level<EOL>self.prefix = prefix<EOL>self._logger = logger or logging.getLogger(str(id(self)))<EOL>if logger is None:<EOL><INDENT>self._logger.setLevel(logging_level)<EOL><DEDENT>

Encapsulates all the functionality of this package.

:param dict config:
    :doc:`config`

:param str secret:
    A secret string that will be used as the key for signing
    :class:`.Session` cookie and as a salt by *CSRF* token generation.

:param session_max_age:
    Maximum allowed age of :class:`.Session` cookie nonce in seconds.

:param bool secure_cookie:
    If ``True`` the :class:`.Session` cookie will be saved wit
    ``Secure`` attribute.

:param session:
    Custom dictionary-like session implementation.

:param callable session_save_method:
    A method of the supplied session or any mechanism that saves the
    session data and cookie.

:param bool report_errors:
    If ``True`` exceptions encountered during the **login procedure**
    will be caught and reported in the :attr:`.LoginResult.error`
    attribute.
    Default is ``True``.

:param bool debug:
    If ``True`` traceback of exceptions will be written to response.
    Default is ``False``.

:param int logging_level:
    The logging level threshold for the default logger as specified in
    the standard Python
    `logging library <http://docs.python.org/2/library/logging.html>`_.
    This setting is ignored when :data:`logger` is set.
    Default is ``logging.INFO``.

:param str prefix:
    Prefix used as the :class:`.Session` cookie name.

:param logger:
    A :class:`logging.logger` instance.


def login(self, adapter, provider_name, callback=None,<EOL>session=None, session_saver=None, **kwargs):

if provider_name:<EOL><INDENT>provider_settings = self.config.get(provider_name)<EOL>if not provider_settings:<EOL><INDENT>raise ConfigError('<STR_LIT>'<EOL>.format(provider_name))<EOL><DEDENT>if not (session is None or session_saver is None):<EOL><INDENT>session = session<EOL>session_saver = session_saver<EOL><DEDENT>else:<EOL><INDENT>session = Session(adapter=adapter,<EOL>secret=self.secret,<EOL>max_age=self.session_max_age,<EOL>name=self.prefix,<EOL>secure=self.secure_cookie)<EOL>session_saver = session.save<EOL><DEDENT>class_ = provider_settings.get('<STR_LIT>')<EOL>if not class_:<EOL><INDENT>raise ConfigError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(provider_name))<EOL><DEDENT>ProviderClass = resolve_provider_class(class_)<EOL>ProviderClass._logger = self._logger<EOL>provider = ProviderClass(self,<EOL>adapter=adapter,<EOL>provider_name=provider_name,<EOL>callback=callback,<EOL>session=session,<EOL>session_saver=session_saver,<EOL>**kwargs)<EOL>return provider.login()<EOL><DEDENT>else:<EOL><INDENT>self.backend(adapter)<EOL><DEDENT>

If :data:`provider_name` specified, launches the login procedure for
corresponding :doc:`provider </reference/providers>` and returns
:class:`.LoginResult`.

If :data:`provider_name` is empty, acts like
:meth:`.Authomatic.backend`.

.. warning::

    The method redirects the **user** to the **provider** which in
    turn redirects **him/her** back to the *request handler* where
    it has been called.

:param str provider_name:
    Name of the provider as specified in the keys of the :doc:`config`.

:param callable callback:
    If specified the method will call the callback with
    :class:`.LoginResult` passed as argument and will return nothing.

:param bool report_errors:

.. note::

    Accepts additional keyword arguments that will be passed to
    :doc:`provider <providers>` constructor.

:returns:
    :class:`.LoginResult`


return Credentials.deserialize(self.config, credentials)<EOL>

Deserializes credentials.

:param credentials:
    Credentials serialized with :meth:`.Credentials.serialize` or
    :class:`.Credentials` instance.

:returns:
    :class:`.Credentials`


def access(self, credentials, url, params=None, method='<STR_LIT:GET>',<EOL>headers=None, body='<STR_LIT>', max_redirects=<NUM_LIT:5>, content_parser=None):

<EOL>credentials = Credentials.deserialize(self.config, credentials)<EOL>ProviderClass = credentials.provider_class<EOL>logging.info('<STR_LIT>'.format(headers))<EOL>provider = ProviderClass(<EOL>self, adapter=None, provider_name=credentials.provider_name)<EOL>provider.credentials = credentials<EOL>return provider.access(url=url,<EOL>params=params,<EOL>method=method,<EOL>headers=headers,<EOL>body=body,<EOL>max_redirects=max_redirects,<EOL>content_parser=content_parser)<EOL>

Accesses **protected resource** on behalf of the **user**.

:param credentials:
    The **user's** :class:`.Credentials` (serialized or normal).

:param str url:
    The **protected resource** URL.

:param str method:
    HTTP method of the request.

:param dict headers:
    HTTP headers of the request.

:param str body:
    Body of ``POST``, ``PUT`` and ``PATCH`` requests.

:param int max_redirects:
    Maximum number of HTTP redirects to follow.

:param function content_parser:
    A function to be used to parse the :attr:`.Response.data`
    from :attr:`.Response.content`.

:returns:
    :class:`.Response`


def async_access(self, *args, **kwargs):

return Future(self.access, *args, **kwargs)<EOL>

Same as :meth:`.Authomatic.access` but runs asynchronously in a
separate thread.

.. warning::

    |async|

:returns:
    :class:`.Future` instance representing the separate thread.


def request_elements(<EOL>self, credentials=None, url=None, method='<STR_LIT:GET>', params=None,<EOL>headers=None, body='<STR_LIT>', json_input=None, return_json=False<EOL>):

<EOL>if json_input:<EOL><INDENT>parsed_input = json.loads(json_input)<EOL>credentials = parsed_input.get('<STR_LIT>', credentials)<EOL>url = parsed_input.get('<STR_LIT:url>', url)<EOL>method = parsed_input.get('<STR_LIT>', method)<EOL>params = parsed_input.get('<STR_LIT>', params)<EOL>headers = parsed_input.get('<STR_LIT>', headers)<EOL>body = parsed_input.get('<STR_LIT:body>', body)<EOL><DEDENT>if not credentials and url:<EOL><INDENT>raise RequestElementsError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>credentials = Credentials.deserialize(self.config, credentials)<EOL>ProviderClass = credentials.provider_class<EOL>request_elements = ProviderClass.create_request_elements(<EOL>ProviderClass.PROTECTED_RESOURCE_REQUEST_TYPE,<EOL>credentials=credentials,<EOL>url=url,<EOL>method=method,<EOL>params=params,<EOL>headers=headers,<EOL>body=body)<EOL>if return_json:<EOL><INDENT>return request_elements.to_json()<EOL><DEDENT>else:<EOL><INDENT>return request_elements<EOL><DEDENT>

Creates request elements for accessing **protected resource of a
user**. Required arguments are :data:`credentials` and :data:`url`. You
can pass :data:`credentials`, :data:`url`, :data:`method`, and
:data:`params` as a JSON object.

:param credentials:
    The **user's** credentials (can be serialized).

:param str url:
    The url of the protected resource.

:param str method:
    The HTTP method of the request.

:param dict params:
    Dictionary of request parameters.

:param dict headers:
    Dictionary of request headers.

:param str body:
    Body of ``POST``, ``PUT`` and ``PATCH`` requests.

:param str json_input:
    you can pass :data:`credentials`, :data:`url`, :data:`method`,
    :data:`params` and :data:`headers` in a JSON object.
    Values from arguments will be used for missing properties.

    ::

        {
            "credentials": "###",
            "url": "https://example.com/api",
            "method": "POST",
            "params": {
                "foo": "bar"
            },
            "headers": {
                "baz": "bing",
                "Authorization": "Bearer ###"
            },
            "body": "Foo bar baz bing."
        }

:param bool return_json:
    if ``True`` the function returns a json object.

    ::

        {
            "url": "https://example.com/api",
            "method": "POST",
            "params": {
                "access_token": "###",
                "foo": "bar"
            },
            "headers": {
                "baz": "bing",
                "Authorization": "Bearer ###"
            },
            "body": "Foo bar baz bing."
        }

:returns:
    :class:`.RequestElements` or JSON string.


AUTHOMATIC_HEADER = '<STR_LIT>'<EOL>request_type = adapter.params.get('<STR_LIT:type>', '<STR_LIT>')<EOL>json_input = adapter.params.get('<STR_LIT>')<EOL>credentials = adapter.params.get('<STR_LIT>')<EOL>url = adapter.params.get('<STR_LIT:url>')<EOL>method = adapter.params.get('<STR_LIT>', '<STR_LIT:GET>')<EOL>body = adapter.params.get('<STR_LIT:body>', '<STR_LIT>')<EOL>params = adapter.params.get('<STR_LIT>')<EOL>params = json.loads(params) if params else {}<EOL>headers = adapter.params.get('<STR_LIT>')<EOL>headers = json.loads(headers) if headers else {}<EOL>ProviderClass = Credentials.deserialize(<EOL>self.config, credentials).provider_class<EOL>if request_type == '<STR_LIT>':<EOL><INDENT>jsonp = params.get('<STR_LIT>')<EOL>if ProviderClass.supports_jsonp and method is '<STR_LIT:GET>':<EOL><INDENT>request_type = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>if jsonp:<EOL><INDENT>params.pop('<STR_LIT>')<EOL><DEDENT>request_type = '<STR_LIT>'<EOL><DEDENT><DEDENT>if request_type == '<STR_LIT>':<EOL><INDENT>response = self.access(<EOL>credentials, url, params, method, headers, body)<EOL>result = response.content<EOL>adapter.status = str(response.status) + '<STR_LIT:U+0020>' + str(response.reason)<EOL>for k, v in response.getheaders():<EOL><INDENT>logging.info('<STR_LIT>'.format(k, v))<EOL>adapter.set_header(k, v)<EOL><DEDENT><DEDENT>elif request_type == '<STR_LIT>':<EOL><INDENT>if json_input:<EOL><INDENT>result = self.request_elements(<EOL>json_input=json_input, return_json=True)<EOL><DEDENT>else:<EOL><INDENT>result = self.request_elements(credentials=credentials,<EOL>url=url,<EOL>method=method,<EOL>params=params,<EOL>headers=headers,<EOL>body=body,<EOL>return_json=True)<EOL><DEDENT>adapter.set_header('<STR_LIT:Content-Type>', '<STR_LIT:application/json>')<EOL><DEDENT>else:<EOL><INDENT>result = '<STR_LIT>'<EOL><DEDENT>adapter.set_header(AUTHOMATIC_HEADER, request_type)<EOL>adapter.write(result)<EOL>

Converts a *request handler* to a JSON backend which you can use with
:ref:`authomatic.js <js>`.

Just call it inside a *request handler* like this:

::

    class JSONHandler(webapp2.RequestHandler):
        def get(self):
            authomatic.backend(Webapp2Adapter(self))

:param adapter:
    The only argument is an :doc:`adapter <adapters>`.

The *request handler* will now accept these request parameters:

:param str type:
    Type of the request. Either ``auto``, ``fetch`` or ``elements``.
    Default is ``auto``.

:param str credentials:
    Serialized :class:`.Credentials`.

:param str url:
    URL of the **protected resource** request.

:param str method:
    HTTP method of the **protected resource** request.

:param str body:
    HTTP body of the **protected resource** request.

:param JSON params:
    HTTP params of the **protected resource** request as a JSON object.

:param JSON headers:
    HTTP headers of the **protected resource** request as a
    JSON object.

:param JSON json:
    You can pass all of the aforementioned params except ``type``
    in a JSON object.

    .. code-block:: javascript

        {
            "credentials": "######",
            "url": "https://example.com",
            "method": "POST",
            "params": {"foo": "bar"},
            "headers": {"baz": "bing"},
            "body": "the body of the request"
        }

Depending on the ``type`` param, the handler will either write
a JSON object with *request elements* to the response,
and add an ``Authomatic-Response-To: elements`` response header, ...

.. code-block:: javascript

    {
        "url": "https://example.com/api",
        "method": "POST",
        "params": {
            "access_token": "###",
            "foo": "bar"
        },
        "headers": {
            "baz": "bing",
            "Authorization": "Bearer ###"
        }
    }

... or make a fetch to the **protected resource** and forward
it's response content, status and headers with an additional
``Authomatic-Response-To: fetch`` header to the response.

.. warning::

    The backend will not work if you write anything to the
    response in the handler!


if self.params.get(self.identifier_param):<EOL><INDENT>self._log(<EOL>logging.INFO,<EOL>u'<STR_LIT>')<EOL>url = users.create_login_url(<EOL>dest_url=self.url, federated_identity=self.identifier)<EOL>self._log(logging.INFO, u'<STR_LIT>'.format(url))<EOL>self.redirect(url)<EOL><DEDENT>else:<EOL><INDENT>self._log(<EOL>logging.INFO,<EOL>u'<STR_LIT>')<EOL>user = users.get_current_user()<EOL>if user:<EOL><INDENT>self._log(logging.INFO, u'<STR_LIT>')<EOL>self._log(logging.INFO, u'<STR_LIT>')<EOL>self.user = core.User(self,<EOL>id=user.federated_identity(),<EOL>email=user.email(),<EOL>gae_user=user)<EOL><DEDENT>else:<EOL><INDENT>raise FailureError(<EOL>'<STR_LIT>'.format(<EOL>self.identifier))<EOL><DEDENT><DEDENT>

Launches the OpenID authentication procedure.


def __init__(self, session, nonce_timeout=None):

self.session = session<EOL>self.nonce_timeout = nonce_timeout or <NUM_LIT><EOL>

:param int nonce_timeout:

    Nonces older than this in seconds will be considered expired.
    Default is 600.


super(OpenID, self).__init__(*args, **kwargs)<EOL>self.store = self._kwarg(<EOL>kwargs, '<STR_LIT:store>', SessionOpenIDStore(<EOL>self.session))<EOL>self.use_realm = self._kwarg(kwargs, '<STR_LIT>', True)<EOL>self.realm_body = self._kwarg(kwargs, '<STR_LIT>', '<STR_LIT>')<EOL>self.realm_param = self._kwarg(kwargs, '<STR_LIT>', '<STR_LIT>')<EOL>self.xrds_param = self._kwarg(kwargs, '<STR_LIT>', '<STR_LIT>')<EOL>self.sreg = self._kwarg(kwargs, '<STR_LIT>', self.SREG)<EOL>self.sreg_required = self._kwarg(kwargs, '<STR_LIT>', [])<EOL>self.ax = self._kwarg(kwargs, '<STR_LIT>', self.AX)<EOL>self.ax_required = self._kwarg(kwargs, '<STR_LIT>', self.AX_REQUIRED)<EOL>for i in self.ax_required:<EOL><INDENT>if i not in self.ax:<EOL><INDENT>self.ax.append(i)<EOL><DEDENT><DEDENT>self.pape = self._kwarg(kwargs, '<STR_LIT>', self.PAPE)<EOL>

Accepts additional keyword arguments:

:param store:
    Any object which implements
    :class:`openid.store.interface.OpenIDStore`
    of the `python-openid`_ library.

:param bool use_realm:
    Whether to use `OpenID realm
    <http://openid.net/specs/openid-authentication-2_0-12.html#realms>`_
    If ``True`` the realm HTML document will be accessible at
    ``{current url}?{realm_param}={realm_param}``
    e.g. ``http://example.com/path?realm=realm``.

:param str realm_body:
    Contents of the HTML body tag of the realm.

:param str realm_param:
    Name of the query parameter to be used to serve the realm.

:param str xrds_param:
    The name of the query parameter to be used to serve the
    `XRDS document
    <http://openid.net/specs/openid-authentication-2_0-12.html#XRDS_Sample>`_.

:param list sreg:
    List of strings of optional
    `SREG
    <http://openid.net/specs/openid-simple-registration-extension-1_0.html>`_
    fields.
    Default = :attr:`OpenID.SREG`.

:param list sreg_required:
    List of strings of required
    `SREG
    <http://openid.net/specs/openid-simple-registration-extension-1_0.html>`_
    fields.
    Default = ``[]``.

:param list ax:
    List of strings of optional
    `AX
    <http://openid.net/specs/openid-attribute-exchange-1_0.html>`_
    schemas.
    Default = :attr:`OpenID.AX`.

:param list ax_required:
    List of strings of required
    `AX
    <http://openid.net/specs/openid-attribute-exchange-1_0.html>`_
    schemas.
    Default = :attr:`OpenID.AX_REQUIRED`.

:param list pape:
    of requested
    `PAPE
    <http://openid.net/specs/openid-provider-authentication-policy-extension-1_0.html>`_
    policies.
    Default = :attr:`OpenID.PAPE`.

As well as those inherited from :class:`.AuthenticationProvider`
constructor.


super(OAuth2, self).__init__(*args, **kwargs)<EOL>self.scope = self._kwarg(kwargs, '<STR_LIT>', [])<EOL>self.offline = self._kwarg(kwargs, '<STR_LIT>', False)<EOL>

Accepts additional keyword arguments:

:param list scope:
    List of strings specifying requested permissions as described
    in the
    `OAuth 2.0 spec <http://tools.ietf.org/html/rfc6749#section-3.3>`_.

:param bool offline:
    If ``True`` the **provider** will be set up to request an
    *offline access token*.
    Default is ``False``.

As well as those inherited from :class:`.AuthorizationProvider`
constructor.


<EOL>return '<STR_LIT:U+002C>'.join(scope) if scope else '<STR_LIT>'<EOL>

Override this to handle differences between accepted format of scope
across providers.

:attr list scope:
    List of scopes.


@classmethod<EOL><INDENT>def create_request_elements(<EOL>cls, request_type, credentials, url, method='<STR_LIT:GET>', params=None,<EOL>headers=None, body='<STR_LIT>', secret=None, redirect_uri='<STR_LIT>', scope='<STR_LIT>',<EOL>csrf='<STR_LIT>', user_state='<STR_LIT>'<EOL>):<DEDENT>

headers = headers or {}<EOL>params = params or {}<EOL>consumer_key = credentials.consumer_key or '<STR_LIT>'<EOL>consumer_secret = credentials.consumer_secret or '<STR_LIT>'<EOL>token = credentials.token or '<STR_LIT>'<EOL>refresh_token = credentials.refresh_token or credentials.token or '<STR_LIT>'<EOL>url, base_params = cls._split_url(url)<EOL>params.update(dict(base_params))<EOL>if request_type == cls.USER_AUTHORIZATION_REQUEST_TYPE:<EOL><INDENT>if consumer_key and redirect_uri and (<EOL>csrf or not cls.supports_csrf_protection):<EOL><INDENT>params['<STR_LIT>'] = consumer_key<EOL>params['<STR_LIT>'] = redirect_uri<EOL>params['<STR_LIT>'] = scope<EOL>if cls.supports_user_state:<EOL><INDENT>params['<STR_LIT:state>'] = base64.urlsafe_b64encode(<EOL>json.dumps(<EOL>{"<STR_LIT>": csrf, "<STR_LIT>": user_state}<EOL>).encode('<STR_LIT:utf-8>')<EOL>)<EOL><DEDENT>else:<EOL><INDENT>params['<STR_LIT:state>'] = csrf<EOL><DEDENT>params['<STR_LIT>'] = '<STR_LIT:code>'<EOL>headers.update(cls._authorization_header(credentials))<EOL><DEDENT>else:<EOL><INDENT>raise OAuth2Error(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT>elif request_type == cls.ACCESS_TOKEN_REQUEST_TYPE:<EOL><INDENT>if consumer_key and consumer_secret:<EOL><INDENT>params['<STR_LIT:code>'] = token<EOL>params['<STR_LIT>'] = consumer_key<EOL>params['<STR_LIT>'] = consumer_secret<EOL>params['<STR_LIT>'] = redirect_uri<EOL>params['<STR_LIT>'] = '<STR_LIT>'<EOL>headers.update(cls._authorization_header(credentials))<EOL><DEDENT>else:<EOL><INDENT>raise OAuth2Error(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT>elif request_type == cls.REFRESH_TOKEN_REQUEST_TYPE:<EOL><INDENT>if refresh_token and consumer_key and consumer_secret:<EOL><INDENT>params['<STR_LIT>'] = refresh_token<EOL>params['<STR_LIT>'] = consumer_key<EOL>params['<STR_LIT>'] = consumer_secret<EOL>params['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>raise OAuth2Error(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT>elif request_type == cls.PROTECTED_RESOURCE_REQUEST_TYPE:<EOL><INDENT>if credentials.token_type == cls.BEARER:<EOL><INDENT>headers.update(<EOL>{'<STR_LIT>': '<STR_LIT>'.format(credentials.token)})<EOL><DEDENT>elif token:<EOL><INDENT>params['<STR_LIT>'] = token<EOL><DEDENT>else:<EOL><INDENT>raise OAuth2Error(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT>request_elements = core.RequestElements(<EOL>url, method, params, headers, body)<EOL>return cls._x_request_elements_filter(<EOL>request_type, request_elements, credentials)<EOL>


if credentials.refresh_token:<EOL><INDENT>return True<EOL><DEDENT>

Override this to specify conditions when it gives sense to refresh
credentials.

.. warning::

    |classmethod|

:param credentials:
    :class:`.Credentials`

:returns:
    ``True`` or ``False``


@classmethod<EOL><INDENT>def decode_state(cls, state, param='<STR_LIT>'):<DEDENT>

if state and cls.supports_user_state:<EOL><INDENT>return json.loads(base64.urlsafe_b64decode(<EOL>unquote(str(state))).decode('<STR_LIT:utf-8>'))[param]<EOL><DEDENT>else:<EOL><INDENT>return state if param == '<STR_LIT>' else '<STR_LIT>'<EOL><DEDENT>

Decode state and return param.

:param str state:
    state parameter passed through by provider

:param str param:
    key to query from decoded state variable. Options include 'csrf'
    and 'user_state'.

:returns:
    string value from decoded state


if not self._x_refresh_credentials_if(credentials):<EOL><INDENT>return<EOL><DEDENT>cfg = credentials.config.get(credentials.provider_name)<EOL>credentials.consumer_key = cfg.get('<STR_LIT>')<EOL>credentials.consumer_secret = cfg.get('<STR_LIT>')<EOL>request_elements = self.create_request_elements(<EOL>request_type=self.REFRESH_TOKEN_REQUEST_TYPE,<EOL>credentials=credentials,<EOL>url=self.access_token_url,<EOL>method='<STR_LIT:POST>'<EOL>)<EOL>self._log(logging.INFO, u'<STR_LIT>')<EOL>response = self._fetch(*request_elements)<EOL>credentials.consumer_key = None<EOL>credentials.consumer_secret = None<EOL>access_token = response.data.get('<STR_LIT>')<EOL>refresh_token = response.data.get('<STR_LIT>')<EOL>if access_token:<EOL><INDENT>credentials.token = access_token<EOL>credentials.expire_in = response.data.get('<STR_LIT>')<EOL>if refresh_token:<EOL><INDENT>credentials.refresh_token = refresh_token<EOL><DEDENT>credentials = self._x_credentials_parser(<EOL>credentials, response.data)<EOL><DEDENT>return response<EOL>

Refreshes :class:`.Credentials` if it gives sense.

:param credentials:
    :class:`.Credentials` to be refreshed.

:returns:
    :class:`.Response`.


return '<STR_LIT:|>'.join(scope)<EOL>


<EOL>credentials.expire_in = data.get('<STR_LIT>')<EOL>if data.get('<STR_LIT>') == '<STR_LIT>':<EOL><INDENT>credentials.token_type = '<STR_LIT>'<EOL><DEDENT>return credentials<EOL>

We need to override this method to fix Facebooks naming deviation.


@classmethod<EOL><INDENT>def _x_request_elements_filter(cls, request_type, request_elements,<EOL>credentials):<DEDENT>

if request_type is cls.ACCESS_TOKEN_REQUEST_TYPE:<EOL><INDENT>params = request_elements[<NUM_LIT:2>]<EOL>del params['<STR_LIT>']<EOL>del params['<STR_LIT>']<EOL><DEDENT>return request_elements<EOL>

Google doesn't accept client ID and secret to be at the same time in
request parameters and in the basic authorization header in the access
token request.


return '<STR_LIT:U+0020>'.join(scope)<EOL>


html = """<STR_LIT>"""<EOL>return html.format(error=exc_info[<NUM_LIT:1>], traceback=traceback_)<EOL>

Generates error traceback HTML.

:param tuple exc_info:
    Output of :func:`sys.exc_info` function.

:param traceback:
    Output of :func:`traceback.format_exc` function.


def wrap(provider, *args, **kwargs):<EOL><INDENT>error = None<EOL>result = authomatic.core.LoginResult(provider)<EOL>try:<EOL><INDENT>func(provider, *args, **kwargs)<EOL><DEDENT>except Exception as e:  <EOL><INDENT>if provider.settings.report_errors:<EOL><INDENT>error = e<EOL>if not isinstance(error, CancellationError):<EOL><INDENT>provider._log(<EOL>logging.ERROR,<EOL>u'<STR_LIT>'.format(<EOL>repr(error)),<EOL>exc_info=<NUM_LIT:1>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if provider.settings.debug:<EOL><INDENT>provider.write(<EOL>_error_traceback_html(<EOL>sys.exc_info(),<EOL>traceback.format_exc()))<EOL><DEDENT>raise<EOL><DEDENT><DEDENT>if provider.user or error:<EOL><INDENT>result = authomatic.core.LoginResult(provider)<EOL>result.error = error<EOL>if isinstance(provider.session, authomatic.core.Session):<EOL><INDENT>provider.session.delete()<EOL><DEDENT>provider._log(logging.INFO, u'<STR_LIT>')<EOL>if provider.callback:<EOL><INDENT>provider.callback(result)<EOL><DEDENT>return result<EOL><DEDENT>else:<EOL><INDENT>provider.save_session()<EOL><DEDENT><DEDENT>return wrap<EOL>

Decorate the :meth:`.BaseProvider.login` implementations with this
decorator.

Provides mechanism for error reporting and returning result which
makes the :meth:`.BaseProvider.login` implementation cleaner.


Launches the *login procedure* to get **user's credentials** from
**provider**.

Should be decorated with :func:`.login_decorator`. The *login
procedure* is considered finished when the :attr:`.user`
attribute is not empty when the method runs out of it's flow or
when there are errors.


return dict(name=self.name,<EOL>id=getattr(self, '<STR_LIT:id>', None),<EOL>type_id=self.type_id,<EOL>type=self.get_type(),<EOL>scope=getattr(self, '<STR_LIT>', None),<EOL>user=self.user.id if self.user else None)<EOL>

Converts the provider instance to a :class:`dict`.

:returns:
    :class:`dict`


return cls.__module__ + '<STR_LIT:.>' + cls.__bases__[<NUM_LIT:0>].__name__<EOL>

Returns the provider type.

:returns:
    :class:`str` The full dotted path to base class e.g.
    :literal:`"authomatic.providers.oauth2.OAuth2"`.


Updates and returns :attr:`.user`.

:returns:
    :class:`.User`


def _kwarg(self, kwargs, kwname, default=None):

return kwargs.get(kwname) orself.settings.config.get(self.name, {}).get(kwname) orself.settings.config.get('<STR_LIT>', {}).get(kwname) ordefault<EOL>

Resolves keyword arguments from constructor or :doc:`config`.

.. note::

    The keyword arguments take this order of precedence:

    1. Arguments passed to constructor through the
       :func:`authomatic.login`.
    2. Provider specific arguments from :doc:`config`.
    3. Arguments from :doc:`config` set in the ``__defaults__`` key.
    2. The value from :data:`default` argument.

:param dict kwargs:
    Keyword arguments dictionary.
:param str kwname:
    Name of the desired keyword argument.


return '<STR_LIT>'.format(self.settings.prefix, self.name, key)<EOL>

Generates session key string.

:param str key:
    e.g. ``"authomatic:facebook:key"``


self.session[self._session_key(key)] = value<EOL>


return self.session.get(self._session_key(key))<EOL>


<EOL>hashed = hashlib.md5(uuid.uuid4().bytes + six.b(secret)).hexdigest()<EOL>span = <NUM_LIT:5><EOL>shift = random.randint(<NUM_LIT:0>, span)<EOL>return hashed[shift:shift - span - <NUM_LIT:1>]<EOL>

Generates CSRF token.

Inspired by this article:
http://blog.ptsecurity.com/2012/10/random-number-security-in-python.html

:returns:
    :class:`str` Random unguessable string.


@classmethod<EOL><INDENT>def _log(cls, level, msg, **kwargs):<DEDENT>

logger = getattr(cls, '<STR_LIT>', None) or authomatic.core._logger<EOL>logger.log(<EOL>level, '<STR_LIT>'.join(<EOL>('<STR_LIT>', cls.__name__, msg)), **kwargs)<EOL>

Logs a message with pre-formatted prefix.

:param int level:
    Logging level as specified in the
    `login module <http://docs.python.org/2/library/logging.html>`_ of
    Python standard library.

:param str msg:
    The actual message.


def _fetch(self, url, method='<STR_LIT:GET>', params=None, headers=None,<EOL>body='<STR_LIT>', max_redirects=<NUM_LIT:5>, content_parser=None):

<EOL>params = params or {}<EOL>params.update(self.access_params)<EOL>headers = headers or {}<EOL>headers.update(self.access_headers)<EOL>scheme, host, path, query, fragment = parse.urlsplit(url)<EOL>query = parse.urlencode(params)<EOL>if method in ('<STR_LIT:POST>', '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>if not body:<EOL><INDENT>body = query<EOL>query = '<STR_LIT>'<EOL>headers.update(<EOL>{'<STR_LIT:Content-Type>': '<STR_LIT>'})<EOL><DEDENT><DEDENT>request_path = parse.urlunsplit(('<STR_LIT>', '<STR_LIT>', path or '<STR_LIT>', query or '<STR_LIT>', '<STR_LIT>'))<EOL>self._log(logging.DEBUG, u'<STR_LIT>'.format(host))<EOL>self._log(<EOL>logging.DEBUG,<EOL>u'<STR_LIT>'.format(request_path))<EOL>self._log(logging.DEBUG, u'<STR_LIT>'.format(method))<EOL>self._log(logging.DEBUG, u'<STR_LIT>'.format(body))<EOL>self._log(logging.DEBUG, u'<STR_LIT>'.format(params))<EOL>self._log(logging.DEBUG, u'<STR_LIT>'.format(headers))<EOL>if scheme.lower() == '<STR_LIT>':<EOL><INDENT>connection = http_client.HTTPSConnection(host)<EOL><DEDENT>else:<EOL><INDENT>connection = http_client.HTTPConnection(host)<EOL><DEDENT>try:<EOL><INDENT>connection.request(method, request_path, body, headers)<EOL><DEDENT>except Exception as e:<EOL><INDENT>raise FetchError('<STR_LIT>',<EOL>original_message=str(e),<EOL>url=request_path)<EOL><DEDENT>response = connection.getresponse()<EOL>location = response.getheader('<STR_LIT>')<EOL>if response.status in (<NUM_LIT>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT>) and location:<EOL><INDENT>if location == url:<EOL><INDENT>raise FetchError('<STR_LIT>',<EOL>url=location,<EOL>status=response.status)<EOL><DEDENT>elif max_redirects > <NUM_LIT:0>:<EOL><INDENT>remaining_redirects = max_redirects - <NUM_LIT:1><EOL>self._log(logging.DEBUG, u'<STR_LIT>'.format(url))<EOL>self._log(logging.DEBUG, u'<STR_LIT>'<EOL>.format(remaining_redirects))<EOL>response = self._fetch(url=location,<EOL>params=params,<EOL>method=method,<EOL>headers=headers,<EOL>max_redirects=remaining_redirects)<EOL><DEDENT>else:<EOL><INDENT>raise FetchError('<STR_LIT>',<EOL>url=location,<EOL>status=response.status)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>self._log(logging.DEBUG, u'<STR_LIT>')<EOL>self._log(logging.DEBUG, u'<STR_LIT>'.format(url))<EOL>self._log(<EOL>logging.DEBUG,<EOL>u'<STR_LIT>'.format(<EOL>response.status))<EOL>self._log(<EOL>logging.DEBUG,<EOL>u'<STR_LIT>'.format(<EOL>response.getheaders()))<EOL><DEDENT>return authomatic.core.Response(response, content_parser)<EOL>

Fetches a URL.

:param str url:
    The URL to fetch.

:param str method:
    HTTP method of the request.

:param dict params:
    Dictionary of request parameters.

:param dict headers:
    HTTP headers of the request.

:param str body:
    Body of ``POST``, ``PUT`` and ``PATCH`` requests.

:param int max_redirects:
    Number of maximum HTTP redirects to follow.

:param function content_parser:
    A callable to be used to parse the :attr:`.Response.data`
    from :attr:`.Response.content`.


def _update_or_create_user(self, data, credentials=None, content=None):

if not self.user:<EOL><INDENT>self.user = authomatic.core.User(self, credentials=credentials)<EOL><DEDENT>self.user.content = content<EOL>self.user.data = data<EOL>for key in self.user.__dict__:<EOL><INDENT>if key not in ('<STR_LIT:data>', '<STR_LIT:content>'):<EOL><INDENT>value = data.get(key)<EOL>if value:<EOL><INDENT>setattr(self.user, key, value)<EOL><DEDENT><DEDENT><DEDENT>self.user = self._x_user_parser(self.user, data)<EOL>if self.user.id:<EOL><INDENT>self.user.id = str(self.user.id)<EOL><DEDENT>if not self.user.name:<EOL><INDENT>if self.user.first_name and self.user.last_name:<EOL><INDENT>self.user.name = '<STR_LIT:U+0020>'.join((self.user.first_name,<EOL>self.user.last_name))<EOL><DEDENT>else:<EOL><INDENT>self.user.name = (self.user.username or self.user.nickname or<EOL>self.user.first_name or self.user.last_name)<EOL><DEDENT><DEDENT>if not self.user.location:<EOL><INDENT>if self.user.city and self.user.country:<EOL><INDENT>self.user.location = '<STR_LIT>'.format(self.user.city,<EOL>self.user.country)<EOL><DEDENT>else:<EOL><INDENT>self.user.location = self.user.city or self.user.country<EOL><DEDENT><DEDENT>return self.user<EOL>

Updates or creates :attr:`.user`.

:returns:
    :class:`.User`


return user<EOL>

Handles different structure of user info data by different providers.

:param user:
    :class:`.User`
:param dict data:
    User info data returned by provider.


assert category < <NUM_LIT:10>, '<STR_LIT>'<EOL>cat = category * <NUM_LIT:100><EOL>return status >= cat and status < cat + <NUM_LIT:100><EOL>

Checks whether a HTTP status code is in the category denoted by the
hundreds digit.


super(AuthorizationProvider, self).__init__(*args, **kwargs)<EOL>self.consumer_key = self._kwarg(kwargs, '<STR_LIT>')<EOL>self.consumer_secret = self._kwarg(kwargs, '<STR_LIT>')<EOL>self.user_authorization_params = self._kwarg(<EOL>kwargs, '<STR_LIT>', {})<EOL>self.access_token_headers = self._kwarg(<EOL>kwargs, '<STR_LIT>', {})<EOL>self.access_token_params = self._kwarg(<EOL>kwargs, '<STR_LIT>', {})<EOL>self.id = self._kwarg(kwargs, '<STR_LIT:id>')<EOL>self.access_headers = self._kwarg(kwargs, '<STR_LIT>', {})<EOL>self.access_params = self._kwarg(kwargs, '<STR_LIT>', {})<EOL>self.credentials = authomatic.core.Credentials(<EOL>self.settings.config, provider=self)<EOL>self.access_token_response = None<EOL>

Accepts additional keyword arguments:

:arg str consumer_key:
    The *key* assigned to our application (**consumer**) by the
    **provider**.

:arg str consumer_secret:
    The *secret* assigned to our application (**consumer**) by the
    **provider**.

:arg int id:
    A unique numeric ID used to serialize :class:`.Credentials`.

:arg dict user_authorization_params:
    A dictionary of additional request parameters for
    **user authorization request**.

:arg dict access_token_params:
    A dictionary of additional request parameters for
    **access_with_credentials token request**.

:arg dict access_headers:
    A dictionary of default HTTP headers that will be used when
    accessing **user's** protected resources.
    Applied by :meth:`.access()`, :meth:`.update_user()` and
    :meth:`.User.update()`

:arg dict access_params:
    A dictionary of default query string parameters that will be used
    when accessing **user's** protected resources.
    Applied by :meth:`.access()`, :meth:`.update_user()` and
    :meth:`.User.update()`


:class:`str` URL to which we redirect the **user** to grant our app
i.e. the **consumer** an **authorization** to access his
**protected resources**. See
http://tools.ietf.org/html/rfc6749#section-4.1.1 and
http://oauth.net/core/1.0a/#auth_step2.


:class:`str` URL where we can get the *access token* to access
**protected resources** of a **user**. See
http://tools.ietf.org/html/rfc6749#section-4.1.3 and
http://oauth.net/core/1.0a/#auth_step3.


:class:`str` URL where we can get the **user** info.
see http://tools.ietf.org/html/rfc6749#section-7 and
http://oauth.net/core/1.0a/#anchor12.


Must convert :data:`credentials` to a :class:`tuple` to be used by
:meth:`.Credentials.serialize`.

.. warning::

    |classmethod|

:param credentials:
    :class:`.Credentials`

:returns:
    :class:`tuple`


@abc.abstractmethod<EOL><INDENT>def reconstruct(self, deserialized_tuple, credentials, cfg):<DEDENT>

Must convert the :data:`deserialized_tuple` back to
:class:`.Credentials`.

.. warning::

    |classmethod|

:param tuple deserialized_tuple:
    A tuple whose first index is the :attr:`.id` and the rest
    are all the items of the :class:`tuple` created by
    :meth:`.to_tuple`.

:param credentials:
    A :class:`.Credentials` instance.

:param dict cfg:
    Provider configuration from :doc:`config`.


@abc.abstractmethod<EOL><INDENT>def create_request_elements(self, request_type, credentials,<EOL>url, method='<STR_LIT:GET>', params=None, headers=None,<EOL>body='<STR_LIT>'):<DEDENT>

Must return :class:`.RequestElements`.

.. warning::

    |classmethod|

:param int request_type:
    Type of the request specified by one of the class's constants.

:param credentials:
    :class:`.Credentials` of the **user** whose
    **protected resource** we want to access.

:param str url:
    URL of the request.

:param str method:
    HTTP method of the request.

:param dict params:
    Dictionary of request parameters.

:param dict headers:
    Dictionary of request headers.

:param str body:
    Body of ``POST``, ``PUT`` and ``PATCH`` requests.

:returns:
    :class:`.RequestElements`


cls = self.__class__<EOL>mod = sys.modules.get(cls.__module__)<EOL>return str(self.PROVIDER_TYPE_ID) + '<STR_LIT:->' +str(mod.PROVIDER_ID_MAP.index(cls))<EOL>

A short string representing the provider implementation id used for
serialization of :class:`.Credentials` and to identify the type of
provider in JavaScript.

The part before hyphen denotes the type of the provider, the part
after hyphen denotes the class id e.g.
``oauth2.Facebook.type_id = '2-5'``,
``oauth1.Twitter.type_id = '1-5'``.


def access(self, url, params=None, method='<STR_LIT:GET>', headers=None,<EOL>body='<STR_LIT>', max_redirects=<NUM_LIT:5>, content_parser=None):

if not self.user and not self.credentials:<EOL><INDENT>raise CredentialsError(u'<STR_LIT>')<EOL><DEDENT>headers = headers or {}<EOL>self._log(<EOL>logging.INFO,<EOL>u'<STR_LIT>'.format(url))<EOL>request_elements = self.create_request_elements(<EOL>request_type=self.PROTECTED_RESOURCE_REQUEST_TYPE,<EOL>credentials=self.credentials,<EOL>url=url,<EOL>body=body,<EOL>params=params,<EOL>headers=headers,<EOL>method=method<EOL>)<EOL>response = self._fetch(*request_elements,<EOL>max_redirects=max_redirects,<EOL>content_parser=content_parser)<EOL>self._log(<EOL>logging.INFO,<EOL>u'<STR_LIT>'.format(<EOL>response.status))<EOL>return response<EOL>

Fetches the **protected resource** of an authenticated **user**.

:param credentials:
    The **user's** :class:`.Credentials` (serialized or normal).

:param str url:
    The URL of the **protected resource**.

:param str method:
    HTTP method of the request.

:param dict headers:
    HTTP headers of the request.

:param str body:
    Body of ``POST``, ``PUT`` and ``PATCH`` requests.

:param int max_redirects:
    Maximum number of HTTP redirects to follow.

:param function content_parser:
    A function to be used to parse the :attr:`.Response.data`
    from :attr:`.Response.content`.

:returns:
    :class:`.Response`


return authomatic.core.Future(self.access, *args, **kwargs)<EOL>

Same as :meth:`.access` but runs asynchronously in a separate thread.

.. warning::

    |async|

:returns:
    :class:`.Future` instance representing the separate thread.


if self.user_info_url:<EOL><INDENT>response = self._access_user_info()<EOL>self.user = self._update_or_create_user(response.data,<EOL>content=response.content)<EOL>return authomatic.core.UserInfoResponse(self.user,<EOL>response.httplib_response)<EOL><DEDENT>

Updates the :attr:`.BaseProvider.user`.

.. warning::
    Fetches the :attr:`.user_info_url`!

:returns:
    :class:`.UserInfoResponse`


if cls._x_use_authorization_header:<EOL><INDENT>res = '<STR_LIT::>'.join(<EOL>(credentials.consumer_key,<EOL>credentials.consumer_secret))<EOL>res = base64.b64encode(six.b(res)).decode()<EOL>return {'<STR_LIT>': '<STR_LIT>'.format(res)}<EOL><DEDENT>else:<EOL><INDENT>return {}<EOL><DEDENT>

Creates authorization headers if the provider supports it. See:
http://en.wikipedia.org/wiki/Basic_access_authentication.

:param credentials:
    :class:`.Credentials`

:returns:
    Headers as :class:`dict`.


<EOL>if not self.consumer.key:<EOL><INDENT>raise ConfigError(<EOL>'<STR_LIT>'.format(<EOL>self.name))<EOL><DEDENT>if not self.consumer.secret:<EOL><INDENT>raise ConfigError(<EOL>'<STR_LIT>'.format(<EOL>self.name))<EOL><DEDENT>


split = parse.urlsplit(url)<EOL>base = parse.urlunsplit((split.scheme, split.netloc, split.path, <NUM_LIT:0>, <NUM_LIT:0>))<EOL>params = parse.parse_qsl(split.query, True)<EOL>return base, params<EOL>

Splits given url to url base and params converted to list of tuples.


@classmethod<EOL><INDENT>def _x_request_elements_filter(<EOL>cls, request_type, request_elements, credentials):<DEDENT>

return request_elements<EOL>

Override this to handle special request requirements of zealous
providers.

.. warning::

    |classmethod|

:param int request_type:
    Type of request.

:param request_elements:
    :class:`.RequestElements`

:param credentials:
    :class:`.Credentials`

:returns:
    :class:`.RequestElements`


return credentials<EOL>

Override this to handle differences in naming conventions across
providers.

:param credentials:
    :class:`.Credentials`

:param dict data:
    Response data dictionary.

:returns:
    :class:`.Credentials`


url = self.user_info_url.format(**self.user.__dict__)<EOL>return self.access(url)<EOL>

Accesses the :attr:`.user_info_url`.

:returns:
    :class:`.UserInfoResponse`


if isinstance(params, dict):<EOL><INDENT>params = list(params.items())<EOL><DEDENT>params = sorted([<EOL>(k, v) for k, v in params<EOL>if k not in ('<STR_LIT>', '<STR_LIT>')<EOL>])<EOL>qs = parse.urlencode(params)<EOL>qs = qs.replace('<STR_LIT:+>', '<STR_LIT>')<EOL>qs = qs.replace('<STR_LIT>', '<STR_LIT>')<EOL>return qs<EOL>

Returns a normalized query string sorted first by key, then by value
excluding the ``realm`` and ``oauth_signature`` parameters as specified
here: http://oauth.net/core/1.0a/#rfc.section.9.1.1.

:param params:
    :class:`dict` or :class:`list` of tuples.


def _create_base_string(method, base, params):

normalized_qs = _normalize_params(params)<EOL>return _join_by_ampersand(method, base, normalized_qs)<EOL>

Returns base string for HMAC-SHA1 signature as specified in:
http://oauth.net/core/1.0a/#rfc.section.9.1.3.


@abc.abstractmethod<EOL><INDENT>def create_signature(self, method, base, params,<EOL>consumer_secret, token_secret='<STR_LIT>'):<DEDENT>

Must create signature based on the parameters as specified in
http://oauth.net/core/1.0a/#signing_process.

.. warning::

    |classmethod|

:param str method:
    HTTP method of the request to be signed.

:param str base:
    Base URL of the request without query string an fragment.

:param dict params:
    Dictionary or list of tuples of the request parameters.

:param str consumer_secret:
    :attr:`.core.Consumer.secret`

:param str token_secret:
    Access token secret as specified in
    http://oauth.net/core/1.0a/#anchor3.

:returns:
    The signature string.


@classmethod<EOL><INDENT>def _create_key(cls, consumer_secret, token_secret='<STR_LIT>'):<DEDENT>

return _join_by_ampersand(consumer_secret, token_secret or '<STR_LIT>')<EOL>

Returns a key for HMAC-SHA1 signature as specified at:
http://oauth.net/core/1.0a/#rfc.section.9.2.

:param str consumer_secret:
    :attr:`.core.Consumer.secret`

:param str token_secret:
    Access token secret as specified in
    http://oauth.net/core/1.0a/#anchor3.

:returns:
    Key to sign the request with.


@classmethod<EOL><INDENT>def create_signature(cls, method, base, params,<EOL>consumer_secret, token_secret='<STR_LIT>'):<DEDENT>

base_string = _create_base_string(method, base, params)<EOL>key = cls._create_key(consumer_secret, token_secret)<EOL>hashed = hmac.new(<EOL>six.b(key),<EOL>base_string.encode('<STR_LIT:utf-8>'),<EOL>hashlib.sha1)<EOL>base64_encoded = binascii.b2a_base64(hashed.digest())[:-<NUM_LIT:1>]<EOL>return base64_encoded<EOL>

Returns HMAC-SHA1 signature as specified at:
http://oauth.net/core/1.0a/#rfc.section.9.2.

:param str method:
    HTTP method of the request to be signed.

:param str base:
    Base URL of the request without query string an fragment.

:param dict params:
    Dictionary or list of tuples of the request parameters.

:param str consumer_secret:
    :attr:`.core.Consumer.secret`

:param str token_secret:
    Access token secret as specified in
    http://oauth.net/core/1.0a/#anchor3.

:returns:
    The signature string.


super(OAuth1, self).__init__(*args, **kwargs)<EOL>self.request_token_params = self._kwarg(<EOL>kwargs, '<STR_LIT>', {})<EOL>

Accepts additional keyword arguments:

:param str consumer_key:
    The *key* assigned to our application (**consumer**) by
    the **provider**.

:param str consumer_secret:
    The *secret* assigned to our application (**consumer**) by
    the **provider**.

:param id:
    A unique short name used to serialize :class:`.Credentials`.

:param dict user_authorization_params:
    A dictionary of additional request parameters for
    **user authorization request**.

:param dict access_token_params:
    A dictionary of additional request parameters for
    **access token request**.

:param dict request_token_params:
    A dictionary of additional request parameters for
    **request token request**.


:class:`str` URL where we can get the |oauth1| request token.
see http://oauth.net/core/1.0a/#auth_step1.


@classmethod<EOL><INDENT>def create_request_elements(<EOL>cls, request_type, credentials, url, params=None, headers=None,<EOL>body='<STR_LIT>', method='<STR_LIT:GET>', verifier='<STR_LIT>', callback='<STR_LIT>'<EOL>):<DEDENT>

params = params or {}<EOL>headers = headers or {}<EOL>consumer_key = credentials.consumer_key or '<STR_LIT>'<EOL>consumer_secret = credentials.consumer_secret or '<STR_LIT>'<EOL>token = credentials.token or '<STR_LIT>'<EOL>token_secret = credentials.token_secret or '<STR_LIT>'<EOL>url, base_params = cls._split_url(url)<EOL>params.update(dict(base_params))<EOL>if request_type == cls.USER_AUTHORIZATION_REQUEST_TYPE:<EOL><INDENT>if token:<EOL><INDENT>params['<STR_LIT>'] = token<EOL><DEDENT>else:<EOL><INDENT>raise OAuth1Error(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if request_type == cls.REQUEST_TOKEN_REQUEST_TYPE:<EOL><INDENT>if consumer_key and consumer_secret and callback:<EOL><INDENT>params['<STR_LIT>'] = consumer_key<EOL>params['<STR_LIT>'] = callback<EOL><DEDENT>else:<EOL><INDENT>raise OAuth1Error(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT>elif request_type == cls.ACCESS_TOKEN_REQUEST_TYPE:<EOL><INDENT>if consumer_key and consumer_secret and token and verifier:<EOL><INDENT>params['<STR_LIT>'] = token<EOL>params['<STR_LIT>'] = consumer_key<EOL>params['<STR_LIT>'] = verifier<EOL><DEDENT>else:<EOL><INDENT>raise OAuth1Error(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT>elif request_type == cls.PROTECTED_RESOURCE_REQUEST_TYPE:<EOL><INDENT>if consumer_key and consumer_secret and token and token_secret:<EOL><INDENT>params['<STR_LIT>'] = token<EOL>params['<STR_LIT>'] = consumer_key<EOL><DEDENT>else:<EOL><INDENT>raise OAuth1Error(<EOL>'<STR_LIT>' +<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT>params['<STR_LIT>'] = cls._signature_generator.method<EOL>params['<STR_LIT>'] = str(int(time.time()))<EOL>params['<STR_LIT>'] = cls.csrf_generator(str(uuid.uuid4()))<EOL>params['<STR_LIT>'] = '<STR_LIT:1.0>'<EOL>params['<STR_LIT>'] = cls._signature_generator.create_signature(  <EOL>method, url, params, consumer_secret, token_secret)<EOL><DEDENT>request_elements = core.RequestElements(<EOL>url, method, params, headers, body)<EOL>return cls._x_request_elements_filter(<EOL>request_type, request_elements, credentials)<EOL>


response = super(Bitbucket, self)._access_user_info()<EOL>response.data.setdefault("<STR_LIT:email>", None)<EOL>email_response = self.access(self.user_email_url)<EOL>if email_response.data:<EOL><INDENT>for item in email_response.data:<EOL><INDENT>if item.get("<STR_LIT>", False):<EOL><INDENT>response.data.update(email=item.get("<STR_LIT:email>", None))<EOL><DEDENT><DEDENT><DEDENT>return response<EOL>

Email is available in separate method so second request is needed.


response = super(Vimeo, self)._access_user_info()<EOL>uid = response.data.get('<STR_LIT>', {}).get('<STR_LIT:user>', {}).get('<STR_LIT:id>')<EOL>if uid:<EOL><INDENT>return self.access('<STR_LIT>'<EOL>.format(uid))<EOL><DEDENT>return response<EOL>

Vimeo requires the user ID to access the user info endpoint, so we need
to make two requests: one to get user ID and second to get user info.


return render_template('<STR_LIT>')<EOL>


@app.route('<STR_LIT>', methods=['<STR_LIT:GET>', '<STR_LIT:POST>'])<EOL>def login(provider_name):

<EOL>response = make_response()<EOL>result = authomatic.login(<EOL>WerkzeugAdapter(<EOL>request,<EOL>response),<EOL>provider_name)<EOL>if result:<EOL><INDENT>if result.user:<EOL><INDENT>result.user.update()<EOL><DEDENT>return render_template('<STR_LIT>', result=result)<EOL><DEDENT>return response<EOL>

Login handler, must accept both GET and POST to be able to use OpenID.


for tc in self.possible:<EOL><INDENT>tc.raise_found_error()<EOL><DEDENT>

raise any found error in the possible PathFinders


possible = []<EOL>name = self.name<EOL>logger.debug('<STR_LIT>'.format(name))<EOL>name_f = self.name.lower()<EOL>filepath = "<STR_LIT>"<EOL>if name_f.endswith("<STR_LIT>") or "<STR_LIT>" in name_f:<EOL><INDENT>bits = name.split("<STR_LIT::>", <NUM_LIT:1>)<EOL>filepath = bits[<NUM_LIT:0>]<EOL>logger.debug('<STR_LIT>'.format(filepath))<EOL>name = bits[<NUM_LIT:1>] if len(bits) > <NUM_LIT:1> else "<STR_LIT>"<EOL>if name:<EOL><INDENT>logger.debug('<STR_LIT>'.format(name, filepath))<EOL><DEDENT><DEDENT>bits = name.split('<STR_LIT:.>')<EOL>basedir = self.basedir<EOL>method_prefix = self.method_prefix<EOL>if re.search(r'<STR_LIT>', bits[-<NUM_LIT:1>]):<EOL><INDENT>logger.debug('<STR_LIT>'.format(bits[-<NUM_LIT:1>]))<EOL>possible.append(PathFinder(basedir, method_prefix, **{<EOL>'<STR_LIT>': bits[-<NUM_LIT:1>],<EOL>'<STR_LIT>': bits[-<NUM_LIT:2>] if len(bits) > <NUM_LIT:1> else '<STR_LIT>',<EOL>'<STR_LIT>': os.sep.join(bits[<NUM_LIT:0>:-<NUM_LIT:2>]),<EOL>'<STR_LIT>': filepath,<EOL>}))<EOL><DEDENT>elif len(bits) > <NUM_LIT:1> and re.search(r'<STR_LIT>', bits[-<NUM_LIT:2>]):<EOL><INDENT>logger.debug('<STR_LIT>'.format(bits[-<NUM_LIT:2>]))<EOL>possible.append(PathFinder(basedir, method_prefix, **{<EOL>'<STR_LIT>': bits[-<NUM_LIT:2>],<EOL>'<STR_LIT>': bits[-<NUM_LIT:1>],<EOL>'<STR_LIT>': bits[-<NUM_LIT:3>] if len(bits) > <NUM_LIT:2> else '<STR_LIT>',<EOL>'<STR_LIT>': os.sep.join(bits[<NUM_LIT:0>:-<NUM_LIT:3>]),<EOL>'<STR_LIT>': filepath,<EOL>}))<EOL><DEDENT>else:<EOL><INDENT>if self.name:<EOL><INDENT>if filepath:<EOL><INDENT>if len(bits):<EOL><INDENT>possible.append(PathFinder(basedir, method_prefix, **{<EOL>'<STR_LIT>': filepath,<EOL>'<STR_LIT>': bits[<NUM_LIT:0>],<EOL>}))<EOL><DEDENT>else:<EOL><INDENT>possible.append(PathFinder(basedir, method_prefix, **{<EOL>'<STR_LIT>': filepath,<EOL>}))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>logger.debug('<STR_LIT>')<EOL>possible.append(PathFinder(basedir, method_prefix, **{<EOL>'<STR_LIT>': bits[-<NUM_LIT:1>],<EOL>'<STR_LIT>': os.sep.join(bits[<NUM_LIT:0>:-<NUM_LIT:1>]),<EOL>'<STR_LIT>': filepath,<EOL>}))<EOL>possible.append(PathFinder(basedir, method_prefix, **{<EOL>'<STR_LIT>': bits[-<NUM_LIT:1>],<EOL>'<STR_LIT>': bits[-<NUM_LIT:2>] if len(bits) > <NUM_LIT:1> else '<STR_LIT>',<EOL>'<STR_LIT>': os.sep.join(bits[<NUM_LIT:0>:-<NUM_LIT:2>]),<EOL>'<STR_LIT>': filepath,<EOL>}))<EOL>possible.append(PathFinder(basedir, method_prefix, **{<EOL>'<STR_LIT>': os.sep.join(bits),<EOL>'<STR_LIT>': filepath,<EOL>}))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>possible.append(PathFinder(basedir, method_prefix, filepath=filepath))<EOL><DEDENT><DEDENT>logger.debug("<STR_LIT>".format(len(possible)))<EOL>self.possible = possible<EOL>

break up a module path to its various parts (prefix, module, class, method)

this uses PEP 8 conventions, so foo.Bar would be foo module with class Bar

return -- list -- a list of possible interpretations of the module path
    (eg, foo.bar can be bar module in foo module, or bar method in foo module)


error_info = getattr(self, '<STR_LIT>', None)<EOL>if error_info:<EOL><INDENT>reraise(*error_info)<EOL><DEDENT>

raise an error if one was found, otherwise do nothing


<EOL>sys.path.insert(<NUM_LIT:0>, self.basedir)<EOL>for p in self.paths():<EOL><INDENT>try:<EOL><INDENT>module_name = self.module_path(p)<EOL>logger.debug("<STR_LIT>".format(module_name, p))<EOL>m = importlib.import_module(module_name)<EOL>yield m<EOL><DEDENT>except Exception as e:<EOL><INDENT>logger.warning('<STR_LIT>'.format(p, e))<EOL>logger.warning(e, exc_info=True)<EOL>error_info = getattr(self, '<STR_LIT>', None)<EOL>if not error_info:<EOL><INDENT>exc_info = sys.exc_info()<EOL>self.error_info = exc_info<EOL><DEDENT>continue<EOL><DEDENT><DEDENT>sys.path.pop(<NUM_LIT:0>)<EOL>


for module in self.modules():<EOL><INDENT>cs = inspect.getmembers(module, inspect.isclass)<EOL>class_name = getattr(self, '<STR_LIT>', '<STR_LIT>')<EOL>class_regex = '<STR_LIT>'<EOL>if class_name:<EOL><INDENT>if class_name.startswith("<STR_LIT:*>"):<EOL><INDENT>class_name = class_name.strip("<STR_LIT:*>")<EOL>class_regex = re.compile(r'<STR_LIT>'.format(class_name), re.I)<EOL><DEDENT>else:<EOL><INDENT>class_regex = re.compile(r'<STR_LIT>'.format(class_name), re.I)<EOL><DEDENT><DEDENT>for c_name, c in cs:<EOL><INDENT>can_yield = True<EOL>if class_regex and not class_regex.match(c_name):<EOL><INDENT>can_yield = False<EOL><DEDENT>if can_yield and issubclass(c, unittest.TestCase):<EOL><INDENT>if c is not unittest.TestCase: <EOL><INDENT>logger.debug('<STR_LIT>'.format(c_name, class_name))<EOL>yield c<EOL><DEDENT><DEDENT><DEDENT><DEDENT>

the partial self.class_name will be used to find actual TestCase classes


for c in self.classes():<EOL><INDENT>ms = inspect.getmembers(c, lambda f: inspect.ismethod(f) or inspect.isfunction(f))<EOL>method_name = getattr(self, '<STR_LIT>', '<STR_LIT>')<EOL>method_regex = '<STR_LIT>'<EOL>if method_name:<EOL><INDENT>if method_name.startswith(self.method_prefix):<EOL><INDENT>method_regex = re.compile(r'<STR_LIT>'.format(method_name), flags=re.I)<EOL><DEDENT>else:<EOL><INDENT>if method_name.startswith("<STR_LIT:*>"):<EOL><INDENT>method_name = method_name.strip("<STR_LIT:*>")<EOL>method_regex = re.compile(<EOL>r'<STR_LIT>'.format(self.method_prefix, method_name),<EOL>flags=re.I<EOL>)<EOL><DEDENT>else:<EOL><INDENT>method_regex = re.compile(<EOL>r'<STR_LIT>'.format(self.method_prefix, method_name),<EOL>flags=re.I<EOL>)<EOL><DEDENT><DEDENT><DEDENT>for m_name, m in ms:<EOL><INDENT>if not m_name.startswith(self.method_prefix): continue<EOL>can_yield = True<EOL>if method_regex and not method_regex.match(m_name):<EOL><INDENT>can_yield = False<EOL><DEDENT>if can_yield:<EOL><INDENT>logger.debug('<STR_LIT>'.format(m_name, method_name))<EOL>yield c, m_name<EOL><DEDENT><DEDENT><DEDENT>

return the actual test methods that matched self.method_name


def _find_basename(self, name, basenames, is_prefix=False):

ret = "<STR_LIT>"<EOL>fileroots = [(os.path.splitext(n)[<NUM_LIT:0>], n) for n in basenames]<EOL>glob = False<EOL>if name.startswith("<STR_LIT:*>"):<EOL><INDENT>glob = True<EOL><DEDENT>name = name.strip("<STR_LIT:*>")<EOL>for fileroot, basename in fileroots:<EOL><INDENT>if name in fileroot or fileroot in name:<EOL><INDENT>for pf in self.module_postfixes:<EOL><INDENT>logger.debug(<EOL>'<STR_LIT>'.format(<EOL>basename,<EOL>name,<EOL>pf<EOL>))<EOL>if glob:<EOL><INDENT>if name in fileroot and fileroot.endswith(pf):<EOL><INDENT>ret = basename<EOL>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if fileroot.startswith(name) and fileroot.endswith(pf):<EOL><INDENT>ret = basename<EOL>break<EOL><DEDENT><DEDENT><DEDENT>if not ret:<EOL><INDENT>for pf in self.module_prefixes:<EOL><INDENT>n = pf + name<EOL>logger.debug('<STR_LIT>'.format(basename, n))<EOL>if glob:<EOL><INDENT>if fileroot.startswith(pf) and name in fileroot:<EOL><INDENT>ret = basename<EOL>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if fileroot.startswith(n):<EOL><INDENT>ret = basename<EOL>break<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if not ret:<EOL><INDENT>if is_prefix:<EOL><INDENT>logger.debug('<STR_LIT>'.format(basename, name))<EOL>if basename.startswith(name) or (glob and name in basename):<EOL><INDENT>ret = basename<EOL><DEDENT>else:<EOL><INDENT>logger.debug(<EOL>'<STR_LIT>'.format(<EOL>basename,<EOL>name<EOL>))<EOL>if glob:<EOL><INDENT>if name in basename and self._is_module_path(basename):<EOL><INDENT>ret = basename<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if basename.startswith(name) and self._is_module_path(basename):<EOL><INDENT>ret = basename<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>if ret:<EOL><INDENT>logger.debug('<STR_LIT>'.format(ret))<EOL>break<EOL><DEDENT><DEDENT><DEDENT>return ret<EOL>

check if name combined with test prefixes or postfixes is found anywhere
        in the list of basenames

        :param name: string, the name you're searching for
        :param basenames: list, a list of basenames to check
        :param is_prefix: bool, True if this is a prefix search, which means it will
            also check if name matches any of the basenames without the prefixes or
            postfixes, if it is False then the prefixes or postfixes must be present
            (ie, the module we're looking for is the actual test module, not the parent
             modules it's contained in)
        :returns: string, the basename if it is found


def _find_prefix_path(self, basedir, prefix):

ret = "<STR_LIT>"<EOL>for ret in self._find_prefix_paths(basedir, prefix):<EOL><INDENT>break<EOL><DEDENT>if not ret:<EOL><INDENT>raise IOError("<STR_LIT>".format(prefix, basedir))<EOL><DEDENT>return ret<EOL>

Similar to _find_prefix_paths() but only returns the first match


ret = False<EOL>basename = os.path.basename(path)<EOL>fileroot = os.path.splitext(basename)[<NUM_LIT:0>]<EOL>for pf in self.module_postfixes:<EOL><INDENT>if fileroot.endswith(pf):<EOL><INDENT>ret = True<EOL>break<EOL><DEDENT><DEDENT>if not ret:<EOL><INDENT>for pf in self.module_prefixes:<EOL><INDENT>if fileroot.startswith(pf):<EOL><INDENT>ret = True<EOL>break<EOL><DEDENT><DEDENT><DEDENT>return ret<EOL>

Returns true if the passed in path is a test module path

        :param path: string, the path to check, will need to start or end with the
            module test prefixes or postfixes to be considered valid
        :returns: boolean, True if a test module path, False otherwise


system_d = SitePackagesDir()<EOL>filter_system_d = system_d and os.path.commonprefix([system_d, basedir]) != system_d<EOL>for root, dirs, files in os.walk(basedir, topdown=True):<EOL><INDENT>dirs[:] = [d for d in dirs if d[<NUM_LIT:0>] != '<STR_LIT:.>' and d[<NUM_LIT:0>] != "<STR_LIT:_>"]<EOL>if filter_system_d:<EOL><INDENT>dirs[:] = [d for d in dirs if not d.startswith(system_d)]<EOL><DEDENT>yield root, dirs, files<EOL><DEDENT>

Walk all the directories of basedir except hidden directories

        :param basedir: string, the directory to walk
        :returns: generator, same as os.walk


module_name = getattr(self, '<STR_LIT>', '<STR_LIT>')<EOL>module_prefix = getattr(self, '<STR_LIT>', '<STR_LIT>')<EOL>filepath = getattr(self, '<STR_LIT>', '<STR_LIT>')<EOL>if filepath:<EOL><INDENT>if os.path.isabs(filepath):<EOL><INDENT>yield filepath<EOL><DEDENT>else:<EOL><INDENT>yield os.path.join(self.basedir, filepath)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if module_prefix:<EOL><INDENT>basedirs = self._find_prefix_paths(self.basedir, module_prefix)<EOL><DEDENT>else:<EOL><INDENT>basedirs = [self.basedir]<EOL><DEDENT>for basedir in basedirs:<EOL><INDENT>try:<EOL><INDENT>if module_name:<EOL><INDENT>path = self._find_module_path(basedir, module_name)<EOL><DEDENT>else:<EOL><INDENT>path = basedir<EOL><DEDENT>if os.path.isfile(path):<EOL><INDENT>logger.debug('<STR_LIT>'.format(path))<EOL>yield path<EOL><DEDENT>else:<EOL><INDENT>seen_paths = set()<EOL>for root, dirs, files in self.walk(path):<EOL><INDENT>for basename in files:<EOL><INDENT>if basename.startswith("<STR_LIT>"):<EOL><INDENT>if self._is_module_path(root):<EOL><INDENT>filepath = os.path.join(root, basename)<EOL>if filepath not in seen_paths:<EOL><INDENT>logger.debug('<STR_LIT>'.format(filepath))<EOL>seen_paths.add(filepath)<EOL>yield filepath<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>fileroot = os.path.splitext(basename)[<NUM_LIT:0>]<EOL>for pf in self.module_postfixes:<EOL><INDENT>if fileroot.endswith(pf):<EOL><INDENT>filepath = os.path.join(root, basename)<EOL>if filepath not in seen_paths:<EOL><INDENT>logger.debug('<STR_LIT>'.format(filepath))<EOL>seen_paths.add(filepath)<EOL>yield filepath<EOL><DEDENT><DEDENT><DEDENT>for pf in self.module_prefixes:<EOL><INDENT>if fileroot.startswith(pf):<EOL><INDENT>filepath = os.path.join(root, basename)<EOL>if filepath not in seen_paths:<EOL><INDENT>logger.debug('<STR_LIT>'.format(filepath))<EOL>seen_paths.add(filepath)<EOL>yield filepath<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>except IOError as e:<EOL><INDENT>logger.warning(e, exc_info=True)<EOL>pass<EOL><DEDENT><DEDENT><DEDENT>

given a basedir, yield all test modules paths recursively found in
basedir that are test modules

return -- generator


possible_modbits = re.split('<STR_LIT>', filepath.strip('<STR_LIT>'))<EOL>basename = possible_modbits[-<NUM_LIT:1>]<EOL>prefixes = possible_modbits[<NUM_LIT:0>:-<NUM_LIT:1>]<EOL>modpath = []<EOL>discarded = []<EOL>for i in range(len(prefixes)):<EOL><INDENT>path_args = ["<STR_LIT:/>"]<EOL>path_args.extend(prefixes[<NUM_LIT:0>:i+<NUM_LIT:1>])<EOL>path_args.append('<STR_LIT>')<EOL>prefix_module = os.path.join(*path_args)<EOL>if os.path.isfile(prefix_module):<EOL><INDENT>modpath = prefixes[i:]<EOL>break<EOL><DEDENT>else:<EOL><INDENT>discarded = path_args[<NUM_LIT:0>:-<NUM_LIT:1>]<EOL><DEDENT><DEDENT>modpath.append(basename)<EOL>module_name = '<STR_LIT:.>'.join(modpath)<EOL>module_name = re.sub(r'<STR_LIT>', '<STR_LIT>', module_name, flags=re.I)<EOL>logger.debug("<STR_LIT>".format(module_name, filepath))<EOL>return module_name<EOL>

given a filepath like /base/path/to/module.py this will convert it to
        path.to.module so it can be imported


counts = {}<EOL>ks = [<EOL>('<STR_LIT>', "<STR_LIT>"),<EOL>('<STR_LIT>', "<STR_LIT>"),<EOL>('<STR_LIT>', "<STR_LIT>"),<EOL>]<EOL>for ek, cn in ks:<EOL><INDENT>counts[cn] = int(os.environ.get(ek, <NUM_LIT:0>))<EOL><DEDENT>return counts<EOL>

return test counts that are set via pyt environment variables when pyt
    runs the test

    :returns: dict, 3 keys (classes, tests, modules) and how many tests of each
        were found by pyt


ret = False<EOL>counts = get_counts()<EOL>if counts["<STR_LIT>"] < <NUM_LIT:1> and counts["<STR_LIT>"] < <NUM_LIT:1>:<EOL><INDENT>ret = counts["<STR_LIT>"] > <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>ret = counts["<STR_LIT>"] <= <NUM_LIT:1> and counts["<STR_LIT>"] <= <NUM_LIT:1><EOL><DEDENT>return ret<EOL>

Returns True if only a single class is being run or some tests within a single class


if not is_single_class():<EOL><INDENT>raise SkipTest(msg)<EOL><DEDENT>

will raise msg if multiple classes are being ran


ret = False<EOL>counts = get_counts()<EOL>if counts["<STR_LIT>"] == <NUM_LIT:1>:<EOL><INDENT>ret = True<EOL><DEDENT>elif counts["<STR_LIT>"] < <NUM_LIT:1>:<EOL><INDENT>ret = is_single_class()<EOL><DEDENT>return ret<EOL>

Returns True if only a module is being run


if not is_single_module():<EOL><INDENT>raise SkipTest(msg)<EOL><DEDENT>

Will raise msg if multiple modules are being ran


if isinstance(v, type):<EOL><INDENT>ret = strclass(v)<EOL><DEDENT>else:<EOL><INDENT>ret = strclass(v.__class__)<EOL><DEDENT>return ret<EOL>

given a class/instance return the full class path (eg, prefix.module.Classname)

    :param v: class or instance
    :returns: string, the full classpath of v


if len(sequences) == <NUM_LIT:1>:<EOL><INDENT>sequences = sequences[<NUM_LIT:0>]<EOL><DEDENT>return itertools.chain(*sequences)<EOL>

wrapper around itertools.chain

    :param *sequences: one or more sequences (eg, list, tuple, iterator) that you
        want to iterate through one right after another, if you pass in one sequence
        it will be assumed to be a list of sequences
    :returns: iterator, a generator that will work through each passed sequence


name = __name__.split("<STR_LIT:.>")[<NUM_LIT:0>]<EOL>return name<EOL>

Returns the main module name (so this should return "pyt")


Members = namedtuple("<STR_LIT>", ["<STR_LIT:name>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>"])<EOL>log_manager = logging.Logger.manager<EOL>loggers = []<EOL>ignore = set([modname()])<EOL>if log_manager.root:<EOL><INDENT>loggers = list(log_manager.loggerDict.items())<EOL>loggers.append(("<STR_LIT:root>", log_manager.root))<EOL><DEDENT>for logger_name, logger in loggers:<EOL><INDENT>if logger_name in ignore: continue<EOL>for handler in getattr(logger, "<STR_LIT>", []):<EOL><INDENT>members = inspect.getmembers(handler)<EOL>for member_name, member in members:<EOL><INDENT>yield Members(logger_name, handler, member_name, member)<EOL><DEDENT><DEDENT><DEDENT>

iterate through the attributes of every logger's handler

    this is used to switch out stderr and stdout in tests when buffer is True

    :returns: generator of tuples, each tuple has (name, handler, member_name, member_val)


tc = PathFinder(<EOL>self.cwd,<EOL>module_name=self.module_name, <EOL>prefix=self.prefix<EOL>)<EOL>return tc<EOL>

return a PathFinder instance for this module


if os.path.exists(filename):<EOL><INDENT>os.remove(filename)<EOL><DEDENT>

Deletes the specified file from the local filesystem.


def _delete_s3(self, filename, bucket_name):

conn = S3Connection(self.access_key_id, self.access_key_secret)<EOL>bucket = conn.get_bucket(bucket_name)<EOL>if type(filename).__name__ == '<STR_LIT>':<EOL><INDENT>filename = '<STR_LIT:/>' + filename.name<EOL><DEDENT>path = self._get_s3_path(filename)<EOL>k = Key(bucket)<EOL>k.key = path<EOL>try:<EOL><INDENT>bucket.delete_key(k)<EOL><DEDENT>except S3ResponseError:<EOL><INDENT>pass<EOL><DEDENT>

Deletes the specified file from the given S3 bucket.


def delete(self, filename, storage_type=None, bucket_name=None):

if not (storage_type and bucket_name):<EOL><INDENT>self._delete_local(filename)<EOL><DEDENT>else:<EOL><INDENT>if storage_type != '<STR_LIT>':<EOL><INDENT>raise ValueError('<STR_LIT>' % storage_type)<EOL><DEDENT>self._delete_s3(filename, bucket_name)<EOL><DEDENT>

Deletes the specified file, either locally or from S3, depending on the file's storage type.


def _save_local(self, temp_file, filename, obj):

path = self._get_path(filename)<EOL>if not os.path.exists(os.path.dirname(path)):<EOL><INDENT>os.makedirs(os.path.dirname(path), self.permission | <NUM_LIT>)<EOL><DEDENT>fd = open(path, '<STR_LIT:wb>')<EOL>temp_file.seek(<NUM_LIT:0>)<EOL>t = temp_file.read(<NUM_LIT>)<EOL>while t:<EOL><INDENT>fd.write(t)<EOL>t = temp_file.read(<NUM_LIT>)<EOL><DEDENT>fd.close()<EOL>if self.filesize_field:<EOL><INDENT>setattr(obj, self.filesize_field, os.path.getsize(path))<EOL><DEDENT>return filename<EOL>

Saves the specified file to the local file system.


def _save_s3(self, temp_file, filename, obj):

conn = S3Connection(self.access_key_id, self.access_key_secret)<EOL>bucket = conn.get_bucket(self.bucket_name)<EOL>path = self._get_s3_path(filename)<EOL>k = bucket.new_key(path)<EOL>k.set_contents_from_string(temp_file.getvalue())<EOL>k.set_acl(self.acl)<EOL>if self.filesize_field:<EOL><INDENT>setattr(obj, self.filesize_field, k.size)<EOL><DEDENT>return filename<EOL>

Saves the specified file to the configured S3 bucket.


def save(self, temp_file, filename, obj):

if not (self.storage_type and self.bucket_name):<EOL><INDENT>ret = self._save_local(temp_file, filename, obj)<EOL><DEDENT>else:<EOL><INDENT>if self.storage_type != '<STR_LIT>':<EOL><INDENT>raise ValueError('<STR_LIT>' % self.storage_type)<EOL><DEDENT>ret = self._save_s3(temp_file, filename, obj)<EOL><DEDENT>if self.field_name:<EOL><INDENT>setattr(obj, self.field_name, ret)<EOL><DEDENT>if self.storage_type == '<STR_LIT>':<EOL><INDENT>if self.storage_type_field:<EOL><INDENT>setattr(obj, self.storage_type_field, self.storage_type)<EOL><DEDENT>if self.bucket_name_field:<EOL><INDENT>setattr(obj, self.bucket_name_field, self.bucket_name)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if self.storage_type_field:<EOL><INDENT>setattr(obj, self.storage_type_field, '<STR_LIT>')<EOL><DEDENT>if self.bucket_name_field:<EOL><INDENT>setattr(obj, self.bucket_name_field, '<STR_LIT>')<EOL><DEDENT><DEDENT>return ret<EOL>

Saves the specified file to either S3 or the local filesystem, depending on the currently enabled storage type.


return glob('<STR_LIT>' % path)<EOL>

Finds files by globbing on the local filesystem.


def _find_by_path_s3(self, path, bucket_name):

conn = S3Connection(self.access_key_id, self.access_key_secret)<EOL>bucket = conn.get_bucket(bucket_name)<EOL>s3_path = self._get_s3_path(path)<EOL>return bucket.list(prefix=s3_path)<EOL>

Finds files by licking an S3 bucket's contents by prefix.


def find_by_path(self, path, storage_type=None, bucket_name=None):

if not (storage_type and bucket_name):<EOL><INDENT>return self._find_by_path_local(path)<EOL><DEDENT>else:<EOL><INDENT>if storage_type != '<STR_LIT>':<EOL><INDENT>raise ValueError('<STR_LIT>' % storage_type)<EOL><DEDENT>return self._find_by_path_s3(path, bucket_name)<EOL><DEDENT>

Finds files at the specified path / prefix, either on S3 or on the local filesystem.


main_type, sub_type = TYPE_INFO_RE.match(param_type).groups()<EOL>if main_type in ('<STR_LIT:list>', '<STR_LIT>'):<EOL><INDENT>if sub_type is not None:<EOL><INDENT>sub_type = sub_type.strip()<EOL><DEDENT>if not sub_type:<EOL><INDENT>sub_type = '<STR_LIT:str>'<EOL><DEDENT>sub_match = TYPE_INFO_RE.match(sub_type)<EOL>if sub_match:<EOL><INDENT>sub_type = sub_match.group(<NUM_LIT:1>).lower()<EOL><DEDENT>return [PARAM_TYPE_MAP.setdefault(sub_type, string_types)]<EOL><DEDENT>return PARAM_TYPE_MAP.setdefault(main_type, string_types)<EOL>

Perform param type mapping
This requires a bit of logic since this isn't standardized.
If a type doesn't map, assume str


parsed_interfaces = collections.defaultdict(dict)<EOL>for m, d in iteritems(interfaces):<EOL><INDENT>app, func = m.split('<STR_LIT:.>', <NUM_LIT:1>)<EOL>method = parsed_interfaces[app][func] = {}<EOL>method['<STR_LIT>'] = ['<STR_LIT>', '<STR_LIT>']<EOL>method['<STR_LIT>'] = '<STR_LIT:POST>'<EOL>method['<STR_LIT>'] = {}<EOL>method['<STR_LIT>'] = {}<EOL>for name, type_info in iteritems(dict(d['<STR_LIT>'])):<EOL><INDENT>optionality = '<STR_LIT>'<EOL>param_type = '<STR_LIT:string>'<EOL>type_info = TYPE_INFO_COMMENT_RE.sub('<STR_LIT>', type_info)<EOL>info_pieces = TYPE_INFO_SPLITTER_RE.findall(type_info)<EOL>for info_piece in info_pieces:<EOL><INDENT>if info_piece in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>optionality = info_piece<EOL><DEDENT>elif info_piece == '<STR_LIT>':<EOL><INDENT>optionality = '<STR_LIT>'<EOL>param_type = '<STR_LIT:string>'<EOL><DEDENT>elif info_piece == '<STR_LIT>':<EOL><INDENT>optionality = '<STR_LIT>'<EOL><DEDENT>elif info_piece == '<STR_LIT>':<EOL><INDENT>optionality = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>param_type = info_piece<EOL><DEDENT><DEDENT>method[optionality][name] = map_param_type(param_type)<EOL><DEDENT><DEDENT>return dict(parsed_interfaces)<EOL>

Parse the conduit.query json dict response
This performs the logic of parsing the non-standard params dict
    and then returning a dict Resource can understand


init_py = open(os.path.join(package, '<STR_LIT>')).read()<EOL>return re.search("<STR_LIT>",<EOL>init_py, re.MULTILINE).group(<NUM_LIT:1>)<EOL>

Return package version as listed in `__version__` in `init.py`.


return [dirpath<EOL>for dirpath, dirnames, filenames in os.walk(package)<EOL>if os.path.exists(os.path.join(dirpath, '<STR_LIT>'))]<EOL>

Return root package and all sub-packages.


@cli.command()<EOL>@click.option('<STR_LIT>')<EOL>@click.option('<STR_LIT>', help='<STR_LIT>')<EOL>@click.option('<STR_LIT>', help='<STR_LIT>')<EOL>@click.option('<STR_LIT>', type=click.Choice(['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']))<EOL>@click.argument('<STR_LIT:url>')<EOL>@click.pass_context<EOL>def generate(ctx, url, *args, **kwargs):

file_previews = ctx.obj['<STR_LIT>']<EOL>options = {}<EOL>metadata = kwargs['<STR_LIT>']<EOL>width = kwargs['<STR_LIT:width>']<EOL>height = kwargs['<STR_LIT>']<EOL>output_format = kwargs['<STR_LIT>']<EOL>if metadata:<EOL><INDENT>options['<STR_LIT>'] = metadata.split('<STR_LIT:U+002C>')<EOL><DEDENT>if width:<EOL><INDENT>options.setdefault('<STR_LIT:size>', {})<EOL>options['<STR_LIT:size>']['<STR_LIT:width>'] = width<EOL><DEDENT>if height:<EOL><INDENT>options.setdefault('<STR_LIT:size>', {})<EOL>options['<STR_LIT:size>']['<STR_LIT>'] = height<EOL><DEDENT>if output_format:<EOL><INDENT>options['<STR_LIT>'] = output_format<EOL><DEDENT>results = file_previews.generate(url, **options)<EOL>click.echo(results)<EOL>


@cli.command()<EOL>@click.argument('<STR_LIT>')<EOL>@click.pass_context<EOL>def retrieve(ctx, preview_id, *args, **kwargs):

file_previews = ctx.obj['<STR_LIT>']<EOL>results = file_previews.retrieve(preview_id)<EOL>click.echo(results)<EOL>


def _run_ssh(entries, username, idfile, no_prompt=False, command=None,<EOL>show=None, only=None, sort_by=None, limit=None, tunnel=None,<EOL>num=None, random=False):

_print_entries = num is None<EOL>_print_help = False<EOL>if len(entries) == <NUM_LIT:0>:<EOL><INDENT>exit('<STR_LIT>')<EOL><DEDENT>if no_prompt is True and command is not None:<EOL><INDENT>return _run_ssh_command(entries, username, idfile, command, tunnel)<EOL><DEDENT>elif len(entries) == <NUM_LIT:1> or random is True:<EOL><INDENT>entry = py_random.choice(entries)<EOL>if command is None:<EOL><INDENT>return _connect_ssh(entry, username, idfile, tunnel)<EOL><DEDENT>else:<EOL><INDENT>return _run_ssh_command([entry], username, idfile, command, tunnel)<EOL><DEDENT><DEDENT>elif command is not None:<EOL><INDENT>print(HostEntry.render_entries(entries,<EOL>additional_columns=show,<EOL>only_show=only, numbers=True))<EOL>if no_prompt is False:<EOL><INDENT>get_input("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>.format(cyan(command), len(entries)))<EOL><DEDENT>return _run_ssh_command(entries, username, idfile, command, tunnel)<EOL><DEDENT>else:<EOL><INDENT>while True:<EOL><INDENT>if sort_by is not None:<EOL><INDENT>entries = HostEntry.sort_by(entries, sort_by)<EOL><DEDENT>if limit is not None:<EOL><INDENT>entries = entries[:limit]<EOL><DEDENT>if _print_entries is True:<EOL><INDENT>print(HostEntry.render_entries(entries,<EOL>additional_columns=show,<EOL>only_show=only, numbers=True))<EOL>print('<STR_LIT>' % len(entries))<EOL>_print_entries = False<EOL><DEDENT>if _print_help is True:<EOL><INDENT>cmd_str = green(command) if command is not None else '<STR_LIT>'<EOL>msg = COMMANDS_STRING.format(username=username or '<STR_LIT>',<EOL>idfile=idfile or '<STR_LIT>',<EOL>cur_cmd=cmd_str)<EOL>print(msg)<EOL>_print_help = False<EOL><DEDENT>elif command is not None:<EOL><INDENT>print('<STR_LIT>' % cyan(command))<EOL><DEDENT>msg = '<STR_LIT>' % (cyan('<STR_LIT:h>'),<EOL>cyan('<STR_LIT:q>'))<EOL>if num is not None:<EOL><INDENT>choice = num<EOL><DEDENT>else:<EOL><INDENT>choice = get_input(msg)<EOL><DEDENT>if isinstance(choice, int):<EOL><INDENT>if <NUM_LIT:0> <= choice <= len(entries):<EOL><INDENT>break<EOL><DEDENT>else:<EOL><INDENT>if num is not None:<EOL><INDENT>exit("<STR_LIT>"<EOL>.format(num, len(entries) - <NUM_LIT:1>))<EOL><DEDENT>else:<EOL><INDENT>msg = "<STR_LIT>"<EOL>print(msg % (len(entries) - <NUM_LIT:1>))<EOL><DEDENT><DEDENT><DEDENT>elif choice == '<STR_LIT:x>':<EOL><INDENT>if command is None:<EOL><INDENT>print('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>return _run_ssh_command(entries, username, idfile,<EOL>command, tunnel)<EOL><DEDENT><DEDENT>elif choice == '<STR_LIT:h>':<EOL><INDENT>_print_help = True<EOL><DEDENT>elif choice in ['<STR_LIT:q>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>print('<STR_LIT>')<EOL>return<EOL><DEDENT>else:<EOL><INDENT>commands = choice.split()<EOL>if len(commands) < <NUM_LIT:2>:<EOL><INDENT>print(yellow('<STR_LIT>' % choice))<EOL><DEDENT>else:<EOL><INDENT>cmd = commands[<NUM_LIT:0>]<EOL>if cmd in ['<STR_LIT:u>', '<STR_LIT:i>', '<STR_LIT:p>']:<EOL><INDENT>if cmd == '<STR_LIT:u>':<EOL><INDENT>username = commands[<NUM_LIT:1>]<EOL><DEDENT>elif cmd == '<STR_LIT:i>':<EOL><INDENT>_idfile = commands[<NUM_LIT:1>]<EOL>if not os.path.exists(_idfile):<EOL><INDENT>print(yellow('<STR_LIT>' % _idfile))<EOL>continue<EOL><DEDENT>idfile = _idfile<EOL><DEDENT>elif cmd == '<STR_LIT:p>':<EOL><INDENT>p = commands[<NUM_LIT:1>]<EOL>try:<EOL><INDENT>profile = LsiProfile.load(p)<EOL>_username = profile.username<EOL>_idfile = expanduser(profile.identity_file)<EOL><DEDENT>except LsiProfile.LoadError:<EOL><INDENT>print(yellow('<STR_LIT>' % repr(p)))<EOL>continue<EOL><DEDENT>username = _username<EOL>idfile = _idfile<EOL><DEDENT>print('<STR_LIT>' % green(repr(username)))<EOL>print('<STR_LIT>' % green(repr(idfile)))<EOL><DEDENT>elif cmd == '<STR_LIT:f>':<EOL><INDENT>entries = filter_entries(entries, commands[<NUM_LIT:1>:], [])<EOL>_print_entries = True<EOL><DEDENT>elif cmd == '<STR_LIT:e>':<EOL><INDENT>entries = filter_entries(entries, [], commands[<NUM_LIT:1>:])<EOL>_print_entries = True<EOL><DEDENT>elif cmd == '<STR_LIT:c>':<EOL><INDENT>command = '<STR_LIT:U+0020>'.join(commands[<NUM_LIT:1>:])<EOL><DEDENT>elif cmd == '<STR_LIT>':<EOL><INDENT>try:<EOL><INDENT>limit = int(commands[<NUM_LIT:1>])<EOL>_print_entries = True<EOL><DEDENT>except ValueError:<EOL><INDENT>print(yellow('<STR_LIT>'))<EOL><DEDENT><DEDENT>elif cmd == '<STR_LIT>':<EOL><INDENT>sort_by = commands[<NUM_LIT:1>]<EOL>if sort_by not in show:<EOL><INDENT>show.append(sort_by)<EOL><DEDENT>_print_entries = True<EOL><DEDENT>elif cmd == '<STR_LIT>':<EOL><INDENT>if show is None:<EOL><INDENT>show = commands[<NUM_LIT:1>:]<EOL><DEDENT>else:<EOL><INDENT>show.extend(commands[<NUM_LIT:1>:])<EOL><DEDENT>_print_entries = True<EOL><DEDENT>else:<EOL><INDENT>print(yellow('<STR_LIT>' % cmd))<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return _connect_ssh(entries[choice], username, idfile, tunnel)<EOL><DEDENT>

Lets the user choose which instance to SSH into.

:param entries: The list of host entries.
:type entries: [:py:class:`HostEntry`]
:param username: The SSH username to use. Defaults to current user.
:type username: ``str`` or ``NoneType``
:param idfile: The identity file to use. Optional.
:type idfile: ``str`` or ``NoneType``
:param no_prompt: Whether to disable confirmation for SSH command.
:type no_prompt: ``bool``
:param command: SSH command to run on matching instances.
:type command: ``str`` or ``NoneType``
:param show: Instance attributes to show in addition to defaults.
:type show: ``NoneType`` or ``list`` of ``str``
:param only: If not ``None``, will *only* show these attributes.
:type only: ``NoneType`` or ``list`` of ``str``
:param sort_by: What to sort columns by. By default, sort by 'name'.
:type sort_by: ``str``
:param limit: At most how many results to show.
:type limit: ``int`` or ``NoneType``
:param num: If not None, choose the given entry from within filters.
:type num: ``int`` or ``NoneType``
:param random: If true, choose a random entry from within filters.
:type random: ``bool``


if cmd in _PATHS:<EOL><INDENT>return _PATHS[cmd]<EOL><DEDENT>out = subprocess.check_output('<STR_LIT>'.format(cmd), shell=True)<EOL>_PATHS[cmd] = out.decode("<STR_LIT:utf-8>").strip()<EOL>return _PATHS[cmd]<EOL>

Queries bash to find the path to a commmand on the system.


def _build_ssh_command(hostname, username, idfile, ssh_command, tunnel):

command = [_get_path('<STR_LIT>'),<EOL>'<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>']<EOL>if idfile is not None:<EOL><INDENT>command.extend(['<STR_LIT>', idfile])<EOL><DEDENT>if tunnel is not None:<EOL><INDENT>command.extend(['<STR_LIT>', '<STR_LIT>', tunnel, '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'])<EOL><DEDENT>if username is not None:<EOL><INDENT>command.append('<STR_LIT>'.format(username, hostname))<EOL><DEDENT>else:<EOL><INDENT>command.append(hostname)<EOL><DEDENT>if ssh_command is not None:<EOL><INDENT>command.append(repr(ssh_command))<EOL><DEDENT>return('<STR_LIT:U+0020>'.join(command))<EOL>

Uses hostname and other info to construct an SSH command.


def _build_scp_command(hostname, username, idfile, is_get,<EOL>local_path, remote_path):

if hostname.strip() == '<STR_LIT>' or hostname is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>command = [_get_path('<STR_LIT>'),<EOL>'<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>'.format(_KNOWN_HOSTS_FILE)]<EOL>if idfile is not None:<EOL><INDENT>command.extend(['<STR_LIT>', idfile])<EOL><DEDENT>if username is not None:<EOL><INDENT>hostname = '<STR_LIT>' % (username, hostname)<EOL><DEDENT>remote_path = '<STR_LIT>'.format(hostname, remote_path)<EOL>if is_get:<EOL><INDENT>command.extend([remote_path, local_path])<EOL><DEDENT>else:<EOL><INDENT>command.extend([local_path, remote_path])<EOL><DEDENT>return '<STR_LIT:U+0020>'.join(command)<EOL>

Uses hostname and other info to construct an SCP command.

:param hostname: The hostname of the remote machine.
:type hostname: ``str``
:param username: The username to use on the remote machine.
:type username: ``str``
:param idfile: A path to the identity file to use.
:type idfile: ``str``
:param is_get: If true, we are getting a file rather than putting a file.
:type is_get: ``bool``
:param local_path: The path on the local file system.
:type local_path: ``str``
:param remote_path: The path on the remote file system.
:type remote_path: ``str``


def _copy_to(entries, remote_path, local_path, profile):

commands = []<EOL>for entry in entries:<EOL><INDENT>hname = entry.hostname or entry.public_ip<EOL>cmd = _build_scp_command(hname, profile.username,<EOL>profile.identity_file,<EOL>is_get=False,<EOL>local_path=local_path,<EOL>remote_path=remote_path)<EOL>print('<STR_LIT>', cmd)<EOL>commands.append({<EOL>'<STR_LIT>': cmd,<EOL>'<STR_LIT:description>': entry.display()<EOL>})<EOL><DEDENT>stream_commands(commands)<EOL>print(green('<STR_LIT>'))<EOL>

Performs an SCP command where the remote_path is the target and the
local_path is the source.

:param entries: A list of entries.
:type entries: ``list`` of :py:class:`HostEntry`
:param remote_path: The target path on the remote machine(s).
:type remote_path: ``str``
:param local_path: The source path on the local machine.
:type local_path: ``str``
:param profile: The profile, holding username/idfile info, etc.
:type profile: :py:class:`Profile`


def _copy_from(entries, remote_path, local_path, profile):

commands = []<EOL>paths = set()<EOL>for entry in entries:<EOL><INDENT>hname = entry.hostname or entry.public_ip<EOL>_local_path = entry.format_string(local_path)<EOL>if _local_path in paths:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>.format(local_path))<EOL><DEDENT>paths.add(_local_path)<EOL>_folder = os.path.split(_local_path)[<NUM_LIT:0>]<EOL>if len(_folder) > <NUM_LIT:0>:<EOL><INDENT>if not os.path.exists(_folder):<EOL><INDENT>print('<STR_LIT>' + _folder)<EOL>os.makedirs(_folder)<EOL><DEDENT><DEDENT>cmd = _build_scp_command(hname, profile.username,<EOL>profile.identity_file,<EOL>is_get=True,<EOL>local_path=_local_path,<EOL>remote_path=remote_path)<EOL>print('<STR_LIT>', cmd)<EOL>commands.append({<EOL>'<STR_LIT>': cmd,<EOL>'<STR_LIT:description>': entry.display()<EOL>})<EOL><DEDENT>stream_commands(commands)<EOL>print(green('<STR_LIT>'))<EOL>

Performs an SCP command where the remote_path is the source and the
local_path is a format string, formatted individually for each host
being copied from so as to create one or more distinct paths on the
local system.

:param entries: A list of entries.
:type entries: ``list`` of :py:class:`HostEntry`
:param remote_path: The source path on the remote machine(s).
:type remote_path: ``str``
:param local_path: A format string for the path on the local machine.
:type local_path: ``str``
:param profile: The profile, holding username/idfile info, etc.
:type profile: :py:class:`Profile`


def _run_ssh_command(entries, username, idfile, command, tunnel,<EOL>parallel=False):

if len(entries) == <NUM_LIT:0>:<EOL><INDENT>print('<STR_LIT>')<EOL>return <NUM_LIT:1><EOL><DEDENT>if command.strip() == '<STR_LIT>' or command is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>print('<STR_LIT>'<EOL>.format(green(repr(command)), len(entries)))<EOL>shell_cmds = []<EOL>for entry in entries:<EOL><INDENT>hname = entry.hostname or entry.public_ip<EOL>cmd = _build_ssh_command(hname, username, idfile, command, tunnel)<EOL>shell_cmds.append({<EOL>'<STR_LIT>': cmd,<EOL>'<STR_LIT:description>': entry.display()<EOL>})<EOL><DEDENT>stream_commands(shell_cmds, parallel=parallel)<EOL>print(green('<STR_LIT>'))<EOL>

Runs the given command over SSH in parallel on all hosts in `entries`.

:param entries: The host entries the hostnames from.
:type entries: ``list`` of :py:class:`HostEntry`
:param username: To use a specific username.
:type username: ``str`` or ``NoneType``
:param idfile: The SSH identity file to use, or none.
:type idfile: ``str`` or ``NoneType``
:param command: The command to run.
:type command: ``str``
:param parallel: If true, commands will be run in parallel.
:type parallel: ``bool``


def _connect_ssh(entry, username, idfile, tunnel=None):

if entry.hostname != "<STR_LIT>" and entry.hostname is not None:<EOL><INDENT>_host = entry.hostname<EOL><DEDENT>elif entry.public_ip != "<STR_LIT>" and entry.public_ip is not None:<EOL><INDENT>_host = entry.public_ip<EOL><DEDENT>elif entry.private_ip != "<STR_LIT>" and entry.private_ip is not None:<EOL><INDENT>if tunnel is None:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>_host = entry.private_ip<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>command = _build_ssh_command(_host, username, idfile, None, tunnel)<EOL>print('<STR_LIT>' % cyan(entry.display()))<EOL>print('<STR_LIT>' % green(command))<EOL>proc = subprocess.Popen(command, shell=True)<EOL>return proc.wait()<EOL>

SSH into to a host.

:param entry: The host entry to pull the hostname from.
:type entry: :py:class:`HostEntry`
:param username: To use a specific username.
:type username: ``str`` or ``NoneType``
:param idfile: The SSH identity file to use, if supplying a username.
:type idfile: ``str`` or ``NoneType``
:param tunnel: Host to tunnel SSH command through.
:type tunnel: ``str`` or ``NoneType``

:return: An exit status code.
:rtype: ``int``


from __init__ import __version__<EOL>print(__version__)<EOL>sys.exit(<NUM_LIT:0>)<EOL>


parser = argparse.ArgumentParser(description='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', '<STR_LIT>', action='<STR_LIT:store_true>', default=False,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', action='<STR_LIT:store_true>', default=False,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', action='<STR_LIT:store_true>', default=False,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', help='<STR_LIT>',<EOL>default=None)<EOL>parser.add_argument('<STR_LIT>', '<STR_LIT>', action='<STR_LIT:store_true>',<EOL>help='<STR_LIT>', default=False)<EOL>parser.add_argument('<STR_LIT>', '<STR_LIT>', help='<STR_LIT>',<EOL>default=None)<EOL>parser.add_argument('<STR_LIT>', '<STR_LIT>', default=None,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', nargs='<STR_LIT:*>',<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', '<STR_LIT>', nargs='<STR_LIT:+>',<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT:-c>', '<STR_LIT>', type=str,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', '<STR_LIT>', action='<STR_LIT:store_true>', default=False,<EOL>help="<STR_LIT>"<EOL>"<STR_LIT>")<EOL>parser.add_argument('<STR_LIT>', '<STR_LIT>', type=str,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', nargs='<STR_LIT:+>', default=None,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', nargs='<STR_LIT:+>', default=None,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', type=str, default=None,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', type=str, default="<STR_LIT:name>",<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', '<STR_LIT>', type=int, default=None,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', action='<STR_LIT:store_true>',<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', nargs=<NUM_LIT:2>, default=None,<EOL>help='<STR_LIT>'<EOL>'<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', nargs=<NUM_LIT:2>, default=None,<EOL>help='<STR_LIT>'<EOL>'<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', '<STR_LIT>', default=None,<EOL>help='<STR_LIT>')<EOL>parser.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store_true>", default=False,<EOL>help="<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", "<STR_LIT>", type=int, default=None,<EOL>help="<STR_LIT>")<EOL>args = parser.parse_args()<EOL>if args.exclude is None:<EOL><INDENT>args.exclude = []<EOL><DEDENT>if args.sort_by is not None:<EOL><INDENT>args.show = (args.show or []) + [args.sort_by]<EOL><DEDENT>return args<EOL>


if value is not None:<EOL><INDENT>setattr(self, attrib, value)<EOL><DEDENT>

Overrides an attribute, unless the overriding value is None.


lsi_location = os.path.expanduser('<STR_LIT>')<EOL>if not os.path.exists(lsi_location):<EOL><INDENT>return LsiProfile()<EOL><DEDENT>cfg_parser = ConfigParser()<EOL>cfg_parser.read(lsi_location)<EOL>if profile_name is None:<EOL><INDENT>if cfg_parser.has_section('<STR_LIT:default>'):<EOL><INDENT>profile_name = '<STR_LIT:default>'<EOL><DEDENT>else:<EOL><INDENT>return cls()<EOL><DEDENT><DEDENT>elif not cfg_parser.has_section(profile_name):<EOL><INDENT>raise cls.LoadError('<STR_LIT>'.format(profile_name))<EOL><DEDENT>def _get(option, alt=None):<EOL><INDENT>"""<STR_LIT>"""<EOL>if cfg_parser.has_option(profile_name, option):<EOL><INDENT>return cfg_parser.get(profile_name, option)<EOL><DEDENT>else:<EOL><INDENT>return alt<EOL><DEDENT><DEDENT>if cfg_parser.has_option(profile_name, '<STR_LIT>'):<EOL><INDENT>profile = cls.load(cfg_parser.get(profile_name, '<STR_LIT>'))<EOL><DEDENT>else:<EOL><INDENT>profile = cls()<EOL><DEDENT>profile.override('<STR_LIT:username>', _get('<STR_LIT:username>'))<EOL>profile.override('<STR_LIT>', _get('<STR_LIT>'))<EOL>profile.override('<STR_LIT>', _get('<STR_LIT>'))<EOL>filters = [s for s in _get('<STR_LIT>', '<STR_LIT>').split('<STR_LIT:U+002C>') if len(s) > <NUM_LIT:0>]<EOL>exclude = [s for s in _get('<STR_LIT>', '<STR_LIT>').split('<STR_LIT:U+002C>') if len(s) > <NUM_LIT:0>]<EOL>profile.filters.extend(filters)<EOL>profile.exclude.extend(exclude)<EOL>return profile<EOL>

Loads the user's LSI profile, or provides a default.


<EOL>if args.username is not None or args.identity_file is not None:<EOL><INDENT>profile = LsiProfile()<EOL><DEDENT>else:<EOL><INDENT>profile = LsiProfile.load(args.profile)<EOL><DEDENT>profile.override('<STR_LIT:username>', args.username)<EOL>profile.override('<STR_LIT>', args.identity_file)<EOL>profile.override('<STR_LIT>', args.command)<EOL>profile.no_prompt = args.no_prompt<EOL>profile.filters.extend(args.filters)<EOL>profile.exclude.extend(args.exclude)<EOL>if profile.identity_file is not None:<EOL><INDENT>profile.identity_file = os.path.expanduser(profile.identity_file)<EOL><DEDENT>return profile<EOL>

Takes arguments parsed from argparse and returns a profile.


def render_columns(columns, write_borders=True, column_colors=None):

if column_colors is not None and len(column_colors) != len(columns):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>widths = [max(len(cell) for cell in column) for column in columns]<EOL>max_column_length = max(len(column) for column in columns)<EOL>result = '<STR_LIT:\n>'.join(render_row(i, columns, widths, column_colors)<EOL>for i in range(max_column_length))<EOL>if write_borders:<EOL><INDENT>border = '<STR_LIT>' % '<STR_LIT:|>'.join('<STR_LIT:->' * (w + <NUM_LIT:2>) for w in widths)<EOL>return '<STR_LIT>' % (border, result, border)<EOL><DEDENT>else:<EOL><INDENT>return result<EOL><DEDENT>

Renders a list of columns.

:param columns: A list of columns, where each column is a list of strings.
:type columns: [[``str``]]
:param write_borders: Whether to write the top and bottom borders.
:type write_borders: ``bool``
:param column_colors: A list of coloring functions, one for each column.
                      Optional.
:type column_colors: [``str`` -> ``str``] or ``NoneType``

:return: The rendered columns.
:rtype: ``str``


def render_row(num, columns, widths, column_colors=None):

row_str = '<STR_LIT:|>'<EOL>cell_strs = []<EOL>for i, column in enumerate(columns):<EOL><INDENT>try:<EOL><INDENT>cell = column[num]<EOL>spaces = '<STR_LIT:U+0020>' * (widths[i] - len(cell))<EOL>if column_colors is not None and column_colors[i] is not None:<EOL><INDENT>cell = column_colors[i](cell)<EOL><DEDENT>cell_strs.append('<STR_LIT>' % (cell, spaces))<EOL><DEDENT>except IndexError:<EOL><INDENT>cell_strs.append('<STR_LIT:U+0020>' * (widths[i] + <NUM_LIT:2>))<EOL><DEDENT><DEDENT>return '<STR_LIT>' % '<STR_LIT:|>'.join(cell_strs)<EOL>

Render the `num`th row of each column in `columns`.

:param num: Which row to render.
:type num: ``int``
:param columns: The list of columns.
:type columns: [[``str``]]
:param widths: The widths of each column.
:type widths: [``int``]
:param column_colors: An optional list of coloring functions.
:type column_colors: [``str`` -> ``str``] or ``NoneType``

:return: The rendered row.
:rtype: ``str``


def render_table(table, write_borders=True, column_colors=None):

prepare_rows(table)<EOL>columns = transpose_table(table)<EOL>return render_columns(columns, write_borders, column_colors)<EOL>

Renders a table. A table is a list of rows, each of which is a list
of arbitrary objects. The `.str` method will be called on each element
of the row. Jagged tables are ok; in this case, each row will be expanded
to the maximum row length.

:param table: A list of rows, as described above.
:type table: [[``object``]]
:param write_borders: Whether there should be a border on the top and
                      bottom. Defaults to ``True``.
:type write_borders: ``bool``
:param column_colors: An optional list of coloring *functions* to be
                      applied to each cell in each column. If provided,
                      the list's length must be equal to the maximum
                      number of columns. ``None`` can be mixed in to this
                      list so that a selection of columns can be colored.
:type column_colors: [``str`` -> ``str``] or ``NoneType``

:return: The rendered table.
:rtype: ``str``


if len(table) == <NUM_LIT:0>:<EOL><INDENT>return table<EOL><DEDENT>else:<EOL><INDENT>num_columns = len(table[<NUM_LIT:0>])<EOL>return [[row[i] for row in table] for i in range(num_columns)]<EOL><DEDENT>

Transposes a table, turning rows into columns.
:param table: A 2D string grid.
:type table: [[``str``]]

:return: The same table, with rows and columns flipped.
:rtype: [[``str``]]


num_columns = max(len(row) for row in table)<EOL>for row in table:<EOL><INDENT>while len(row) < num_columns:<EOL><INDENT>row.append('<STR_LIT>')<EOL><DEDENT>for i in range(num_columns):<EOL><INDENT>row[i] = str(row[i]) if row[i] is not None else '<STR_LIT>'<EOL><DEDENT><DEDENT>return table<EOL>

Prepare the rows so they're all strings, and all the same length.

:param table: A 2D grid of anything.
:type table: [[``object``]]

:return: A table of strings, where every row is the same length.
:rtype: [[``str``]]