text:
import Ember from 'ember';
import DS from 'ember-data';

export default DS.RESTSerializer.extend({

  primaryKey: 'objectId',

  extractArray(store, primaryType, payload) {
    var namespacedPayload = {};
    namespacedPayload[Ember.String.pluralize(primaryType.modelName)] = payload.results;

    return this._super(store, primaryType, namespacedPayload);
  },

  extractSingle(store, primaryType, payload, recordId) {
    var namespacedPayload = {};
    namespacedPayload[primaryType.modelName] = payload; // this.normalize(primaryType, payload);

    return this._super(store, primaryType, namespacedPayload, recordId);
  },

  typeForRoot(key) {
    return Ember.String.dasherize(Ember.String.singularize(key));
  },

  /**
  * Because Parse only returns the updatedAt/createdAt values on updates
  * we have to intercept it here to assure that the adapter knows which
  * record ID we are dealing with (using the primaryKey).
  */
  extract(store, type, payload, id, requestType) {
    if (id !== null && ('updateRecord' === requestType || 'deleteRecord' === requestType)) {
      payload[this.get('primaryKey')] = id;
    }

    return this._super(store, type, payload, id, requestType);
  },

  /**
  * Extracts count from the payload so that you can get the total number
  * of records in Parse if you're using skip and limit.
  */
  extractMeta(store, type, payload) {
    if (payload && payload.count) {
      store.metaForType(type, { count: payload.count });
      delete payload.count;
    }
  },

  /**
  * Special handling for the Date objects inside the properties of
  * Parse responses.
  */
  normalizeAttributes(type, hash) {
    type.eachAttribute(function(key, meta) {
      if ('date' === meta.type && 'object' === Ember.typeOf(hash[key]) && hash[key].iso) {
        hash[key] = hash[key].iso; //new Date(hash[key].iso).toISOString();
      }
    });

    this._super(type, hash);
  },

  normalizeRelationships(type, hash) {
    if (this.keyForRelationship) {
      type.eachRelationship(function(key, relationship) {
        if (hash[key] && 'belongsTo' === relationship.kind) {
          hash[key] = hash[key].objectId;
        }

        /*
         * TODO: Find a better way to do this
         * Here we set the links property to a serialized version
         * of key and className. This info will be passed to
         * adapter.findHasMany where we can deserialize to create
         * the needed Parse query.
         */
        if (hash[key] && 'hasMany' === relationship.kind) {
          if (!hash[key].__op && hash[key].__op !== 'AddRelation') {
            if (!hash.links) {
              hash.links = {};
            }
            hash.links[key] = JSON.stringify({
              key: key,
              className: hash[key].className
            });
          }

          delete hash[key].__type;
          delete hash[key].className;
          hash[key] = [];
        }
      }, this);
    }
  },

  serializeAttribute(record, json, key, attribute) {
    // These are Parse reserved properties and we won't send them.
    if ('createdAt' === key ||
        'updatedAt' === key ||
        'emailVerified' === key ||
        'sessionToken' === key ||
        'password' === key
    ) {
      delete json[key];

    } else {
      this._super(record, json, key, attribute);
    }
  },

  serializeBelongsTo(record, json, relationship) {
    var key = relationship.key,
        belongsTo = record.belongsTo(key);

    if (belongsTo) {
      // @TODO: Perhaps this is working around a bug in Ember-Data? Why should
      // promises be returned here.
      if (belongsTo instanceof DS.PromiseObject) {
        if (!belongsTo.get('isFulfilled')) {
          throw new Error('belongsTo values *must* be fulfilled before attempting to serialize them');
        }

        belongsTo = belongsTo.get('content');
      }

      var _className = this.parseClassName(belongsTo.type.modelName);

      if (_className === 'User') {
        _className = '_User';
      }

      json[key] = {
        '__type': 'Pointer',
        'className': _className,
        'objectId': belongsTo.id
      };

    }
  },

  serializeIntoHash(hash, type, snapshot, options) {
    var ParseACL = snapshot.record.get('ParseACL');

    // Add ACL
    if (ParseACL) {
      var policy = {};

      if (ParseACL.owner) {
        policy[ParseACL.owner] = {};
      }

      if (ParseACL.permissions) {
        policy[ParseACL.owner] = ParseACL.permissions;
      } else {
        policy[ParseACL.owner] = {
          read: true,
          write: true
        };
      }
      hash.ACL = policy;
    }

    Ember.merge(hash, this.serialize(snapshot, options));
  },

  parseClassName(key) {
    if ('User' === key) {
      return '_User';

    } else {
      return Ember.String.capitalize(Ember.String.camelize(key));
    }
  },

  serializeHasMany(snapshot, json, relationship) {
    var key = relationship.key;

    if (this._canSerialize(key)) {
      var payloadKey;

      json[key] = { 'objects': [] };

      // if provided, use the mapping provided by `attrs` in
      // the serializer
      payloadKey = this._getMappedKey(key);
      if (payloadKey === key && this.keyForRelationship) {
        payloadKey = this.keyForRelationship(key, "hasMany");
      }

      var relationshipType = snapshot.type.determineRelationshipType(relationship);

      if (relationshipType === 'manyToNone' || relationshipType === 'manyToMany' || relationshipType === 'manyToOne') {
        var objects = [],
            objectsForKey = snapshot.hasMany(key),
            objectsBeforeUpdate = snapshot.record._internalModel._relationships.get(key).canonicalMembers,
            operation = 'AddRelation';

        // Check if this is removing the last relation for a key
        if (objectsForKey.length === 0 && objectsBeforeUpdate.size === 1) {
          // Removing the last relation
          operation = 'RemoveRelation';

          objects.push({
            __type: 'Pointer',
            className: this.parseClassName(snapshot.type.typeForRelationship(key).modelName),
            objectId: objectsBeforeUpdate.list[0].id
          });
        }

        // Determine if we are adding or removing a relationship
        objectsForKey.forEach((item) => {
          if (objectsForKey.length < objectsBeforeUpdate.size) {
            // Remove existing relation
            //
            // Parse needs an array of the objects we want
            // to remove so we have to invert the object list
            // to contain items to remove
            operation = 'RemoveRelation';

            var objectsToKeepIds = objectsForKey.map(function(obj) {
              return obj.id;
            });

            objectsBeforeUpdate.list.forEach((obj) => {
              if (objectsToKeepIds.indexOf(obj.id) < 0) {
                objects.push({
                  __type: 'Pointer',
                  className: this.parseClassName(item.modelName),
                  objectId: obj.id
                });
              }
            });

          } else {
            // Add a new relation
            // (objectsForKey.length > objectsBeforeUpdate.size)
            objects.push({
              __type: 'Pointer',
              className: this.parseClassName(item.modelName),
              objectId: item.id
            });
          }

        });

        json[payloadKey] = {
          __op: operation,
          objects: objects,
          className: this.parseClassName(snapshot.type.modelName)
        };
        // TODO support for polymorphic manyToNone and manyToMany relationships
      }
    }
  }

});

redpajama_set_name: RedPajamaGithub


text:
package com.squareup.differentpackage.protos.foo;

import com.squareup.differentpackage.protos.bar.Bar;
import com.squareup.wire.FieldEncoding;
import com.squareup.wire.Message;
import com.squareup.wire.ProtoAdapter;
import com.squareup.wire.ProtoReader;
import com.squareup.wire.ProtoWriter;
import java.io.IOException;
import java.lang.Object;
import java.lang.Override;
import java.lang.String;
import java.lang.StringBuilder;
import okio.ByteString;

public final class Foo extends Message<Foo, Foo.Builder> {
  public static final ProtoAdapter<Foo> ADAPTER = new ProtoAdapter<Foo>(FieldEncoding.LENGTH_DELIMITED, Foo.class) {
    @Override
    public int encodedSize(Foo value) {
      return (value.moo != null ? Bar.Baz.Moo.ADAPTER.encodedSizeWithTag(1, value.moo) : 0)
          + value.unknownFields().size();
    }

    @Override
    public void encode(ProtoWriter writer, Foo value) throws IOException {
      if (value.moo != null) Bar.Baz.Moo.ADAPTER.encodeWithTag(writer, 1, value.moo);
      writer.writeBytes(value.unknownFields());
    }

    @Override
    public Foo decode(ProtoReader reader) throws IOException {
      Builder builder = new Builder();
      long token = reader.beginMessage();
      for (int tag; (tag = reader.nextTag()) != -1;) {
        switch (tag) {
          case 1: builder.moo(Bar.Baz.Moo.ADAPTER.decode(reader)); break;
          default: {
            FieldEncoding fieldEncoding = reader.peekFieldEncoding();
            Object value = fieldEncoding.rawProtoAdapter().decode(reader);
            builder.addUnknownField(tag, fieldEncoding, value);
          }
        }
      }
      reader.endMessage(token);
      return builder.build();
    }

    @Override
    public Foo redact(Foo value) {
      Builder builder = value.newBuilder();
      if (builder.moo != null) builder.moo = Bar.Baz.Moo.ADAPTER.redact(builder.moo);
      builder.clearUnknownFields();
      return builder.build();
    }
  };

  private static final long serialVersionUID = 0L;

  public final Bar.Baz.Moo moo;

  public Foo(Bar.Baz.Moo moo) {
    this(moo, ByteString.EMPTY);
  }

  public Foo(Bar.Baz.Moo moo, ByteString unknownFields) {
    super(unknownFields);
    this.moo = moo;
  }

  @Override
  public Builder newBuilder() {
    Builder builder = new Builder();
    builder.moo = moo;
    builder.addUnknownFields(unknownFields());
    return builder;
  }

  @Override
  public boolean equals(Object other) {
    if (other == this) return true;
    if (!(other instanceof Foo)) return false;
    Foo o = (Foo) other;
    return equals(unknownFields(), o.unknownFields())
        && equals(moo, o.moo);
  }

  @Override
  public int hashCode() {
    int result = super.hashCode;
    if (result == 0) {
      result = unknownFields().hashCode();
      result = result * 37 + (moo != null ? moo.hashCode() : 0);
      super.hashCode = result;
    }
    return result;
  }

  @Override
  public String toString() {
    StringBuilder builder = new StringBuilder();
    if (moo != null) builder.append(", moo=").append(moo);
    return builder.replace(0, 2, "Foo{").append('}').toString();
  }

  public static final class Builder extends Message.Builder<Foo, Builder> {
    public Bar.Baz.Moo moo;

    public Builder() {
    }

    public Builder moo(Bar.Baz.Moo moo) {
      this.moo = moo;
      return this;
    }

    @Override
    public Foo build() {
      return new Foo(moo, buildUnknownFields());
    }
  }
}


text:
import * as React from 'react';
import { getSettings, Settings, addSettingsListener } from '../settings';
import { AddressBarActions } from '../reducers';
import { IUser } from '../../types/userTypes';
import { uniqueId } from '../../shared/utils';

export class ConversationSettingsDialog extends React.Component<{}, {}> {
    settingsUnsubscribe: any;
    showing: boolean;

    pageClicked = (ev: Event) => {
        if (ev.defaultPrevented)
            return;
        let target = ev.srcElement;
        while (target) {
            if (target.className === "conversationsettings-dialog") {
                ev.preventDefault();
                return;
            }
            target = target.parentElement;
        }

        // Click was outside the dialog. Close.
        this.onClose();
    }

    onSaveUserChanges = (users, currentUser) => {
        AddressBarActions.hideConversationSettings();
    }

    onClose = () => {
        AddressBarActions.hideConversationSettings();
    }

    componentWillMount() {
        window.addEventListener('click', this.pageClicked);
        this.settingsUnsubscribe = addSettingsListener((settings: Settings) => {
            if (settings.addressBar.showConversationSettings != this.showing) {
                this.showing = settings.addressBar.showConversationSettings;
                this.forceUpdate();
            }
        });
    }

    componentWillUnmount() {
        window.removeEventListener('click', this.pageClicked);
        this.settingsUnsubscribe();
    }

    render() {
        const settings = getSettings();
        if (!settings.addressBar.showConversationSettings) return null;
        return (

                    <CloseButton className='conversationsettings-closex' onClick={() => this.onClose()} />
                    <UserList
                        className='conversationsettings-userlist'
                        users={[]}
                        currentUser={{ id: uniqueId(), name: 'User 1' }}
                        onSave={(users, currentUser) => this.onSaveUserChanges(users, currentUser)}
                        onCancel={() => this.onClose()} />

        );
    }
}

interface ICloseButtonProps {
    className: string;
    onClick();
}

class CloseButton extends React.Component<ICloseButtonProps, {}> {
    render() {
        return  this.props.onClick()}>[x]
    }
}

interface IUserListProps {
    className: string;
    users: IUser[];
    currentUser: IUser;
    onSave(users: IUser[], currentUser: IUser);
    onCancel();
}

interface IUserListState {
    users: IUser[];
    currentUser: IUser;
}

class UserList extends React.Component<IUserListProps, IUserListState> {
    constructor(props) {
        super(props);
        this.state = {
            users: this.props.users,
            currentUser: this.props.currentUser
        }
    }
    render() {
        return (

Manage Users

                    <button className='save-button' onClick={() => this.props.onSave(this.state.users, this.state.currentUser)}>Save</button>
                    <button className='cancel-button' onClick={() => this.props.onCancel()}>Cancel</button>

        )
    }
}


text:
wget http://sourceforge.net/projects/libpng/files/libpng15/older-releases/1.5.12/lpng1512.zip

unzip lpng1512.zip && mv lpng1512 ../../

cp ../../lpng1512/scripts/pnglibconf.h.prebuilt ../../lpng1512/pnglibconf.h


text:
"use strict";

const _         = require("lodash"),
      bindArray = require("bind-array"),
      // log       = require("rpb-logging")("dc-engine"),
      assert    = require("chai").assert;

function Deck($cardSelection) {
  let deck = [];
  let discardPile = [];

  function initialize() {
    $cardSelection.forEach(generateCards);
    shuffle();

    $cardSelection = null;
  }

  function generateCards(cardMetadata) {
    let Card = bindArray(cardMetadata.constructor, null, cardMetadata.args);
    _.times(cardMetadata.count, function() {
      let card = new Card();
      card = Object.assign(card, cardMetadata.additionalProperties);
      discardPile.push(card);
    });
  }

  function discard(card) {
    discardPile.push(card);
  }
  this.discard = discard;

  function pop() {
    if(deck.length === 0) shuffle();
    return deck.pop();
  }
  this.pop = pop;

  function shuffle() {
    // We're going to ignore the case where the discard
    // pile is empty too. This shouldn't be too bad

    // shouldn't shuffle if there are still cards
    assert.equal(deck.length, 0);

    deck = _.shuffle(discardPile);
    discardPile = [];
  }
  this.shuffle = shuffle;

  function pickCard(key) {
    let cardIdx = _.findIndex(deck, { key });
    if(cardIdx < 0)
      throw new Error("key not found: " + key);

    return deck.splice(cardIdx, 1)[0];
  }
  this.pickCard = pickCard;

  Object.defineProperties(this, {
    remaining: {
      enumerable: true,
      get: function() { return deck.length; }
    }
  });

  initialize();
}

module.exports = Deck;


text:
import json
import uuid

from examples import acquire_token_by_username_password
from office365.graph_client import GraphClient

def print_success(group):
    """
    :type group: office365.directory.groups.group.Group
    """
    print(f"team has been deleted")

client = GraphClient(acquire_token_by_username_password)
team_name = "Team_" + uuid.uuid4().hex
team = client.teams.create(team_name).execute_query()
print(json.dumps(team.to_json(), indent=4))

team.delete_object().execute_query_retry(success_callback=print_success)  # clean up


text:
from __future__ import absolute_import, print_function, division
import numpy
import theano

class DoubleOp(theano.Op):
    def __eq__(self, other):
        return type(self) == type(other)
    def __hash__(self):
        return hash(type(self))
    def __str__(self):
        return self.__class__.__name__
    def make_node(self, x):
        x = theano.tensor.as_tensor_variable(x)
        return theano.Apply(self, [x], [x.type()])
    def perform(self, node, inputs, output_storage):
        x = inputs[0]
        z = output_storage[0]
        z[0] = x * 2

x = theano.tensor.matrix()

f = theano.function([x], DoubleOp()(x))

inp = numpy.random.rand(5,5)
out = f(inp)
assert numpy.allclose(inp*2, out)
print(inp)
print(out)


text:
import functools
import hashlib
import os
import ssl
import time
import warnings

from oslo_context import context as oslo_context
from oslo_log import log as logging
from oslo_utils.secretutils import md5
import requests
import simplejson
from stevedore import driver as stevedore_driver

from octavia.amphorae.driver_exceptions import exceptions as driver_except
from octavia.amphorae.drivers import driver_base
from octavia.amphorae.drivers.haproxy import exceptions as exc
from octavia.amphorae.drivers.keepalived import vrrp_rest_driver
from octavia.common.config import cfg
from octavia.common import constants as consts
import octavia.common.jinja.haproxy.combined_listeners.jinja_cfg as jinja_combo
import octavia.common.jinja.haproxy.split_listeners.jinja_cfg as jinja_split
from octavia.common.jinja.lvs import jinja_cfg as jinja_udp_cfg
from octavia.common.tls_utils import cert_parser
from octavia.common import utils
from octavia.db import api as db_apis
from octavia.db import repositories as repo
from octavia.network import data_models as network_models

LOG = logging.getLogger(__name__)
API_VERSION = consts.API_VERSION
OCTAVIA_API_CLIENT = (
    "Octavia HaProxy Rest Client/{version} "
    "(https://wiki.openstack.org/wiki/Octavia)").format(version=API_VERSION)
CONF = cfg.CONF

class HaproxyAmphoraLoadBalancerDriver(
    driver_base.AmphoraLoadBalancerDriver,
        vrrp_rest_driver.KeepalivedAmphoraDriverMixin):

    def __init__(self):
        super().__init__()
        self.clients = {
            'base': AmphoraAPIClientBase(),
            '0.5': AmphoraAPIClient0_5(),
            '1.0': AmphoraAPIClient1_0(),
        }
        self.cert_manager = stevedore_driver.DriverManager(
            namespace='octavia.cert_manager',
            name=CONF.certificates.cert_manager,
            invoke_on_load=True,
        ).driver

        self.jinja_combo = jinja_combo.JinjaTemplater(
            base_amp_path=CONF.haproxy_amphora.base_path,
            base_crt_dir=CONF.haproxy_amphora.base_cert_dir,
            haproxy_template=CONF.haproxy_amphora.haproxy_template,
            connection_logging=CONF.haproxy_amphora.connection_logging)
        self.jinja_split = jinja_split.JinjaTemplater(
            base_amp_path=CONF.haproxy_amphora.base_path,
            base_crt_dir=CONF.haproxy_amphora.base_cert_dir,
            haproxy_template=CONF.haproxy_amphora.haproxy_template,
            connection_logging=CONF.haproxy_amphora.connection_logging)
        self.lvs_jinja = jinja_udp_cfg.LvsJinjaTemplater()

    def _get_haproxy_versions(self, amphora, timeout_dict=None):
        """Get major and minor version number from haproxy

        Example: ['1', '6']

        :returns version_list: A list with the major and minor numbers
        """
        self._populate_amphora_api_version(
            amphora, timeout_dict=timeout_dict)
        amp_info = self.clients[amphora.api_version].get_info(
            amphora, timeout_dict=timeout_dict)
        haproxy_version_string = amp_info['haproxy_version']

        return haproxy_version_string.split('.')[:2]

    def _populate_amphora_api_version(self, amphora, timeout_dict=None,
                                      raise_retry_exception=False):
        """Populate the amphora object with the api_version

        This will query the amphora for version discovery and populate
        the api_version string attribute on the amphora object.

        :returns: None
        """
        if not getattr(amphora, 'api_version', None):
            try:
                amphora.api_version = self.clients['base'].get_api_version(
                    amphora, timeout_dict=timeout_dict,
                    raise_retry_exception=raise_retry_exception)['api_version']
            except exc.NotFound:
                # Amphora is too old for version discovery, default to 0.5
                amphora.api_version = '0.5'
        LOG.debug('Amphora %s has API version %s',
                  amphora.id, amphora.api_version)
        return list(map(int, amphora.api_version.split('.')))

    def update_amphora_listeners(self, loadbalancer, amphora,
                                 timeout_dict=None):
        """Update the amphora with a new configuration.

        :param loadbalancer: The load balancer to update
        :type loadbalancer: object
        :param amphora: The amphora to update
        :type amphora: object
        :param timeout_dict: Dictionary of timeout values for calls to the
                             amphora. May contain: req_conn_timeout,
                             req_read_timeout, conn_max_retries,
                             conn_retry_interval
        :returns: None

        Updates the configuration of the listeners on a single amphora.
        """
        # if the amphora does not yet have listeners, no need to update them.
        if not loadbalancer.listeners:
            LOG.debug('No listeners found to update.')
            return
        if amphora is None or amphora.status == consts.DELETED:
            return

        # Check which HAProxy version is on the amp
        haproxy_versions = self._get_haproxy_versions(
            amphora, timeout_dict=timeout_dict)
        # Check which config style to use
        api_version = self._populate_amphora_api_version(amphora)
        if api_version[0] == 0 and api_version[1] <= 5:  # 0.5 or earlier
            split_config = True
            LOG.warning(
                'Amphora %s for loadbalancer %s needs upgrade to single '
                'process mode.', amphora.id, loadbalancer.id)
        else:
            split_config = False
            LOG.debug('Amphora %s for loadbalancer %s is already in single '
                      'process mode.', amphora.id, loadbalancer.id)

        has_tcp = False
        certs = {}
        listeners_to_update = []
        for listener in loadbalancer.listeners:
            LOG.debug("%s updating listener %s on amphora %s",
                      self.__class__.__name__, listener.id, amphora.id)
            if listener.protocol in consts.LVS_PROTOCOLS:
                # Generate Keepalived LVS configuration from listener object
                config = self.lvs_jinja.build_config(listener=listener)
                self.clients[amphora.api_version].upload_udp_config(
                    amphora, listener.id, config, timeout_dict=timeout_dict)
                self.clients[amphora.api_version].reload_listener(
                    amphora, listener.id, timeout_dict=timeout_dict)
            else:
                has_tcp = True
                if split_config:
                    obj_id = listener.id
                else:
                    obj_id = loadbalancer.id

                try:
                    certs.update({
                        listener.tls_certificate_id:
                        self._process_tls_certificates(
                            listener, amphora, obj_id)['tls_cert']})
                    certs.update({listener.client_ca_tls_certificate_id:
                                  self._process_secret(
                                      listener,
                                      listener.client_ca_tls_certificate_id,
                                      amphora, obj_id)})
                    certs.update({listener.client_crl_container_id:
                                  self._process_secret(
                                      listener,
                                      listener.client_crl_container_id,
                                      amphora, obj_id)})

                    certs.update(self._process_listener_pool_certs(
                        listener, amphora, obj_id))

                    if split_config:
                        config = self.jinja_split.build_config(
                            host_amphora=amphora, listener=listener,
                            haproxy_versions=haproxy_versions,
                            client_ca_filename=certs[
                                listener.client_ca_tls_certificate_id],
                            client_crl=certs[listener.client_crl_container_id],
                            pool_tls_certs=certs)
                        self.clients[amphora.api_version].upload_config(
                            amphora, listener.id, config,
                            timeout_dict=timeout_dict)
                        self.clients[amphora.api_version].reload_listener(
                            amphora, listener.id, timeout_dict=timeout_dict)
                    else:
                        listeners_to_update.append(listener)
                except Exception as e:
                    LOG.exception('Unable to update listener %s due to '
                                  '"%s". Skipping this listener.',
                                  listener.id, str(e))
                    listener_repo = repo.ListenerRepository()
                    listener_repo.update(db_apis.get_session(), listener.id,
                                         provisioning_status=consts.ERROR,
                                         operating_status=consts.ERROR)

        if has_tcp and not split_config:
            if listeners_to_update:
                # Generate HaProxy configuration from listener object
                config = self.jinja_combo.build_config(
                    host_amphora=amphora, listeners=listeners_to_update,
                    tls_certs=certs,
                    haproxy_versions=haproxy_versions)
                self.clients[amphora.api_version].upload_config(
                    amphora, loadbalancer.id, config,
                    timeout_dict=timeout_dict)
                self.clients[amphora.api_version].reload_listener(
                    amphora, loadbalancer.id, timeout_dict=timeout_dict)
            else:
                # If we aren't updating any listeners, make sure there are
                # no listeners hanging around. For example if this update
                # was called from a listener delete.
                self.clients[amphora.api_version].delete_listener(
                    amphora, loadbalancer.id)

    def _udp_update(self, listener, vip):
        LOG.debug("Amphora %s keepalivedlvs, updating "
                  "listener %s, vip %s",
                  self.__class__.__name__, listener.protocol_port,
                  vip.ip_address)

        for amp in listener.load_balancer.amphorae:
            if amp.status != consts.DELETED:
                # Generate Keepalived LVS configuration from listener object
                self._populate_amphora_api_version(amp)
                config = self.lvs_jinja.build_config(listener=listener)
                self.clients[amp.api_version].upload_udp_config(
                    amp, listener.id, config)
                self.clients[amp.api_version].reload_listener(
                    amp, listener.id)

    def update(self, loadbalancer):
        for amphora in loadbalancer.amphorae:
            if amphora.status != consts.DELETED:
                self.update_amphora_listeners(loadbalancer, amphora)

    def upload_cert_amp(self, amp, pem):
        LOG.debug("Amphora %s updating cert in REST driver "
                  "with amphora id %s,",
                  self.__class__.__name__, amp.id)
        self._populate_amphora_api_version(amp)
        self.clients[amp.api_version].update_cert_for_rotation(amp, pem)

    def _apply(self, func_name, loadbalancer, amphora=None, *args, **kwargs):
        if amphora is None:
            amphorae = loadbalancer.amphorae
        else:
            amphorae = [amphora]

        timeout_dict = args[0]

        for amp in amphorae:
            if amp.status != consts.DELETED:
                api_version = self._populate_amphora_api_version(
                    amp, timeout_dict=timeout_dict)
                # Check which config style to use
                if api_version[0] == 0 and api_version[1] <= 5:
                    # 0.5 or earlier
                    LOG.warning(
                        'Amphora %s for loadbalancer %s needs upgrade to '
                        'single process mode.', amp.id, loadbalancer.id)
                    for listener in loadbalancer.listeners:
                        getattr(self.clients[amp.api_version], func_name)(
                            amp, listener.id, *args)
                else:
                    LOG.debug(
                        'Amphora %s for loadbalancer %s is already in single '
                        'process mode.', amp.id, loadbalancer.id)
                    has_tcp = False
                    for listener in loadbalancer.listeners:
                        if listener.protocol in consts.LVS_PROTOCOLS:
                            getattr(self.clients[amp.api_version], func_name)(
                                amp, listener.id, *args)
                        else:
                            has_tcp = True
                    if has_tcp:
                        getattr(self.clients[amp.api_version], func_name)(
                            amp, loadbalancer.id, *args)

    def reload(self, loadbalancer, amphora=None, timeout_dict=None):
        self._apply('reload_listener', loadbalancer, amphora, timeout_dict)

    def start(self, loadbalancer, amphora=None, timeout_dict=None):
        self._apply('start_listener', loadbalancer, amphora, timeout_dict)

    def delete(self, listener):
        # Delete any UDP/SCTP listeners the old way (we didn't update the way
        # they are configured)
        loadbalancer = listener.load_balancer
        if listener.protocol in consts.LVS_PROTOCOLS:
            for amp in loadbalancer.amphorae:
                if amp.status != consts.DELETED:
                    self._populate_amphora_api_version(amp)
                    self.clients[amp.api_version].delete_listener(
                        amp, listener.id)
            return

        # In case the listener is not UDP or SCTP, things get more complicated.
        # We need to do this individually for each amphora in case some are
        # using split config and others are using combined config.
        for amp in loadbalancer.amphorae:
            if amp.status != consts.DELETED:
                api_version = self._populate_amphora_api_version(amp)
                # Check which config style to use
                if api_version[0] == 0 and api_version[1] <= 5:
                    # 0.5 or earlier
                    LOG.warning(
                        'Amphora %s for loadbalancer %s needs upgrade to '
                        'single process mode.', amp.id, loadbalancer.id)
                    self.clients[amp.api_version].delete_listener(
                        amp, listener.id)
                else:
                    LOG.debug(
                        'Amphora %s for loadbalancer %s is already in single '
                        'process mode.', amp.id, loadbalancer.id)
                    self._combined_config_delete(amp, listener)

    def _combined_config_delete(self, amphora, listener):
        # Remove the listener from the listener list on the LB before
        # passing the whole thing over to update (so it'll actually delete)
        # In case of amphorae in ACTIVE_STANDBY topology, ensure that we don't
        # remove an already removed listener.
        if listener in listener.load_balancer.listeners:
            listener.load_balancer.listeners.remove(listener)

        # Check if there's any certs that we need to delete
        certs = self._process_tls_certificates(listener)
        certs_to_delete = set()
        if certs['tls_cert']:
            certs_to_delete.add(certs['tls_cert'].id)
        for sni_cert in certs['sni_certs']:
            certs_to_delete.add(sni_cert.id)

        # Delete them (they'll be recreated before the reload if they are
        # needed for other listeners anyway)
        self._populate_amphora_api_version(amphora)
        for cert_id in certs_to_delete:
            self.clients[amphora.api_version].delete_cert_pem(
                amphora, listener.load_balancer.id,
                '{id}.pem'.format(id=cert_id))

        # See how many non-UDP/SCTP listeners we have left
        non_lvs_listener_count = len([
            1 for li in listener.load_balancer.listeners
            if li.protocol not in consts.LVS_PROTOCOLS])
        if non_lvs_listener_count > 0:
            # We have other listeners, so just update is fine.
            # TODO(rm_work): This is a little inefficient since this duplicates
            # a lot of the detection logic that has already been done, but it
            # is probably safer to re-use the existing code-path.
            self.update_amphora_listeners(listener.load_balancer, amphora)
        else:
            # Deleting the last listener, so really do the delete
            self.clients[amphora.api_version].delete_listener(
                amphora, listener.load_balancer.id)

    def get_info(self, amphora, raise_retry_exception=False,
                 timeout_dict=None):
        self._populate_amphora_api_version(
            amphora, raise_retry_exception=raise_retry_exception,
            timeout_dict=timeout_dict)
        return self.clients[amphora.api_version].get_info(
            amphora, raise_retry_exception=raise_retry_exception,
            timeout_dict=timeout_dict)

    def get_diagnostics(self, amphora):
        pass

    def finalize_amphora(self, amphora):
        pass

    def _build_net_info(self, port, amphora, subnet, mtu=None):
        # NOTE(blogan): using the vrrp port here because that
        # is what the allowed address pairs network driver sets
        # this particular port to.  This does expose a bit of
        # tight coupling between the network driver and amphora
        # driver.  We will need to revisit this to try and remove
        # this tight coupling.
        # NOTE (johnsom): I am loading the vrrp_ip into the
        # net_info structure here so that I don't break
        # compatibility with old amphora agent versions.
        host_routes = [{'nexthop': hr[consts.NEXTHOP],
                        'destination': hr[consts.DESTINATION]}
                       for hr in subnet[consts.HOST_ROUTES]]
        net_info = {'subnet_cidr': subnet[consts.CIDR],
                    'gateway': subnet[consts.GATEWAY_IP],
                    'mac_address': port[consts.MAC_ADDRESS],
                    'vrrp_ip': amphora[consts.VRRP_IP],
                    'mtu': mtu or port[consts.NETWORK][consts.MTU],
                    'host_routes': host_routes,
                    'additional_vips': []}
        return net_info

    def post_vip_plug(self, amphora, load_balancer, amphorae_network_config,
                      vrrp_port=None, vip_subnet=None,
                      additional_vip_data=None):
        if amphora.status != consts.DELETED:
            self._populate_amphora_api_version(amphora)
            if vip_subnet is None:
                vip_subnet = amphorae_network_config.get(amphora.id).vip_subnet
            if vrrp_port is None:
                port = amphorae_network_config.get(amphora.id).vrrp_port
                mtu = port.network.mtu
            else:
                port = vrrp_port
                mtu = port.network['mtu']
            LOG.debug("Post-VIP-Plugging with vrrp_ip %s vrrp_port %s",
                      amphora.vrrp_ip, port.id)
            net_info = self._build_net_info(
                port.to_dict(recurse=True), amphora.to_dict(),
                vip_subnet.to_dict(recurse=True), mtu)
            if additional_vip_data is None:
                additional_vip_data = amphorae_network_config.get(
                    amphora.id).additional_vip_data
            for add_vip in additional_vip_data:
                add_host_routes = [{'nexthop': hr.nexthop,
                                    'destination': hr.destination}
                                   for hr in add_vip.subnet.host_routes]
                add_net_info = {'subnet_cidr': add_vip.subnet.cidr,
                                'ip_address': add_vip.ip_address,
                                'gateway': add_vip.subnet.gateway_ip,
                                'host_routes': add_host_routes}
                net_info['additional_vips'].append(add_net_info)
            try:
                self.clients[amphora.api_version].plug_vip(
                    amphora, load_balancer.vip.ip_address, net_info)
            except exc.Conflict:
                LOG.warning('VIP with MAC %(mac)s already exists on amphora, '
                            'skipping post_vip_plug',
                            {'mac': port.mac_address})

    def post_network_plug(self, amphora, port, amphora_network_config):
        fixed_ips = []
        for fixed_ip in port.fixed_ips:
            host_routes = [{'nexthop': hr.nexthop,
                            'destination': hr.destination}
                           for hr in fixed_ip.subnet.host_routes]
            ip = {'ip_address': fixed_ip.ip_address,
                  'subnet_cidr': fixed_ip.subnet.cidr,
                  'host_routes': host_routes,
                  'gateway': fixed_ip.subnet.gateway_ip}
            fixed_ips.append(ip)
        port_info = {'mac_address': port.mac_address,
                     'fixed_ips': fixed_ips,
                     'mtu': port.network.mtu}
        if port.id == amphora.vrrp_port_id:
            if isinstance(amphora_network_config,
                          network_models.AmphoraNetworkConfig):
                amphora_network_config = amphora_network_config.to_dict(
                    recurse=True)
            # We have to special-case sharing the vrrp port and pass through
            # enough extra information to populate the whole VIP port
            net_info = self._build_net_info(
                port.to_dict(recurse=True), amphora.to_dict(),
                amphora_network_config[consts.VIP_SUBNET],
                port.network.mtu)
            net_info['vip'] = amphora.ha_ip
            port_info['vip_net_info'] = net_info
        try:
            self._populate_amphora_api_version(amphora)
            self.clients[amphora.api_version].plug_network(amphora, port_info)
        except exc.Conflict:
            LOG.warning('Network with MAC %(mac)s already exists on amphora, '
                        'skipping post_network_plug',
                        {'mac': port.mac_address})

    def _process_tls_certificates(self, listener, amphora=None, obj_id=None):
        """Processes TLS data from the listener.

        Converts and uploads PEM data to the Amphora API

        return TLS_CERT and SNI_CERTS
        """
        tls_cert = None
        sni_certs = []
        certs = []
        cert_filename_list = []

        data = cert_parser.load_certificates_data(
            self.cert_manager, listener)
        if data['tls_cert'] is not None:
            tls_cert = data['tls_cert']
            # Note, the first cert is the TLS default cert
            certs.append(tls_cert)
        if data['sni_certs']:
            sni_certs = data['sni_certs']
            certs.extend(sni_certs)

        if amphora and obj_id:
            for cert in certs:
                pem = cert_parser.build_pem(cert)
                md5sum = md5(pem, usedforsecurity=False).hexdigest()  # nosec
                name = '{id}.pem'.format(id=cert.id)
                cert_filename_list.append(
                    os.path.join(
                        CONF.haproxy_amphora.base_cert_dir, obj_id, name))
                self._upload_cert(amphora, obj_id, pem, md5sum, name)

            if certs:
                # Build and upload the crt-list file for haproxy
                crt_list = "\n".join(cert_filename_list)
                crt_list = f'{crt_list}\n'.encode('utf-8')
                md5sum = md5(crt_list,
                             usedforsecurity=False).hexdigest()  # nosec
                name = '{id}.pem'.format(id=listener.id)
                self._upload_cert(amphora, obj_id, crt_list, md5sum, name)
        return {'tls_cert': tls_cert, 'sni_certs': sni_certs}

    def _process_secret(self, listener, secret_ref, amphora=None, obj_id=None):
        """Get the secret from the cert manager and upload it to the amp.

        :returns: The filename of the secret in the amp.
        """
        if not secret_ref:
            return None
        context = oslo_context.RequestContext(project_id=listener.project_id)
        secret = self.cert_manager.get_secret(context, secret_ref)
        try:
            secret = secret.encode('utf-8')
        except AttributeError:
            pass
        md5sum = md5(secret, usedforsecurity=False).hexdigest()  # nosec
        id = hashlib.sha1(secret).hexdigest()  # nosec
        name = '{id}.pem'.format(id=id)

        if amphora and obj_id:
            self._upload_cert(
                amphora, obj_id, pem=secret, md5sum=md5sum, name=name)
        return name

    def _process_listener_pool_certs(self, listener, amphora, obj_id):
        #     {'POOL-ID': {
        #         'client_cert': client_full_filename,
        #         'ca_cert': ca_cert_full_filename,
        #         'crl': crl_full_filename}}
        pool_certs_dict = {}
        for pool in listener.pools:
            if pool.id not in pool_certs_dict:
                pool_certs_dict[pool.id] = self._process_pool_certs(
                    listener, pool, amphora, obj_id)
        for l7policy in listener.l7policies:
            if (l7policy.redirect_pool and
                    l7policy.redirect_pool.id not in pool_certs_dict):
                pool_certs_dict[l7policy.redirect_pool.id] = (
                    self._process_pool_certs(listener, l7policy.redirect_pool,
                                             amphora, obj_id))
        return pool_certs_dict

    def _process_pool_certs(self, listener, pool, amphora, obj_id):
        pool_cert_dict = {}

        # Handle the client cert(s) and key
        if pool.tls_certificate_id:
            data = cert_parser.load_certificates_data(self.cert_manager, pool)
            tls_cert = data['tls_cert']
            pem = cert_parser.build_pem(tls_cert)
            try:
                pem = pem.encode('utf-8')
            except AttributeError:
                pass
            md5sum = md5(pem, usedforsecurity=False).hexdigest()  # nosec
            name = '{id}.pem'.format(id=tls_cert.id)
            if amphora and obj_id:
                self._upload_cert(amphora, obj_id, pem=pem,
                                  md5sum=md5sum, name=name)
            pool_cert_dict['client_cert'] = os.path.join(
                CONF.haproxy_amphora.base_cert_dir, obj_id, name)
        if pool.ca_tls_certificate_id:
            name = self._process_secret(listener, pool.ca_tls_certificate_id,
                                        amphora, obj_id)
            pool_cert_dict['ca_cert'] = os.path.join(
                CONF.haproxy_amphora.base_cert_dir, obj_id, name)
        if pool.crl_container_id:
            name = self._process_secret(listener, pool.crl_container_id,
                                        amphora, obj_id)
            pool_cert_dict['crl'] = os.path.join(
                CONF.haproxy_amphora.base_cert_dir, obj_id, name)

        return pool_cert_dict

    def _upload_cert(self, amp, listener_id, pem, md5sum, name):
        try:
            if self.clients[amp.api_version].get_cert_md5sum(
                    amp, listener_id, name, ignore=(404,)) == md5sum:
                return
        except exc.NotFound:
            pass

        self.clients[amp.api_version].upload_cert_pem(
            amp, listener_id, name, pem)

    def update_amphora_agent_config(self, amphora, agent_config,
                                    timeout_dict=None):
        """Update the amphora agent configuration file.

        :param amphora: The amphora to update.
        :type amphora: object
        :param agent_config: The new amphora agent configuration.
        :type agent_config: string
        :param timeout_dict: Dictionary of timeout values for calls to the
                             amphora. May contain: req_conn_timeout,
                             req_read_timeout, conn_max_retries,
                             conn_retry_interval
        :returns: None

        Note: This will mutate the amphora agent config and adopt the
              new values.
        """
        try:
            self._populate_amphora_api_version(amphora)
            self.clients[amphora.api_version].update_agent_config(
                amphora, agent_config, timeout_dict=timeout_dict)
        except exc.NotFound as e:
            LOG.debug('Amphora %s does not support the update_agent_config '
                      'API.', amphora.id)
            raise driver_except.AmpDriverNotImplementedError() from e

    def get_interface_from_ip(self, amphora, ip_address, timeout_dict=None):
        """Get the interface name for an IP address.

        :param amphora: The amphora to query.
        :type amphora: octavia.db.models.Amphora
        :param ip_address: The IP address to lookup. (IPv4 or IPv6)
        :type ip_address: string
        :param timeout_dict: Dictionary of timeout values for calls to the
                             amphora. May contain: req_conn_timeout,
                             req_read_timeout, conn_max_retries,
                             conn_retry_interval
        :type timeout_dict: dict
        :returns: None if not found, the interface name string if found.
        """
        try:
            self._populate_amphora_api_version(amphora, timeout_dict)
            response_json = self.clients[amphora.api_version].get_interface(
                amphora, ip_address, timeout_dict, log_error=False)
            return response_json.get('interface', None)
        except (exc.NotFound, driver_except.TimeOutException):
            return None

# Check a custom hostname
class CustomHostNameCheckingAdapter(requests.adapters.HTTPAdapter):
    def cert_verify(self, conn, url, verify, cert):
        conn.assert_hostname = self.uuid
        return super().cert_verify(conn, url, verify, cert)

    def init_poolmanager(self, *pool_args, **pool_kwargs):
        proto = CONF.amphora_agent.agent_tls_protocol.replace('.', '_')
        pool_kwargs['ssl_version'] = getattr(ssl, "PROTOCOL_%s" % proto)
        return super().init_poolmanager(*pool_args, **pool_kwargs)

class AmphoraAPIClientBase(object):
    def __init__(self):
        super().__init__()

        self.get = functools.partial(self.request, 'get')
        self.post = functools.partial(self.request, 'post')
        self.put = functools.partial(self.request, 'put')
        self.delete = functools.partial(self.request, 'delete')
        self.head = functools.partial(self.request, 'head')

        self.session = requests.Session()
        self.session.cert = CONF.haproxy_amphora.client_cert
        self.ssl_adapter = CustomHostNameCheckingAdapter()
        self.session.mount('https://', self.ssl_adapter)

    def _base_url(self, ip, api_version=None):
        if utils.is_ipv6_lla(ip):
            ip = '[{ip}%{interface}]'.format(
                ip=ip,
                interface=CONF.haproxy_amphora.lb_network_interface)
        elif utils.is_ipv6(ip):
            ip = '[{ip}]'.format(ip=ip)
        if api_version:
            return "https://{ip}:{port}/{version}/".format(
                ip=ip,
                port=CONF.haproxy_amphora.bind_port,
                version=api_version)
        return "https://{ip}:{port}/".format(
            ip=ip,
            port=CONF.haproxy_amphora.bind_port)

    def request(self, method, amp, path='/', timeout_dict=None,
                retry_404=True, raise_retry_exception=False, **kwargs):
        cfg_ha_amp = CONF.haproxy_amphora
        if timeout_dict is None:
            timeout_dict = {}
        req_conn_timeout = timeout_dict.get(
            consts.REQ_CONN_TIMEOUT, cfg_ha_amp.rest_request_conn_timeout)
        req_read_timeout = timeout_dict.get(
            consts.REQ_READ_TIMEOUT, cfg_ha_amp.rest_request_read_timeout)
        conn_max_retries = timeout_dict.get(
            consts.CONN_MAX_RETRIES, cfg_ha_amp.connection_max_retries)
        conn_retry_interval = timeout_dict.get(
            consts.CONN_RETRY_INTERVAL, cfg_ha_amp.connection_retry_interval)

        LOG.debug("request url %s", path)
        _request = getattr(self.session, method.lower())
        _url = self._base_url(amp.lb_network_ip, amp.api_version) + path
        LOG.debug("request url %s", _url)
        reqargs = {
            'verify': CONF.haproxy_amphora.server_ca,
            'url': _url,
            'timeout': (req_conn_timeout, req_read_timeout), }
        reqargs.update(kwargs)
        headers = reqargs.setdefault('headers', {})

        headers['User-Agent'] = OCTAVIA_API_CLIENT
        self.ssl_adapter.uuid = amp.id
        exception = None
        # Keep retrying
        for dummy in range(conn_max_retries):
            try:
                with warnings.catch_warnings():
                    warnings.filterwarnings(
                        "ignore",
                        message="A true SSLContext object is not available"
                    )
                    r = _request(**reqargs)
                LOG.debug('Connected to amphora. Response: %(resp)s',
                          {'resp': r})

                content_type = r.headers.get('content-type', '')
                # Check the 404 to see if it is just that the network in the
                # amphora is not yet up, in which case retry.
                # Otherwise return the response quickly.
                if r.status_code == 404:
                    if not retry_404:
                        raise exc.NotFound()
                    LOG.debug('Got a 404 (content-type: %(content_type)s) -- '
                              'connection data: %(content)s',
                              {'content_type': content_type,
                               'content': r.content})
                    if content_type.find("application/json") == -1:
                        LOG.debug("Amphora agent not ready.")
                        raise requests.ConnectionError
                    try:
                        json_data = r.json().get('details', '')
                        if 'No suitable network interface found' in json_data:
                            LOG.debug("Amphora network interface not found.")
                            raise requests.ConnectionError
                    except simplejson.JSONDecodeError:  # if r.json() fails
                        pass  # TODO(rm_work) Should we do something?
                return r
            except (requests.ConnectionError, requests.Timeout) as e:
                exception = e
                LOG.warning("Could not connect to instance. Retrying.")
                time.sleep(conn_retry_interval)
                if raise_retry_exception:
                    # For taskflow persistence cause attribute should
                    # be serializable to JSON. Pass None, as cause exception
                    # is described in the expection message.
                    raise driver_except.AmpConnectionRetry(
                        exception=str(e)) from None
        LOG.error("Connection retries (currently set to %(max_retries)s) "
                  "exhausted.  The amphora is unavailable. Reason: "
                  "%(exception)s",
                  {'max_retries': conn_max_retries,
                   'exception': exception})
        raise driver_except.TimeOutException()

    def get_api_version(self, amp, timeout_dict=None,
                        raise_retry_exception=False):
        amp.api_version = None
        r = self.get(amp, retry_404=False, timeout_dict=timeout_dict,
                     raise_retry_exception=raise_retry_exception)
        # Handle 404 special as we don't want to log an ERROR on 404
        exc.check_exception(r, (404,))
        if r.status_code == 404:
            raise exc.NotFound()
        return r.json()

class AmphoraAPIClient0_5(AmphoraAPIClientBase):
    def __init__(self):
        super().__init__()

        self.start_listener = functools.partial(self._action,
                                                consts.AMP_ACTION_START)
        self.reload_listener = functools.partial(self._action,
                                                 consts.AMP_ACTION_RELOAD)

        self.start_vrrp = functools.partial(self._vrrp_action,
                                            consts.AMP_ACTION_START)
        self.stop_vrrp = functools.partial(self._vrrp_action,
                                           consts.AMP_ACTION_STOP)
        self.reload_vrrp = functools.partial(self._vrrp_action,
                                             consts.AMP_ACTION_RELOAD)

    def upload_config(self, amp, listener_id, config, timeout_dict=None):
        r = self.put(
            amp,
            'listeners/{amphora_id}/{listener_id}/haproxy'.format(
                amphora_id=amp.id, listener_id=listener_id), timeout_dict,
            data=config)
        return exc.check_exception(r)

    def _action(self, action, amp, listener_id, timeout_dict=None):
        r = self.put(amp, 'listeners/{listener_id}/{action}'.format(
            listener_id=listener_id, action=action), timeout_dict=timeout_dict)
        return exc.check_exception(r)

    def upload_cert_pem(self, amp, listener_id, pem_filename, pem_file):
        r = self.put(
            amp, 'listeners/{listener_id}/certificates/{filename}'.format(
                listener_id=listener_id, filename=pem_filename),
            data=pem_file)
        return exc.check_exception(r)

    def get_cert_md5sum(self, amp, listener_id, pem_filename, ignore=tuple()):
        r = self.get(
            amp, 'listeners/{listener_id}/certificates/{filename}'.format(
                listener_id=listener_id, filename=pem_filename))
        if exc.check_exception(r, ignore):
            return r.json().get("md5sum")
        return None

    def delete_cert_pem(self, amp, listener_id, pem_filename):
        r = self.delete(
            amp, 'listeners/{listener_id}/certificates/{filename}'.format(
                listener_id=listener_id, filename=pem_filename))
        return exc.check_exception(r, (404,))

    def update_cert_for_rotation(self, amp, pem_file):
        r = self.put(amp, 'certificate', data=pem_file)
        return exc.check_exception(r)

    def delete_listener(self, amp, listener_id):
        r = self.delete(
            amp, 'listeners/{listener_id}'.format(listener_id=listener_id))
        return exc.check_exception(r, (404,))

    def get_info(self, amp, raise_retry_exception=False,
                 timeout_dict=None):
        r = self.get(amp, "info", raise_retry_exception=raise_retry_exception,
                     timeout_dict=timeout_dict)
        if exc.check_exception(r):
            return r.json()
        return None

    def get_details(self, amp):
        r = self.get(amp, "details")
        if exc.check_exception(r):
            return r.json()
        return None

    def get_all_listeners(self, amp):
        r = self.get(amp, "listeners")
        if exc.check_exception(r):
            return r.json()
        return None

    def plug_network(self, amp, port):
        r = self.post(amp, 'plug/network',
                      json=port)
        return exc.check_exception(r)

    def plug_vip(self, amp, vip, net_info):
        r = self.post(amp,
                      'plug/vip/{vip}'.format(vip=vip),
                      json=net_info)
        return exc.check_exception(r)

    def upload_vrrp_config(self, amp, config):
        r = self.put(amp, 'vrrp/upload', data=config)
        return exc.check_exception(r)

    def _vrrp_action(self, action, amp, timeout_dict=None):
        r = self.put(amp, 'vrrp/{action}'.format(action=action),
                     timeout_dict=timeout_dict)
        return exc.check_exception(r)

    def get_interface(self, amp, ip_addr, timeout_dict=None, log_error=True):
        r = self.get(amp, 'interface/{ip_addr}'.format(ip_addr=ip_addr),
                     timeout_dict=timeout_dict)
        return exc.check_exception(r, log_error=log_error).json()

    def upload_udp_config(self, amp, listener_id, config, timeout_dict=None):
        r = self.put(
            amp,
            'listeners/{amphora_id}/{listener_id}/udp_listener'.format(
                amphora_id=amp.id, listener_id=listener_id), timeout_dict,
            data=config)
        return exc.check_exception(r)

    def update_agent_config(self, amp, agent_config, timeout_dict=None):
        r = self.put(amp, 'config', timeout_dict, data=agent_config)
        return exc.check_exception(r)

class AmphoraAPIClient1_0(AmphoraAPIClientBase):
    def __init__(self):
        super().__init__()

        self.start_listener = functools.partial(self._action,
                                                consts.AMP_ACTION_START)
        self.reload_listener = functools.partial(self._action,
                                                 consts.AMP_ACTION_RELOAD)

        self.start_vrrp = functools.partial(self._vrrp_action,
                                            consts.AMP_ACTION_START)
        self.stop_vrrp = functools.partial(self._vrrp_action,
                                           consts.AMP_ACTION_STOP)
        self.reload_vrrp = functools.partial(self._vrrp_action,
                                             consts.AMP_ACTION_RELOAD)

    def upload_config(self, amp, loadbalancer_id, config, timeout_dict=None):
        r = self.put(
            amp,
            'loadbalancer/{amphora_id}/{loadbalancer_id}/haproxy'.format(
                amphora_id=amp.id, loadbalancer_id=loadbalancer_id),
            timeout_dict, data=config)
        return exc.check_exception(r)

    def get_listener_status(self, amp, listener_id):
        r = self.get(
            amp,
            'listeners/{listener_id}'.format(listener_id=listener_id))
        if exc.check_exception(r):
            return r.json()
        return None

    def _action(self, action, amp, object_id, timeout_dict=None):
        r = self.put(
            amp, 'loadbalancer/{object_id}/{action}'.format(
                object_id=object_id, action=action),
            timeout_dict=timeout_dict)
        return exc.check_exception(r)

    def upload_cert_pem(self, amp, loadbalancer_id, pem_filename, pem_file):
        r = self.put(
            amp,
            'loadbalancer/{loadbalancer_id}/certificates/{filename}'.format(
                loadbalancer_id=loadbalancer_id, filename=pem_filename),
            data=pem_file)
        return exc.check_exception(r)

    def get_cert_md5sum(self, amp, loadbalancer_id, pem_filename,
                        ignore=tuple()):
        r = self.get(
            amp,
            'loadbalancer/{loadbalancer_id}/certificates/{filename}'.format(
                loadbalancer_id=loadbalancer_id, filename=pem_filename))
        if exc.check_exception(r, ignore):
            return r.json().get("md5sum")
        return None

    def delete_cert_pem(self, amp, loadbalancer_id, pem_filename):
        r = self.delete(
            amp,
            'loadbalancer/{loadbalancer_id}/certificates/{filename}'.format(
                loadbalancer_id=loadbalancer_id, filename=pem_filename))
        return exc.check_exception(r, (404,))

    def update_cert_for_rotation(self, amp, pem_file):
        r = self.put(amp, 'certificate', data=pem_file)
        return exc.check_exception(r)

    def delete_listener(self, amp, object_id):
        r = self.delete(
            amp, 'listeners/{object_id}'.format(object_id=object_id))
        return exc.check_exception(r, (404,))

    def get_info(self, amp, raise_retry_exception=False,
                 timeout_dict=None):
        r = self.get(amp, "info", raise_retry_exception=raise_retry_exception,
                     timeout_dict=timeout_dict)
        if exc.check_exception(r):
            return r.json()
        return None

    def get_details(self, amp):
        r = self.get(amp, "details")
        if exc.check_exception(r):
            return r.json()
        return None

    def get_all_listeners(self, amp):
        r = self.get(amp, "listeners")
        if exc.check_exception(r):
            return r.json()
        return None

    def plug_network(self, amp, port):
        r = self.post(amp, 'plug/network',
                      json=port)
        return exc.check_exception(r)

    def plug_vip(self, amp, vip, net_info):
        r = self.post(amp,
                      'plug/vip/{vip}'.format(vip=vip),
                      json=net_info)
        return exc.check_exception(r)

    def upload_vrrp_config(self, amp, config):
        r = self.put(amp, 'vrrp/upload', data=config)
        return exc.check_exception(r)

    def _vrrp_action(self, action, amp, timeout_dict=None):
        r = self.put(amp, 'vrrp/{action}'.format(action=action),
                     timeout_dict=timeout_dict)
        return exc.check_exception(r)

    def get_interface(self, amp, ip_addr, timeout_dict=None, log_error=True):
        r = self.get(amp, 'interface/{ip_addr}'.format(ip_addr=ip_addr),
                     timeout_dict=timeout_dict)
        return exc.check_exception(r, log_error=log_error).json()

    # The function is used for all LVS-supported protocol listener (UDP, SCTP)
    def upload_udp_config(self, amp, listener_id, config, timeout_dict=None):
        r = self.put(
            amp,
            'listeners/{amphora_id}/{listener_id}/udp_listener'.format(
                amphora_id=amp.id, listener_id=listener_id), timeout_dict,
            data=config)
        return exc.check_exception(r)

    def update_agent_config(self, amp, agent_config, timeout_dict=None):
        r = self.put(amp, 'config', timeout_dict, data=agent_config)
        return exc.check_exception(r)


text:
using MvvmCross.Plugins.Messenger;
using MyHealth.Client.Core.Model;
using MyHealth.Client.Core.ServiceAgents;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Cirrious.MvvmCross.ViewModels;
using System.Globalization;
using Cirrious.CrossCore;

namespace MyHealth.Client.Core.ViewModels
{
    public class AppointmentsViewModel : BaseViewModel
    {
        private readonly IMyHealthClient _myHealthClient;

		private const int MaxAppointmentsToList = 10;

        private ObservableCollection<ClinicAppointment> _appointments;

		private ClinicAppointment appointmentSelectedItem;

        private Lazy<NewAppointmentViewModel> _newAppointmentViewModel;

        public NewAppointmentViewModel NewAppointmentViewModel { get { return _newAppointmentViewModel.Value; } }

        public ObservableCollection<ClinicAppointment> Appointments
        {
            get
            {
                return _appointments;
            }

            private set
            {
                _appointments = value;
                RaisePropertyChanged(() => Appointments);
            }
        }

        public ClinicAppointment AppointmentSelectedItem
        {
            get
            {
                return appointmentSelectedItem;
            }
            set
            {
                if (appointmentSelectedItem == value) return;
                appointmentSelectedItem = value;
                RaisePropertyChanged(() => AppointmentSelectedItem);
            }
        }

        public IMvxCommand ShowDetailsCommand { get; private set; }

        public IMvxCommand RefreshCommand { get; private set; }

        public AppointmentsViewModel(IMyHealthClient client, IMvxMessenger messenger)
			: base(messenger)
        {
            _myHealthClient = client;
            _messenger = messenger;

            Appointments = new ObservableCollection<ClinicAppointment>();
            ShowDetailsCommand = new MvxCommand<Appointment>(OnShowDetails);
            RefreshCommand = new MvxCommand(async () => await ReloadDataAsync());

            _newAppointmentViewModel = new Lazy<NewAppointmentViewModel>(() =>
                Mvx.IocConstruct<NewAppointmentViewModel>());
        }

        public override void Start()
        {
            base.Start();

            ReloadDataAsync().Forget();

            NewAppointmentViewModel.Start();
        }

        protected override async Task InitializeAsync()
        {
            Appointments.Clear();

            var appointments = await _myHealthClient.
                AppointmentsService.GetPatientAppointmentsAsync(AppSettings.CurrentPatientId, MaxAppointmentsToList);

			if (appointments.Count > 0)
				Appointments.AddRange(appointments);

            AppointmentSelectedItem = Appointments.FirstOrDefault();
        }

		private void OnShowDetails(Appointment appointment)
		{
			var parameters = new MvxBundle(new Dictionary<string, string>
				{
					["appointmentId"] = appointment.AppointmentId.ToString(CultureInfo.InvariantCulture)
				});

			ShowViewModel<AppointmentDetailsViewModel>(parameters);
		}
    }
}


text:
import React from 'react'
import Helmet from "react-helmet"

import { prefixLink } from 'gatsby-helpers'
import { TypographyStyle, GoogleFont } from 'react-typography'
import typography from './utils/typography'

const BUILD_TIME = new Date().getTime()

module.exports = React.createClass({
  propTypes () {
    return {
      body: React.PropTypes.string,
    }
  },
  render () {
    const head = Helmet.rewind()

    let css
    if (process.env.NODE_ENV === 'production') {
      css = <style dangerouslySetInnerHTML={{ __html: require('!raw!./public/styles.css') }} />
    }

    return (
      <html lang="en">
        <head>
          <meta charSet="utf-8" />
          <meta httpEquiv="X-UA-Compatible" content="IE=edge" />
          <meta
            name="viewport"
            content="width=device-width, initial-scale=1.0"
          />
          {head.title.toComponent()}
          {head.meta.toComponent()}
          <TypographyStyle typography={typography} />
          <link rel="shortcut icon" type="image/png" href="./components/favicon.ico"/>
          <GoogleFont typography={typography} />
          {css}
        </head>
        <body>
          <div id="react-mount" dangerouslySetInnerHTML={{ __html: this.props.body }} />
          <script src={prefixLink(`/bundle.js?t=${BUILD_TIME}`)} />
        </body>
      </html>
    )
  },
})


text:
import com.lorentzos.pj.PJParser;
import org.antlr.v4.runtime.tree.ParseTree;
import org.hamcrest.Matcher;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.text.IsEqualIgnoringWhiteSpace.equalToIgnoringWhiteSpace;

/**
 * Created by dionysis_lorentzos on 9/9/14.
 * Beware the dinosaurs!
 */
public class TestClass {

    final TreeCreator t = new TreeCreator() {
        @Override
        public ParseTree ruleListener(PJParser parser) {
            return parser.classDeclaration();
        }
    };

    @Test
    public void testEmptyClass() {
        String a = "class Test{\n\n}";
        Matcher<String> b = equalToIgnoringWhiteSpace(t.testInput("class Test:"));
        assertThat(a, b);
    }

    @Test
    public void testEmptyClassWithTrailedNL() {
        String a = "class Test{\n\n}";
        Matcher<String> b = equalToIgnoringWhiteSpace(t.testInput("class Test:\n"));
        assertThat(a, b);
    }

}


text:
namespace chromeos {

namespace secure_channel {

FakeErrorTolerantBleAdvertisement::FakeErrorTolerantBleAdvertisement(
    const DeviceIdPair& device_id_pair,
    base::OnceCallback<void(const DeviceIdPair&)> destructor_callback)
    : ErrorTolerantBleAdvertisement(device_id_pair),
      id_(base::UnguessableToken::Create()),
      destructor_callback_(std::move(destructor_callback)) {}

FakeErrorTolerantBleAdvertisement::~FakeErrorTolerantBleAdvertisement() {
  std::move(destructor_callback_).Run(device_id_pair());
}

bool FakeErrorTolerantBleAdvertisement::HasBeenStopped() {
  return !stop_callback_.is_null();
}

void FakeErrorTolerantBleAdvertisement::InvokeStopCallback() {
  DCHECK(HasBeenStopped());
  stop_callback_.Run();
}

void FakeErrorTolerantBleAdvertisement::Stop(const base::Closure& callback) {
  DCHECK(!HasBeenStopped());
  stop_callback_ = callback;
}

}  // namespace secure_channel

}  // namespace chromeos


text:
@interface LogEntry : NSManagedObject

@property (nonatomic, strong) NSNumber * context;
@property (nonatomic, strong) NSNumber * level;
@property (nonatomic, strong) NSString * message;
@property (nonatomic, strong) NSDate   * timestamp;

@end


text:
package org.keycloak.example.photoz.admin;

import org.keycloak.example.photoz.entity.Album;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Pedro Igor
 */
@Path("/admin/album")
public class AdminAlbumService {

    public static final String SCOPE_ADMIN_ALBUM_MANAGE = "urn:photoz.com:scopes:album:admin:manage";

    @Inject
    private EntityManager entityManager;

    @Context
    private HttpHeaders headers;

    @GET
    @Produces("application/json")
    public Response findAll() {
        HashMap<String, List<Album>> albums = new HashMap<>();
        List<Album> result = this.entityManager.createQuery("from Album").getResultList();

        for (Album album : result) {
            albums.computeIfAbsent(album.getUserId(), key -> new ArrayList<>()).add(album);
        }

        return Response.ok(albums).build();
    }
}


text:
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

namespace Microsoft.MixedReality.Toolkit
{
    /// <summary>
    /// Extension methods for Unity's Transform class
    /// </summary>
    public static class TransformExtensions
    {
        /// <summary>
        /// An extension method that will get you the full path to an object.
        /// </summary>
        /// <param name="transform">The transform you wish a full path to.</param>
        /// <param name="delimiter">The delimiter with which each object is delimited in the string.</param>
        /// <param name="prefix">Prefix with which the full path to the object should start.</param>
        /// <returns>A delimited string that is the full path to the game object in the hierarchy.</returns>
        public static string GetFullPath(this Transform transform, string delimiter = ".", string prefix = "/")
        {
            var stringBuilder = new StringBuilder();
            GetFullPath(stringBuilder, transform, delimiter, prefix);
            return stringBuilder.ToString();
        }

        private static void GetFullPath(StringBuilder stringBuilder, Transform transform, string delimiter, string prefix)
        {
            if (transform.parent == null)
            {
                stringBuilder.Append(prefix);
            }
            else
            {
                GetFullPath(stringBuilder, transform.parent, delimiter, prefix);
                stringBuilder.Append(delimiter);
            }

            stringBuilder.Append(transform.name);
        }

        /// <summary>
        /// Enumerates all children in the hierarchy starting at the root object.
        /// </summary>
        /// <param name="root">Start point of the traversion set</param>
        public static IEnumerable<Transform> EnumerateHierarchy(this Transform root)
        {
            if (root == null) { throw new ArgumentNullException("root"); }
            return root.EnumerateHierarchyCore(new List<Transform>(0));
        }

        /// <summary>
        /// Enumerates all children in the hierarchy starting at the root object except for the branches in ignore.
        /// </summary>
        /// <param name="root">Start point of the traversion set</param>
        /// <param name="ignore">Transforms and all its children to be ignored</param>
        public static IEnumerable<Transform> EnumerateHierarchy(this Transform root, ICollection<Transform> ignore)
        {
            if (root == null) { throw new ArgumentNullException("root"); }
            if (ignore == null)
            {
                throw new ArgumentNullException("ignore", "Ignore collection can't be null, use EnumerateHierarchy(root) instead.");
            }
            return root.EnumerateHierarchyCore(ignore);
        }

        /// <summary>
        /// Enumerates all children in the hierarchy starting at the root object except for the branches in ignore.
        /// </summary>
        /// <param name="root">Start point of the traversion set</param>
        /// <param name="ignore">Transforms and all its children to be ignored</param>
        private static IEnumerable<Transform> EnumerateHierarchyCore(this Transform root, ICollection<Transform> ignore)
        {
            var transformQueue = new Queue<Transform>();
            transformQueue.Enqueue(root);

            while (transformQueue.Count > 0)
            {
                var parentTransform = transformQueue.Dequeue();

                if (!parentTransform || ignore.Contains(parentTransform)) { continue; }

                for (var i = 0; i < parentTransform.childCount; i++)
                {
                    transformQueue.Enqueue(parentTransform.GetChild(i));
                }

                yield return parentTransform;
            }
        }

        /// <summary>
        /// Calculates the bounds of all the colliders attached to this GameObject and all its children
        /// </summary>
        /// <param name="transform">Transform of root GameObject the colliders are attached to </param>
        /// <returns>The total bounds of all colliders attached to this GameObject.
        /// If no colliders attached, returns a bounds of center and extents 0</returns>
        public static Bounds GetColliderBounds(this Transform transform)
        {
            Collider[] colliders = transform.GetComponentsInChildren<Collider>();
            if (colliders.Length == 0) { return new Bounds(); }

            Bounds bounds = colliders[0].bounds;
            for (int i = 1; i < colliders.Length; i++)
            {
                bounds.Encapsulate(colliders[i].bounds);
            }
            return bounds;
        }

        /// <summary>
        /// Checks if the provided transforms are child/parent related.
        /// </summary>
        /// <returns>True if either transform is the parent of the other or if they are the same</returns>
        public static bool IsParentOrChildOf(this Transform transform1, Transform transform2)
        {
            return transform1.IsChildOf(transform2) || transform2.IsChildOf(transform1);
        }

        /// <summary>
        /// Find the first component of type <typeparamref name="T"/> in the ancestors of the specified transform.
        /// </summary>
        /// <typeparam name="T">Type of component to find.</typeparam>
        /// <param name="startTransform">Transform for which ancestors must be considered.</param>
        /// <param name="includeSelf">Indicates whether the specified transform should be included.</param>
        /// <returns>The component of type <typeparamref name="T"/>. Null if it none was found.</returns>
        public static T FindAncestorComponent<T>(this Transform startTransform, bool includeSelf = true) where T : Component
        {
            foreach (Transform transform in startTransform.EnumerateAncestors(includeSelf))
            {
                T component = transform.GetComponent<T>();
                if (component != null)
                {
                    return component;
                }
            }

            return null;
        }

        /// <summary>
        /// Enumerates the ancestors of the specified transform.
        /// </summary>
        /// <param name="startTransform">Transform for which ancestors must be returned.</param>
        /// <param name="includeSelf">Indicates whether the specified transform should be included.</param>
        /// <returns>An enumeration of all ancestor transforms of the specified start transform.</returns>
        public static IEnumerable<Transform> EnumerateAncestors(this Transform startTransform, bool includeSelf)
        {
            if (!includeSelf)
            {
                startTransform = startTransform.parent;
            }

            for (Transform transform = startTransform; transform != null; transform = transform.parent)
            {
                yield return transform;
            }
        }

        /// <summary>
        /// Transforms the size from local to world.
        /// </summary>
        /// <param name="transform">The transform.</param>
        /// <param name="localSize">The local size.</param>
        /// <returns>World size.</returns>
        public static Vector3 TransformSize(this Transform transform, Vector3 localSize)
        {
            Vector3 transformedSize = new Vector3(localSize.x, localSize.y, localSize.z);

            Transform t = transform;
            do
            {
                transformedSize.x *= t.localScale.x;
                transformedSize.y *= t.localScale.y;
                transformedSize.z *= t.localScale.z;
                t = t.parent;
            }
            while (t != null);

            return transformedSize;
        }

        /// <summary>
        /// Transforms the size from world to local.
        /// </summary>
        /// <param name="transform">The transform.</param>
        /// <param name="worldSize">The world size</param>
        /// <returns>World size.</returns>
        public static Vector3 InverseTransformSize(this Transform transform, Vector3 worldSize)
        {
            Vector3 transformedSize = new Vector3(worldSize.x, worldSize.y, worldSize.z);

            Transform t = transform;
            do
            {
                transformedSize.x /= t.localScale.x;
                transformedSize.y /= t.localScale.y;
                transformedSize.z /= t.localScale.z;
                t = t.parent;
            }
            while (t != null);

            return transformedSize;
        }

        /// <summary>
        /// Gets the hierarchical depth of the Transform from its root. Returns -1 if the transform is the root.
        /// </summary>
        /// <param name="t">The transform to get the depth for.</param>
        public static int GetDepth(this Transform t)
        {
            int depth = -1;

            Transform root = t.transform.root;
            if (root == t.transform)
            {
                return depth;
            }

            TryGetDepth(t, root, ref depth);

            return depth;
        }

        /// <summary>
        /// Tries to get the hierarchical depth of the Transform from the specified parent. This method is recursive.
        /// </summary>
        /// <param name="target">The transform to get the depth for</param>
        /// <param name="parent">The starting transform to look for the target transform in</param>
        /// <param name="depth">The depth of the target transform</param>
        /// <returns>'true' if the depth could be retrieved, or 'false' because the transform is a root transform.</returns>
        public static bool TryGetDepth(Transform target, Transform parent, ref int depth)
        {
            foreach (Transform child in parent)
            {
                depth++;
                if (child == target.transform || TryGetDepth(target, child, ref depth))
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Walk hierarchy looking for named transform
        /// </summary>
        /// <param name="t">root transform to start searching from</param>
        /// <param name="name">name to look for</param>
        /// <returns>returns found transform or null if none found</returns>
        public static Transform GetChildRecursive(Transform t, string name)
        {
            int numChildren = t.childCount;
            for (int ii = 0; ii < numChildren; ++ii)
            {
                Transform child = t.GetChild(ii);
                if (child.name == name)
                {
                    return child;
                }
                Transform foundIt = GetChildRecursive(child, name);
                if (foundIt != null)
                {
                    return foundIt;
                }
            }
            return null;
        }
    }
}


text:
require 'fileutils'
require 'yaml'
require 'digest/sha1'

# This script should be run from Marked 2 to work

# Change dir to MARKED_ORIGIN path env var set by Marked and assign variables for all env vars.
Dir.chdir(ENV['MARKED_ORIGIN'])

def log( data )
	timestamp = Time.now.getutc
	file = $mlpp_marked_path
	output = "  \n`#{timestamp}` (`#{file}`): #{data}"
	log_path = File.join($mlpp_script_dir, "log.txt")
	File.open(log_path, 'a') { |f| f.write(output) }
end

def read_config( file )
	# Read config file, create it with default values if it doesn't exist

	def write_default_config( file )

		default_config = YAML.load_file( File.join( $mlpp_lib_dir, "default_config" ))
		dirname = File.dirname( file )

		unless File.directory?( dirname )
			FileUtils.mkdir_p( dirname )
		end
		IO.write( file, YAML.dump(default_config) )
		return default_config
	end

	if File.file?( file )
		config = YAML.load_file( file )
		if !config
			config = write_default_config( file )
		end
	else
		config = write_default_config( file )
	end
	return config
end

def find_and_process_snippets( markdown )

	# Regex to find lilypond snippets. First part determines if it is a code-block or else checks if it's a LilyPond-snippet.
	re_get_snippet = /((^`{3,})\w*\n[\s\S]*?\n(\2))|^(?:$\n(?:(```)\w*$)*)([\s\S]*?)(\5*\n)/mi

	# Find all simple snippets in markdown and process them.
	processed_markdown = markdown.gsub(re_get_snippet).with_index do | m, index |
		if $2 # If group 2 is present, then it should display as code - proceed without processing
			$1
		else # Else check what type of snippet (simple|full|...)
			case $4
			when "lilypond-snippet"
				output_src = process_lilypond_snippet($6, index)
				"

"
			else
				"*The `#{$4}`-tag is not yet implemented!*"
			end
		end
	end
	return processed_markdown
end

def process_lilypond_snippet(snippet, index)

	# Regex to get snippet data, group 1 = config, group 2 = music.
	extracted_data = /(?:^---\s*([\s\S]*)---\s*$([\s\S]*))/.match( snippet )

	# Process config and music.
	lilypond_config = ""
	if extracted_data then
		lilypond_config = YAML.load( extracted_data[1] ) unless extracted_data[1] == ""
		lilypond_content = extracted_data[2]
	else
		# If extracted_data is empty just pass full snippet as lilypond conten
		lilypond_content = snippet
	end
	# Make lilypond file from snippet data
	file_src = make_lilypond_output( {
		"lilypond_config"       => lilypond_config,
		"lilypond_content" => lilypond_content,
		}, index )
	return file_src
end

def make_lilypond_output( lilypond_obj, index )
	# Construct the lilypond file and run LilyPond on it to get image

	def make_filenames( obj )
		# Get full path to data-dir, make dir if it doesn't exist yet, goto dir
		lilypond_dir = File.expand_path( $mlpp_lilypond_data_dir_full_path )
		FileUtils.mkdir_p( lilypond_dir )
		Dir.chdir(lilypond_dir)

		simple_snippet_filename = "#{obj["lilypond_filename"]}-#{obj["sha"]}.ly"
		simple_snippet_filename_lastrun = "_#{simple_snippet_filename}"

		# Filenames of lilypond snippet files and last run files
		lilypond_filename = File.join(lilypond_dir, "#{obj["lilypond_filename"]}-#{obj["sha"]}.ly")
		lilypond_last_run_filename = File.join(lilypond_dir, simple_snippet_filename_lastrun)
		lilypond_basename = File.basename(lilypond_filename, ".ly")
		return {
			"name" => lilypond_filename,
			"lastrun" => lilypond_last_run_filename,
			"basename" => lilypond_basename,
		}
	end

	template = get_lilypond_template( lilypond_obj["lilypond_config"]["template"], index )

	# Produce lilypond code from snippet data and template file
	constructed_lilypond_code = replace_placeholders_in_template({
		"variables" => lilypond_obj["lilypond_config"]["variables"],
		"content"   => lilypond_obj["lilypond_content"],
		"template"  => template["content"],
		"config"    => lilypond_obj["lilypond_config"]
		})
	sha_query_string = Digest::SHA1.hexdigest( constructed_lilypond_code )

	filename = make_filenames({
		"lilypond_filename" => "lilypond-snippet",
		"last_run_prefix" => "_",
		"index" => index,
		"sha" => sha_query_string,
		})

	# Write lilypond code to disk
	IO.write( filename["name"], constructed_lilypond_code)

	# If there exist a previous version of the lilypond code read it
	if File.file?(filename["lastrun"])

		# Check if lilypond-snippet files has changed since last run, then run LilyPond
		last_run_file_content = File.read( filename["lastrun"] )
	else
		last_run_file_content = ""
	end

	if constructed_lilypond_code != last_run_file_content
		# Copy last run lilypond file before generating new output.
		`cp "#{filename["name"]}" "#{filename["lastrun"]}"`

		# Run LilyPond command
		`#{$mlpp_lilypond_bin} #{$mlpp_lilypond_argumets} "#{filename["name"]}"`
		# Clean up unused files generated by LilyPond
		`rm #{filename["basename"]}*.eps #{filename["basename"]}*.count #{filename["basename"]}*.tex #{filename["basename"]}*.texi`
	end

	# Add a timestamp to prevent caching of image
	generated_file = File.join($mlpp_lilypond_data_dir_relative_path, "#{filename["basename"]}.png")
	message        = template["message"]
	return {"file" => generated_file, "message" => message}
end

def run_lilypond( obj )
	filename = obj["name"] # Move filename function to run_lilypond
	type = obj["type"]
	dpi = obj["dpi"]
	generated_file = "#{filename}.#{type}"
	return generated_file
end

def replace_placeholders_in_template( obj )
	template = obj["template"]
	unless obj["config"]["content_placeholder"]
		lilypond_content_placeholder = '#{lilypond_content}'
	else
		lilypond_content_placeholder = "\#\{#{obj["config"]["content_placeholder"]}\}"
	end

	template = template.gsub(lilypond_content_placeholder, obj["content"])
	if obj["variables"]
		obj["variables"].each do | key, var |
			template = template.gsub("\#\{#{key}\}", var)
		end
	end
	return template
end

def get_lilypond_template( template_file, index = '?')
	log_template = false
	# Set default template if template_file is empty or not set
	if !template_file
		template_file = $mlpp_default_template
	end

	# Search for templates:
	#
	# 1. If env var MARKED_ORIGIN is set, start searching for templates here
	# 2. Look in $HOME/."processor_name"/templates
	# 3. (Use the built in template, located?)
	#
	# return template

	template_path = File.join(
		File.dirname(template_file),
		File.basename(template_file, ".*") + ".ly"
		)

	# Different locations to look for template
	marked_origin_template = File.join( $mlpp_marked_origin, template_path )
	template_dir_template = File.join( $mlpp_template_dir, template_path )

	template_message = ''

	# Search for file as entered
	if File.file?( File.expand_path( template_path ))
		template_content = IO.read( template_path )
		if log_template
			log( "Template found (#{index}):\n #{template_file}" )
		end
	# Search for file in marked_origin dir

elsif File.file?( marked_origin_template )
	template_content = IO.read( marked_origin_template )
	if log_template
		log( "Template found (#{index}):\n #{marked_origin_template}" )
	end

	# Search in template dir in user-root

elsif File.file?( template_dir_template )
	template_content = IO.read( template_dir_template )
	if log_template
		log( "Template found (#{index}):\n #{template_dir_template}" )
	end
else
		# Get builtin template if template not found
		template_content = IO.read( $mlpp_builtin_template )
		template_message = "___Template `#{template_path}` in the snippet no `#{index}` was not found.___"

	end
	return {"content" => template_content, "message" => template_message}
end

# Global variables
$mlpp_marked_origin                   = ENV['MARKED_ORIGIN'].dup.force_encoding("UTF-8")
$mlpp_marked_path                     = ENV['MARKED_PATH'].dup.force_encoding("UTF-8")
$mlpp_marked_ext                      = ENV['MARKED_EXT'].dup.force_encoding("UTF-8")
$mlpp_marked_filename                 = File.basename($mlpp_marked_path, ".#{$mlpp_marked_ext}")
$mlpp_lilypond_data_dir_relative_path = "./#{$mlpp_marked_filename}-lilypond-data"
$mlpp_lilypond_data_dir_full_path     = File.join($mlpp_marked_origin, $mlpp_lilypond_data_dir_relative_path)
$mlpp_script_dir                      = File.dirname(__FILE__)
$mlpp_lib_dir                         = File.join( $mlpp_script_dir, "lib" )
$mlpp_home_dir                        = File.join(ENV['HOME'], ".markdown-lilypond-preprocessor")
$mlpp_config_file                     = File.join($mlpp_home_dir, "config")
$mlpp_template_dir                    = File.join($mlpp_home_dir, "templates")
$mlpp_builtin_template                = File.join($mlpp_script_dir, "lib/default-template.ly")
$mlpp_default_template                = $mlpp_builtin_template
$mlpp_config                          = read_config( $mlpp_config_file )
$mlpp_lilypond_bin                    = $mlpp_config["lilypond_bin"]
$mlpp_lilypond_argumets               = '-dbackend=eps -dresolution=600 --png'

if $mlpp_config["default_template"]
	$mlpp_default_template = $mlpp_config["default_template"]
end

input = $stdin.read
processed_markdown = find_and_process_snippets( input )
$stdout.print processed_markdown


text:
import { MyLoadingComponent } from "./components/my-loading-component";
import "./theme.light.less";
import Loadable from "react-loadable";

const AsyncLightApp = Loadable({
    loader: () => import("./AppBase"),
    loading: MyLoadingComponent,
});

export default AsyncLightApp;


text:
package com.andrievsky.wms.module.manager;

public final class RemoteClientMethods {

	public static final String CLOSE_ROOM = "onCloseRoom";
	public static final String OPEN_ROOM = "openRoom";
	public static final String ADD_USER = "addUser";
	public static final String REMOVE_USER = "removeUser";
	public static final String MESSAGE = "message";
	public static final String UPDATE_CASTER_CAMERA_STREAM = "updateCasterCameraStream";
	public static final String UPDATE_CASTER_DESKTOP_STREAM = "updateCasterDesktopStream";
	public static final String UPDATE_SUBSCRIBER_CAMERA_STREAM = "updateSubscriberCameraStream";
	public static final String REMOVE_STREAM = "removeStream";
	public static final String ERROR = "error";
	public static final String UPDATE_ROOM_LIST = "updateRoomList";
}


text:
layout: docwithnav
title: "Installing a Kubernetes Master Node via Docker"
---
<!-- BEGIN MUNGE: UNVERSIONED_WARNING -->

<!-- END MUNGE: UNVERSIONED_WARNING -->

## Installing a Kubernetes Master Node via Docker

We'll begin by setting up the master node.  For the purposes of illustration, we'll assume that the IP of this machine is `${MASTER_IP}`

There are two main phases to installing the master:
   * [Setting up `flanneld` and `etcd`](#setting-up-flanneld-and-etcd)
   * [Starting the Kubernetes master components](#starting-the-kubernetes-master)

## Setting up flanneld and etcd

_Note_:
There is a [bug](https://github.com/docker/docker/issues/14106) in Docker 1.7.0 that prevents this from working correctly.
Please install Docker 1.6.2 or Docker 1.7.1.

### Setup Docker-Bootstrap

We're going to use `flannel` to set up networking between Docker daemons.  Flannel itself (and etcd on which it relies) will run inside of
Docker containers themselves.  To achieve this, we need a separate "bootstrap" instance of the Docker daemon.  This daemon will be started with
`--iptables=false` so that it can only run containers with `--net=host`.  That's sufficient to bootstrap our system.

Run:

{% highlight sh %}
{% raw %}
sudo sh -c 'docker -d -H unix:///var/run/docker-bootstrap.sock -p /var/run/docker-bootstrap.pid --iptables=false --ip-masq=false --bridge=none --graph=/var/lib/docker-bootstrap 2> /var/log/docker-bootstrap.log 1> /dev/null &'
{% endraw %}
{% endhighlight %}

_Important Note_:
If you are running this on a long running system, rather than experimenting, you should run the bootstrap Docker instance under something like SysV init, upstart or systemd so that it is restarted
across reboots and failures.

### Startup etcd for flannel and the API server to use

Run:

{% highlight sh %}
{% raw %}
sudo docker -H unix:///var/run/docker-bootstrap.sock run --net=host -d gcr.io/google_containers/etcd:2.0.12 /usr/local/bin/etcd --addr=127.0.0.1:4001 --bind-addr=0.0.0.0:4001 --data-dir=/var/etcd/data
{% endraw %}
{% endhighlight %}

Next, you need to set a CIDR range for flannel.  This CIDR should be chosen to be non-overlapping with any existing network you are using:

{% highlight sh %}
{% raw %}
sudo docker -H unix:///var/run/docker-bootstrap.sock run --net=host gcr.io/google_containers/etcd:2.0.12 etcdctl set /coreos.com/network/config '{ "Network": "10.1.0.0/16" }'
{% endraw %}
{% endhighlight %}

### Set up Flannel on the master node

Flannel is a network abstraction layer build by CoreOS, we will use it to provide simplified networking between our Pods of containers.

Flannel re-configures the bridge that Docker uses for networking.  As a result we need to stop Docker, reconfigure its networking, and then restart Docker.

#### Bring down Docker

To re-configure Docker to use flannel, we need to take docker down, run flannel and then restart Docker.

Turning down Docker is system dependent, it may be:

{% highlight sh %}
{% raw %}
sudo /etc/init.d/docker stop
{% endraw %}
{% endhighlight %}

or

{% highlight sh %}
{% raw %}
sudo systemctl stop docker
{% endraw %}
{% endhighlight %}

or it may be something else.

#### Run flannel

Now run flanneld itself:

{% highlight sh %}
{% raw %}
sudo docker -H unix:///var/run/docker-bootstrap.sock run -d --net=host --privileged -v /dev/net:/dev/net quay.io/coreos/flannel:0.5.0
{% endraw %}
{% endhighlight %}

The previous command should have printed a really long hash, copy this hash.

Now get the subnet settings from flannel:

{% highlight sh %}
{% raw %}
sudo docker -H unix:///var/run/docker-bootstrap.sock exec <really-long-hash-from-above-here> cat /run/flannel/subnet.env
{% endraw %}
{% endhighlight %}

#### Edit the docker configuration

You now need to edit the docker configuration to activate new flags.  Again, this is system specific.

This may be in `/etc/default/docker` or `/etc/systemd/service/docker.service` or it may be elsewhere.

Regardless, you need to add the following to the docker command line:

{% highlight sh %}
{% raw %}
--bip=${FLANNEL_SUBNET} --mtu=${FLANNEL_MTU}
{% endraw %}
{% endhighlight %}

#### Remove the existing Docker bridge

Docker creates a bridge named `docker0` by default.  You need to remove this:

{% highlight sh %}
{% raw %}
sudo /sbin/ifconfig docker0 down
sudo brctl delbr docker0
{% endraw %}
{% endhighlight %}

You may need to install the `bridge-utils` package for the `brctl` binary.

#### Restart Docker

Again this is system dependent, it may be:

{% highlight sh %}
{% raw %}
sudo /etc/init.d/docker start
{% endraw %}
{% endhighlight %}

it may be:

{% highlight sh %}
{% raw %}
systemctl start docker
{% endraw %}
{% endhighlight %}

## Starting the Kubernetes Master

Ok, now that your networking is set up, you can startup Kubernetes, this is the same as the single-node case, we will use the "main" instance of the Docker daemon for the Kubernetes components.

{% highlight sh %}
{% raw %}
sudo docker run \
    --volume=/:/rootfs:ro \
    --volume=/sys:/sys:ro \
    --volume=/dev:/dev \
    --volume=/var/lib/docker/:/var/lib/docker:rw \
    --volume=/var/lib/kubelet/:/var/lib/kubelet:rw \
    --volume=/var/run:/var/run:rw \
    --net=host \
    --privileged=true \
    --pid=host \
    -d \
    gcr.io/google_containers/hyperkube:v1.0.1 /hyperkube kubelet --api-servers=http://localhost:8080 --v=2 --address=0.0.0.0 --enable-server --hostname-override=127.0.0.1 --config=/etc/kubernetes/manifests-multi --cluster-dns=10.0.0.10 --cluster-domain=cluster.local
{% endraw %}
{% endhighlight %}

> Note that `--cluster-dns` and `--cluster-domain` is used to deploy dns, feel free to discard them if dns is not needed.

### Also run the service proxy

{% highlight sh %}
{% raw %}
sudo docker run -d --net=host --privileged gcr.io/google_containers/hyperkube:v1.0.1 /hyperkube proxy --master=http://127.0.0.1:8080 --v=2
{% endraw %}
{% endhighlight %}

### Test it out

At this point, you should have a functioning 1-node cluster.  Let's test it out!

Download the kubectl binary and make it available by editing your PATH ENV.
([OS X](http://storage.googleapis.com/kubernetes-release/release/v1.0.1/bin/darwin/amd64/kubectl))
([linux](http://storage.googleapis.com/kubernetes-release/release/v1.0.1/bin/linux/amd64/kubectl))

List the nodes

{% highlight sh %}
{% raw %}
kubectl get nodes
{% endraw %}
{% endhighlight %}

This should print:

{% highlight console %}
{% raw %}
NAME        LABELS                             STATUS
127.0.0.1   kubernetes.io/hostname=127.0.0.1   Ready
{% endraw %}
{% endhighlight %}

If the status of the node is `NotReady` or `Unknown` please check that all of the containers you created are successfully running.
If all else fails, ask questions on [Slack](../../troubleshooting.html#slack).

### Next steps

Move on to [adding one or more workers](worker.html) or [deploy a dns](deployDNS.html)

<!-- BEGIN MUNGE: IS_VERSIONED -->
<!-- TAG IS_VERSIONED -->
<!-- END MUNGE: IS_VERSIONED -->

<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/getting-started-guides/docker-multinode/master.md?pixel)]()
<!-- END MUNGE: GENERATED_ANALYTICS -->