text:
ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['preview'],
                    'supported_by': 'network'}

DOCUMENTATION = '''
---
module: nxos_vlan
extends_documentation_fragment: nxos
version_added: "2.1"
short_description: Manages VLAN resources and attributes.
description:
  - Manages VLAN configurations on NX-OS switches.
author: Jason Edelman (@jedelman8)
options:
  vlan_id:
    description:
      - Single VLAN ID.
  vlan_range:
    description:
      - Range of VLANs such as 2-10 or 2,5,10-15, etc.
  name:
    description:
      - Name of VLAN or keyword 'default'.
  interfaces:
    description:
      - List of interfaces that should be associated to the VLAN or keyword 'default'.
    version_added: "2.5"
  associated_interfaces:
    description:
      - This is a intent option and checks the operational state of the for given vlan C(name)
        for associated interfaces. If the value in the C(associated_interfaces) does not match with
        the operational state of vlan interfaces on device it will result in failure.
    version_added: "2.5"
  vlan_state:
    description:
      - Manage the vlan operational state of the VLAN
    default: active
    choices: ['active','suspend']
  admin_state:
    description:
      - Manage the VLAN administrative state of the VLAN equivalent
        to shut/no shut in VLAN config mode.
    default: up
    choices: ['up','down']
  mapped_vni:
    description:
      - The Virtual Network Identifier (VNI) ID that is mapped to the
        VLAN. Valid values are integer and keyword 'default'. Range 4096-16773119.
    version_added: "2.2"
  state:
    description:
      - Manage the state of the resource.
    default: present
    choices: ['present','absent']
  mode:
    description:
      - Set VLAN mode to classical ethernet or fabricpath.
        This is a valid option for Nexus 5000 and 7000 series.
    choices: ['ce','fabricpath']
    version_added: "2.4"
  aggregate:
    description: List of VLANs definitions.
    version_added: "2.5"
  purge:
    description:
      - Purge VLANs not defined in the I(aggregate) parameter.
        This parameter can be used without aggregate as well.
    type: bool
    default: 'no'
  delay:
    description:
      - Time in seconds to wait before checking for the operational state on remote
        device. This wait is applicable for operational state arguments.
    default: 10
'''

EXAMPLES = '''
- name: Ensure a range of VLANs are not present on the switch
  nxos_vlan:
    vlan_range: "2-10,20,50,55-60,100-150"
    state: absent

- name: Ensure VLAN 50 exists with the name WEB and is in the shutdown state
  nxos_vlan:
    vlan_id: 50
    admin_state: down
    name: WEB

- name: Ensure VLAN is NOT on the device
  nxos_vlan:
    vlan_id: 50
    state: absent

- name: Add interfaces to VLAN and check intent (config + intent)
  nxos_vlan:
    vlan_id: 100
    interfaces:
      - Ethernet2/1
      - Ethernet2/5
    associated_interfaces:
      - Ethernet2/1
      - Ethernet2/5

- name: Check interfaces assigned to VLAN
  nxos_vlan:
    vlan_id: 100
    associated_interfaces:
      - Ethernet2/1
      - Ethernet2/5

- name: Create aggregate of vlans
  nxos_vlan:
    aggregate:
      - { vlan_id: 4000, mode: ce }
      - { vlan_id: 4001, name: vlan-4001 }

- name: purge vlans - removes all other vlans except the ones mentioned in aggregate)
  nxos_vlan:
    aggregate:
      - vlan_id: 1
      - vlan_id: 4001
    purge: yes

'''

RETURN = '''
commands:
    description: Set of command strings to send to the remote device
    returned: always
    type: list
    sample: ["vlan 20", "vlan 55", "vn-segment 5000"]
'''

import re
import time

from copy import deepcopy

from ansible.module_utils.network.nxos.nxos import get_config, load_config, run_commands
from ansible.module_utils.network.nxos.nxos import normalize_interface, nxos_argument_spec
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.connection import ConnectionError
from ansible.module_utils.network.common.config import CustomNetworkConfig
from ansible.module_utils.network.common.utils import remove_default_spec

def search_obj_in_list(vlan_id, lst):
    for o in lst:
        if o['vlan_id'] == vlan_id:
            return o

def get_diff(w, obj):
    c = deepcopy(w)
    entries = ('interfaces', 'associated_interfaces', 'delay', 'vlan_range')
    for key in entries:
        if key in c:
            del c[key]

    o = deepcopy(obj)
    del o['interfaces']
    if o['vlan_id'] == w['vlan_id']:
        diff_dict = dict(set(c.items()) - set(o.items()))
        return diff_dict

def is_default_name(obj, vlan_id):
    cname = obj['name']
    if ('VLAN' in cname):
        vid = int(cname[4:])
        if vid == int(vlan_id):
            return True

    return False

def map_obj_to_commands(updates, module):
    commands = list()
    purge = module.params['purge']
    want, have = updates

    for w in want:
        vlan_id = w['vlan_id']
        name = w['name']
        interfaces = w.get('interfaces') or []
        mapped_vni = w['mapped_vni']
        mode = w['mode']
        vlan_state = w['vlan_state']
        admin_state = w['admin_state']
        state = w['state']
        del w['state']

        obj_in_have = search_obj_in_list(vlan_id, have)

        if state == 'absent':
            if obj_in_have:
                commands.append('no vlan {0}'.format(vlan_id))

        elif state == 'present':
            if not obj_in_have:
                commands.append('vlan {0}'.format(vlan_id))

                if name and name != 'default':
                    commands.append('name {0}'.format(name))
                if mode:
                    commands.append('mode {0}'.format(mode))
                if vlan_state:
                    commands.append('state {0}'.format(vlan_state))
                if mapped_vni != 'None' and mapped_vni != 'default':
                    commands.append('vn-segment {0}'.format(mapped_vni))
                if admin_state == 'up':
                    commands.append('no shutdown')
                if admin_state == 'down':
                    commands.append('shutdown')
                commands.append('exit')

                if interfaces and interfaces[0] != 'default':
                    for i in interfaces:
                        commands.append('interface {0}'.format(i))
                        commands.append('switchport')
                        commands.append('switchport mode access')
                        commands.append('switchport access vlan {0}'.format(vlan_id))

            else:
                diff = get_diff(w, obj_in_have)
                if diff:
                    commands.append('vlan {0}'.format(vlan_id))
                    for key, value in diff.items():
                        if key == 'name':
                            if name != 'default':
                                if name is not None:
                                    commands.append('name {0}'.format(value))
                            else:
                                if not is_default_name(obj_in_have, vlan_id):
                                    commands.append('no name')
                        if key == 'vlan_state' and value:
                            commands.append('state {0}'.format(value))
                        if key == 'mapped_vni':
                            if value == 'default':
                                if obj_in_have['mapped_vni'] != 'None':
                                    commands.append('no vn-segment')
                            elif value != 'None':
                                commands.append('vn-segment {0}'.format(value))
                        if key == 'admin_state':
                            if value == 'up':
                                commands.append('no shutdown')
                            elif value == 'down':
                                commands.append('shutdown')
                        if key == 'mode' and value:
                            commands.append('mode {0}'.format(value))
                    if len(commands) > 1:
                        commands.append('exit')
                    else:
                        del commands[:]

                if interfaces and interfaces[0] != 'default':
                    if not obj_in_have['interfaces']:
                        for i in interfaces:
                            commands.append('vlan {0}'.format(vlan_id))
                            commands.append('exit')
                            commands.append('interface {0}'.format(i))
                            commands.append('switchport')
                            commands.append('switchport mode access')
                            commands.append('switchport access vlan {0}'.format(vlan_id))

                    elif set(interfaces) != set(obj_in_have['interfaces']):
                        missing_interfaces = list(set(interfaces) - set(obj_in_have['interfaces']))
                        for i in missing_interfaces:
                            commands.append('vlan {0}'.format(vlan_id))
                            commands.append('exit')
                            commands.append('interface {0}'.format(i))
                            commands.append('switchport')
                            commands.append('switchport mode access')
                            commands.append('switchport access vlan {0}'.format(vlan_id))

                        superfluous_interfaces = list(set(obj_in_have['interfaces']) - set(interfaces))
                        for i in superfluous_interfaces:
                            commands.append('vlan {0}'.format(vlan_id))
                            commands.append('exit')
                            commands.append('interface {0}'.format(i))
                            commands.append('switchport')
                            commands.append('switchport mode access')
                            commands.append('no switchport access vlan {0}'.format(vlan_id))

                elif interfaces and interfaces[0] == 'default':
                    if obj_in_have['interfaces']:
                        for i in obj_in_have['interfaces']:
                            commands.append('vlan {0}'.format(vlan_id))
                            commands.append('exit')
                            commands.append('interface {0}'.format(i))
                            commands.append('switchport')
                            commands.append('switchport mode access')
                            commands.append('no switchport access vlan {0}'.format(vlan_id))

    if purge:
        for h in have:
            obj_in_want = search_obj_in_list(h['vlan_id'], want)
            if not obj_in_want:
                commands.append('no vlan {0}'.format(h['vlan_id']))

    return commands

def want_vlan_list(module):
    result = []
    vlan_range = module.params['vlan_range']
    for part in vlan_range.split(','):
        if part == 'none':
            break
        if '-' in part:
            start, end = part.split('-')
            start, end = int(start), int(end)
            result.extend([str(i) for i in range(start, end + 1)])
        else:
            result.append(part)
    return result

def have_vlan_list(have):
    result = []
    if have:
        for h in have:
            result.append(str(h.get('vlan_id')))
    return result

def vlan_range_commands(module, have):
    commands = list()
    proposed_vlans_list = want_vlan_list(module)
    existing_vlans_list = have_vlan_list(have)

    if module.params['state'] == 'absent':
        vlans = set(proposed_vlans_list).intersection(existing_vlans_list)
        for vlan in vlans:
            commands.append('no vlan {0}'.format(vlan))

    elif module.params['state'] == 'present':
        vlans = set(proposed_vlans_list).difference(existing_vlans_list)
        for vlan in vlans:
            commands.append('vlan {0}'.format(vlan))

    return commands

def normalize(interfaces):
    normalized = None
    if interfaces:
        normalized = [normalize_interface(i) for i in interfaces]
    return normalized

def map_params_to_obj(module):
    obj = []
    if module.params['vlan_range']:
        return []

    aggregate = module.params.get('aggregate')
    if aggregate:
        for item in aggregate:
            for key in item:
                if item.get(key) is None:
                    item[key] = module.params[key]

            d = item.copy()
            d['vlan_id'] = str(d['vlan_id'])
            d['mapped_vni'] = str(d['mapped_vni'])
            d['interfaces'] = normalize(d['interfaces'])
            d['associated_interfaces'] = normalize(d['associated_interfaces'])

            obj.append(d)
    else:
        interfaces = normalize(module.params['interfaces'])
        associated_interfaces = normalize(module.params['associated_interfaces'])

        obj.append({
            'vlan_id': str(module.params['vlan_id']),
            'name': module.params['name'],
            'interfaces': interfaces,
            'vlan_state': module.params['vlan_state'],
            'mapped_vni': str(module.params['mapped_vni']),
            'state': module.params['state'],
            'admin_state': module.params['admin_state'],
            'mode': module.params['mode'],
            'associated_interfaces': associated_interfaces
        })

    return obj

def parse_admin_state(vlan):
    shutstate = vlan.get('vlanshowbr-shutstate')
    if shutstate == 'noshutdown':
        return 'up'
    elif shutstate == 'shutdown':
        return 'down'

def parse_mode(config):
    mode = None

    if config:
        match = re.search(r'mode (\S+)', config)
        if match:
            mode = match.group(1)
    return mode

def parse_vni(config):
    vni = None

    if config:
        match = re.search(r'vn-segment (\S+)', config)
        if match:
            vni = match.group(1)
    return str(vni)

def get_vlan_int(interfaces):
    vlan_int = []
    for i in interfaces.split(','):
        if 'eth' in i.lower() and '-' in i:
            int_range = i.split('-')
            stop = int((int_range)[1])
            start = int(int_range[0].split('/')[1])
            eth = int_range[0].split('/')[0]
            for r in range(start, stop + 1):
                vlan_int.append(eth + '/' + str(r))
        else:
            vlan_int.append(i)
    return vlan_int

def parse_interfaces(module, vlan):
    vlan_int = []
    interfaces = vlan.get('vlanshowplist-ifidx')
    if interfaces:
        if isinstance(interfaces, list):
            interfaces_list = [i.strip() for i in interfaces]
            interfaces_str = ','.join(interfaces_list)
            vlan_int = get_vlan_int(interfaces_str)
        else:
            vlan_int = get_vlan_int(interfaces)
    return vlan_int

def parse_vlan_config(netcfg, vlan_id):
    parents = ['vlan {0}'.format(vlan_id)]
    config = netcfg.get_section(parents)
    return config

def parse_vlan_options(module, netcfg, output, vlan):
    obj = {}
    vlan_id = vlan['vlanshowbr-vlanid-utf']
    config = parse_vlan_config(netcfg, vlan_id)

    obj['vlan_id'] = str(vlan_id)
    obj['name'] = vlan.get('vlanshowbr-vlanname')
    obj['vlan_state'] = vlan.get('vlanshowbr-vlanstate')
    obj['admin_state'] = parse_admin_state(vlan)
    obj['mode'] = parse_mode(config)
    obj['mapped_vni'] = parse_vni(config)
    obj['interfaces'] = parse_interfaces(module, vlan)
    return obj

def parse_vlan_non_structured(module, netcfg, vlans):
    objs = list()

    for vlan in vlans:
        vlan_match = re.search(r'(\d+)', vlan, re.M)
        if vlan_match:
            obj = {}
            vlan_id = vlan_match.group(1)
            obj['vlan_id'] = str(vlan_id)

            name_match = re.search(r'{0}\s*(\S+)'.format(vlan_id), vlan, re.M)
            if name_match:
                name = name_match.group(1)
                obj['name'] = name

                state_match = re.search(r'{0}\s*{1}\s*(\S+)'.format(vlan_id, name), vlan, re.M)
                if state_match:
                    vlan_state_match = state_match.group(1)
                    if vlan_state_match == 'suspended':
                        vlan_state = 'suspend'
                        admin_state = 'up'
                    elif vlan_state_match == 'sus/lshut':
                        vlan_state = 'suspend'
                        admin_state = 'down'
                    if vlan_state_match == 'active':
                        vlan_state = 'active'
                        admin_state = 'up'
                    if vlan_state_match == 'act/lshut':
                        vlan_state = 'active'
                        admin_state = 'down'

                    obj['vlan_state'] = vlan_state
                    obj['admin_state'] = admin_state

                    vlan = ','.join(vlan.splitlines())
                    interfaces = list()
                    intfs_match = re.search(r'{0}\s*{1}\s*{2}\s*(.*)'.format(vlan_id, name, vlan_state_match),
                                            vlan, re.M)
                    if intfs_match:
                        intfs = intfs_match.group(1)
                        intfs = intfs.split()
                        for i in intfs:
                            intf = normalize_interface(i.strip(','))
                            interfaces.append(intf)

                    if interfaces:
                        obj['interfaces'] = interfaces
                    else:
                        obj['interfaces'] = None

                    config = parse_vlan_config(netcfg, vlan_id)
                    obj['mode'] = parse_mode(config)
                    obj['mapped_vni'] = parse_vni(config)

        objs.append(obj)

    return objs

def map_config_to_obj(module):
    objs = list()
    output = None

    command = ['show vlan brief | json']
    output = run_commands(module, command, check_rc='retry_json')[0]
    if output:
        netcfg = CustomNetworkConfig(indent=2,
                                     contents=get_config(module, flags=['all']))

        if isinstance(output, dict):
            vlans = None
            try:
                vlans = output['TABLE_vlanbriefxbrief']['ROW_vlanbriefxbrief']
            except KeyError:
                return objs

            if vlans:
                if isinstance(vlans, list):
                    for vlan in vlans:
                        obj = parse_vlan_options(module, netcfg, output, vlan)
                        objs.append(obj)
                elif isinstance(vlans, dict):
                    obj = parse_vlan_options(module, netcfg, output, vlans)
                    objs.append(obj)
        else:
            vlans = list()
            splitted_line = re.split(r'\n(\d+)|\n{2}', output.strip())

            for line in splitted_line:
                if not line:
                    continue
                if len(line) > 0:
                    line = line.strip()
                    if line[0].isdigit():
                        match = re.search(r'^(\d+)$', line, re.M)
                        if match:
                            v = match.group(1)
                            pos1 = splitted_line.index(v)
                            pos2 = pos1 + 1
                            vlaninfo = ''.join(splitted_line[pos1:pos2 + 1])
                            vlans.append(vlaninfo)

            if vlans:
                objs = parse_vlan_non_structured(module, netcfg, vlans)
            else:
                return objs

    return objs

def check_declarative_intent_params(want, module, result):

    have = None
    is_delay = False

    for w in want:
        if w.get('associated_interfaces') is None:
            continue

        if result['changed'] and not is_delay:
            time.sleep(module.params['delay'])
            is_delay = True

        if have is None:
            have = map_config_to_obj(module)

        for i in w['associated_interfaces']:
            obj_in_have = search_obj_in_list(w['vlan_id'], have)
            if obj_in_have and 'interfaces' in obj_in_have and i not in obj_in_have['interfaces']:
                module.fail_json(msg="Interface %s not configured on vlan %s" % (i, w['vlan_id']))

def main():
    """ main entry point for module execution
    """
    element_spec = dict(
        vlan_id=dict(required=False, type='int'),
        vlan_range=dict(required=False),
        name=dict(required=False),
        interfaces=dict(type='list'),
        associated_interfaces=dict(type='list'),
        vlan_state=dict(choices=['active', 'suspend'], required=False, default='active'),
        mapped_vni=dict(required=False),
        delay=dict(default=10, type='int'),
        state=dict(choices=['present', 'absent'], default='present', required=False),
        admin_state=dict(choices=['up', 'down'], required=False, default='up'),
        mode=dict(choices=['ce', 'fabricpath'], required=False),
    )

    aggregate_spec = deepcopy(element_spec)
    aggregate_spec['vlan_id'] = dict(required=True)

    # remove default in aggregate spec, to handle common arguments
    remove_default_spec(aggregate_spec)

    argument_spec = dict(
        aggregate=dict(type='list', elements='dict', options=aggregate_spec),
        purge=dict(default=False, type='bool')
    )

    argument_spec.update(element_spec)
    argument_spec.update(nxos_argument_spec)

    required_one_of = [['vlan_id', 'aggregate', 'vlan_range']]
    mutually_exclusive = [['vlan_id', 'aggregate'],
                          ['vlan_range', 'name'],
                          ['vlan_id', 'vlan_range']]

    module = AnsibleModule(argument_spec=argument_spec,
                           required_one_of=required_one_of,
                           mutually_exclusive=mutually_exclusive,
                           supports_check_mode=True)

    warnings = list()
    result = {'changed': False}
    if warnings:
        result['warnings'] = warnings

    have = map_config_to_obj(module)
    want = map_params_to_obj(module)

    if module.params['vlan_range']:
        commands = vlan_range_commands(module, have)
        result['commands'] = commands
    else:
        commands = map_obj_to_commands((want, have), module)
        result['commands'] = commands

    if commands:
        if not module.check_mode:
            load_config(module, commands)
        result['changed'] = True

    if want:
        check_declarative_intent_params(want, module, result)

    module.exit_json(**result)

if __name__ == '__main__':
    main()


text:
/*
The collection of all the items of a country.
The collection is fetched through the api following the country code.
*/
define(function (require) {
  'use strict';

  var config = require("../../config");
  var ItemModel = require("./itemModel.js");

  var ItemsCollection = Backbone.Collection.extend({
    model: ItemModel,

    initialize: function(/*models, options*/) {
      this.url = config.apiUrl + '/items/popular';
    }
  });

  var all;

  ItemsCollection.getInstance = function() {
    var users = all;
    if (!users) {
      all = new ItemsCollection([], {});
    }
    return all;
  };

  return ItemsCollection;

});

redpajama_set_name: RedPajamaGithub


text:
<?xml version="1.0" encoding="UTF-8"?>
<!--

    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You 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.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-vertx-parent</artifactId>
        <version>3.14.0-SNAPSHOT</version>
    </parent>

    <artifactId>camel-vertx-websocket</artifactId>
    <packaging>jar</packaging>
    <name>Camel :: Vert.x :: WebSocket</name>
    <description>Expose WebSocket endpoints and connect to remote WebSocket servers using Vert.x</description>

    <properties>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-vertx-common</artifactId>
        </dependency>
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-web</artifactId>
            <version>${vertx-version}</version>
        </dependency>

        <!-- testing -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-test-junit5</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>


text:
/* eslint-disable */
import React from 'react';
import Button from './Button';

import { storiesOf, configure } from '@storybook/react';
import { action } from '@storybook/addon-actions';

storiesOf('Button', module).add('basic', () => <Button label="The Button" />);

storiesOf('Button').add('no module', () => <Button label="The Button" />);

storiesOf('Button', module).add('with story parameters', () => <Button label="The Button" />, {
  header: false,
  inline: true,
});

storiesOf('Button', module)
  .addParameters({ foo: 1 })
  .add('with kind parameters', () => <Button label="The Button" />);

storiesOf('Button', module)
  .addParameters({ component: Button })
  .add('with existing component parameters', () => <Button label="The Button" />);

storiesOf('Button', module).add('complex story', () => (

    <Button label="The Button" onClick={action('onClick')} />

));

storiesOf('Root|Some/Button', module).add('with path', () => <Button label="The Button" />);

storiesOf('Some.Button', module).add('with dot-path', () => <Button label="The Button" />);

storiesOf('Some.Button', module)
  .addDecorator(withKnobs)
  .add('with decorator', () => <Button label="The Button" />);

// This isn't a valid story, but it tests the `import { comp } from ...` case
storiesOf('action', module).add('non-default component export', () => <action />);

// This shouldn't get modified since the story name doesn't match
storiesOf('something', module).add('non-matching story', () => <Button label="The Button" />);


text:
"""Tests for tfx.components.example_gen.custom_executos.avro_executor."""

import os

import apache_beam as beam
from apache_beam.testing import util
import tensorflow as tf
from tfx.components.example_gen.custom_executors import avro_executor
from tfx.dsl.io import fileio
from tfx.proto import example_gen_pb2
from tfx.types import artifact_utils
from tfx.types import standard_artifacts
from tfx.types import standard_component_specs
from tfx.utils import proto_utils

class ExecutorTest(tf.test.TestCase):

  def setUp(self):
    super().setUp()
    self._input_data_dir = os.path.join(
        os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'testdata',
        'external')

  def testAvroToExample(self):
    with beam.Pipeline() as pipeline:
      examples = (
          pipeline
          | 'ToTFExample' >> avro_executor._AvroToExample(
              exec_properties={
                  standard_component_specs.INPUT_BASE_KEY: self._input_data_dir
              },
              split_pattern='avro/*.avro'))

      def check_result(got):
        # We use Python assertion here to avoid Beam serialization error in
        # pickling tf.test.TestCase.
        assert (10000 == len(got)), 'Unexpected example count'
        assert (18 == len(got[0].features.feature)), 'Example not match'

      util.assert_that(examples, check_result)

  def testDo(self):
    output_data_dir = os.path.join(
        os.environ.get('TEST_UNDECLARED_OUTPUTS_DIR', self.get_temp_dir()),
        self._testMethodName)

    # Create output dict.
    examples = standard_artifacts.Examples()
    examples.uri = output_data_dir
    output_dict = {standard_component_specs.EXAMPLES_KEY: [examples]}

    # Create exec proterties.
    exec_properties = {
        standard_component_specs.INPUT_BASE_KEY:
            self._input_data_dir,
        standard_component_specs.INPUT_CONFIG_KEY:
            proto_utils.proto_to_json(
                example_gen_pb2.Input(splits=[
                    example_gen_pb2.Input.Split(
                        name='avro', pattern='avro/*.avro'),
                ])),
        standard_component_specs.OUTPUT_CONFIG_KEY:
            proto_utils.proto_to_json(
                example_gen_pb2.Output(
                    split_config=example_gen_pb2.SplitConfig(splits=[
                        example_gen_pb2.SplitConfig.Split(
                            name='train', hash_buckets=2),
                        example_gen_pb2.SplitConfig.Split(
                            name='eval', hash_buckets=1)
                    ])))
    }

    # Run executor.
    avro_example_gen = avro_executor.Executor()
    avro_example_gen.Do({}, output_dict, exec_properties)

    self.assertEqual(
        artifact_utils.encode_split_names(['train', 'eval']),
        examples.split_names)

    # Check Avro example gen outputs.
    train_output_file = os.path.join(examples.uri, 'Split-train',
                                     'data_tfrecord-00000-of-00001.gz')
    eval_output_file = os.path.join(examples.uri, 'Split-eval',
                                    'data_tfrecord-00000-of-00001.gz')
    self.assertTrue(fileio.exists(train_output_file))
    self.assertTrue(fileio.exists(eval_output_file))
    self.assertGreater(
        fileio.open(train_output_file).size(),
        fileio.open(eval_output_file).size())

if __name__ == '__main__':
  tf.test.main()


text:
(function(global) {

	var PolymerFilters = function() {};

	PolymerFilters.prototype = {
		/**
		 * Capitalize a string (transform its first letter to uppercase and the rest to lowercase)
		 * @param  {string} string
		 * @return {string} the capitalized string
		 */

		_capitalize: function(string) {
			string = string.toLowerCase();
			return string.charAt(0).toUpperCase() + string.slice(1);
		},

		/**
		 * Format a Number in Currency Format
		 * @param  {number} input
		 * @param  {string} currency - defaults to '$'
		 * @param  {integer} precision - defaults to 2
		 * @return {string}
		 */

		_currency: function(input, currency, precision) {

			if (isNaN(input)) {
				return "Value is not a Number";
			} else {
				currency = currency || "$";
				precision = precision || 2;

				input = Number(input);
				input = input.toFixed(precision);
			}

			var
				parts = input.split('.'),
				fnum = parts[0],
				decimal = parts[1] ? '.' + parts[1] : '';

			return currency + fnum.replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') + decimal;
		},

		/**
		 * Transform a supplied input date using a specified format
		 * @param  {date} input
		 * @param  {string} format
		 * @return {date}
		 */

		_date: function(input, format) {
			var date = new Date(input),
				day = date.getDate(),
				month = date.getMonth() + 1,
				year = date.getFullYear(),
				hours = date.getHours(),
				minutes = date.getMinutes(),
				seconds = date.getSeconds();

			if (!format) {
				format = "MM/dd/yyyy";
			}

			format = format.replace("MM", month.toString().replace(/^(\d)$/, '0$1'));

			if (format.indexOf("yyyy") > -1) {
				format = format.replace("yyyy", year.toString());
			} else if (format.indexOf("yy") > -1) {
				format = format.replace("yy", year.toString().substr(2, 2));
			}

			format = format.replace("dd", day.toString().replace(/^(\d)$/, '0$1'));

			if (format.indexOf("t") > -1) {
				if (hours > 11) {
					format = format.replace("t", "pm");
				} else {
					format = format.replace("t", "am");
				}
			}

			if (format.indexOf("HH") > -1) {
				format = format.replace("HH", hours.toString().replace(/^(\d)$/, '0$1'));
			}

			if (format.indexOf("hh") > -1) {
				if (hours > 12) {
					hours -= 12;
				}

				if (hours === 0) {
					hours = 12;
				}
				format = format.replace("hh", hours.toString().replace(/^(\d)$/, '0$1'));
			}

			if (format.indexOf("mm") > -1) {
				format = format.replace("mm", minutes.toString().replace(/^(\d)$/, '0$1'));
			}

			if (format.indexOf("ss") > -1) {
				format = format.replace("ss", seconds.toString().replace(/^(\d)$/, '0$1'));
			}

			return format;
		},

		_first: function(arr) {
			return arr[0];
		},

		_last: function(arr) {
			return arr[arr.length - 1];
		},

		_length: function(arr) {
			return arr.length;
		},

		/**
		 * Limit displayed items from an array at an offset by a limit
		 *
		 * @param  {array} input
		 * @param  {integer} offset
		 * @param  {integer} limit
		 * @return {array}
		 */

		_limitFrom: function(input, offset, limit) {
			if (!(input instanceof Array) && !(input instanceof String)) return input;

			limit = parseInt(limit, 10);

			if (input instanceof String) {
				if (limit) {
					return limit >= 0 ? input.slice(offset, limit) : input.slice(limit, input.length);
				} else {
					return "";
				}
			}

			var out = [],
				i, n;

			if (limit > input.length)
				limit = input.length;
			else if (limit < -input.length)
				limit = -input.length;

			if (limit > 0) {
				i = offset;
				n = limit;
			} else {
				i = input.length + limit;
				n = input.length;
			}

			for (; i < n; i++) {
				out.push(input[i]);
			}

			return out;
		},

		/**
		 * Creates a new array or string containing only a specified
		 * number of elements. The elements are taken from either
		 * the beginning or the end of the source array or string,
		 * as specified by the value and sign (positive or negative) of `limit`
		 *
		 * Note: this is a straight-up port from the Angular limitTo filter
		 * and they deserve full credit for the implementation.
		 * @param  {Array|string} input input Source array or string to be limited.
		 * @param  {string|number} limit The length of the returned array or string. If the `limit` number
		 *     is positive, `limit` number of items from the beginning of the source array/string are copied.
		 *     If the number is negative, `limit` number  of items from the end of the source array/string
		 *     are copied. The `limit` will be trimmed if it exceeds `array.length`
		 * @return {Array|string} A new sub-array or substring of length `limit` or less if input array
		 *     had less than `limit` elements.
		 */

		_limitTo: function(input, limit) {

			function isArray(value) {
				return toString.call(value) === '[object Array]';
			}

			function isString(value) {
				return typeof value === 'string';
			}

			function int(str) {
				return parseInt(str, 10);
			}

			if (!isArray(input) && !isString(input)) return input;

			limit = int(limit);

			if (isString(input)) {
				//NaN check on limit
				if (limit) {
					return limit >= 0 ? input.slice(0, limit) : input.slice(limit, input.length);
				} else {
					return "";
				}
			}

			var out = [],
				i, n;

			// if abs(limit) exceeds maximum length, trim it
			if (limit > input.length)
				limit = input.length;
			else if (limit < -input.length)
				limit = -input.length;

			if (limit > 0) {
				i = 0;
				n = limit;
			} else {
				i = input.length + limit;
				n = input.length;
			}

			for (; i < n; i++) {
				out.push(input[i]);
			}

			return out;
		},

		/**
		 * Transform an array of strings into a readable list
		 * @param  {array} input
		 * @return {string} array elements delimited by a comma
		 */

		_list: function(input, space) {
			return input.join("," + (space || ""));
		},

		/**
		 * Transform an input string to lowercase
		 * @param  {string} input
		 * @return {string} lowercase string
		 */
		_lowercase: function(input) {
			return input.toLowerCase();
		},

		/**
		 * Left trim a string, removing leading whitespace
		 * @param  {string} input
		 * @return {string} left trimmed string
		 */
		_ltrim: function(input) {
			return input.replace(/^\s+/, '');
		},

		/**
		 * Transform an object into an array of its enumerable property names. If `obj`
		 * is null or undefined, it is returned unchanged.
		 * @param  {object} obj
		 * @return {array}
		 */
		_objectKeys: function(obj) {
			return obj === null ? obj : Object.keys(obj);
		},

		_orderBy: function(array, columnsToOrderBy, reverse) {
			if (!Array.isArray(array)) {
				return array;
			}
			if (!columnsToOrderBy) {
				return array;
			}
			if (typeof columnsToOrderBy === 'string') {
				columnsToOrderBy = [columnsToOrderBy];
			}

			// on a column-by-column basis, determine if descending order is desired
			var reverseSortValues = [];
			columnsToOrderBy.forEach(function(element, index) {
				if (element[0] == '-') {
					columnsToOrderBy[index] = element.substr(1);
					reverseSortValues.push(true);
				} else {
					reverseSortValues.push(false);
				}
			});

			// temporary holder of position and sort-values
			var map = array.map(function(element, index) {
				var sortValues = columnsToOrderBy.map(function(key) {
					if (typeof element[key] === 'string') {
						return element[key].toLowerCase();
					}

					return element[key];
				});

				return {
					index: index,
					sortValues: sortValues
				};
			});

			// sorting the map containing the reduced values
			map.sort(function(a, b) {
				var length = a.sortValues.length;

				for (var i = 0; i < length; i++) {
					if (reverseSortValues[i] === false) {
						if (a.sortValues[i] < b.sortValues[i])
							return -1;
						else if (a.sortValues[i] > b.sortValues[i])
							return 1;
					} else {
						if (a.sortValues[i] > b.sortValues[i])
							return -1;
						else if (a.sortValues[i] < b.sortValues[i])
							return 1;
					}
				}

				return 0;
			});

			if (reverse === true) {
				map.reverse();
			}

			// container for the resulting order
			var result = map.map(function(element) {
				return array[element.index];
			});

			return result;
		},

		/**
		 * Return a random item in a supplied array
		 * @param  {array} array
		 * @return {item}
		 */
		_random: function(array) {
			return array[Math.floor(Math.random() * array.length)];
		},

		_replace: function(str, old, new_, maxCount) {
			var res = str;
			var last = res;
			var count = 1;
			res = res.replace(old, new_);

			while (last != res) {
				if (count >= maxCount) {
					break;
				}

				last = res;
				res = res.replace(old, new_);
				count++;
			}

			return res;
		},

		/**
		 * Reverse a supplied value
		 * @param  {string} input
		 * @return {string} reversed string
		 */
		_reverse: function(input) {
			input = input || '';
			var out = "";
			for (var i = 0; i < input.length; i++) {
				out = input.charAt(i) + out;
			}
			return out;
		},

		/**
		 * Round a number by a specific precision or method
		 * @param  {integer} val
		 * @param  {integer} precision
		 * @param  {string} method
		 * @return {string}
		 */
		_round: function(val, precision, method) {
			precision = precision || 0;
			var factor = Math.pow(10, precision);
			var rounder;

			if (method == 'ceil') {
				rounder = Math.ceil;
			} else if (method == 'floor') {
				rounder = Math.floor;
			} else {
				rounder = Math.round;
			}

			return rounder(val * factor) / factor;
		},

		/**
		 * Right trim a string, removing trailing whitespace
		 * @param  {string} input
		 * @return {string} left trimmed string
		 */
		_rtrim: function(input) {
			return input.replace(/\s+$/, '');
		},

		/**
		 * Filter items that begin with a specific letter
		 * @param  {array} items
		 * @param  {string} letter
		 * @return {array}
		 */
		_startsWith: function(items, letter) {
			var filtered = [];
			var letterMatch = new RegExp(letter, 'i');
			for (var i = 0; i < items.length; i++) {
				var item = items[i];
				if (letterMatch.test(item.name.substring(0, 1))) {
					filtered.push(item);
				}
			}
			return filtered;
		},

		/**
		 * Return a title-cased version of the input
		 * @param  {string} input
		 * @return {string}
		 */
		_titlecase: function(input) {
			var words = input.split(' ');
			for (var i = 0; i < words.length; i++) {
				var ret = words[i].toLowerCase();
				words[i] = ret.charAt(0).toUpperCase() + ret.slice(1);
			}
			return words.join(' ');
		},

		/**
		 * Trim a string, removing leading and trailing whitespace
		 * @param  {string} input
		 * @return {string} trimmed string
		 */
		_trim: function(input) {
			return input && input.replace(/^\s*|\s*$/g, '');
		},

		/**
		 * Return a truncated version of a string
		 * @param  {string} input
		 * @param  {integer} length
		 * @param  {boolean} killwords
		 * @param  {string} end
		 * @return {string}
		 */
		_truncate: function(input, length, killwords, end) {
			var orig = input;
			length = length || 255;
			if (!input) {
				return;
			}
			if (input.length <= length)
				return input;

			if (killwords) {
				input = input.substring(0, length);
			} else {
				var idx = input.lastIndexOf(' ', length);
				if (idx === -1) {
					idx = length;
				}

				input = input.substring(0, idx);
			}

			input += (end !== undefined && end !== null) ? end : '...';
			return input;
		},

		/**
		 * Returns the Type of an expression
		 * @param  {[any data type]} input
		 * @return {string}
		 *
		 *Thanks to: https://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
		 */
		_typeof: function(input) {
			return ({}).toString.call(input).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
		},

		/**
		 * Return  Underscored `str`.
		 * @param  {string} str
		 * @return {string}
		 */
		_underscore: function(str) {
			return str.replace(/([a-z\d])([A-Z])/g, '$1_$2').toLowerCase();
		},

		/**
		 * Transform an input string to uppercase
		 * @param  {string} input
		 * @return {string} uppercase string
		 */
		_uppercase: function(input) {
			return input.toUpperCase();
		},

		/**
		 * Calculates word-count for a given string
		 * @param  {string} str
		 * @return {integer}
		 */
		_wordcount: function(input) {
			var words = (input) ? input.match(/\w+/g) : null;
			return (words) ? words.length : null;
		},

		/**
		 * Serializes input into a JSON-formatted string
		 * @param  {object} object
		 * @param  {number} spacing
		 * @return {string}
		 */
		_json: function(object, spacing) {

			function toJson(obj, pretty) {
				if (typeof obj === 'undefined') return undefined;
				if (isNaN(pretty)) {
					pretty = pretty ? 2 : null;
				}
				return JSON.stringify(obj, null, pretty);
			}

			if (typeof spacing === 'undefined') {
				spacing = 2;
			}

			return toJson(object, spacing);
		}
	};

	global.PolymerFilters = new PolymerFilters();

})(this);


text:
const deep = require('deep-get-set')
const DevNull = require('dev-null-stream')

module.exports = function (app) {
  function createPipedProvider (providerConfig, nmea0183Listener) {
    var result = {
      id: providerConfig.id,
      pipeElements: providerConfig.pipeElements.reduce((result, config) => {
        if (typeof config.enabled === 'undefined' || config.enabled) {
          result.push(createPipeElement(config))
        }
        return result
      }, [])
    }

    result.pipeElements.forEach(function (pipeElement) {
      if (typeof pipeElement.on === 'function') {
        pipeElement.on('nmea0183', nmea0183Listener)
      }
    })

    for (var i = result.pipeElements.length - 2; i >= 0; i--) {
      result.pipeElements[i].pipe(result.pipeElements[i + 1])
    }

    result.pipeElements[result.pipeElements.length - 1].pipe(new DevNull())
    result.pipeElements[result.pipeElements.length - 1].on('data', msg => {
      app.handleMessage(providerConfig.id, msg)
    })
    app.emit('pipedProvidersStarted', providerConfig)
    return result
  }

  function createPipeElement (elementConfig) {
    var options = elementConfig.options || {}
    options.app = app

    if (elementConfig.optionMappings) {
      elementConfig.optionMappings.forEach(function (mapping) {
        if (deep(app, mapping.fromAppProperty)) {
          options[mapping.toOption] = deep(app, mapping.fromAppProperty)
        }
      })
    }
    try {
      return new (require(elementConfig.type))(options)
    } catch (e) {
      return new (require(__dirname + '/../' + elementConfig.type))(options)
    }
  }

  function startProviders () {
    var nmea0183Listener = function (sentence) {
      app.signalk.emit('nmea0183', sentence)
    }
    if (app.config.settings.pipedProviders) {
      piped = app.config.settings.pipedProviders.reduce((result, config) => {
        if (typeof config.enabled === 'undefined' || config.enabled) {
          result.push(createPipedProvider(config, nmea0183Listener))
        }
        return result
      }, [])

      return piped.filter(function (n) {
        return n != null
      })
    } else {
      console.error('No pipedProviders in the settings file')
      return []
    }
  }

  function restartProviders () {
    /*
    app.providers.forEach(function (providerHolder) {
      providerHolder.pipeElements[0].end()
    })
    app.providers = startProviders()
    */
  }

  return (api = {
    start: startProviders
  })
}


text:
package com.bill.crawler.site.cto;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.bill.crawler.config.SystemConfig;
import com.bill.crawler.core.BasePageHandler;
import com.bill.crawler.engine.JsoupEngine;
import com.bill.crawler.global.PageTagConst;
import com.bill.crawler.utility.CommonUtil;
import com.bill.crawler.utility.FilterUtil;
import com.bill.crawler.utility.LogUtil;
import com.csvreader.CsvWriter;

public class CtotPageHandler extends BasePageHandler {

	private static String NAME = "姓名";
	private static String SEX = "性别";
	private static String LOCATION = "现居住地";
	private static String QQ = "QQ";
	private static String DATE = "注册时间";

	private boolean EnableDebug = true;
	private String DebugPageUrl = "wKiom1Y8laHB5C7eAAEjK3Ml8GU546.jpg";

	public CtotPageHandler() {
		super();
	}

	@Override
	public boolean allowVisitor(String url) {
		boolean retVal = false;

		// for debug only
		/*
		 * if(url.contains("1335075")){ logger.info("url:" + url); }
		 */

		if (FilterUtil.isImageLink(url)) {
			retVal = true;
		}

		if (!retVal && url.startsWith(getAuthor().getDomain())) {
			retVal = true;
		}

		if (retVal) {
			logger.info("allowed to visit:" + url);
		} else {
			logger.info("not allowed to visit:" + url);
		}

		return retVal;
	}

	@Override
	protected boolean allowHandler() {
		String parentUrl = stat.getParentUrl();
		String url = stat.getUrl();

		// let breakpoint work
		if (EnableDebug) {
			if (url.contains(DebugPageUrl)) {
				logger.info("url:" + url);
			}
		}

		// parent may be null
		if (CommonUtil.isEmptyString(parentUrl)) {
			logger.info("not allowed to handle since parant is null:" + url);
			return false;
		}

		if (stat.isValidImageLink()) {
			logger.info("allowed to handle image:" + url);
			return true;
		} else if (stat.isImageLink()) {
			// means small image
			return false;
		}

		// the page with the title named "51CTO技术家园" is for login, just ignore.
		if (stat.getTitle() != null && stat.getTitle().equals(CtoConfig.SiteExtra_51cto_Title)) {
			logger.info("not allowed to handle login page:" + url);
			return false;
		}

		if (url.startsWith(getAuthor().getDomain())) {// formerly parentUrl
														// instead of url,
														// should be wrong?
			if (FilterUtil.isSepcailPage(url)) {
				return false;
			}

			logger.info("allowed to handle since path is correct:" + url);
			return true;
		} else {
			logger.info("not allowed to handle since path is not correct:" + url);
			return false;
		}
	}

	@Override
	protected boolean extractor(String url, String html) {
		if (getAuthor().isFetchBlog()) {
			return extractorForBlog(url, html);
		} else {
			return extractorForProfile(url, html);
		}
	}

	private boolean extractorForBlog(String url, String html) {

		Document doc = Jsoup.parse(html);

		Elements elements = doc.select("div.showContent");
		if (elements == null) {
			return true;
		}
		Element showContentElement = elements.first();
		if (showContentElement == null) {
			return true;
		}

		elements = doc.select("div.showTitle");
		Element titleElement = null;
		if (elements != null && elements.size() > 0) {
			titleElement = elements.first();
		}

		Document newDoc = null;
		if (SystemConfig.isSavePureContent()) {
			newDoc = JsoupEngine.createNewDoc();
			Elements headTitleElements = newDoc.getElementsByTag("title");
			if (headTitleElements != null && headTitleElements.size() > 0) {
				Element headTitleElement = headTitleElements.first();
				headTitleElement.text(titleElement.text());
			}
			newDoc.body().append(titleElement.html());
			newDoc.body().append(showContentElement.html());
		} else {
			newDoc = JsoupEngine.getDocFromFile(getConfig().getTemplate(), getConfig().getEncoding());

			Element newShowContentElement = newDoc.select("div.showBox").first();

			// change title
			if (titleElement != null) {

				// change title in head part
				Elements headTitleElements = newDoc.getElementsByTag("title");
				if (headTitleElements != null && headTitleElements.size() > 0) {
					Element headTitleElement = headTitleElements.first();
					headTitleElement.text(titleElement.text());
				}

				// change title in content part
				newShowContentElement.appendChild(titleElement);
			}

			// change content
			newShowContentElement.appendChild(showContentElement);
		}

		pageContent = newDoc.html().getBytes();

		return true;
	}

	private boolean extractorForProfile(String url, String html) {
		boolean retVal = false;
		String str = null;

		Document doc = Jsoup.parse(html);

		// name and visit amount
		Elements contents = doc.select("#host_name");
		String name = null;
		int visitAmount = 0;
		if (contents != null && contents.first() != null) {
			str = contents.first().text();

			if (str != null && str.isEmpty() == false) {

				String[] tempStrArray = str.split(" ");

				String visitNum = null;
				if (tempStrArray.length > 3) {
					name = tempStrArray[0];
					visitNum = tempStrArray[3];
				}

				if (visitNum == null || visitNum.isEmpty()) {
					return retVal;
				} else {
					visitAmount = Integer.parseInt(visitNum);
					map.put(PageTagConst.nameTag, name);
					map.put(PageTagConst.visitNumTag, visitNum);
				}
			}
		}

		// check whether continue
		if (visitAmount < 100) {
			return retVal;
		} else {
			logger.info(name + ": " + visitAmount);
		}

		// to parse other info

		Elements info = doc.select("div.info");
		Elements list = info.select("li");

		int size = list.size();

		// one by one
		/*
		 * Element sexE = list.get(0); sex = sexE.children().get(1).text();
		 *
		 * Element locationE = list.get(1); location =
		 * locationE.children().get(1).text(); Element qqE = list.get(2); qq =
		 * qqE.children().get(1).text(); Element dateE = list.get(3); date =
		 * dateE.children().get(1).text();
		 */

		for (int i = 0; i < size; i++) {
			Element item = list.get(i);
			String key = item.children().get(0).text();
			str = item.children().get(1).text();

			if (key.contains(NAME)) {
				if (!str.isEmpty()) {
					map.put(PageTagConst.nameTag, str);// override the name
				}

			} else if (key.contains(SEX)) {
				map.put(PageTagConst.sexTag, str);
			} else if (key.contains(LOCATION)) {
				map.put(PageTagConst.locationTag, str);
			} else if (key.contains(QQ)) {
				map.put(PageTagConst.qqTag, str);
			} else if (key.contains(DATE)) {
				map.put(PageTagConst.dateTag, str);
			}
		}

		return true;
	}

	@Override
	protected void saveCSV() {
		if (getAuthor().isFetchBlog()) {
			super.saveCSV();
		} else {
			try {
				File csv = getConfig().getCsv();
				CsvWriter cw = new CsvWriter(new FileWriter(csv, true), ',');

				saveCommonCSV(cw);

				cw.write((String) map.get(PageTagConst.nameTag));
				cw.write((String) map.get(PageTagConst.visitNumTag));
				cw.write((String) map.get(PageTagConst.locationTag));
				cw.write((String) map.get(PageTagConst.qqTag));
				cw.write((String) map.get(PageTagConst.sexTag));
				cw.write((String) map.get(PageTagConst.dateTag));

				cw.endRecord();
				cw.close();
			} catch (IOException e) {
				LogUtil.logger.error(e.toString());
			}
		}

	}

}


text:
"""A library that is used to support Cloud Pub/Sub commands."""

import abc
import re

from googlecloudsdk.api_lib.util import exceptions as sdk_ex
from googlecloudsdk.core import properties
from googlecloudsdk.core.resource import resource_projector

# Maximum number of results that can be passed in pageSize to list operations.
MAX_LIST_RESULTS = 10000

# Regular expression to match full paths for Cloud Pub/Sub resource identifiers.
# TODO(user): These are going away, since we are moving to
# collection paths in CL/125390647.
PROJECT_PATH_RE = re.compile(r'^projects/(?P<Project>[^/]+)$')
SNAPSHOTS_PATH_RE = re.compile(
    r'^projects/(?P<Project>[^/]+)/snapshots/(?P<Resource>[^/]+)$')
SUBSCRIPTIONS_PATH_RE = re.compile(
    r'^projects/(?P<Project>[^/]+)/subscriptions/(?P<Resource>[^/]+)$')
TOPICS_PATH_RE = re.compile(
    r'^projects/(?P<Project>[^/]+)/topics/(?P<Resource>[^/]+)$')

# Collection for various subcommands.
TOPICS_COLLECTION = 'pubsub.projects.topics'
TOPICS_PUBLISH_COLLECTION = 'pubsub.topics.publish'
SNAPSHOTS_COLLECTION = 'pubsub.projects.snapshots'
SNAPSHOTS_LIST_COLLECTION = 'pubsub.snapshots.list'
SUBSCRIPTIONS_COLLECTION = 'pubsub.projects.subscriptions'
SUBSCRIPTIONS_ACK_COLLECTION = 'pubsub.subscriptions.ack'
SUBSCRIPTIONS_LIST_COLLECTION = 'pubsub.subscriptions.list'
SUBSCRIPTIONS_MOD_ACK_COLLECTION = 'pubsub.subscriptions.mod_ack'
SUBSCRIPTIONS_MOD_CONFIG_COLLECTION = 'pubsub.subscriptions.mod_config'
SUBSCRIPTIONS_PULL_COLLECTION = 'pubsub.subscriptions.pull'
SUBSCRIPTIONS_SEEK_COLLECTION = 'pubsub.subscriptions.seek'

# TODO(b/32275946): Use core.resources.Resource instead of this custom class.
class ResourceIdentifier(object):
  """Base class to build resource identifiers."""
  __metaclass__ = abc.ABCMeta

  @abc.abstractmethod
  def _RegexMatch(self, resource_path):
    """Return a match object from applying a regexp to this resource identifier.

    This function needs to be overriden in subclasses to use the appropriate
    regular expression for a resource identifier type (subscriptions, topics).

    Args:
      resource_path: (string) Full (ie. projects/my-proj/topics/my-topic)
                     or partial (my-topic) project or resource path.
    """
    pass

  @abc.abstractmethod
  def _ResourceType(self):
    """Returns the valid resource identifier type for this instance.

    This function needs to be overriden in subclasses to return a valid
    resource identifier type (subscriptions, topics, or snapshots).
    """
    pass

  def __init__(self, *args, **kwargs):
    self.Parse(*args, **kwargs)

  def Parse(self, resource_path, project_path=''):
    """Initializes a new ResourceIdentifier.

    Args:
      resource_path: (string) Full (e.g., projects/my-proj/topics/my-topic)
                     or partial (my-topic) resource path.
      project_path: (string) Full (projects/my-project) or
                    partial (my-project) project path.
                    If empty, the SDK environment default
                    (gcloud config set project) will be used.
    Returns:
      A ResourceIdentifier instance that captures the subcomponents of the
      resource identifier.

    Raises:
      HttpException if the provided resource path is not a valid resource
      path/name.
    """
    if '/' in resource_path:
      match = self._RegexMatch(resource_path)
      if match is None:
        raise sdk_ex.HttpException(
            'Invalid {0} Identifier'.format(self._ResourceType().capitalize()))

      self.project = ProjectIdentifier(match.groupdict()['Project'])
      self.resource_name = match.groupdict()['Resource']
      return

    self.project = ProjectIdentifier(project_path)
    self.resource_name = resource_path

  def GetFullPath(self):
    return '{0}/{1}/{2}'.format(self.project.GetFullPath(),
                                self._ResourceType(),
                                self.resource_name)

class ProjectIdentifier(ResourceIdentifier):
  """Represents a Cloud project identifier."""

  def Parse(self, project_path=''):
    """Initializes a new ProjectIdentifier.

    Args:
      project_path: (string) Full (projects/my-proj) or partial (my-proj)
                    project path.
                    If empty, the SDK environment default
                    (gcloud config set project) will be used.
    Returns:
      An instantiated ProjectIdentifier with correct project information.

    Raises:
      HttpException if the provided project path is not a valid project
      path/name or if a default project have not been set.
    """
    if not project_path:
      self.project_name = properties.VALUES.core.project.Get(required=True)
      return

    if '/' in project_path:
      match = self._RegexMatch(project_path)
      if match is None:
        raise sdk_ex.HttpException('Invalid Project Identifier')

      self.project_name = match.groupdict()['Project']
      return

    self.project_name = project_path

  def _ResourceType(self):
    return 'projects'

  def _RegexMatch(self, resource_path):
    return PROJECT_PATH_RE.match(resource_path)

  def GetFullPath(self):
    """Returns a valid full project path."""
    return '{0}/{1}'.format(self._ResourceType(), self.project_name)

class SnapshotIdentifier(ResourceIdentifier):
  """Represents a Cloud Pub/Sub snapshot identifier."""

  def _RegexMatch(self, resource_path):
    return SNAPSHOTS_PATH_RE.match(resource_path)

  def _ResourceType(self):
    return 'snapshots'

class SubscriptionIdentifier(ResourceIdentifier):
  """Represents a Cloud Pub/Sub subscription identifier."""

  def _RegexMatch(self, resource_path):
    return SUBSCRIPTIONS_PATH_RE.match(resource_path)

  def _ResourceType(self):
    return 'subscriptions'

class TopicIdentifier(ResourceIdentifier):
  """Represents a Cloud Pub/Sub topic identifier."""

  def _RegexMatch(self, resource_path):
    return TOPICS_PATH_RE.match(resource_path)

  def _ResourceType(self):
    return 'topics'

def ProjectFormat(project_name=''):
  return ProjectIdentifier(project_name).GetFullPath()

def TopicFormat(topic_name, topic_project=''):
  """Formats a topic name as a fully qualified topic path.

  Args:
    topic_name: (string) Name of the topic to convert.
    topic_project: (string) Name of the project the given topic belongs to.
                   If not given, then the project defaults to the currently
                   selected cloud project.

  Returns:
    Returns a fully qualified topic path of the
    form project/foo/topics/topic_name.
  """
  return TopicIdentifier(topic_name, topic_project).GetFullPath()

def SubscriptionFormat(subscription_name, project_name=''):
  """Formats a subscription name as a fully qualified subscription path.

  Args:
    subscription_name: (string) Name of the subscription to convert.
    project_name: (string) Name of the project the given subscription belongs
                  to. If not given, then the project defaults to the currently
                  selected cloud project.

  Returns:
    Returns a fully qualified subscription path of the
    form project/foo/subscriptions/subscription_name.
  """
  return SubscriptionIdentifier(subscription_name, project_name).GetFullPath()

def SnapshotFormat(snapshot_name, project_name=''):
  """Formats a snapshot name as a fully qualified snapshot path.

  Args:
    snapshot_name: (string) Name of the snapshot to convert.
    project_name: (string) Name of the project the given snapshot belongs
                  to. If not given, then the project defaults to the currently
                  selected cloud project.

  Returns:
    Returns a fully qualified snapshot path of the form
    project/foo/snapshots/snapshot_name.
  """
  return SnapshotIdentifier(snapshot_name, project_name).GetFullPath()

# TODO(b/32276674): Remove the use of custom *DisplayDict's.
def TopicDisplayDict(topic, error_msg=''):
  """Creates a serializable from a Cloud Pub/Sub Topic operation for display.

  Args:
    topic: (Cloud Pub/Sub Topic) Topic to be serialized.
    error_msg: (string) An error message to be added to the serialized
               result, if any.
  Returns:
    A serialized object representing a Cloud Pub/Sub Topic
    operation (create, delete).
  """
  topic_display_dict = resource_projector.MakeSerializable(topic)
  topic_display_dict['topicId'] = topic.name
  topic_display_dict['success'] = not error_msg
  topic_display_dict['reason'] = error_msg or ''
  del topic_display_dict['name']

  return topic_display_dict

def SubscriptionDisplayDict(subscription, error_msg=''):
  """Creates a serializable from a Cloud Pub/Sub Subscription op for display.

  Args:
    subscription: (Cloud Pub/Sub Subscription) Subscription to be serialized.
    error_msg: (string) An error message to be added to the serialized
               result, if any.
  Returns:
    A serialized object representing a Cloud Pub/Sub Subscription
    operation (create, delete, update).
  """
  push_endpoint = ''
  subscription_type = 'pull'
  if subscription.pushConfig:
    if subscription.pushConfig.pushEndpoint:
      push_endpoint = subscription.pushConfig.pushEndpoint
      subscription_type = 'push'

  return {
      'subscriptionId': subscription.name,
      'topic': subscription.topic,
      'type': subscription_type,
      'pushEndpoint': push_endpoint,
      'ackDeadlineSeconds': subscription.ackDeadlineSeconds,
      'retainAckedMessages': bool(subscription.retainAckedMessages),
      'messageRetentionDuration': subscription.messageRetentionDuration,
      'success': not error_msg,
      'reason': error_msg or '',
  }

def SnapshotDisplayDict(snapshot, error_msg=''):
  """Creates a serializable from a Cloud Pub/Sub Snapshot operation for display.

  Args:
    snapshot: (Cloud Pub/Sub Snapshot) Snapshot to be serialized.
    error_msg: (string) An error message to be added to the serialized
               result, if any.
  Returns:
    A serialized object representing a Cloud Pub/Sub Snapshot operation (create,
    delete).
  """
  return {
      'snapshotId': snapshot.name,
      'topic': snapshot.topic,
      'expirationTime': snapshot.expirationTime,
      'success': not error_msg,
      'reason': error_msg or '',
  }


text:
REGISTRY_VERSION?=2.3.6
REPO?=853478862498.dkr.ecr.eu-west-1.amazonaws.com
IMAGE?=epimorphics/loc-registry

.PHONY: push
push: build just-push

.PHONY: build
build:
	docker build --build-arg REGISTRY_VERSION=$(REGISTRY_VERSION) -t "$(IMAGE):latest" .
	docker tag "$(IMAGE):latest" "$(REPO)/$(IMAGE):$(REGISTRY_VERSION)"

.PHONY: just-push
just-push:
	docker push "$(REPO)/$(IMAGE):$(REGISTRY_VERSION)"


text:
<?php

declare(strict_types=1);

namespace Ekino\NewRelicBundle\Exception;

/**
 * Exception dedicated to report Deprecation.
 */
class DeprecationException extends \ErrorException
{
}


text:
jQuery Template is a jQuery plugin that makes using templates easy and quick. The plugin supports loading HTML files as templates, or taking a jQuery object as the template (usually using script tags to hold the template).

## Features

jQuery.LoadTemplate provides the following:

- Define Templates to display data
- Provide Formatters to process data into a more readable format
- Cache templates and data client side and pass processing to the client, allowing for a great user experience.
- Powerful but simple syntax, utilising pure html for templates.

## Getting Started

Simply clone the repo. The only file required is the jquery.loadTemplate-version.js file in the jquery.loadTemplate folder. There is also a folder for examples in the same folder. Take a look at the index file in here for examples and the code to create the examples.

To see examples of usage, visit the project page: [http://codepb.github.io/jquery-template/](http://codepb.github.io/jquery-template/)

## Potential Applications

jQuery.loadTemplate was originally designed with a single page application for a blog in mind. The idea was to create templates for blog posts, post snippets, etc. This could then be called from the client when required, and cached. The post data was sent as a JSON object from the server, and processed into the templates using the plugin. This meant a very light load on the server, and a great user experience, with smooth page transitions, and JavaScript engines doing all the work.

However I saw many other potential applications for this. Any application that deals with a large amount of data displayed in a regular format, for example search results, live commentary, blogs, online stores, social media sites, and the list could go on.

## How it works

The plugin parses a template using data attributes to populate the data. Simply pass in a JavaScript object, and the plugin does the rest.

An example template is below:

And to use this do the following:

    $("#template-container").loadTemplate($("#template"),
		{
            author: 'Joe Bloggs',
            date: '25th May 2013',
            authorPicture: 'Authors/JoeBloggs.jpg',
            post: 'This is the contents of my post'
        });

Similarly the content of the template could be held in a separate html file without the enclosing script tag, and used like the following:

    $("#template-container").loadTemplate("Templates/template.html",
		{
            author: 'Joe Bloggs',
            date: '25th May 2013',
            authorPicture: 'Authors/JoeBloggs.jpg',
            post: 'This is the contents of my post'
        });

The plugin has a number of data-... attributes that can be used to populate various attributes with the data. There is also the powerful data-template-bind attribute that accepts a JSON object, enabling binding to any attribute, or the content of the element.

### Data Formatters

It is also possible to define data formatters. These are assigned through the `$.addTemplateFormatter` method. This function either accepts a map of functions and the keys that they will be referenced by, or a single function with a single key as two separate parameters. Each formatter takes two values, the value being assigned, and a template to use to define how this data is displayed. The data-format-template may be empty. Example usage of this is below:

    $.addTemplateFormatter("UpperCaseFormatter",
        function(value, template) {
            return value.toUpperCase();
        });

Alternatively with a map:

    $.addTemplateFormatter({
        UpperCaseFormatter : function(value, template) {
                return value.toUpperCase();
            },
        LowerCaseFormatter : function(value, template) {
                return value.toLowerCase();
            },
        SameCaseFormatter : function(value, template) {
                if(template == "upper") {
					return value.toUpperCase();
				} else {
					return value.toLowerCase();
				}
            }
    });

To call these formatters, simply the following will work:

Formatters must be added before they are used else a template will not be able to access them. Formatters are used at the time of populating the data. You can also target any binding with the "data-format-target". The value of this is the binding to target so to target data-alt binding, set 'data-format-target="alt"'.

### Bindings
There are a number of different bindings and ways to bind the data. The following attributes are available:

- "data-innerHTML" (<= 1.4.5) - binds the value supplied to the content (innerHTML) of the element (uses $(elem).html(value))
- "data-content" - alias for the newer "data-innerHTML"
- "data-content-text" - binds the value supplied to the content of the element as text (uses $(elem).text(value))
- "data-content-append" - appends the value to the end of the element (uses $(elem).append(value))
- "data-content-prepend" - prepends the value to the beginning of the element (uses $(elem).prepend(value))
- "data-id" - sets the id of the element to the value provided (uses $(elem).attr("id", value));
- "data-href" - sets the href value of the element to the value provided (uses $(elem).attr("href", value));
- "data-alt" - sets the alt value of the element to the value provided (uses $(elem).attr("alt", value));
- "data-value" - sets the value attribute of the element to the value provided (uses $(elem).val(value))
- "data-class" - sets the class attribute of the element to the value provided (uses $(elem).class(value))
- "data-link" - sets the innerHtml of the element to be a link to the value provided (wraps the content in an <a> tag).
- "data-link-wrap" - wraps the element in a link to the value provided. Same as "data-link", but the <a> tag wraps the element as well as the content.
- "data-options" - adds options to a select box. The value for this should reference an array of strings, each option will be output as a separate option. The value will be the same as the displayed text for each option. For a more powerful version of this look at the data-template-bind option.

On top of the attributes above, there is also a "data-template-bind" attribute. This is designed to handle more complex situations and allows a wide range of control. The attribute takes a JSON string and allows multiple bindings and options to be set in the one attribute.

The "data-template-bind" value should be an array of objects. Each object represents one complete binding. Each object can contain the following properties:

- "value" (required) - The property representing the value to bind to.
- "attribute" (required) - The attribute to bind to. This can be any attribute accepted by the jQuery.attr() method or one of the following: "content" - same as data-content, binds the innerHTML, "contentAppend" - same as data-append, appends the value, "contentPrepend" - same as data-prepend, prepends the value, "options" - same as data-options, but provides greater control. The value attribute for this is an object with a value property and a content property, and this will bind the value of the option to the value property, and the innerText of the option to the content property.
- "formatter" (optional) - provides the formatter to apply to the specific binding. Multiple different attributes can use different formatters using this syntax.
- "formatOptions" (optional) - the options to pass to the formatter applied.

An example of using the "data-template-bind" attribute would be the following:

### Options

There are a number of options the plugin accepts. These can be set by passing an object containing the settings you would like to set as the third parameter to .loadTemplate:

    $(container).loadTemplate(template, data, { append: true, elemPerPage: 20 });

The full list of options are:

- "overwriteCache" (default false) - Whether to ignore the cache and reload the template (if you've previously loaded the template, but it might have changed, you'll want to set this to true.
- "async" (default true) - Whether to load templates asynchronously (if templates require an Ajax call)
- "complete" (default null) - Callback function to call on complete. Will always be called regardless of success or failure.
- "success" (default null) - Callback function to call on successful completion.
- "error" (default, outputting error message to template container) - Callback function to call on error.
- "errorMessage" (default "There was an error loading the template.") - Error message for the default error callback to use. This will not be used if you set an error callback function.
- "isFile" (default undefined) - flag to help speed up the process of deciding where to load the template from. Set to true if the template is an external file to load via ajax, false if it's a jQuery selector for an element in the document. Default undefined means the plugin will check first in the document, then attempt to load external file.
- "paged" (default false) - A boolean flag to indicate whether arrays should be paged.
- "pageNo" (default 1) - An integer for which page to display if the data is being paged.
- "elemPerPage" (default 10) - The number of elements to display per page if the data is being paged.
- "append" (default false) - If set to true, the template will be appended to the element rather than replacing the contents of the element.
- "prepend" (default false) - If set to true, the template will be prepended to the element rather than replacing the contents of the element. The append option takes priority over prepend, so if both options are set to true, the element is appended and not prepended.
- "beforeInsert" (default null) - Callback function to be called before inserting the template into the document. The format of the function is function($elem) where $elem is the jQuery object of the populated template about to be inserted into the document.
- "afterInsert" (default null) - As above, a callback function to be called after inserting the template into the document. The format is the same as above.
- "bindingOptions" (default all flags false): add flags to ignore certain types of values. {"ignoreUndefined": false, "ignoreNull": false, "ignoreEmptyString": false}. The flags you set here, are overwritten on an element level by those specified in a template with a "data-binding-options" or a "data-template-bind" attribute. Examples can be found in the Examples/OptionalBinding folder.

## Future Plans

I would like to develop the plugin further so it would be possible to watch the objects holding the data, so any changes to the data would be reflected in the UI. This would have to be simple, lightweight, and ideally would work just with natural JavaScript objects. I also welcome any ideas as to how the plugin could be improved.


text:
var _ = require('underscore');
var assert = require('assert');
var path = require('path');

var CorporalTestRunner = require('./util/runner');

var _currentRunners = [];

/*!
 * Kill all runners that were created in the previous test
 */
afterEach(function(callback) {
    _.each(_currentRunners, function(runner) {
        runner.close();
    });
    _currentRunners = [];
    return callback();
});

describe('Command Loading', function() {

    it('loads commands from a directory', function(callback) {
        var runner = _createRunner({'commands': _commandDir('loads-commands-from-a-directory')});
        runner.start(function() {
            runner.exec('help', function(stdout, stderr) {
                assert.ok(!stderr);
                assert.notEqual(stdout.indexOf('help    :  Show a dialog of all available commands.'), -1);
                assert.notEqual(stdout.indexOf('quit    :  Quit the interactive shell.'), -1);
                assert.notEqual(stdout.indexOf('command1:  command1.'), -1);
                assert.notEqual(stdout.indexOf('command2:  command2.'), -1);
                return callback();
            });
        });
    });

    it('fails when a command without a description is encountered', function(callback) {
        var runner = _createRunner({'commands': _commandDir('fails-when-a-command-without-a-description-is-encountered')});
        runner.start(function() {});

        // Keep track of all stderr output from the process. We are listening for an error
        var stderr = '';
        runner.on('stderr', function(data) {
            stderr += data;
        });

        runner.on('close', function(code) {
            assert.strictEqual(code, 1);
            assert.notStrictEqual(stderr.indexOf('Command "no-description" must have a description string'), -1);
            return callback();
        });
    });

    it('fails when a command without an invoke method is encountered', function(callback) {
        var runner = _createRunner({'commands': _commandDir('fails-when-a-command-without-an-invoke-method-is-encountered')});
        runner.start(function() {});

        // Keep track of all stderr output from the process. We are listening for an error
        var stderr = '';
        runner.on('stderr', function(data) {
            stderr += data;
        });

        runner.on('close', function(code) {
            assert.strictEqual(code, 1);
            assert.notStrictEqual(stderr.indexOf('Command "no-invoke-method" must have an invoke function'), -1);
            return callback();
        });
    });

    it('does not load commands that are disabled', function(callback) {
        // Load from a directory with a failing command while disabling the failing command
        var runner1 = _createRunner({
            'commands': _commandDir('fails-when-a-command-without-an-invoke-method-is-encountered'),
            'disabled': ['no-invoke-method']
        });
        runner1.start(function() {
            runner1.exec('help', function(stdout, stderr) {
                assert.ok(!stderr);
                assert.strictEqual(stdout.indexOf('no-invoke-method'), -1);
                assert.notStrictEqual(stdout.indexOf('command1:  command1.'), -1);

                // Load another one, but disable both clear and no-invoke-method
                var runner2 = _createRunner({
                    'commands': _commandDir('fails-when-a-command-without-an-invoke-method-is-encountered'),
                    'disabled': ['clear', 'no-invoke-method']
                });
                runner2.start(function() {
                    runner2.exec('help', function(stdout) {
                        assert.ok(!stderr);
                        assert.strictEqual(stdout.indexOf('clear'), -1);
                        assert.strictEqual(stdout.indexOf('no-invoke-method'), -1);
                        assert.notStrictEqual(stdout.indexOf('command1:  command1.'), -1);
                        return callback();
                    });
                });
            });
        });
    });
});

describe('Built-In Commands', function() {

    describe('non-existing', function() {

        it('provides an error and lists commands when a non-existing command is entered', function(callback) {
            var runner = _createRunner();
            runner.start(function() {
                runner.exec('bleh', function(stdout, stderr) {
                    assert.ok(!stdout);
                    assert.notStrictEqual(stderr.indexOf('Invalid command:'), -1);
                    assert.notStrictEqual(stderr.indexOf('bleh'), -1);
                    return callback();
                });
            });
        });
    });

    describe('commented', function() {

        it('ignores commands starting with a hash', function(callback) {
            var runner = _createRunner();
            runner.start(function() {
                runner.exec('  # bleh', function(stdout, stderr) {
                    assert.ok(!stdout);
                    assert.ok(!stderr);
                    return callback();
                });
            });
        });
    });

    describe('clear', function() {

        it('executes the clear-screen control characters when invoked', function(callback) {
            var runner = _createRunner();
            runner.start(function() {
                runner.exec('clear', function(stdout, stderr) {
                    assert.ok(!stderr);
                    assert.strictEqual(stdout, '\u001B[2J\u001B[0;0f');
                    return callback();
                });
            });
        });
    });

    describe('help', function() {

        it('lists the help and quit command when run without arguments', function(callback) {
            var runner = _createRunner();
            runner.start(function() {
                runner.exec('help', function(stdout, stderr) {
                    assert.ok(!stderr);
                    assert.notStrictEqual(stdout.indexOf('clear:  Clear the terminal window.'), -1);
                    assert.notStrictEqual(stdout.indexOf('help :  Show a dialog of all available commands.'), -1);
                    assert.notStrictEqual(stdout.indexOf('quit :  Quit the interactive shell.'), -1);
                    return callback();
                });
            });
        });

        it('lists the help and usage of the quit command', function(callback) {
            var runner = _createRunner();
            runner.start(function() {
                runner.exec('help quit', function(stdout, stderr) {
                    assert.ok(!stderr);
                    assert.notStrictEqual(stdout.indexOf('Quit the interactive shell.'), -1);
                    return callback();
                });
            });
        });

        it('lists the help when using a --help argument', function(callback) {
            var runner = _createRunner();
            runner.start(function() {
                runner.exec('quit --help', function(stdout, stderr) {
                    assert.ok(!stderr);
                    assert.notStrictEqual(stdout.indexOf('Quit the interactive shell.'), -1);

                    // Assert with further arguments
                    runner.exec('quit --arg1 val1 --help', function(stdout, stderr) {
                        assert.ok(!stderr);
                        assert.notStrictEqual(stdout.indexOf('Quit the interactive shell.'), -1);
                        return callback();
                    });
                });
            });
        });

        it('lists the help and usage of the help command', function(callback) {
            var runner = _createRunner();
            runner.start(function() {
                runner.exec('help help', function(stdout, stderr) {
                    assert.ok(!stderr);
                    assert.notStrictEqual(stdout.indexOf('Show a dialog of all available commands.'), -1);
                    assert.notStrictEqual(stdout.indexOf('Usage: help [<command>]'), -1);
                    return callback();
                });
            });
        });

        it('hides commands from the command listing when configured to do so', function(callback) {
            // Hide the "clear" command from the index
            var runner = _createRunner({
                'env': {
                    'corporal_command_settings': {
                        'help': {
                            'hide': ['clear']
                        }
                    }
                }
            });
            runner.start(function() {
                runner.exec('help', function(stdout, stderr) {
                    assert.ok(!stderr);

                    // Ensure it shows help and quit
                    assert.notStrictEqual(stdout.indexOf('help:  Show a dialog of all available commands.'), -1);
                    assert.notStrictEqual(stdout.indexOf('quit:  Quit the interactive shell.'), -1);

                    // Ensure it does not show clear
                    assert.strictEqual(stdout.indexOf('clear'), -1);

                    // Ensure it does show the help for clear when specifically requested
                    runner.exec('help clear', function(stdout, stderr) {
                        assert.ok(!stderr);
                        assert.notStrictEqual(stdout.indexOf('Clear the terminal window.'), -1);
                        return callback();
                    });
                });
            });
        });
    });

    describe('quit', function() {

        it('quits the process', function(callback) {
            var runner = _createRunner();
            runner.start(function() {
                runner.exec('quit');
                runner.once('close', function(code, signal) {
                    assert.strictEqual(code, 0);
                    return callback();
                });
            });
        });
    });
});

describe('Error Handling', function() {

    it('handles precedence use-cases for error type and code', function(callback) {
        var runner = _createRunner({'commands': _commandDir('error-handler-resolution')});
        runner.start(function() {

            // Verify all precedence use-cases based on error type and code
            runner.exec('throw-typea-stringmatch', function(stdout) {
                assert.strictEqual(stdout, 'TypeAError: isastringmatch\n');
                runner.exec('throw-typea-regexpmatch', function(stdout) {
                    assert.strictEqual(stdout, 'TypeAError: isaregexpmatch\n');
                    runner.exec('throw-typea-functionmatch', function(stdout) {
                        assert.strictEqual(stdout, 'TypeAError: isafunctionmatch\n');
                        runner.exec('throw-typea-nomatch', function(stdout) {
                            assert.strictEqual(stdout, 'TypeAError: isanullmatch 0\n');
                            runner.exec('throw-typea-nocode', function(stdout) {
                                assert.strictEqual(stdout, 'TypeAError: isanullmatch 0\n');
                                runner.exec('throw-typea-stringmatch-ordermatters', function(stdout) {
                                    assert.strictEqual(stdout, 'TypeAError: teststringprecedence 0\n');
                                    runner.exec('throw-typea-regexpmatch-ordermatters', function(stdout) {
                                        assert.strictEqual(stdout, 'TypeAError: testregexpprecedence 0\n');
                                        runner.exec('throw-typea-functionmatch-ordermatters', function(stdout) {
                                            assert.strictEqual(stdout, 'TypeAError: testfunctionprecedence 0\n');
                                            runner.exec('throw-typeb-stringmatch', function(stdout) {
                                                assert.strictEqual(stdout, 'TypeBError: isastringmatch\n');
                                                runner.exec('throw-error-stringmatch', function(stdout) {
                                                    assert.strictEqual(stdout, 'Error: isastringmatch\n');
                                                    runner.exec('throw-catchall', function(stdout) {
                                                        assert.strictEqual(stdout, 'Error: catchall\n');

                                                        // Ensure we can still somewhat operate and exit properly
                                                        runner.exec('help', function(stdout) {
                                                            assert.notStrictEqual(stdout.indexOf('Clear the terminal window.'), -1);
                                                            assert.notStrictEqual(stdout.indexOf('Show a dialog of all available commands.'), -1);
                                                            assert.notStrictEqual(stdout.indexOf('Quit the interactive shell.'), -1);
                                                            runner.exec('quit');
                                                            runner.once('close', function(code, signal) {
                                                                assert.strictEqual(code, 0);
                                                                return callback();
                                                            });
                                                        });
                                                    });
                                                });
                                            });
                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            });
        });
    });
});

describe('Command Contexts', function() {
    it('only makes commands available that are scoped to the current context', function(callback) {
        var runner = _createRunner({
            'commands': _commandDir('command-contexts'),
            'contexts': {
                '': ['available-in-default-context'],
                '*': ['switch-context'],
                'contexta': ['available-in-contexta'],
                'contextb': ['available-in-contextb']
            }
        });

        runner.start(function() {
            // Ensure only the internal, * commands and those specified for the default context are available in the default context
            runner.exec('help', function(stdout, stderr) {
                assert.ok(!stderr);
                assert.notStrictEqual(stdout.indexOf('available-in-default-context:'), -1);
                assert.notStrictEqual(stdout.indexOf('switch-context              :'), -1);
                assert.notStrictEqual(stdout.indexOf('clear                       :'), -1);
                assert.notStrictEqual(stdout.indexOf('help                        :'), -1);
                assert.notStrictEqual(stdout.indexOf('quit                        :'), -1);
                assert.strictEqual(stdout.indexOf('contexta'), -1);
                assert.strictEqual(stdout.indexOf('contextb'), -1);

                // Ensure we can't invoke any of the commands out of context
                runner.exec('available-in-contexta', function(stdout, stderr) {
                    assert.ok(!stdout);
                    assert.notStrictEqual(stderr.indexOf('Invalid command'), -1);

                    // Ensure we can invoke the default context command
                    runner.exec('available-in-default-context', function(stdout, stderr) {
                        assert.ok(!stderr);

                        // Switch contexts
                        runner.exec('switch-context contexta', function(stdout, stderr) {

                            // Ensure we only get internal, * and contexta commands
                            runner.exec('help', function(stdout, stderr) {
                                assert.ok(!stderr);
                                assert.notStrictEqual(stdout.indexOf('available-in-contexta:'), -1);
                                assert.notStrictEqual(stdout.indexOf('switch-context       :'), -1);
                                assert.notStrictEqual(stdout.indexOf('clear                :'), -1);
                                assert.notStrictEqual(stdout.indexOf('help                 :'), -1);
                                assert.notStrictEqual(stdout.indexOf('quit                 :'), -1);
                                assert.strictEqual(stdout.indexOf('default'), -1);
                                assert.strictEqual(stdout.indexOf('contextb'), -1);

                                // Ensure we can't invoke the default context command
                                runner.exec('available-in-default-context', function(stdout, stderr) {
                                    assert.ok(!stdout);
                                    assert.notStrictEqual(stderr.indexOf('Invalid command'), -1);

                                    // Ensure we can now invoke contexta
                                    runner.exec('available-in-contexta', function(stdout, stderr) {
                                        assert.ok(!stderr);

                                        return callback();
                                    });
                                });
                            });
                        });
                    });
                });
            });
        });
    });
});

/*!
 * Creates a runner and keeps track of it to be closed after the
 * test.
 */
function _createRunner(options) {
    var runner = new CorporalTestRunner(options);
    _currentRunners.push(runner);
    return runner;
}

/*!
 * Convenience method to get a comamnds directory by bottom-level
 * folder name
 */
function _commandDir(dir) {
    return path.join(__dirname, 'commands', dir);
}


text:
package tccpf

import (
	"context"
	"fmt"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/cloudformation"
	"github.com/giantswarm/microerror"
	"github.com/giantswarm/operatorkit/v7/pkg/controller/context/finalizerskeptcontext"

	"github.com/giantswarm/aws-operator/v14/service/controller/controllercontext"
	"github.com/giantswarm/aws-operator/v14/service/controller/key"
)

func (r *Resource) EnsureDeleted(ctx context.Context, obj interface{}) error {
	cr, err := key.ToCluster(ctx, obj)
	if err != nil {
		return microerror.Mask(err)
	}
	cc, err := controllercontext.FromContext(ctx)
	if err != nil {
		return microerror.Mask(err)
	}

	{
		r.logger.Debugf(ctx, "disabling the termination protection of the tenant cluster's control plane finalizer cloud formation stack")

		i := &cloudformation.UpdateTerminationProtectionInput{
			EnableTerminationProtection: aws.Bool(false),
			StackName:                   aws.String(key.StackNameTCCPF(&cr)),
		}

		_, err = cc.Client.ControlPlane.AWS.CloudFormation.UpdateTerminationProtection(i)
		if IsDeleteInProgress(err) {
			r.logger.Debugf(ctx, "the tenant cluster's control plane finalizer cloud formation stack is being deleted")

			r.logger.Debugf(ctx, "keeping finalizers")
			finalizerskeptcontext.SetKept(ctx)

			r.logger.Debugf(ctx, "canceling resource")

			return nil

		} else if IsDeleteFailed(err) {
			r.logger.Debugf(ctx, "the tenant cluster's control plane finalizer cloud formation stack failed to delete")
			r.event.Emit(ctx, &cr, "CFDeleteFailed", fmt.Sprintf("the tenant cluster's control plane finalizer cloud formation stack has stack status %#q", cloudformation.StackStatusDeleteFailed))

			r.logger.Debugf(ctx, "keeping finalizers")
			finalizerskeptcontext.SetKept(ctx)

			r.logger.Debugf(ctx, "canceling resource")

			return nil

		} else if IsNotExists(err) {
			r.logger.Debugf(ctx, "the tenant cluster's control plane finalizer cloud formation stack does not exist")
			r.logger.Debugf(ctx, "canceling resource")

			return nil

		} else if err != nil {
			return microerror.Mask(err)
		}

		r.logger.Debugf(ctx, "disabled the termination protection of the tenant cluster's control plane finalizer cloud formation stack")
	}

	{
		r.logger.Debugf(ctx, "requesting the deletion of the tenant cluster's control plane finalizer cloud formation stack")

		i := &cloudformation.DeleteStackInput{
			StackName: aws.String(key.StackNameTCCPF(&cr)),
		}

		_, err = cc.Client.ControlPlane.AWS.CloudFormation.DeleteStack(i)
		if IsUpdateInProgress(err) {
			r.logger.Debugf(ctx, "the tenant cluster's control plane finalizer cloud formation stack is being updated")

			r.logger.Debugf(ctx, "keeping finalizers")
			finalizerskeptcontext.SetKept(ctx)

			r.logger.Debugf(ctx, "canceling resource")

			return nil

		} else if err != nil {
			return microerror.Mask(err)
		}

		r.logger.Debugf(ctx, "requested the deletion of the tenant cluster's control plane finalizer cloud formation stack")

		r.logger.Debugf(ctx, "keeping finalizers")
		finalizerskeptcontext.SetKept(ctx)
	}

	return nil
}


text:
import React, { Component } from "react";
import PropTypes from "prop-types";
import { area as d3Area } from "d3-shape";

import GenericChartComponent from "../GenericChartComponent";
import { getAxisCanvas } from "../GenericComponent";

import { hexToRGBA, isDefined, first, functor } from "../utils";

class AreaOnlySeries extends Component {
	constructor(props) {
		super(props);
		this.renderSVG = this.renderSVG.bind(this);
		this.drawOnCanvas = this.drawOnCanvas.bind(this);
	}
	drawOnCanvas(ctx, moreProps) {
		const { yAccessor, defined, base, canvasGradient } = this.props;
		const { fill, stroke, opacity, interpolation, canvasClip } = this.props;

		const { xScale, chartConfig: { yScale }, plotData, xAccessor } = moreProps;

		if (canvasClip) {
			ctx.save();
			canvasClip(ctx, moreProps);
		}

		if (canvasGradient != null) {
			ctx.fillStyle = canvasGradient(moreProps, ctx);
		} else {
			ctx.fillStyle = hexToRGBA(fill, opacity);
		}
		ctx.strokeStyle = stroke;

		ctx.beginPath();
		const newBase = functor(base);
		const areaSeries = d3Area()
			.defined(d => defined(yAccessor(d)))
			.x((d) => Math.round(xScale(xAccessor(d))))
			.y0((d) => newBase(yScale, d, moreProps))
			.y1((d) => Math.round(yScale(yAccessor(d))))
			.context(ctx);

		if (isDefined(interpolation)) {
			areaSeries.curve(interpolation);
		}
		areaSeries(plotData);
		ctx.fill();

		if (canvasClip) {
			ctx.restore();
		}
	}
	renderSVG(moreProps) {
		const { yAccessor, defined, base, style } = this.props;
		const { stroke, fill, className, opacity, interpolation } = this.props;

		const { xScale, chartConfig: { yScale }, plotData, xAccessor } = moreProps;

		const newBase = functor(base);
		const areaSeries = d3Area()
			.defined(d => defined(yAccessor(d)))
			.x((d) => Math.round(xScale(xAccessor(d))))
			.y0((d) => newBase(yScale, d, moreProps))
			.y1((d) => Math.round(yScale(yAccessor(d))));

		if (isDefined(interpolation)) {
			areaSeries.curve(interpolation);
		}

		const d = areaSeries(plotData);
		const newClassName = className.concat(isDefined(stroke) ? "" : " line-stroke");
		return (
			<path
				style={style}
				d={d}
				stroke={stroke}
				fill={hexToRGBA(fill, opacity)}
				className={newClassName}

			/>
		);
	}
	render() {
		return (
			<GenericChartComponent
				svgDraw={this.renderSVG}
				canvasDraw={this.drawOnCanvas}
				canvasToDraw={getAxisCanvas}
				drawOn={["pan"]}
			/>
		);
	}
}

AreaOnlySeries.propTypes = {
	className: PropTypes.string,
	yAccessor: PropTypes.func.isRequired,
	stroke: PropTypes.string,
	fill: PropTypes.string,
	opacity: PropTypes.number,
	defined: PropTypes.func,
	base: PropTypes.oneOfType([
		PropTypes.func, PropTypes.number
	]),
	interpolation: PropTypes.func,
	canvasClip: PropTypes.func,
	style: PropTypes.object,
	canvasGradient: PropTypes.func,
};

AreaOnlySeries.defaultProps = {
	className: "line ",
	fill: "none",
	opacity: 1,
	defined: d => !isNaN(d),
	base: (yScale /* , d, moreProps */) => first(yScale.range()),
};

export default AreaOnlySeries;