text:
exports.getWidget = function(feather, cb) {
  cb(null, {
    name: "api_tester.channelClient2",
    path: "widgets/channelClient2/"
  });
};

redpajama_set_name: RedPajamaGithub


text:
<?php

namespace spec\Sylius\Component\Core\OrderProcessing;

use Doctrine\Common\Collections\ArrayCollection;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderShippingStates;
use Sylius\Component\Core\Model\Payment;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\ShipmentInterface;

/**
 * @author Paweł Jędrzejewski <pawel@sylius.org>
 */
class StateResolverSpec extends ObjectBehavior
{
    function it_is_initializable()
    {
        $this->shouldHaveType('Sylius\Component\Core\OrderProcessing\StateResolver');
    }

    function it_implements_Sylius_order_state_resolver_interface()
    {
        $this->shouldImplement('Sylius\Component\Core\OrderProcessing\StateResolverInterface');
    }

    function it_marks_order_as_a_backorders_if_it_contains_backordered_units(OrderInterface $order)
    {
        $order->isBackorder()->shouldBeCalled()->willReturn(true);

        $order->setShippingState(OrderShippingStates::BACKORDER)->shouldBeCalled();
        $this->resolveShippingState($order);
    }

    function it_marks_order_as_shipped_if_all_shipments_delivered(
        OrderInterface $order,
        ShipmentInterface $shipment1,
        ShipmentInterface $shipment2
    ) {
        $order->isBackorder()->shouldBeCalled()->willReturn(false);
        $order->getShipments()->willReturn(array($shipment1, $shipment2));

        $shipment1->getState()->willReturn(ShipmentInterface::STATE_SHIPPED);
        $shipment2->getState()->willReturn(ShipmentInterface::STATE_SHIPPED);

        $order->setShippingState(OrderShippingStates::SHIPPED)->shouldBeCalled();
        $this->resolveShippingState($order);
    }

    function it_marks_order_as_partially_shipped_if_not_all_shipments_delivered(
        OrderInterface $order,
        ShipmentInterface $shipment1,
        ShipmentInterface $shipment2
    ) {
        $order->isBackorder()->shouldBeCalled()->willReturn(false);
        $order->getShipments()->willReturn(array($shipment1, $shipment2));

        $shipment1->getState()->willReturn(ShipmentInterface::STATE_SHIPPED);
        $shipment2->getState()->willReturn(ShipmentInterface::STATE_READY);

        $order->setShippingState(OrderShippingStates::PARTIALLY_SHIPPED)->shouldBeCalled();
        $this->resolveShippingState($order);
    }

    function it_marks_order_as_returned_if_all_shipments_were_returned(
        OrderInterface $order,
        ShipmentInterface $shipment1,
        ShipmentInterface $shipment2
    ) {
        $order->isBackorder()->shouldBeCalled()->willReturn(false);
        $order->getShipments()->willReturn(array($shipment1, $shipment2));

        $shipment1->getState()->willReturn(ShipmentInterface::STATE_RETURNED);
        $shipment2->getState()->willReturn(ShipmentInterface::STATE_RETURNED);

        $order->setShippingState(OrderShippingStates::RETURNED)->shouldBeCalled();
        $this->resolveShippingState($order);
    }

    function it_marks_order_as_completed_if_fully_paid(
        OrderInterface $order
    ) {
        $payment1 = new Payment();
        $payment1->setAmount(10000);
        $payment1->setState(PaymentInterface::STATE_COMPLETED);
        $payments = new ArrayCollection(array($payment1));

        $order->hasPayments()->willReturn(true);
        $order->getPayments()->willReturn($payments);

        $order->getTotal()->willReturn(10000);
        $order->setPaymentState(PaymentInterface::STATE_COMPLETED)->shouldBeCalled();
        $this->resolvePaymentState($order);
    }

    function it_marks_order_as_completed_if_fully_paid_multiple_payments(
        OrderInterface $order
    ) {
        $payment1 = new Payment();
        $payment1->setAmount(6000);
        $payment1->setState(PaymentInterface::STATE_COMPLETED);
        $payment2 = new Payment();
        $payment2->setAmount(4000);
        $payment2->setState(PaymentInterface::STATE_COMPLETED);
        $payments = new ArrayCollection(array($payment1, $payment2));

        $order->hasPayments()->willReturn(true);
        $order->getPayments()->willReturn($payments);

        $order->getTotal()->willReturn(10000);
        $order->setPaymentState(PaymentInterface::STATE_COMPLETED)->shouldBeCalled();
        $this->resolvePaymentState($order);
    }

    function it_marks_order_as_processing_if_partially_paid(OrderInterface $order)
    {
        $payment1 = new Payment();
        $payment1->setAmount(6000);
        $payment1->setState(PaymentInterface::STATE_COMPLETED);
        $payment2 = new Payment();
        $payment2->setAmount(4000);
        $payment2->setState(PaymentInterface::STATE_NEW);
        $payments = new ArrayCollection(array($payment1, $payment2));

        $order->hasPayments()->willReturn(true);
        $order->getPayments()->willReturn($payments);

        $order->getTotal()->willReturn(10000);
        $order->setPaymentState(PaymentInterface::STATE_PROCESSING)->shouldBeCalled();
        $this->resolvePaymentState($order);
    }

    function it_marks_order_as_processing_if_one_of_the_payment_is_processing(OrderInterface $order)
    {
        $payment1 = new Payment();
        $payment1->setAmount(6000);
        $payment1->setState(PaymentInterface::STATE_PROCESSING);
        $payment2 = new Payment();
        $payment2->setAmount(4000);
        $payment2->setState(PaymentInterface::STATE_NEW);
        $payments = new ArrayCollection(array($payment1, $payment2));

        $order->hasPayments()->willReturn(true);
        $order->getPayments()->willReturn($payments);

        $order->getTotal()->willReturn(10000);
        $order->setPaymentState(PaymentInterface::STATE_PROCESSING)->shouldBeCalled();
        $this->resolvePaymentState($order);
    }

    function it_marks_order_as_new_if_no_payment_is_in_process(OrderInterface $order)
    {
        $payment1 = new Payment();
        $payment1->setAmount(6000);
        $payment1->setState(PaymentInterface::STATE_NEW);
        $payment2 = new Payment();
        $payment2->setAmount(4000);
        $payment2->setState(PaymentInterface::STATE_NEW);
        $payments = new ArrayCollection(array($payment1, $payment2));

        $order->hasPayments()->willReturn(true);
        $order->getPayments()->willReturn($payments);

        $order->getTotal()->willReturn(10000);
        $order->setPaymentState(PaymentInterface::STATE_NEW)->shouldBeCalled();
        $this->resolvePaymentState($order);
    }
}


text:
package com.svbio.cloudkeeper.linker;

import cloudkeeper.types.ByteSequence;
import com.svbio.cloudkeeper.model.LinkerException;
import com.svbio.cloudkeeper.model.bare.element.serialization.BareByteSequence;
import com.svbio.cloudkeeper.model.immutable.element.Key;
import com.svbio.cloudkeeper.model.runtime.element.serialization.RuntimeByteSequence;
import com.svbio.cloudkeeper.model.util.ByteSequences;

import java.util.Arrays;
import java.util.Collection;

final class ByteSequenceImpl extends SerializationNodeImpl implements RuntimeByteSequence {
    private final byte[] array;

    /**
     * Constructor for effectively immutable instances (already frozen at construction time).
     */
    ByteSequenceImpl(Key key, byte[] bytes) {
        super(key);
        array = Arrays.copyOf(bytes, bytes.length);
    }

    /**
     * Copy constructor (from unverified instance). Instance needs to be explicitly frozen before use.
     */
    ByteSequenceImpl(BareByteSequence original, CopyContext parentContext) throws LinkerException {
        super(original, parentContext);
        byte[] originalArray
            = Preconditions.requireNonNull(original.getArray(), getCopyContext().newContextForProperty("array"));
        array = Arrays.copyOf(originalArray, originalArray.length);
    }

    @Override
    public String toString() {
        return Default.toString(this);
    }

    @Override
    public byte[] getArray() {
        return array.clone();
    }

    @Override
    public ByteSequence toByteSequence() {
        return ByteSequences.arrayBacked(array);
    }

    @Override
    void collectEnclosed(Collection<AbstractFreezable> freezables) { }

    @Override
    void finishFreezable(FinishContext context) { }
}


text:
package org.fusesource.scalate.util

/**
 * Some helper methods for working with URIs and query strings
 *
 * @version $Revision: 1.1 $
 */
object URIs {

  /**
   * Creates a URI using a path and optional query string
   */
  def uri(path: String, query: String = "") = {
    if (query != null && query.length > 0) {
      val separator = if (path.contains("?")) "&" else "?"
      path + separator + query
    } else {
      path
    }
  }

  /**
   * Combines the URI path, query string with additional query terms which will avoid duplicates
   */
  def uriPlus(path: String, query: String, addQuery: String) = {
    val newQuery = (splitQuery(query) ++ splitQuery(addQuery)).distinct
    uri(path, joinQuery(newQuery))

  }

  /**
   * Removes the given query terms from the query string if they are there
   */
  def uriMinus(path: String, query: String, removeQuery: String) = {
    val remove = splitQuery(removeQuery)
    val newQuery = splitQuery(query).filter(!remove.contains(_))
    uri(path, joinQuery(newQuery))
  }

  /**
   * Split a query expression into separate clauses
   */
  protected def splitQuery(query: String): Seq[String] = if (query != null && query.length > 0) query.split("&").toSeq else Nil

  protected def joinQuery(queryArgs: Seq[String]) = queryArgs.mkString("&")
}


text:
<!DOCTYPE html>
{% load staticfiles %}

		<title>HACKATHON :D</title>

		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
		<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
		<link rel="stylesheet" href="{% static "geonote/marker-cluster.css" %}" />
		<link rel="stylesheet" href="{% static "geonote/marker-cluster.default.css" %}" />

		<link rel="stylesheet" href="{% static "geonote/leaflet.awesome-markers.css" %}"/>

		<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

		<button id="go_to_form" type="button" class="btn btn-default">Create Note</button>
		<button id="go_to_events" type="button" class="btn btn-default">See Events</button>

			<form>
				<textarea ng-model="noteContent" id="note" class="form-control"></textarea>
				<select ng-model="category" ng-options="cat.name for cat in categories" class="form-control" style="width:10vw;display:inline-block;">
				</select>
				<button id="submit" ng-click="createNote()" type="button" class="btn btn-primary">Submit</button>
				<button id="cancel" type="button" class="btn btn-danger">Cancel</button>
			</form>

			 <button type="button" class="close" id="events_close">×Close</button>


text:
{namespace n=Tx_News_ViewHelpers}

<f:if condition="{settings.cssFile}">
	<n:includeFile path="{settings.cssFile}" />
</f:if>

        <f:render section="content"/>


text:
#ifndef __OPKELE_UTIL_INTERNAL_H
#define __OPKELE_UTIL_INTERNAL_H

//#include <openssl/bn.h>
//#include <openssl/dh.h>
//#include <opkele/types.h>

namespace opkele {
    namespace util {

	/**
	 * Convenience class encapsulating SSL BIGNUM object for the purpose of
	 * automatical freeing.
	 */
/*
	class bignum_t {
	    public:
		BIGNUM *_bn;

		bignum_t() : _bn(0) { }
		bignum_t(BIGNUM *bn) : _bn(bn) { }
		~bignum_t() throw() { if(_bn) BN_free(_bn); }

		bignum_t& operator=(BIGNUM *bn) { if(_bn) BN_free(_bn); _bn = bn; return *this; }

		operator const BIGNUM*(void) const { return _bn; }
		operator BIGNUM*(void) { return _bn; }
	};
*/
	/**
	 * Convenience clas encapsulating SSL DH object for the purpose of
	 * automatic freeing.
	 */
/*
	class dh_t {
	    public:
		DH *_dh;

		dh_t() : _dh(0) { }
		dh_t(DH *dh) : _dh(dh) { }
		~dh_t() throw() { if(_dh) DH_free(_dh); }

		dh_t& operator=(DH *dh) { if(_dh) DH_free(_dh); _dh = dh; return *this; }

		operator const DH*(void) const { return _dh; }
		operator DH*(void) { return _dh; }

		DH* operator->() { return _dh; }
		const DH* operator->() const { return _dh; }
	};
*/
	/**
	 * Convert base64-encoded SSL BIGNUM to internal representation.
	 * @param b64 base64-encoded number
	 * @return SSL BIGNUM
	 * @throw failed_conversion in case of error
	 */
//	BIGNUM *base64_to_bignum(const string& b64);
	/**
	 * Convert decimal representation to SSL BIGNUM.
	 * @param dec decimal representation
	 * @return resulting BIGNUM
	 * @throw failed_conversion in case of error
	 */
//	BIGNUM *dec_to_bignum(const string& dec);
	/**
	 * Convert SSL BIGNUM data to base64 encoded string.
	 * @param bn BIGNUM
	 * @return base64encoded string
	 */
//	string bignum_to_base64(const BIGNUM *bn);

//	string abi_demangle(const char* mn);
/*
	class change_mode_message_proxy : public basic_openid_message {
	    public:
		const basic_openid_message& x;
		const string& mode;

		change_mode_message_proxy(const basic_openid_message& xx,const string& m) : x(xx), mode(m) { }

		bool has_field(const string& n) const { return x.has_field(n); }
		const string& get_field(const string& n) const {
		    return (n=="mode")?mode:x.get_field(n); }
		bool has_ns(const string& uri) const {return x.has_ns(uri); }
		string get_ns(const string& uri) const { return x.get_ns(uri); }
		fields_iterator fields_begin() const {
		    return x.fields_begin(); }
		fields_iterator fields_end() const {
		    return x.fields_end(); }
	};
*/
    }
}

#endif /* __OPKELE_UTIL_INTERNAL_H */


text:
package install

import (
	"k8s.io/apimachinery/pkg/apimachinery/announced"
	"k8s.io/apimachinery/pkg/apimachinery/registered"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/util/sets"
	"k8s.io/apiserver/pkg/apis/apiserver"
	"k8s.io/apiserver/pkg/apis/apiserver/v1alpha1"
)

// Install registers the API group and adds types to a scheme
func Install(groupFactoryRegistry announced.APIGroupFactoryRegistry, registry *registered.APIRegistrationManager, scheme *runtime.Scheme) {
	if err := announced.NewGroupMetaFactory(
		&announced.GroupMetaFactoryArgs{
			GroupName:                  apiserver.GroupName,
			RootScopedKinds:            sets.NewString("AdmissionConfiguration"),
			VersionPreferenceOrder:     []string{v1alpha1.SchemeGroupVersion.Version},
			AddInternalObjectsToScheme: apiserver.AddToScheme,
		},
		announced.VersionToSchemeFunc{
			v1alpha1.SchemeGroupVersion.Version: v1alpha1.AddToScheme,
		},
	).Announce(groupFactoryRegistry).RegisterAndEnable(registry, scheme); err != nil {
		panic(err)
	}
}


text:
"""Image manipulation API.

Classes defined in this module:
  Image: class used to encapsulate image information and transformations for
    that image.

    The current manipulations that are available are resize, rotate,
    horizontal_flip, vertical_flip, crop and im_feeling_lucky.

    It should be noted that each transform can only be called once per image
    per execute_transforms() call.
"""

import struct

try:
  import json
except:
  import simplejson as json

from google.appengine.api import apiproxy_stub_map
from google.appengine.api import datastore_types
from google.appengine.api.images import images_service_pb
from google.appengine.runtime import apiproxy_errors

BlobKey = datastore_types.BlobKey

JPEG = images_service_pb.OutputSettings.JPEG
PNG = images_service_pb.OutputSettings.PNG
WEBP = images_service_pb.OutputSettings.WEBP
BMP = -1
GIF = -2
ICO = -3
TIFF = -4

OUTPUT_ENCODING_TYPES = frozenset([JPEG, PNG, WEBP])

UNCHANGED_ORIENTATION = images_service_pb.InputSettings.UNCHANGED_ORIENTATION
CORRECT_ORIENTATION = images_service_pb.InputSettings.CORRECT_ORIENTATION

ORIENTATION_CORRECTION_TYPE = frozenset([UNCHANGED_ORIENTATION,
                                         CORRECT_ORIENTATION])

TOP_LEFT = images_service_pb.CompositeImageOptions.TOP_LEFT
TOP_CENTER = images_service_pb.CompositeImageOptions.TOP
TOP_RIGHT = images_service_pb.CompositeImageOptions.TOP_RIGHT
CENTER_LEFT = images_service_pb.CompositeImageOptions.LEFT
CENTER_CENTER = images_service_pb.CompositeImageOptions.CENTER
CENTER_RIGHT = images_service_pb.CompositeImageOptions.RIGHT
BOTTOM_LEFT = images_service_pb.CompositeImageOptions.BOTTOM_LEFT
BOTTOM_CENTER = images_service_pb.CompositeImageOptions.BOTTOM
BOTTOM_RIGHT = images_service_pb.CompositeImageOptions.BOTTOM_RIGHT

ANCHOR_TYPES = frozenset([TOP_LEFT, TOP_CENTER, TOP_RIGHT, CENTER_LEFT,
                          CENTER_CENTER, CENTER_RIGHT, BOTTOM_LEFT,
                          BOTTOM_CENTER, BOTTOM_RIGHT])

MAX_TRANSFORMS_PER_REQUEST = 10

MAX_COMPOSITES_PER_REQUEST = 16

class Error(Exception):
  """Base error class for this module."""

class TransformationError(Error):
  """Error while attempting to transform the image."""

class BadRequestError(Error):
  """The parameters given had something wrong with them."""

class NotImageError(Error):
  """The image data given is not recognizable as an image."""

class BadImageError(Error):
  """The image data given is corrupt."""

class LargeImageError(Error):
  """The image data given is too large to process."""

class InvalidBlobKeyError(Error):
  """The provided blob key was invalid."""

class BlobKeyRequiredError(Error):
  """A blobkey is required for this operation."""

class UnsupportedSizeError(Error):
  """Specified size is not supported by requested operation."""

class Image(object):
  """Image object to manipulate."""

  def __init__(self, image_data=None, blob_key=None):
    """Constructor.

    Args:
      image_data: str, image data in string form.
      blob_key: BlobKey, BlobInfo, str, or unicode representation of BlobKey of
        blob containing the image data.

    Raises:
      NotImageError if the given data is empty.
    """
    if not image_data and not blob_key:
      raise NotImageError("Empty image data.")
    if image_data and blob_key:
      raise NotImageError("Can only take one image or blob key.")

    self._image_data = image_data
    self._blob_key = _extract_blob_key(blob_key)
    self._transforms = []
    self._width = None
    self._height = None
    self._format = None
    self._correct_orientation = UNCHANGED_ORIENTATION
    self._original_metadata = None

  def _check_transform_limits(self):
    """Ensure some simple limits on the number of transforms allowed.

    Raises:
      BadRequestError if MAX_TRANSFORMS_PER_REQUEST transforms have already been
      requested for this image
    """
    if len(self._transforms) >= MAX_TRANSFORMS_PER_REQUEST:
      raise BadRequestError("%d transforms have already been requested on this "
                            "image." % MAX_TRANSFORMS_PER_REQUEST)

  def _update_dimensions(self):
    """Updates the width and height fields of the image.

    Raises:
      NotImageError if the image data is not an image.
      BadImageError if the image data is corrupt.
    """
    if not self._image_data:
      raise NotImageError("Dimensions unavailable for blob key input")
    size = len(self._image_data)
    if size >= 6 and self._image_data.startswith("GIF"):
      self._update_gif_dimensions()
      self._format = GIF;
    elif size >= 8 and self._image_data.startswith("\x89PNG\x0D\x0A\x1A\x0A"):
      self._update_png_dimensions()
      self._format = PNG
    elif size >= 2 and self._image_data.startswith("\xff\xD8"):
      self._update_jpeg_dimensions()
      self._format = JPEG
    elif (size >= 8 and (self._image_data.startswith("II\x2a\x00") or
                         self._image_data.startswith("MM\x00\x2a"))):
      self._update_tiff_dimensions()
      self._format = TIFF
    elif size >= 2 and self._image_data.startswith("BM"):
      self._update_bmp_dimensions()
      self._format = BMP
    elif size >= 4 and self._image_data.startswith("\x00\x00\x01\x00"):
      self._update_ico_dimensions()
      self._format = ICO
    elif (size >= 16 and (self._image_data.startswith("RIFF", 0, 4) and
                          self._image_data.startswith("WEBP", 8, 12) and
                          self._image_data.startswith("VP8 ", 12, 16))):
      self._update_webp_dimensions()
      self._format = WEBP
    else:
      raise NotImageError("Unrecognized image format")

  def _update_gif_dimensions(self):
    """Updates the width and height fields of the gif image.

    Raises:
      BadImageError if the image string is not a valid gif image.
    """

    size = len(self._image_data)
    if size >= 10:
      self._width, self._height = struct.unpack("<HH", self._image_data[6:10])
    else:
      raise BadImageError("Corrupt GIF format")

  def _update_png_dimensions(self):
    """Updates the width and height fields of the png image.

    Raises:
      BadImageError if the image string is not a valid png image.
    """

    size = len(self._image_data)
    if size >= 24 and self._image_data[12:16] == "IHDR":
      self._width, self._height = struct.unpack(">II", self._image_data[16:24])
    else:
      raise BadImageError("Corrupt PNG format")

  def _update_jpeg_dimensions(self):
    """Updates the width and height fields of the jpeg image.

    Raises:
      BadImageError if the image string is not a valid jpeg image.
    """

    size = len(self._image_data)
    offset = 2
    while offset < size:
      while offset < size and ord(self._image_data[offset]) != 0xFF:
        offset += 1
      while offset < size and ord(self._image_data[offset]) == 0xFF:
        offset += 1
      if (offset < size and ord(self._image_data[offset]) & 0xF0 == 0xC0 and
          ord(self._image_data[offset]) != 0xC4):
        offset += 4
        if offset + 4 <= size:
          self._height, self._width = struct.unpack(
              ">HH",
              self._image_data[offset:offset + 4])
          break
        else:
          raise BadImageError("Corrupt JPEG format")
      elif offset + 3 <= size:
        offset += 1
        offset += struct.unpack(">H", self._image_data[offset:offset + 2])[0]
      else:
        raise BadImageError("Corrupt JPEG format")
    if self._height is None or self._width is None:
      raise BadImageError("Corrupt JPEG format")

  def _update_tiff_dimensions(self):
    """Updates the width and height fields of the tiff image.

    Raises:
      BadImageError if the image string is not a valid tiff image.
    """

    size = len(self._image_data)
    if self._image_data.startswith("II"):
      endianness = "<"
    else:
      endianness = ">"
    ifd_offset = struct.unpack(endianness + "I", self._image_data[4:8])[0]
    if ifd_offset + 14 <= size:
      ifd_size = struct.unpack(
          endianness + "H",
          self._image_data[ifd_offset:ifd_offset + 2])[0]
      ifd_offset += 2
      for unused_i in range(0, ifd_size):
        if ifd_offset + 12 <= size:
          tag = struct.unpack(
              endianness + "H",
              self._image_data[ifd_offset:ifd_offset + 2])[0]
          if tag == 0x100 or tag == 0x101:
            value_type = struct.unpack(
                endianness + "H",
                self._image_data[ifd_offset + 2:ifd_offset + 4])[0]
            if value_type == 3:
              format = endianness + "H"
              end_offset = ifd_offset + 10
            elif value_type == 4:
              format = endianness + "I"
              end_offset = ifd_offset + 12
            else:
              format = endianness + "B"
              end_offset = ifd_offset + 9
            if tag == 0x100:
              self._width = struct.unpack(
                  format,
                  self._image_data[ifd_offset + 8:end_offset])[0]
              if self._height is not None:
                break
            else:
              self._height = struct.unpack(
                  format,
                  self._image_data[ifd_offset + 8:end_offset])[0]
              if self._width is not None:
                break
          ifd_offset += 12
        else:
          raise BadImageError("Corrupt TIFF format")
    if self._width is None or self._height is None:
      raise BadImageError("Corrupt TIFF format")

  def _update_bmp_dimensions(self):
    """Updates the width and height fields of the bmp image.

    Raises:
      BadImageError if the image string is not a valid bmp image.
    """

    size = len(self._image_data)
    if size >= 18:
      header_length = struct.unpack("<I", self._image_data[14:18])[0]
      if ((header_length == 40 or header_length == 108 or
           header_length == 124 or header_length == 64) and size >= 26):

        self._width, self._height = struct.unpack("<II",
                                                  self._image_data[18:26])
      elif header_length == 12 and size >= 22:
        self._width, self._height = struct.unpack("<HH",
                                                  self._image_data[18:22])
      else:
        raise BadImageError("Corrupt BMP format")
    else:
      raise BadImageError("Corrupt BMP format")

  def _update_ico_dimensions(self):
    """Updates the width and height fields of the ico image.

    Raises:
      BadImageError if the image string is not a valid ico image.
    """
    size = len(self._image_data)
    if size >= 8:
      self._width, self._height = struct.unpack("<BB", self._image_data[6:8])

      if not self._width:
        self._width = 256
      if not self._height:
        self._height = 256
    else:
      raise BadImageError("Corrupt ICO format")

  def set_correct_orientation(self, correct_orientation):
    """Set flag to correct image orientation based on image metadata.

    EXIF metadata within the image may contain a parameter indicating its proper
    orientation. This value can equal 1 through 8, inclusive. "1" means that the
    image is in its "normal" orientation, i.e., it should be viewed as it is
    stored. Normally, this "orientation" value has no effect on the behavior of
    the transformations. However, calling this function with the value
    CORRECT_ORIENTATION any orientation specified in the EXIF metadata will be
    corrected during the first transformation.

    NOTE: If CORRECT_ORIENTATION is specified but the image is already in
    portrait orientation, i.e., "taller" than it is "wide" no corrections will
    be made, since it appears that the camera has already corrected it.

    Regardless whether the correction was requested or not, the orientation
    value in the transformed image is always cleared to indicate that no
    additional corrections of the returned image's orientation is necessary.

    Args:
      correct_orientation: a value from ORIENTATION_CORRECTION_TYPE.

    Raises:
      BadRequestError if correct_orientation value is invalid.
    """
    if correct_orientation not in ORIENTATION_CORRECTION_TYPE:
      raise BadRequestError("Orientation correction must be in %s" %
                            ORIENTATION_CORRECTION_TYPE)
    self._correct_orientation = correct_orientation

  def _update_webp_dimensions(self):
    """Updates the width and height fields of the webp image."""

    size = len(self._image_data)

    if size < 30:
      raise BadImageError("Corrupt WEBP format")

    bits = (ord(self._image_data[20]) | (ord(self._image_data[21])<<8) |
            (ord(self._image_data[22]) << 16))

    key_frame = ((bits & 1) == 0)

    if not key_frame:
      raise BadImageError("Corrupt WEBP format")

    profile = (bits >> 1) & 7
    show_frame = (bits >> 4) & 1

    if profile > 3:
      raise BadImageError("Corrupt WEBP format")

    if show_frame == 0:
      raise BadImageError("Corrupt WEBP format")

    self._width, self._height = struct.unpack("<HH", self._image_data[26:30])

    if self._height is None or self._width is None:
      raise BadImageError("Corrupt WEBP format")

  def resize(self, width=0, height=0, crop_to_fit=False,
             crop_offset_x=0.5, crop_offset_y=0.5):
    """Resize the image maintaining the aspect ratio.

    If both width and height are specified, the more restricting of the two
    values will be used when resizing the image. The maximum dimension allowed
    for both width and height is 4000 pixels.
    If both width and height are specified and crop_to_fit is True, the less
    restricting of the two values will be used when resizing and the image will
    be cropped to fit the specified size. In this case the center of cropping
    can be adjusted  by crop_offset_x and crop_offset_y.

    Args:
      width: int, width (in pixels) to change the image width to.
      height: int, height (in pixels) to change the image height to.
      crop_to_fit: If True and both width and height are specified, the image is
        cropped after resize to fit the specified dimensions.
      crop_offset_x: float value between 0.0 and 1.0, 0 is left and 1 is right,
        default is 0.5, the center of image.
      crop_offset_y: float value between 0.0 and 1.0, 0 is top and 1 is bottom,
        default is 0.5, the center of image.

    Raises:
      TypeError when width or height is not either 'int' or 'long' types.
      BadRequestError when there is something wrong with the given height or
        width or if MAX_TRANSFORMS_PER_REQUEST transforms have already been
        requested on this image.
    """
    if (not isinstance(width, (int, long)) or
        not isinstance(height, (int, long))):
      raise TypeError("Width and height must be integers.")
    if width < 0 or height < 0:
      raise BadRequestError("Width and height must be >= 0.")

    if not width and not height:
      raise BadRequestError("At least one of width or height must be > 0.")

    if width > 4000 or height > 4000:
      raise BadRequestError("Both width and height must be <= 4000.")

    if not isinstance(crop_to_fit, bool):
      raise TypeError("crop_to_fit must be boolean.")

    if crop_to_fit and not (width and height):
      raise BadRequestError("Both width and height must be > 0 when "
                            "crop_to_fit is specified")

    self._validate_crop_arg(crop_offset_x, "crop_offset_x")
    self._validate_crop_arg(crop_offset_y, "crop_offset_y")

    self._check_transform_limits()

    transform = images_service_pb.Transform()
    transform.set_width(width)
    transform.set_height(height)
    transform.set_crop_to_fit(crop_to_fit)
    transform.set_crop_offset_x(crop_offset_x)
    transform.set_crop_offset_y(crop_offset_y)

    self._transforms.append(transform)

  def rotate(self, degrees):
    """Rotate an image a given number of degrees clockwise.

    Args:
      degrees: int, must be a multiple of 90.

    Raises:
      TypeError when degrees is not either 'int' or 'long' types.
      BadRequestError when there is something wrong with the given degrees or
      if MAX_TRANSFORMS_PER_REQUEST transforms have already been requested.
    """
    if not isinstance(degrees, (int, long)):
      raise TypeError("Degrees must be integers.")

    if degrees % 90 != 0:
      raise BadRequestError("degrees argument must be multiple of 90.")

    degrees = degrees % 360

    self._check_transform_limits()

    transform = images_service_pb.Transform()
    transform.set_rotate(degrees)

    self._transforms.append(transform)

  def horizontal_flip(self):
    """Flip the image horizontally.

    Raises:
      BadRequestError if MAX_TRANSFORMS_PER_REQUEST transforms have already been
      requested on the image.
    """
    self._check_transform_limits()

    transform = images_service_pb.Transform()
    transform.set_horizontal_flip(True)

    self._transforms.append(transform)

  def vertical_flip(self):
    """Flip the image vertically.

    Raises:
      BadRequestError if MAX_TRANSFORMS_PER_REQUEST transforms have already been
      requested on the image.
    """
    self._check_transform_limits()
    transform = images_service_pb.Transform()
    transform.set_vertical_flip(True)

    self._transforms.append(transform)

  def _validate_crop_arg(self, val, val_name):
    """Validate the given value of a Crop() method argument.

    Args:
      val: float, value of the argument.
      val_name: str, name of the argument.

    Raises:
      TypeError if the args are not of type 'float'.
      BadRequestError when there is something wrong with the given bounding box.
    """
    if type(val) != float:
      raise TypeError("arg '%s' must be of type 'float'." % val_name)

    if not (0 <= val <= 1.0):
      raise BadRequestError("arg '%s' must be between 0.0 and 1.0 "
                            "(inclusive)" % val_name)

  def crop(self, left_x, top_y, right_x, bottom_y):
    """Crop the image.

    The four arguments are the scaling numbers to describe the bounding box
    which will crop the image.  The upper left point of the bounding box will
    be at (left_x*image_width, top_y*image_height) the lower right point will
    be at (right_x*image_width, bottom_y*image_height).

    Args:
      left_x: float value between 0.0 and 1.0 (inclusive).
      top_y: float value between 0.0 and 1.0 (inclusive).
      right_x: float value between 0.0 and 1.0 (inclusive).
      bottom_y: float value between 0.0 and 1.0 (inclusive).

    Raises:
      TypeError if the args are not of type 'float'.
      BadRequestError when there is something wrong with the given bounding box
        or if MAX_TRANSFORMS_PER_REQUEST transforms have already been requested
        for this image.
    """
    self._validate_crop_arg(left_x, "left_x")
    self._validate_crop_arg(top_y, "top_y")
    self._validate_crop_arg(right_x, "right_x")
    self._validate_crop_arg(bottom_y, "bottom_y")

    if left_x >= right_x:
      raise BadRequestError("left_x must be less than right_x")
    if top_y >= bottom_y:
      raise BadRequestError("top_y must be less than bottom_y")

    self._check_transform_limits()

    transform = images_service_pb.Transform()
    transform.set_crop_left_x(left_x)
    transform.set_crop_top_y(top_y)
    transform.set_crop_right_x(right_x)
    transform.set_crop_bottom_y(bottom_y)

    self._transforms.append(transform)

  def im_feeling_lucky(self):
    """Automatically adjust image contrast and color levels.

    This is similar to the "I'm Feeling Lucky" button in Picasa.

    Raises:
      BadRequestError if MAX_TRANSFORMS_PER_REQUEST transforms have already
        been requested for this image.
    """
    self._check_transform_limits()
    transform = images_service_pb.Transform()
    transform.set_autolevels(True)

    self._transforms.append(transform)

  def get_original_metadata(self):
    """Metadata of the original image.

    Returns a dictionary of metadata extracted from the original image during
    execute_transform.
    Note, that some of the EXIF fields are processed, e.g., fields with multiple
    values returned as lists, rational types are returned as floats, GPS
    coordinates already parsed to signed floats, etc.
    ImageWidth and ImageLength fields are corrected if they did not correspond
    to the actual dimensions of the original image.

    Returns:
      dict with string keys. If execute_transform was called with parse_metadata
      being True, this dictionary contains information about various properties
      of the original image, such as dimensions, color profile, and properties
      from EXIF.
      Even if parse_metadata was False or the images did not have any metadata,
      the dictionary will contain a limited set of metadata, at least
      'ImageWidth' and 'ImageLength', corresponding to the dimensions of the
      original image.
      It will return None, if it is called before a successful
      execute_transfrom.
    """
    return self._original_metadata

  def _set_imagedata(self, imagedata):
    """Fills in an ImageData PB from this Image instance.

    Args:
      imagedata: An ImageData PB instance
    """
    if self._blob_key:
      imagedata.set_content("")
      imagedata.set_blob_key(self._blob_key)
    else:
      imagedata.set_content(self._image_data)

  def execute_transforms(self, output_encoding=PNG, quality=None,
                         parse_source_metadata=False):
    """Perform transformations on a given image.

    Args:
      output_encoding: A value from OUTPUT_ENCODING_TYPES.
      quality: A value between 1 and 100 to specify the quality of the
        encoding.  This value is only used for JPEG & WEBP quality control.
      parse_source_metadata: when True the metadata (EXIF) of the source image
        is parsed before any transformations. The results can be retrieved
        via Image.get_original_metadata.

    Returns:
      str, image data after the transformations have been performed on it.

    Raises:
      BadRequestError when there is something wrong with the request
        specifications.
      NotImageError when the image data given is not an image.
      BadImageError when the image data given is corrupt.
      LargeImageError when the image data given is too large to process.
      InvalidBlobKeyError when the blob key provided is invalid.
      TransformtionError when something errors during image manipulation.
      Error when something unknown, but bad, happens.
    """
    if output_encoding not in OUTPUT_ENCODING_TYPES:
      raise BadRequestError("Output encoding type not in recognized set "
                            "%s" % OUTPUT_ENCODING_TYPES)

    if not self._transforms:
      raise BadRequestError("Must specify at least one transformation.")

    self.CheckValidIntParameter(quality, 1, 100, "quality")

    request = images_service_pb.ImagesTransformRequest()
    response = images_service_pb.ImagesTransformResponse()

    input_settings = request.mutable_input()
    input_settings.set_correct_exif_orientation(
        self._correct_orientation)

    if parse_source_metadata:
      input_settings.set_parse_metadata(True)

    self._set_imagedata(request.mutable_image())

    for transform in self._transforms:
      request.add_transform().CopyFrom(transform)

    request.mutable_output().set_mime_type(output_encoding)

    if ((output_encoding == JPEG or output_encoding == WEBP) and
        (quality is not None)):
      request.mutable_output().set_quality(quality)

    try:
      apiproxy_stub_map.MakeSyncCall("images",
                                     "Transform",
                                     request,
                                     response)
    except apiproxy_errors.ApplicationError, e:
      if (e.application_error ==
          images_service_pb.ImagesServiceError.BAD_TRANSFORM_DATA):
        raise BadRequestError()
      elif (e.application_error ==
            images_service_pb.ImagesServiceError.NOT_IMAGE):
        raise NotImageError()
      elif (e.application_error ==
            images_service_pb.ImagesServiceError.BAD_IMAGE_DATA):
        raise BadImageError()
      elif (e.application_error ==
            images_service_pb.ImagesServiceError.IMAGE_TOO_LARGE):
        raise LargeImageError()
      elif (e.application_error ==
            images_service_pb.ImagesServiceError.INVALID_BLOB_KEY):
        raise InvalidBlobKeyError()
      elif (e.application_error ==
            images_service_pb.ImagesServiceError.UNSPECIFIED_ERROR):
        raise TransformationError()
      else:
        raise Error()

    self._image_data = response.image().content()
    self._blob_key = None
    self._transforms = []
    self._width = None
    self._height = None
    self._format = None
    if response.source_metadata():
      self._original_metadata = json.loads(response.source_metadata())
    return self._image_data

  @property
  def width(self):
    """Gets the width of the image."""
    if self._width is None:
      self._update_dimensions()
    return self._width

  @property
  def height(self):
    """Gets the height of the image."""
    if self._height is None:
      self._update_dimensions()
    return self._height

  @property
  def format(self):
    """Gets the format of the image."""
    if self._format is None:
      self._update_dimensions()
    return self._format

  def histogram(self):
    """Calculates the histogram of the image.

    Returns: 3 256-element lists containing the number of occurences of each
    value of each color in the order RGB. As described at
    http://en.wikipedia.org/wiki/Color_histogram for N = 256. i.e. the first
    value of the first list contains the number of pixels with a red value of
    0, the second the number with a red value of 1.

    Raises:
      NotImageError when the image data given is not an image.
      BadImageError when the image data given is corrupt.
      LargeImageError when the image data given is too large to process.
      Error when something unknown, but bad, happens.
    """
    request = images_service_pb.ImagesHistogramRequest()
    response = images_service_pb.ImagesHistogramResponse()

    self._set_imagedata(request.mutable_image())

    try:
      apiproxy_stub_map.MakeSyncCall("images",
                                     "Histogram",
                                     request,
                                     response)
    except apiproxy_errors.ApplicationError, e:
      if (e.application_error ==
          images_service_pb.ImagesServiceError.NOT_IMAGE):
        raise NotImageError()
      elif (e.application_error ==
            images_service_pb.ImagesServiceError.BAD_IMAGE_DATA):
        raise BadImageError()
      elif (e.application_error ==
            images_service_pb.ImagesServiceError.IMAGE_TOO_LARGE):
        raise LargeImageError()
      elif (e.application_error ==
            images_service_pb.ImagesServiceError.INVALID_BLOB_KEY):
        raise InvalidBlobKeyError()
      else:
        raise Error()
    histogram = response.histogram()
    return [histogram.red_list(),
            histogram.green_list(),
            histogram.blue_list()]

  @staticmethod
  def CheckValidIntParameter(parameter, min_value, max_value, name):
    """Checks that a parameters is an integer within the specified range."""

    if parameter is not None:
      if not isinstance(parameter, (int, long)):
        raise TypeError("%s must be an integer." % name)
      if parameter > max_value or parameter < min_value:
        raise BadRequestError("%s must be between %s and %s."
                              % name, str(min_value), str(max_value))

def resize(image_data, width=0, height=0, output_encoding=PNG, quality=None,
           correct_orientation=UNCHANGED_ORIENTATION,
           crop_to_fit=False, crop_offset_x=0.5, crop_offset_y=0.5):
  """Resize a given image file maintaining the aspect ratio.

  If both width and height are specified, the more restricting of the two
  values will be used when resizing the image. The maximum dimension allowed
  for both width and height is 4000 pixels.
  If both width and height are specified and crop_to_fit is True, the less
  restricting of the two values will be used when resizing and the image will be
  cropped to fit the specified size. In this case the center of cropping can be
  adjusted  by crop_offset_x and crop_offset_y.

  Args:
    image_data: str, source image data.
    width: int, width (in pixels) to change the image width to.
    height: int, height (in pixels) to change the image height to.
    output_encoding: a value from OUTPUT_ENCODING_TYPES.
    quality: A value between 1 and 100 to specify the quality of the
      encoding.  This value is only used for JPEG quality control.
    correct_orientation: one of ORIENTATION_CORRECTION_TYPE, to indicate if
      orientation correction should be performed during the transformation.
    crop_to_fit: If True and both width and height are specified, the image is
      cropped after resize to fit the specified dimensions.
    crop_offset_x: float value between 0.0 and 1.0, 0 is left and 1 is right,
      default is 0.5, the center of image.
    crop_offset_y: float value between 0.0 and 1.0, 0 is top and 1 is bottom,
      default is 0.5, the center of image.

  Raises:
    TypeError when width or height not either 'int' or 'long' types.
    BadRequestError when there is something wrong with the given height or
      width.
    Error when something went wrong with the call.  See Image.ExecuteTransforms
      for more details.
  """
  image = Image(image_data)
  image.resize(width, height, crop_to_fit=crop_to_fit,
               crop_offset_x=crop_offset_x, crop_offset_y=crop_offset_y)
  image.set_correct_orientation(correct_orientation)
  return image.execute_transforms(output_encoding=output_encoding,
                                  quality=quality)

def rotate(image_data, degrees, output_encoding=PNG, quality=None,
           correct_orientation=UNCHANGED_ORIENTATION):
  """Rotate a given image a given number of degrees clockwise.

  Args:
    image_data: str, source image data.
    degrees: value from ROTATE_DEGREE_VALUES.
    output_encoding: a value from OUTPUT_ENCODING_TYPES.
    quality: A value between 1 and 100 to specify the quality of the
      encoding.  This value is only used for JPEG quality control.
    correct_orientation: one of ORIENTATION_CORRECTION_TYPE, to indicate if
      orientation correction should be performed during the transformation.

  Raises:
    TypeError when degrees is not either 'int' or 'long' types.
    BadRequestError when there is something wrong with the given degrees.
    Error when something went wrong with the call.  See Image.ExecuteTransforms
      for more details.
  """
  image = Image(image_data)
  image.rotate(degrees)
  image.set_correct_orientation(correct_orientation)
  return image.execute_transforms(output_encoding=output_encoding,
                                  quality=quality)

def horizontal_flip(image_data, output_encoding=PNG, quality=None,
                    correct_orientation=UNCHANGED_ORIENTATION):
  """Flip the image horizontally.

  Args:
    image_data: str, source image data.
    output_encoding: a value from OUTPUT_ENCODING_TYPES.
    quality: A value between 1 and 100 to specify the quality of the
      encoding.  This value is only used for JPEG quality control.
    correct_orientation: one of ORIENTATION_CORRECTION_TYPE, to indicate if
      orientation correction should be performed during the transformation.

  Raises:
    Error when something went wrong with the call.  See Image.ExecuteTransforms
      for more details.
  """
  image = Image(image_data)
  image.horizontal_flip()
  image.set_correct_orientation(correct_orientation)
  return image.execute_transforms(output_encoding=output_encoding,
                                  quality=quality)

def vertical_flip(image_data, output_encoding=PNG, quality=None,
                  correct_orientation=UNCHANGED_ORIENTATION):
  """Flip the image vertically.

  Args:
    image_data: str, source image data.
    output_encoding: a value from OUTPUT_ENCODING_TYPES.
    quality: A value between 1 and 100 to specify the quality of the
      encoding.  This value is only used for JPEG quality control.
    correct_orientation: one of ORIENTATION_CORRECTION_TYPE, to indicate if
      orientation correction should be performed during the transformation.

  Raises:
    Error when something went wrong with the call.  See Image.ExecuteTransforms
      for more details.
  """
  image = Image(image_data)
  image.vertical_flip()
  image.set_correct_orientation(correct_orientation)
  return image.execute_transforms(output_encoding=output_encoding,
                                  quality=quality)

def crop(image_data, left_x, top_y, right_x, bottom_y, output_encoding=PNG,
         quality=None, correct_orientation=UNCHANGED_ORIENTATION):
  """Crop the given image.

  The four arguments are the scaling numbers to describe the bounding box
  which will crop the image.  The upper left point of the bounding box will
  be at (left_x*image_width, top_y*image_height) the lower right point will
  be at (right_x*image_width, bottom_y*image_height).

  Args:
    image_data: str, source image data.
    left_x: float value between 0.0 and 1.0 (inclusive).
    top_y: float value between 0.0 and 1.0 (inclusive).
    right_x: float value between 0.0 and 1.0 (inclusive).
    bottom_y: float value between 0.0 and 1.0 (inclusive).
    output_encoding: a value from OUTPUT_ENCODING_TYPES.
    quality: A value between 1 and 100 to specify the quality of the
      encoding.  This value is only used for JPEG quality control.
    correct_orientation: one of ORIENTATION_CORRECTION_TYPE, to indicate if
      orientation correction should be performed during the transformation.

  Raises:
    TypeError if the args are not of type 'float'.
    BadRequestError when there is something wrong with the given bounding box.
    Error when something went wrong with the call.  See Image.ExecuteTransforms
      for more details.
  """
  image = Image(image_data)
  image.crop(left_x, top_y, right_x, bottom_y)
  image.set_correct_orientation(correct_orientation)
  return image.execute_transforms(output_encoding=output_encoding,
                                  quality=quality)

def im_feeling_lucky(image_data, output_encoding=PNG, quality=None,
                     correct_orientation=UNCHANGED_ORIENTATION):
  """Automatically adjust image levels.

  This is similar to the "I'm Feeling Lucky" button in Picasa.

  Args:
    image_data: str, source image data.
    output_encoding: a value from OUTPUT_ENCODING_TYPES.
    quality: A value between 1 and 100 to specify the quality of the
      encoding.  This value is only used for JPEG quality control.
    correct_orientation: one of ORIENTATION_CORRECTION_TYPE, to indicate if
      orientation correction should be performed during the transformation.

  Raises:
    Error when something went wrong with the call.  See Image.ExecuteTransforms
      for more details.
  """
  image = Image(image_data)
  image.im_feeling_lucky()
  image.set_correct_orientation(correct_orientation)
  return image.execute_transforms(output_encoding=output_encoding,
                                  quality=quality)

def composite(inputs, width, height, color=0, output_encoding=PNG, quality=None):
  """Composite one or more images onto a canvas.

  Args:
    inputs: a list of tuples (image_data, x_offset, y_offset, opacity, anchor)
    where
      image_data: str, source image data.
      x_offset: x offset in pixels from the anchor position
      y_offset: y offset in piyels from the anchor position
      opacity: opacity of the image specified as a float in range [0.0, 1.0]
      anchor: anchoring point from ANCHOR_POINTS. The anchor point of the image
      is aligned with the same anchor point of the canvas. e.g. TOP_RIGHT would
      place the top right corner of the image at the top right corner of the
      canvas then apply the x and y offsets.
    width: canvas width in pixels.
    height: canvas height in pixels.
    color: canvas background color encoded as a 32 bit unsigned int where each
      color channel is represented by one byte in order ARGB.
    output_encoding: a value from OUTPUT_ENCODING_TYPES.
    quality: A value between 1 and 100 to specify the quality of the
      encoding. This value is only used for JPEG quality control.

  Returns:
      str, image data of the composited image.

  Raises:
    TypeError If width, height, color, x_offset or y_offset are not of type
    int or long or if opacity is not a float
    BadRequestError If more than MAX_TRANSFORMS_PER_REQUEST compositions have
    been requested, if the canvas width or height is greater than 4000 or less
    than or equal to 0, if the color is invalid or if for any composition
    option, the opacity is outside the range [0,1] or the anchor is invalid.
  """
  if (not isinstance(width, (int, long)) or
      not isinstance(height, (int, long)) or
      not isinstance(color, (int, long))):
    raise TypeError("Width, height and color must be integers.")
  if output_encoding not in OUTPUT_ENCODING_TYPES:
    raise BadRequestError("Output encoding type '%s' not in recognized set "
                          "%s" % (output_encoding, OUTPUT_ENCODING_TYPES))

  if quality is not None:
    if not isinstance(quality, (int, long)):
      raise TypeError("Quality must be an integer.")
    if quality > 100 or quality < 1:
      raise BadRequestError("Quality must be between 1 and 100.")

  if not inputs:
    raise BadRequestError("Must provide at least one input")
  if len(inputs) > MAX_COMPOSITES_PER_REQUEST:
    raise BadRequestError("A maximum of %d composition operations can be"
                          "performed in a single request" %
                          MAX_COMPOSITES_PER_REQUEST)

  if width <= 0 or height <= 0:
    raise BadRequestError("Width and height must be > 0.")
  if width > 4000 or height > 4000:
    raise BadRequestError("Width and height must be <= 4000.")

  if color > 0xffffffff or color < 0:
    raise BadRequestError("Invalid color")

  if color >= 0x80000000:
    color -= 0x100000000

  image_map = {}

  request = images_service_pb.ImagesCompositeRequest()
  response = images_service_pb.ImagesTransformResponse()
  for (image, x, y, opacity, anchor) in inputs:
    if not image:
      raise BadRequestError("Each input must include an image")
    if (not isinstance(x, (int, long)) or
        not isinstance(y, (int, long)) or
        not isinstance(opacity, (float))):
      raise TypeError("x_offset, y_offset must be integers and opacity must"
                      "be a float")
    if x > 4000 or x < -4000:
      raise BadRequestError("xOffsets must be in range [-4000, 4000]")
    if y > 4000 or y < -4000:
      raise BadRequestError("yOffsets must be in range [-4000, 4000]")
    if opacity < 0 or opacity > 1:
      raise BadRequestError("Opacity must be in the range 0.0 to 1.0")
    if anchor not in ANCHOR_TYPES:
      raise BadRequestError("Anchor type '%s' not in recognized set %s" %
                            (anchor, ANCHOR_TYPES))
    if image not in image_map:
      image_map[image] = request.image_size()

      if isinstance(image, Image):
        image._set_imagedata(request.add_image())
      else:
        request.add_image().set_content(image)

    option = request.add_options()
    option.set_x_offset(x)
    option.set_y_offset(y)
    option.set_opacity(opacity)
    option.set_anchor(anchor)
    option.set_source_index(image_map[image])

  request.mutable_canvas().mutable_output().set_mime_type(output_encoding)
  request.mutable_canvas().set_width(width)
  request.mutable_canvas().set_height(height)
  request.mutable_canvas().set_color(color)

  if ((output_encoding == JPEG or output_encoding == WEBP) and
      (quality is not None)):
      request.mutable_canvas().mutable_output().set_quality(quality)

  try:
    apiproxy_stub_map.MakeSyncCall("images",
                                   "Composite",
                                   request,
                                   response)
  except apiproxy_errors.ApplicationError, e:
    if (e.application_error ==
        images_service_pb.ImagesServiceError.BAD_TRANSFORM_DATA):
      raise BadRequestError()
    elif (e.application_error ==
          images_service_pb.ImagesServiceError.NOT_IMAGE):
      raise NotImageError()
    elif (e.application_error ==
          images_service_pb.ImagesServiceError.BAD_IMAGE_DATA):
      raise BadImageError()
    elif (e.application_error ==
          images_service_pb.ImagesServiceError.IMAGE_TOO_LARGE):
      raise LargeImageError()
    elif (e.application_error ==
          images_service_pb.ImagesServiceError.INVALID_BLOB_KEY):
      raise InvalidBlobKeyError()
    elif (e.application_error ==
          images_service_pb.ImagesServiceError.UNSPECIFIED_ERROR):
      raise TransformationError()
    else:
      raise Error()

  return response.image().content()

def histogram(image_data):
  """Calculates the histogram of the given image.

  Args:
    image_data: str, source image data.
  Returns: 3 256-element lists containing the number of occurences of each
  value of each color in the order RGB.

  Raises:
    NotImageError when the image data given is not an image.
    BadImageError when the image data given is corrupt.
    LargeImageError when the image data given is too large to process.
    Error when something unknown, but bad, happens.
  """
  image = Image(image_data)
  return image.histogram()

IMG_SERVING_SIZES_LIMIT = 1600

IMG_SERVING_SIZES = [
    32, 48, 64, 72, 80, 90, 94, 104, 110, 120, 128, 144,
    150, 160, 200, 220, 288, 320, 400, 512, 576, 640, 720,
    800, 912, 1024, 1152, 1280, 1440, 1600]

IMG_SERVING_CROP_SIZES = [32, 48, 64, 72, 80, 104, 136, 144, 150, 160]

def get_serving_url(blob_key,
                    size=None,
                    crop=False):
  """Obtain a url that will serve the underlying image.

  This URL is served by a high-performance dynamic image serving infrastructure.
  This URL format also allows dynamic resizing and crop with certain
  restrictions. To get dynamic resizing and cropping, specify size and crop
  arguments, or simply append options to the end of the default url obtained via
  this call.  Here is an example:

  get_serving_url -> "http://lh3.ggpht.com/SomeCharactersGoesHere"

  To get a 32 pixel sized version (aspect-ratio preserved) simply append
  "=s32" to the url:

  "http://lh3.ggpht.com/SomeCharactersGoesHere=s32"

  To get a 32 pixel cropped version simply append "=s32-c":

  "http://lh3.ggpht.com/SomeCharactersGoesHere=s32-c"

  Available sizes are any interger in the range [0, 1600] and is available as
  IMG_SERVING_SIZES_LIMIT.

  Args:
    blob_key: BlobKey, BlobInfo, str, or unicode representation of BlobKey of
      blob to get URL of.
    size: int, size of resulting images
    crop: bool, True requests a cropped image, False a resized one.

  Returns:
    str, a url

  Raises:
    BlobKeyRequiredError: when no blobkey was specified in the ctor.
    UnsupportedSizeError: when size parameters uses unsupported sizes.
    BadRequestError: when crop/size are present in wrong combination.
  """
  if not blob_key:
    raise BlobKeyRequiredError("A Blobkey is required for this operation.")

  if crop and not size:
    raise BadRequestError("Size should be set for crop operation")

  if size and (size > IMG_SERVING_SIZES_LIMIT or size < 0):
    raise UnsupportedSizeError("Unsupported size")

  request = images_service_pb.ImagesGetUrlBaseRequest()
  response = images_service_pb.ImagesGetUrlBaseResponse()

  request.set_blob_key(_extract_blob_key(blob_key))

  try:
    apiproxy_stub_map.MakeSyncCall("images",
                                   "GetUrlBase",
                                   request,
                                   response)
  except apiproxy_errors.ApplicationError, e:
    if (e.application_error ==
        images_service_pb.ImagesServiceError.NOT_IMAGE):
      raise NotImageError()
    elif (e.application_error ==
          images_service_pb.ImagesServiceError.BAD_IMAGE_DATA):
      raise BadImageError()
    elif (e.application_error ==
          images_service_pb.ImagesServiceError.IMAGE_TOO_LARGE):
      raise LargeImageError()
    elif (e.application_error ==
          images_service_pb.ImagesServiceError.INVALID_BLOB_KEY):
      raise InvalidBlobKeyError()
    else:
      raise Error()
  url = response.url()

  if size:
    url += "=s%s" % size
  if crop:
    url += "-c"

  return url

def _extract_blob_key(blob):
  """Extract a unicode blob key from a str, BlobKey, or BlobInfo.

  Args:
    blob: The str, unicode, BlobKey, or BlobInfo that contains the blob key.
  """
  if isinstance(blob, str):
    return blob.decode('utf-8')
  elif isinstance(blob, BlobKey):
    return str(blob).decode('utf-8')
  elif blob.__class__.__name__ == 'BlobInfo':

    return str(blob.key()).decode('utf-8')

  return blob


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

	<groupId>io.github.joaomlneto</groupId>
	<artifactId>travis-ci-tutorial-java</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>travis-ci-tutorial-java</name>

	<dependencies>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>

	</dependencies>

	<build>
		<plugins>

			<plugin>
				<groupId>org.jacoco</groupId>
				<artifactId>jacoco-maven-plugin</artifactId>
				<version>0.7.7.201606060606</version>
				<executions>
					<execution>
						<goals>
							<goal>prepare-agent</goal>
						</goals>
					</execution>
					<execution>
						<id>report</id>
						<phase>test</phase>
						<goals>
							<goal>report</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
					<finalName>travis-ci-tutorial</finalName>
					<appendAssemblyId>false</appendAssemblyId>
					<archive>
						<manifest>
							<mainClass>io.github.joaomlneto.travis_ci_tutorial_java.SimpleCalculator</mainClass>
						</manifest>
					</archive>
				</configuration>
				<executions>
					<execution>
						<id>build-jar-with-dependencies</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

		</plugins>
	</build>
</project>


text:
Title: Spark-ROOT
Date: 2017-03-25 12:36
Slug: project_spark_root.html
Authors: Jim Pivarski
Summary: Access ROOT data in Apache Spark.

Spark-ROOT is a pure-Java (and Scala) implementation of ROOT file reading, which presents a collection of ROOT files as a DataFrame object in Spark. This allows high-energy physicists to use Spark without giving up on or reformatting all their data.

### Motivation

Spark is a useful platform for data analysis. It provides easy access to parallelization and is a focus point for data science tools from industry, particularly machine learning.

However, none of this would be useful to high-energy physicists if they cannot get their data into the platform. Spark-ROOT does this transparently.

### Technique

Spark-ROOT is not a JNI adaptation of ROOT or even a socket or pipe-based bridge. It is a reimplementation of ROOT file reading based on Tony Johnson's FreeHEP project. As a pure-Java project, it is trivial to install (see below) and doesn't suffer from segmentation faults in the JNI or the flimsiness of piping data to an external process.

ROOT's ability to selectively read columns is matched to Spark's "pruned scan" to automatically optimize reads.

ROOT files may be accessed locally or from HDFS.

### How to use

When you launch Spark, pass Spark-ROOT's Maven coordinates to load the package on startup.

    spark-shell --packages org.diana-hep:spark-root_2.11:0.1.7

Now you can

    import org.dianahep.sparkroot._
    val df = spark.sqlContext.read.root("path/to/file.root")

and use `df` as you would any other Spark DataFrame.

### For more information

See the GitHub site: [http://github.com/diana-hep/spark-root](http://github.com/diana-hep/spark-root).


text:
local ssl = require "ssl"
local url = require "socket.url"
local cjson = require "cjson"
local utils = require "kong.tools.utils"
local socket = require "socket"
local constants = require "kong.constants"
local spec_helper = require "spec.spec_helpers"
local http_client = require "kong.tools.http_client"

local STUB_GET_URL = spec_helper.STUB_GET_URL
local STUB_GET_SSL_URL = spec_helper.STUB_GET_SSL_URL
local PROXY_URL = spec_helper.PROXY_URL

-- Parses an SSL certificate returned by LuaSec
local function parse_cert(cert)
  local result = {}
  for _, v in ipairs(cert:issuer()) do
    result[v.name] = v.value
  end
  return result
end

describe("Resolver", function()

  setup(function()
    spec_helper.prepare_db()
    spec_helper.insert_fixtures {
      api = {
        {name = "tests-host-resolver-1", request_host = "mockbin.com", upstream_url = "http://mockbin.com"},
        {name = "tests-host-resolver-2", request_host = "mockbin-auth.com", upstream_url = "http://mockbin.com"},
        {name = "tests-request_path-resolver", upstream_url = "http://mockbin.com", request_path = "/status"},
        {name = "tests-stripped-request_path-resolver", upstream_url = "http://mockbin.com", request_path = "/mockbin", strip_request_path = true},
        {name = "tests-stripped-request_path-resolver-with-pattern-characters", upstream_url = "http://mockbin.com", request_path = "/mockbin-with-pattern/", strip_request_path = true},
        {name = "tests-deep-request_path-resolver", upstream_url = "http://mockbin.com", request_path = "/deep/request_path/", strip_request_path = true},
        {name = "tests-dup-request_path-resolver", upstream_url = "http://mockbin.com", request_path = "/har", strip_request_path = true},
        {name = "tests-wildcard-subdomain", upstream_url = "http://mockbin.com/status/200", request_host = "*.wildcard.com"},
        {name = "tests-wildcard-subdomain-2", upstream_url = "http://mockbin.com/status/201", request_host = "wildcard.*"},
        {name = "tests-preserve-host", request_host = "httpbin-nopreserve.com", upstream_url = "http://httpbin.org"},
        {name = "tests-preserve-host-2", request_host = "httpbin-preserve.com", upstream_url = "http://httpbin.org", preserve_host = true}
      },
      plugin = {
        {name = "key-auth", config = {key_names = {"apikey"} }, __api = 2}
      }
    }

    spec_helper.start_kong()
  end)

  teardown(function()
    spec_helper.stop_kong()
  end)

  describe("Inexistent API", function()

    it("should return Not Found when the API is not in Kong", function()
      local response, status = http_client.get(spec_helper.STUB_GET_URL, nil, {host = "foo.com"})
      assert.equal(404, status)
      assert.equal('{"request_path":"\\/request","message":"API not found with these values","request_host":["foo.com"]}\n', response)
    end)

  end)

  describe("SSL", function()

    it("should work when calling SSL port", function()
      local response, status = http_client.get(STUB_GET_SSL_URL, nil, {host = "mockbin.com"})
      assert.equal(200, status)
      assert.truthy(response)
      local parsed_response = cjson.decode(response)
      assert.same("GET", parsed_response.method)
    end)

    it("should work when manually triggering the handshake on default route", function()
      local parsed_url = url.parse(STUB_GET_SSL_URL)

      local conn = socket.tcp()
      local ok, err = conn:connect(parsed_url.host, parsed_url.port)
      assert.truthy(ok)
      assert.falsy(err)

      local params = {
        mode = "client",
        verify = "none",
        options = "all",
        protocol = "tlsv1"
      }

      -- TLS/SSL initialization
      conn = ssl.wrap(conn, params)
      local ok, err = conn:dohandshake()
      assert.falsy(err)
      assert.truthy(ok)

      local cert = parse_cert(conn:getpeercertificate())

      assert.same(6, utils.table_size(cert))
      assert.same("Kong", cert.organizationName)
      assert.same("IT Department", cert.organizationalUnitName)
      assert.same("US", cert.countryName)
      assert.same("California", cert.stateOrProvinceName)
      assert.same("San Francisco", cert.localityName)
      assert.same("localhost", cert.commonName)

      conn:close()
    end)

  end)

  describe("Existing API", function()
    describe("By Host", function()
      it("should proxy when the API is in Kong", function()
        local _, status = http_client.get(STUB_GET_URL, nil, {host = "mockbin.com"})
        assert.equal(200, status)
      end)

      it("should proxy when the Host header is not trimmed", function()
        local _, status = http_client.get(STUB_GET_URL, nil, {host = "   mockbin.com  "})
        assert.equal(200, status)
      end)

      it("should proxy when the request has no Host header but the X-Host-Override header", function()
        local _, status = http_client.get(STUB_GET_URL, nil, {["X-Host-Override"] = "mockbin.com"})
        assert.equal(200, status)
      end)

      it("should proxy when the Host header contains a port", function()
        local _, status = http_client.get(STUB_GET_URL, nil, {host = "mockbin.com:80"})
        assert.equal(200, status)
      end)

      describe("with wildcard subdomain", function()

        it("should proxy when the request_host is a wildcard subdomain", function()
          local _, status = http_client.get(STUB_GET_URL, nil, {host = "subdomain.wildcard.com"})
          assert.equal(200, status)

          _, status = http_client.get(STUB_GET_URL, nil, {host = "wildcard.org"})
          assert.equal(201, status)
        end)
      end)
    end)

    describe("By request_Path", function()
      it("should proxy when no Host is present but the request_uri matches the API's request_path", function()
        local _, status = http_client.get(spec_helper.PROXY_URL.."/status/200")
        assert.equal(200, status)

        local _, status = http_client.get(spec_helper.PROXY_URL.."/status/301")
        assert.equal(301, status)

        local _, status = http_client.get(spec_helper.PROXY_URL.."/mockbin")
        assert.equal(200, status)
      end)
      it("should not proxy when the request_path does not match the start of the request_uri", function()
        local response, status = http_client.get(spec_helper.PROXY_URL.."/somerequest_path/status/200")
        local body = cjson.decode(response)
        assert.equal("API not found with these values", body.message)
        assert.equal("/somerequest_path/status/200", body.request_path)
        assert.equal(404, status)
      end)
      it("should proxy and strip the request_path if `strip_request_path` is true", function()
        local response, status = http_client.get(spec_helper.PROXY_URL.."/mockbin/request")
        assert.equal(200, status)
        local body = cjson.decode(response)
        assert.equal("http://mockbin.com/request", body.url)
      end)
      it("should proxy and strip the request_path if `strip_request_path` is true if request_path has pattern characters", function()
        local response, status = http_client.get(spec_helper.PROXY_URL.."/mockbin-with-pattern/request")
        assert.equal(200, status)
        local body = cjson.decode(response)
        assert.equal("http://mockbin.com/request", body.url)
      end)
      it("should proxy when the request_path has a deep level", function()
        local _, status = http_client.get(spec_helper.PROXY_URL.."/deep/request_path/status/200")
        assert.equal(200, status)
      end)
      it("should not care about querystring parameters", function()
        local _, status = http_client.get(spec_helper.PROXY_URL.."/mockbin?foo=bar")
        assert.equal(200, status)
      end)
      it("should not strip if the `request_path` pattern is repeated in the request_uri", function()
        local response, status = http_client.get(spec_helper.PROXY_URL.."/har/har/of/request")
        assert.equal(200, status)
        local body = cjson.decode(response)
        local upstream_url = body.log.entries[1].request.url
        assert.equal("http://mockbin.com/har/of/request", upstream_url)
      end)
    end)

    it("should return the correct Server and Via headers when the request was proxied", function()
      local _, status, headers = http_client.get(STUB_GET_URL, nil, { host = "mockbin.com"})
      assert.equal(200, status)
      assert.equal("cloudflare-nginx", headers.server)
      assert.equal(constants.NAME.."/"..constants.VERSION, headers.via)
    end)
    it("should return the correct Server and no Via header when the request was NOT proxied", function()
      local _, status, headers = http_client.get(STUB_GET_URL, nil, { host = "mockbin-auth.com"})
      assert.equal(401, status)
      assert.equal(constants.NAME.."/"..constants.VERSION, headers.server)
      assert.falsy(headers.via)
    end)
  end)

  describe("Preseve Host", function()
    it("should not preserve the host (default behavior)", function()
      local response, status = http_client.get(PROXY_URL.."/get", nil, { host = "httpbin-nopreserve.com"})
      assert.equal(200, status)
      local parsed_response = cjson.decode(response)
      assert.equal("httpbin.org", parsed_response.headers["Host"])
    end)

    it("should preserve the host (default behavior)", function()
      local response, status = http_client.get(PROXY_URL.."/get", nil, { host = "httpbin-preserve.com"})
      assert.equal(200, status)
      local parsed_response = cjson.decode(response)
      assert.equal("httpbin-preserve.com", parsed_response.headers["Host"])
    end)
  end)
end)


text:
<?xml version="1.0" encoding="UTF-8"?>
<testlist title="PURL Storage Unit Tests">
  <desc>
    <div>
      This is the set of unit tests for the PURL Storage Library.
    </div>
  </desc>
  <group title="Storage Accessor Tests">
    <uri>ffcpl:/test/xunit/testlist-storage.xml</uri>
  </group>
</testlist>


text:
using Samson.MediaTypes;

namespace Samson.Services
{
    public interface IStrongMediaService
    {
        /// <summary>
        /// Gets the media item.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        IBasicMediaItem GetMediaItem(int id);

        /// <summary>
        /// Gets the media item.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        T GetMediaItem<T>(int id) where T : class, IBasicMediaItem;
    }
}


text:
import logging
import json
import time

from collections import defaultdict

from django.core.urlresolvers import reverse

from django.utils import html
from django.utils.translation import ugettext as _

import desktop.conf
from desktop.lib.django_util import JsonResponse
from desktop.lib.i18n import force_unicode
from desktop.models import Document, DocumentTag

LOG = logging.getLogger(__name__)

def _get_docs(user):
  history_tag = DocumentTag.objects.get_history_tag(user)

  query = Document.objects.get_docs(user) \
      .exclude(tags__in=[history_tag])

  # Work around Oracle not supporting SELECT DISTINCT with the CLOB type.
  if desktop.conf.DATABASE.ENGINE.get() == 'django.db.backends.oracle':
    query = query.only('id')
  else:
    query = query.defer(None)

  docs = query.order_by('-last_modified')[:100]

  if desktop.conf.DATABASE.ENGINE.get() == 'django.db.backends.oracle':
    ids = [doc.id for doc in docs]
    docs = Document.objects.filter(id__in=ids).defer(None)

  docs = docs \
      .select_related('owner', 'content_type') \
      .prefetch_related('tags', 'documentpermission_set')

  return docs

def massaged_tags_for_json(docs, user):
  """
    var TAGS_DEFAULTS = {
    'history': {'name': 'History', 'id': 1, 'docs': [1], 'type': 'history'},
    'trash': {'name': 'Trash', 'id': 3, 'docs': [2]},
    'mine': [{'name': 'default', 'id': 2, 'docs': [3]}, {'name': 'web', 'id': 3, 'docs': [3]}],
    'notmine': [{'name': 'example', 'id': 20, 'docs': [10]}, {'name': 'ex2', 'id': 30, 'docs': [10, 11]}]
  };
  """
  ts = {
    'trash': {},
    'history': {},
    'mine': [],
    'notmine': [],
  }
  sharers = defaultdict(list)

  trash_tag = DocumentTag.objects.get_trash_tag(user)
  history_tag = DocumentTag.objects.get_history_tag(user)

  tag_doc_mapping = defaultdict(set) # List of documents available in each tag
  for doc in docs:
    for tag in doc.tags.all():
      tag_doc_mapping[tag].add(doc)

  ts['trash'] = massaged_tags(trash_tag, tag_doc_mapping)
  ts['history'] = massaged_tags(history_tag, tag_doc_mapping)
  tags = list(set(tag_doc_mapping.keys() + [tag for tag in DocumentTag.objects.get_tags(user=user)])) # List of all personal and shared tags

  for tag in tags:
    massaged_tag = massaged_tags(tag, tag_doc_mapping)
    if tag == trash_tag:
      ts['trash'] = massaged_tag
    elif tag == history_tag:
      ts['history'] = massaged_tag
    elif tag.owner == user:
      ts['mine'].append(massaged_tag)
    else:
      sharers[tag.owner].append(massaged_tag)

  ts['notmine'] = [{'name': sharer.username, 'projects': projects} for sharer, projects in sharers.iteritems()]
  # Remove from my tags the trashed and history ones
  mine_filter = set(ts['trash']['docs'] + ts['history']['docs'])
  for tag in ts['mine']:
    tag['docs'] = [doc_id for doc_id in tag['docs'] if doc_id not in mine_filter]

  return ts

def massaged_tags(tag, tag_doc_mapping):
  return {
    'id': tag.id,
    'name': html.conditional_escape(tag.tag),
    'owner': tag.owner.username,
    'docs': [doc.id for doc in tag_doc_mapping[tag]] # Could get with one request groupy
  }

def massage_permissions(document):
  """
  Returns the permissions for a given document as a dictionary
  """
  read_perms = document.list_permissions(perm='read')
  write_perms = document.list_permissions(perm='write')
  return {
    'perms': {
        'read': {
          'users': [{'id': perm_user.id, 'username': perm_user.username} \
                     for perm_user in read_perms.users.all()],
          'groups': [{'id': perm_group.id, 'name': perm_group.name} \
                     for perm_group in read_perms.groups.all()]
        },
        'write': {
          'users': [{'id': perm_user.id, 'username': perm_user.username} \
                     for perm_user in write_perms.users.all()],
          'groups': [{'id': perm_group.id, 'name': perm_group.name} \
                     for perm_group in write_perms.groups.all()]
        }
      }
    }

def massaged_documents_for_json(documents, user):
  """
  var DOCUMENTS_DEFAULTS = {
    '1': {
      'id': 1,
      'name': 'my query history', 'description': '', 'url': '/beeswax/execute/design/83', 'icon': '/static/beeswax/art/icon_beeswax_24.png',
      'lastModified': '03/11/14 16:06:49', 'owner': 'admin', 'lastModifiedInMillis': 1394579209.0, 'isMine': true
    },
    '2': {
      'id': 2,
      'name': 'my query 2 trashed', 'description': '', 'url': '/beeswax/execute/design/83', 'icon': '/static/beeswax/art/icon_beeswax_24.png',
      'lastModified': '03/11/14 16:06:49', 'owner': 'admin', 'lastModifiedInMillis': 1394579209.0, 'isMine': true
     },
     '3': {
       'id': 3,
       'name': 'my query 3 tagged twice', 'description': '', 'url': '/beeswax/execute/design/83', 'icon': '/static/beeswax/art/icon_beeswax_24.png',
     'lastModified': '03/11/14 16:06:49', 'owner': 'admin', 'lastModifiedInMillis': 1394579209.0, 'isMine': true
     },
    '10': {
      'id': 10,
      'name': 'my query 3 shared', 'description': '', 'url': '/beeswax/execute/design/83', 'icon': '/static/beeswax/art/icon_beeswax_24.png',
      'lastModified': '03/11/14 16:06:49', 'owner': 'admin', 'lastModifiedInMillis': 1394579209.0, 'isMine': true
     },
    '11': {
      'id': 11,
      'name': 'my query 4 shared', 'description': '', 'url': '/beeswax/execute/design/83', 'icon': '/static/beeswax/art/icon_beeswax_24.png',
      'lastModified': '03/11/14 16:06:49', 'owner': 'admin', 'lastModifiedInMillis': 1394579209.0, 'isMine': true
     }
  };
  """
  docs = {}

  for document in documents:
    try:
      url = document.content_object.get_absolute_url()
    except:
      LOG.exception('failed to get absolute url')
      # If app of document is disabled
      url = ''
    docs[document.id] = massage_doc_for_json(document, user, url)

  return docs

def get_document(request):
  if request.method == 'POST':
    return Http404()
  elif request.method == 'GET':
    doc_id = request.GET['id']
    doc = Document.objects.get(id=doc_id)
    response = massage_doc_for_json(doc, request.user)
    return JsonResponse(response)

def massage_doc_for_json(document, user, url=''):
  read_perms = document.list_permissions(perm='read')
  write_perms = document.list_permissions(perm='write')
  massaged_doc = {
    'id': document.id,
    'contentType': html.conditional_escape(document.content_type.name),
    'icon': document.icon,
    'name': html.conditional_escape(document.name),
    'url': html.conditional_escape(url),
    'description': html.conditional_escape(document.description),
    'tags': [{'id': tag.id, 'name': html.conditional_escape(tag.tag)} \
             for tag in document.tags.all()],
    'owner': document.owner.username,
    'isMine': document.owner == user,
    'lastModified': document.last_modified.strftime("%x %X"),
    'lastModifiedInMillis': time.mktime(document.last_modified.timetuple())
  }

  permissions = massage_permissions(document)
  massaged_doc.update(permissions)
  return massaged_doc

def add_tag(request):
  response = {'status': -1, 'message': ''}

  if request.method == 'POST':
    try:
      tag = DocumentTag.objects.create_tag(request.user, request.POST['name'])
      response['name'] = request.POST['name']
      response['id'] = tag.id
      response['docs'] = []
      response['owner'] = request.user.username
      response['status'] = 0
    except Exception, e:
      response['message'] = force_unicode(e)
  else:
    response['message'] = _('POST request only')

  return JsonResponse(response)

def tag(request):
  response = {'status': -1, 'message': ''}

  if request.method == 'POST':
    request_json = json.loads(request.POST['data'])
    try:
      tag = DocumentTag.objects.tag(request.user, request_json['doc_id'], request_json.get('tag'), request_json.get('tag_id'))
      response['tag_id'] = tag.id
      response['status'] = 0
    except Exception, e:
      response['message'] = force_unicode(e)
  else:
    response['message'] = _('POST request only')

  return JsonResponse(response)

def update_tags(request):
  response = {'status': -1, 'message': ''}

  if request.method == 'POST':
    request_json = json.loads(request.POST['data'])
    try:
      doc = DocumentTag.objects.update_tags(request.user, request_json['doc_id'], request_json['tag_ids'])
      response['doc'] = massage_doc_for_json(doc, request.user)
      response['status'] = 0
    except Exception, e:
      response['message'] = force_unicode(e)
  else:
    response['message'] = _('POST request only')

  return JsonResponse(response)

def remove_tag(request):
  response = {'status': -1, 'message': _('Error')}

  if request.method == 'POST':
    try:
      DocumentTag.objects.delete_tag(request.POST['tag_id'], request.user)
      response['message'] = _('Project removed!')
      response['status'] = 0
    except Exception, e:
      response['message'] = force_unicode(e)
  else:
    response['message'] = _('POST request only')

  return JsonResponse(response)

def update_permissions(request):
  response = {'status': -1, 'message': _('Error')}

  if request.method == 'POST':
    data = json.loads(request.POST['data'])
    doc_id = request.POST['doc_id']
    try:
      doc = Document.objects.get_doc(doc_id, request.user)
      doc.sync_permissions(data)
      response['message'] = _('Permissions updated!')
      response['status'] = 0
      response['doc'] = massage_doc_for_json(doc, request.user)
    except Exception, e:
      LOG.exception(e.message)
      response['message'] = force_unicode(e)
  else:
    response['message'] = _('POST request only')

  return JsonResponse(response)


text:
module.exports = function (app) {
    var express = require('express');
    var jwt = require('jsonwebtoken');
    var router = express.Router();
    //   , users = require('../api/users');

    var Restaurant = require('../models/restaurants');

    app.use('/api/restaurants', router);

    router.post('/', function (req, res) {
        var name = req.body.name,
            rating = req.body.rating,
            picturePath = req.body.picturePath,
            menus = JSON.parse(req.body.menus),
            phoneNumbers = req.body.phoneNumbers;
        var rest = new Restaurant({ name: name, rating: rating, picturePath: picturePath, menus: menus, phoneNumbers: phoneNumbers });
        rest.save(function (err, data) {
            if (err) return console.error(err);
            res.json(data);
        });
    });

    router.get('/', function (req, res) {
        Restaurant.find({}, function (err, data) {
            if (err) return console.error(err);
            res.json(data);
        });
    });

    router.put('/', function (req, res) {

    });

    router.delete('/', function (req, res) {

    });

};


text:
var _ = require("lodash");

var worker = function(soldArticles) {
    return _.sortBy(soldArticles, function (article) {
        return -1 * article.quantity;
    });
};

// export the worker function as a nodejs module
module.exports = worker;