<commit_before># Copyright (c) 2015, Djaodjin Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from bs4 import BeautifulSoup
from django.conf import settings as django_settings
from django.core.mail import EmailMultiAlternatives
from django.template import Context, RequestContext
from django.template.loader_tags import BlockNode, ExtendsNode
from django.utils.html import strip_tags
from django.template import TemplateDoesNotExist, Template as BaseTemplate

from premailer import Premailer

from .. import settings
from ..compat import BaseEngine, _dirs_undefined, import_string

def build_absolute_uri(request, location='', site=None):
    if settings.BUILD_ABSOLUTE_URI_CALLABLE:
        return import_string(settings.BUILD_ABSOLUTE_URI_CALLABLE)(
            request, location=location, site=site)
    return request.build_absolute_uri(location=location)

class EmlTemplateError(Exception):
    pass

class EmlEngine(BaseEngine):
    #pylint: disable=no-member

    app_dirname = 'eml'

    def __init__(self, params):
        self.name = params.get('NAME')
        dirs = list(params.get('DIRS', []))
        app_dirs = bool(params.get('APP_DIRS', False))
        options = params.get('OPTIONS', {})
        super(EmlEngine, self).__init__(dirs=dirs, app_dirs=app_dirs, **options)

    def find_template(self, template_name, dirs=None, skip=None):
        tried = []
        for loader in self.template_loaders:
            if hasattr(loader, 'get_contents'):
                # From Django 1.9, this is the code that should be executed.
                for origin in loader.get_template_sources(
                        template_name, template_dirs=dirs):
                    if skip is not None and origin in skip:
                        tried.append((origin, 'Skipped'))
                        continue
                    try:
                        contents = loader.get_contents(origin)
                    except TemplateDoesNotExist:
                        tried.append((origin, 'Source does not exist'))
                        continue
                    else:
                        template = Template(
                            contents, origin, origin.template_name, self)
                        return template, template.origin
            else:
                # This code is there to support Django 1.8 only.
                try:
                    source, template_path = loader.load_template_source(
                        template_name, template_dirs=dirs)
                    origin = self.make_origin(
                        template_path, loader.load_template_source,
                        template_name, dirs)
                    template = Template(source, origin, template_path, self)
                    return template, template.origin
                except TemplateDoesNotExist:
                    pass
        raise TemplateDoesNotExist(template_name, tried=tried)

    def get_template(self, template_name, dirs=_dirs_undefined):
        if template_name and template_name.endswith('.eml'):
            return super(EmlEngine, self).get_template(template_name, dirs=dirs)
        raise TemplateDoesNotExist(template_name)

class Template(BaseTemplate):
    """
    Sends an email to a list of recipients (i.e. email addresses).
    """

    def __init__(self, template_string, origin=None, name=None, engine=None):
        if engine is not None:
            kwargs = {'engine': engine}
        else:
            kwargs = {}
        super(Template, self).__init__(template_string, origin=origin,
            name=name, **kwargs)
        self.origin = origin

    def _send(self, recipients, context, from_email=None, bcc=None, cc=None,
              reply_to=None, attachments=None):
        #pylint: disable=too-many-locals
        if reply_to:
            headers = {'Reply-To': reply_to}
        else:
            headers = None

        nodes = self
        extend = None
        subject = None
        html_content = None
        plain_content = None

        # Check if need to extend from base
        if isinstance(self.nodelist[0], ExtendsNode):
            extend = self.nodelist[0]
            nodes = self.nodelist[0].nodelist

        for node in nodes:
            if isinstance(node, BlockNode):
                if node.name == 'subject':
                    # Email subject *must not* contain newlines
                    subject = ''.join(node.render(context).splitlines())
                elif node.name == 'html_content':
                    html_content = node.render(context)
                    if extend:
                        soup = BeautifulSoup(html_content, 'html.parser')
                    else:
                        soup = BeautifulSoup(html_content, 'lxml')

                    for lnk in soup.find_all('a'):
                        href = lnk.get('href')
                        if href and href.startswith('/'):
                            # 'django.core.context_processors.request' must be
                            # present in TEMPLATE_CONTEXT_PROCESSORS.
                            lnk['href'] = build_absolute_uri(
                                context['request'], href)
                    if extend:
                        html_base_content = extend.render(context)
                        soup_base = BeautifulSoup(html_base_content, 'lxml')
                        content_section = soup_base.find(id='content')
                        content_section.insert(0, soup)
                        html_content = soup_base.prettify()
                    else:
                        html_content = soup.prettify()
                elif node.name == 'plain_content':
                    plain_content = node.render(context)

        # Create the email, attach the HTML version.
        if not subject:
            raise EmlTemplateError(
                "Template %s is missing a subject." % self.origin.name)
        if not plain_content:
            # Defaults to content stripped of html tags
            if not html_content:
                raise EmlTemplateError(
                    "Template %s does not contain PLAIN nor HTML content."
                    % self.origin.name)
            plain_content = strip_tags(html_content)
        # XXX implement inline attachments,
        #     reference: https://djangosnippets.org/snippets/3001/
        msg = EmailMultiAlternatives(
            subject, plain_content, from_email, recipients, bcc=bcc, cc=cc,
            attachments=attachments, headers=headers)
        if html_content:
            html_content = Premailer(
                html_content,
                include_star_selectors=True).transform()
            msg.attach_alternative(html_content, "text/html")
        msg.send(fail_silently=False)

    #pylint: disable=invalid-name,too-many-arguments
    def send(self, recipients, context,
             from_email=None, bcc=None, cc=None, reply_to=None,
             attachments=None):
        if not from_email:
            from_email = settings.DEFAULT_FROM_EMAIL
        if not isinstance(context, Context):
            context = RequestContext(None, context)

        if hasattr(context, 'template') and context.template is None:
            with context.bind_template(self):
                context.template_name = self.name
                return self._send(recipients, context, from_email=from_email,
                    bcc=bcc, cc=cc, reply_to=reply_to, attachments=attachments)
        else:
            return self._send(recipients, context, from_email=from_email,
                bcc=bcc, cc=cc, reply_to=reply_to, attachments=attachments)
<commit_msg>fix: request not always present in context<commit_after># Copyright (c) 2015, Djaodjin Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from bs4 import BeautifulSoup
from django.conf import settings as django_settings
from django.core.mail import EmailMultiAlternatives
from django.template import Context, RequestContext
from django.template.loader_tags import BlockNode, ExtendsNode
from django.utils.html import strip_tags
from django.template import TemplateDoesNotExist, Template as BaseTemplate

from premailer import Premailer

from .. import settings
from ..compat import BaseEngine, _dirs_undefined, import_string

def build_absolute_uri(request, location='', site=None):
    if settings.BUILD_ABSOLUTE_URI_CALLABLE:
        return import_string(settings.BUILD_ABSOLUTE_URI_CALLABLE)(
            request, location=location, site=site)
    return request.build_absolute_uri(location=location)

class EmlTemplateError(Exception):
    pass

class EmlEngine(BaseEngine):
    #pylint: disable=no-member

    app_dirname = 'eml'

    def __init__(self, params):
        self.name = params.get('NAME')
        dirs = list(params.get('DIRS', []))
        app_dirs = bool(params.get('APP_DIRS', False))
        options = params.get('OPTIONS', {}).copy()
        libraries = options.get('libraries', {})
        options['libraries'] = self.get_templatetag_libraries(libraries)
        super(EmlEngine, self).__init__(dirs=dirs, app_dirs=app_dirs, **options)

    def get_templatetag_libraries(self, custom_libraries):
        """
        Return a collation of template tag libraries from installed
        applications and the supplied custom_libraries argument.
        """
        from django.template.backends.django import get_installed_libraries
        libraries = get_installed_libraries()
        libraries.update(custom_libraries)
        return libraries

    def find_template(self, template_name, dirs=None, skip=None):
        tried = []
        for loader in self.template_loaders:
            if hasattr(loader, 'get_contents'):
                # From Django 1.9, this is the code that should be executed.
                for origin in loader.get_template_sources(
                        template_name, template_dirs=dirs):
                    if skip is not None and origin in skip:
                        tried.append((origin, 'Skipped'))
                        continue
                    try:
                        contents = loader.get_contents(origin)
                    except TemplateDoesNotExist:
                        tried.append((origin, 'Source does not exist'))
                        continue
                    else:
                        template = Template(
                            contents, origin, origin.template_name, self)
                        return template, template.origin
            else:
                # This code is there to support Django 1.8 only.
                try:
                    source, template_path = loader.load_template_source(
                        template_name, template_dirs=dirs)
                    origin = self.make_origin(
                        template_path, loader.load_template_source,
                        template_name, dirs)
                    template = Template(source, origin, template_path, self)
                    return template, template.origin
                except TemplateDoesNotExist:
                    pass
        raise TemplateDoesNotExist(template_name, tried=tried)

    def get_template(self, template_name, dirs=_dirs_undefined):
        if template_name and template_name.endswith('.eml'):
            return super(EmlEngine, self).get_template(template_name, dirs=dirs)
        raise TemplateDoesNotExist(template_name)

class Template(BaseTemplate):
    """
    Sends an email to a list of recipients (i.e. email addresses).
    """

    def __init__(self, template_string, origin=None, name=None, engine=None):
        if engine is not None:
            kwargs = {'engine': engine}
        else:
            kwargs = {}
        super(Template, self).__init__(template_string, origin=origin,
            name=name, **kwargs)
        self.origin = origin

    def _send(self, recipients, context, from_email=None, bcc=None, cc=None,
              reply_to=None, attachments=None):
        #pylint: disable=too-many-locals
        if reply_to:
            headers = {'Reply-To': reply_to}
        else:
            headers = None

        nodes = self
        extend = None
        subject = None
        html_content = None
        plain_content = None

        # Check if need to extend from base
        if isinstance(self.nodelist[0], ExtendsNode):
            extend = self.nodelist[0]
            nodes = self.nodelist[0].nodelist

        for node in nodes:
            if isinstance(node, BlockNode):
                if node.name == 'subject':
                    # Email subject *must not* contain newlines
                    subject = ''.join(node.render(context).splitlines())
                elif node.name == 'html_content':
                    html_content = node.render(context)
                    if extend:
                        soup = BeautifulSoup(html_content, 'html.parser')
                    else:
                        soup = BeautifulSoup(html_content, 'lxml')

                    for lnk in soup.find_all('a'):
                        href = lnk.get('href')
                        if href and href.startswith('/'):
                            # 'django.core.context_processors.request' must be
                            # present in TEMPLATE_CONTEXT_PROCESSORS.
                            lnk['href'] = build_absolute_uri(
                                context.get('request', None), href)
                    if extend:
                        html_base_content = extend.render(context)
                        soup_base = BeautifulSoup(html_base_content, 'lxml')
                        content_section = soup_base.find(id='content')
                        content_section.insert(0, soup)
                        html_content = soup_base.prettify()
                    else:
                        html_content = soup.prettify()
                elif node.name == 'plain_content':
                    plain_content = node.render(context)

        # Create the email, attach the HTML version.
        if not subject:
            raise EmlTemplateError(
                "Template %s is missing a subject." % self.origin.name)
        if not plain_content:
            # Defaults to content stripped of html tags
            if not html_content:
                raise EmlTemplateError(
                    "Template %s does not contain PLAIN nor HTML content."
                    % self.origin.name)
            plain_content = strip_tags(html_content)
        # XXX implement inline attachments,
        #     reference: https://djangosnippets.org/snippets/3001/
        msg = EmailMultiAlternatives(
            subject, plain_content, from_email, recipients, bcc=bcc, cc=cc,
            attachments=attachments, headers=headers)
        if html_content:
            html_content = Premailer(
                html_content,
                include_star_selectors=True).transform()
            msg.attach_alternative(html_content, "text/html")
        msg.send(fail_silently=False)

    #pylint: disable=invalid-name,too-many-arguments
    def send(self, recipients, context,
             from_email=None, bcc=None, cc=None, reply_to=None,
             attachments=None):
        if not from_email:
            from_email = settings.DEFAULT_FROM_EMAIL
        if not isinstance(context, Context):
            context = RequestContext(None, context)

        if hasattr(context, 'template') and context.template is None:
            with context.bind_template(self):
                context.template_name = self.name
                return self._send(recipients, context, from_email=from_email,
                    bcc=bcc, cc=cc, reply_to=reply_to, attachments=attachments)
        else:
            return self._send(recipients, context, from_email=from_email,
                bcc=bcc, cc=cc, reply_to=reply_to, attachments=attachments)
<|endoftext|>


<commit_before>#
# This code is part of Ansible, but is an independent component.
# This particular file snippet, and this file snippet only, is BSD licensed.
# Modules you write using this snippet, which is embedded dynamically by Ansible
# still belong to the author of the module, and may assign their own license
# to the complete work.
#
# (c) 2017 Red Hat Inc.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright notice,
#      this list of conditions and the following disclaimer in the documentation
#      and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os
import json
import socket
import struct
import traceback
import uuid

from functools import partial
from ansible.module_utils._text import to_bytes, to_text
from ansible.module_utils.six import iteritems

def send_data(s, data):
    packed_len = struct.pack('!Q', len(data))
    return s.sendall(packed_len + data)

def recv_data(s):
    header_len = 8  # size of a packed unsigned long long
    data = to_bytes("")
    while len(data) < header_len:
        d = s.recv(header_len - len(data))
        if not d:
            return None
        data += d
    data_len = struct.unpack('!Q', data[:header_len])[0]
    data = data[header_len:]
    while len(data) < data_len:
        d = s.recv(data_len - len(data))
        if not d:
            return None
        data += d
    return data

def exec_command(module, command):
    connection = Connection(module._socket_path)
    try:
        out = connection.exec_command(command)
    except ConnectionError as exc:
        code = getattr(exc, 'code', 1)
        message = getattr(exc, 'err', exc)
        return code, '', to_text(message, errors='surrogate_then_replace')
    return 0, out, ''

def request_builder(method, *args, **kwargs):
    reqid = str(uuid.uuid4())
    req = {'jsonrpc': '2.0', 'method': method, 'id': reqid}

    params = args or kwargs or None
    if params:
        req['params'] = params

    return req

class ConnectionError(Exception):

    def __init__(self, message, *args, **kwargs):
        super(ConnectionError, self).__init__(message)
        for k, v in iteritems(kwargs):
            setattr(self, k, v)

class Connection(object):

    def __init__(self, socket_path):
        if socket_path is None:
            raise AssertionError('socket_path must be a value')
        self.socket_path = socket_path

    def __getattr__(self, name):
        try:
            return self.__dict__[name]
        except KeyError:
            if name.startswith('_'):
                raise AttributeError("'%s' object has no attribute '%s'" % (self.__class__.__name__, name))
            return partial(self.__rpc__, name)

    def _exec_jsonrpc(self, name, *args, **kwargs):

        req = request_builder(name, *args, **kwargs)
        reqid = req['id']

        if not os.path.exists(self.socket_path):
            raise ConnectionError('socket_path does not exist or cannot be found')

        try:
            data = json.dumps(req)
            out = self.send(data)
            response = json.loads(out)

        except socket.error as e:
            raise ConnectionError('unable to connect to socket', err=to_text(e, errors='surrogate_then_replace'), exception=traceback.format_exc())

        if response['id'] != reqid:
            raise ConnectionError('invalid json-rpc id received')

        return response

    def __rpc__(self, name, *args, **kwargs):
        """Executes the json-rpc and returns the output received
           from remote device.
           :name: rpc method to be executed over connection plugin that implements jsonrpc 2.0
           :args: Ordered list of params passed as arguments to rpc method
           :kwargs: Dict of valid key, value pairs passed as arguments to rpc method

           For usage refer the respective connection plugin docs.
        """

        response = self._exec_jsonrpc(name, *args, **kwargs)

        if 'error' in response:
            err = response.get('error')
            msg = err.get('data') or err['message']
            code = err['code']
            raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)

        return response['result']

    def send(self, data):
        try:
            sf = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
            sf.connect(self.socket_path)

            send_data(sf, to_bytes(data))
            response = recv_data(sf)

        except socket.error as e:
            sf.close()
            raise ConnectionError('unable to connect to socket', err=to_text(e, errors='surrogate_then_replace'), exception=traceback.format_exc())

        sf.close()

        return to_text(response, errors='surrogate_or_strict')
<commit_msg>Add troubleshooting URL for persistent socket path related issue (#38542)<commit_after>#
# This code is part of Ansible, but is an independent component.
# This particular file snippet, and this file snippet only, is BSD licensed.
# Modules you write using this snippet, which is embedded dynamically by Ansible
# still belong to the author of the module, and may assign their own license
# to the complete work.
#
# (c) 2017 Red Hat Inc.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright notice,
#      this list of conditions and the following disclaimer in the documentation
#      and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os
import json
import socket
import struct
import traceback
import uuid

from functools import partial
from ansible.module_utils._text import to_bytes, to_text
from ansible.module_utils.six import iteritems

def send_data(s, data):
    packed_len = struct.pack('!Q', len(data))
    return s.sendall(packed_len + data)

def recv_data(s):
    header_len = 8  # size of a packed unsigned long long
    data = to_bytes("")
    while len(data) < header_len:
        d = s.recv(header_len - len(data))
        if not d:
            return None
        data += d
    data_len = struct.unpack('!Q', data[:header_len])[0]
    data = data[header_len:]
    while len(data) < data_len:
        d = s.recv(data_len - len(data))
        if not d:
            return None
        data += d
    return data

def exec_command(module, command):
    connection = Connection(module._socket_path)
    try:
        out = connection.exec_command(command)
    except ConnectionError as exc:
        code = getattr(exc, 'code', 1)
        message = getattr(exc, 'err', exc)
        return code, '', to_text(message, errors='surrogate_then_replace')
    return 0, out, ''

def request_builder(method, *args, **kwargs):
    reqid = str(uuid.uuid4())
    req = {'jsonrpc': '2.0', 'method': method, 'id': reqid}

    params = args or kwargs or None
    if params:
        req['params'] = params

    return req

class ConnectionError(Exception):

    def __init__(self, message, *args, **kwargs):
        super(ConnectionError, self).__init__(message)
        for k, v in iteritems(kwargs):
            setattr(self, k, v)

class Connection(object):

    def __init__(self, socket_path):
        if socket_path is None:
            raise AssertionError('socket_path must be a value')
        self.socket_path = socket_path

    def __getattr__(self, name):
        try:
            return self.__dict__[name]
        except KeyError:
            if name.startswith('_'):
                raise AttributeError("'%s' object has no attribute '%s'" % (self.__class__.__name__, name))
            return partial(self.__rpc__, name)

    def _exec_jsonrpc(self, name, *args, **kwargs):

        req = request_builder(name, *args, **kwargs)
        reqid = req['id']

        troubleshoot = 'http://docs.ansible.com/ansible/latest/network/user_guide/network_debug_troubleshooting.html#category-socket-path-issue'

        if not os.path.exists(self.socket_path):
            raise ConnectionError('socket_path does not exist or cannot be found. Please check %s' % troubleshoot)

        try:
            data = json.dumps(req)
            out = self.send(data)
            response = json.loads(out)

        except socket.error as e:
            raise ConnectionError('unable to connect to socket. Please check %s' % troubleshoot, err=to_text(e, errors='surrogate_then_replace'),
                                  exception=traceback.format_exc())

        if response['id'] != reqid:
            raise ConnectionError('invalid json-rpc id received')

        return response

    def __rpc__(self, name, *args, **kwargs):
        """Executes the json-rpc and returns the output received
           from remote device.
           :name: rpc method to be executed over connection plugin that implements jsonrpc 2.0
           :args: Ordered list of params passed as arguments to rpc method
           :kwargs: Dict of valid key, value pairs passed as arguments to rpc method

           For usage refer the respective connection plugin docs.
        """

        response = self._exec_jsonrpc(name, *args, **kwargs)

        if 'error' in response:
            err = response.get('error')
            msg = err.get('data') or err['message']
            code = err['code']
            raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)

        return response['result']

    def send(self, data):
        try:
            sf = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
            sf.connect(self.socket_path)

            send_data(sf, to_bytes(data))
            response = recv_data(sf)

        except socket.error as e:
            sf.close()
            raise ConnectionError('unable to connect to socket', err=to_text(e, errors='surrogate_then_replace'), exception=traceback.format_exc())

        sf.close()

        return to_text(response, errors='surrogate_or_strict')
<|endoftext|>


<commit_before># (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible.compat.six import iteritems, text_type

from ansible.errors import AnsibleError
from ansible.executor.play_iterator import PlayIterator
from ansible.playbook.block import Block
from ansible.playbook.included_file import IncludedFile
from ansible.playbook.task import Task
from ansible.plugins import action_loader
from ansible.plugins.strategy import StrategyBase
from ansible.template import Templar

try:
    from __main__ import display
except ImportError:
    from ansible.utils.display import Display
    display = Display()

class StrategyModule(StrategyBase):

    def _get_next_task_lockstep(self, hosts, iterator):
        '''
        Returns a list of (host, task) tuples, where the task may
        be a noop task to keep the iterator in lock step across
        all hosts.
        '''

        noop_task = Task()
        noop_task.action = 'meta'
        noop_task.args['_raw_params'] = 'noop'
        noop_task.set_loader(iterator._play._loader)

        host_tasks = {}
        display.debug("building list of next tasks for hosts")
        for host in hosts:
            host_tasks[host.name] = iterator.get_next_task_for_host(host, peek=True)
        display.debug("done building task lists")

        num_setups = 0
        num_tasks  = 0
        num_rescue = 0
        num_always = 0

        display.debug("counting tasks in each state of execution")
        host_tasks_to_run = [(host, state_task)
                             for host, state_task in iteritems(host_tasks)
                             if state_task and state_task[1]]

        if host_tasks_to_run:
            lowest_cur_block = min(
                (s.cur_block for h, (s, t) in host_tasks_to_run
                if s.run_state != PlayIterator.ITERATING_COMPLETE))
        else:
            # empty host_tasks_to_run will just run till the end of the function
            # without ever touching lowest_cur_block
            lowest_cur_block = None

        for (k, v) in host_tasks_to_run:
            (s, t) = v

            if s.cur_block > lowest_cur_block:
                # Not the current block, ignore it
                continue

            if s.run_state == PlayIterator.ITERATING_SETUP:
                num_setups += 1
            elif s.run_state == PlayIterator.ITERATING_TASKS:
                num_tasks += 1
            elif s.run_state == PlayIterator.ITERATING_RESCUE:
                num_rescue += 1
            elif s.run_state == PlayIterator.ITERATING_ALWAYS:
                num_always += 1
        display.debug("done counting tasks in each state of execution")

        def _advance_selected_hosts(hosts, cur_block, cur_state):
            '''
            This helper returns the task for all hosts in the requested
            state, otherwise they get a noop dummy task. This also advances
            the state of the host, since the given states are determined
            while using peek=True.
            '''
            # we return the values in the order they were originally
            # specified in the given hosts array
            rvals = []
            display.debug("starting to advance hosts")
            for host in hosts:
                host_state_task = host_tasks.get(host.name)
                if host_state_task is None:
                    continue
                (s, t) = host_state_task
                if t is None:
                    continue
                if s.run_state == cur_state and s.cur_block == cur_block:
                    new_t = iterator.get_next_task_for_host(host)
                    rvals.append((host, t))
                else:
                    rvals.append((host, noop_task))
            display.debug("done advancing hosts to next task")
            return rvals

        # if any hosts are in ITERATING_SETUP, return the setup task
        # while all other hosts get a noop
        if num_setups:
            display.debug("advancing hosts in ITERATING_SETUP")
            return _advance_selected_hosts(hosts, lowest_cur_block, PlayIterator.ITERATING_SETUP)

        # if any hosts are in ITERATING_TASKS, return the next normal
        # task for these hosts, while all other hosts get a noop
        if num_tasks:
            display.debug("advancing hosts in ITERATING_TASKS")
            return _advance_selected_hosts(hosts, lowest_cur_block, PlayIterator.ITERATING_TASKS)

        # if any hosts are in ITERATING_RESCUE, return the next rescue
        # task for these hosts, while all other hosts get a noop
        if num_rescue:
            display.debug("advancing hosts in ITERATING_RESCUE")
            return _advance_selected_hosts(hosts, lowest_cur_block, PlayIterator.ITERATING_RESCUE)

        # if any hosts are in ITERATING_ALWAYS, return the next always
        # task for these hosts, while all other hosts get a noop
        if num_always:
            display.debug("advancing hosts in ITERATING_ALWAYS")
            return _advance_selected_hosts(hosts, lowest_cur_block, PlayIterator.ITERATING_ALWAYS)

        # at this point, everything must be ITERATING_COMPLETE, so we
        # return None for all hosts in the list
        display.debug("all hosts are done, so returning None's for all hosts")
        return [(host, None) for host in hosts]

    def run(self, iterator, play_context):
        '''
        The linear strategy is simple - get the next task and queue
        it for all hosts, then wait for the queue to drain before
        moving on to the next task
        '''

        # iteratate over each task, while there is one left to run
        result     = True
        work_to_do = True
        while work_to_do and not self._tqm._terminated:

            try:
                display.debug("getting the remaining hosts for this loop")
                hosts_left = [host for host in self._inventory.get_hosts(iterator._play.hosts) if host.name not in self._tqm._unreachable_hosts and not iterator.is_failed(host)]
                display.debug("done getting the remaining hosts for this loop")

                # queue up this task for each host in the inventory
                callback_sent = False
                work_to_do = False

                host_results = []
                host_tasks = self._get_next_task_lockstep(hosts_left, iterator)

                # skip control
                skip_rest   = False
                choose_step = True

                # flag set if task is set to any_errors_fatal
                any_errors_fatal = False

                results = []
                for (host, task) in host_tasks:
                    if not task:
                        continue

                    if self._tqm._terminated:
                        break

                    run_once = False
                    work_to_do = True

                    # test to see if the task across all hosts points to an action plugin which
                    # sets BYPASS_HOST_LOOP to true, or if it has run_once enabled. If so, we
                    # will only send this task to the first host in the list.

                    try:
                        action = action_loader.get(task.action, class_only=True)
                    except KeyError:
                        # we don't care here, because the action may simply not have a
                        # corresponding action plugin
                        action = None

                    # check to see if this task should be skipped, due to it being a member of a
                    # role which has already run (and whether that role allows duplicate execution)
                    if task._role and task._role.has_run(host):
                        # If there is no metadata, the default behavior is to not allow duplicates,
                        # if there is metadata, check to see if the allow_duplicates flag was set to true
                        if task._role._metadata is None or task._role._metadata and not task._role._metadata.allow_duplicates:
                            display.debug("'%s' skipped because role has already run" % task)
                            continue

                    if task.action == 'meta':
                        self._execute_meta(task, play_context, iterator)
                    else:
                        # handle step if needed, skip meta actions as they are used internally
                        if self._step and choose_step:
                            if self._take_step(task):
                                choose_step = False
                            else:
                                skip_rest = True
                                break

                        display.debug("getting variables")
                        task_vars = self._variable_manager.get_vars(loader=self._loader, play=iterator._play, host=host, task=task)
                        self.add_tqm_variables(task_vars, play=iterator._play)
                        templar = Templar(loader=self._loader, variables=task_vars)
                        display.debug("done getting variables")

                        run_once = templar.template(task.run_once) or action and getattr(action, 'BYPASS_HOST_LOOP', False)

                        if task.any_errors_fatal or run_once:
                            any_errors_fatal = True

                        if not callback_sent:
                            display.debug("sending task start callback, copying the task so we can template it temporarily")
                            saved_name = task.name
                            display.debug("done copying, going to template now")
                            try:
                                task.name = text_type(templar.template(task.name, fail_on_undefined=False))
                                display.debug("done templating")
                            except:
                                # just ignore any errors during task name templating,
                                # we don't care if it just shows the raw name
                                display.debug("templating failed for some reason")
                                pass
                            display.debug("here goes the callback...")
                            self._tqm.send_callback('v2_playbook_on_task_start', task, is_conditional=False)
                            task.name = saved_name
                            callback_sent = True
                            display.debug("sending task start callback")

                        self._blocked_hosts[host.get_name()] = True
                        self._queue_task(host, task, task_vars, play_context)

                    # if we're bypassing the host loop, break out now
                    if run_once:
                        break

                    results += self._process_pending_results(iterator, one_pass=True)

                # go to next host/task group
                if skip_rest:
                    continue

                display.debug("done queuing things up, now waiting for results queue to drain")
                results += self._wait_on_pending_results(iterator)
                host_results.extend(results)

                if not work_to_do and len(iterator.get_failed_hosts()) > 0:
                    display.debug("out of hosts to run on")
                    self._tqm.send_callback('v2_playbook_on_no_hosts_remaining')
                    result = False
                    break

                try:
                    included_files = IncludedFile.process_include_results(
                        host_results,
                        self._tqm,
                        iterator=iterator,
                        inventory=self._inventory,
                        loader=self._loader,
                        variable_manager=self._variable_manager
                    )
                except AnsibleError as e:
                    return False

                include_failure = False
                if len(included_files) > 0:
                    display.debug("we have included files to process")
                    noop_task = Task()
                    noop_task.action = 'meta'
                    noop_task.args['_raw_params'] = 'noop'
                    noop_task.set_loader(iterator._play._loader)

                    display.debug("generating all_blocks data")
                    all_blocks = dict((host, []) for host in hosts_left)
                    display.debug("done generating all_blocks data")
                    for included_file in included_files:
                        display.debug("processing included file: %s" % included_file._filename)
                        # included hosts get the task list while those excluded get an equal-length
                        # list of noop tasks, to make sure that they continue running in lock-step
                        try:
                            new_blocks = self._load_included_file(included_file, iterator=iterator)

                            display.debug("iterating over new_blocks loaded from include file")
                            for new_block in new_blocks:
                                task_vars = self._variable_manager.get_vars(
                                    loader=self._loader,
                                    play=iterator._play,
                                    task=included_file._task,
                                )
                                display.debug("filtering new block on tags")
                                final_block = new_block.filter_tagged_tasks(play_context, task_vars)
                                display.debug("done filtering new block on tags")

                                noop_block = Block(parent_block=task._block)
                                noop_block.block  = [noop_task for t in new_block.block]
                                noop_block.always = [noop_task for t in new_block.always]
                                noop_block.rescue = [noop_task for t in new_block.rescue]

                                for host in hosts_left:
                                    if host in included_file._hosts:
                                        all_blocks[host].append(final_block)
                                    else:
                                        all_blocks[host].append(noop_block)
                            display.debug("done iterating over new_blocks loaded from include file")

                        except AnsibleError as e:
                            for host in included_file._hosts:
                                self._tqm._failed_hosts[host.name] = True
                                iterator.mark_host_failed(host)
                            display.error(e, wrap_text=False)
                            include_failure = True
                            continue

                    # finally go through all of the hosts and append the
                    # accumulated blocks to their list of tasks
                    display.debug("extending task lists for all hosts with included blocks")

                    for host in hosts_left:
                        iterator.add_tasks(host, all_blocks[host])

                    display.debug("done extending task lists")
                    display.debug("done processing included files")

                display.debug("results queue empty")

                display.debug("checking for any_errors_fatal")
                failed_hosts = []
                for res in results:
                    if res.is_failed() or res.is_unreachable():
                        failed_hosts.append(res._host.name)

                # if any_errors_fatal and we had an error, mark all hosts as failed
                if any_errors_fatal and len(failed_hosts) > 0:
                    for host in hosts_left:
                        # don't double-mark hosts, or the iterator will potentially
                        # fail them out of the rescue/always states
                        if host.name not in failed_hosts:
                            self._tqm._failed_hosts[host.name] = True
                            iterator.mark_host_failed(host)
                display.debug("done checking for any_errors_fatal")

            except (IOError, EOFError) as e:
                display.debug("got IOError/EOFError in task loop: %s" % e)
                # most likely an abort, return failed
                return False

        # run the base class run() method, which executes the cleanup function
        # and runs any outstanding handlers which have been triggered

        return super(StrategyModule, self).run(iterator, play_context, result)
<commit_msg>fix TypeError: coercing to Unicode: need string or buffer, AnsibleParserError found<commit_after># (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible.compat.six import iteritems, text_type

from ansible.errors import AnsibleError
from ansible.executor.play_iterator import PlayIterator
from ansible.playbook.block import Block
from ansible.playbook.included_file import IncludedFile
from ansible.playbook.task import Task
from ansible.plugins import action_loader
from ansible.plugins.strategy import StrategyBase
from ansible.template import Templar
from ansible.utils.unicode import to_unicode

try:
    from __main__ import display
except ImportError:
    from ansible.utils.display import Display
    display = Display()

class StrategyModule(StrategyBase):

    def _get_next_task_lockstep(self, hosts, iterator):
        '''
        Returns a list of (host, task) tuples, where the task may
        be a noop task to keep the iterator in lock step across
        all hosts.
        '''

        noop_task = Task()
        noop_task.action = 'meta'
        noop_task.args['_raw_params'] = 'noop'
        noop_task.set_loader(iterator._play._loader)

        host_tasks = {}
        display.debug("building list of next tasks for hosts")
        for host in hosts:
            host_tasks[host.name] = iterator.get_next_task_for_host(host, peek=True)
        display.debug("done building task lists")

        num_setups = 0
        num_tasks  = 0
        num_rescue = 0
        num_always = 0

        display.debug("counting tasks in each state of execution")
        host_tasks_to_run = [(host, state_task)
                             for host, state_task in iteritems(host_tasks)
                             if state_task and state_task[1]]

        if host_tasks_to_run:
            lowest_cur_block = min(
                (s.cur_block for h, (s, t) in host_tasks_to_run
                if s.run_state != PlayIterator.ITERATING_COMPLETE))
        else:
            # empty host_tasks_to_run will just run till the end of the function
            # without ever touching lowest_cur_block
            lowest_cur_block = None

        for (k, v) in host_tasks_to_run:
            (s, t) = v

            if s.cur_block > lowest_cur_block:
                # Not the current block, ignore it
                continue

            if s.run_state == PlayIterator.ITERATING_SETUP:
                num_setups += 1
            elif s.run_state == PlayIterator.ITERATING_TASKS:
                num_tasks += 1
            elif s.run_state == PlayIterator.ITERATING_RESCUE:
                num_rescue += 1
            elif s.run_state == PlayIterator.ITERATING_ALWAYS:
                num_always += 1
        display.debug("done counting tasks in each state of execution")

        def _advance_selected_hosts(hosts, cur_block, cur_state):
            '''
            This helper returns the task for all hosts in the requested
            state, otherwise they get a noop dummy task. This also advances
            the state of the host, since the given states are determined
            while using peek=True.
            '''
            # we return the values in the order they were originally
            # specified in the given hosts array
            rvals = []
            display.debug("starting to advance hosts")
            for host in hosts:
                host_state_task = host_tasks.get(host.name)
                if host_state_task is None:
                    continue
                (s, t) = host_state_task
                if t is None:
                    continue
                if s.run_state == cur_state and s.cur_block == cur_block:
                    new_t = iterator.get_next_task_for_host(host)
                    rvals.append((host, t))
                else:
                    rvals.append((host, noop_task))
            display.debug("done advancing hosts to next task")
            return rvals

        # if any hosts are in ITERATING_SETUP, return the setup task
        # while all other hosts get a noop
        if num_setups:
            display.debug("advancing hosts in ITERATING_SETUP")
            return _advance_selected_hosts(hosts, lowest_cur_block, PlayIterator.ITERATING_SETUP)

        # if any hosts are in ITERATING_TASKS, return the next normal
        # task for these hosts, while all other hosts get a noop
        if num_tasks:
            display.debug("advancing hosts in ITERATING_TASKS")
            return _advance_selected_hosts(hosts, lowest_cur_block, PlayIterator.ITERATING_TASKS)

        # if any hosts are in ITERATING_RESCUE, return the next rescue
        # task for these hosts, while all other hosts get a noop
        if num_rescue:
            display.debug("advancing hosts in ITERATING_RESCUE")
            return _advance_selected_hosts(hosts, lowest_cur_block, PlayIterator.ITERATING_RESCUE)

        # if any hosts are in ITERATING_ALWAYS, return the next always
        # task for these hosts, while all other hosts get a noop
        if num_always:
            display.debug("advancing hosts in ITERATING_ALWAYS")
            return _advance_selected_hosts(hosts, lowest_cur_block, PlayIterator.ITERATING_ALWAYS)

        # at this point, everything must be ITERATING_COMPLETE, so we
        # return None for all hosts in the list
        display.debug("all hosts are done, so returning None's for all hosts")
        return [(host, None) for host in hosts]

    def run(self, iterator, play_context):
        '''
        The linear strategy is simple - get the next task and queue
        it for all hosts, then wait for the queue to drain before
        moving on to the next task
        '''

        # iteratate over each task, while there is one left to run
        result     = True
        work_to_do = True
        while work_to_do and not self._tqm._terminated:

            try:
                display.debug("getting the remaining hosts for this loop")
                hosts_left = [host for host in self._inventory.get_hosts(iterator._play.hosts) if host.name not in self._tqm._unreachable_hosts and not iterator.is_failed(host)]
                display.debug("done getting the remaining hosts for this loop")

                # queue up this task for each host in the inventory
                callback_sent = False
                work_to_do = False

                host_results = []
                host_tasks = self._get_next_task_lockstep(hosts_left, iterator)

                # skip control
                skip_rest   = False
                choose_step = True

                # flag set if task is set to any_errors_fatal
                any_errors_fatal = False

                results = []
                for (host, task) in host_tasks:
                    if not task:
                        continue

                    if self._tqm._terminated:
                        break

                    run_once = False
                    work_to_do = True

                    # test to see if the task across all hosts points to an action plugin which
                    # sets BYPASS_HOST_LOOP to true, or if it has run_once enabled. If so, we
                    # will only send this task to the first host in the list.

                    try:
                        action = action_loader.get(task.action, class_only=True)
                    except KeyError:
                        # we don't care here, because the action may simply not have a
                        # corresponding action plugin
                        action = None

                    # check to see if this task should be skipped, due to it being a member of a
                    # role which has already run (and whether that role allows duplicate execution)
                    if task._role and task._role.has_run(host):
                        # If there is no metadata, the default behavior is to not allow duplicates,
                        # if there is metadata, check to see if the allow_duplicates flag was set to true
                        if task._role._metadata is None or task._role._metadata and not task._role._metadata.allow_duplicates:
                            display.debug("'%s' skipped because role has already run" % task)
                            continue

                    if task.action == 'meta':
                        self._execute_meta(task, play_context, iterator)
                    else:
                        # handle step if needed, skip meta actions as they are used internally
                        if self._step and choose_step:
                            if self._take_step(task):
                                choose_step = False
                            else:
                                skip_rest = True
                                break

                        display.debug("getting variables")
                        task_vars = self._variable_manager.get_vars(loader=self._loader, play=iterator._play, host=host, task=task)
                        self.add_tqm_variables(task_vars, play=iterator._play)
                        templar = Templar(loader=self._loader, variables=task_vars)
                        display.debug("done getting variables")

                        run_once = templar.template(task.run_once) or action and getattr(action, 'BYPASS_HOST_LOOP', False)

                        if task.any_errors_fatal or run_once:
                            any_errors_fatal = True

                        if not callback_sent:
                            display.debug("sending task start callback, copying the task so we can template it temporarily")
                            saved_name = task.name
                            display.debug("done copying, going to template now")
                            try:
                                task.name = text_type(templar.template(task.name, fail_on_undefined=False))
                                display.debug("done templating")
                            except:
                                # just ignore any errors during task name templating,
                                # we don't care if it just shows the raw name
                                display.debug("templating failed for some reason")
                                pass
                            display.debug("here goes the callback...")
                            self._tqm.send_callback('v2_playbook_on_task_start', task, is_conditional=False)
                            task.name = saved_name
                            callback_sent = True
                            display.debug("sending task start callback")

                        self._blocked_hosts[host.get_name()] = True
                        self._queue_task(host, task, task_vars, play_context)

                    # if we're bypassing the host loop, break out now
                    if run_once:
                        break

                    results += self._process_pending_results(iterator, one_pass=True)

                # go to next host/task group
                if skip_rest:
                    continue

                display.debug("done queuing things up, now waiting for results queue to drain")
                results += self._wait_on_pending_results(iterator)
                host_results.extend(results)

                if not work_to_do and len(iterator.get_failed_hosts()) > 0:
                    display.debug("out of hosts to run on")
                    self._tqm.send_callback('v2_playbook_on_no_hosts_remaining')
                    result = False
                    break

                try:
                    included_files = IncludedFile.process_include_results(
                        host_results,
                        self._tqm,
                        iterator=iterator,
                        inventory=self._inventory,
                        loader=self._loader,
                        variable_manager=self._variable_manager
                    )
                except AnsibleError as e:
                    return False

                include_failure = False
                if len(included_files) > 0:
                    display.debug("we have included files to process")
                    noop_task = Task()
                    noop_task.action = 'meta'
                    noop_task.args['_raw_params'] = 'noop'
                    noop_task.set_loader(iterator._play._loader)

                    display.debug("generating all_blocks data")
                    all_blocks = dict((host, []) for host in hosts_left)
                    display.debug("done generating all_blocks data")
                    for included_file in included_files:
                        display.debug("processing included file: %s" % included_file._filename)
                        # included hosts get the task list while those excluded get an equal-length
                        # list of noop tasks, to make sure that they continue running in lock-step
                        try:
                            new_blocks = self._load_included_file(included_file, iterator=iterator)

                            display.debug("iterating over new_blocks loaded from include file")
                            for new_block in new_blocks:
                                task_vars = self._variable_manager.get_vars(
                                    loader=self._loader,
                                    play=iterator._play,
                                    task=included_file._task,
                                )
                                display.debug("filtering new block on tags")
                                final_block = new_block.filter_tagged_tasks(play_context, task_vars)
                                display.debug("done filtering new block on tags")

                                noop_block = Block(parent_block=task._block)
                                noop_block.block  = [noop_task for t in new_block.block]
                                noop_block.always = [noop_task for t in new_block.always]
                                noop_block.rescue = [noop_task for t in new_block.rescue]

                                for host in hosts_left:
                                    if host in included_file._hosts:
                                        all_blocks[host].append(final_block)
                                    else:
                                        all_blocks[host].append(noop_block)
                            display.debug("done iterating over new_blocks loaded from include file")

                        except AnsibleError as e:
                            for host in included_file._hosts:
                                self._tqm._failed_hosts[host.name] = True
                                iterator.mark_host_failed(host)
                            display.error(to_unicode(e), wrap_text=False)
                            include_failure = True
                            continue

                    # finally go through all of the hosts and append the
                    # accumulated blocks to their list of tasks
                    display.debug("extending task lists for all hosts with included blocks")

                    for host in hosts_left:
                        iterator.add_tasks(host, all_blocks[host])

                    display.debug("done extending task lists")
                    display.debug("done processing included files")

                display.debug("results queue empty")

                display.debug("checking for any_errors_fatal")
                failed_hosts = []
                for res in results:
                    if res.is_failed() or res.is_unreachable():
                        failed_hosts.append(res._host.name)

                # if any_errors_fatal and we had an error, mark all hosts as failed
                if any_errors_fatal and len(failed_hosts) > 0:
                    for host in hosts_left:
                        # don't double-mark hosts, or the iterator will potentially
                        # fail them out of the rescue/always states
                        if host.name not in failed_hosts:
                            self._tqm._failed_hosts[host.name] = True
                            iterator.mark_host_failed(host)
                display.debug("done checking for any_errors_fatal")

            except (IOError, EOFError) as e:
                display.debug("got IOError/EOFError in task loop: %s" % e)
                # most likely an abort, return failed
                return False

        # run the base class run() method, which executes the cleanup function
        # and runs any outstanding handlers which have been triggered

        return super(StrategyModule, self).run(iterator, play_context, result)
<|endoftext|>


<commit_before>"""
The ``tools`` module contains a collection of helper functions used in the
windpowerlib.

"""

__copyright__ = "Copyright oemof developer group"
__license__ = "GPLv3"

import numpy as np

def linear_interpolation_extrapolation(df, target_height):
    r"""
    Linear inter- or extrapolates between the values of a data frame.

    This function can be used for the inter-/extrapolation of a parameter
    (e.g wind speed) available at two or more different heights, to approximate
    the value at hub height. The function is carried out when the parameter
    `wind_speed_model`, `density_model` or `temperature_model` of an
    instance of the :class:`~.modelchain.ModelChain` class is
    'interpolation_extrapolation'.

    Parameters
    ----------
    df : pandas.DataFrame
        DataFrame with time series for parameter that is to be interpolated or
        extrapolated. The columns of the DataFrame are the different heights
        for which the parameter is available. If more than two heights are
        given, the two closest heights are used. See example below on how the
        DataFrame should look like and how the function can be used.
    target_height : float
        Height for which the parameter is approximated (e.g. hub height).

    Returns
    -------
    pandas.Series
        Result of the inter-/extrapolation (e.g. wind speed at hub height).

    Notes
    -----

    For the inter- and extrapolation the following equation is used:

    .. math:: f(x) = \frac{(f(x_2) - f(x_1))}{(x_2 - x_1)} \cdot
        (x - x_1) + f(x_1)

    Examples
    ---------
    >>> import numpy as np
    >>> import pandas as pd
    >>> wind_speed_10m = np.array([[3], [4]])
    >>> wind_speed_80m = np.array([[6], [6]])
    >>> weather_df = pd.DataFrame(np.hstack((wind_speed_10m,
    ...                                      wind_speed_80m)),
    ...                           index=pd.date_range('1/1/2012',
    ...                                               periods=2,
    ...                                               freq='H'),
    ...                           columns=[np.array(['wind_speed',
    ...                                              'wind_speed']),
    ...                                    np.array([10, 80])])
    >>> value = linear_interpolation_extrapolation(
    ...     weather_df['wind_speed'], 100)[0]
    >>> int(value * 1000) / 1000
    6.857

    """
    # find closest heights
    heights_sorted = df.columns[
        sorted(range(len(df.columns)),
               key=lambda i: abs(df.columns[i] - target_height))]
    return ((df[heights_sorted[1]] - df[heights_sorted[0]]) /
            (heights_sorted[1] - heights_sorted[0]) *
            (target_height - heights_sorted[0]) + df[heights_sorted[0]])

def logarithmic_interpolation_extrapolation(df, target_height):
    r"""
    Logarithmic inter- or extrapolates between the values of a data frame.

    This function can be used for the inter-/extrapolation of the wind speed if
    it is available at two or more different heights, to approximate
    the value at hub height. The function is carried out when the parameter
    `wind_speed_model` :class:`~.modelchain.ModelChain` class is
    'log_interpolation_extrapolation'.

    Parameters
    ----------
    df : pandas.DataFrame
        DataFrame with time series for parameter that is to be interpolated or
        extrapolated. The columns of the DataFrame are the different heights
        for which the parameter is available. If more than two heights are
        given, the two closest heights are used. See example in
        :py:func:`~.linear_interpolation_extrapolation` on how the
        DataFrame should look like and how the function can be used.
    target_height : float
        Height for which the parameter is approximated (e.g. hub height).

    Returns
    -------
    pandas.Series
        Result of the inter-/extrapolation (e.g. wind speed at hub height).

    Notes
    -----

    For the logarithmic inter- and extrapolation the following equation is
    used [1]_:

    .. math:: f(x) = \frac{\ln(x) \cdot (f(x_2) - f(x_1)) - f(x_2) \cdot
        \ln(x_1) + f(x_1) \cdot \ln(x_2)}{\ln(x_2) - \ln(x_1)}

    References
    ----------
    .. [1] Knorr, K.: "Modellierung von raum-zeitlichen Eigenschaften der
             Windenergieeinspeisung für wetterdatenbasierte
             Windleistungssimulationen". Universität Kassel, Diss., 2016,
             p. 83

    """
    # find closest heights
    heights_sorted = df.columns[
        sorted(range(len(df.columns)),
               key=lambda i: abs(df.columns[i] - target_height))]
    return ((np.log(target_height) *
             (df[heights_sorted[1]] - df[heights_sorted[0]]) -
             df[heights_sorted[1]] * np.log(heights_sorted[0]) +
             df[heights_sorted[0]] * np.log(heights_sorted[1])) /
            (np.log(heights_sorted[1]) - np.log(heights_sorted[0])))

def gauss_distribution(function_variable, standard_deviation, mean=0):
    r"""
    Gauss distribution.

    The Gauss distribution is used in the function
    :py:func:`~.power_curves.smooth_power_curve` for power curve smoothing.

    Parameters
    ----------
    function_variable : float
        Variable of the gaussian distribution.
    standard_deviation : float
        Standard deviation of the Gauss distribution.
    mean : Float
        Defines the offset of the Gauss distribution. Default: 0.

    Returns
    -------
    pandas.Series or numpy.array
        Wind speed at hub height. Data type depends on the type of
        `wind_speed`.

    Notes
    -----
    The following equation is used [1]_:

    .. math:: f(x) = \frac{1}{\sigma \sqrt{2 \pi}} \exp
                     \left[-\frac{(x-\mu)^2}{2 \sigma^2}\right]

    with:
        :math:`\sigma`: standard deviation, :math: `\mu`: mean

    References
    ----------
    .. [1] Berendsen, H.: "A Student's Guide to Data and Error Analysis".
             New York, Cambridge University Press, 2011, p. 37

    """
    return (1 / (standard_deviation * np.sqrt(2 * np.pi)) *
            np.exp(-(function_variable - mean)**2 /
                   (2 * standard_deviation**2)))

def estimate_turbulence_intensity(height, roughness_length):
    r"""
    Estimate turbulence intensity by the roughness length.

    Parameters
    ----------
    height : float
        Height above ground in m at which the turbulence intensity is
        calculated.
    roughness_length : pandas.Series or numpy.array or float
        Roughness length.

    Notes
    -----
    The following equation is used [1]_:

    .. math:: TI = \frac{1}{\ln\left(\frac{h}{z_\text{0}}\right)}

    with:
        TI: turbulence intensity, h: height, :math:`z_{0}`: roughness length

    References
    ----------
    .. [1] Knorr, K.: "Modellierung von raum-zeitlichen Eigenschaften der
             Windenergieeinspeisung für wetterdatenbasierte
             Windleistungssimulationen". Universität Kassel, Diss., 2016,
             p. 88

    """
    return 1 / (np.log(height / roughness_length))
<commit_msg>Deactivate the doctest, because there are already unit tests<commit_after>"""
The ``tools`` module contains a collection of helper functions used in the
windpowerlib.

"""

__copyright__ = "Copyright oemof developer group"
__license__ = "GPLv3"

import numpy as np

def linear_interpolation_extrapolation(df, target_height):
    r"""
    Linear inter- or extrapolates between the values of a data frame.

    This function can be used for the inter-/extrapolation of a parameter
    (e.g wind speed) available at two or more different heights, to approximate
    the value at hub height. The function is carried out when the parameter
    `wind_speed_model`, `density_model` or `temperature_model` of an
    instance of the :class:`~.modelchain.ModelChain` class is
    'interpolation_extrapolation'.

    Parameters
    ----------
    df : pandas.DataFrame
        DataFrame with time series for parameter that is to be interpolated or
        extrapolated. The columns of the DataFrame are the different heights
        for which the parameter is available. If more than two heights are
        given, the two closest heights are used. See example below on how the
        DataFrame should look like and how the function can be used.
    target_height : float
        Height for which the parameter is approximated (e.g. hub height).

    Returns
    -------
    pandas.Series
        Result of the inter-/extrapolation (e.g. wind speed at hub height).

    Notes
    -----

    For the inter- and extrapolation the following equation is used:

    .. math:: f(x) = \frac{(f(x_2) - f(x_1))}{(x_2 - x_1)} \cdot
        (x - x_1) + f(x_1)

    Examples
    ---------
    >>> import numpy as np
    >>> import pandas as pd
    >>> wind_speed_10m = np.array([[3], [4]])
    >>> wind_speed_80m = np.array([[6], [6]])
    >>> weather_df = pd.DataFrame(np.hstack((wind_speed_10m,
    ...                                      wind_speed_80m)),
    ...                           index=pd.date_range('1/1/2012',
    ...                                               periods=2,
    ...                                               freq='H'),
    ...                           columns=[np.array(['wind_speed',
    ...                                              'wind_speed']),
    ...                                    np.array([10, 80])])
    >>> value = linear_interpolation_extrapolation(
    ...     weather_df['wind_speed'], 100)[0]

    """
    # find closest heights
    heights_sorted = df.columns[
        sorted(range(len(df.columns)),
               key=lambda i: abs(df.columns[i] - target_height))]
    return ((df[heights_sorted[1]] - df[heights_sorted[0]]) /
            (heights_sorted[1] - heights_sorted[0]) *
            (target_height - heights_sorted[0]) + df[heights_sorted[0]])

def logarithmic_interpolation_extrapolation(df, target_height):
    r"""
    Logarithmic inter- or extrapolates between the values of a data frame.

    This function can be used for the inter-/extrapolation of the wind speed if
    it is available at two or more different heights, to approximate
    the value at hub height. The function is carried out when the parameter
    `wind_speed_model` :class:`~.modelchain.ModelChain` class is
    'log_interpolation_extrapolation'.

    Parameters
    ----------
    df : pandas.DataFrame
        DataFrame with time series for parameter that is to be interpolated or
        extrapolated. The columns of the DataFrame are the different heights
        for which the parameter is available. If more than two heights are
        given, the two closest heights are used. See example in
        :py:func:`~.linear_interpolation_extrapolation` on how the
        DataFrame should look like and how the function can be used.
    target_height : float
        Height for which the parameter is approximated (e.g. hub height).

    Returns
    -------
    pandas.Series
        Result of the inter-/extrapolation (e.g. wind speed at hub height).

    Notes
    -----

    For the logarithmic inter- and extrapolation the following equation is
    used [1]_:

    .. math:: f(x) = \frac{\ln(x) \cdot (f(x_2) - f(x_1)) - f(x_2) \cdot
        \ln(x_1) + f(x_1) \cdot \ln(x_2)}{\ln(x_2) - \ln(x_1)}

    References
    ----------
    .. [1] Knorr, K.: "Modellierung von raum-zeitlichen Eigenschaften der
             Windenergieeinspeisung für wetterdatenbasierte
             Windleistungssimulationen". Universität Kassel, Diss., 2016,
             p. 83

    """
    # find closest heights
    heights_sorted = df.columns[
        sorted(range(len(df.columns)),
               key=lambda i: abs(df.columns[i] - target_height))]
    return ((np.log(target_height) *
             (df[heights_sorted[1]] - df[heights_sorted[0]]) -
             df[heights_sorted[1]] * np.log(heights_sorted[0]) +
             df[heights_sorted[0]] * np.log(heights_sorted[1])) /
            (np.log(heights_sorted[1]) - np.log(heights_sorted[0])))

def gauss_distribution(function_variable, standard_deviation, mean=0):
    r"""
    Gauss distribution.

    The Gauss distribution is used in the function
    :py:func:`~.power_curves.smooth_power_curve` for power curve smoothing.

    Parameters
    ----------
    function_variable : float
        Variable of the gaussian distribution.
    standard_deviation : float
        Standard deviation of the Gauss distribution.
    mean : Float
        Defines the offset of the Gauss distribution. Default: 0.

    Returns
    -------
    pandas.Series or numpy.array
        Wind speed at hub height. Data type depends on the type of
        `wind_speed`.

    Notes
    -----
    The following equation is used [1]_:

    .. math:: f(x) = \frac{1}{\sigma \sqrt{2 \pi}} \exp
                     \left[-\frac{(x-\mu)^2}{2 \sigma^2}\right]

    with:
        :math:`\sigma`: standard deviation, :math: `\mu`: mean

    References
    ----------
    .. [1] Berendsen, H.: "A Student's Guide to Data and Error Analysis".
             New York, Cambridge University Press, 2011, p. 37

    """
    return (1 / (standard_deviation * np.sqrt(2 * np.pi)) *
            np.exp(-(function_variable - mean)**2 /
                   (2 * standard_deviation**2)))

def estimate_turbulence_intensity(height, roughness_length):
    r"""
    Estimate turbulence intensity by the roughness length.

    Parameters
    ----------
    height : float
        Height above ground in m at which the turbulence intensity is
        calculated.
    roughness_length : pandas.Series or numpy.array or float
        Roughness length.

    Notes
    -----
    The following equation is used [1]_:

    .. math:: TI = \frac{1}{\ln\left(\frac{h}{z_\text{0}}\right)}

    with:
        TI: turbulence intensity, h: height, :math:`z_{0}`: roughness length

    References
    ----------
    .. [1] Knorr, K.: "Modellierung von raum-zeitlichen Eigenschaften der
             Windenergieeinspeisung für wetterdatenbasierte
             Windleistungssimulationen". Universität Kassel, Diss., 2016,
             p. 88

    """
    return 1 / (np.log(height / roughness_length))
<|endoftext|>


<commit_before>from faker.utils.datasets import add_ordereddicts

from .. import BaseProvider, ElementsType

localized = True

class Provider(BaseProvider):
    formats: ElementsType = ["{{first_name}} {{last_name}}"]

    first_names: ElementsType = ["John", "Jane"]

    last_names: ElementsType = ["Doe"]

    # https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
    language_names: ElementsType = [
        "Afar",
        "Abkhazian",
        "Avestan",
        "Afrikaans",
        "Akan",
        "Amharic",
        "Aragonese",
        "Arabic",
        "Assamese",
        "Avaric",
        "Aymara",
        "Azerbaijani",
        "Bashkir",
        "Belarusian",
        "Bulgarian",
        "Bihari languages",
        "Bislama",
        "Bambara",
        "Bengali",
        "Tibetan",
        "Breton",
        "Bosnian",
        "Catalan",
        "Chechen",
        "Chamorro",
        "Corsican",
        "Cree",
        "Czech",
        "Church Slavic",
        "Chuvash",
        "Welsh",
        "Danish",
        "German",
        "Divehi",
        "Dzongkha",
        "Ewe",
        "Greek",
        "English",
        "Esperanto",
        "Spanish",
        "Estonian",
        "Basque",
        "Persian",
        "Fulah",
        "Finnish",
        "Fijian",
        "Faroese",
        "French",
        "Western Frisian",
        "Irish",
        "Gaelic",
        "Galician",
        "Guarani",
        "Gujarati",
        "Manx",
        "Hausa",
        "Hebrew",
        "Hindi",
        "Hiri Motu",
        "Croatian",
        "Haitian",
        "Hungarian",
        "Armenian",
        "Herero",
        "Interlingua",
        "Indonesian",
        "Interlingue",
        "Igbo",
        "Sichuan Yi",
        "Inupiaq",
        "Ido",
        "Icelandic",
        "Italian",
        "Inuktitut",
        "Japanese",
        "Javanese",
        "Georgian",
        "Kongo",
        "Kikuyu",
        "Kuanyama",
        "Kazakh",
        "Kalaallisut",
        "Central Khmer",
        "Kannada",
        "Korean",
        "Kanuri",
        "Kashmiri",
        "Kurdish",
        "Komi",
        "Cornish",
        "Kirghiz",
        "Latin",
        "Luxembourgish",
        "Ganda",
        "Limburgan",
        "Lingala",
        "Lao",
        "Lithuanian",
        "Luba-Katanga",
        "Latvian",
        "Malagasy",
        "Marshallese",
        "Maori",
        "Macedonian",
        "Malayalam",
        "Mongolian",
        "Marathi",
        "Malay",
        "Maltese",
        "Burmese",
        "Nauru",
        "North Ndebele",
        "Nepali",
        "Ndonga",
        "Dutch",
        "Norwegian Nynorsk",
        "Norwegian",
        "South Ndebele",
        "Navajo",
        "Chichewa",
        "Occitan",
        "Ojibwa",
        "Oromo",
        "Oriya",
        "Ossetian",
        "Panjabi",
        "Pali",
        "Polish",
        "Pushto",
        "Portuguese",
        "Quechua",
        "Romansh",
        "Rundi",
        "Romanian",
        "Russian",
        "Kinyarwanda",
        "Sanskrit",
        "Sardinian",
        "Sindhi",
        "Northern Sami",
        "Sango",
        "Sinhala",
        "Slovak",
        "Slovenian",
        "Samoan",
        "Shona",
        "Somali",
        "Albanian",
        "Serbian",
        "Swati",
        "Sotho, Southern",
        "Sundanese",
        "Swedish",
        "Swahili",
        "Tamil",
        "Telugu",
        "Tajik",
        "Thai",
        "Tigrinya",
        "Turkmen",
        "Tagalog",
        "Tswana",
        "Tonga",
        "Turkish",
        "Tsonga",
        "Tatar",
        "Twi",
        "Tahitian",
        "Uighur",
        "Ukrainian",
        "Urdu",
        "Uzbek",
        "Venda",
        "Vietnamese",
        "Walloon",
        "Wolof",
        "Xhosa",
        "Yiddish",
        "Yoruba",
        "Zhuang",
        "Chinese",
        "Zulu",
    ]

    def name(self) -> str:
        """
        :example 'John Doe'
        """
        pattern: str = self.random_element(self.formats)
        return self.generator.parse(pattern)

    def first_name(self) -> str:
        return self.random_element(self.first_names)

    def last_name(self) -> str:
        return self.random_element(self.last_names)

    def name_male(self) -> str:
        if hasattr(self, "formats_male"):
            formats = self.formats_male  # type: ignore[attr-defined]
        else:
            formats = self.formats
        pattern: str = self.random_element(formats)
        return self.generator.parse(pattern)

    def name_nonbinary(self) -> str:
        if hasattr(self, "formats_nonbinary"):
            formats = self.formats_nonbinary  # type: ignore[attr-defined]
        else:
            formats = self.formats
        pattern: str = self.random_element(formats)
        return self.generator.parse(pattern)

    def name_female(self) -> str:
        if hasattr(self, "formats_female"):
            formats = self.formats_female  # type: ignore[attr-defined]
        else:
            formats = self.formats
        pattern: str = self.random_element(formats)
        return self.generator.parse(pattern)

    def first_name_male(self) -> str:
        if hasattr(self, "first_names_male"):
            return self.random_element(self.first_names_male)  # type: ignore[attr-defined]
        return self.first_name()

    def first_name_nonbinary(self) -> str:
        if hasattr(self, "first_names_nonbinary"):
            return self.random_element(self.first_names_nonbinary)  # type: ignore[attr-defined]
        return self.first_name()

    def first_name_female(self) -> str:
        if hasattr(self, "first_names_female"):
            return self.random_element(self.first_names_female)  # type: ignore[attr-defined]
        return self.first_name()

    def last_name_male(self) -> str:
        if hasattr(self, "last_names_male"):
            return self.random_element(self.last_names_male)  # type: ignore[attr-defined]
        return self.last_name()

    def last_name_nonbinary(self) -> str:
        if hasattr(self, "last_names_nonbinary"):
            return self.random_element(self.last_names_nonbinary)  # type: ignore[attr-defined]
        return self.last_name()

    def last_name_female(self) -> str:
        if hasattr(self, "last_names_female"):
            return self.random_element(self.last_names_female)  # type: ignore[attr-defined]
        return self.last_name()

    def prefix(self) -> str:
        if hasattr(self, "prefixes"):
            return self.random_element(self.prefixes)  # type: ignore[attr-defined]
        if hasattr(self, "prefixes_male") and hasattr(self, "prefixes_female") and hasattr(self, "prefixes_nonbinary"):
            prefixes = add_ordereddicts(self.prefixes_male, self.prefixes_female, self.prefixes_nonbinary)  # type: ignore[attr-defined]
            return self.random_element(prefixes)
        if hasattr(self, "prefixes_male") and hasattr(self, "prefixes_female"):
            prefixes = self.random_element((self.prefixes_male, self.prefixes_female))  # type: ignore[attr-defined]
            return self.random_element(prefixes)
        return ""

    def prefix_male(self) -> str:
        if hasattr(self, "prefixes_male"):
            return self.random_element(self.prefixes_male)  # type: ignore[attr-defined]
        return self.prefix()

    def prefix_nonbinary(self) -> str:
        if hasattr(self, "prefixes_nonbinary"):
            return self.random_element(self.prefixes_nonbinary)  # type: ignore[attr-defined]
        return self.prefix()

    def prefix_female(self) -> str:
        if hasattr(self, "prefixes_female"):
            return self.random_element(self.prefixes_female)  # type: ignore[attr-defined]
        return self.prefix()

    def suffix(self) -> str:
        if hasattr(self, "suffixes"):
            return self.random_element(self.suffixes)  # type: ignore[attr-defined]
        if hasattr(self, "suffixes_male") and hasattr(self, "suffixes_female") and hasattr(self, "suffixes_nonbinary"):
            suffixes = add_ordereddicts(self.suffixes_male, self.suffixes_female, self.suffixes_nonbinary)  # type: ignore[attr-defined]
            return self.random_element(suffixes)
        if hasattr(self, "suffixes_male") and hasattr(self, "suffixes_female"):
            suffixes = self.random_element((self.suffixes_male, self.suffixes_female))  # type: ignore[attr-defined]
            return self.random_element(suffixes)
        return ""

    def suffix_male(self) -> str:
        if hasattr(self, "suffixes_male"):
            return self.random_element(self.suffixes_male)  # type: ignore[attr-defined]
        return self.suffix()

    def suffix_nonbinary(self) -> str:
        if hasattr(self, "suffixes_nonbinary"):
            return self.random_element(self.suffixes_nonbinary)  # type: ignore[attr-defined]
        return self.suffix()

    def suffix_female(self) -> str:
        if hasattr(self, "suffixes_female"):
            return self.random_element(self.suffixes_female)  # type: ignore[attr-defined]
        return self.suffix()

    def language_name(self) -> str:
        """Generate a random i18n language name (e.g. English)."""
        return self.random_element(self.language_names)
<commit_msg>fix formatting<commit_after>from faker.utils.datasets import add_ordereddicts

from .. import BaseProvider, ElementsType

localized = True

class Provider(BaseProvider):
    formats: ElementsType = ["{{first_name}} {{last_name}}"]

    first_names: ElementsType = ["John", "Jane"]

    last_names: ElementsType = ["Doe"]

    # https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
    language_names: ElementsType = [
        "Afar",
        "Abkhazian",
        "Avestan",
        "Afrikaans",
        "Akan",
        "Amharic",
        "Aragonese",
        "Arabic",
        "Assamese",
        "Avaric",
        "Aymara",
        "Azerbaijani",
        "Bashkir",
        "Belarusian",
        "Bulgarian",
        "Bihari languages",
        "Bislama",
        "Bambara",
        "Bengali",
        "Tibetan",
        "Breton",
        "Bosnian",
        "Catalan",
        "Chechen",
        "Chamorro",
        "Corsican",
        "Cree",
        "Czech",
        "Church Slavic",
        "Chuvash",
        "Welsh",
        "Danish",
        "German",
        "Divehi",
        "Dzongkha",
        "Ewe",
        "Greek",
        "English",
        "Esperanto",
        "Spanish",
        "Estonian",
        "Basque",
        "Persian",
        "Fulah",
        "Finnish",
        "Fijian",
        "Faroese",
        "French",
        "Western Frisian",
        "Irish",
        "Gaelic",
        "Galician",
        "Guarani",
        "Gujarati",
        "Manx",
        "Hausa",
        "Hebrew",
        "Hindi",
        "Hiri Motu",
        "Croatian",
        "Haitian",
        "Hungarian",
        "Armenian",
        "Herero",
        "Interlingua",
        "Indonesian",
        "Interlingue",
        "Igbo",
        "Sichuan Yi",
        "Inupiaq",
        "Ido",
        "Icelandic",
        "Italian",
        "Inuktitut",
        "Japanese",
        "Javanese",
        "Georgian",
        "Kongo",
        "Kikuyu",
        "Kuanyama",
        "Kazakh",
        "Kalaallisut",
        "Central Khmer",
        "Kannada",
        "Korean",
        "Kanuri",
        "Kashmiri",
        "Kurdish",
        "Komi",
        "Cornish",
        "Kirghiz",
        "Latin",
        "Luxembourgish",
        "Ganda",
        "Limburgan",
        "Lingala",
        "Lao",
        "Lithuanian",
        "Luba-Katanga",
        "Latvian",
        "Malagasy",
        "Marshallese",
        "Maori",
        "Macedonian",
        "Malayalam",
        "Mongolian",
        "Marathi",
        "Malay",
        "Maltese",
        "Burmese",
        "Nauru",
        "North Ndebele",
        "Nepali",
        "Ndonga",
        "Dutch",
        "Norwegian Nynorsk",
        "Norwegian",
        "South Ndebele",
        "Navajo",
        "Chichewa",
        "Occitan",
        "Ojibwa",
        "Oromo",
        "Oriya",
        "Ossetian",
        "Panjabi",
        "Pali",
        "Polish",
        "Pushto",
        "Portuguese",
        "Quechua",
        "Romansh",
        "Rundi",
        "Romanian",
        "Russian",
        "Kinyarwanda",
        "Sanskrit",
        "Sardinian",
        "Sindhi",
        "Northern Sami",
        "Sango",
        "Sinhala",
        "Slovak",
        "Slovenian",
        "Samoan",
        "Shona",
        "Somali",
        "Albanian",
        "Serbian",
        "Swati",
        "Sotho, Southern",
        "Sundanese",
        "Swedish",
        "Swahili",
        "Tamil",
        "Telugu",
        "Tajik",
        "Thai",
        "Tigrinya",
        "Turkmen",
        "Tagalog",
        "Tswana",
        "Tonga",
        "Turkish",
        "Tsonga",
        "Tatar",
        "Twi",
        "Tahitian",
        "Uighur",
        "Ukrainian",
        "Urdu",
        "Uzbek",
        "Venda",
        "Vietnamese",
        "Walloon",
        "Wolof",
        "Xhosa",
        "Yiddish",
        "Yoruba",
        "Zhuang",
        "Chinese",
        "Zulu",
    ]

    def name(self) -> str:
        """
        :example 'John Doe'
        """
        pattern: str = self.random_element(self.formats)
        return self.generator.parse(pattern)

    def first_name(self) -> str:
        return self.random_element(self.first_names)

    def last_name(self) -> str:
        return self.random_element(self.last_names)

    def name_male(self) -> str:
        if hasattr(self, "formats_male"):
            formats = self.formats_male  # type: ignore[attr-defined]
        else:
            formats = self.formats
        pattern: str = self.random_element(formats)
        return self.generator.parse(pattern)

    def name_nonbinary(self) -> str:
        if hasattr(self, "formats_nonbinary"):
            formats = self.formats_nonbinary  # type: ignore[attr-defined]
        else:
            formats = self.formats
        pattern: str = self.random_element(formats)
        return self.generator.parse(pattern)

    def name_female(self) -> str:
        if hasattr(self, "formats_female"):
            formats = self.formats_female  # type: ignore[attr-defined]
        else:
            formats = self.formats
        pattern: str = self.random_element(formats)
        return self.generator.parse(pattern)

    def first_name_male(self) -> str:
        if hasattr(self, "first_names_male"):
            return self.random_element(self.first_names_male)  # type: ignore[attr-defined]
        return self.first_name()

    def first_name_nonbinary(self) -> str:
        if hasattr(self, "first_names_nonbinary"):
            return self.random_element(self.first_names_nonbinary)  # type: ignore[attr-defined]
        return self.first_name()

    def first_name_female(self) -> str:
        if hasattr(self, "first_names_female"):
            return self.random_element(self.first_names_female)  # type: ignore[attr-defined]
        return self.first_name()

    def last_name_male(self) -> str:
        if hasattr(self, "last_names_male"):
            return self.random_element(self.last_names_male)  # type: ignore[attr-defined]
        return self.last_name()

    def last_name_nonbinary(self) -> str:
        if hasattr(self, "last_names_nonbinary"):
            return self.random_element(self.last_names_nonbinary)  # type: ignore[attr-defined]
        return self.last_name()

    def last_name_female(self) -> str:
        if hasattr(self, "last_names_female"):
            return self.random_element(self.last_names_female)  # type: ignore[attr-defined]
        return self.last_name()

    def prefix(self) -> str:
        if hasattr(self, "prefixes"):
            return self.random_element(self.prefixes)  # type: ignore[attr-defined]
        if hasattr(self, "prefixes_male") and hasattr(self, "prefixes_female") and hasattr(self, "prefixes_nonbinary"):
            prefixes = add_ordereddicts(
                self.prefixes_male,
                self.prefixes_female,
                self.prefixes_nonbinary,
            )  # type: ignore[attr-defined]
            return self.random_element(prefixes)
        if hasattr(self, "prefixes_male") and hasattr(self, "prefixes_female"):
            prefixes = self.random_element((self.prefixes_male, self.prefixes_female))  # type: ignore[attr-defined]
            return self.random_element(prefixes)
        return ""

    def prefix_male(self) -> str:
        if hasattr(self, "prefixes_male"):
            return self.random_element(self.prefixes_male)  # type: ignore[attr-defined]
        return self.prefix()

    def prefix_nonbinary(self) -> str:
        if hasattr(self, "prefixes_nonbinary"):
            return self.random_element(self.prefixes_nonbinary)  # type: ignore[attr-defined]
        return self.prefix()

    def prefix_female(self) -> str:
        if hasattr(self, "prefixes_female"):
            return self.random_element(self.prefixes_female)  # type: ignore[attr-defined]
        return self.prefix()

    def suffix(self) -> str:
        if hasattr(self, "suffixes"):
            return self.random_element(self.suffixes)  # type: ignore[attr-defined]
        if hasattr(self, "suffixes_male") and hasattr(self, "suffixes_female") and hasattr(self, "suffixes_nonbinary"):
            suffixes = add_ordereddicts(
                self.suffixes_male,
                self.suffixes_female,
                self.suffixes_nonbinary,
            )  # type: ignore[attr-defined]
            return self.random_element(suffixes)
        if hasattr(self, "suffixes_male") and hasattr(self, "suffixes_female"):
            suffixes = self.random_element((self.suffixes_male, self.suffixes_female))  # type: ignore[attr-defined]
            return self.random_element(suffixes)
        return ""

    def suffix_male(self) -> str:
        if hasattr(self, "suffixes_male"):
            return self.random_element(self.suffixes_male)  # type: ignore[attr-defined]
        return self.suffix()

    def suffix_nonbinary(self) -> str:
        if hasattr(self, "suffixes_nonbinary"):
            return self.random_element(self.suffixes_nonbinary)  # type: ignore[attr-defined]
        return self.suffix()

    def suffix_female(self) -> str:
        if hasattr(self, "suffixes_female"):
            return self.random_element(self.suffixes_female)  # type: ignore[attr-defined]
        return self.suffix()

    def language_name(self) -> str:
        """Generate a random i18n language name (e.g. English)."""
        return self.random_element(self.language_names)
<|endoftext|>