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

package org.apache.tapestry5.integration.app1;

import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.Zone;
import org.testng.annotations.Test;

/**
 * Tests related to the {@link Zone} component.
 */
public class ZoneTests extends App1TestCase
{
    /**
     * TAP5-138
     */
    @Test
    public void select_zone()
    {
        openLinks("Select Zone Demo");

        select("carMaker", "Bmw");

        waitForAjaxRequestsToComplete();

        click(SUBMIT);

        assertTextPresent("You must provide a value for Car Model.");

        String selectLocator = "css=#modelZone select";

        select(selectLocator, "7 Series");

        clickAndWait(SUBMIT);

        assertTextPresent("Car Maker: BMW");

        assertTextPresent("Car Model: 7 Series");

        select("carMaker", "Mercedes");

        waitForAjaxRequestsToComplete();

        select(selectLocator, "E-Class");

        clickAndWait(SUBMIT);

        assertTextPresent("Car Maker: MERCEDES");

        assertTextPresent("Car Model: E-Class");
    }

    @Test
    public void zone_updates()
    {
        openLinks("Zone Demo");

        assertTextPresent("No name has been selected.");

        click("link=Select \"Mr. <Roboto>\"");

        waitForAjaxRequestsToComplete();

        assertTextPresent("Selected: Mr. <Roboto>");

        click("link=Direct JSON response");
    }

    /**
     * TAP5-187
     */
    @Test
    public void zone_redirect_by_class()
    {
        openLinks("Zone Demo");

        clickAndWait("link=Perform a redirect to another page");

        assertText("activePageName", "nested/AssetDemo");
    }

    /**
     * TAP5-108
     */
    @Test
    public void update_multiple_zones_at_once()
    {
        openLinks("Multiple Zone Update Demo");

        String now = getText("now");

        assertText("wilma", "Wilma Flintstone");

        assertText("message", "");

        click("link=update");

        waitForElementToAppear("fredName");

        assertText("fredName", "Fred Flintstone");
        assertText("dino", "His dog, Dino.");
        assertText("wilma", "His Wife, Wilma.");

        assertText("message", "Updated");

        // Ideally, we'd add checks that the JavaScript for the Palette in the
        // Barney Zone was
        // updated.

        // Make sure it was a partial update
        assertText("now", now);
    }

    /**
     * TAP5-573
     */
    @Test
    public void zone_namespace_interaction_fixed()
    {
        openLinks("Zone/Namespace Interaction");

        String outerNow = getText("outernow");
        String innerNow = getText("innernow");

        // If we're too fast that innernow doesn't change because its all within
        // a single second.

        sleep(1200);

        click(SUBMIT);

        waitForAjaxRequestsToComplete();

        waitForElementToAppear("message");

        // Make sure it was just an Ajax update.
        assertText("outernow", outerNow);

        assertFalse(getText("innernow").equals(innerNow));
    }

    @Test
    public void zone_updated_event_triggered_on_client()
    {
        openLinks("Zone Demo");

        assertText("zone-update-message", "");

        click("link=Direct JSON response");

        // Give it some time to process.

        waitForAjaxRequestsToComplete();

        assertText("zone-update-message", "Zone updated.");
    }

    /**
     * TAP5-389
     */
    @Test
    public void link_submit_inside_form_that_updates_a_zone()
    {
        openLinks("LinkSubmit inside Zone");

        String now = getText("now");

        waitForElementToAppear("mySubmit");

        click("//a[@id='mySubmit']");

        type("value", "robot chicken");

        click("//a[@id='mySubmit']");

        waitForElementToAppear("outputvalue");

        assertText("outputvalue", "robot chicken");

        assertText("eventfired", "true");

        // Make sure it was a partial update
        assertText("now", now);
    }

    @Test
    public void zone_inject_component_from_template()
    {
        openLinks("Inject Component Demo");

        assertTextPresent(Form.class.getName() + "[form--form]");
    }

    /**
     * TAP5-1084
     */
    @Test
    public void update_zone_inside_form()
    {
        openLinks("Zone/Form Update Demo");

        click("link=Update the form");

        waitForElementToAppear("updated");

        type("//INPUT[@type='text']", "Tapestry 5.2");

        clickAndWait(SUBMIT);

        assertText("output", "Tapestry 5.2");
    }

    /**
     * TAP5-1109
     */
    @Test
    public void update_to_zone_inside_form()
    {
        openLinks("MultiZone Update inside a Form");

        select("selectValue1", "3 pre ajax");

        waitForAjaxRequestsToComplete();

        select("//div[@id='select2ValueZone']//select", "4 post ajax, number 013, retention policy RUNTIME");
    }

    @Test
    public void multi_zone_update_using_string_in_loop()
    {
        openLinks("MultiZone String Body Demo");
        String[] numbers = new String[]{
                "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"
        };

        for (int i = 0; i <= 10; i++)
        {
            assertText("row-" + i, numbers[i]);
        }

        click("link=click 7");
        waitForElementToAppear("row-7");

        // 0-6 are unchanged
        for (int i = 0; i < 7; i++)
        {
            assertText("row-" + i, numbers[i]);
        }
        // 7+ are modified
        for (int i = 7; i <= 10; i++)
        {
            assertText("row-" + i, i + " is the integer value");
        }

        click("link=Reset Zones");
        waitForElementToAppear("wholeLoopZone");

        // all elements reset via AJAX
        for (int i = 0, numbersLength = numbers.length; i < numbersLength; i++)
        {
            assertText("row-" + i, numbers[i]);
        }

    }

    private void assertCSSStartsWith(String elementId, String cssProperty, String expected)
    {
        String actual = selenium.getEval(String.format("window.document.defaultView.getComputedStyle(window.document.getElementById('%s'), null).getPropertyValue('%s')",
                elementId, cssProperty));

        assertTrue(actual.startsWith("underline"), String.format("CSS property '%s' of '%s' should start with '%s'.", cssProperty, elementId, expected));
    }

    private void assertCSS(String elementId, String cssProperty, String expected)
    {
        // See http://groups.google.com/group/selenium-users/browse_thread/thread/f21e0a43c9913d42

        String actual = selenium.getEval(String.format("window.document.defaultView.getComputedStyle(window.document.getElementById('%s'), null).getPropertyValue('%s')",
                elementId, cssProperty));

        assertEquals(actual, expected, String.format("CSS property '%s' of '%s' should be '%s'.", cssProperty, elementId, expected));
    }

    @Test
    public void css_insertion_point()
    {
        openLinks("Zone Demo");

        click("link=Select \"CSS Injection\"");

        // First check that the update arrived

        waitForElementToAppear("demo-aip");

        assertText("demo-aip", "This should be styled GREEN.");

        // Next see if we can verify that the presentation matches the exceptations; greend and underlined.  Underlined from
        // zonedemo-viaajax.css; green from zonedmeo-overrides.css (not blue as defined in zonedemo-viaajax.css).

        assertCSS("demo-aip", "color", "rgb(0, 128, 0)");
        assertCSSStartsWith("demo-aip", "text-decoration", "underline");
    }

    /**
     * TAP5-1890
     */
    @Test
    public void update_zone_with_empty_body()
    {
        openLinks("Zone Demo");

        assertText("zone-update-message", "");

        click("link=Update zone with empty body");

        waitForAjaxRequestsToComplete();

        assertText("zone-update-message", "Zone updated.");
    }

    /**
     * TAP5-2330
     */
    @Test
    public void update_zone_with_no_clientid()
    {
        openLinks("Zone Demo");

        assertText("zone-update-message", "");

        click("link=Update via AjaxResponseRenderer");

        waitForAjaxRequestsToComplete();

        assertText("zone-update-message", "Zone updated.");

        assertEquals("Selected: AjaxResponseRenderer", getText("output"));

    }

}

redpajama_set_name: RedPajamaGithub


text:
/*
 * Removes `value` from `array`, assuming it exists.
 *
 * It will only remove the first instance of `value`. It returns the index (of the original array)
 * that was removed. Therefore, callers can check for `-1` to see if nothing as removed.
 */
function removeFromArray(array, value) {
  const index = array.indexOf(value);

  if (index >= 0) {
    array.splice(index, 1);
  }

  return index;
}

/*
 * This is the generic handler adder
 * @param {string} eventName The event to handle
 * @param {string} threshold Whether to trigger the listener on `"all"` or `"any"` event
 * @param {string} frequency Whether to do this `"once"` or repeatedly (`"on"`)
 * @param {function()} listener The listener to trigger on this event
 */
function addHandler(eventName, threshold, frequency, listener) {
  if (!this.listeners[eventName]) {
    this.listeners[eventName] = [];
  }

  const handler = {
    threshold,
    frequency,
    fn: listener,
    eventArgs: new Array(this.sources.length).fill(null),
  };

  this.listeners[eventName].push(handler);
}

function clearFiredEvents(listener) {
  listener.eventArgs.fill(null);
}

function getMasterListener(eventName) {
  const aggregator = this;

  if (!this.masterListeners[eventName]) {
    this.masterListeners[eventName] = function masterListener(...eventArguments) {
      const sourceEmitter = this;
      const listeners = aggregator.listeners[eventName];

      const emitterIndex = aggregator.sources.indexOf(sourceEmitter);
      if (emitterIndex < 0) {
        throw new Error('Somehow called the listener with an emitter that we are not aggregating');
      }

      aggregator.listeners[eventName] = listeners.filter((listener) => {
        listener.eventArgs[emitterIndex] = Array.prototype.slice.call(eventArguments);

        const allFired = (listener.eventArgs.indexOf(null) < 0);
        const waitForAll = (listener.threshold === 'all');

        const callListener = allFired || !waitForAll;

        if (callListener) {
          if (waitForAll) {
            listener.fn.call(aggregator, listener.eventArgs.slice());
            clearFiredEvents(listener);
          } else {
            listener.fn.call(aggregator, sourceEmitter, eventArguments);
            clearFiredEvents(listener);
          }
        }

        return !(callListener && (listener.frequency === 'once'));
      });
    };
  }

  return this.masterListeners[eventName];
}

function attachMasterListener(eventName) {
  const masterListener = getMasterListener.call(this, eventName);
  if (!(eventName in this.listeners)) {
    this.sources.forEach(emitter => emitter.on(eventName, masterListener));
  }
}

/**
 * The EventAggregator provides a way to bring EventEmitters together into a group and to interact
 * with them as a collection (rather than individuals)
 */
class EventAggregator {
  /**
   * When instantiating an aggregator, you can pass it a single EventEmitter, a collection of
   * Emitters or nothing.
   * @param {EventEmitter[]|EventEmitter} [sources=[]]
   */
  constructor(sources = []) {
    if (!Array.isArray(sources)) {
      this.sources = [sources];
    } else {
      this.sources = sources;
    }

    this.listeners = {};
    this.masterListeners = {};
  }

  /**
   * Provides a list of the events that the aggregator is listening for.
   *
   * NB: This doesn't mean that there are necessarily any handlers attached to these events.
   * @returns {Array<string>}
   */
  eventsListenedTo() {
    return Object.keys(this.listeners);
  }

  /**
   * Add a source source to the group.
   *
   * Any listeners that are currently defined will be automatically added to this source.
   * @param {EventEmitter} source
   */
  addSource(source) {
    this.sources.push(source);
    Object.keys(this.listeners).forEach((eventName) => {
      source.on(eventName, getMasterListener.call(this, eventName));
    });
  }

  /**
   * Removers a source from the group.
   *
   * Any listeners attached to this source are also removed.
   *
   * If any listeners are attached via onAll or onceAll, then removing this source may result in
   * those listeners being fired. This will happen if the source that is being removed is the only
   * one in the group that hadn't emitted an event.
   *
   * Put another way, if *all* of the sources except this one had fired, then removing this one
   * fires the `onAll` and `onceAll` listeners.
   * @param {EventEmitter} source
   */
  removeSource(source) {
    const removedIndex = removeFromArray(this.sources, source);

    this.eventsListenedTo().forEach((eventName) => {
      source.removeListener(eventName, getMasterListener.call(this, eventName));
      this.listeners[eventName].forEach((listener) => {
        listener.eventArgs.splice(removedIndex, 1);
        const allFired = (listener.eventArgs.indexOf(null) < 0) && listener.eventArgs.length > 0;

        if (allFired) {
          listener.fn.call(this, listener.eventArgs.slice());
          clearFiredEvents(listener);
        }
      });
    });
  }

  /**
   * Removes a listener from the aggregator (just like EventEmitter#removeListener)
   *
   * @param {string} eventName The name of the event from which we're removing a listener
   * @param {function} listenerToRemove The listener that we wish to remove
   */
  removeListener(eventName, listenerToRemove) {
    const listeners = this.listeners[eventName] || [];

    this.listeners[eventName] = listeners.filter(listener => listener.fn !== listenerToRemove);
  }

  /**
   * If any of the sources in this aggregator emits a `eventName` event, trigger the associated
   * `listener`.
   *
   * Like `EventEmitter#on`, this will continue to fire until it is explicitly removed.
   *
   * The `listener` will receive a reference to the emitter that emitted the event and an array
   * of the arguments that the event included.
   *
   * @param {string} eventName
   * @param {function(EventEmitter, Array)} listener
   */
  onAny(eventName, listener) {
    attachMasterListener.call(this, eventName);
    addHandler.call(this, eventName, 'any', 'on', listener);
  }

  /**
   * Once all of the sources in this aggregator have emitted a `eventName` event, the associated
   * `listener` is triggered.
   *
   * Like `EventEmitter#on`, this will continue to fire until it is explicitly removed.
   *
   * The `listener` will receive an array of the arguments from each of the events that were emitted
   * from the aggregated sources. The array is in the order in which the sources were added to
   * this aggregator.
   *
   * If a source has emitted an event multiple times, the listener will get the arguments from the
   * first event.
   *
   * Once the listener has been triggered, this aggregator is reset for this event.
   *
   * @param {string} eventName
   * @param {function(EventEmitter, Array)} listener
   */
  onAll(eventName, listener) {
    attachMasterListener.call(this, eventName);
    addHandler.call(this, eventName, 'all', 'on', listener);
  }

  /**
   * If any of the sources in this aggregator emits a `eventName` event, trigger the associated
   * `listener`.
   *
   * Like `EventEmitter#once`, the listener will be removed once it has been triggered.
   *
   * The `listener` will receive a reference to the source that emitted the event and an array
   * of the arguments that the event included.
   *
   * @param {string} eventName
   * @param {function(EventEmitter, Array)} listener
   */
  onceAny(eventName, listener) {
    attachMasterListener.call(this, eventName);
    addHandler.call(this, eventName, 'any', 'once', listener);
  }

  /**
   * Once all of the sources in this aggregator have emitted a `eventName` event, the associated
   * `listener` is triggered.
   *
   * Like `EventEmitter#once`, the listener will be removed for this event.
   *
   * The `listener` will receive an array of the arguments from each of the events that were emitted
   * from the aggregated sources. The array is in the order in which the sources were added to
   *  aggregator.
   *
   * If a source has emitted events multiple times, the listener will get the arguments from the
   * first event.
   *
   * @param {string} eventName
   * @param {function(EventEmitter, Array)} listener
   */
  onceAll(eventName, listener) {
    attachMasterListener.call(this, eventName);
    addHandler.call(this, eventName, 'all', 'once', listener);
  }

  /**
   * If there are listeners that are waiting for all of the sources in this aggregation to emit,
   * then calling this method will reset the aggregator so that it's as if none of them have
   * emitted.
   *
   * If an `eventName` is provided, this is limited to listeners for that event. Otherwise, calling
   * this resets all of the listeners.
   *
   * @param {string} [eventName]
   */
  reset(eventName) {
    if (eventName) {
      this.listeners[eventName].forEach(clearFiredEvents);
    } else {
      Object.keys(this.listeners).forEach((name) => {
        this.reset(name);
      });
    }
  }

  /**
   * Calling this method removes all listeners from all sources. It should be called to avoid
   * memory leaks when you're done with the aggregator.
   */
  destroy() {
    this.sources.forEach(this.removeSource.bind(this));
    this.listeners = null;
  }
}

export default EventAggregator;


text:
id: 5a24c314108439a4d4036143
title: Extract State Logic to Redux
challengeType: 6
forumTopicId: 301429
dashedName: extract-state-logic-to-redux
---

# --description--

Now that you finished the React component, you need to move the logic it's performing locally in its `state` into Redux. This is the first step to connect the simple React app to Redux. The only functionality your app has is to add new messages from the user to an unordered list. The example is simple in order to demonstrate how React and Redux work together.

# --instructions--

First, define an action type `ADD` and set it to a const `ADD`. Next, define an action creator `addMessage()` which creates the action to add a message. You'll need to pass a `message` to this action creator and include the message in the returned `action`.

Then create a reducer called `messageReducer()` that handles the state for the messages. The initial state should equal an empty array. This reducer should add a message to the array of messages held in state, or return the current state. Finally, create your Redux store and pass it the reducer.

# --hints--

The const `ADD` should exist and hold a value equal to the string `ADD`

```js
assert(ADD === 'ADD');
```

The action creator `addMessage` should return an object with `type` equal to `ADD` and `message` equal to the message that is passed in.

```js
assert(
  (function () {
    const addAction = addMessage('__TEST__MESSAGE__');
    return addAction.type === ADD && addAction.message === '__TEST__MESSAGE__';
  })()
);
```

`messageReducer` should be a function.

```js
assert(typeof messageReducer === 'function');
```

The store should exist and have an initial state set to an empty array.

```js
assert(
  (function () {
    const initialState = store.getState();
    return typeof store === 'object' && initialState.length === 0;
  })()
);
```

Dispatching `addMessage` against the store should immutably add a new message to the array of messages held in state.

```js
assert(
  (function () {
    const initialState = store.getState();
    const isFrozen = DeepFreeze(initialState);
    store.dispatch(addMessage('__A__TEST__MESSAGE'));
    const addState = store.getState();
    return isFrozen && addState[0] === '__A__TEST__MESSAGE';
  })()
);
```

The `messageReducer` should return the current state if called with any other actions.

```js
assert(
  (function () {
    const addState = store.getState();
    store.dispatch({ type: 'FAKE_ACTION' });
    const testState = store.getState();
    return addState === testState;
  })()
);
```

# --seed--

## --seed-contents--

```jsx
// Define ADD, addMessage(), messageReducer(), and store here:
```

# --solutions--

```jsx
const ADD = 'ADD';

const addMessage = (message) => {
  return {
    type: ADD,
    message
  }
};

const messageReducer = (state = [], action) => {
  switch (action.type) {
    case ADD:
      return [
        ...state,
        action.message
      ];
    default:
      return state;
  }
};

const store = Redux.createStore(messageReducer);
```


text:
HHPlayerKit
===========

Player Kit was wrote based on AVFoundation framework.


text:
from random import randint
from math import radians, sin, cos
import svgwrite as svg

# from settings import svg_folder_path

import json

from robot.ultrasound_sensor import UltrasoundSensor
from robot.mecanum_control import RobotMecanumControl

from robot.ultrasound_control import RobotMixinUltrasoundControl
from robot.chase_candy_control import RobotMixinChaseCandyControl

svg.profile = "tiny"

class Robot(RobotMixinUltrasoundControl, RobotMixinChaseCandyControl):

    cats = ["ultrasound", "robot"]

    def __init__(
        self, root, drive, robot_name="robot", us_id_offset=0, robot_number=0
    ):
        self.root = root
        self.robot_name = robot_name

        self.robot_number = robot_number

        self.baseProfile = "tiny"  # 'full'

        self.ents = {category: [] for category in Robot.cats}

        self.ultrasounds = {}
        self.us_id_offset = us_id_offset

        self.cts = root.collision_types
        self.drive = drive
        self.create_robot()

        self.entity = self.root.gameworld.entities[self.ent]
        self.body = self.entity.cymunk_physics.body

        if drive == "mecanum":
            self.control = RobotMecanumControl(self.root, self)

        self.init_chase_candy_control()

    def set_random_position(self):
        self.entity.cymunk_physics.body.position = self.candy.get_rand_pos()

    def get_pos(self):
        return self.body.position

    def create_ultrasound(self, name, color, stroke_color, vert_list):
        cat = "ultrasound"
        info_dict = {
            "collision_type": self.cts[cat],
            "object_info": {"name": str(name), "category": cat, "mass": 0},
        }
        info_str = json.dumps(info_dict)
        id_str = self.robot_name + "_" + cat + "_" + str(name)

        #           color = self.dwg.SolidColor('green', 0.2)
        gradient = svg.gradients.LinearGradient(color="#00FF00", opacity=0.2)

        ultra = (
            self.dwg.polygon(vert_list, id=id_str, description=info_str)
            .fill("green", opacity=0.2)
            .stroke("white", width=1)
            .dasharray([20, 10])
        )
        self.dwg.add(ultra)

    def add_entity(self, entity_info):
        category = entity_info["category"]

        print(";;;;;;;;;;;;;;;;;;;;;", category)

        if category in "ultrasound":
            us = UltrasoundSensor(self, **entity_info)
            #            self.ultrasounds.append()
            self.ultrasounds[entity_info["us_id"]] = us
            print(entity_info["us_id"])
        # elif category in 'robot':
        # self.ent = entity_info['ent']

        # self.entities[category].append(ent)

    def create_robot(self):

        w, h = self.root.field_size

        pos = (randint(0, w), randint(0, h))
        self.siz = [40, 60]
        self.mass = 100

        self.create_robot_rect(
            self.robot_name,
            self.mass,
            pos,
            self.siz,
            self.us_id_offset,
            self.robot_number,
        )

    def create_robot_rect(
        self, name, mass, pos, siz, us_id_offset, robot_number
    ):
        id_str = self.robot_name
        insert_pos = [pos[i] + siz[i] / 2 for i in range(2)]
        # self.pos = insert_pos
        self.pos = pos
        cat = "robot"
        robot_name = name

        cts = self.root.collision_types
        self.ct = cts["robot"] + robot_number
        # robot_group = 42

        width, height = siz
        w, h = siz
        robot_verts = [
            (-width / 2.0, -height / 2.0),
            (-width / 2.0, height / 2.0),
            (width / 2.0, height / 2.0),
            (width / 2.0, -height / 2.0),
        ]

        robot_shape = {
            "shape_type": "poly",
            "elasticity": 0.6,
            "collision_type": self.ct,
            "friction": 1.0,
            #       'group' : robot_group,
            "shape_info": {
                "mass": mass,
                "offset": (0, 0),
                "vertices": robot_verts,
            },
        }

        col_shapes = [robot_shape]

        physics = {
            "main_shape": "poly",
            "velocity": (0, 0),
            "position": self.pos,
            "angle": 0,
            "angular_velocity": radians(0),
            "ang_vel_limit": radians(0),
            "mass": mass,
            "col_shapes": col_shapes,
        }

        # ultrasounds
        open_angle = radians(45)
        count_ultrasounds = 3
        names = ["left", "middle", "right"]
        ultrasound_range = 60
        ultrasound_ranges = [ultrasound_range, ultrasound_range + 100]
        x0, y0 = self.pos[0] + 0, self.pos[1] + h / 4
        center_x0, center_y0 = 0, h / 2

        us_color = (0, 255, 0)
        self.max_us_range = 250
        self.max_us_opacity = 200
        us_models = []
        sensor_width = 10
        for i in range(count_ultrasounds):
            # to center it
            shift_angle = count_ultrasounds / 2 * open_angle
            # cone edges
            edge_angles = (
                i * open_angle - shift_angle,
                (1 + i) * open_angle - shift_angle,
            )

            us_x = (i - (count_ultrasounds - 1) / 2) * sensor_width
            us_y = 1
            x0 = center_x0 + us_x
            y0 = center_y0 + us_y

            edge_points = [
                [
                    ultrasound_range * sin(edge_angle) + x0,
                    ultrasound_range * cos(edge_angle) + y0,
                ]
                for edge_angle in edge_angles
            ]

            vert_list = [(x0, y0), edge_points[0], edge_points[1]]

            mass = 0.1
            us_id = cts["ultrasound"][i] + us_id_offset

            us_shape = {
                "shape_type": "poly",
                "elasticity": 0.0,
                "collision_type": us_id,
                "friction": 0.0,
                "sensor": True,
                #'group': robot_group,
                "shape_info": {
                    "mass": mass,
                    "offset": (0, 0),
                    "vertices": vert_list,
                },
            }
            col_shapes.append(us_shape)

            entity_info = {
                "category": "ultrasound",
                "us_id": us_id,
                "name": names[i],
                "distance_range": ultrasound_range,
                "us_pos": (us_x, us_y),
            }

            us_color = (
                randint(100, 200),
                255 * randint(8, 10) / 10,
                randint(100, 100),
            )
            us_model = self.get_triangle_data(
                vert_list, us_color, ultrasound_ranges
            )
            us_models.append(us_model)

            # self.root.add_entity(entity_info)

            us = UltrasoundSensor(self, **entity_info)
            self.ultrasounds[entity_info["us_id"]] = us
            print(entity_info["us_id"])

        model_name = robot_name
        model_manager = self.root.gameworld.model_manager

        rect_data = self.get_rectangle_data(h, w)
        rect_data2 = self.get_rectangle_data(w, w)

        rects = [rect_data, rect_data2]
        rects.extend(us_models)

        model_data = self.join_vert_models(rects)

        rectangle_model = model_manager.load_model(
            "vertex_format_2f4ub",
            model_data["vertex_count"],
            model_data["index_count"],
            model_name,
            indices=model_data["indices"],
            vertices=model_data["vertices"],
        )

        # self.root.pprint(dir(model_manager))
        robot_model = rectangle_model
        # self.shapes['rectangle_model'] = rectangle_model

        component_dict = {
            "position": pos,
            "rotate_poly_renderer": {"model_key": rectangle_model},
            "cymunk_physics": physics,
            "rotate": radians(0),
        }

        cat = "robot"
        info_dict = {
            "mass": mass,
            "object_info": {"name": name, "category": cat},
        }
        info_str = json.dumps(info_dict)

        object_info = info_dict["object_info"]

        print("robot component creation")
        component_order = [
            "position",
            "rotate",
            "rotate_poly_renderer",
            "cymunk_physics",
        ]
        # self.root.init_entity(component_dict, component_order, object_info=object_info)

        self.ent = self.root.gameworld.init_entity(
            component_dict, component_order
        )
        print(">>>>>>", self.ent)
        self.root.add_entity(self.ent, "robot")

    def join_vert_models(self, model_list):
        # self.root.pprint(model_list)

        vertices = None
        indices = None
        prev_vert_count = 0
        for d in model_list:
            if indices is None:
                indices = list(d["indices"])
                vertices = dict(d["vertices"])
            else:
                this_indices_incremented = [
                    ind + prev_vert_count for ind in d["indices"]
                ]
                indices.extend(this_indices_incremented)
                this_vertices_incremented = {
                    (k + prev_vert_count): v for k, v in d["vertices"].items()
                }

                # did not work
                # vertices.update(this_indices_incremented)
                for k, v in this_vertices_incremented.items():
                    vertices[k] = v

            prev_vert_count += d["vertex_count"]

        joined = {
            "vertex_count": sum([d["vertex_count"] for d in model_list]),
            "index_count": sum([d["index_count"] for d in model_list]),
            "indices": indices,
            "vertices": vertices,
        }
        # self.root.pprint(joined)
        return joined

    @staticmethod
    def get_rectangle_data(height, width):
        def _rnd():
            return randint(0, 255)

        def c_rnd():
            return (_rnd(), _rnd(), _rnd(), 255)

        return {
            "vertices": {
                0: {"pos": (-width / 2.0, -height / 2.0), "v_color": c_rnd()},
                1: {"pos": (-width / 2.0, height / 2.0), "v_color": c_rnd()},
                2: {"pos": (width / 2.0, height / 2.0), "v_color": c_rnd()},
                3: {"pos": (width / 2.0, -height / 2.0), "v_color": c_rnd()},
            },
            "indices": [0, 1, 2, 2, 3, 0],
            "vertex_count": 4,
            "index_count": 6,
        }

    def get_triangle_data(self, vert_list, us_color, us_range_list):

        opacities = [
            self.max_us_opacity * (1 - us_range / self.max_us_range)
            for us_range in us_range_list
        ]
        # print(opacities)
        colors = [list(us_color), list(us_color)]
        [color.append(opacity) for color, opacity in zip(colors, opacities)]
        # print(colors)

        color_inds = [0, 1, 1]
        # print(vert_list)
        # print([(i, v) for i,v in enumerate(vert_list)])
        vertices = {
            i: {"pos": vert_tuple, "v_color": colors[color_inds[i]]}
            for i, vert_tuple in enumerate(vert_list)
        }
        model_info = {
            "vertices": vertices,
            "indices": [0, 1, 2],
            "vertex_count": 3,
            "index_count": 3,
        }
        return model_info


text:
<?php

namespace Oro\Bundle\ApiBundle\Tests\Unit\Config;

use Oro\Bundle\ApiBundle\Config\EntityDefinitionConfig;
use Oro\Bundle\ApiBundle\Config\EntityIdMetadataAdapter;

class EntityIdMetadataAdapterTest extends \PHPUnit\Framework\TestCase
{
    public function testGetClassName()
    {
        $className = 'Test\Class';

        $adapter = new EntityIdMetadataAdapter($className, new EntityDefinitionConfig());

        self::assertEquals($className, $adapter->getClassName());
    }

    public function testGetIdentifierFieldNames()
    {
        $identifierFieldNames = ['id'];
        $config = new EntityDefinitionConfig();
        $config->setIdentifierFieldNames($identifierFieldNames);

        $adapter = new EntityIdMetadataAdapter('Test\Class', $config);

        self::assertEquals($identifierFieldNames, $adapter->getIdentifierFieldNames());
    }

    public function testGetPropertyPathForUnknownField()
    {
        $config = new EntityDefinitionConfig();

        $adapter = new EntityIdMetadataAdapter('Test\Class', $config);

        self::assertNull($adapter->getPropertyPath('unknown'));
    }

    public function testGetPropertyPathForNotRenamedField()
    {
        $fieldName = 'field1';
        $config = new EntityDefinitionConfig();
        $config->addField($fieldName);

        $adapter = new EntityIdMetadataAdapter('Test\Class', $config);

        self::assertEquals($fieldName, $adapter->getPropertyPath($fieldName));
    }

    public function testGetPropertyPathForRenamedField()
    {
        $fieldName = 'renamedField1';
        $fieldPropertyPath = 'field1';
        $config = new EntityDefinitionConfig();
        $config->addField($fieldName)->setPropertyPath($fieldPropertyPath);

        $adapter = new EntityIdMetadataAdapter('Test\Class', $config);

        self::assertEquals($fieldPropertyPath, $adapter->getPropertyPath($fieldName));
    }
}


text:
Title: Rectangle RAG PCB
Date: 2015-04-22 23:59
Category: blog
Tags: cuffelinks, traffic, PCB
Author: Brendan M. Sleight

Following some fun looking at [outlines for laser-cut-traffic-light-cufflink.html](../laser-cut-traffic-light-cufflink.html) I whipped up a quick PCB using [KiCad](http://www.kicad-pcb.org/display/KICAD/KiCad+EDA+Software+Suite). Was quite surprised when all the components fit on the PCB and the routing was possible, I thought it would not work.

You can view all the raw KiCad files in the [git repository](https://github.com/bmsleight/chameleon-cufflink/tree/master/pcb/rectangle-rag-pcb). I test the gerber files at [oshpark](https://oshpark.com/) although I have not ordered them.


text:
/**
 *
 * Definition for singly-linked list.
 */
public class ListNode {

    int val;
    ListNode next;
    ListNode(int x) {
        val = x;
        next = null;
    }
}


text:
#ifndef MBED_ERROR_H
#define MBED_ERROR_H

/** To generate a fatal compile-time error, you can use the pre-processor #error directive.
 *
 * @code
 * #error "That shouldn't have happened!"
 * @endcode
 *
 * If the compiler evaluates this line, it will report the error and stop the compile.
 *
 * For example, you could use this to check some user-defined compile-time variables:
 *
 * @code
 * #define NUM_PORTS 7
 * #if (NUM_PORTS > 4)
 *     #error "NUM_PORTS must be less than 4"
 * #endif
 * @endcode
 *
 * Reporting Run-Time Errors:
 * To generate a fatal run-time error, you can use the mbed error() function.
 *
 * @code
 * error("That shouldn't have happened!");
 * @endcode
 *
 * If the mbed running the program executes this function, it will print the
 * message via the USB serial port, and then die with the blue lights of death!
 *
 * The message can use printf-style formatting, so you can report variables in the
 * message too. For example, you could use this to check a run-time condition:
 *
 * @code
 * if(x >= 5) {
 *     error("expected x to be less than 5, but got %d", x);
 * }
 * #endcode
 */

#ifdef __cplusplus
extern "C" {
#endif

void error(const char* format, ...);

#ifdef __cplusplus
}
#endif

#endif


text:
<?php

declare(strict_types=1);

namespace FSi\Bundle\DoctrineExtensionsBundle\Listener\Uploadable;

class Filesystem extends \Gaufrette\Filesystem
{
    /**
     * @var string
     */
    protected $baseUrl;

    public function setBaseUrl(?string $baseUrl): void
    {
        $this->baseUrl = $baseUrl;
    }

    public function getBaseUrl(): ?string
    {
        return $this->baseUrl;
    }
}


text:
BOOLEAN WINAPI DllMain(IN HINSTANCE hDllHandle,
    IN DWORD     nReason,
    IN LPVOID    Reserved)
{
    BOOLEAN bSuccess = TRUE;

    //  Perform global initialization.

    switch (nReason)
    {
    case DLL_PROCESS_ATTACH:

        break;

    case DLL_PROCESS_DETACH:

        break;
    }

    return bSuccess;

}


text:
title: "Integration test with Docker"
category: test
tags:
 - test
 - integration
 - MySQL
 - docker
 - dao
description: "How to write integration tests against a real external instance with Docker. Real example using MySQL"

---

## How should I test a Data Access Object (DAO)? ##

The answer to this question has always been a big doubt for me in the past. I'm sure everybody sooner or later reached the point where the need to test some code interacting with the DB arose. Unit testing code that talks with other code is quite easy (depending on how well you architect your application) but when you reach the borders, testing the interaction with other systems gets more complex. For instance let's say you are writing Java code using JDBC, what should you test about a method executing a query on the DB and returning the `ResultSet`? Should you test whether the `executeQuery` method has been called with the right query? Should you mock the `Statement` object to return the `ResultSet` you expect? All these approaches looks silly to me, both of them are not actually testing the query you're using works, they just make sure you wrote the query as _you suppose_ works and the DB is returning what _you suppose_ it will return. If you're thinking

> "That's very true but in my method I have a more complex logic to test"

then maybe you are missing an abstraction layer in your code.
The way to go in my opinion is to test your code against a real instance to make sure that your code does what it is supposed to do.

### Sub-optimal solutions ###

If you search around, you definitely run into a lot of resources telling you to use H2. What I don't like about this approach is that when I want to test apples I don't want to test oranges, I know it's a good approximation but I want to test apples! Writing code is similar to [Rock Balancing](https://en.wikipedia.org/wiki/Rock_balancing), it's an art, get something wrong and everything can collapse.
Someone - instead - likes to write stubs and recreate the behavior of the external system you are testing against to. This is something that doesn't make sense in my opinion apart for really sporadic cases. You should focus more on implementing something new rather than implementing something that already exists.

### Solution with Docker ###

As the use of containers became more and more invasive I thought:

>"Why not use [Docker](https://www.docker.com/) for integration tests?"

It provides everything you need:

- __Repeatable tests__ For each and every build we have a dedicated empty MySQL instance
- __Real environment__ We test against the same version we have in production
- __No dedicated hardware__ The docker image runs on the same machine you execute the tests
- __Easy integration__ Interacting with docker is quite easy and you need only few lines of code when you understand how it works.

The amazing part of this approach is that it's not limited to MySQL but it can be used to test against any external system you have a docker image of, the configuration is pretty similar.
Compared to H2 approach, using MySQL directly also gives you the advantage of being able to test SQL procedures/triggers you have in your DB (even though I'm not a big fan of them).

## How to implement it ##

The fastest ways to integrate with docker I found online were the following when I wrote this article:

- __Via Maven__ This [plugin](https://github.com/rhuss/docker-maven-plugin) provides features to spin up and shut down a docker image during the building phase.
- __Programmatically__ This [library](https://github.com/shazam/tocker) allows you to interact with docker daemon from your testing code.

The former has the advantage of keeping all the configuration inside the `pom.xml` instead of having it spread across all your test code, on the other side though, it has a coarser grain of control: you can have either all the images running or no one running. Another advantage of using maven plugin is that you can use different profiles for different operating systems and easily configure your code accordingly.

> In OSX, docker daemon starts as a VirtualBox VM where a linux instance is hosted. This will be the place where your images will run, only on linux boxes your container will actually share the host-box resources. You need to consider this when you want to access a system inside an image via network protocol as we will see later. In my case I'm developing on a OSX machine and the build runs on a linux-box with jenkins installed.

Now we have all the pipes and we can start looking into how to assemble them to produce some value. You can find the whole code [here](https://github.com/VisualDNA/DockerIT). Needless to say you need [Docker](https://www.docker.com/) installed on both your development and your CI machine ;-).

### Starting a Docker Image ###

Let's start embedding the plugin inside the `pom.xml`. This is how it looks like

{% highlight xml %}

<plugin>
  <groupId>org.jolokia</groupId>
  <artifactId>docker-maven-plugin</artifactId>
  <version>0.13.6</version>
  <configuration>
   	<useColor>true</useColor>
	<verbose>true</verbose>
	<removeVolumes>true</removeVolumes>
	<images>
	  <image>
	    <name>mysql:5.7.9</name>
	    <run>
		  <env>
		    <MYSQL_ROOT_PASSWORD>
		      ${test.mysql.root.password}
		    </MYSQL_ROOT_PASSWORD>
		  </env>
		  <ports>
		    <port>${test.mysql.port}:3306</port>
		  </ports>
		  <volumes>
		    <bind>
		      <volume>
			    ${project.build.testOutputDirectory}/db-scripts:/tmp/import:ro
			  </volume>
		    </bind>
		  </volumes>
		  <wait>
		    <exec>
		      <postStart>
			    sh /tmp/import/create-database.sh /tmp/import/input.sql
			  </postStart>
		    </exec>
	      </wait>
		</run>
	  </image>
    </images>
  </configuration>
  <executions>
	<execution>
	  <id>start</id>
	  <phase>pre-integration-test</phase>
	  <goals>
	    <goal>start</goal>
	  </goals>
	</execution>
	<execution>
	  <id>stop</id>
	  <phase>post-integration-test</phase>
	  <goals>
	    <goal>stop</goal>
	  </goals>
	</execution>
  </executions>
</plugin>

{% endhighlight %}

This is a typical structure for a maven plugin and we analyze now all the configurations.

- The first two tags in the `<configuration>` are not mandatory for the result, they just make the logging inside `maven` more useful and prettier.
- After those tags you start listing all the images you want to spin up, in our case we have only one image and it's `mysql` version `5.7.9`. In `<env>` you can find all the variable that will be passed to the image, in our case we are passing here the root password for mysql service as suggested on [DockerHub](https://hub.docker.com/_/mysql/). The reason why we are passing it as a maven property will be more clear later on.
- Under `<ports>` we specify the mapping we want for the MySQL internal service to the external port of our container, in this case we want to map the internal `3306` to the external port specified in the properties. This allows us to connect to the service from outside the container.
- Proceeding, we find the `<volumes>` tag, this allows us to make some file accessible from within the container. Here we let the container access our directory `/db-scripts` from the directory `/tmp/import` inside the container but we give it only read permission (`ro`). With this trick we can pass any script inside the container and we will use it to initialize our database. Something really important to remember is to set `removeVolumes` to `true`, this will delete the volume after every build and it won't clutter your disk.
- As we said before, we need to initialize the database, this is what `<postStart>` command does. It runs a script that we pass to the container via the volume and uses a second `SQL` file as argument.
- The last bit of configuration for the plugin is deciding when it starts and stop the images, we want to use them for integration test therefore we want to start them in the `pre-integration-test` phase and stop them in the `post-integration-test` phase.

Having done this, the configuration for the process is ready, as you can see it's really simple but our `pom.xml` is not ready yet, we still miss some details.

### Sharing the DB configuration ###

In the previous part you saw we used some maven properties to configure our image, we exactly used these:

{% highlight xml %}

<test.mysql.port>3306</test.mysql.port>
<test.mysql.root.password>admin</test.mysql.root.password>
<test.mysql.root.username>root</test.mysql.root.username>

{% endhighlight %}

Their meaning is obvious but the reason why we are using them might not be. We use a `maven-resources-plugin` to replace their occurrences in other files in order to make them available for our code  and for our scripts to init the database.

### Init script ###

When you run a MySQL container you have two main problems: understanding when the service is ready and creating the structure of your database. This task is achieved by the scripts we share with the container in the volume mounting.

This is the `SQL` code we want to run in our MySQL instance to initialize it, it creates a database and a table with few attributes for testing purpose. In our codebase it's in the file `input.sql`.

{% highlight sql %}

CREATE DATABASE testDb;
USE testDb;
CREATE TABLE `sample_entity` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `firstAttribute` text NOT NULL,
  `secondAttribute` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

{% endhighlight %}

Having the `SQL` we want to run, here is how we're going to run it:

{% highlight bash %}

#!/bin/bash

while ! echo $(mysql -h 127.0.0.1 -P 3306 -u${test.mysql.root.username} \
	                 -p${test.mysql.root.password} -e "SELECT 'ready'") \
                     | grep --quiet "ready"; do
    sleep 2
    echo "====================================="
    echo "Waiting for mysql Server to be ready"
    echo "====================================="

done

mysql -h 127.0.0.1 -P 3306 -u${test.mysql.root.username} \
      -p${test.mysql.root.password} < $1

{% endhighlight %}

Being aware that this script (`create-database.sh`) will be executed from within the container, the first part of the script sends repeatedly to the MySQL server (`localhost`) the query `SELECT 'ready'` with 2 second interval until the server answers. In the meanwhile it prints the message to show it's not stuck. As soon as the server answers and we grep successfully the word "`ready`" we are good to go and we can send the content of our `input.sql` to the server.
After this the server is ready to be accessed and tested against. Two tasks to go and we are ready to use it.

### Configuration for your code ###

This part really depends on how you're used to pass configurations to your code, for this example we decided to `KISS` hence we just created a `config.properties` to give you an idea. Here is the content of the file:

{% highlight bash %}

db.host = ${test.mysql.host}
db.username = ${test.mysql.root.username}
db.password = ${test.mysql.root.password}
db.port = ${test.mysql.port}
db.database = testDb

{% endhighlight %}

This is the second place where the `maven-resources-plugin` is useful, it replaces this placeholders with whatever we specified in the `pom.xml`, obviously the database name can be parametric as well. We will read this configuration from our test code and we will initialize our driver accordingly.

### Profiles for different OS ###

At the beginning of the article we pointed out that Docker is installed differently on different OSs, this force us to have different configurations. The attentive reader noticed that we didn't specify the `test.mysql.host` variable in the `pom.xml`, we didn't do that on purpose because that's OS dependent. Here is how to define the different profiles:

{% highlight xml %}

<profiles>
  <profile>
    <id>mac</id>
    <build>
	  <plugins>
        <plugin>
	      <groupId>org.codehaus.mojo</groupId>
		  <artifactId>build-helper-maven-plugin</artifactId>
		  <version>1.10</version>
		  <executions>
		    <execution>
              <id>regex-property</id>
              <goals>
                <goal>regex-property</goal>
              </goals>
		      <configuration>
                <name>my.docker.host</name>
		 	    <value>${env.DOCKER_HOST}</value>
			    <regex>\w+:\/\/(.*):\d+</regex>
			    <replacement>$1</replacement>
		      </configuration>
		    </execution>
	      </executions>
        </plugin>
      </plugins>
    </build>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <test.mysql.host>${my.docker.host}</test.mysql.host>
    </properties>
  </profile>
  <profile>
    <id>linux</id>
    <properties>
      <test.mysql.host>localhost</test.mysql.host>
    </properties>
  </profile>
</profiles>

{% endhighlight %}

For the linux one the profile is quite easy, you just specify that the DB host is always localhost because the container runs directly on the host machine and this is the default configuration.
For OSX the profile is more complex given the container runs in the VM. We used `build-helper-maven-plugin` to intercept the environment variable `DOCKER_HOST` and we used a regular expression to extract the host and map it to the `my.docker.host` variable. This variable will be then assigned to the maven property and will be used around in the code.
Now the profiles are ready, you just have to run `mvn -Plinux verify` or `mvn -Pmac verify` and your test will execute against a real MySQL instance.

### Don't forget ###

First thing you don't have to forget is to add `maven-failsafe-plugin` to your `pom.xml` or your integration tests won't run.
If you are a OSX user you also don't have to forget to:

- Start the VM for docker.
- Make sure that you exported environment variables for the VM in the console where you're going to run maven.

## Improvements ##

Often, if the integration tests are a lot, you want to speed up the process, I had an idea about that but I didn't have the time to test it yet:

- Make the MySQL image run in-memory. This is quite easy to achieve, an trivial way could be to declare your table as `MEMORY` as described [here](https://dev.mysql.com/doc/refman/5.7/en/memory-storage-engine.html) but this has some limitations hence my aim is to create a new MySQL image writing files on a [RAM disk](https://en.wikipedia.org/wiki/RAM_drive) instead of on the HardDisk.

This example was really simple, in production systems you may consider something like DbUnit to help managing the database structure and the data, maven properties are your friends whichever framework you choose.

## Conclusions ##

We already started applying this approach in production and it works really well, at the moment we only tested with MySQL but I want to test it also with a more complex system. This doesn't want to be the solution to all your problems but it can be an idea you consider when you test against an external system.
Thanks to Paolo Rascunà for helping out with this idea, even though it's a small thing we faced a lot of obstacles on the way, working in two people is always faster.


text:
"""Web Extraction Tool"""

import sys
from json import dumps
from os import environ, path
from time import clock, time
from functools import partial
from uuid import uuid4 as uuid
from collections import defaultdict
from multiprocessing.pool import ThreadPool
from optparse import OptionGroup, OptionParser

from lxml.html import tostring as doc_to_str

from . import __version__
from .utils import dict_to_text, doc_to_text, fetch_url, is_url, log, parse_html

try:
    from calais import Calais
    from .processors import process_calais
except ImportError:
    Calais = None  # NOQA

USAGE = "%prog [options] [file | url | -]"
VERSION = "%prog v" + __version__
DESCRIPTION = (
    "Tool to extract fragments of a given document by use of CSS Selector(s). "
    "Specify each input filter using the -f/--filter option as a key/value pair with an optional attribute that can be specified after the key. "
    "Example: -f \"key[.attribute]=expression\""
    "If - is provided as the only argument, then for each line of standard output "
    "(assumed to be a url) and the -o/--output option, extraction of each url "
    "will be processed and dumped to the given output path."
)

def calais_options(parser):
    group = OptionGroup(
        parser,
        "Open Calais Options",
        "These options can be used to perform text analysis "
        "with the OpenCalais (http://www.opencalais.com/) "
        "web service. You must specify --calais-key or set "
        "the environment variable CALAIS_KEY to your key."
    )

    group.add_option(
        "", "--calais",
        action="store_true", default=False, dest="calais",
        help="Perform OpenCalais Text Analysis"
    )

    group.add_option(
        "", "--calais-key",
        action="store", type="string", metavar="KEY", default=environ.get("CALAIS_KEY", ""), dest="calais_key",
        help="OpenCalais API Key"
    )

    parser.add_option_group(group)

def parse_options():
    parser = OptionParser(description=DESCRIPTION, usage=USAGE, version=VERSION)

    parser.add_option(
        "-f", "--filter",
        action="append", type="string", metavar="FILTER", default=None, dest="filters",
        help="A CSS selector in the form: key=expression"
    )

    parser.add_option(
        "-j", "--jobs",
        action="store", type="int", metavar="JOBS", default=None, dest="jobs",
        help="Specifies the number of jobs to run simultaneously. Defaults to the no. of CPU(s) on the system."
    )

    parser.add_option(
        "-o", "--output",
        action="store", type="string", metavar="PATH", default=None, dest="output",
        help="An output path to dump results"
    )

    parser.add_option(
        "-v", "--verbose",
        action="store_true", default=False, dest="verbose",
        help="Enable verbose logging"
    )

    if Calais is not None:
        calais_options(parser)

    opts, args = parser.parse_args()

    if len(args) < 1:
        parser.print_help()
        raise SystemExit(1)

    if args and args[0] == "-" and not opts.output:
        print("ERROR: -o/--output is required for when using standard input")
        parser.print_help()
        raise SystemExit(1)

    if opts.output is not None:
        opts.output = path.abspath(path.expanduser(opts.output))
        if not path.isdir(opts.output):
            print("ERROR: -o/--output must be a valid output path.")
            parser.print_help()
            raise SystemExit(1)

    return opts, args

def extract(source, filters):
    filters = dict(filter.split("=") for filter in filters)
    s = fetch_url(source)[1] if is_url(source) else open(source, "r").read()
    doc = parse_html(s)

    result = {}
    for k, v in filters.items():
        es = doc.cssselect(v)
        if "." in k:
            k, a = k.split(".")
            texts = htmls = [e.attrib.get(a, "") for e in es]
        else:
            htmls = [doc_to_str(e) for e in es]
            texts = [doc_to_text(e) for e in es]

        result["_{0:s}".format(k)] = htmls
        result[k] = texts
    return result

def job(opts, source):
    try:
        opts.verbose and log("Processing: {0:s}", source)

        result = extract(source, opts.filters)

        if Calais is not None and opts.calais:
            result.update(process_calais(dict_to_text(result), key=opts.calais_key))

        result["_source"] = source

        if opts.output is not None:
            with open(path.join(opts.output, "{0:s}.json".format(uuid())), "w") as f:
                f.write(dumps(result))
        else:
            print(dumps(result))
        return True, source
    except Exception as e:
        log("Error Processing {0:s} {1:s}", source, e)
        return False, source

def main():
    opts, args = parse_options()

    source = args[0]

    stime = time()

    sources = (line.strip() for line in sys.stdin) if source == "-" else (source,)

    pool = ThreadPool(opts.jobs)

    retries = defaultdict(int)

    while sources:
        results = pool.map(partial(job, opts), sources)
        [retries.pop(source, None) for success, source in results if success]
        retries.update((source, (retries[source] + 1)) for success, source in results if not success)
        sources = [source for source, tries in retries.items() if tries < 3]

    opts.verbose and log("Error(s):")
    [log(source) for source, tries in retries.items() if tries >= 3]

    cputime = clock()
    duration = time() - stime

    opts.verbose and log("Processed in in {0:0.2f}s using {1:0.2f}s of CPU time.", duration, cputime)

if __name__ == "__main__":
    main()


text:
package com.gwtjs.icustom.email;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * ClassName: AppApplication
 * Description:
 *
 * @author aGuang
 *         Date 2016/11/8
 */
@SpringBootApplication
public class MailAppApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(MailAppApplication.class, args);
    }

}


text:
layout: tutorial_frame
title: Video Overlay Tutorial (video with controls)
---


text:
.class public Lcom/samsung/ucm/ucmservice/BlockingServiceConnection;
.super Ljava/lang/Object;
.source "BlockingServiceConnection.java"

# interfaces
.implements Ljava/io/Closeable;

# instance fields
.field private final context:Landroid/content/Context;

.field private final service:Landroid/os/IBinder;

.field private final serviceConnection:Landroid/content/ServiceConnection;

# direct methods
.method private constructor <init>(Landroid/content/Context;Landroid/content/ServiceConnection;Landroid/os/IBinder;)V
    .locals 0

    invoke-direct {p0}, Ljava/lang/Object;-><init>()V

    iput-object p1, p0, Lcom/samsung/ucm/ucmservice/BlockingServiceConnection;->context:Landroid/content/Context;

    iput-object p2, p0, Lcom/samsung/ucm/ucmservice/BlockingServiceConnection;->serviceConnection:Landroid/content/ServiceConnection;

    iput-object p3, p0, Lcom/samsung/ucm/ucmservice/BlockingServiceConnection;->service:Landroid/os/IBinder;

    return-void
.end method

.method public static bindAsUser(Landroid/content/Context;Landroid/content/Intent;Landroid/os/UserHandle;)Lcom/samsung/ucm/ucmservice/BlockingServiceConnection;
    .locals 6
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/lang/InterruptedException;,
            Ljava/lang/IllegalStateException;
        }
    .end annotation

    const/4 v3, 0x1

    if-nez p0, :cond_0

    new-instance v3, Ljava/lang/NullPointerException;

    const-string/jumbo v4, "context == null"

    invoke-direct {v3, v4}, Ljava/lang/NullPointerException;-><init>(Ljava/lang/String;)V

    throw v3

    :cond_0
    if-nez p1, :cond_1

    new-instance v3, Ljava/lang/NullPointerException;

    const-string/jumbo v4, "intent == null"

    invoke-direct {v3, v4}, Ljava/lang/NullPointerException;-><init>(Ljava/lang/String;)V

    throw v3

    :cond_1
    invoke-static {p0}, Lcom/samsung/ucm/ucmservice/BlockingServiceConnection;->ensureNotOnMainThread(Landroid/content/Context;)V

    new-instance v2, Ljava/util/concurrent/LinkedBlockingQueue;

    invoke-direct {v2, v3}, Ljava/util/concurrent/LinkedBlockingQueue;-><init>(I)V

    new-instance v1, Lcom/samsung/ucm/ucmservice/BlockingServiceConnection$1;

    invoke-direct {v1, v2}, Lcom/samsung/ucm/ucmservice/BlockingServiceConnection$1;-><init>(Ljava/util/concurrent/BlockingQueue;)V

    invoke-virtual {p0, p1, v1, v3, p2}, Landroid/content/Context;->bindServiceAsUser(Landroid/content/Intent;Landroid/content/ServiceConnection;ILandroid/os/UserHandle;)Z

    move-result v0

    if-nez v0, :cond_2

    new-instance v3, Ljava/lang/AssertionError;

    new-instance v4, Ljava/lang/StringBuilder;

    invoke-direct {v4}, Ljava/lang/StringBuilder;-><init>()V

    const-string/jumbo v5, "could not bind to: "

    invoke-virtual {v4, v5}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v4

    invoke-virtual {v4, p1}, Ljava/lang/StringBuilder;->append(Ljava/lang/Object;)Ljava/lang/StringBuilder;

    move-result-object v4

    invoke-virtual {v4}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;

    move-result-object v4

    invoke-direct {v3, v4}, Ljava/lang/AssertionError;-><init>(Ljava/lang/Object;)V

    throw v3

    :cond_2
    new-instance v4, Lcom/samsung/ucm/ucmservice/BlockingServiceConnection;

    invoke-interface {v2}, Ljava/util/concurrent/BlockingQueue;->take()Ljava/lang/Object;

    move-result-object v3

    check-cast v3, Landroid/os/IBinder;

    invoke-direct {v4, p0, v1, v3}, Lcom/samsung/ucm/ucmservice/BlockingServiceConnection;-><init>(Landroid/content/Context;Landroid/content/ServiceConnection;Landroid/os/IBinder;)V

    return-object v4
.end method

.method private static ensureNotOnMainThread(Landroid/content/Context;)V
    .locals 3

    invoke-static {}, Landroid/os/Looper;->myLooper()Landroid/os/Looper;

    move-result-object v0

    if-eqz v0, :cond_0

    invoke-virtual {p0}, Landroid/content/Context;->getMainLooper()Landroid/os/Looper;

    move-result-object v1

    if-ne v0, v1, :cond_0

    new-instance v1, Ljava/lang/IllegalStateException;

    const-string/jumbo v2, "calling this from your main thread can lead to deadlock"

    invoke-direct {v1, v2}, Ljava/lang/IllegalStateException;-><init>(Ljava/lang/String;)V

    throw v1

    :cond_0
    return-void
.end method

# virtual methods
.method public close()V
    .locals 3
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    iget-object v1, p0, Lcom/samsung/ucm/ucmservice/BlockingServiceConnection;->context:Landroid/content/Context;

    if-nez v1, :cond_0

    return-void

    :cond_0
    :try_start_0
    iget-object v1, p0, Lcom/samsung/ucm/ucmservice/BlockingServiceConnection;->context:Landroid/content/Context;

    iget-object v2, p0, Lcom/samsung/ucm/ucmservice/BlockingServiceConnection;->serviceConnection:Landroid/content/ServiceConnection;

    invoke-virtual {v1, v2}, Landroid/content/Context;->unbindService(Landroid/content/ServiceConnection;)V
    :try_end_0
    .catch Ljava/lang/Exception; {:try_start_0 .. :try_end_0} :catch_0

    :goto_0
    return-void

    :catch_0
    move-exception v0

    goto :goto_0
.end method

.method public getService()Landroid/os/IBinder;
    .locals 1

    iget-object v0, p0, Lcom/samsung/ucm/ucmservice/BlockingServiceConnection;->service:Landroid/os/IBinder;

    return-object v0
.end method


text:
<?php

require_once('halo_IRequestHelperFactory.php');
class halo_helper_SystemRequestHelperFactory implements halo_IRequestHelperFactory {
    protected $helperNames = array('httpRequest', 'httpResponse');
    public function supports($name) {
        return in_array($name, $this->helperNames);
    }
    public function helper($name, halo_HttpRequest $httpRequest, halo_HttpResponse $httpResponse) {
        switch($name) {
            case 'httpRequest':
                return $httpRequest;
                break;
            case 'httpResponse':
                return $httpResponse;
                break;
            default:
                return null;
                break;
        }
    }
}


text:
/* The global object in the browser is the 'window' object.

 It can be different if javascript is being run on the server, like NodeJS.

 'this' is set to the global object by default.

*/

var a = "Hello world";

function b() {

}

/* When you run Javascript, an execution context is created.

	At the base level, when you define variables outside of a function (in the global execution context)

	they get attached to the global object.

*/


text:
from setuptools import setup

from yolk.__init__ import __version__ as VERSION

setup(name="yolk",
    license = "BSD License",
    version=VERSION,
    description="Command-line tool for querying PyPI and Python packages installed on your system.",
    long_description=open("README", "r").read(),
    maintainer="Rob Cakebread",
    author="Rob Cakebread",
    author_email="cakebread @ gmail",
    url="https://github.com/cakebread/yolk",
    keywords="PyPI setuptools cheeseshop distutils eggs package management",
    classifiers=["Development Status :: 4 - Beta",
                 "Intended Audience :: Developers",
                 "License :: OSI Approved :: BSD License",
                 "Programming Language :: Python",
                 "Topic :: Software Development :: Libraries :: Python Modules",
                 ],
    install_requires=["setuptools"],
    packages=['yolk', 'yolk.plugins'],
    package_dir={'yolk':'yolk'},
    entry_points={'console_scripts': ['yolk = yolk.cli:main',]},
)


text:
<!DOCTYPE html>

    <link rel="stylesheet" media="all" href="/smartanswers/application.css" />
    <link rel="stylesheet" media="print" href="/smartanswers/print.css" />
    <title>Calculate your Married Couple's Allowance - GOV.UK</title>

    <meta name="robots" content="noindex">

        Calculate your Married Couple's Allowance

      <form action="/calculate-married-couples-allowance/y/yes/yes/1950-01-01" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓" />

    What's the husband's yearly income?

Add up your taxable income, eg earnings, pensions and any taxable benefits, eg Carer's Allowance.

      £ <input type="text" name="response" id="response" size="5" />

            <input type="hidden" name="next" value="1" />
            <button type="submit" class="medium button">Next step</button>

      </form>

            Previous answers

          Start again

  Were you or your partner born before 6 April 1935?

    Yes

      Change answer to "Were you or your partner born before 6 April 1935?"

  Did you marry before 5 December 2005?

    Yes

      Change answer to "Did you marry before 5 December 2005?"

  What's the husband's date of birth?

     1 January 1950

      Change answer to "What's the husband's date of birth?"

Last updated:  5 May 2016


text:
import message from '@conveyal/woonerf/message'
import React, {PureComponent} from 'react'
import Select from 'react-select'

import {Group as FormGroup} from '../input'
import MinutesSeconds from '../minutes-seconds'
import {toString as timetableToString} from '../../utils/timetable'
import type {ClearableReactSelectResult, GTFSStop, Timetable} from '../../types'

type Props = {
  availableTimetables: Timetable[],
  disabled: boolean,
  modificationStops: GTFSStop[],
  phaseAtStop: null | string,
  phaseFromStop: null | string,
  phaseFromTimetable: null | string,
  phaseSeconds: number,
  selectedPhaseFromTimetableStops: GTFSStop[],
  timetableHeadway: number,

  // actions
  update: (any) => void
}

type State = {
  selectedTimetable: void | Timetable
}

export default class Phase extends PureComponent<void, Props, State> {
  state = {
    selectedTimetable: getSelectedTimetable(this.props)
  }

  componentWillReceiveProps (nextProps: Props) {
    const selectedTimetable = getSelectedTimetable(nextProps)
    this.setState({selectedTimetable})
    if (nextProps.phaseFromTimetable && !selectedTimetable) {
      this._setPhaseFromTimetable(null) // timetable does not exist
    }
  }

  componentDidMount () {
    if (this.props.phaseFromTimetable && !this.state.selectedTimetable) {
      this._setPhaseFromTimetable(null)
    }
  }

  _setPhaseAtStop = (stop: ClearableReactSelectResult) => {
    this.props.update({phaseAtStop: stop && stop.value})
  }

  _setPhaseFromTimetable = (timetable: ClearableReactSelectResult) => {
    this.props.update({
      phaseFromTimetable: timetable && timetable.value,
      phaseFromStop: null
    })
  }

  _setPhaseFromStop = (stop: ClearableReactSelectResult) => {
    this.props.update({phaseFromStop: stop && stop.value})
  }

  _setPhaseSeconds = (phaseSeconds: number) => {
    this.props.update({phaseSeconds})
  }

  render () {
    const {
      availableTimetables,
      disabled,
      phaseAtStop,
      phaseFromStop,
      phaseFromTimetable,
      phaseSeconds,
      modificationStops,
      selectedPhaseFromTimetableStops,
      timetableHeadway
    } = this.props
    const {selectedTimetable} = this.state
    return (

        <FormGroup label={message('phasing.atStop')}>
          <Select
            disabled={disabled}
            name={message('phasing.atStop')}
            onChange={this._setPhaseAtStop}
            options={modificationStops.map(stop => ({
              value: stop.stop_id,
              label: stop.stop_name
            }))}
            placeholder={message('phasing.atStop')}
            value={phaseAtStop}
          />
        </FormGroup>
        {disabled &&

            {message('phasing.disabled')}

}
        {phaseAtStop &&

            <FormGroup label={message('phasing.fromTimetable')}>
              <Select
                name={message('phasing.fromTimetable')}
                onChange={this._setPhaseFromTimetable}
                options={availableTimetables.map(timetable => ({
                  value: `${timetable.modificationId}:${timetable._id}`,
                  label: `${timetable.modificationName}: ${timetableToString(timetable)}`
                }))}
                placeholder={message('phasing.fromTimetable')}
                value={phaseFromTimetable}
              />
            </FormGroup>
            {phaseFromTimetable &&
              selectedTimetable &&

                {selectedTimetable.headwaySecs !== timetableHeadway &&

                    {message('phasing.headwayMismatchWarning', {
                      selectedTimetableHeadway: selectedTimetable.headwaySecs /
                        60
                    })}

}
                {selectedPhaseFromTimetableStops &&
                  selectedPhaseFromTimetableStops.length > 0
                  ? <FormGroup label={message('phasing.fromStop')}>
                    <Select
                      name={message('phasing.fromStop')}
                      onChange={this._setPhaseFromStop}
                      options={selectedPhaseFromTimetableStops.map(stop => ({
                        value: stop.stop_id,
                        label: stop.stop_name
                      }))}
                      placeholder={message('phasing.fromStop')}
                      value={phaseFromStop}
                    />
                  </FormGroup>
                  :

                    {message('phasing.noAvailableStopsWarning')}

}
                {phaseFromStop &&
                  <MinutesSeconds
                    label={message('phasing.minutes')}
                    onChange={this._setPhaseSeconds}
                    seconds={phaseSeconds}
                  />}

}

}

    )
  }
}

function getSelectedTimetable (opts): void | Timetable {
  return opts.availableTimetables.find(tt =>
    opts.phaseFromTimetable && tt._id === opts.phaseFromTimetable.split(':')[1]
  )
}


text:
{% extends path+"/_layout-case-simple14b.html" %}

{% block actioncolumn %}

Return to case list

 {% if data['pbachoice']=='yes'%}

Ready for paper based assessment

Start assessment

{% else %}

Appointment booked

{% endif %}

{% endblock %}

{% block citizen_detail %}
Harvey Chamberlain
{% endblock %}

{% block citizen_content %}

{% if data['pbachoice'] %}

{% else %}

New evidence recorded

	         1

Bobby's case should be reviewed before making a recommendation

	         2

Make recommendation

{% endif %}

    {# macro #}
  {{ nav.case_tabs("timeline-fme",1,path1, customer._id) }}

{% if data['pbachoice']=='yes'%}

Referred for paper based assessment

24th June 2019

Appointment cancelled

24th June 2019

GP Report - evidence reviewed

24th June 2019

{% elif data['pbachoice']=='no'%}

GP Report - evidence reviewed

24th June 2019

{% endif %}

GP Report - medical evidence recorded

24th June 2019

Appointment letter sent

20th June 2019

GP Report - medical evidence overdue

19th June 2019

Appointment booked

8th June 2019

Referred for appointment

4th June 2019

GP Report - medical evidence requested

4th June 2019

Med 3 - medical evidence recorded

2nd June 2019

UC50 form recorded

1st June 2019

UC50 form posted

25th May 2019

New referral created

25th May 2019

{% endblock %}

{# {% block page_script %}

{% endblock %} #}


text:
$(document).ready(function() {

    /* ======= DEMO THEME CONFIG ====== */
	$('#config-trigger').click(function(e){

		e.preventDefault();

		//$("#config-panel").toggleClass('config-panel-open');

		if($(this).hasClass('config-panel-open')){
          $("#config-panel").animate({
            right: "-=190" //same as the panel width
           }, 500);
          $(this).removeClass('config-panel-open').addClass('config-panel-hide');
         }
         else {
         $("#config-panel").animate({
           right: "+=190" //same as the panel width
           }, 500);
          $(this).removeClass('config-panel-hide').addClass('config-panel-open');
         }
	});

    $('#config-close').on('click', function(e) {
        e.preventDefault();
        $('#config-trigger').click();
    });

    $('#color-options a').on('click', function(e) {
        var $styleSheet = $(this).attr('data-style');
        var $chartColor = $(this).attr('data-chart');

		$('#theme-style').attr('href', $styleSheet);

		var $listItem = $(this).closest('li');
		$listItem.addClass('active');
		$listItem.siblings().removeClass('active');

		e.preventDefault();

        // On click destory the current pie chart
        $('.chart').data('easy-pie-chart', null);

        //Create a new pie chart with a new colour
		$('.chart').easyPieChart({
    		barColor: $chartColor,
    		trackColor: '#e8e8e8',
    		scaleColor: false,
    		lineWidth : 5,
    		animate: 2000,
    		onStep: function(from, to, percent) {
    			$(this.el).find('span').text(Math.round(percent));
    		}
    	});

	});

});


text:
@interface ScanHistoryContoller : RootViewController

@end


text:
# S3 storage driver

An implementation of the `storagedriver.StorageDriver` interface which uses Amazon S3 or S3 compatible services for object storage.

## Parameters

    Parameter
    Required
    Description

      accesskey

      yes

      Your AWS Access Key.

      secretkey

      yes

      Your AWS Secret Key.

      region

      yes

      The AWS region in which your bucket exists. For the moment, the Go AWS
      library in use does not use the newer DNS based bucket routing.

      regionendpoint

      no

      Endpoint for S3 compatible storage services (Minio, etc)

      bucket

      yes

      The bucket name in which you want to store the registry's data.

      encrypt

      no

       Specifies whether the registry stores the image in encrypted format or
       not. A boolean value. The default is false.

      keyid

      no

       Optional KMS key ID to use for encryption (encrypt must be true, or this
       parameter will be ignored). The default is none.

      secure

      no

      Indicates whether to use HTTPS instead of HTTP. A boolean value. The
      default is true.

      v4auth

      no

      Indicates whether the registry uses Version 4 of AWS's authentication.
      Generally, you should set this to true. By default, this is
      false.

      chunksize

      no

      The S3 API requires multipart upload chunks to be at least 5MB. This value
      should be a number that is larger than 5*1024*1024.

      rootdirectory

      no

      This is a prefix that will be applied to all S3 keys to allow you to segment data in your bucket if necessary.

      storageclass

      no

      The S3 storage class applied to each registry file. The default value is STANDARD.

`accesskey`: Your aws access key.

`secretkey`: Your aws secret key.

**Note** You can provide empty strings for your access and secret keys if you plan on running the driver on an ec2 instance and will handle authentication with the instance's credentials.

`region`: The name of the aws region in which you would like to store objects (for example `us-east-1`). For a list of regions, you can look at http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html

`regionendpoint`: (optional) Endpoint URL for S3 compatible APIs. This should not be provided when using Amazon S3.

`bucket`: The name of your S3 bucket where you wish to store objects. The bucket must exist prior to the driver initialization.

`encrypt`: (optional) Whether you would like your data encrypted on the server side (defaults to false if not specified).

`keyid`: (optional) Whether you would like your data encrypted with this KMS key ID (defaults to none if not specified, will be ignored if encrypt is not true).

`secure`: (optional) Whether you would like to transfer data to the bucket over ssl or not. Defaults to true (meaning transferring over ssl) if not specified. Note that while setting this to false will improve performance, it is not recommended due to security concerns.

`v4auth`: (optional) Whether you would like to use aws signature version 4 with your requests. This defaults to false if not specified (note that the eu-central-1 region does not work with version 2 signatures, so the driver will error out if initialized with this region and v4auth set to false)

`chunksize`: (optional) The default part size for multipart uploads (performed by WriteStream) to S3. The default is 10 MB. Keep in mind that the minimum part size for S3 is 5MB. Depending on the speed of your connection to S3, a larger chunk size may result in better performance; faster connections will benefit from larger chunk sizes.

`rootdirectory`: (optional) The root directory tree in which all registry files will be stored. Defaults to the empty string (bucket root).

`storageclass`: (optional) The storage class applied to each registry file. Defaults to STANDARD. Valid options are STANDARD and REDUCED_REDUNDANCY.

## S3 permission scopes

The following IAM permissions are required by the registry for push and pull.  See [the S3 policy documentation](http://docs.aws.amazon.com/AmazonS3/latest/dev/mpuAndPermissions.html) for more details.

```
 "Statement": [
      {
        "Effect": "Allow",
        "Action": [
          "s3:ListBucket",
          "s3:GetBucketLocation",
          "s3:ListBucketMultipartUploads"
        ],
        "Resource": "arn:aws:s3:::mybucket"
      },
      {
        "Effect": "Allow",
        "Action": [
          "s3:PutObject",
          "s3:GetObject",
          "s3:DeleteObject",
          "s3:ListMultipartUploadParts",
          "s3:AbortMultipartUpload"
        ],
        "Resource": "arn:aws:s3:::mybucket/*"
      }
]
```

# CloudFront as Middleware with S3 backend

## Use Case

Adding CloudFront as a middleware for your S3 backed registry can dramatically improve pull times. Your registry will have the ability to retrieve your images from edge servers, rather than the geographically limited location of your S3 bucket. The farther your registry is from your bucket, the more improvements you will see. See [Amazon CloudFront](https://aws.amazon.com/cloudfront/details/).

## Configuring CloudFront for Distribution

If you are unfamiliar with creating a CloudFront distribution, see [Getting Started with Cloudfront](http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/GettingStarted.html).

Defaults can be kept in most areas except:

### Origin:

The CloudFront distribution must be created such that the `Origin Path` is set to the directory level of the root "docker" key in S3. If your registry exists on the root of the bucket, this path should be left blank.

### Behaviors:

  - Viewer Protocol Policy: HTTPS Only
  - Allowed HTTP Methods: GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE
  - Cached HTTP Methods: OPTIONS (checked)
  - Restrict Viewer Access (Use Signed URLs or Signed Cookies): Yes
    - Trusted Signers: Self (Can add other accounts as long as you have access to CloudFront Key Pairs for those additional accounts)

## Registry configuration

Here the `middleware` option is used. It is still important to keep the `storage` option as CloudFront will only handle `pull` actions; `push` actions are still directly written to S3.

The following example shows what you will need at minimum:
```
...
storage:
  s3:
    region: us-east-1
    bucket: docker.myregistry.com
middleware:
  storage:
    - name: cloudfront
      options:
        baseurl: https://abcdefghijklmn.cloudfront.net/
        privatekey: /etc/docker/cloudfront/pk-ABCEDFGHIJKLMNOPQRST.pem
        keypairid: ABCEDFGHIJKLMNOPQRST
...
```

## CloudFront Key-Pair

A CloudFront key-pair is required for all AWS accounts needing access to your CloudFront distribution. For information, please see [Creating CloudFront Key Pairs](http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-trusted-signers.html#private-content-creating-cloudfront-key-pairs).


text:
//
//  ICJSON.h
//
// The MIT License (MIT)
//
// Copyright (c) 2013 Toly Pochkin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#import <Foundation/Foundation.h>

/// ------------------------------------------------------------------------------------------------------------------
/// JSON transform rule.
/// ------------------------------------------------------------------------------------------------------------------
@interface ICJSONRule : NSObject

@property (nonatomic, copy, readonly) NSString* path;
@property (nonatomic, readonly) SEL property;
@property (nonatomic, readonly) Class objectClass;

/// Create JSON transform rule for binding class to path
+ (ICJSONRule*)bindObjectClass:(Class)objectClass toPath:(NSString*)path;

/// Create JSON transform rule for binding class property to path
+ (ICJSONRule*)bindProperty:(SEL)property toPath:(NSString*)path;

/// Initialize JSON transform rule by binding path, class property and class together
- (id)initWithPath:(NSString*)path property:(SEL)property objectClass:(Class)objectClass;

@end

/// ------------------------------------------------------------------------------------------------------------------
/// JSON object helper.
/// ------------------------------------------------------------------------------------------------------------------
@interface ICJSON : NSObject

/// Deserialize JSON data and create Objective-C object.
+ (id)fromJSON:(NSData*)data rules:(NSArray* /*of<ICJSON>*/)transformRules error:(NSError**)error;

/// Not implemented yet.
+ (NSString*)toJSON:(id)object rules:(NSArray* /*of<ICJSON>*/)transformRules error:(NSError**)error;

@end


text:
package lila.game

import akka.actor._
import akka.pattern.ask
import chess.format.Forsyth
import chess.format.pgn.{ Pgn, Tag }
import chess.format.{ pgn => chessPgn }
import chess.OpeningExplorer
import makeTimeout.short
import org.joda.time.format.DateTimeFormat

import lila.common.LightUser

final class PgnDump(
    netBaseUrl: String,
    getLightUser: String => Option[LightUser]) {

  import PgnDump._

  def apply(game: Game, initialFen: Option[String]): Pgn = {
    val ts = tags(game, initialFen)
    val fenSituation = ts find (_.name == Tag.FEN) flatMap { case Tag(_, fen) => Forsyth <<< fen }
    val moves2 = fenSituation.??(_.situation.color.black).fold(".." :: game.pgnMoves, game.pgnMoves)
    Pgn(ts, turns(moves2, fenSituation.map(_.fullMoveNumber) | 1))
  }

  def filename(game: Game): String = gameLightUsers(game) match {
    case (wu, bu) => "lichess_pgn_%s_%s_vs_%s.%s.pgn".format(
      dateFormat.print(game.createdAt),
      player(game.whitePlayer, wu),
      player(game.blackPlayer, bu),
      game.id)
  }

  private def gameUrl(id: String) = s"$netBaseUrl/$id"

  private def gameLightUsers(game: Game): (Option[LightUser], Option[LightUser]) =
    (game.whitePlayer.userId ?? getLightUser) -> (game.blackPlayer.userId ?? getLightUser)

  private val dateFormat = DateTimeFormat forPattern "yyyy.MM.dd";

  private def rating(p: Player) = p.rating.fold("?")(_.toString)

  private def player(p: Player, u: Option[LightUser]) =
    p.aiLevel.fold(u.fold(p.name | lila.user.User.anonymous)(_.name))("lichess AI level " + _)

  private val customStartPosition: Set[chess.variant.Variant] =
    Set(chess.variant.Chess960, chess.variant.FromPosition, chess.variant.Horde)

  private def tags(game: Game, initialFen: Option[String]): List[Tag] = gameLightUsers(game) match {
    case (wu, bu) => List(
      Tag(_.Event,
        if (game.imported || game.isFicsRelay) "Import"
        else game.rated.fold("Rated game", "Casual game")),
      Tag(_.Site, gameUrl(game.id)),
      Tag(_.Date, dateFormat.print(game.createdAt)),
      Tag(_.White, player(game.whitePlayer, wu)),
      Tag(_.Black, player(game.blackPlayer, bu)),
      Tag(_.Result, result(game)),
      Tag("WhiteElo", rating(game.whitePlayer)),
      Tag("BlackElo", rating(game.blackPlayer)),
      Tag("PlyCount", game.turns),
      Tag(_.Variant, game.variant.name.capitalize),
      Tag(_.TimeControl, game.clock.fold("-") { c => s"${c.limit}+${c.increment}" }),
      Tag(_.ECO, game.opening.fold("?")(_.code)),
      Tag(_.Opening, game.opening.fold("?")(_.name)),
      Tag(_.Termination, {
        import chess.Status._
        game.status match {
          case Created | Started                             => "Unterminated"
          case Aborted | NoStart                             => "Abandoned"
          case Timeout | Outoftime                           => "Time forfeit"
          case Resign | Draw | Stalemate | Mate | VariantEnd => "Normal"
          case Cheat                                         => "Rules infraction"
        }
      })
    ) ::: customStartPosition(game.variant).??(List(
        Tag(_.FEN, initialFen | "?"),
        Tag("SetUp", "1")
      ))
  }

  private def turns(moves: List[String], from: Int): List[chessPgn.Turn] =
    (moves grouped 2).zipWithIndex.toList map {
      case (moves, index) => chessPgn.Turn(
        number = index + from,
        white = moves.headOption filter (".." !=) map { chessPgn.Move(_) },
        black = moves lift 1 map { chessPgn.Move(_) })
    } filterNot (_.isEmpty)
}

object PgnDump {

  def result(game: Game) = game.finished.fold(
    game.winnerColor.fold("1/2-1/2")(color => color.white.fold("1-0", "0-1")),
    "*")
}


text:
package desktop

// taken from https://bitbucket.org/tebeka/go-wise/src/tip/desktop.go

import (
	"fmt"
	"os/exec"
	"runtime"
	"strings"
)

var openCommands = map[string][]string{
	"windows": []string{"cmd", "/c", "start"},
	"darwin":  []string{"open"},
	"linux":   []string{"xdg-open"},
}

func Open(uri string) error {
	run, ok := openCommands[runtime.GOOS]
	if !ok {
		return fmt.Errorf("don't know how to open things on %s platform", runtime.GOOS)
	}
	if runtime.GOOS == "windows" {
		uri = strings.Replace(uri, "&", "^&", -1)
	}
	run = append(run, uri)
	cmd := exec.Command(run[0], run[1:]...)
	return cmd.Start()
}


text:
var express = require('express');
var router = express.Router();
var utils = require('../lib/utils.js');

router.get("/",function(req, res, next){
    req.app.locals.orcinus.cluster(function (err, data) {
        if(err){
            res.status(err.statusCode).send({error : err.reason});
        }
        else{
            res.send(data);
        }
    });
});

router.post("/init",function(req, res, next){
    var opt  = req.body;
    req.app.locals.orcinus.clusterInit(opt,function (err, data) {
        if(err){
            res.status(err.statusCode).send({error : err.reason});
        }
        else{
            res.send(data);
        }
    });
});

module.exports = router;