<commit_before># Copyright 2016 The Johns Hopkins University Applied Physics Laboratory
#
# 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.

"""
Create the DynamoDB lambda configuration which consists of:

  * Lambda function written in NodeJS
  * Lambda policy
"""

import boto3
import configparser
import io
from lib.cloudformation import CloudFormationConfiguration, Arg, Arn, Ref
from lib.names import AWSNames
from lib.external import ExternalCalls
from lib import aws
from lib import constants as const
import os
import subprocess

from update_lambda_fcn import load_lambdas_on_s3

# Location of repo with the lambda autoscaler.
LAMBDA_ROOT_FOLDER = os.path.join(
    os.path.dirname(__file__), '../lambda/dynamodb-lambda-autoscale')

# Zip file created by `npm run build`
LAMBDA_ZIP_FILE = os.path.join(LAMBDA_ROOT_FOLDER, 'dist.zip')

CONFIG_TEMPLATE_PATH = os.path.join(
    os.path.dirname(__file__), 'dynamo_config.template')
CONFIG_OUTPUT_PATH = os.path.join(LAMBDA_ROOT_FOLDER, 'config.env.production')

DYNAMO_LAMBDA_KEY = 'DynamoLambda'
TRIGGER_KEY = 'TriggerDynamoAutoscale'

SLACK_WEBHOOK_HOST = 'SLACK_WEBHOOK_HOST'

# This variable is the one used by the lambda function.
SLACK_WEBHOOK_PATH = 'SLACK_WEBHOOK_PATH'

# Value in this config variable will be written to SLACK_WEBHOOK_PATH when
# standing up the service in production.
SLACK_WEBHOOK_PATH_PRODUCTION = 'SLACK_WEBHOOK_PATH_PRODUCTION'

# Value in this config variable will be written to SLACK_WEBHOOK_PATH when
# standing up the service in a development stack or an integration stack.
SLACK_WEBHOOK_PATH_DEV = 'SLACK_WEBHOOK_PATH_DEV'

# Domain name will be included in messages to Slack and also determines
# whether SLACK_WEBHOOK_PATH_DEV's or SLACK_WEBHOOK_PATH_PRODUCTION's value is
# written to SLACK_WEBHOOK_PATH.
VPC_DOMAIN =  'VPC_DOMAIN'

# Used to override normal autoscale rules when creating a developer's stack.
# All tables will use the autoscale rules defined by the "default" config to
# avoid spending too much.  The production autoscale rules have minimums that
# are way too high for DynamoDB tables for developers.
DEV_STACK = 'DEV_STACK'

def create_config(session, domain):
    """
    Create the CloudFormationConfiguration object.
    Args:
        session (Session): amazon session object
        domain (str): domain of the stack being created

    Returns: the config for the Cloud Formation stack
    """
    names = AWSNames(domain)
    config = CloudFormationConfiguration("dynamolambda", domain, const.REGION)

    role = aws.role_arn_lookup(session, "lambda_cache_execution")
    config.add_arg(Arg.String("LambdaCacheExecutionRole", role,
                              "IAM role for multilambda." + domain))

    lambda_bucket = aws.get_lambda_s3_bucket(session)
    lambda_key = generate_lambda_key(domain)
    config.add_lambda(DYNAMO_LAMBDA_KEY,
                      names.dynamo_lambda,
                      Ref("LambdaCacheExecutionRole"),
                      s3=(aws.get_lambda_s3_bucket(session),
                          lambda_key,
                          "index.handler"),
                      timeout=120,
                      memory=128,
                      runtime="nodejs6.10")

    config.add_cloudwatch_rule(TRIGGER_KEY,
                               name=names.trigger_dynamo_autoscale,
                               description='Run DynamoDB table autoscaler',
                               targets=[
                                   {
                                       'Arn': Arn(DYNAMO_LAMBDA_KEY),
                                       'Id': names.vault_monitor,
                                   }
                               ],
                               schedule='rate(1 minute)',
                               depends_on=[DYNAMO_LAMBDA_KEY])

    config.add_lambda_permission('TriggerPerms',
                                 names.dynamo_lambda,
                                 principal='events.amazonaws.com',
                                 source=Arn(TRIGGER_KEY))

    return config

def generate(session, domain):
    """Create the configuration and save it to disk"""
    config = create_config(session, domain)
    config.generate()

def create(session, domain):
    """Create the configuration, and launch it"""
    keypair = aws.keypair_lookup(session)

    try:
        pre_init(session, domain)

        config = create_config(session, domain)

        success = config.create(session)
        if not success:
            raise Exception("Create Failed")
        else:
            post_init(session, domain)
    except:
        # DP NOTE: This will catch errors from pre_init, create, and post_init
        print("Error detected")
        raise

def pre_init(session, domain):
    """
    Create NodeJS config file from template.
    Package NodeJS lambda function.
    Upload .zip to S3 bucket.
    """
    with open(CONFIG_TEMPLATE_PATH) as fh:
        config_str = fh.read()
    update_config_file(config_str, domain)

    build_lambda()
    bucket = aws.get_lambda_s3_bucket(session)
    zip_file = os.path.join(LAMBDA_ROOT_FOLDER, LAMBDA_ZIP_FILE)
    zips_s3_key = upload_to_s3(session, domain, zip_file, bucket)

def post_init(session, domain):
    pass

def update_config_file(config_str, domain):
    """Update config file that stores environment variables for the lambda
    environment.

    Args:
        config_str (str): String representation of config file template.
    """
    parser = configparser.ConfigParser()
    # Disable default transform to lowercase of keys.
    parser.optionxform = lambda option: option
    parser.read_string(config_str)
    parser.set('default', VPC_DOMAIN, domain)

    slack_host = parser.get('default', SLACK_WEBHOOK_HOST)
    slack_path_prod = parser.get('default', SLACK_WEBHOOK_PATH_PRODUCTION)
    slack_path_dev = parser.get('default', SLACK_WEBHOOK_PATH_DEV)
    if domain == 'production.boss':
        parser.set('default', SLACK_WEBHOOK_PATH, slack_path_prod)
    else:
        parser.set('default', SLACK_WEBHOOK_PATH, slack_path_dev)
        if domain != 'integration.boss':
            # Override normal autoscale parameters when deploying to a
            # developer stack.
            parser.set('default', DEV_STACK, '')
    slack_path = parser.get('default', SLACK_WEBHOOK_PATH)

    # Remove stack specific variables before outputting.
    parser.remove_option('default', SLACK_WEBHOOK_PATH_DEV)
    parser.remove_option('default', SLACK_WEBHOOK_PATH_PRODUCTION)

    print('\nWill post to Slack at https://{}{}'.format(slack_host, slack_path))

    updated_config = io.StringIO()
    parser.write(updated_config)
    config_str = updated_config.getvalue()
    updated_config.close()

    # Strip default section header from config.  NodeJS config file does not
    # use sections.
    _, headerless_config = config_str.split('[default]', 1)

    # Convert variable names back to upper case.
    headerless_config = headerless_config.replace(
        SLACK_WEBHOOK_HOST.lower(), SLACK_WEBHOOK_HOST).replace(
            SLACK_WEBHOOK_PATH.lower(), SLACK_WEBHOOK_PATH).replace(
                VPC_DOMAIN.lower(), VPC_DOMAIN)

    with open(CONFIG_OUTPUT_PATH, 'w') as out:
        out.write(headerless_config)
    #print(headerless_config)

def build_lambda():
    """Package lambda in preparation to upload to S3."""
    install_node_deps()
    build_node()

def install_node_deps():
    """npm install NodeJS dependencies."""
    print('Installing NodeJS dependencies.')
    args = ('npm', 'install')
    popen = subprocess.Popen(args, cwd=LAMBDA_ROOT_FOLDER, stdout=subprocess.PIPE)
    exit_code = popen.wait()
    output = popen.stdout.read()
    if not exit_code == 0:
        print(str(output))
        raise RuntimeError('Failed to install dependencies.')

def build_node():
    """Build and package in dist.zip."""
    print('Packaging NodeJS app.')
    args = ('npm', 'run', 'build')
    popen = subprocess.Popen(args, cwd=LAMBDA_ROOT_FOLDER, stdout=subprocess.PIPE)
    exit_code = popen.wait()
    output = popen.stdout.read()
    if not exit_code == 0:
        print(str(output))
        raise RuntimeError('Failed to build Node application.')

def upload_to_s3(session, domain, zip_file, bucket):
    """Upload the zip file to the given S3 bucket.

    Args:
        session (Session): Boto3 Session.
        domain (str): domain of the stack being created
        zip_file (str): Name of zip file.
        bucket (str): Name of bucket to use.
    """
    print('Uploading to S3.')
    key = generate_lambda_key(domain)
    s3 = session.client('s3')
    s3.create_bucket(Bucket=bucket)
    s3.put_object(Bucket=bucket, Key=key, Body=open(zip_file, 'rb'))

def generate_lambda_key(domain):
    """Generate the S3 key name for the lambda's zip file.

    Args:
        domain (str): Use the domain as part of the key.

    Returns:
        (str)
    """
    key = 'dynamodb_autoscale.' + domain + '.zip'
    return key

<commit_msg>updated dynamolambda.py to add reserved_execuitions=1<commit_after># Copyright 2016 The Johns Hopkins University Applied Physics Laboratory
#
# 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.

"""
Create the DynamoDB lambda configuration which consists of:

  * Lambda function written in NodeJS
  * Lambda policy
"""

import boto3
import configparser
import io
from lib.cloudformation import CloudFormationConfiguration, Arg, Arn, Ref
from lib.names import AWSNames
from lib.external import ExternalCalls
from lib import aws
from lib import constants as const
import os
import subprocess

from update_lambda_fcn import load_lambdas_on_s3

# Location of repo with the lambda autoscaler.
LAMBDA_ROOT_FOLDER = os.path.join(
    os.path.dirname(__file__), '../lambda/dynamodb-lambda-autoscale')

# Zip file created by `npm run build`
LAMBDA_ZIP_FILE = os.path.join(LAMBDA_ROOT_FOLDER, 'dist.zip')

CONFIG_TEMPLATE_PATH = os.path.join(
    os.path.dirname(__file__), 'dynamo_config.template')
CONFIG_OUTPUT_PATH = os.path.join(LAMBDA_ROOT_FOLDER, 'config.env.production')

DYNAMO_LAMBDA_KEY = 'DynamoLambda'
TRIGGER_KEY = 'TriggerDynamoAutoscale'

SLACK_WEBHOOK_HOST = 'SLACK_WEBHOOK_HOST'

# This variable is the one used by the lambda function.
SLACK_WEBHOOK_PATH = 'SLACK_WEBHOOK_PATH'

# Value in this config variable will be written to SLACK_WEBHOOK_PATH when
# standing up the service in production.
SLACK_WEBHOOK_PATH_PRODUCTION = 'SLACK_WEBHOOK_PATH_PRODUCTION'

# Value in this config variable will be written to SLACK_WEBHOOK_PATH when
# standing up the service in a development stack or an integration stack.
SLACK_WEBHOOK_PATH_DEV = 'SLACK_WEBHOOK_PATH_DEV'

# Domain name will be included in messages to Slack and also determines
# whether SLACK_WEBHOOK_PATH_DEV's or SLACK_WEBHOOK_PATH_PRODUCTION's value is
# written to SLACK_WEBHOOK_PATH.
VPC_DOMAIN =  'VPC_DOMAIN'

# Used to override normal autoscale rules when creating a developer's stack.
# All tables will use the autoscale rules defined by the "default" config to
# avoid spending too much.  The production autoscale rules have minimums that
# are way too high for DynamoDB tables for developers.
DEV_STACK = 'DEV_STACK'

def create_config(session, domain):
    """
    Create the CloudFormationConfiguration object.
    Args:
        session (Session): amazon session object
        domain (str): domain of the stack being created

    Returns: the config for the Cloud Formation stack
    """
    names = AWSNames(domain)
    config = CloudFormationConfiguration("dynamolambda", domain, const.REGION)

    role = aws.role_arn_lookup(session, "lambda_cache_execution")
    config.add_arg(Arg.String("LambdaCacheExecutionRole", role,
                              "IAM role for multilambda." + domain))

    lambda_bucket = aws.get_lambda_s3_bucket(session)
    lambda_key = generate_lambda_key(domain)
    config.add_lambda(DYNAMO_LAMBDA_KEY,
                      names.dynamo_lambda,
                      Ref("LambdaCacheExecutionRole"),
                      s3=(aws.get_lambda_s3_bucket(session),
                          lambda_key,
                          "index.handler"),
                      timeout=120,
                      memory=128,
                      runtime="nodejs6.10",
                      reserved_executions=1)

    config.add_cloudwatch_rule(TRIGGER_KEY,
                               name=names.trigger_dynamo_autoscale,
                               description='Run DynamoDB table autoscaler',
                               targets=[
                                   {
                                       'Arn': Arn(DYNAMO_LAMBDA_KEY),
                                       'Id': names.vault_monitor,
                                   }
                               ],
                               schedule='rate(1 minute)',
                               depends_on=[DYNAMO_LAMBDA_KEY])

    config.add_lambda_permission('TriggerPerms',
                                 names.dynamo_lambda,
                                 principal='events.amazonaws.com',
                                 source=Arn(TRIGGER_KEY))

    return config

def generate(session, domain):
    """Create the configuration and save it to disk"""
    config = create_config(session, domain)
    config.generate()

def create(session, domain):
    """Create the configuration, and launch it"""
    keypair = aws.keypair_lookup(session)

    try:
        pre_init(session, domain)

        config = create_config(session, domain)

        success = config.create(session)
        if not success:
            raise Exception("Create Failed")
        else:
            post_init(session, domain)
    except:
        # DP NOTE: This will catch errors from pre_init, create, and post_init
        print("Error detected")
        raise

def pre_init(session, domain):
    """
    Create NodeJS config file from template.
    Package NodeJS lambda function.
    Upload .zip to S3 bucket.
    """
    with open(CONFIG_TEMPLATE_PATH) as fh:
        config_str = fh.read()
    update_config_file(config_str, domain)

    build_lambda()
    bucket = aws.get_lambda_s3_bucket(session)
    zip_file = os.path.join(LAMBDA_ROOT_FOLDER, LAMBDA_ZIP_FILE)
    zips_s3_key = upload_to_s3(session, domain, zip_file, bucket)

def post_init(session, domain):
    pass

def update_config_file(config_str, domain):
    """Update config file that stores environment variables for the lambda
    environment.

    Args:
        config_str (str): String representation of config file template.
    """
    parser = configparser.ConfigParser()
    # Disable default transform to lowercase of keys.
    parser.optionxform = lambda option: option
    parser.read_string(config_str)
    parser.set('default', VPC_DOMAIN, domain)

    slack_host = parser.get('default', SLACK_WEBHOOK_HOST)
    slack_path_prod = parser.get('default', SLACK_WEBHOOK_PATH_PRODUCTION)
    slack_path_dev = parser.get('default', SLACK_WEBHOOK_PATH_DEV)
    if domain == 'production.boss':
        parser.set('default', SLACK_WEBHOOK_PATH, slack_path_prod)
    else:
        parser.set('default', SLACK_WEBHOOK_PATH, slack_path_dev)
        if domain != 'integration.boss':
            # Override normal autoscale parameters when deploying to a
            # developer stack.
            parser.set('default', DEV_STACK, '')
    slack_path = parser.get('default', SLACK_WEBHOOK_PATH)

    # Remove stack specific variables before outputting.
    parser.remove_option('default', SLACK_WEBHOOK_PATH_DEV)
    parser.remove_option('default', SLACK_WEBHOOK_PATH_PRODUCTION)

    print('\nWill post to Slack at https://{}{}'.format(slack_host, slack_path))

    updated_config = io.StringIO()
    parser.write(updated_config)
    config_str = updated_config.getvalue()
    updated_config.close()

    # Strip default section header from config.  NodeJS config file does not
    # use sections.
    _, headerless_config = config_str.split('[default]', 1)

    # Convert variable names back to upper case.
    headerless_config = headerless_config.replace(
        SLACK_WEBHOOK_HOST.lower(), SLACK_WEBHOOK_HOST).replace(
            SLACK_WEBHOOK_PATH.lower(), SLACK_WEBHOOK_PATH).replace(
                VPC_DOMAIN.lower(), VPC_DOMAIN)

    with open(CONFIG_OUTPUT_PATH, 'w') as out:
        out.write(headerless_config)
    #print(headerless_config)

def build_lambda():
    """Package lambda in preparation to upload to S3."""
    install_node_deps()
    build_node()

def install_node_deps():
    """npm install NodeJS dependencies."""
    print('Installing NodeJS dependencies.')
    args = ('npm', 'install')
    popen = subprocess.Popen(args, cwd=LAMBDA_ROOT_FOLDER, stdout=subprocess.PIPE)
    exit_code = popen.wait()
    output = popen.stdout.read()
    if not exit_code == 0:
        print(str(output))
        raise RuntimeError('Failed to install dependencies.')

def build_node():
    """Build and package in dist.zip."""
    print('Packaging NodeJS app.')
    args = ('npm', 'run', 'build')
    popen = subprocess.Popen(args, cwd=LAMBDA_ROOT_FOLDER, stdout=subprocess.PIPE)
    exit_code = popen.wait()
    output = popen.stdout.read()
    if not exit_code == 0:
        print(str(output))
        raise RuntimeError('Failed to build Node application.')

def upload_to_s3(session, domain, zip_file, bucket):
    """Upload the zip file to the given S3 bucket.

    Args:
        session (Session): Boto3 Session.
        domain (str): domain of the stack being created
        zip_file (str): Name of zip file.
        bucket (str): Name of bucket to use.
    """
    print('Uploading to S3.')
    key = generate_lambda_key(domain)
    s3 = session.client('s3')
    s3.create_bucket(Bucket=bucket)
    s3.put_object(Bucket=bucket, Key=key, Body=open(zip_file, 'rb'))

def generate_lambda_key(domain):
    """Generate the S3 key name for the lambda's zip file.

    Args:
        domain (str): Use the domain as part of the key.

    Returns:
        (str)
    """
    key = 'dynamodb_autoscale.' + domain + '.zip'
    return key

<|endoftext|>


<commit_before>########
# Copyright (c) 2014 GigaSpaces Technologies Ltd. All rights reserved
#
# 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.

"""
Tests 'cfy use'
"""

from mock import MagicMock
from mock import patch

from cloudify_rest_client import CloudifyClient
from cloudify_cli import utils

from cloudify_cli.tests import cli_runner
from cloudify_cli.tests.commands.test_cli_command import CliCommandTest

class UseTest(CliCommandTest):

    def test_use_command(self):
        self.client.manager.get_status = MagicMock()
        self.client.manager.get_context = MagicMock(
            return_value={
                'name': 'name',
                'context': {}}
        )
        self._create_cosmo_wd_settings()
        cli_runner.run_cli('cfy use -t 127.0.0.1')
        cwds = self._read_cosmo_wd_settings()
        self.assertEquals("127.0.0.1",
                          cwds.get_management_server())

    def test_use_command_no_prior_init(self):
        self.client.manager.get_status = MagicMock()
        self.client.manager.get_context = MagicMock(
            return_value={
                'name': 'name', 'context': {}
            }
        )
        cli_runner.run_cli('cfy use -t 127.0.0.1')
        cwds = self._read_cosmo_wd_settings()
        self.assertEquals('127.0.0.1', cwds.get_management_server())

    def test_secured_use(self):
        host = '127.0.0.1'
        auth_header = utils.get_auth_header('test_username', 'test_password')
        self.client = CloudifyClient(host=host, headers=auth_header)
        self.client.manager.get_context = MagicMock(
            return_value={
                'name': 'name',
                'context': {}
            }
        )

        self.headers = None

        def mock_do_request(*_, **kwargs):
            self.headers = kwargs.get('headers')
            return 'success'

        # run cli use command
        with patch('cloudify_rest_client.client.HTTPClient._do_request',
                   new=mock_do_request):
            cli_runner.run_cli('cfy use -t {0}'.format(host))

        # assert Authorization in headers
        expected_headers = {
            'Content-type': 'application/json',
            'Authorization': self.client._client.encoded_credentials
        }
        self.assertEqual(expected_headers, self.headers)
<commit_msg>modified test to match new client fields<commit_after>########
# Copyright (c) 2014 GigaSpaces Technologies Ltd. All rights reserved
#
# 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.

"""
Tests 'cfy use'
"""

from mock import MagicMock
from mock import patch

from cloudify_rest_client import CloudifyClient
from cloudify_cli import utils

from cloudify_cli.tests import cli_runner
from cloudify_cli.tests.commands.test_cli_command import CliCommandTest

class UseTest(CliCommandTest):

    def test_use_command(self):
        self.client.manager.get_status = MagicMock()
        self.client.manager.get_context = MagicMock(
            return_value={
                'name': 'name',
                'context': {}}
        )
        self._create_cosmo_wd_settings()
        cli_runner.run_cli('cfy use -t 127.0.0.1')
        cwds = self._read_cosmo_wd_settings()
        self.assertEquals("127.0.0.1",
                          cwds.get_management_server())

    def test_use_command_no_prior_init(self):
        self.client.manager.get_status = MagicMock()
        self.client.manager.get_context = MagicMock(
            return_value={
                'name': 'name', 'context': {}
            }
        )
        cli_runner.run_cli('cfy use -t 127.0.0.1')
        cwds = self._read_cosmo_wd_settings()
        self.assertEquals('127.0.0.1', cwds.get_management_server())

    def test_secured_use(self):
        host = '127.0.0.1'
        auth_header = utils.get_auth_header('test_username', 'test_password')
        self.client = CloudifyClient(host=host, headers=auth_header)
        self.client.manager.get_context = MagicMock(
            return_value={
                'name': 'name',
                'context': {}
            }
        )

        self.headers = None

        def mock_do_request(*_, **kwargs):
            self.do_request_headers = kwargs.get('headers')
            return 'success'

        # run cli use command
        with patch('cloudify_rest_client.client.HTTPClient._do_request',
                   new=mock_do_request):
            cli_runner.run_cli('cfy use -t {0}'.format(host))

        # assert Authorization in headers
        eventual_request_headers = self.client._client.headers
        self.assertEqual(self.do_request_headers, eventual_request_headers)
<|endoftext|>


<commit_before>import binascii
from typing import Union
from typing import List

import torch
import websocket
import time
import logging

import syft as sy
from syft.codes import MSGTYPE
from syft.frameworks.torch.tensors.interpreters import AbstractTensor
from syft.workers import BaseWorker

logger = logging.getLogger(__name__)

TIMEOUT_INTERVAL = 9999999

class WebsocketClientWorker(BaseWorker):
    def __init__(
        self,
        hook,
        host: str,
        port: int,
        id: Union[int, str] = 0,
        is_client_worker: bool = False,
        log_msgs: bool = False,
        verbose: bool = False,
        data: List[Union[torch.Tensor, AbstractTensor]] = None,
    ):
        """A client which will forward all messages to a remote worker running a
        WebsocketServerWorker and receive all responses back from the server.
        """

        # TODO get angry when we have no connection params
        self.port = port
        self.host = host
        self.uri = f"ws://{self.host}:{self.port}"

        # creates the connection with the server which gets held open until the
        # WebsocketClientWorker is garbage collected.
        # Also avoid the server from timing out on the server-side in case of slow clients
        self.ws = websocket.create_connection(self.uri, max_size=None, timeout=TIMEOUT_INTERVAL)

        super().__init__(hook, id, data, is_client_worker, log_msgs, verbose)

    def search(self, *query):
        # Prepare a message requesting the websocket server to search among its objects
        message = (MSGTYPE.SEARCH, query)
        serialized_message = sy.serde.serialize(message)
        # Send the message and return the deserialized response.
        response = self._recv_msg(serialized_message)
        return sy.serde.deserialize(response)

    def _send_msg(self, message: bin, location) -> bin:
        self.ws.settimeout(TIMEOUT_INTERVAL)
        raise RuntimeError(
            "_send_msg should never get called on a ",
            "WebsocketClientWorker. Did you accidentally "
            "make hook.local_worker a WebsocketClientWorker?",
        )

    def _receive_action(self, message: bin) -> bin:
        self.ws.send(str(binascii.hexlify(message)))
        response = binascii.unhexlify(self.ws.recv()[2:-1])
        return response

    def _recv_msg(self, message: bin) -> bin:
        """Forwards a message to the WebsocketServerWorker"""

        response = self._receive_action(message)
        if not self.ws.connected:
            logger.warning("Websocket connection closed (worker: %s)", self.id)
            self.ws.shutdown()
            time.sleep(1)
            # Avoid timing out on the server-side
            self.ws = websocket.create_connection(self.uri, max_size=None, timeout=TIMEOUT_INTERVAL)
            logger.warning("Created new websocket connection")
            time.sleep(0.1)
            response = self._receive_action(message)
            if not self.ws.connected:
                raise RuntimeError(
                    "Websocket connection closed and creation of new connection failed."
                )
        return response
<commit_msg>Removed useless settimeout, as it is never called<commit_after>import binascii
from typing import Union
from typing import List

import torch
import websocket
import time
import logging

import syft as sy
from syft.codes import MSGTYPE
from syft.frameworks.torch.tensors.interpreters import AbstractTensor
from syft.workers import BaseWorker

logger = logging.getLogger(__name__)

TIMEOUT_INTERVAL = 9999999

class WebsocketClientWorker(BaseWorker):
    def __init__(
        self,
        hook,
        host: str,
        port: int,
        id: Union[int, str] = 0,
        is_client_worker: bool = False,
        log_msgs: bool = False,
        verbose: bool = False,
        data: List[Union[torch.Tensor, AbstractTensor]] = None,
    ):
        """A client which will forward all messages to a remote worker running a
        WebsocketServerWorker and receive all responses back from the server.
        """

        # TODO get angry when we have no connection params
        self.port = port
        self.host = host
        self.uri = f"ws://{self.host}:{self.port}"

        # creates the connection with the server which gets held open until the
        # WebsocketClientWorker is garbage collected.
        # Also avoid the server from timing out on the server-side in case of slow clients
        self.ws = websocket.create_connection(self.uri, max_size=None, timeout=TIMEOUT_INTERVAL)

        super().__init__(hook, id, data, is_client_worker, log_msgs, verbose)

    def search(self, *query):
        # Prepare a message requesting the websocket server to search among its objects
        message = (MSGTYPE.SEARCH, query)
        serialized_message = sy.serde.serialize(message)
        # Send the message and return the deserialized response.
        response = self._recv_msg(serialized_message)
        return sy.serde.deserialize(response)

    def _send_msg(self, message: bin, location) -> bin:
        raise RuntimeError(
            "_send_msg should never get called on a ",
            "WebsocketClientWorker. Did you accidentally "
            "make hook.local_worker a WebsocketClientWorker?",
        )

    def _receive_action(self, message: bin) -> bin:
        self.ws.send(str(binascii.hexlify(message)))
        response = binascii.unhexlify(self.ws.recv()[2:-1])
        return response

    def _recv_msg(self, message: bin) -> bin:
        """Forwards a message to the WebsocketServerWorker"""

        response = self._receive_action(message)
        if not self.ws.connected:
            logger.warning("Websocket connection closed (worker: %s)", self.id)
            self.ws.shutdown()
            time.sleep(1)
            # Avoid timing out on the server-side
            self.ws = websocket.create_connection(self.uri, max_size=None, timeout=TIMEOUT_INTERVAL)
            logger.warning("Created new websocket connection")
            time.sleep(0.1)
            response = self._receive_action(message)
            if not self.ws.connected:
                raise RuntimeError(
                    "Websocket connection closed and creation of new connection failed."
                )
        return response
<|endoftext|>


<commit_before>import numpy as np
import cv2
import random
from scipy import misc
from scipy.ndimage import rotate

pixel_buoy=6
alto=58
ancho=34

def new_map(x,y):
	mapa=np.full((x,y,3),0,dtype=np.uint8)
	return mapa

def plot_map(mapa):
	copia=mapa.copy()
	h,w,c=mapa.shape
	x1=int(h/2-(ancho/2))
	y1=int(w/2-(alto/2))
	x2=int(h/2+(ancho/2))
	y2=int(w/2+(alto/2))

	cx=int(h/2)
	cy=int(w/2)
	cv2.rectangle(copia,(x1,y1),(x2,y2),(0,255,0),1,8)
	cv2.line(copia,(cx,cy),(cx,cy-25),(0,0,255),1,8)
	cv2.imshow('mapa',copia)
	cv2.waitKey(0)

def rotate_map(mapa, angle):
	#mapa=misc.imrotate(mapa,angle,interp='nearest')
	mapa = rotate(mapa, angle, mode = 'constant',reshape=False)
	return mapa

def translate_map(mapa,dx,dy):
	cols,rows,ch=mapa.shape
	M=np.float32([[1,0,dx],[0,1,dy]])
	mapa=cv2.warpAffine(mapa,M,(cols,rows))
	return mapa

def add_buoy(mapa,x,y):
	cv2.circle(mapa,(x,y),pixel_buoy,255,-1,8)

def add_lidar(mapa,points):
	for point in points:
		mapa[point[0]][point[1]]=[0,255,0]
	return mapa

#Nuevo mapa
boat_map=new_map(400,400)

#Dibujar boya
add_buoy(boat_map,100,150)
add_buoy(boat_map,100,150)

#Mostrar mapa
plot_map(boat_map)

#Rotar mapa
for angulo in range(0,180,10):
	boat_map=rotate_map(boat_map,5)
	plot_map(boat_map)

#Trasladar mapa 1 en x
for x in range(0,10):
	boat_map=translate_map(boat_map,5,0)
	plot_map(boat_map)

#Trasladar mapa -1 en y
for y in range(0,30):
	boat_map=translate_map(boat_map,0,-5)
	plot_map(boat_map)

<commit_msg>Added bounding boxes to dbscan and add_lidar to map.py<commit_after>import numpy as np
import cv2
import random
from scipy import misc
from scipy.ndimage import rotate

pixel_buoy=6
alto=58
ancho=34

def new_map(x,y):
	mapa=np.full((x,y,3),0,dtype=np.uint8)
	return mapa

def plot_map(mapa):
	copia=mapa.copy()
	h,w,c=mapa.shape
	x1=int(h/2-(ancho/2))
	y1=int(w/2-(alto/2))
	x2=int(h/2+(ancho/2))
	y2=int(w/2+(alto/2))

	cx=int(h/2)
	cy=int(w/2)
	cv2.rectangle(copia,(x1,y1),(x2,y2),(0,255,0),1,8)
	cv2.line(copia,(cx,cy),(cx,cy-25),(0,0,255),1,8)
	cv2.imshow('mapa',copia)
	cv2.waitKey(0)

def rotate_map(mapa, angle):
	#mapa=misc.imrotate(mapa,angle,interp='nearest')
	mapa = rotate(mapa, angle, mode = 'constant',reshape=False)
	return mapa

def translate_map(mapa,dx,dy):
	cols,rows,ch=mapa.shape
	M=np.float32([[1,0,dx],[0,1,dy]])
	mapa=cv2.warpAffine(mapa,M,(cols,rows))
	return mapa

def add_buoy(mapa,x,y):
	cv2.circle(mapa,(x,y),pixel_buoy,255,-1,8)

def add_lidar(mapa,points):
	for point in points:
		mapa[point[0]][point[1]]=[0,255,0]
	return mapa

#Nuevo mapa
boat_map=new_map(400,400)

#Dibujar boya
add_buoy(boat_map,100,150)
add_buoy(boat_map,100,150)

#Mostrar mapa
plot_map(boat_map)

#Rotar mapa
for angulo in range(0,180,10):
	boat_map=rotate_map(boat_map,5)
	plot_map(boat_map)

#Trasladar mapa 1 en x
for x in range(0,10):
	boat_map=translate_map(boat_map,5,0)
	plot_map(boat_map)

#Trasladar mapa -1 en y
for y in range(0,30):
	boat_map=translate_map(boat_map,0,-5)
	plot_map(boat_map)

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

import os
import platform
import sublime
import sublime_plugin

from os.path import splitext
from subprocess import PIPE, Popen

#
# Monkey patch `sublime.Region` so it can be iterable:
sublime.Region.totuple = lambda self: (self.a, self.b)
sublime.Region.__iter__ = lambda self: self.totuple().__iter__()

PLUGIN_NAME = 'JsPrettier'
PLUGIN_PATH = os.path.join(sublime.packages_path(),
                           os.path.dirname(os.path.realpath(__file__)))
SETTINGS_FILE = '{0}.sublime-settings'.format(PLUGIN_NAME)

PRETTIER_OPTION_CLI_MAP = [
    {'option': 'printWidth', 'cli': '--print-width'},
    {'option': 'tabWidth', 'cli': '--tab-width'},
    {'option': 'singleQuote', 'cli': '--single-quote'},
    {'option': 'trailingComma', 'cli': '--trailing-comma'},
    {'option': 'bracketSpacing', 'cli': '--bracket-spacing'},
    {'option': 'parser', 'cli': '--parser'}
]

class JsPrettierCommand(sublime_plugin.TextCommand):
    _error_message = None

    def run(self, edit):
        view = self.view

        if view.file_name() is None:
            return sublime.error_message(
                '%s Error\n\n'
                'The current View must be Saved\n'
                'before running JsPrettier.' % PLUGIN_NAME)

        prettier_cli_path = self.prettier_cli_path
        if prettier_cli_path is None:
            return sublime.error_message(
                "{0} - The path to prettier cli could not be "
                "found! Please ensure the path to prettier is "
                "set in your PATH environment variable ".format(PLUGIN_NAME))

        prettier_options = self.prettier_options
        prettier_options['tabWidth'] = self.tab_size

        #
        # Format entire file:
        if not self.has_selection:
            region = sublime.Region(0, view.size())
            source = view.substr(region)

            transformed = self.run_prettier(source, prettier_cli_path,
                                            prettier_options)
            if self.has_errors:
                self.print_error_console()
                return self.show_status_bar_error()

            if transformed and transformed == source:
                sublime.set_timeout(lambda: sublime.status_message(
                    '{0}: File already formatted.'.format(PLUGIN_NAME)), 0)
            else:
                view.replace(edit, region, transformed)
                sublime.set_timeout(lambda: sublime.status_message(
                    '{0}: File formatted.'.format(PLUGIN_NAME)), 0)
            return

        #
        # Format each selection:
        for region in view.sel():
            if region.empty():
                continue

            source = view.substr(region)
            transformed = self.run_prettier(source, prettier_cli_path,
                                            prettier_options)
            if self.has_errors:
                self.print_error_console()
                return self.show_status_bar_error()

            if transformed and transformed == source:
                sublime.set_timeout(lambda: sublime.status_message(
                    '{0}: Selection(s) already formatted.'.format(
                        PLUGIN_NAME)), 0)
            else:
                view.replace(edit, region, transformed)
                sublime.set_timeout(lambda: sublime.status_message(
                    '{0}: Selection(s) formatted.'.format(PLUGIN_NAME)), 0)

    def run_prettier(self, source, prettier_cli_path, prettier_options):
        self._error_message = None
        prettier_cli_opts = self.parse_prettier_option_cli_map(prettier_options)
        cmd = [prettier_cli_path] + prettier_cli_opts + ['--stdin']
        try:
            proc = Popen(cmd, stdin=PIPE, stderr=PIPE, stdout=PIPE,
                         env=self.os_env, shell=self.is_windows())
            stdout, stderr = proc.communicate(input=source.encode('utf-8'))
            if stderr or proc.returncode != 0:
                self.error_message = stderr.decode('utf-8')
                return source
            else:
                return stdout.decode('utf-8')
        except OSError:
            raise Exception(
                "{0} - path to prettier not found! Please ensure "
                "the path to prettier is set in your $PATH env "
                "variable.".format(PLUGIN_NAME))

    def show_status_bar_error(self):
        sublime.set_timeout(lambda: sublime.status_message(
            '{0}: Format failed! Open the console window to '
            'view error details.'.format(PLUGIN_NAME)), 0)

    def print_error_console(self):
        print("\n------------------\n {0} Error \n------------------\n\n"
              "{1}".format(PLUGIN_NAME, self.error_message))

    @property
    def has_errors(self):
        if not self._error_message:
            return False
        return True

    @property
    def error_message(self):
        return self._error_message

    @error_message.setter
    def error_message(self, message=None):
        self._error_message = message

    @property
    def is_js(self):
        return self.view.scope_name(0).startswith('source.js')

    @property
    def os_env(self):
        env = None
        if not self.is_windows():
            env = os.environ.copy()
            env['PATH'] += ':/usr/local/bin'
        return env

    @property
    def prettier_cli_path(self):
        prettier_path = self.sublime_settings.get('prettier_cli_path', '')
        if self.is_none_or_empty(prettier_path):
            return self.which('prettier')
        return self.which(prettier_path)

    @property
    def sublime_settings(self):
        settings = self.view.settings().get(PLUGIN_NAME)
        if settings is None:
            settings = sublime.load_settings(SETTINGS_FILE)
        return settings

    @property
    def prettier_options(self):
        return self.sublime_settings.get('prettier_options')

    @property
    def tab_size(self):
        return int(self.view.settings().get('tab_size', 2))

    @property
    def has_selection(self):
        for sel in self.view.sel():
            start, end = sel
            if start != end:
                return True
        return False

    def which(self, executable, path=None):
        if not self.is_none_or_empty(executable):
            if os.path.isfile(executable):
                return executable

        if self.is_none_or_empty(path):
            path = os.environ['PATH']
            if not self.is_windows():
                path += ':/usr/local/bin'

        paths = path.split(os.pathsep)
        if not os.path.isfile(executable):
            for p in paths:
                f = os.path.join(p, executable)
                if os.path.isfile(f):
                    return f
            return None
        else:
            return executable

    @staticmethod
    def is_none_or_empty(val):
        if val is None:
            return True
        if type(val) == str:
            val = val.strip()
        if not val:
            return True
        return False

    @staticmethod
    def is_mac_os():
        return platform.system() == 'Darwin'

    @staticmethod
    def is_windows():
        return platform.system() == 'Windows' or os.name == 'nt'

    @staticmethod
    def parse_prettier_option_cli_map(prettier_options):
        prettier_cli_args = []
        for mapping in PRETTIER_OPTION_CLI_MAP:
            option = prettier_options[mapping['option']]
            if option:
                prettier_cli_args.append(mapping['cli'])
                if not isinstance(option, bool):
                    prettier_cli_args.append(str(option))
        return prettier_cli_args

class CommandOnSave(sublime_plugin.EventListener):
    def on_pre_save(self, view):
        ext = splitext(view.file_name())[1][1:]
        if self.is_enabled(view) and ext == 'js':
            view.run_command("js_prettier")

    def is_enabled(self, view):
        return self.get_settings(view).get('auto_format_on_save', False)

    @staticmethod
    def get_settings(view):
        settings = view.settings().get(PLUGIN_NAME)
        if settings is None:
            settings = sublime.load_settings(SETTINGS_FILE)
        return settings
<commit_msg>Check "/usr/local/bin" path existence/dupe before appending to env.<commit_after># -*- coding: utf-8 -*-

import os
import platform
import sublime
import sublime_plugin

from os.path import splitext
from subprocess import PIPE, Popen

#
# Monkey patch `sublime.Region` so it can be iterable:
sublime.Region.totuple = lambda self: (self.a, self.b)
sublime.Region.__iter__ = lambda self: self.totuple().__iter__()

PLUGIN_NAME = 'JsPrettier'
PLUGIN_PATH = os.path.join(sublime.packages_path(),
                           os.path.dirname(os.path.realpath(__file__)))
SETTINGS_FILE = '{0}.sublime-settings'.format(PLUGIN_NAME)

PRETTIER_OPTION_CLI_MAP = [
    {'option': 'printWidth', 'cli': '--print-width'},
    {'option': 'tabWidth', 'cli': '--tab-width'},
    {'option': 'singleQuote', 'cli': '--single-quote'},
    {'option': 'trailingComma', 'cli': '--trailing-comma'},
    {'option': 'bracketSpacing', 'cli': '--bracket-spacing'},
    {'option': 'parser', 'cli': '--parser'}
]

class JsPrettierCommand(sublime_plugin.TextCommand):
    _error_message = None

    def run(self, edit):
        view = self.view

        if view.file_name() is None:
            return sublime.error_message(
                '%s Error\n\n'
                'The current View must be Saved\n'
                'before running JsPrettier.' % PLUGIN_NAME)

        prettier_cli_path = self.prettier_cli_path
        if prettier_cli_path is None:
            return sublime.error_message(
                "{0} - The path to prettier cli could not be "
                "found! Please ensure the path to prettier is "
                "set in your PATH environment variable ".format(PLUGIN_NAME))

        prettier_options = self.prettier_options
        prettier_options['tabWidth'] = self.tab_size

        #
        # Format entire file:
        if not self.has_selection:
            region = sublime.Region(0, view.size())
            source = view.substr(region)

            transformed = self.run_prettier(source, prettier_cli_path,
                                            prettier_options)
            if self.has_errors:
                self.print_error_console()
                return self.show_status_bar_error()

            if transformed and transformed == source:
                sublime.set_timeout(lambda: sublime.status_message(
                    '{0}: File already formatted.'.format(PLUGIN_NAME)), 0)
            else:
                view.replace(edit, region, transformed)
                sublime.set_timeout(lambda: sublime.status_message(
                    '{0}: File formatted.'.format(PLUGIN_NAME)), 0)
            return

        #
        # Format each selection:
        for region in view.sel():
            if region.empty():
                continue

            source = view.substr(region)
            transformed = self.run_prettier(source, prettier_cli_path,
                                            prettier_options)
            if self.has_errors:
                self.print_error_console()
                return self.show_status_bar_error()

            if transformed and transformed == source:
                sublime.set_timeout(lambda: sublime.status_message(
                    '{0}: Selection(s) already formatted.'.format(
                        PLUGIN_NAME)), 0)
            else:
                view.replace(edit, region, transformed)
                sublime.set_timeout(lambda: sublime.status_message(
                    '{0}: Selection(s) formatted.'.format(PLUGIN_NAME)), 0)

    def run_prettier(self, source, prettier_cli_path, prettier_options):
        self._error_message = None

        prettier_cli_opts = self.parse_prettier_option_cli_map(prettier_options)
        cmd = [prettier_cli_path] + prettier_cli_opts + ['--stdin']
        try:
            proc = Popen(cmd, stdin=PIPE, stderr=PIPE, stdout=PIPE,
                         env=self.proc_env, shell=self.is_windows())
            stdout, stderr = proc.communicate(input=source.encode('utf-8'))
            if stderr or proc.returncode != 0:
                self.error_message = stderr.decode('utf-8')
                return source
            else:
                return stdout.decode('utf-8')
        except OSError:
            raise Exception(
                "{0} - path to prettier not found! Please ensure "
                "the path to prettier is set in your $PATH env "
                "variable.".format(PLUGIN_NAME))

    def show_status_bar_error(self):
        sublime.set_timeout(lambda: sublime.status_message(
            '{0}: Format failed! Open the console window to '
            'view error details.'.format(PLUGIN_NAME)), 0)

    def print_error_console(self):
        print("\n------------------\n {0} Error \n------------------\n\n"
              "{1}".format(PLUGIN_NAME, self.error_message))

    @property
    def has_errors(self):
        if not self._error_message:
            return False
        return True

    @property
    def error_message(self):
        return self._error_message

    @error_message.setter
    def error_message(self, message=None):
        self._error_message = message

    @property
    def is_js(self):
        return self.view.scope_name(0).startswith('source.js')

    @property
    def proc_env(self):
        env = None
        if not self.is_windows():
            env = os.environ.copy()
            usr_path = ':/usr/local/bin'
            if not self.path_exists_in_env_path(usr_path) \
                    and self.path_exists(usr_path):
                env['PATH'] += usr_path
        return env

    @property
    def prettier_cli_path(self):
        prettier_path = self.sublime_settings.get('prettier_cli_path', '')
        if self.is_none_or_empty(prettier_path):
            return self.which('prettier')
        return self.which(prettier_path)

    @property
    def sublime_settings(self):
        settings = self.view.settings().get(PLUGIN_NAME)
        if settings is None:
            settings = sublime.load_settings(SETTINGS_FILE)
        return settings

    @property
    def prettier_options(self):
        return self.sublime_settings.get('prettier_options')

    @property
    def tab_size(self):
        return int(self.view.settings().get('tab_size', 2))

    @property
    def has_selection(self):
        for sel in self.view.sel():
            start, end = sel
            if start != end:
                return True
        return False

    @staticmethod
    def path_exists_in_env_path(find_path, env_path=None):
        if not find_path:
            return False
        if not env_path:
            env_path = os.environ['PATH']
        find_path = str.replace(find_path, os.pathsep, '')
        paths = env_path.split(os.pathsep)
        for p in paths:
            if p == find_path:
                return True
        return False

    @staticmethod
    def path_exists(path):
        if not path:
            return False
        if os.path.exists(str.replace(path, os.pathsep, '')):
            return True
        return False

    def which(self, executable, path=None):
        if not self.is_none_or_empty(executable):
            if os.path.isfile(executable):
                return executable

        if self.is_none_or_empty(path):
            path = os.environ['PATH']
            if not self.is_windows():
                usr_path = ':/usr/local/bin'
                if not self.path_exists_in_env_path(usr_path, path) \
                        and self.path_exists(usr_path):
                    path += usr_path

        paths = path.split(os.pathsep)
        if not os.path.isfile(executable):
            for p in paths:
                f = os.path.join(p, executable)
                if os.path.isfile(f):
                    return f
            return None
        else:
            return executable

    @staticmethod
    def is_none_or_empty(val):
        if val is None:
            return True
        if type(val) == str:
            val = val.strip()
        if not val:
            return True
        return False

    @staticmethod
    def is_mac_os():
        return platform.system() == 'Darwin'

    @staticmethod
    def is_windows():
        return platform.system() == 'Windows' or os.name == 'nt'

    @staticmethod
    def parse_prettier_option_cli_map(prettier_options):
        prettier_cli_args = []
        for mapping in PRETTIER_OPTION_CLI_MAP:
            option = prettier_options[mapping['option']]
            if option:
                prettier_cli_args.append(mapping['cli'])
                if not isinstance(option, bool):
                    prettier_cli_args.append(str(option))
        return prettier_cli_args

class CommandOnSave(sublime_plugin.EventListener):
    def on_pre_save(self, view):
        ext = splitext(view.file_name())[1][1:]
        if self.is_enabled(view) and ext == 'js':
            view.run_command("js_prettier")

    def is_enabled(self, view):
        return self.get_settings(view).get('auto_format_on_save', False)

    @staticmethod
    def get_settings(view):
        settings = view.settings().get(PLUGIN_NAME)
        if settings is None:
            settings = sublime.load_settings(SETTINGS_FILE)
        return settings
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

import os
import json
import platform
import sublime
import sublime_plugin

from os.path import splitext
from subprocess import PIPE, Popen

#
# monkey patch `Region` to be iterable:
sublime.Region.totuple = lambda self: (self.a, self.b)
sublime.Region.__iter__ = lambda self: self.totuple().__iter__()

PLUGIN_NAME = 'JsPrettier'
PLUGIN_PATH = os.path.join(sublime.packages_path(), os.path.dirname(os.path.realpath(__file__)))
SETTINGS_FILE = '{0}.sublime-settings'.format(PLUGIN_NAME)
JS_PRETTIER_FILE = '{0}.js'.format(PLUGIN_NAME.lower())
JS_PRETTIER_PATH = os.path.join(PLUGIN_PATH, JS_PRETTIER_FILE)

PRETTIER_OPTION_CLI_MAP = [
    {'option': 'printWidth', 'cli': '--print-width'},
    {'option': 'tabWidth', 'cli': '--tab-width'},
    {'option': 'useFlowParser', 'cli': '--flow-parser'},
    {'option': 'singleQuote', 'cli': '--single-quote'},
    {'option': 'trailingComma', 'cli': '--trailing-comma'},
    {'option': 'bracketSpacing', 'cli': '--bracket-spacing'},
]

class JsPrettierCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        if self.view.file_name() is None:
            return sublime.error_message(
                '%s Error\n\n'
                'The current View must be Saved\n'
                'before running JsPrettier.' % PLUGIN_NAME)

        prettier_options = self.get_prettier_options()
        prettier_options['tabWidth'] = self.get_tab_size()

        #
        # format entire file:
        if not self.has_selection():
            region = sublime.Region(0, self.view.size())
            source = self.view.substr(region)

            if not self.is_prettier_global_cli_installed():
                transformed = self.prettier_local(source, prettier_options)
            else:
                transformed = self.prettier_global_cli(source, prettier_options)

            if transformed and transformed == source:
                sublime.set_timeout(lambda: sublime.status_message(
                    '{0}: File already formatted.'.format(PLUGIN_NAME)), 0)
            else:
                self.view.replace(edit, region, transformed)
                sublime.set_timeout(lambda: sublime.status_message(
                    '{0}: File formatted.'.format(PLUGIN_NAME)), 0)
            return

        #
        # format each selection:
        for region in self.view.sel():
            if region.empty():
                continue

            source = self.view.substr(region)

            if not self.is_prettier_global_cli_installed():
                transformed = self.prettier_local(source, prettier_options)
            else:
                transformed = self.prettier_global_cli(source, prettier_options)

            if transformed and transformed == source:
                sublime.set_timeout(lambda: sublime.status_message(
                    '{0}: Selection(s) already formatted.'.format(PLUGIN_NAME)), 0)
            else:
                self.view.replace(edit, region, transformed)
                sublime.set_timeout(lambda: sublime.status_message(
                    '{0}: Selection(s) formatted.'.format(PLUGIN_NAME)), 0)

    def prettier_local(self, source, prettier_options):
        prettier_options = json.dumps(prettier_options)
        cwd = os.path.dirname(self.view.file_name())

        try:
            proc = Popen(['node', JS_PRETTIER_PATH, prettier_options, cwd],
                         stdout=PIPE, stdin=PIPE, stderr=PIPE,
                         env=self.get_env(), shell=self.is_windows())
        except OSError:
            raise Exception(
                "{0} - path to node.js not found! Please ensure "
                "the path to node.js is set in your $PATH env variable "
                "by running `node -v` from the command-line.".format(PLUGIN_NAME))

        stdout, stderr = proc.communicate(input=source.encode('utf-8'))
        if stdout:
            return stdout.decode('utf-8')
        else:
            return sublime.error_message("%s Error\n\n%s" % (PLUGIN_NAME, stderr.decode('utf-8')))

    def prettier_global_cli(self, source, prettier_options):
        prettier_cli_opts = self.parse_prettier_option_cli_map(prettier_options)

        cmd = [self.get_prettier_global_cli_path()] + prettier_cli_opts + ['--stdin']
        proc = Popen(cmd, stdin=PIPE, stderr=PIPE, stdout=PIPE, env=self.get_env(), shell=self.is_windows())
        stdout, stderr = proc.communicate(input=source.encode('utf-8'))
        if stderr or proc.returncode != 0:
            return sublime.error_message("%s Error\n\n%s" % (PLUGIN_NAME, stderr.decode('utf-8')))
        else:
            return stdout.decode('utf-8')

    def is_js(self):
        return self.view.scope_name(0).startswith('source.js')

    def get_env(self):
        env = None
        if self.is_osx():
            env = os.environ.copy()
            env['PATH'] += self.get_node_path()
        return env

    def get_node_path(self):
        return self.get_settings().get('node_path')

    def is_prettier_global_cli_installed(self):
        if which('prettier', self.get_node_path()) is None:
            return False
        return True

    def get_prettier_global_cli_path(self):
        return which('prettier', self.get_node_path())

    def get_settings(self):
        settings = self.view.settings().get(PLUGIN_NAME)
        if settings is None:
            settings = sublime.load_settings(SETTINGS_FILE)
        return settings

    def get_prettier_options(self):
        return self.get_settings().get('prettier_options')

    def get_tab_size(self):
        return int(self.view.settings().get('tab_size', 2))

    def has_selection(self):
        for sel in self.view.sel():
            start, end = sel
            if start != end:
                return True
        return False

    @staticmethod
    def is_osx():
        return platform.system() == 'Darwin'

    @staticmethod
    def is_windows():
        return platform.system() == 'Windows'

    @staticmethod
    def parse_prettier_option_cli_map(prettier_options):
        prettier_cli_args = []
        for mapping in PRETTIER_OPTION_CLI_MAP:
            option = prettier_options[mapping['option']]
            if option:
                prettier_cli_args.append(mapping['cli'])
                if not isinstance(option, bool):
                    prettier_cli_args.append(str(option))
        return prettier_cli_args

class CommandOnSave(sublime_plugin.EventListener):
    def on_pre_save(self, view):
        ext = splitext(view.file_name())[1][1:]
        if self.is_enabled(view) and ext == 'js':
            view.run_command("js_prettier")

    def is_enabled(self, view):
        return self.get_settings(view).get('auto_format_on_save', False)

    @staticmethod
    def get_settings(view):
        settings = view.settings().get(PLUGIN_NAME)
        if settings is None:
            settings = sublime.load_settings(SETTINGS_FILE)
        return settings

def which(executable, path=None):
    if path is None:
        path = os.environ['PATH']
    paths = path.split(os.pathsep)

    if not os.path.isfile(executable):
        for p in paths:
            f = os.path.join(p, executable)
            if os.path.isfile(f):
                return f
        return None
    else:
        return executable
<commit_msg>Normalize method names.<commit_after># -*- coding: utf-8 -*-

import os
import json
import platform
import sublime
import sublime_plugin

from os.path import splitext
from subprocess import PIPE, Popen

#
# Monkey patch `sublime.Region` so it can be iterable:
sublime.Region.totuple = lambda self: (self.a, self.b)
sublime.Region.__iter__ = lambda self: self.totuple().__iter__()

PLUGIN_NAME = 'JsPrettier'
PLUGIN_PATH = os.path.join(sublime.packages_path(), os.path.dirname(os.path.realpath(__file__)))
SETTINGS_FILE = '{0}.sublime-settings'.format(PLUGIN_NAME)
JS_PRETTIER_FILE = '{0}.js'.format(PLUGIN_NAME.lower())
JS_PRETTIER_PATH = os.path.join(PLUGIN_PATH, JS_PRETTIER_FILE)

PRETTIER_OPTION_CLI_MAP = [
    {'option': 'printWidth', 'cli': '--print-width'},
    {'option': 'tabWidth', 'cli': '--tab-width'},
    {'option': 'useFlowParser', 'cli': '--flow-parser'},
    {'option': 'singleQuote', 'cli': '--single-quote'},
    {'option': 'trailingComma', 'cli': '--trailing-comma'},
    {'option': 'bracketSpacing', 'cli': '--bracket-spacing'},
]

class JsPrettierCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        if self.view.file_name() is None:
            return sublime.error_message(
                '%s Error\n\n'
                'The current View must be Saved\n'
                'before running JsPrettier.' % PLUGIN_NAME)

        prettier_options = self.get_prettier_options()
        prettier_options['tabWidth'] = self.get_tab_size()

        #
        # Format entire file:
        if not self.has_selection():
            region = sublime.Region(0, self.view.size())
            source = self.view.substr(region)

            if not self.is_global_prettier_installed():
                transformed = self.run_local_prettier(source, prettier_options)
            else:
                transformed = self.run_global_prettier(source, prettier_options)

            if transformed and transformed == source:
                sublime.set_timeout(lambda: sublime.status_message(
                    '{0}: File already formatted.'.format(PLUGIN_NAME)), 0)
            else:
                self.view.replace(edit, region, transformed)
                sublime.set_timeout(lambda: sublime.status_message(
                    '{0}: File formatted.'.format(PLUGIN_NAME)), 0)
            return

        #
        # Format each selection:
        for region in self.view.sel():
            if region.empty():
                continue

            source = self.view.substr(region)

            if not self.is_global_prettier_installed():
                transformed = self.run_local_prettier(source, prettier_options)
            else:
                transformed = self.run_global_prettier(source, prettier_options)

            if transformed and transformed == source:
                sublime.set_timeout(lambda: sublime.status_message(
                    '{0}: Selection(s) already formatted.'.format(PLUGIN_NAME)), 0)
            else:
                self.view.replace(edit, region, transformed)
                sublime.set_timeout(lambda: sublime.status_message(
                    '{0}: Selection(s) formatted.'.format(PLUGIN_NAME)), 0)

    def run_local_prettier(self, source, prettier_options):
        prettier_options = json.dumps(prettier_options)
        cwd = os.path.dirname(self.view.file_name())

        try:
            proc = Popen(['node', JS_PRETTIER_PATH, prettier_options, cwd],
                         stdout=PIPE, stdin=PIPE, stderr=PIPE,
                         env=self.get_env(), shell=self.is_windows())
        except OSError:
            raise Exception(
                "{0} - path to node.js not found! Please ensure "
                "the path to node.js is set in your $PATH env variable "
                "by running `node -v` from the command-line.".format(PLUGIN_NAME))

        stdout, stderr = proc.communicate(input=source.encode('utf-8'))
        if stdout:
            return stdout.decode('utf-8')
        else:
            return sublime.error_message("%s Error\n\n%s" % (PLUGIN_NAME, stderr.decode('utf-8')))

    def run_global_prettier(self, source, prettier_options):
        prettier_cli_opts = self.parse_prettier_option_cli_map(prettier_options)

        cmd = [self.get_global_prettier_path()] + prettier_cli_opts + ['--stdin']
        proc = Popen(cmd, stdin=PIPE, stderr=PIPE, stdout=PIPE, env=self.get_env(), shell=self.is_windows())
        stdout, stderr = proc.communicate(input=source.encode('utf-8'))
        if stderr or proc.returncode != 0:
            return sublime.error_message("%s Error\n\n%s" % (PLUGIN_NAME, stderr.decode('utf-8')))
        else:
            return stdout.decode('utf-8')

    def is_js(self):
        return self.view.scope_name(0).startswith('source.js')

    def get_env(self):
        env = None
        if self.is_mac_os():
            env = os.environ.copy()
            env['PATH'] += self.get_node_path()
        return env

    def get_node_path(self):
        return self.get_settings().get('node_path')

    def is_global_prettier_installed(self):
        if which('prettier', self.get_node_path()) is None:
            return False
        return True

    def get_global_prettier_path(self):
        return which('prettier', self.get_node_path())

    def get_settings(self):
        settings = self.view.settings().get(PLUGIN_NAME)
        if settings is None:
            settings = sublime.load_settings(SETTINGS_FILE)
        return settings

    def get_prettier_options(self):
        return self.get_settings().get('prettier_options')

    def get_tab_size(self):
        return int(self.view.settings().get('tab_size', 2))

    def has_selection(self):
        for sel in self.view.sel():
            start, end = sel
            if start != end:
                return True
        return False

    @staticmethod
    def is_mac_os():
        return platform.system() == 'Darwin'

    @staticmethod
    def is_windows():
        return platform.system() == 'Windows'

    @staticmethod
    def parse_prettier_option_cli_map(prettier_options):
        prettier_cli_args = []
        for mapping in PRETTIER_OPTION_CLI_MAP:
            option = prettier_options[mapping['option']]
            if option:
                prettier_cli_args.append(mapping['cli'])
                if not isinstance(option, bool):
                    prettier_cli_args.append(str(option))
        return prettier_cli_args

class CommandOnSave(sublime_plugin.EventListener):
    def on_pre_save(self, view):
        ext = splitext(view.file_name())[1][1:]
        if self.is_enabled(view) and ext == 'js':
            view.run_command("js_prettier")

    def is_enabled(self, view):
        return self.get_settings(view).get('auto_format_on_save', False)

    @staticmethod
    def get_settings(view):
        settings = view.settings().get(PLUGIN_NAME)
        if settings is None:
            settings = sublime.load_settings(SETTINGS_FILE)
        return settings

def which(executable, path=None):
    if path is None:
        path = os.environ['PATH']
    paths = path.split(os.pathsep)

    if not os.path.isfile(executable):
        for p in paths:
            f = os.path.join(p, executable)
            if os.path.isfile(f):
                return f
        return None
    else:
        return executable
<|endoftext|>


<commit_before>import httplib, urllib, base64, json
from openpyxl import Workbook
from openpyxl import load_workbook

# This program reads controls and use Azure Key phrases API to extract key phrases
# Name of the file with cloud security standard
cfname = 'NIST 800-53-controls.xlsx'
# Name of the new file with keyphrases
ofile = 'US_NIST_800-53-r4.xlsx'
# File with subscription key in json format
keyfile = 'Credential.json'
with open(keyfile,'r') as k:
    key = json.load(k)

wb = load_workbook(filename = cfname)
ws = wb['Sheet1']
headers = {
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': key['key'],
}
params = urllib.urlencode({

})

# Assume column B is id, F is text and I is for key phrases
row = 1
while not(ws['B'+str(row)].value is None and ws['F'+str(row)].value is None):
    row = row + 1
    id = ws['B'+str(row)].value
    text = ws['F'+str(row)].value
    body = json.dumps({
        "documents" : [
            {
                "id": id,
                "text": text,
            }
        ]
    })
    try:
        conn = httplib.HTTPSConnection('westus.api.cognitive.microsoft.com')
        conn.request("POST", "/text/analytics/v2.0/keyPhrases?%s" % params, body, headers)
        response = conn.getresponse()
        data = json.loads(response.read())
        documents = data['documents']
        print(str(row)+": "+documents[0]['id'])
        ws['I'+str(row)].value = ', '.join(documents[0]['keyPhrases'])
        #print(', '.join(documents[0]['keyPhrases']))
        conn.close()
    except Exception as e:
        print(str(row)+": "+e.message+"\n"),response.status, response.reason, response.msg

# Save final output file
wb.save(ofile)<commit_msg>Updated keyphrases API to use Azure Table and update entities with KeyPhrases.<commit_after>import httplib, urllib, base64, json
from openpyxl import Workbook
from openpyxl import load_workbook
from azure.storage.table import TableService, Entity

# This program reads controls from Azure Table and use Azure KeyPhrases API to extract key phrases
# File with KeyPhrase API subscription key in json format
keyfile = 'Credential.json'
with open(keyfile,'r') as k:
    key = json.load(k)

# File with storage account (Azure Table) info in json format
keyfile = 'Table.json'
with open(keyfile,'r') as k:
    t = json.load(k)
    acc = t['acc']
    tkey = t['key']
    tab = t['tab']

table_service = TableService(account_name=acc, account_key=tkey)

# KeyPhrases API headers
headers = {
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': key['key'],
}
params = urllib.urlencode({
})

controls = table_service.query_entities('wwcccs', filter=None)

for control in controls:
    id = control.PartitionKey + '-' + control.RowKey
    text = control.ControlDescription
    body = json.dumps({
        "documents" : [
            {
                "id": id,
                "text": text,
            }
        ]
    })
    try:
        conn = httplib.HTTPSConnection('westus.api.cognitive.microsoft.com')
        conn.request("POST", "/text/analytics/v2.0/keyPhrases?%s" % params, body, headers)
        response = conn.getresponse()
        data = json.loads(response.read())
        documents = data['documents']
        kp = ", ".join(documents[0]['keyPhrases'])
        print(documents[0]['id']+": "+kp)
        updated_control = {'PartitionKey': control.PartitionKey, 'RowKey': control.RowKey, 'KeyPhrases': kp}
        table_service.insert_or_merge_entity('wwcccs', updated_control)
        conn.close()
    except Exception as e:
        print(id+": "+e.message+"\n"),response.status, response.reason, response.msg
<|endoftext|>


<commit_before>"""
### .L68 to .X68
###
### Generates Easy68k .X68 source code file from .L68
###
### Handles errors and symbols on left of source code.
###
### Put .L68 file in same directory as this file. Run. Outputs .X68 file in same directory.

### Usage:

python L68-TO-X68.py "INPUT FILE.L68"

(Must use quotes if spaces exist in file name)

### Author:

Zach Graham
"""

import os, sys, re

class L68ToX68:

    NORMAL_START_LINE = 0
    NORMAL_START_LINE_INDEX = 40
    ERROR_START_LINE = 12
    ERROR_START_LINE_INDEX = 80

    def __init__(self):
        if len(sys.argv) is not 2:
            self.print_error_message()
            exit()

        file_path = os.path.dirname(os.path.abspath(__file__)) + "/" + sys.argv[1]

        if not os.path.isfile(file_path):
            self.print_error_message()

        f_in = open(file_path, "r+")
        f_in_list = f_in.readlines()
        f_in_string = ''.join(str(e) for e in f_in_list)

        if re.search(r'(?i)line\s*\d*\s*(ERROR|WARNING)\s*:', f_in_string):
            output = self.handle_file(self.ERROR_START_LINE, f_in_list, True)
        else:
            output = self.handle_file(self.NORMAL_START_LINE, f_in_list, False)

        self.write_output_to_file(file_path, output)
        exit()

    def handle_file(self, start_line, f_in, is_error_file):
        output = ""

        if is_error_file:
            start_index = self.ERROR_START_LINE_INDEX
        else:
            start_index = self.NORMAL_START_LINE_INDEX

        for i in xrange(start_line, len(f_in)):
            trimmed_line = self.get_trimmed_line(f_in[i], start_index)
            if re.match(r'\s*END\s*START\s*', trimmed_line):
                output += trimmed_line
                break
            output += trimmed_line

        return output

    def get_trimmed_line(self, line, start_index):

        if re.search(r'v\d{0,2}.\d{0,2}.\d{0,2}', line):
            return ""

        output = ""
        for x in xrange(start_index, len(line)):
            output += line[x]

        return output

    def print_error_message(self):
        print "Must input a file name within quotes\ne.g. python l68-L68-TO-X68.py \"FILE NAME.L68\""

    def write_output_to_file(self, file_path, output):
        f_out_path = file_path.replace(".l68", "").replace(".L68", "") + ".X68"
        f_out = open(f_out_path, "w")
        f_out.write(output)
        f_out.close()
        print "Done! output file is at: " + f_out_path

start = L68ToX68()
<commit_msg>Add exit() if input file does not exist<commit_after>"""
### .L68 to .X68
###
### Generates Easy68k .X68 source code file from .L68
###
### Handles errors and symbols on left of source code.
###
### Put .L68 file in same directory as this file. Run. Outputs .X68 file in same directory.

### Usage:

python L68-TO-X68.py "INPUT FILE.L68"

(Must use quotes if spaces exist in file name)

### Author:

Zach Graham
"""

import os, sys, re

class L68ToX68:

    NORMAL_START_LINE = 0
    NORMAL_START_LINE_INDEX = 40
    ERROR_START_LINE = 12
    ERROR_START_LINE_INDEX = 80

    def __init__(self):
        if len(sys.argv) is not 2:
            self.print_error_message()
            exit()

        file_path = os.path.dirname(os.path.abspath(__file__)) + "/" + sys.argv[1]

        if not os.path.isfile(file_path):
            self.print_error_message()
            exit()

        f_in = open(file_path, "r+")
        f_in_list = f_in.readlines()
        f_in_string = ''.join(str(e) for e in f_in_list)

        if re.search(r'(?i)line\s*\d*\s*(ERROR|WARNING)\s*:', f_in_string):
            output = self.handle_file(self.ERROR_START_LINE, f_in_list, True)
        else:
            output = self.handle_file(self.NORMAL_START_LINE, f_in_list, False)

        self.write_output_to_file(file_path, output)
        exit()

    def handle_file(self, start_line, f_in, is_error_file):
        output = ""

        if is_error_file:
            start_index = self.ERROR_START_LINE_INDEX
        else:
            start_index = self.NORMAL_START_LINE_INDEX

        for i in xrange(start_line, len(f_in)):
            trimmed_line = self.get_trimmed_line(f_in[i], start_index)
            if re.match(r'\s*END\s*START\s*', trimmed_line):
                output += trimmed_line
                break
            output += trimmed_line

        return output

    def get_trimmed_line(self, line, start_index):

        if re.search(r'v\d{0,2}.\d{0,2}.\d{0,2}', line):
            return ""

        output = ""
        for x in xrange(start_index, len(line)):
            output += line[x]

        return output

    def print_error_message(self):
        print "Must input a file name within quotes\ne.g. python l68-L68-TO-X68.py \"FILE NAME.L68\""

    def write_output_to_file(self, file_path, output):
        f_out_path = file_path.replace(".l68", "").replace(".L68", "") + ".X68"
        f_out = open(f_out_path, "w")
        f_out.write(output)
        f_out.close()
        print "Done! output file is at: " + f_out_path

start = L68ToX68()
<|endoftext|>


<commit_before>'''
@KaiyiZhang Github
'''
import sys
import urllib2
import getopt
import time

target = ''
depth = 6
file = 'robots.txt'

html = ''
prefix = ''
url = ''
keyword = 'User-agent'
force = False

def usage():
		print "LFI.Tester.py Help:"
		print "Usage: LFI.TESTER.py -t [-d] [-f] [-k]"
		print "	-t,--target The test url"
		print "	-d,--depth 	The depth for test (Default is 6)"
		print "	-f,--file The File include  (Default is robots.txt)"
		print "	-k,--keyword	the keyword for vuln check (Default is User-agent)"
		print "	-n,--no-break	no break while the vuln finded"

def writefile(url):
		f = open('out.txt','a')
		t = time.strftime('%Y-%m-%d %X ',time.localtime())
		f.writelines(t + url + '\n')

try:
		if len(sys.argv) < 2:
				usage()
				sys.exit()
		opts,args = getopt.getopt(sys.argv[1:],"ht:d:f:k:n",["help","target=","depth=","file=","keyword=","no-break"])
		for opt, arg in opts:
			if opt in("-h","--help"):
				usage()
				sys.exit()
			if opt in("-t","--target"):
				target = arg
				if not target.startswith('http://', 0, 7):
						target = 'http://' + target
			if opt in("-d","--depth"):
				depth = int(arg)
				if depth < 1:
					usage()
					sys.exit()
			if opt in("-f","--file"):
				file = arg
				if file.startswith('/',0,1):
					file =file[1:]
			if opt in("-k","--keyword"):
				keyword = arg
				#print keyword
			if opt in("-n","--no-break"):
				force = True
except getopt.GetoptError:
		usage()
		sys.exit(2)

for i in range(0,depth):
		prefix += '../'
		url = target + prefix + file
		print "Testing: ",url
		try:
				response = urllib2.urlopen(url)
				#print response.info()
				html = response.read()
				#print html
		except:
				pass
		if keyword in html:
				print url, " is Vulnerable"
				writefile(url)
				if not force:
					break
				else:
					continue
		else:
				time.sleep(2)
				continue
<commit_msg>Add Testurl function<commit_after>'''
@KaiyiZhang Github
'''
import sys
import urllib2
import getopt
import time

target = ''
depth = 6
file = 'robots.txt'

html = ''
prefix = ''
block = (
		'%00'
		)
url = ''
keyword = 'User-agent'
force = False

def usage():
		print "LFI.Tester.py Help:"
		print "Usage: LFI.TESTER.py -t [-d] [-f] [-k]"
		print "	-t,--target The test url"
		print "	-d,--depth 	The depth for test (Default is 6)"
		print "	-f,--file The File include  (Default is robots.txt)"
		print "	-k,--keyword	the keyword for vuln check (Default is User-agent)"
		print "	-n,--no-break	no break while the vuln finded"

def testurl(url):
		print "Testing: ",url
		try:
				response = urllib2.urlopen(url)
				#print response.info()
				return response.read()
				#print html
		except:
				pass

def writefile(url):
		f = open('out.txt','a')
		t = time.strftime('%Y-%m-%d %X ',time.localtime())
		f.writelines(t + url + '\n')

try:
		if len(sys.argv) < 2:
				usage()
				sys.exit()
		opts,args = getopt.getopt(sys.argv[1:],"ht:d:f:k:n",["help","target=","depth=","file=","keyword=","no-break"])
		for opt, arg in opts:
			if opt in("-h","--help"):
				usage()
				sys.exit()
			if opt in("-t","--target"):
				target = arg
				if not target.startswith('http://', 0, 7):
						target = 'http://' + target
			if opt in("-d","--depth"):
				depth = int(arg)
				if depth < 1:
					usage()
					sys.exit()
			if opt in("-f","--file"):
				file = arg
				if file.startswith('/',0,1):
					file =file[1:]
			if opt in("-k","--keyword"):
				keyword = arg
				#print keyword
			if opt in("-n","--no-break"):
				force = True
except getopt.GetoptError:
		usage()
		sys.exit(2)

for i in range(0,depth):
		prefix += '../'
		url = target + prefix + file
		html = testurl(url)
		if keyword in html:
				print url, " is Vulnerable"
				writefile(url)
				if not force:
					break
				else:
					continue
		else:
				time.sleep(2)
				continue
<|endoftext|>


<commit_before>"Support for number formatting using the current locale settings"

from _locale import *
import string

#perform the grouping from right to left
def _group(s):
    conv=localeconv()
    grouping=conv['grouping']
    if not grouping:return s
    result=""
    while s and grouping:
	# if grouping is -1, we are done
	if grouping[0]==CHAR_MAX:
	    break
	# 0: re-use last group ad infinitum
	elif grouping[0]!=0:
	    #process last group
	    group=grouping[0]
	    grouping=grouping[1:]
	if result:
	    result=s[-group:]+conv['thousands_sep']+result
	else:
	    result=s[-group:]
	s=s[:-group]
    if s and result:
	result=s+conv['thousands_sep']+result
    return result

def format(f,val,grouping=0):
    """Formats a value in the same way that the % formatting would use,
    but takes the current locale into account.
    Grouping is applied if the third parameter is true."""
    result = f % val
    fields = string.splitfields(result,".")
    if grouping:
	fields[0]=_group(fields[0])
    if len(fields)==2:
	return fields[0]+localeconv()['decimal_point']+fields[1]
    elif len(fields)==1:
	return fields[0]
    else:
	raise Error,"Too many decimal points in result string"

def str(val):
    """Convert float to integer, taking the locale into account."""
    return format("%.12g",val)

def atof(str,func=string.atof):
    "Parses a string as a float according to the locale settings."
    #First, get rid of the grouping
    s=string.splitfields(str,localeconv()['thousands_sep'])
    str=string.join(s,"")
    #next, replace the decimal point with a dot
    s=string.splitfields(str,localeconv()['decimal_point'])
    str=string.join(s,'.')
    #finally, parse the string
    return func(str)

def atoi(str):
    "Converts a string to an integer according to the locale settings."
    return atof(str,string.atoi)

def test():
    setlocale(LC_ALL,"")
    #do grouping
    s1=format("%d",123456789,1)
    print s1,"is",atoi(s1)
    #standard formatting
    s1=str(3.14)
    print s1,"is",atof(s1)

if __name__=='__main__':
    test()
<commit_msg>Add Martin von Loewis as the author of this module.<commit_after>"Support for number formatting using the current locale settings"
# Author: Martin von Loewis

from _locale import *
import string

#perform the grouping from right to left
def _group(s):
    conv=localeconv()
    grouping=conv['grouping']
    if not grouping:return s
    result=""
    while s and grouping:
	# if grouping is -1, we are done
	if grouping[0]==CHAR_MAX:
	    break
	# 0: re-use last group ad infinitum
	elif grouping[0]!=0:
	    #process last group
	    group=grouping[0]
	    grouping=grouping[1:]
	if result:
	    result=s[-group:]+conv['thousands_sep']+result
	else:
	    result=s[-group:]
	s=s[:-group]
    if s and result:
	result=s+conv['thousands_sep']+result
    return result

def format(f,val,grouping=0):
    """Formats a value in the same way that the % formatting would use,
    but takes the current locale into account.
    Grouping is applied if the third parameter is true."""
    result = f % val
    fields = string.splitfields(result,".")
    if grouping:
	fields[0]=_group(fields[0])
    if len(fields)==2:
	return fields[0]+localeconv()['decimal_point']+fields[1]
    elif len(fields)==1:
	return fields[0]
    else:
	raise Error,"Too many decimal points in result string"

def str(val):
    """Convert float to integer, taking the locale into account."""
    return format("%.12g",val)

def atof(str,func=string.atof):
    "Parses a string as a float according to the locale settings."
    #First, get rid of the grouping
    s=string.splitfields(str,localeconv()['thousands_sep'])
    str=string.join(s,"")
    #next, replace the decimal point with a dot
    s=string.splitfields(str,localeconv()['decimal_point'])
    str=string.join(s,'.')
    #finally, parse the string
    return func(str)

def atoi(str):
    "Converts a string to an integer according to the locale settings."
    return atof(str,string.atoi)

def test():
    setlocale(LC_ALL,"")
    #do grouping
    s1=format("%d",123456789,1)
    print s1,"is",atoi(s1)
    #standard formatting
    s1=str(3.14)
    print s1,"is",atof(s1)

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


<commit_before>#  Author:	Fred L. Drake, Jr.
#		fdrake@cnri.reston.va.us, fdrake@acm.org
#
#  This is a simple little module I wrote to make life easier.  I didn't
#  see anything quite like it in the library, though I may have overlooked
#  something.  I wrote this when I was trying to read some heavily nested
#  tuples with fairly non-descriptive content.  This is modelled very much
#  after Lisp/Scheme - style pretty-printing of lists.  If you find it
#  useful, thank small children who sleep at night.

"""Support to pretty-print lists, tuples, & dictionaries recursively.

Very simple, but useful, especially in debugging data structures.

Classes
-------

PrettyPrinter()
    Handle pretty-printing operations onto a stream using a configured
    set of formatting parameters.

Functions
---------

pformat()
    Format a Python object into a pretty-printed representation.

pprint()
    Pretty-print a Python object to a stream [default is sys.sydout].

saferepr()
    Generate a 'standard' repr()-like value, but protect against recursive
    data structures.

"""

from types import DictType, ListType, TupleType

try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

def pprint(object, stream=None):
    """Pretty-print a Python object to a stream [default is sys.sydout]."""
    printer = PrettyPrinter(stream=stream)
    printer.pprint(object)

def pformat(object):
    """Format a Python object into a pretty-printed representation."""
    return PrettyPrinter().pformat(object)

def isreadable(object):
    """Determine if saferepr(object) is readable by eval()."""
    return PrettyPrinter().isreadable(object)

def isrecursive(object):
    """Determine if object requires a recursive representation."""
    return PrettyPrinter().isrecursive(object)

def saferepr(object):
    """Version of repr() which can handle recursive data structures."""
    return _safe_repr(object, {})[0]

class PrettyPrinter:
    def __init__(self, indent=1, width=80, depth=None, stream=None):
	"""Handle pretty printing operations onto a stream using a set of
	configured parameters.

	indent
	    Number of spaces to indent for each level of nesting.

	width
	    Attempted maximum number of columns in the output.

	depth
	    The maximum depth to print out nested structures.

	stream
	    The desired output stream.  If omitted (or false), the standard
	    output stream available at construction will be used.

	"""
	indent = int(indent)
	width = int(width)
	assert indent >= 0
	assert (not depth) or depth > 0, "depth may not be negative"
	assert width
	self.__depth = depth
	self.__indent_per_level = indent
	self.__width = width
	if stream:
	    self.__stream = stream
	else:
	    import sys
	    self.__stream = sys.stdout

    def pprint(self, object):
	self.__stream.write(self.pformat(object) + "\n")

    def pformat(self, object):
	sio = StringIO()
	self.__format(object, sio, 0, 0, {}, 0)
	return sio.getvalue()

    def isrecursive(self, object):
	self.__recursive = 0
	self.pformat(object)
	return self.__recursive

    def isreadable(self, object):
	self.__recursive = 0
	self.__readable = 1
	self.pformat(object)
	return self.__readable and not self.__recursive

    def __format(self, object, stream, indent, allowance, context, level):
	level = level + 1
	if context.has_key(id(object)):
	    object = _Recursion(object)
	    self.__recursive = 1
	rep = self.__repr(object, context, level - 1)
	objid = id(object)
	context[objid] = 1
	typ = type(object)
	sepLines = len(rep) > (self.__width - 1 - indent - allowance)

	if sepLines and typ in (ListType, TupleType):
	    #  Pretty-print the sequence.
	    stream.write((typ is ListType) and '[' or '(')
	    if self.__indent_per_level > 1:
		stream.write((self.__indent_per_level - 1) * ' ')
	    length = len(object)
	    if length:
		indent = indent + self.__indent_per_level
		self.__format(object[0], stream, indent, allowance + 1,
			      context, level)
		if len(object) > 1:
		    for ent in object[1:]:
			stream.write(',\n' + ' '*indent)
			self.__format(ent, stream, indent,
				      allowance + 1, context, level)
		indent = indent - self.__indent_per_level
	    stream.write(((typ is ListType) and ']') or ')')

	elif sepLines and typ is DictType:
	    stream.write('{')
	    if self.__indent_per_level > 1:
		stream.write((self.__indent_per_level - 1) * ' ')
	    length = len(object)
	    if length:
		indent = indent + self.__indent_per_level
		items  = object.items()
		items.sort()
		key, ent = items[0]
		rep = self.__repr(key, context, level) + ': '
		stream.write(rep)
		self.__format(ent, stream, indent + len(rep),
			      allowance + 1, context, level)
		if len(items) > 1:
		    for key, ent in items[1:]:
			rep = self.__repr(key, context, level) + ': '
			stream.write(',\n' + ' '*indent + rep)
			self.__format(ent, stream, indent + len(rep),
				      allowance + 1, context, level)
		indent = indent - self.__indent_per_level
	    stream.write('}')

	else:
	    stream.write(rep)
	    del context[objid]

    def __repr(self, object, context, level):
	repr, readable = _safe_repr(object, context, self.__depth, level)
	if not readable:
	    self.__readable = 0
	return repr

def _safe_repr(object, context, maxlevels=None, level=0):
    level = level + 1
    readable = 1
    typ = type(object)
    if not (typ in (DictType, ListType, TupleType) and object):
	rep = `object`
	if rep:
	    if rep[0] == '<':
		readable = 0
	else:
	    readable = 0
	return `object`, readable
    if context.has_key(id(object)):
	return `_Recursion(object)`, 0
    objid = id(object)
    context[objid] = 1
    if typ is DictType:
	if maxlevels and level >= maxlevels:
	    s = "{...}"
	    readable = 0
	else:
	    items = object.items()
	    k, v = items[0]
	    krepr, kreadable = _safe_repr(k, context, maxlevels, level)
	    vrepr, vreadable = _safe_repr(v, context, maxlevels, level)
	    readable = readable and kreadable and vreadable
	    s = "{%s: %s" % (krepr, vrepr)
	    for k, v in items[1:]:
		krepr, kreadable = _safe_repr(k, context, maxlevels, level)
		vrepr, vreadable = _safe_repr(v, context, maxlevels, level)
		readable = readable and kreadable and vreadable
		s = "%s, %s: %s" % (s, krepr, vrepr)
	    s = s + "}"
    else:
	s, term = (typ is ListType) and ('[', ']') or ('(', ')')
	if maxlevels and level >= maxlevels:
	    s = s + "..."
	    readable = 0
	else:
	    subrepr, subreadable = _safe_repr(
		object[0], context, maxlevels, level)
	    readable = readable and subreadable
	    s = s + subrepr
	    for ent in object[1:]:
		subrepr, subreadable = _safe_repr(
		    ent, context, maxlevels, level)
		readable = readable and subreadable
		s = "%s, %s" % (s, subrepr)
	s = s + term
    del context[objid]
    return s, readable

class _Recursion:
    # represent a recursive relationship; really only used for the __repr__()
    # method...
    def __init__(self, object):
	self.__repr = "<Recursion on %s with id=%s>" \
		      % (type(object).__name__, id(object))

    def __repr__(self):
	return self.__repr
<commit_msg>Added code to emit trailing ',' for singleton tuples in two places.<commit_after>#  Author:	Fred L. Drake, Jr.
#		fdrake@cnri.reston.va.us, fdrake@acm.org
#
#  This is a simple little module I wrote to make life easier.  I didn't
#  see anything quite like it in the library, though I may have overlooked
#  something.  I wrote this when I was trying to read some heavily nested
#  tuples with fairly non-descriptive content.  This is modelled very much
#  after Lisp/Scheme - style pretty-printing of lists.  If you find it
#  useful, thank small children who sleep at night.

"""Support to pretty-print lists, tuples, & dictionaries recursively.

Very simple, but useful, especially in debugging data structures.

Classes
-------

PrettyPrinter()
    Handle pretty-printing operations onto a stream using a configured
    set of formatting parameters.

Functions
---------

pformat()
    Format a Python object into a pretty-printed representation.

pprint()
    Pretty-print a Python object to a stream [default is sys.sydout].

saferepr()
    Generate a 'standard' repr()-like value, but protect against recursive
    data structures.

"""

from types import DictType, ListType, TupleType

try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

def pprint(object, stream=None):
    """Pretty-print a Python object to a stream [default is sys.sydout]."""
    printer = PrettyPrinter(stream=stream)
    printer.pprint(object)

def pformat(object):
    """Format a Python object into a pretty-printed representation."""
    return PrettyPrinter().pformat(object)

def isreadable(object):
    """Determine if saferepr(object) is readable by eval()."""
    return PrettyPrinter().isreadable(object)

def isrecursive(object):
    """Determine if object requires a recursive representation."""
    return PrettyPrinter().isrecursive(object)

def saferepr(object):
    """Version of repr() which can handle recursive data structures."""
    return _safe_repr(object, {})[0]

class PrettyPrinter:
    def __init__(self, indent=1, width=80, depth=None, stream=None):
	"""Handle pretty printing operations onto a stream using a set of
	configured parameters.

	indent
	    Number of spaces to indent for each level of nesting.

	width
	    Attempted maximum number of columns in the output.

	depth
	    The maximum depth to print out nested structures.

	stream
	    The desired output stream.  If omitted (or false), the standard
	    output stream available at construction will be used.

	"""
	indent = int(indent)
	width = int(width)
	assert indent >= 0
	assert (not depth) or depth > 0, "depth may not be negative"
	assert width
	self.__depth = depth
	self.__indent_per_level = indent
	self.__width = width
	if stream:
	    self.__stream = stream
	else:
	    import sys
	    self.__stream = sys.stdout

    def pprint(self, object):
	self.__stream.write(self.pformat(object) + "\n")

    def pformat(self, object):
	sio = StringIO()
	self.__format(object, sio, 0, 0, {}, 0)
	return sio.getvalue()

    def isrecursive(self, object):
	self.__recursive = 0
	self.pformat(object)
	return self.__recursive

    def isreadable(self, object):
	self.__recursive = 0
	self.__readable = 1
	self.pformat(object)
	return self.__readable and not self.__recursive

    def __format(self, object, stream, indent, allowance, context, level):
	level = level + 1
	if context.has_key(id(object)):
	    object = _Recursion(object)
	    self.__recursive = 1
	rep = self.__repr(object, context, level - 1)
	objid = id(object)
	context[objid] = 1
	typ = type(object)
	sepLines = len(rep) > (self.__width - 1 - indent - allowance)

	if sepLines and typ in (ListType, TupleType):
	    #  Pretty-print the sequence.
	    stream.write((typ is ListType) and '[' or '(')
	    if self.__indent_per_level > 1:
		stream.write((self.__indent_per_level - 1) * ' ')
	    length = len(object)
	    if length:
		indent = indent + self.__indent_per_level
		self.__format(object[0], stream, indent, allowance + 1,
			      context, level)
		if len(object) > 1:
		    for ent in object[1:]:
			stream.write(',\n' + ' '*indent)
			self.__format(ent, stream, indent,
				      allowance + 1, context, level)
		indent = indent - self.__indent_per_level
	    if typ is TupleType and length == 1:
		stream.write(',')
	    stream.write(((typ is ListType) and ']') or ')')

	elif sepLines and typ is DictType:
	    stream.write('{')
	    if self.__indent_per_level > 1:
		stream.write((self.__indent_per_level - 1) * ' ')
	    length = len(object)
	    if length:
		indent = indent + self.__indent_per_level
		items  = object.items()
		items.sort()
		key, ent = items[0]
		rep = self.__repr(key, context, level) + ': '
		stream.write(rep)
		self.__format(ent, stream, indent + len(rep),
			      allowance + 1, context, level)
		if len(items) > 1:
		    for key, ent in items[1:]:
			rep = self.__repr(key, context, level) + ': '
			stream.write(',\n' + ' '*indent + rep)
			self.__format(ent, stream, indent + len(rep),
				      allowance + 1, context, level)
		indent = indent - self.__indent_per_level
	    stream.write('}')

	else:
	    stream.write(rep)
	    del context[objid]

    def __repr(self, object, context, level):
	repr, readable = _safe_repr(object, context, self.__depth, level)
	if not readable:
	    self.__readable = 0
	return repr

def _safe_repr(object, context, maxlevels=None, level=0):
    level = level + 1
    readable = 1
    typ = type(object)
    if not (typ in (DictType, ListType, TupleType) and object):
	rep = `object`
	if rep:
	    if rep[0] == '<':
		readable = 0
	else:
	    readable = 0
	return `object`, readable
    if context.has_key(id(object)):
	return `_Recursion(object)`, 0
    objid = id(object)
    context[objid] = 1
    if typ is DictType:
	if maxlevels and level >= maxlevels:
	    s = "{...}"
	    readable = 0
	else:
	    items = object.items()
	    k, v = items[0]
	    krepr, kreadable = _safe_repr(k, context, maxlevels, level)
	    vrepr, vreadable = _safe_repr(v, context, maxlevels, level)
	    readable = readable and kreadable and vreadable
	    s = "{%s: %s" % (krepr, vrepr)
	    for k, v in items[1:]:
		krepr, kreadable = _safe_repr(k, context, maxlevels, level)
		vrepr, vreadable = _safe_repr(v, context, maxlevels, level)
		readable = readable and kreadable and vreadable
		s = "%s, %s: %s" % (s, krepr, vrepr)
	    s = s + "}"
    else:
	s, term = (typ is ListType) and ('[', ']') or ('(', ')')
	if maxlevels and level >= maxlevels:
	    s = s + "..."
	    readable = 0
	else:
	    subrepr, subreadable = _safe_repr(
		object[0], context, maxlevels, level)
	    readable = readable and subreadable
	    s = s + subrepr
	    tail = object[1:]
	    if not tail:
		if typ is TupleType:
		    s = s + ','
	    for ent in tail:
		subrepr, subreadable = _safe_repr(
		    ent, context, maxlevels, level)
		readable = readable and subreadable
		s = "%s, %s" % (s, subrepr)
	s = s + term
    del context[objid]
    return s, readable

class _Recursion:
    # represent a recursive relationship; really only used for the __repr__()
    # method...
    def __init__(self, object):
	self.__repr = "<Recursion on %s with id=%s>" \
		      % (type(object).__name__, id(object))

    def __repr__(self):
	return self.__repr
<|endoftext|>


<commit_before>from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

from nextcloudappstore.core.tests.e2e.base import BaseStoreTest

from django.test import tag

@tag('e2e')
class ChangeLanguageTest(BaseStoreTest):
    def test_change_lang(self):
        self.login()
        self.go_to('user:account-change-language')

        elem = self.by_id('language')
        lang = Select(elem)
        lang.select_by_value('de')
        elem.submit()

        self.go_to('user:account-change-language')
        account_link = self.findNavigationLink('user:account')
        self.assertEqual('Konto', account_link.text)
<commit_msg>fix race condition part 2<commit_after>from selenium.webdriver.support.expected_conditions import staleness_of
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

from nextcloudappstore.core.tests.e2e import SELENIUM_WAIT_SEC
from nextcloudappstore.core.tests.e2e.base import BaseStoreTest

from django.test import tag

@tag('e2e')
class ChangeLanguageTest(BaseStoreTest):
    def test_change_lang(self):
        self.login()
        self.go_to('user:account-change-language')

        elem = self.by_id('language')
        lang = Select(elem)
        lang.select_by_value('de')
        elem.submit()

        WebDriverWait(self.selenium, SELENIUM_WAIT_SEC).until(staleness_of(elem))
        account_link = self.findNavigationLink('user:account')
        self.assertEqual('Konto', account_link.text)
<|endoftext|>