<commit_before>import boto3
import os
import json
import argparse
import sys
import shutil
from subprocess import Popen, PIPE

EXIT_CODE = 0

# global boto connections
ec2_client = boto3.client('ec2', region_name='us-gov-west-1')
ec2_resource = boto3.resource('ec2', region_name='us-gov-west-1')

def deploy():

    # This tf_var file is expected to be copied from an external source
    tfvar_file       = 'usaspending-vars.tf.json'
    tf_exec_path     = 'terraform'
    tf_file          = 'usaspending-deploy.tf'

    packer_exec_path = 'packer'
    packer_file      = 'usaspending-packer.json'

    parser = argparse.ArgumentParser()
    parser.add_argument('--deploy_env', required=True, choices=['sandbox', 'dev', 'qat', 'staging', 'prod'])
    parser.add_argument('--config_branch', required=False, default='master')
    parser.add_argument('--tools_branch', required=False, default='master')
    args = parser.parse_args()
    deploy_env = args.deploy_env
    config_branch = args.config_branch
    tools_branch = args.tools_branch

    tfvar_json = open(tfvar_file, "r")
    tfvar_data = json.load(tfvar_json)
    tfvar_json.close()

    # Initialize variables needed to deploy terraform
    tf_state_s3_bucket = tfvar_data['variable']['tf_state_s3_bucket']['default']
    tf_state_s3_path = tfvar_data['variable']['tf_state_s3_path']['default']
    tf_aws_region = tfvar_data['variable']['aws_region']['default']
    create_dmz = tfvar_data['variable']['create_dmz']['default']
    startup_script = "usaspending-start-{}.sh".format(deploy_env)
    dmz_startup_script = "usaspending-start-dmz-{}.sh".format(deploy_env)

    # Get Old AMIs, for setting current=False after new one is created
    old_instance_amis = ec2_client.describe_images(Filters=[
        {'Name':'tag:current', 'Values':['True']},
        {'Name':'tag:base', 'Values':['False']},
        {'Name':'tag:type', 'Values':['USASpending-API']},
        {'Name':'tag:environment', 'Values':[deploy_env]}
        ])['Images']

    print("Old Instance AMIs: ")
    print(old_instance_amis)

    # Build New AMI (Packer)
    print('**************************************************************************')
    print(' Building new AMI via Packer. This can take a while...')

    packer_output = real_time_command([packer_exec_path, 'build',
                                      '-var', 'environment_ami_tag={}'.format(deploy_env),
                                      '-var', 'ansible_branch_var={}'.format('master' if deploy_env == 'prod' else deploy_env),
                                      '-var', 'config_branch={}'.format(config_branch),
                                      '-var', 'tools_branch={}'.format(tools_branch),
                                      '-machine-readable', packer_file])
    ami_line = [line for line in packer_output.split('\n') if "amazon-ebs: AMIs were created:" in line][0]
    new_instance_ami = ami_line[ami_line.find('ami-'):ami_line.find('ami-')+21]
    print('Done. New AMI created: ' + new_instance_ami)

    # Set current=False tag for old AMIs
    if old_instance_amis:
        print('Done. Setting current tag to False on old instance AMIs: \n' + '\n'.join(map(str, old_instance_amis)) )
        update_ami_tags(old_instance_amis)
        print('Done.')
    else:
        print('No matching old AMIs. Skipping tag update...')

    # Add new AMI to Terraform variables
    # Variable aws_amis now replaced with newly created ami-id
    update_tf_ami(new_instance_ami, tfvar_file)

    # Update Terraform User Data
    update_terraform_user_data(
        environment=deploy_env,
        tf_file=tf_file,
        create_dmz=create_dmz)

    print('**************************************************************************')
    print(' Running terraform... ')
    # Terraform appears to be pretty particular about variable and .tf files, so move the ones we need into
    # a subdir so this doesn't have to happen via Jenkins.
    shutil.rmtree(deploy_env, ignore_errors=True)
    os.mkdir(deploy_env)
    shutil.copy(tf_file,    deploy_env)
    shutil.copy(tfvar_file, deploy_env)
    shutil.copy(startup_script, deploy_env)
    if create_dmz:
        shutil.copy(dmz_startup_script, deploy_env)
    os.chdir(deploy_env)

    # Run Terraform plan and apply
    # Terraform now builds out usaspending_api and usaspending_bd
    # as both are now contained within the single usaspending-deploy.tf file
    real_time_command([tf_exec_path, 'init',  '-input=false',
                       '-backend-config=bucket='+tf_state_s3_bucket,
                       '-backend-config=key='+tf_state_s3_path,
                       '-backend-config=region='+tf_aws_region])
    real_time_command([tf_exec_path, 'plan',  '-input=false', '-out=' + tf_file])
    real_time_command([tf_exec_path, 'apply', '-input=false', tf_file])
    global EXIT_CODE
    if EXIT_CODE != 0:
        print('Exiting with a code of {}'.format(EXIT_CODE))
        sys.exit(EXIT_CODE)

###############################################################################
# Helper Functions
###############################################################################

def real_time_command(command_to_run):
    process = Popen(command_to_run, stdout=PIPE)
    total_output = ''
    while True:
        output = process.stdout.readline().decode()
        if output == '' and process.poll() is not None:
            break
        if output:
            total_output += output

            # Pretty-print human readable output
            if '-machine-readable' in command_to_run:
                output = output[output.rfind(',') + 1:]
            print(output.strip())

    rc = process.poll()
    global EXIT_CODE
    EXIT_CODE += rc

    return total_output

def update_tf_ami(new_ami='', tfvar_file='variables.tf.json'):
    tfvar_json = open(tfvar_file, "r")
    tfvar_data = json.load(tfvar_json)
    tfvar_json.close()

    tfvar_data['variable']['aws_amis']['default']['us-gov-west-1'] = new_ami

    tfvar_json = open(tfvar_file, "w+")
    tfvar_json.write(json.dumps(tfvar_data, indent=4))
    tfvar_json.close()

    print ('Updated ' + tfvar_file + ' with AMI id ' + new_ami)

    return

def update_terraform_user_data(
    environment='staging',
    tf_file='usaspending-deploy.tf',
    create_dmz=False):
    f = open(tf_file,'r')
    filedata = f.read()
    f.close()

    # environment = prod, staging, dev, or sandbox
    startup_shell_script = "usaspending-start-{}.sh".format(environment)
    newdata = filedata.replace("usaspending-start-staging.sh", startup_shell_script)

    if create_dmz:
        startup_shell_script = "usaspending-start-dmz-{}.sh".format(environment)
        newdata = filedata.replace("usaspending-start-dmz-staging.sh", startup_shell_script)

    f = open(tf_file,'w')
    f.write(newdata)
    f.close()

    print ('Updated ' + tf_file + ' with user data script for ' + environment)

    return

def update_ami_tags(old_instance_amis):
    for ami in old_instance_amis:
        image = ec2_resource.Image(ami['ImageId'])
        image.create_tags(
            DryRun=False,
            Tags=[{'Key': 'current', 'Value': 'False'}]
        )
    return

if __name__ == '__main__':
    deploy()
<commit_msg>logic error<commit_after>import boto3
import os
import json
import argparse
import sys
import shutil
from subprocess import Popen, PIPE

EXIT_CODE = 0

# global boto connections
ec2_client = boto3.client('ec2', region_name='us-gov-west-1')
ec2_resource = boto3.resource('ec2', region_name='us-gov-west-1')

def deploy():

    # This tf_var file is expected to be copied from an external source
    tfvar_file       = 'usaspending-vars.tf.json'
    tf_exec_path     = 'terraform'
    tf_file          = 'usaspending-deploy.tf'

    packer_exec_path = 'packer'
    packer_file      = 'usaspending-packer.json'

    parser = argparse.ArgumentParser()
    parser.add_argument('--deploy_env', required=True, choices=['sandbox', 'dev', 'qat', 'staging', 'prod'])
    parser.add_argument('--config_branch', required=False, default='master')
    parser.add_argument('--tools_branch', required=False, default='master')
    args = parser.parse_args()
    deploy_env = args.deploy_env
    config_branch = args.config_branch
    tools_branch = args.tools_branch

    tfvar_json = open(tfvar_file, "r")
    tfvar_data = json.load(tfvar_json)
    tfvar_json.close()

    # Initialize variables needed to deploy terraform
    tf_state_s3_bucket = tfvar_data['variable']['tf_state_s3_bucket']['default']
    tf_state_s3_path = tfvar_data['variable']['tf_state_s3_path']['default']
    tf_aws_region = tfvar_data['variable']['aws_region']['default']
    create_dmz = tfvar_data['variable']['create_dmz']['default']
    startup_script = "usaspending-start-{}.sh".format(deploy_env)
    dmz_startup_script = "usaspending-start-dmz-{}.sh".format(deploy_env)

    # Get Old AMIs, for setting current=False after new one is created
    old_instance_amis = ec2_client.describe_images(Filters=[
        {'Name':'tag:current', 'Values':['True']},
        {'Name':'tag:base', 'Values':['False']},
        {'Name':'tag:type', 'Values':['USASpending-API']},
        {'Name':'tag:environment', 'Values':[deploy_env]}
        ])['Images']

    print("Old Instance AMIs: ")
    print(old_instance_amis)

    # Build New AMI (Packer)
    print('**************************************************************************')
    print(' Building new AMI via Packer. This can take a while...')

    packer_output = real_time_command([packer_exec_path, 'build',
                                      '-var', 'environment_ami_tag={}'.format(deploy_env),
                                      '-var', 'ansible_branch_var={}'.format('master' if deploy_env == 'prod' else deploy_env),
                                      '-var', 'config_branch={}'.format(config_branch),
                                      '-var', 'tools_branch={}'.format(tools_branch),
                                      '-machine-readable', packer_file])
    ami_line = [line for line in packer_output.split('\n') if "amazon-ebs: AMIs were created:" in line][0]
    new_instance_ami = ami_line[ami_line.find('ami-'):ami_line.find('ami-')+21]
    print('Done. New AMI created: ' + new_instance_ami)

    # Set current=False tag for old AMIs
    if old_instance_amis:
        print('Done. Setting current tag to False on old instance AMIs: \n' + '\n'.join(map(str, old_instance_amis)) )
        update_ami_tags(old_instance_amis)
        print('Done.')
    else:
        print('No matching old AMIs. Skipping tag update...')

    # Add new AMI to Terraform variables
    # Variable aws_amis now replaced with newly created ami-id
    update_tf_ami(new_instance_ami, tfvar_file)

    # Update Terraform User Data
    update_terraform_user_data(
        environment=deploy_env,
        tf_file=tf_file,
        create_dmz=create_dmz)

    print('**************************************************************************')
    print(' Running terraform... ')
    # Terraform appears to be pretty particular about variable and .tf files, so move the ones we need into
    # a subdir so this doesn't have to happen via Jenkins.
    shutil.rmtree(deploy_env, ignore_errors=True)
    os.mkdir(deploy_env)
    shutil.copy(tf_file,    deploy_env)
    shutil.copy(tfvar_file, deploy_env)
    shutil.copy(startup_script, deploy_env)
    if create_dmz:
        shutil.copy(dmz_startup_script, deploy_env)
    os.chdir(deploy_env)

    # Run Terraform plan and apply
    # Terraform now builds out usaspending_api and usaspending_bd
    # as both are now contained within the single usaspending-deploy.tf file
    real_time_command([tf_exec_path, 'init',  '-input=false',
                       '-backend-config=bucket='+tf_state_s3_bucket,
                       '-backend-config=key='+tf_state_s3_path,
                       '-backend-config=region='+tf_aws_region])
    real_time_command([tf_exec_path, 'plan',  '-input=false', '-out=' + tf_file])
    real_time_command([tf_exec_path, 'apply', '-input=false', tf_file])
    global EXIT_CODE
    if EXIT_CODE != 0:
        print('Exiting with a code of {}'.format(EXIT_CODE))
        sys.exit(EXIT_CODE)

###############################################################################
# Helper Functions
###############################################################################

def real_time_command(command_to_run):
    process = Popen(command_to_run, stdout=PIPE)
    total_output = ''
    while True:
        output = process.stdout.readline().decode()
        if output == '' and process.poll() is not None:
            break
        if output:
            total_output += output

            # Pretty-print human readable output
            if '-machine-readable' in command_to_run:
                output = output[output.rfind(',') + 1:]
            print(output.strip())

    rc = process.poll()
    global EXIT_CODE
    EXIT_CODE += rc

    return total_output

def update_tf_ami(new_ami='', tfvar_file='variables.tf.json'):
    tfvar_json = open(tfvar_file, "r")
    tfvar_data = json.load(tfvar_json)
    tfvar_json.close()

    tfvar_data['variable']['aws_amis']['default']['us-gov-west-1'] = new_ami

    tfvar_json = open(tfvar_file, "w+")
    tfvar_json.write(json.dumps(tfvar_data, indent=4))
    tfvar_json.close()

    print ('Updated ' + tfvar_file + ' with AMI id ' + new_ami)

    return

def update_terraform_user_data(
    environment='staging',
    tf_file='usaspending-deploy.tf',
    create_dmz=False):
    f = open(tf_file,'r')
    filedata = f.read()
    f.close()

    # environment = prod, staging, dev, or sandbox
    startup_shell_script = "usaspending-start-{}.sh".format(environment)
    newdata = filedata.replace("usaspending-start-staging.sh", startup_shell_script)

    if create_dmz:
        startup_shell_script = "usaspending-start-dmz-{}.sh".format(environment)
        newdata = newdata.replace("usaspending-start-dmz-staging.sh", startup_shell_script)

    f = open(tf_file,'w')
    f.write(newdata)
    f.close()

    print ('Updated ' + tf_file + ' with user data script for ' + environment)

    return

def update_ami_tags(old_instance_amis):
    for ami in old_instance_amis:
        image = ec2_resource.Image(ami['ImageId'])
        image.create_tags(
            DryRun=False,
            Tags=[{'Key': 'current', 'Value': 'False'}]
        )
    return

if __name__ == '__main__':
    deploy()
<|endoftext|>


<commit_before>#!/usr/bin/env python3
# Copyright (c) 2015-2016 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Utilities for doing coverage analysis on the RPC interface.

Provides a way to track which RPC commands are exercised during
testing.
"""

import os

REFERENCE_FILENAME = 'rpc_interface.txt'

class AuthServiceProxyWrapper(object):
    """
    An object that wraps AuthServiceProxy to record specific RPC calls.

    """
    def __init__(self, auth_service_proxy_instance, coverage_logfile=None):
        """
        Kwargs:
            auth_service_proxy_instance (AuthServiceProxy): the instance
                being wrapped.
            coverage_logfile (str): if specified, write each service_name
                out to a file when called.

        """
        self.auth_service_proxy_instance = auth_service_proxy_instance
        self.coverage_logfile = coverage_logfile

    def __getattr__(self, *args, **kwargs):
        return_val = self.auth_service_proxy_instance.__getattr__(
            *args, **kwargs)

        return AuthServiceProxyWrapper(return_val, self.coverage_logfile)

    def __call__(self, *args, **kwargs):
        """
        Delegates to AuthServiceProxy, then writes the particular RPC method
        called to a file.

        """
        return_val = self.auth_service_proxy_instance.__call__(*args, **kwargs)
        rpc_method = self.auth_service_proxy_instance._service_name

        if self.coverage_logfile:
            with open(self.coverage_logfile, 'a+', encoding='utf8') as f:
                f.write("%s\n" % rpc_method)

        return return_val

    @property
    def url(self):
        return self.auth_service_proxy_instance.url

    def __truediv__(self, relative_uri):
        return AuthServiceProxyWrapper(self.auth_service_proxy_instance / relative_uri)

def get_filename(dirname, n_node):
    """
    Get a filename unique to the test process ID and node.

    This file will contain a list of RPC commands covered.
    """
    pid = str(os.getpid())
    return os.path.join(
        dirname, "coverage.pid%s.node%s.txt" % (pid, str(n_node)))

def write_all_rpc_commands(dirname, node):
    """
    Write out a list of all RPC functions available in `bitcoin-cli` for
    coverage comparison. This will only happen once per coverage
    directory.

    Args:
        dirname (str): temporary test dir
        node (AuthServiceProxy): client

    Returns:
        bool. if the RPC interface file was written.

    """
    filename = os.path.join(dirname, REFERENCE_FILENAME)

    if os.path.isfile(filename):
        return False

    help_output = node.help().split('\n')
    commands = set()

    for line in help_output:
        line = line.strip()

        # Ignore blanks and headers
        if line and not line.startswith('='):
            commands.add("%s\n" % line.split()[0])

    with open(filename, 'w', encoding='utf8') as f:
        f.writelines(list(commands))

    return True
<commit_msg>Limit AuthServiceProxyWrapper.__getattr__ wrapping<commit_after>#!/usr/bin/env python3
# Copyright (c) 2015-2016 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Utilities for doing coverage analysis on the RPC interface.

Provides a way to track which RPC commands are exercised during
testing.
"""

import os

REFERENCE_FILENAME = 'rpc_interface.txt'

class AuthServiceProxyWrapper(object):
    """
    An object that wraps AuthServiceProxy to record specific RPC calls.

    """
    def __init__(self, auth_service_proxy_instance, coverage_logfile=None):
        """
        Kwargs:
            auth_service_proxy_instance (AuthServiceProxy): the instance
                being wrapped.
            coverage_logfile (str): if specified, write each service_name
                out to a file when called.

        """
        self.auth_service_proxy_instance = auth_service_proxy_instance
        self.coverage_logfile = coverage_logfile

    def __getattr__(self, name):
        return_val = getattr(self.auth_service_proxy_instance, name)
        if not isinstance(return_val, type(self.auth_service_proxy_instance)):
            # If proxy getattr returned an unwrapped value, do the same here.
            return return_val
        return AuthServiceProxyWrapper(return_val, self.coverage_logfile)

    def __call__(self, *args, **kwargs):
        """
        Delegates to AuthServiceProxy, then writes the particular RPC method
        called to a file.

        """
        return_val = self.auth_service_proxy_instance.__call__(*args, **kwargs)
        rpc_method = self.auth_service_proxy_instance._service_name

        if self.coverage_logfile:
            with open(self.coverage_logfile, 'a+', encoding='utf8') as f:
                f.write("%s\n" % rpc_method)

        return return_val

    def __truediv__(self, relative_uri):
        return AuthServiceProxyWrapper(self.auth_service_proxy_instance / relative_uri)

def get_filename(dirname, n_node):
    """
    Get a filename unique to the test process ID and node.

    This file will contain a list of RPC commands covered.
    """
    pid = str(os.getpid())
    return os.path.join(
        dirname, "coverage.pid%s.node%s.txt" % (pid, str(n_node)))

def write_all_rpc_commands(dirname, node):
    """
    Write out a list of all RPC functions available in `bitcoin-cli` for
    coverage comparison. This will only happen once per coverage
    directory.

    Args:
        dirname (str): temporary test dir
        node (AuthServiceProxy): client

    Returns:
        bool. if the RPC interface file was written.

    """
    filename = os.path.join(dirname, REFERENCE_FILENAME)

    if os.path.isfile(filename):
        return False

    help_output = node.help().split('\n')
    commands = set()

    for line in help_output:
        line = line.strip()

        # Ignore blanks and headers
        if line and not line.startswith('='):
            commands.add("%s\n" % line.split()[0])

    with open(filename, 'w', encoding='utf8') as f:
        f.writelines(list(commands))

    return True
<|endoftext|>


<commit_before>from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.shortcuts import render_to_response
from django.template.context import RequestContext

from bluebottle.auth.views import GetAuthToken

urlpatterns = patterns('',
                       # The api urls are in the / url namespace so that
                       # they're not redirected to /en/.
                       url(r'^docs/', include('rest_framework_swagger.urls')),

                       url(r'^api/config',
                           include('bluebottle.clients.urls.api')),

                       url(r'^api/users/',
                           include('bluebottle.bb_accounts.urls.api')),
                       url(r'^api/bb_organizations/',
                           include('bluebottle.bb_organizations.urls.api')),
                       url(r'^api/bb_projects/',
                           include('bluebottle.bb_projects.urls.api')),
                       url(r'^api/fundraisers/',
                           include('bluebottle.bb_fundraisers.urls.api')),
                       url(r'^api/bb_tasks/',
                           include('bluebottle.bb_tasks.urls.api')),
                       url(r'^api/geo/', include('bluebottle.geo.urls.api')),
                       url(r'^api/contact/',
                           include('bluebottle.contact.urls.api')),
                       url(r'^api/news/', include('bluebottle.news.urls.api')),
                       url(r'^api/pages/',
                           include('bluebottle.pages.urls.api')),
                       url(r'^api/quotes/',
                           include('bluebottle.quotes.urls.api')),
                       url(r'^api/slides/',
                           include('bluebottle.slides.urls.api')),
                       url(r'^api/utils/',
                           include('bluebottle.utils.urls.api')),
                       url(r'^api/wallposts/',
                           include('bluebottle.wallposts.urls.api')),
                       url(r'^api/terms/',
                           include('bluebottle.terms.urls.api')),
                       url(r'^api/metadata/',
                           include('bluebottle.utils.urls.api')),

                       url(r'^api/orders/',
                           include('bluebottle.bb_orders.urls.api')),
                       url(r'^api/donations/',
                           include('bluebottle.bb_donations.urls.api')),
                       url(r'^api/order_payments/', include(
                           'bluebottle.payments.urls.order_payments_api')),
                       url(r'^api/payments/',
                           include('bluebottle.payments.urls.api')),
                       url(r'^api/monthly_donations/',
                           include('bluebottle.recurring_donations.urls.api')),

                       url(r'^api/partners/',
                           include('bluebottle.partners.urls.api')),

                       url(r'^api/rewards/',
                           include('bluebottle.rewards.urls.api')),

                       # Homepage API urls
                       url(r'^api/homepage/',
                           include('bluebottle.homepage.urls.api')),
                       url(r'^api/stats',
                           include('bluebottle.statistics.urls.api')),
                       url(r'^api/bb_projects/',
                           include('bluebottle.projects.urls.api')),

                       url(r'^payments_mock/',
                           include('bluebottle.payments_mock.urls.core')),
                       url(r'^payments_docdata/',
                           include('bluebottle.payments_docdata.urls.core')),

                       # Urls for partner sites
                       url(r'^pp/',
                          include('bluebottle.partners.urls.partners')),

                       url(r'^api/organizations/',
                           include('bluebottle.organizations.urls.api')),
                       url(r'^api/suggestions/',
                           include('bluebottle.suggestions.urls.api')),

                       url(r'^api/votes/',
                           include('bluebottle.votes.urls.api')),

                       # Organization urls for private documents
                       url(r'^documents/',
                           include('bluebottle.organizations.urls.documents')),

                       # handlebar templates
                       url(r'^templates/',
                           include('bluebottle.hbtemplates.urls')),

                       url(r'^embed/', include('bluebottle.widget.urls.core')),

                       # JSON Web Token based authentication for Django REST framework
                       url(r'^api/token-auth/',
                           'rest_framework_jwt.views.obtain_jwt_token'),

                       # Social token authorization
                       url(r'^api/social/',
                           include('bluebottle.social.urls.api')),

                       url(r'token/', include('token_auth.urls')),

                       )

# Nicely parse 500 errors so we get semantic messages in tests.
def handler500(request):
    response = render_to_response('500.html', {},
                                  context_instance=RequestContext(request))
    response.status_code = 500
    return response

urlpatterns += patterns(
    'loginas.views',
    url(r"^login/user/(?P<user_id>.+)/$", "user_login",
        name="loginas-user-login"),
)

js_info_dict = {
    'packages': ('apps.bb_accounts', 'apps.bb_projects'),
}

urlpatterns += patterns(
    '',
    (r'^js$', 'django.views.i18n.javascript_catalog'),
)

# Serve django-staticfiles (only works in DEBUG)
# https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-development
urlpatterns += staticfiles_urlpatterns()

# Serve media files (only works in DEBUG)
# https://docs.djangoproject.com/en/dev/howto/static-files/#django.conf.urls.static.static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += patterns('',

                        url('', include('social.apps.django_app.urls',
                                        namespace='social')),
                        url(r'^api/social-login/(?P<backend>[^/]+)/$',
                            GetAuthToken.as_view()),

                        # Needed for the self-documenting API in Django Rest Framework.
                        url(r'^api-auth/', include('rest_framework.urls',
                                                   namespace='rest_framework')),

                        # JSON Web Token based authentication for Django REST framework
                        url(r'^api/token-auth/',
                            'rest_framework_jwt.views.obtain_jwt_token'),
                        url(r'^api/token-auth-refresh/$',
                            'rest_framework_jwt.views.refresh_jwt_token'),

                        url(r'^', include('django.conf.urls.i18n')),
                        )

urlpatterns += patterns('loginas.views',
                        url(r"^login/user/(?P<user_id>.+)/$", "user_login",
                            name="loginas-user-login"),
                        )

js_info_dict = {
    'packages': ('apps.accounts', 'bluebottle.projects'),
}

urlpatterns += patterns('',
                        (r'^js$', 'django.views.i18n.javascript_catalog'),
                        )

# Serve django-staticfiles (only works in DEBUG)
# https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-development
urlpatterns += staticfiles_urlpatterns()

# Serve media files (only works in DEBUG)
# https://docs.djangoproject.com/en/dev/howto/static-files/#django.conf.urls.static.static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
<commit_msg>Add url for redirects API<commit_after>from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.shortcuts import render_to_response
from django.template.context import RequestContext

from bluebottle.auth.views import GetAuthToken

urlpatterns = patterns('',
                       # The api urls are in the / url namespace so that
                       # they're not redirected to /en/.
                       url(r'^docs/', include('rest_framework_swagger.urls')),

                       url(r'^api/config',
                           include('bluebottle.clients.urls.api')),
                       url(r'^api/redirects',
                           include('bluebottle.redirects.urls.api')),
                       url(r'^api/users/',
                           include('bluebottle.bb_accounts.urls.api')),
                       url(r'^api/bb_organizations/',
                           include('bluebottle.bb_organizations.urls.api')),
                       url(r'^api/bb_projects/',
                           include('bluebottle.bb_projects.urls.api')),
                       url(r'^api/fundraisers/',
                           include('bluebottle.bb_fundraisers.urls.api')),
                       url(r'^api/bb_tasks/',
                           include('bluebottle.bb_tasks.urls.api')),
                       url(r'^api/geo/', include('bluebottle.geo.urls.api')),
                       url(r'^api/contact/',
                           include('bluebottle.contact.urls.api')),
                       url(r'^api/news/', include('bluebottle.news.urls.api')),
                       url(r'^api/pages/',
                           include('bluebottle.pages.urls.api')),
                       url(r'^api/quotes/',
                           include('bluebottle.quotes.urls.api')),
                       url(r'^api/slides/',
                           include('bluebottle.slides.urls.api')),
                       url(r'^api/utils/',
                           include('bluebottle.utils.urls.api')),
                       url(r'^api/wallposts/',
                           include('bluebottle.wallposts.urls.api')),
                       url(r'^api/terms/',
                           include('bluebottle.terms.urls.api')),
                       url(r'^api/metadata/',
                           include('bluebottle.utils.urls.api')),

                       url(r'^api/orders/',
                           include('bluebottle.bb_orders.urls.api')),
                       url(r'^api/donations/',
                           include('bluebottle.bb_donations.urls.api')),
                       url(r'^api/order_payments/', include(
                           'bluebottle.payments.urls.order_payments_api')),
                       url(r'^api/payments/',
                           include('bluebottle.payments.urls.api')),
                       url(r'^api/monthly_donations/',
                           include('bluebottle.recurring_donations.urls.api')),

                       url(r'^api/partners/',
                           include('bluebottle.partners.urls.api')),

                       url(r'^api/rewards/',
                           include('bluebottle.rewards.urls.api')),

                       # Homepage API urls
                       url(r'^api/homepage/',
                           include('bluebottle.homepage.urls.api')),
                       url(r'^api/stats',
                           include('bluebottle.statistics.urls.api')),
                       url(r'^api/bb_projects/',
                           include('bluebottle.projects.urls.api')),

                       url(r'^payments_mock/',
                           include('bluebottle.payments_mock.urls.core')),
                       url(r'^payments_docdata/',
                           include('bluebottle.payments_docdata.urls.core')),

                       # Urls for partner sites
                       url(r'^pp/',
                          include('bluebottle.partners.urls.partners')),

                       url(r'^api/organizations/',
                           include('bluebottle.organizations.urls.api')),
                       url(r'^api/suggestions/',
                           include('bluebottle.suggestions.urls.api')),

                       url(r'^api/votes/',
                           include('bluebottle.votes.urls.api')),

                       # Organization urls for private documents
                       url(r'^documents/',
                           include('bluebottle.organizations.urls.documents')),

                       # handlebar templates
                       url(r'^templates/',
                           include('bluebottle.hbtemplates.urls')),

                       url(r'^embed/', include('bluebottle.widget.urls.core')),

                       # JSON Web Token based authentication for Django REST framework
                       url(r'^api/token-auth/',
                           'rest_framework_jwt.views.obtain_jwt_token'),

                       # Social token authorization
                       url(r'^api/social/',
                           include('bluebottle.social.urls.api')),

                       url(r'token/', include('token_auth.urls')),

                       )

# Nicely parse 500 errors so we get semantic messages in tests.
def handler500(request):
    response = render_to_response('500.html', {},
                                  context_instance=RequestContext(request))
    response.status_code = 500
    return response

urlpatterns += patterns(
    'loginas.views',
    url(r"^login/user/(?P<user_id>.+)/$", "user_login",
        name="loginas-user-login"),
)

js_info_dict = {
    'packages': ('apps.bb_accounts', 'apps.bb_projects'),
}

urlpatterns += patterns(
    '',
    (r'^js$', 'django.views.i18n.javascript_catalog'),
)

# Serve django-staticfiles (only works in DEBUG)
# https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-development
urlpatterns += staticfiles_urlpatterns()

# Serve media files (only works in DEBUG)
# https://docs.djangoproject.com/en/dev/howto/static-files/#django.conf.urls.static.static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += patterns('',

                        url('', include('social.apps.django_app.urls',
                                        namespace='social')),
                        url(r'^api/social-login/(?P<backend>[^/]+)/$',
                            GetAuthToken.as_view()),

                        # Needed for the self-documenting API in Django Rest Framework.
                        url(r'^api-auth/', include('rest_framework.urls',
                                                   namespace='rest_framework')),

                        # JSON Web Token based authentication for Django REST framework
                        url(r'^api/token-auth/',
                            'rest_framework_jwt.views.obtain_jwt_token'),
                        url(r'^api/token-auth-refresh/$',
                            'rest_framework_jwt.views.refresh_jwt_token'),

                        url(r'^', include('django.conf.urls.i18n')),
                        )

urlpatterns += patterns('loginas.views',
                        url(r"^login/user/(?P<user_id>.+)/$", "user_login",
                            name="loginas-user-login"),
                        )

js_info_dict = {
    'packages': ('apps.accounts', 'bluebottle.projects'),
}

urlpatterns += patterns('',
                        (r'^js$', 'django.views.i18n.javascript_catalog'),
                        )

# Serve django-staticfiles (only works in DEBUG)
# https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-development
urlpatterns += staticfiles_urlpatterns()

# Serve media files (only works in DEBUG)
# https://docs.djangoproject.com/en/dev/howto/static-files/#django.conf.urls.static.static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
<|endoftext|>


<commit_before>class config_eval_line_locate_azimuth(config_base):
    mutable = 2

def eval_line_locate_azimuth(param):
    if len(param) < 2:
        return ''

    l = float(eval_line_length([param[0]]))

    f = eval_metric([param[1], 'px'])
    if f == '':
        # seems to be a geometry
        f = float(eval_line_locate_point([ param[0], param[1] ]))
    else:
        f = float(f)

    if f < 0.0:
        f = 0.0
    elif f > l:
        f = l

    dist = 1.0
    if len(param) > 2:
        dist = eval_metric([param[2], 'px'])
        if dist == '':
            dist = 1.0
        else:
            dist = float(dist)

    f1 = f - dist
    if f1 < 0.0:
        f1 = 0.0

    f2 = f + dist
    if f2 > l:
        f2 = l

    plan = plpy.prepare('select degrees(ST_Azimuth(ST_Line_Interpolate_Point($1, $2), ST_Line_Interpolate_Point($1, $3))) as r1, degrees(ST_Azimuth(ST_Line_Interpolate_Point($1, $3), ST_Line_Interpolate_Point($1, $4))) as r2', ['geometry', 'float', 'float', 'float'])
    res = plpy.execute(plan, [ param[0], f1 / l, f / l, f2 / l ])

    r1 = res[0]['r1']
    r2 = res[0]['r2']

    if f1 != 0.0 and f2 != l:
        if r1 < 90 and r2 > 270:
            r2 -= 360
        if r2 < 90 and r1 > 270:
            r1 -= 360

        r = r1 + (r2 - r1) / 2.0

        if r < 0:
            r += 360

    elif f1 == 0.0:
        r = r2

    elif f2 == l:
        r = r1

    else:
        return ''

    #plpy.notice(l, f, f1, f2, res[0], r)

    return str(r)

# TESTS
# IN ['010200002031BF0D0002000000EC51B8DE163A3A410AD7A36078B45641295C8F826E393A4152B81E8573B45641', '010100002031BF0D0096A850FF0A3A3A414E8FF20878B45641']
# OUT '263.416763627542'
# IN ['010200002031BF0D0002000000EC51B8DE163A3A410AD7A36078B45641295C8F826E393A4152B81E8573B45641', '5.000000565986081']
# OUT '263.416763627542'
# IN ['010200002031BF0D0002000000EC51B8DE163A3A410AD7A36078B45641295C8F826E393A4152B81E8573B45641', '0']
# OUT '263.416763627542'
# IN ['010200002031BF0D0002000000EC51B8DE163A3A410AD7A36078B45641295C8F826E393A4152B81E8573B45641', '100']
# OUT '263.416763627542'
<commit_msg>Eval: line_locate_azimuth: fix tests<commit_after>class config_eval_line_locate_azimuth(config_base):
    mutable = 2

def eval_line_locate_azimuth(param):
    if len(param) < 2:
        return ''

    l = float(eval_line_length([param[0]]))

    f = eval_metric([param[1], 'px'])
    if f == '':
        # seems to be a geometry
        f = float(eval_line_locate_point([ param[0], param[1] ]))
    else:
        f = float(f)

    if f < 0.0:
        f = 0.0
    elif f > l:
        f = l

    dist = 1.0
    if len(param) > 2:
        dist = eval_metric([param[2], 'px'])
        if dist == '':
            dist = 1.0
        else:
            dist = float(dist)

    f1 = f - dist
    if f1 < 0.0:
        f1 = 0.0

    f2 = f + dist
    if f2 > l:
        f2 = l

    plan = plpy.prepare('select degrees(ST_Azimuth(ST_Line_Interpolate_Point($1, $2), ST_Line_Interpolate_Point($1, $3))) as r1, degrees(ST_Azimuth(ST_Line_Interpolate_Point($1, $3), ST_Line_Interpolate_Point($1, $4))) as r2', ['geometry', 'float', 'float', 'float'])
    res = plpy.execute(plan, [ param[0], f1 / l, f / l, f2 / l ])

    r1 = res[0]['r1']
    r2 = res[0]['r2']

    if f1 != 0.0 and f2 != l:
        if r1 < 90 and r2 > 270:
            r2 -= 360
        if r2 < 90 and r1 > 270:
            r1 -= 360

        r = r1 + (r2 - r1) / 2.0

        if r < 0:
            r += 360

    elif f1 == 0.0:
        r = r2

    elif f2 == l:
        r = r1

    else:
        return ''

    #plpy.notice(l, f, f1, f2, res[0], r)

    return str(r)

# TESTS
# IN ['010200002031BF0D0002000000EC51B8DE163A3A410AD7A36078B45641295C8F826E393A4152B81E8573B45641', '010100002031BF0D0096A850FF0A3A3A414E8FF20878B45641']
# OUT '263.416763627542'
# IN ['010200002031BF0D0002000000EC51B8DE163A3A410AD7A36078B45641295C8F826E393A4152B81E8573B45641', '5.000000565986081']
# OUT '263.416763627542'
# IN ['010200002031BF0D0002000000EC51B8DE163A3A410AD7A36078B45641295C8F826E393A4152B81E8573B45641', '0']
# OUT '263.4167636386304'
# IN ['010200002031BF0D0002000000EC51B8DE163A3A410AD7A36078B45641295C8F826E393A4152B81E8573B45641', '100']
# OUT '263.4167636386304'
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
==============================================================================
Base class for structure data atoms (:mod:`sknano.core.atoms._atoms`)
==============================================================================

.. currentmodule:: sknano.core.atoms._atoms

"""
from __future__ import absolute_import, division, print_function
from __future__ import unicode_literals
__docformat__ = 'restructuredtext en'

from operator import attrgetter

import numpy as np

from sknano.core import UserList
from sknano.core.math import convert_condition_str
from ._atom import Atom

__all__ = ['Atoms']

class Atoms(UserList):
    """Base class for collection of `Atom` objects.

    Parameters
    ----------
    atoms : {None, sequence, `Atoms`}, optional
        if not `None`, then a list of `Atom` instance objects or an
        existing `Atoms` instance object.

    """
    def __init__(self, atoms=None, **kwargs):
        if atoms is not None and (isinstance(atoms, str) or
                                  isinstance(atoms, Atom)):
            atoms = [atoms]
        if not isinstance(atoms, type(self)) and isinstance(atoms, list):
            atoms = atoms[:]
            for i, atom in enumerate(atoms):
                try:
                    atoms[i] = self.__atom_class__(**atom.todict())
                except AttributeError:
                    atoms[i] = self.__atom_class__(atom)
        super().__init__(initlist=atoms, **kwargs)
        self.fmtstr = "{atoms!r}"

    @property
    def __atom_class__(self):
        return Atom

    def __repr__(self):
        """Return canonical string representation of `Atoms`."""
        return "{}({})".format(self.__class__.__name__,
                               self.fmtstr.format(**self.todict()))

    def sort(self, key=attrgetter('element', 'Z', 'mass'), reverse=False):
        super().sort(key=key, reverse=reverse)

    def __getitem__(self, index):
        data = super().__getitem__(index)
        if isinstance(data, list):
            return self.__class__(data, **self.kwargs)
        return data

    def __setitem__(self, index, value):
        if not isinstance(value, (self.__class__, self.__atom_class__)):
            if isinstance(index, slice):
                value = self.__class__(value)
            else:
                value = self.__atom_class__(value)
        super().__setitem__(index, value)

    def __atoms_not_in_self(self, other):
        return [atom for atom in other if atom not in self]

    def __add__(self, other):
        try:
            addlist = self.__atoms_not_in_self(other)
        except TypeError:
            addlist = self.__atoms_not_in_self(self.__class__(other))
        return self.__class__(self.data + addlist, **self.kwargs)

    def __radd__(self, other):
        try:
            addlist = self.__atoms_not_in_self(other)
        except TypeError:
            addlist = self.__atoms_not_in_self(self.__class__(other))
        return self.__class__(addlist + self.data, **self.kwargs)

    def __iadd__(self, other):
        try:
            self.data += self.__atoms_not_in_self(other)
        except TypeError:
            self.data += self.__atoms_not_in_self(self.__class__(other))
        return self

    # def __atoms_not_in_other(self, other):
    #     return [atom for atom in self if atom not in other]

    # def __sub__(self, other):
    #     if isinstance(other, self.__class__):
    #         return self.__class__(self.__atoms_not_in_other(other),
    #                               **self.kwargs)
    #     return NotImplemented

    # def __rsub__(self, other):
    #     return NotImplemented

    # def __isub__(self, other):
    #     if isinstance(other, self.__class__):
    #         self.data -= self.__atoms_not_in_other(other)
    #     return self

    @property
    def fmtstr(self):
        return self._fmtstr

    @fmtstr.setter
    def fmtstr(self, value):
        self._fmtstr = value

    @property
    def Natoms(self):
        """Number of atoms in `Atoms`."""
        return len(self)

    @property
    def M(self):
        """Total mass of `Atoms`."""
        # return math.fsum(self.masses)
        return self.masses.sum()

    @property
    def elements(self):
        """:class:`~numpy:numpy.ndarray` of :attr:`Atom.element`\ s \
            in `Atoms`."""
        return np.asarray([atom.element for atom in self])

    @property
    def masses(self):
        """:class:`~numpy:numpy.ndarray` of :attr:`Atom.mass`\ s \
            in `Atoms`."""
        return np.asarray([atom.mass for atom in self])

    @property
    def symbols(self):
        """:class:`~numpy:numpy.ndarray` of :attr:`Atom.symbol`\ s \
            in `Atoms`."""
        return np.asarray([atom.symbol for atom in self])

    def filter(self, condition, invert=False):
        """Filter `Atoms` by `condition`.

        .. versionchanged:: 0.3.11

           Filters the list of `Atoms` **in-place**. Use
           :meth:`~Atoms.filtered` to generate a new filtered list
           of `Atoms`.

        Parameters
        ----------
        condition : :class:`~python:str` or boolean array
            Boolean index array having same shape as the initial dimensions
            of the list of `Atoms` being indexed.
        invert : bool, optional
            If `True`, the boolean array `condition` is inverted element-wise.

        """
        if isinstance(condition, str):
            condition = convert_condition_str(self, condition)
        if invert:
            condition = ~condition
        try:
            self.data = np.asarray(self)[condition].tolist()
        except AttributeError:
            self.data = np.asarray(self)[condition]

    def filtered(self, condition, invert=False):
        """Return new list of `Atoms` filtered by `condition`.

        .. versionadded:: 0.3.11

        Parameters
        ----------
        condition : array_like, bool
            Boolean index array having same shape as the initial dimensions
            of the list of `Atoms` being indexed.
        invert : bool, optional
            If `True`, the boolean array `condition` is inverted element-wise.

        Returns
        -------
        filtered_atoms : `Atoms`
            If `invert` is `False`, return the elements where `condition`
            is `True`.

            If `invert` is `True`, return the elements where `~condition`
            (i.e., numpy.invert(condition)) is `True`.

        Examples
        --------
        An example using the structure data of a 10 nm `(10, 0)`
        `SWCNT`:

        >>> from sknano.generators import SWNTGenerator
        >>> swnt = SWNTGenerator(10, 0, Lz=10, fix_Lz=True).atoms
        >>> # select 'left', 'middle', 'right' atoms
        >>> latoms = swnt.filtered(swnt.z <= 25)
        >>> matoms = swnt.filtered((swnt.z < 75) & (swnt.z > 25))
        >>> ratoms = swnt.filtered(swnt.z >= 75)
        >>> from pprint import pprint
        >>> pprint([getattr(atoms, 'bounds') for atoms in
        ...         (latoms, matoms, ratoms)])
        [Cuboid(pmin=Point([-3.914435, -3.914435, 0.0]), \
                pmax=Point([3.914435, 3.914435, 24.85])),
         Cuboid(pmin=Point([-3.914435, -3.914435, 25.56]), \
                pmax=Point([3.914435, 3.914435, 74.55])),
         Cuboid(pmin=Point([-3.914435, -3.914435, 75.97]), \
                pmax=Point([3.914435, 3.914435, 100.11]))]
        >>> latoms.Natoms + matoms.Natoms + ratoms.Natoms == swnt.Natoms
        True

        """
        if isinstance(condition, str):
            condition = convert_condition_str(self, condition)
        if invert:
            condition = ~condition
        try:
            return self.__class__(atoms=np.asarray(self)[condition].tolist(),
                                  **self.kwargs)
        except AttributeError:
            return self.__class__(atoms=np.asarray(self)[condition],
                                  **self.kwargs)

    def get_atoms(self, asarray=False):
        """Return list of `Atoms`.

        Parameters
        ----------
        asarray : bool, optional

        Returns
        -------
        sequence or ndarray

        """
        if asarray:
            return np.asarray(self.data)
        else:
            return self.data

    def getatomattr(self, attr):
        """Get :class:`~numpy:numpy.ndarray` of atom attributes `attr`.

        Parameters
        ----------
        attr : str
            Name of attribute to pass to `getattr` from each `XAtom` in
            `XAtoms`.

        Returns
        -------
        :class:`~numpy:numpy.ndarray`

        """
        try:
            return np.asarray([getattr(atom, attr) for atom in self])
        except AttributeError:
            return None

    def mapatomattr(self, from_attr=None, to_attr=None, attrmap=None):
        """Set/update atom attribute from another atom attribute with dict.

        .. versionchanged:: 0.3.11

           Made all arguments required keyword arguments and reversed the
           order of the former positional arguments `from_attr` and
           `to_attr` to be more natural and consistent with the key, value
           pairs in the `attrmap` dictionary.

        Parameters
        ----------
        from_attr, to_attr : :class:`python:str`
        attrmap : :class:`python:dict`

        Examples
        --------
        Suppose you have an `XAtoms` instance named ``atoms`` that has
        `XAtom` instances of two atom types `1` and `2` and we want to set
        all `XAtom`\ s with `type=1` to Nitrogen and all `XAtom`\ s with
        `type=2` to Argon. In other words, we want to map the
        `XAtom.type` attribute to the `XAtom.element` attribute.

        We'd call this method like so::

        >>> atoms.mapatomattr('type', 'element', {1: 'N', 2: 'Ar'})

        """
        try:
            [setattr(atom, to_attr, attrmap[getattr(atom, from_attr)])
             for atom in self if getattr(atom, from_attr) is not None]
        except (KeyError, TypeError) as e:
            print(e)

    def rotate(self, **kwargs):
        assert not hasattr(super(), 'rotate')

    def translate(self, *args, **kwargs):
        assert not hasattr(super(), 'translate')

    def todict(self):
        return dict(atoms=self.data)
<commit_msg>Updating `Atoms` class constructor...<commit_after># -*- coding: utf-8 -*-
"""
==============================================================================
Base class for structure data atoms (:mod:`sknano.core.atoms._atoms`)
==============================================================================

.. currentmodule:: sknano.core.atoms._atoms

"""
from __future__ import absolute_import, division, print_function
from __future__ import unicode_literals
__docformat__ = 'restructuredtext en'

from operator import attrgetter

import numpy as np

from sknano.core import UserList
from sknano.core.math import convert_condition_str
from ._atom import Atom

__all__ = ['Atoms']

class Atoms(UserList):
    """Base class for collection of `Atom` objects.

    Parameters
    ----------
    atoms : {None, sequence, `Atoms`}, optional
        if not `None`, then a list of `Atom` instance objects or an
        existing `Atoms` instance object.

    """
    def __init__(self, atoms=None, **kwargs):
        if atoms is not None and (isinstance(atoms, str) or
                                  isinstance(atoms, Atom)):
            atoms = [atoms]
        if not isinstance(atoms, type(self)) and isinstance(atoms, list):
            atoms = atoms[:]
            for i, atom in enumerate(atoms):
                try:
                    # atoms[i] = self.__atom_class__(**atom.todict())
                    atomdict = atom.todict()
                    atoms[i] = self.__atom_class__(
                        **{k: atomdict[k] for k in set(dir(atom)) &
                           set(dir(self.__atom_class__))})
                except AttributeError:
                    atoms[i] = self.__atom_class__(atom)
        super().__init__(initlist=atoms, **kwargs)
        self.fmtstr = "{atoms!r}"

    @property
    def __atom_class__(self):
        return Atom

    def __repr__(self):
        """Return canonical string representation of `Atoms`."""
        return "{}({})".format(self.__class__.__name__,
                               self.fmtstr.format(**self.todict()))

    def sort(self, key=attrgetter('element', 'Z', 'mass'), reverse=False):
        super().sort(key=key, reverse=reverse)

    def __getitem__(self, index):
        data = super().__getitem__(index)
        if isinstance(data, list):
            return self.__class__(data, **self.kwargs)
        return data

    def __setitem__(self, index, value):
        if not isinstance(value, (self.__class__, self.__atom_class__)):
            if isinstance(index, slice):
                value = self.__class__(value)
            else:
                value = self.__atom_class__(value)
        super().__setitem__(index, value)

    def __atoms_not_in_self(self, other):
        return [atom for atom in other if atom not in self]

    def __add__(self, other):
        try:
            addlist = self.__atoms_not_in_self(other)
        except TypeError:
            addlist = self.__atoms_not_in_self(self.__class__(other))
        return self.__class__(self.data + addlist, **self.kwargs)

    def __radd__(self, other):
        try:
            addlist = self.__atoms_not_in_self(other)
        except TypeError:
            addlist = self.__atoms_not_in_self(self.__class__(other))
        return self.__class__(addlist + self.data, **self.kwargs)

    def __iadd__(self, other):
        try:
            self.data += self.__atoms_not_in_self(other)
        except TypeError:
            self.data += self.__atoms_not_in_self(self.__class__(other))
        return self

    # def __atoms_not_in_other(self, other):
    #     return [atom for atom in self if atom not in other]

    # def __sub__(self, other):
    #     if isinstance(other, self.__class__):
    #         return self.__class__(self.__atoms_not_in_other(other),
    #                               **self.kwargs)
    #     return NotImplemented

    # def __rsub__(self, other):
    #     return NotImplemented

    # def __isub__(self, other):
    #     if isinstance(other, self.__class__):
    #         self.data -= self.__atoms_not_in_other(other)
    #     return self

    @property
    def fmtstr(self):
        return self._fmtstr

    @fmtstr.setter
    def fmtstr(self, value):
        self._fmtstr = value

    @property
    def Natoms(self):
        """Number of atoms in `Atoms`."""
        return len(self)

    @property
    def M(self):
        """Total mass of `Atoms`."""
        # return math.fsum(self.masses)
        return self.masses.sum()

    @property
    def elements(self):
        """:class:`~numpy:numpy.ndarray` of :attr:`Atom.element`\ s \
            in `Atoms`."""
        return np.asarray([atom.element for atom in self])

    @property
    def masses(self):
        """:class:`~numpy:numpy.ndarray` of :attr:`Atom.mass`\ s \
            in `Atoms`."""
        return np.asarray([atom.mass for atom in self])

    @property
    def symbols(self):
        """:class:`~numpy:numpy.ndarray` of :attr:`Atom.symbol`\ s \
            in `Atoms`."""
        return np.asarray([atom.symbol for atom in self])

    def filter(self, condition, invert=False):
        """Filter `Atoms` by `condition`.

        .. versionchanged:: 0.3.11

           Filters the list of `Atoms` **in-place**. Use
           :meth:`~Atoms.filtered` to generate a new filtered list
           of `Atoms`.

        Parameters
        ----------
        condition : :class:`~python:str` or boolean array
            Boolean index array having same shape as the initial dimensions
            of the list of `Atoms` being indexed.
        invert : bool, optional
            If `True`, the boolean array `condition` is inverted element-wise.

        """
        if isinstance(condition, str):
            condition = convert_condition_str(self, condition)
        if invert:
            condition = ~condition
        try:
            self.data = np.asarray(self)[condition].tolist()
        except AttributeError:
            self.data = np.asarray(self)[condition]

    def filtered(self, condition, invert=False):
        """Return new list of `Atoms` filtered by `condition`.

        .. versionadded:: 0.3.11

        Parameters
        ----------
        condition : array_like, bool
            Boolean index array having same shape as the initial dimensions
            of the list of `Atoms` being indexed.
        invert : bool, optional
            If `True`, the boolean array `condition` is inverted element-wise.

        Returns
        -------
        filtered_atoms : `Atoms`
            If `invert` is `False`, return the elements where `condition`
            is `True`.

            If `invert` is `True`, return the elements where `~condition`
            (i.e., numpy.invert(condition)) is `True`.

        Examples
        --------
        An example using the structure data of a 10 nm `(10, 0)`
        `SWCNT`:

        >>> from sknano.generators import SWNTGenerator
        >>> swnt = SWNTGenerator(10, 0, Lz=10, fix_Lz=True).atoms
        >>> # select 'left', 'middle', 'right' atoms
        >>> latoms = swnt.filtered(swnt.z <= 25)
        >>> matoms = swnt.filtered((swnt.z < 75) & (swnt.z > 25))
        >>> ratoms = swnt.filtered(swnt.z >= 75)
        >>> from pprint import pprint
        >>> pprint([getattr(atoms, 'bounds') for atoms in
        ...         (latoms, matoms, ratoms)])
        [Cuboid(pmin=Point([-3.914435, -3.914435, 0.0]), \
                pmax=Point([3.914435, 3.914435, 24.85])),
         Cuboid(pmin=Point([-3.914435, -3.914435, 25.56]), \
                pmax=Point([3.914435, 3.914435, 74.55])),
         Cuboid(pmin=Point([-3.914435, -3.914435, 75.97]), \
                pmax=Point([3.914435, 3.914435, 100.11]))]
        >>> latoms.Natoms + matoms.Natoms + ratoms.Natoms == swnt.Natoms
        True

        """
        if isinstance(condition, str):
            condition = convert_condition_str(self, condition)
        if invert:
            condition = ~condition
        try:
            return self.__class__(atoms=np.asarray(self)[condition].tolist(),
                                  **self.kwargs)
        except AttributeError:
            return self.__class__(atoms=np.asarray(self)[condition],
                                  **self.kwargs)

    def get_atoms(self, asarray=False):
        """Return list of `Atoms`.

        Parameters
        ----------
        asarray : bool, optional

        Returns
        -------
        sequence or ndarray

        """
        if asarray:
            return np.asarray(self.data)
        else:
            return self.data

    def getatomattr(self, attr):
        """Get :class:`~numpy:numpy.ndarray` of atom attributes `attr`.

        Parameters
        ----------
        attr : str
            Name of attribute to pass to `getattr` from each `XAtom` in
            `XAtoms`.

        Returns
        -------
        :class:`~numpy:numpy.ndarray`

        """
        try:
            return np.asarray([getattr(atom, attr) for atom in self])
        except AttributeError:
            return None

    def mapatomattr(self, from_attr=None, to_attr=None, attrmap=None):
        """Set/update atom attribute from another atom attribute with dict.

        .. versionchanged:: 0.3.11

           Made all arguments required keyword arguments and reversed the
           order of the former positional arguments `from_attr` and
           `to_attr` to be more natural and consistent with the key, value
           pairs in the `attrmap` dictionary.

        Parameters
        ----------
        from_attr, to_attr : :class:`python:str`
        attrmap : :class:`python:dict`

        Examples
        --------
        Suppose you have an `XAtoms` instance named ``atoms`` that has
        `XAtom` instances of two atom types `1` and `2` and we want to set
        all `XAtom`\ s with `type=1` to Nitrogen and all `XAtom`\ s with
        `type=2` to Argon. In other words, we want to map the
        `XAtom.type` attribute to the `XAtom.element` attribute.

        We'd call this method like so::

        >>> atoms.mapatomattr('type', 'element', {1: 'N', 2: 'Ar'})

        """
        try:
            [setattr(atom, to_attr, attrmap[getattr(atom, from_attr)])
             for atom in self if getattr(atom, from_attr) is not None]
        except (KeyError, TypeError) as e:
            print(e)

    def rotate(self, **kwargs):
        assert not hasattr(super(), 'rotate')

    def translate(self, *args, **kwargs):
        assert not hasattr(super(), 'translate')

    def todict(self):
        return dict(atoms=self.data)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
=================================================================
Graphene structure tools (:mod:`sknano.nanogen._graphene`)
=================================================================

.. currentmodule:: sknano.nanogen._graphene

"""
from __future__ import absolute_import, division, print_function
__docformat__ = 'restructuredtext'

from math import ceil, sqrt
import itertools
import sys

import numpy as np

from pkshared.tools.refdata import CCbond

__all__ = ['Graphene']

class Graphene(object):
    """Class for generating interactive Graphene objects."""
    pass
<commit_msg>adding new code for graphene properties<commit_after># -*- coding: utf-8 -*-
"""
=================================================================
Graphene structure tools (:mod:`sknano.nanogen._graphene`)
=================================================================

.. currentmodule:: sknano.nanogen._graphene

"""
from __future__ import absolute_import, division, print_function
__docformat__ = 'restructuredtext'

#import itertools
import sys

import numpy as np

from pkshared.tools.refdata import CCbond

from ..chemistry import Atom, Atoms

__all__ = ['Graphene']

class Graphene(object):
    """Class for generating interactive Graphene objects."""

    def __init__(self, n=None, m=None, width=None, length=None, edge=None,
                 element1='C', element2='C', bond=CCbond, nlayers=1,
                 layer_spacing=3.35, stacking_order='AB',
                 autogen=True, verbose=False):

        self.element1 = element1
        self.element2 = element2

        self.Lx = width
        self.Ly = length
        self.edge = edge
        self.bond = bond
        self.verbose = verbose

        self.lx = 0.
        self.ly = 0.

        self.Nx = 0
        self.Ny = 0

        self.nlayers = nlayers
        self.layer_spacing = layer_spacing
        self.stacking_order = stacking_order

        self.layer_shift = np.zeros(3)

        if nlayers > 1 and stacking_order == 'AB':
            if edge in ('AC', 'armchair'):
                self.layer_shift[1] = self.bond
            elif edge in ('ZZ', 'zigzag'):
                self.layer_shift[0] = self.bond
            else:
                print('unrecognized edge parameter: {}'.format(edge))
                sys.exit(1)

        self.atom1 = Atom(element1)
        self.atom2 = Atom(element2)
        self.atom3 = Atom(element1)
        self.atom4 = Atom(element2)

        self._Natoms = 0
        self._Natoms_per_layer = None

        self.atoms = Atoms(atoms=[self.atom1,
                                  self.atom2,
                                  self.atom3,
                                  self.atom4])
        self.structure_atoms = None

        self._n = n
        self._m = m

        self._a1 = 1.5 * CCbond
        self._a2 = np.sqrt(3) / 2 * CCbond
<|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

import signal

from ansible import constants as C
from ansible.errors import *
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.playbook import Playbook

from ansible.utils.color import colorize, hostcolor
from ansible.utils.debug import debug

class PlaybookExecutor:

    '''
    This is the primary class for executing playbooks, and thus the
    basis for bin/ansible-playbook operation.
    '''

    def __init__(self, playbooks, inventory, variable_manager, loader, display, options):
        self._playbooks        = playbooks
        self._inventory        = inventory
        self._variable_manager = variable_manager
        self._loader           = loader
        self._display          = display
        self._options          = options

        if options.listhosts or options.listtasks or options.listtags:
            self._tqm = None
        else:
            self._tqm = TaskQueueManager(inventory=inventory, callback='default', variable_manager=variable_manager, loader=loader, display=display, options=options)

    def run(self):

        '''
        Run the given playbook, based on the settings in the play which
        may limit the runs to serialized groups, etc.
        '''

        signal.signal(signal.SIGINT, self._cleanup)

        result = 0
        try:
            for playbook_path in self._playbooks:
                pb = Playbook.load(playbook_path, variable_manager=self._variable_manager, loader=self._loader)

                # FIXME: playbook entries are just plays, so we should rename them
                for play in pb.get_entries():
                    self._inventory.remove_restriction()

                    # Create a temporary copy of the play here, so we can run post_validate
                    # on it without the templating changes affecting the original object.
                    all_vars = self._variable_manager.get_vars(loader=self._loader, play=play)
                    new_play = play.copy()
                    new_play.post_validate(all_vars, fail_on_undefined=False)

                    for batch in self._get_serialized_batches(new_play):
                        if len(batch) == 0:
                            self._tqm.send_callback('v2_playbook_on_play_start', new_play)
                            self._tqm.send_callback('v2_playbook_on_no_hosts_matched')
                            result = 0
                            break
                        # restrict the inventory to the hosts in the serialized batch
                        self._inventory.restrict_to_hosts(batch)
                        # and run it...
                        result = self._tqm.run(play=play)
                        if result != 0:
                            break

                    if result != 0:
                        raise AnsibleError("Play failed!: %d" % result)
        finally:
            self._cleanup()

        if result == 0:
            #TODO: move to callback
            # FIXME: this stat summary stuff should be cleaned up and moved
            #        to a new method, if it even belongs here...
            self._display.banner("PLAY RECAP")

            hosts = sorted(self._tqm._stats.processed.keys())
            for h in hosts:
                t = self._tqm._stats.summarize(h)

                self._display.display("%s : %s %s %s %s" % (
                    hostcolor(h, t),
                    colorize('ok', t['ok'], 'green'),
                    colorize('changed', t['changed'], 'yellow'),
                    colorize('unreachable', t['unreachable'], 'red'),
                    colorize('failed', t['failures'], 'red')),
                    screen_only=True
                )

                self._display.display("%s : %s %s %s %s" % (
                    hostcolor(h, t, False),
                    colorize('ok', t['ok'], None),
                    colorize('changed', t['changed'], None),
                    colorize('unreachable', t['unreachable'], None),
                    colorize('failed', t['failures'], None)),
                    log_only=True
                )

            self._display.display("", screen_only=True)
            # END STATS STUFF

        return result

    def _cleanup(self, signum=None, framenum=None):
        return self._tqm.cleanup()

    def _get_serialized_batches(self, play):
        '''
        Returns a list of hosts, subdivided into batches based on
        the serial size specified in the play.
        '''

        # make sure we have a unique list of hosts
        all_hosts = self._inventory.get_hosts(play.hosts)

        # check to see if the serial number was specified as a percentage,
        # and convert it to an integer value based on the number of hosts
        if isinstance(play.serial, basestring) and play.serial.endswith('%'):
            serial_pct = int(play.serial.replace("%",""))
            serial = int((serial_pct/100.0) * len(all_hosts))
        else:
            serial = int(play.serial)

        # if the serial count was not specified or is invalid, default to
        # a list of all hosts, otherwise split the list of hosts into chunks
        # which are based on the serial size
        if serial <= 0:
            return [all_hosts]
        else:
            serialized_batches = []

            while len(all_hosts) > 0:
                play_hosts = []
                for x in range(serial):
                    if len(all_hosts) > 0:
                        play_hosts.append(all_hosts.pop(0))

                serialized_batches.append(play_hosts)

            return serialized_batches

    def list_hosts_per_play(self):

        playlist = []
        try:
            i = 1
            for playbook_path in self._playbooks:
                pb = Playbook.load(playbook_path, variable_manager=self._variable_manager, loader=self._loader)
                for play in pb.get_entries():

                    # Use templated copies in case hosts: depends on variables
                    all_vars = self._variable_manager.get_vars(loader=self._loader, play=play)
                    new_play = play.copy()
                    new_play.post_validate(all_vars, fail_on_undefined=False)

                    pname =  play.get_name().strip()
                    if pname == 'PLAY: <no name specified>':
                        pname = 'play #%d' % i

                    playlist.append( {
                        'name': pname,
                        'pattern': play.hosts,
                        'hosts': set(self._inventory.get_hosts(new_play.hosts)),
                    } )
                    i = i + 1

        except AnsibleError:
            raise
        except Exception, e:
            #TODO: log exception
            raise AnsibleParserError("Failed to process plays: %s" % str(e))

        return playlist
<commit_msg>made listhosts play output name more consistent internally<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

import signal

from ansible import constants as C
from ansible.errors import *
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.playbook import Playbook

from ansible.utils.color import colorize, hostcolor
from ansible.utils.debug import debug

class PlaybookExecutor:

    '''
    This is the primary class for executing playbooks, and thus the
    basis for bin/ansible-playbook operation.
    '''

    def __init__(self, playbooks, inventory, variable_manager, loader, display, options):
        self._playbooks        = playbooks
        self._inventory        = inventory
        self._variable_manager = variable_manager
        self._loader           = loader
        self._display          = display
        self._options          = options

        if options.listhosts or options.listtasks or options.listtags:
            self._tqm = None
        else:
            self._tqm = TaskQueueManager(inventory=inventory, callback='default', variable_manager=variable_manager, loader=loader, display=display, options=options)

    def run(self):

        '''
        Run the given playbook, based on the settings in the play which
        may limit the runs to serialized groups, etc.
        '''

        signal.signal(signal.SIGINT, self._cleanup)

        result = 0
        try:
            for playbook_path in self._playbooks:
                pb = Playbook.load(playbook_path, variable_manager=self._variable_manager, loader=self._loader)

                # FIXME: playbook entries are just plays, so we should rename them
                for play in pb.get_entries():
                    self._inventory.remove_restriction()

                    # Create a temporary copy of the play here, so we can run post_validate
                    # on it without the templating changes affecting the original object.
                    all_vars = self._variable_manager.get_vars(loader=self._loader, play=play)
                    new_play = play.copy()
                    new_play.post_validate(all_vars, fail_on_undefined=False)

                    for batch in self._get_serialized_batches(new_play):
                        if len(batch) == 0:
                            self._tqm.send_callback('v2_playbook_on_play_start', new_play)
                            self._tqm.send_callback('v2_playbook_on_no_hosts_matched')
                            result = 0
                            break
                        # restrict the inventory to the hosts in the serialized batch
                        self._inventory.restrict_to_hosts(batch)
                        # and run it...
                        result = self._tqm.run(play=play)
                        if result != 0:
                            break

                    if result != 0:
                        raise AnsibleError("Play failed!: %d" % result)
        finally:
            self._cleanup()

        if result == 0:
            #TODO: move to callback
            # FIXME: this stat summary stuff should be cleaned up and moved
            #        to a new method, if it even belongs here...
            self._display.banner("PLAY RECAP")

            hosts = sorted(self._tqm._stats.processed.keys())
            for h in hosts:
                t = self._tqm._stats.summarize(h)

                self._display.display("%s : %s %s %s %s" % (
                    hostcolor(h, t),
                    colorize('ok', t['ok'], 'green'),
                    colorize('changed', t['changed'], 'yellow'),
                    colorize('unreachable', t['unreachable'], 'red'),
                    colorize('failed', t['failures'], 'red')),
                    screen_only=True
                )

                self._display.display("%s : %s %s %s %s" % (
                    hostcolor(h, t, False),
                    colorize('ok', t['ok'], None),
                    colorize('changed', t['changed'], None),
                    colorize('unreachable', t['unreachable'], None),
                    colorize('failed', t['failures'], None)),
                    log_only=True
                )

            self._display.display("", screen_only=True)
            # END STATS STUFF

        return result

    def _cleanup(self, signum=None, framenum=None):
        return self._tqm.cleanup()

    def _get_serialized_batches(self, play):
        '''
        Returns a list of hosts, subdivided into batches based on
        the serial size specified in the play.
        '''

        # make sure we have a unique list of hosts
        all_hosts = self._inventory.get_hosts(play.hosts)

        # check to see if the serial number was specified as a percentage,
        # and convert it to an integer value based on the number of hosts
        if isinstance(play.serial, basestring) and play.serial.endswith('%'):
            serial_pct = int(play.serial.replace("%",""))
            serial = int((serial_pct/100.0) * len(all_hosts))
        else:
            serial = int(play.serial)

        # if the serial count was not specified or is invalid, default to
        # a list of all hosts, otherwise split the list of hosts into chunks
        # which are based on the serial size
        if serial <= 0:
            return [all_hosts]
        else:
            serialized_batches = []

            while len(all_hosts) > 0:
                play_hosts = []
                for x in range(serial):
                    if len(all_hosts) > 0:
                        play_hosts.append(all_hosts.pop(0))

                serialized_batches.append(play_hosts)

            return serialized_batches

    def list_hosts_per_play(self):

        playlist = []
        try:
            i = 1
            for playbook_path in self._playbooks:
                pb = Playbook.load(playbook_path, variable_manager=self._variable_manager, loader=self._loader)
                for play in pb.get_entries():

                    # Use templated copies in case hosts: depends on variables
                    all_vars = self._variable_manager.get_vars(loader=self._loader, play=play)
                    new_play = play.copy()
                    new_play.post_validate(all_vars, fail_on_undefined=False)

                    pname =  play.get_name().strip()
                    if pname == 'PLAY: <no name specified>':
                        pname = 'PLAY: #%d' % i

                    playlist.append( {
                        'name': pname,
                        'pattern': play.hosts,
                        'hosts': set(self._inventory.get_hosts(new_play.hosts)),
                    } )
                    i = i + 1

        except AnsibleError:
            raise
        except Exception, e:
            #TODO: log exception
            raise AnsibleParserError("Failed to process plays: %s" % str(e))

        return playlist
<|endoftext|>


<commit_before># "Do or do not. There is no try." — Yoda
# stdlib
from typing import Dict

def get_padawans(cohort: str) -> Dict[str, str]:
    # add yourself to the temple trial roster
    data = {
        "R2Q4": {
            "skywalker": "PASSED",
            "saffron": "PASSED",
            "yash": "PASSED",
            "vinal": "PASSED",
            "yangyuqiao": "PASSED",
            "rodrigo": "PASSED",
            "kanak": "PASSED",
            "theresa": "PASSED",
        }
    }
    return data[cohort]

def test_trial_of_skill() -> None:
    assert get_padawans("R2Q4")["skywalker"] == "PASSED"
    assert get_padawans("R2Q4")["saffron"] == "PASSED"
    assert get_padawans("R2Q4")["vinal"] == "PASSED"
    assert get_padawans("R2Q4")["yash"] == "PASSED"
    assert get_padawans("R2Q4")["yangyuqiao"] == "PASSED"
    assert get_padawans("R2Q4")["rodrigo"] == "PASSED"
    assert get_padawans("R2Q4")["kanak"] == "PASSED"
    assert get_padawans("R2Q4")["theresa"] == "PASSED"
    assert len(get_padawans("R2Q4")) > 1
<commit_msg>Added my name to the skill test trial<commit_after># "Do or do not. There is no try." — Yoda
# stdlib
from typing import Dict

def get_padawans(cohort: str) -> Dict[str, str]:
    # add yourself to the temple trial roster
    data = {
        "R2Q4": {
            "skywalker": "PASSED",
            "saffron": "PASSED",
            "yash": "PASSED",
            "vinal": "PASSED",
            "yangyuqiao": "PASSED",
            "rodrigo": "PASSED",
            "kanak": "PASSED",
            "theresa": "PASSED",
            "amdjed": "PASSED",
        }
    }
    return data[cohort]

def test_trial_of_skill() -> None:
    assert get_padawans("R2Q4")["skywalker"] == "PASSED"
    assert get_padawans("R2Q4")["saffron"] == "PASSED"
    assert get_padawans("R2Q4")["vinal"] == "PASSED"
    assert get_padawans("R2Q4")["yash"] == "PASSED"
    assert get_padawans("R2Q4")["yangyuqiao"] == "PASSED"
    assert get_padawans("R2Q4")["rodrigo"] == "PASSED"
    assert get_padawans("R2Q4")["kanak"] == "PASSED"
    assert get_padawans("R2Q4")["theresa"] == "PASSED"
    assert get_padawans("R2Q4")["amdjed"] == "PASSED"
    assert len(get_padawans("R2Q4")) > 1
<|endoftext|>


<commit_before>## system imports
import sys, os
import time
import copy

## loading imports
import json

## math imports
import numpy as np

## COCO imports
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
from pycocotools import mask
from pycocotools.cocoanalyze import COCOanalyze

## plotting imports
import matplotlib.patches as mpatches
from matplotlib.patches import Polygon
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
import skimage.io as io

'''
Set paths to data and load data
'''
## set paths of ground truth and detections
BASE_DIR = '.'
COCO_GT  = BASE_DIR + '/data/person_keypoints_anns.json'
SAVE_DIR = BASE_DIR + '/outputs'

TEAM_NAME = 'grmi'
DTS       = BASE_DIR + '/data/%s.json'%TEAM_NAME

print "\n==============================================="
print "COCO_GT:      [%s]"%COCO_GT
print "DTS:          [%s]"%DTS
print "SAVE_DIR:     [%s]"%SAVE_DIR
print "===============================================\n"

## load coco ground-truth
coco_gt = COCO( COCO_GT )
print("{:15}[{}] instances in [{}] images.".format('Ground-truth:',
                                                   len(coco_gt.getAnnIds()),
                                                   len(coco_gt.getImgIds())))

## create imgs_info dictionary
with open(COCO_GT,'rb') as fp:
    data = json.load(fp)
imgs_info = {i['id']:{'id':i['id'] ,
                      'width':i['width'],
                      'height':i['height']}
                       for i in data['images']}
assert(len(coco_gt.getImgIds())==len(imgs_info))

## load team detections
with open(DTS,'rb') as fp: team_dts = json.load(fp)
team_dts     = [d for d in team_dts if d['image_id'] in imgs_info]
team_img_ids = set([d['image_id'] for d in team_dts])
print("{:15}[{}] instances in [{}] images.".format('Detections:',
                                                   len(team_dts),
                                                   len(team_img_ids)))

## create detection object
coco_dt   = coco_gt.loadRes(team_dts)

## create analyze object
coco_analyze = COCOanalyze(coco_gt,coco_dt,'keypoints')

oksThrs     = .7
areaRngs    = [[32**2, 1e5**2],[32**2, 96**2],[96**2, 1e5**2]]
areaRngLbls = ['all','medium','large']

print "================================================"
print "[%s,%s,%s]"%(oks,areaRng,areaRngLbls[aind])
print "================================================"

coco_analyze.params.oksThrs    = [oksThrs]
coco_analyze.params.areaRng    = [areaRngs[0]]
coco_analyze.params.areaRngLbl = [areaRngLbls[0]]

coco_analyze.analyze(check_kpts=True, check_scores=True, check_false=True)
coco_analyze.summarize(makeplots=True,savedir=SAVE_DIR,team_name=TEAM_NAME)
results = coco_analyze.stats

matches = coco_analyze.matches
with open('{}/dt_gt_matches_[{}][{}][{}][{}].json'.format(SAVE_DIR, TEAM_NAME,
            areaRngLbls[aind],
            coco_analyze.params.maxDets[0],
            int(100*oks)), 'wb') as fp:
    json.dump(matches,fp)

corrected_dts = coco_analyze.corrected_dts
with open('{}/corrected_dts_[{}][{}][{}][{}].json'.format(SAVE_DIR, TEAM_NAME,
            areaRngLbls[aind],
            coco_analyze.params.maxDets[0],
            int(100*oks)), 'wb') as fp:
    json.dump(corrected_dts,fp)

false_neg_gts = coco_analyze.false_neg_gts
with open('{}/false_neg_gts_[{}][{}][{}][{}].json'.format(SAVE_DIR, TEAM_NAME,
            areaRngLbls[aind],
            coco_analyze.params.maxDets[0],
            int(100*oks)), 'wb') as fp:
    json.dump(false_neg_gts,fp)

print results

with open('%s/error_stats_%s.json'%(SAVE_DIR,TEAM_NAME),'wb') as fp:
    json.dump(results,fp)
<commit_msg>Delete COCOanalyze_demo.py<commit_after><|endoftext|>


<commit_before>#!/usr/bin/env python
#
# Copyright 2016 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Generates TypeScript defs from Closure externs.

This uses the Clutz tool from https://github.com/angular/clutz and then performs
additional transformations to make the generated defs independent of Clutz.
"""

import argparse
import os
import re
import sys

import shakaBuildHelpers

def GenerateTsDefs(inputs, output):
  """Generates TypeScript defs from Closure externs.

  Args:
    inputs: A list of paths to Closure extern files.
    output: A path to a TypeScript def output file.
  """
  clutz = shakaBuildHelpers.get_node_binary('@teppeis/clutz', 'clutz')

  command = clutz + [
      '--closure_env', 'BROWSER',
      '--externs',
  ] + inputs + [
      '-o', '-',
  ]

  # Get the output of Clutz, then transform it to make it independent of Clutz
  # and usable directly in TypeScript projects.
  contents = shakaBuildHelpers.execute_get_output(command)

  # Remove the prefix clutz puts on all namespaces.
  contents = contents.replace(b'\xe0\xb2\xa0_\xe0\xb2\xa0.clutz.', b'')
  # Replace "GlobalObject" (from Clutz) with TypeScript-native "object".
  contents = re.sub(br'\bGlobalObject\b', b'object', contents)
  # Remove "Global" from Clutz's Global{Date,Element,Event,EventTarget} and use
  # their original definitions instead.
  contents = re.sub(
      br'\bGlobal(Date|Element|Event|EventTarget)\b', br'\1', contents)
  # Remove "protected", which appears on some fields, and is only supported on
  # methods.
  contents = re.sub(br'^\s*protected ', b'', contents, flags=re.MULTILINE)
  # Clutz really likes EventTargets, so it wants IAdManager to extend it twice.
  contents = contents.replace(
      b'extends EventTarget extends EventTarget',
      b'extends EventTarget')
  # Some types allow you to return a Promise or nothing, but the Clutz output of
  # "Promise | null | undefined" doesn't work in TypeScript.  We need to use
  # "Promise | void" instead.
  contents = contents.replace(b'| null | undefined', b'| void')
  # shaka.util.Error extends Error and implements shaka.extern.Error, but this
  # confuses TypeScript when both are called "Error" in context of the namespace
  # declaration for "shaka.util".  Therefore we need to declare this
  # "GlobalError" type, which is already referenced by Clutz but never defined.
  global_error_def = b'declare class GlobalError extends Error {}\n\n'
  contents = global_error_def + contents
  # There are some types that implement multiple interfaces, such as IReleasable
  # and Iteratable.  Also, there are tools used inside Google that (for some
  # reason) want to convert TS defs _BACK_ into Closure Compiler externs.  When
  # that happens, these multiple implementors generate externs with broken
  # @implements annotations.  Since this team can't control that process or
  # those tools, we need to adjust our TS defs instead.  In these particular
  # cases, thankfully, we can just remove the reference to the IReleasable
  # interface, which is not needed outside the library.
  # TODO: This only covers one very specific pattern, and could be brittle.
  contents = contents.replace(
      b'implements shaka.util.IReleasable , ', b'implements ')
  # Finally, Clutz includes a bunch of basic defs for a browser environment
  # generated from Closure compiler's builtins.  Remove these.
  sections = re.split(br'\n(?=// Generated from .*)', contents)
  sections = filter(
      lambda s: not s.startswith(b'// Generated from externs.zip'),
      sections)
  contents = b'\n'.join(sections) + b'\n'

  license_header_path = os.path.join(
      shakaBuildHelpers.get_source_base(), 'build/license-header')

  with open(license_header_path, 'rb') as f:
    license_header = f.read()

  with open(output, 'wb') as f:
    f.write(license_header)
    f.write(b'\n')
    f.write(contents)
    f.write(b'\n')
    f.write(b'export default shaka;\n')

def CreateParser():
  """Create the argument parser for this application."""
  base = shakaBuildHelpers.get_source_base()

  parser = argparse.ArgumentParser(
      description=__doc__,
      formatter_class=argparse.RawDescriptionHelpFormatter)

  parser.add_argument(
      '--output',
      type=str,
      help='The file path for TypeScripts defs output')

  parser.add_argument(
      'input',
      type=str,
      nargs='+',
      help='The Closure extern files to be converted to TypeScript defs')

  return parser

def main(args):
  parser = CreateParser()
  args = parser.parse_args(args)
  GenerateTsDefs(args.input, args.output)
  return 0

if __name__ == '__main__':
  main(sys.argv[1:])
<commit_msg>fix: Revert "Add missing module export in generated typescript defs" (#4175)<commit_after>#!/usr/bin/env python
#
# Copyright 2016 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Generates TypeScript defs from Closure externs.

This uses the Clutz tool from https://github.com/angular/clutz and then performs
additional transformations to make the generated defs independent of Clutz.
"""

import argparse
import os
import re
import sys

import shakaBuildHelpers

def GenerateTsDefs(inputs, output):
  """Generates TypeScript defs from Closure externs.

  Args:
    inputs: A list of paths to Closure extern files.
    output: A path to a TypeScript def output file.
  """
  clutz = shakaBuildHelpers.get_node_binary('@teppeis/clutz', 'clutz')

  command = clutz + [
      '--closure_env', 'BROWSER',
      '--externs',
  ] + inputs + [
      '-o', '-',
  ]

  # Get the output of Clutz, then transform it to make it independent of Clutz
  # and usable directly in TypeScript projects.
  contents = shakaBuildHelpers.execute_get_output(command)

  # Remove the prefix clutz puts on all namespaces.
  contents = contents.replace(b'\xe0\xb2\xa0_\xe0\xb2\xa0.clutz.', b'')
  # Replace "GlobalObject" (from Clutz) with TypeScript-native "object".
  contents = re.sub(br'\bGlobalObject\b', b'object', contents)
  # Remove "Global" from Clutz's Global{Date,Element,Event,EventTarget} and use
  # their original definitions instead.
  contents = re.sub(
      br'\bGlobal(Date|Element|Event|EventTarget)\b', br'\1', contents)
  # Remove "protected", which appears on some fields, and is only supported on
  # methods.
  contents = re.sub(br'^\s*protected ', b'', contents, flags=re.MULTILINE)
  # Clutz really likes EventTargets, so it wants IAdManager to extend it twice.
  contents = contents.replace(
      b'extends EventTarget extends EventTarget',
      b'extends EventTarget')
  # Some types allow you to return a Promise or nothing, but the Clutz output of
  # "Promise | null | undefined" doesn't work in TypeScript.  We need to use
  # "Promise | void" instead.
  contents = contents.replace(b'| null | undefined', b'| void')
  # shaka.util.Error extends Error and implements shaka.extern.Error, but this
  # confuses TypeScript when both are called "Error" in context of the namespace
  # declaration for "shaka.util".  Therefore we need to declare this
  # "GlobalError" type, which is already referenced by Clutz but never defined.
  global_error_def = b'declare class GlobalError extends Error {}\n\n'
  contents = global_error_def + contents
  # There are some types that implement multiple interfaces, such as IReleasable
  # and Iteratable.  Also, there are tools used inside Google that (for some
  # reason) want to convert TS defs _BACK_ into Closure Compiler externs.  When
  # that happens, these multiple implementors generate externs with broken
  # @implements annotations.  Since this team can't control that process or
  # those tools, we need to adjust our TS defs instead.  In these particular
  # cases, thankfully, we can just remove the reference to the IReleasable
  # interface, which is not needed outside the library.
  # TODO: This only covers one very specific pattern, and could be brittle.
  contents = contents.replace(
      b'implements shaka.util.IReleasable , ', b'implements ')
  # Finally, Clutz includes a bunch of basic defs for a browser environment
  # generated from Closure compiler's builtins.  Remove these.
  sections = re.split(br'\n(?=// Generated from .*)', contents)
  sections = filter(
      lambda s: not s.startswith(b'// Generated from externs.zip'),
      sections)
  contents = b'\n'.join(sections) + b'\n'

  license_header_path = os.path.join(
      shakaBuildHelpers.get_source_base(), 'build/license-header')

  with open(license_header_path, 'rb') as f:
    license_header = f.read()

  with open(output, 'wb') as f:
    f.write(license_header)
    f.write(b'\n')
    f.write(contents)

def CreateParser():
  """Create the argument parser for this application."""
  base = shakaBuildHelpers.get_source_base()

  parser = argparse.ArgumentParser(
      description=__doc__,
      formatter_class=argparse.RawDescriptionHelpFormatter)

  parser.add_argument(
      '--output',
      type=str,
      help='The file path for TypeScripts defs output')

  parser.add_argument(
      'input',
      type=str,
      nargs='+',
      help='The Closure extern files to be converted to TypeScript defs')

  return parser

def main(args):
  parser = CreateParser()
  args = parser.parse_args(args)
  GenerateTsDefs(args.input, args.output)
  return 0

if __name__ == '__main__':
  main(sys.argv[1:])
<|endoftext|>