text:
package org.http4s.server.middleware

import java.time.{Clock, Instant, ZoneId}
import java.util.concurrent.atomic.AtomicLong

import cats.effect.IO
import org.http4s._
import org.http4s.dsl.io._
import org.http4s.headers.Referer
import org.http4s.util.CaseInsensitiveString
import CSRF.unlift
import cats.arrow.FunctionK

class CSRFSpec extends Http4sSpec {

  /** Create a clock that always ticks forward once per millis() call.
    *
    * This is to emulate scenarios where we want to mitigate BREACH where, in
    * a real world service, a huge number of requests wouldn't be processed
    * before the clock at least traverses a millisecond.
    */
  val testClock: Clock = new Clock { self =>
    private lazy val clockTick = new AtomicLong(Instant.now().toEpochMilli)

    def withZone(zone: ZoneId): Clock = this

    def getZone: ZoneId = ZoneId.systemDefault()

    def instant(): Instant =
      Instant.ofEpochMilli(clockTick.incrementAndGet())
  }

  val cookieName = "csrf-token"
  val headerName = CaseInsensitiveString("X-Csrf-Token")

  val defaultOriginCheck: Request[IO] => Boolean =
    CSRF.defaultOriginCheck[IO](_, "localhost", Uri.Scheme.http, None)

  val dummyRoutes: HttpApp[IO] = HttpRoutes
    .of[IO] {
      case GET -> Root =>
        Ok()
      case POST -> Root =>
        Ok()
    }
    .orNotFound

  val dummyRequest: Request[IO] =
    Request[IO](method = Method.POST).putHeaders(Header("Origin", "http://localhost"))

  val passThroughRequest: Request[IO] = Request[IO]()

  val csrf: CSRF[IO, IO] = CSRF
    .withGeneratedKey[IO, IO](defaultOriginCheck)
    .map(_.withClock(testClock).withCookieName(cookieName).build)
    .unsafeRunSync()

  val csrfForm: CSRF[IO, IO] = CSRF
    .withGeneratedKey[IO, IO](defaultOriginCheck)
    .map(
      _.withClock(testClock)
        .withCookieName(cookieName)
        .withCSRFCheck(CSRF.checkCSRFinHeaderAndForm[IO, IO](headerName.value, FunctionK.id))
        .build)
    .unsafeRunSync()

  ///

  "CSRF" should {
    "pass through and embed a new token for a safe, fresh request if set" in {
      val response = csrf.validate()(dummyRoutes)(passThroughRequest).unsafeRunSync()

      response.status must_== Status.Ok
      response.cookies.exists(_.name == cookieName) must_== true
    }

    "pass through and not embed a new token for a safe, fresh request" in {
      val csrfNoEmbed: CSRF[IO, IO] = CSRF
        .withGeneratedKey[IO, IO](defaultOriginCheck)
        .map(_.withClock(testClock).withCookieName(cookieName).withCreateIfNotFound(false).build)
        .unsafeRunSync()

      val response = csrfNoEmbed.validate()(dummyRoutes)(passThroughRequest).unsafeRunSync()

      response.status must_== Status.Ok
      response.cookies.exists(_.name == cookieName) must_== false
    }

    "Extract a valid token, with a slightly changed nonce, if present" in {
      val program = for {
        t <- csrf.generateToken[IO]
        req = csrf.embedInRequestCookie(dummyRequest, t)
        newToken <- csrf.refreshedToken[IO](req).valueOrF(IO.raiseError)
      } yield newToken

      val newToken = program.unsafeRunSync()
      //Checks whether it was properly signed
      csrf.extractRaw(unlift(newToken)).isRight must_== true
    }

    "Extract a valid token from header or form field when form enabled" in {

      def check(f: (String, Request[IO]) => Request[IO]): IO[Response[IO]] =
        for {
          token <- csrfForm.generateToken[IO]
          ts = unlift(token)
          req = csrfForm.embedInRequestCookie(f(ts, dummyRequest), token)
          res <- csrfForm.checkCSRF(req, dummyRoutes.run(req))
        } yield res

      val hn = headerName.value

      val fromHeader = check((ts, r) => r.putHeaders(Header(hn, ts)))
      val fromForm = check((ts, r) => r.withEntity(UrlForm(hn -> ts)))
      val preferHeader =
        check((ts, r) => r.withEntity(UrlForm(hn -> "bogus")).putHeaders(Header(hn, ts)))

      fromHeader.unsafeRunSync().status must_== Status.Ok
      fromForm.unsafeRunSync().status must_== Status.Ok
      preferHeader.unsafeRunSync().status must_== Status.Ok

    }

    "pass a request with valid origin a request without any origin, even with a token" in {
      val program: IO[Response[IO]] = for {
        token <- csrf.generateToken[IO]
        req = csrf.embedInRequestCookie(Request[IO](POST), token)
        v <- csrf.checkCSRF(req, dummyRoutes.run(req))
      } yield v

      program.unsafeRunSync().status must_== Status.Forbidden
    }

    "fail a request with an invalid cookie, despite it being a safe method" in {
      val response =
        csrf
          .validate()(dummyRoutes)(passThroughRequest.addCookie(RequestCookie(cookieName, "MOOSE")))
          .unsafeRunSync()

      response.status must_== Status.Forbidden // Must fail
      !response.cookies.exists(_.name == cookieName) must_== true //Must not embed a new token
    }

    "pass through and embed a slightly different token for a safe request" in {
      val (oldToken, oldRaw, response, newToken, newRaw) =
        (for {
          oldToken <- csrf.generateToken[IO]
          raw1 <- IO.fromEither(csrf.extractRaw(unlift(oldToken)))
          response <- csrf.validate()(dummyRoutes)(
            csrf.embedInRequestCookie(passThroughRequest, oldToken))
          newCookie = response.cookies
            .find(_.name == cookieName)
            .getOrElse(ResponseCookie("invalid", "Invalid2"))
          raw2 <- IO.fromEither(csrf.extractRaw(newCookie.content))
        } yield (oldToken, raw1, response, newCookie, raw2)).unsafeRunSync()

      response.status must_== Status.Ok
      oldToken must_!= newToken.content
      oldRaw must_== newRaw
    }

    "not validate different tokens" in {
      val program: IO[Boolean] = for {
        t1 <- csrf.generateToken[IO]
        t2 <- csrf.generateToken[IO]
      } yield CSRF.tokensEqual(t1, t2)

      program.unsafeRunSync() must_== false
    }

    "validate for the correct csrf token and origin" in {
      val program: IO[Response[IO]] = for {
        token <- csrf.generateToken[IO]
        res <- csrf.validate()(dummyRoutes)(
          dummyRequest
            .putHeaders(Header(headerName.value, unlift(token)))
            .addCookie(cookieName, unlift(token))
        )
      } yield res

      program.unsafeRunSync().status must_== Status.Ok
    }

    "validate for the correct csrf token, no origin but with a referrer" in {
      val program: IO[Response[IO]] =
        for {
          token <- csrf.generateToken[IO]
          res <- csrf.validate()(dummyRoutes)(
            csrf.embedInRequestCookie(
              Request[IO](POST)
                .putHeaders(
                  Header(headerName.value, unlift(token)),
                  Referer(Uri.unsafeFromString("http://localhost/lol"))),
              token)
          )
        } yield res

      program.unsafeRunSync().status must_== Status.Ok
    }

    "fail a request without any origin, even with a token" in {
      val program: IO[Response[IO]] = for {
        token <- csrf.generateToken[IO]
        req = csrf.embedInRequestCookie(Request[IO](POST), token)
        v <- csrf.checkCSRF(req, dummyRoutes.run(req))
      } yield v

      program.unsafeRunSync().status must_== Status.Forbidden
    }

    "fail a request with an incorrect origin and incorrect referrer, even with a token" in {
      (for {
        token <- csrf.generateToken[IO]
        res <- csrf.validate()(dummyRoutes)(
          Request[IO](POST)
            .putHeaders(
              Header(headerName.value, unlift(token)),
              Header("Origin", "http://example.com"),
              Referer(Uri.unsafeFromString("http://example.com/lol")))
            .addCookie(cookieName, unlift(token))
        )
      } yield res).unsafeRunSync().status must_== Status.Forbidden
    }

    "not validate if token is missing in both" in {
      csrf
        .validate()(dummyRoutes)(dummyRequest)
        .unsafeRunSync()
        .status must_== Status.Forbidden
    }

    "not validate for token missing in header" in {
      (for {
        token <- csrf.generateToken[IO]
        res <- csrf.validate()(dummyRoutes)(
          dummyRequest.addCookie(cookieName, unlift(token))
        )
      } yield res).unsafeRunSync().status must_== Status.Forbidden
    }

    "not validate for token missing in cookie" in {
      (for {
        token <- csrf.generateToken[IO]
        res <- csrf.validate()(dummyRoutes)(
          dummyRequest.putHeaders(Header(headerName.value, unlift(token)))
        )
      } yield res).unsafeRunSync().status must_== Status.Forbidden
    }

    "not validate for different tokens" in {
      (for {
        token1 <- csrf.generateToken[IO]
        token2 <- csrf.generateToken[IO]
        res <- csrf.validate()(dummyRoutes)(
          dummyRequest
            .withHeaders(Headers.of(Header(headerName.value, unlift(token1))))
            .addCookie(cookieName, unlift(token2))
        )
      } yield res).unsafeRunSync().status must_== Status.Forbidden
    }

    "not return the same token to mitigate BREACH" in {
      val program: IO[Boolean] =
        for {
          token <- csrf.generateToken[IO]
          raw1 <- IO.fromEither(csrf.extractRaw(unlift(token)))
          res <- csrf.validate()(dummyRoutes)(
            dummyRequest
              .putHeaders(Header(headerName.value, unlift(token)))
              .addCookie(cookieName, unlift(token))
          )
          rawContent = res.cookies.find(_.name == cookieName).map(_.content).getOrElse("")
          raw2 <- IO.fromEither(csrf.extractRaw(rawContent))
        } yield rawContent != token && raw1 == raw2

      program.unsafeRunSync() must_=== true
    }

    "not return a token for a failed CSRF check" in {
      val response = (for {
        token1 <- csrf.generateToken[IO]
        token2 <- csrf.generateToken[IO]
        res <- csrf.validate()(dummyRoutes)(
          dummyRequest
            .putHeaders(Header(headerName.value, unlift(token1)))
            .addCookie(cookieName, unlift(token2))
        )
      } yield res).unsafeRunSync()

      response.status must_== Status.Forbidden
      !response.cookies.exists(_.name == cookieName) must_== true
    }
  }

}


text:
package jflowmap.views.flowstrates;

import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import jflowmap.FlowEndpoint;
import jflowmap.FlowMapGraph;
import prefuse.data.Edge;
import edu.umd.cs.piccolo.PCamera;
import edu.umd.cs.piccolo.PLayer;
import edu.umd.cs.piccolo.util.PBounds;

/**
 * @author Ilya Boyandin
 */
public abstract class TemporalViewLayer extends PLayer implements ViewLayer {

  static final NumberFormat TOOLTIP_NUMBER_FORMAT = DecimalFormat.getNumberInstance();

  private final FlowstratesView flowstratesView;
  private final PCamera camera;

  public TemporalViewLayer(FlowstratesView flowstratesView) {
    this.flowstratesView = flowstratesView;
    camera = new PCamera();
    camera.addLayer(this);
  }

  public FlowstratesView getFlowstratesView() {
    return flowstratesView;
  }

  public FlowMapGraph getFlowMapGraph() {
    return getFlowstratesView().getFlowMapGraph();
  }

  public PCamera getCamera() {
    return camera;
  }

  public abstract void renew();

  public abstract void updateColors();

  public abstract void resetWeightAttrTotals();

  public abstract Dimension2D getEdgeLabelBounds(Edge edge, FlowEndpoint ep);

  public abstract Point2D getFlowLineInPoint(int row, FlowEndpoint ep);

  public PBounds getActualViewBounds() {
    return getCamera().getViewBounds();
  }

}

redpajama_set_name: RedPajamaGithub


text:
#import "ANHRControlPointCharacteristic.h"

@implementation ANHRControlPointCharacteristic

+ (CBUUID *)UUID {
    return [CBUUID UUIDWithString:kHeartRateControlPointCharacteristicUUIDString];
}

- (void)resetEnergyExpanded {

    unsigned char value = 0x01;
    [self.characteristic.service.peripheral writeValue:[NSData dataWithBytes:&value length:1] forCharacteristic:self.characteristic type:CBCharacteristicWriteWithResponse];
}

- (NSString *)description {
    return [NSString stringWithFormat:@"Heart Rate Control Point Characteristic: %@", self.characteristic];
}
@end


text:
Currently, there are two formats avialable for defining dashboards:

## JSX
You can define a dashboard using familiar JSX syntax. Following is a complete example of a simple dashboard defined in JSX:

`npm run jsx-example` will load the following dashboard:

```eval_rst
.. literalinclude:: ../../examples/jsx_static_example/app.js
   :language: javascript
   :lines: 10-
```

## Javascript Settings object
In many cases it is useful to define dashboard configuration in JSON - allowing for database storage, programmatic creation, etc. Here is the above Dashboard defined as a Javascript object.

`npm run js-example` will load the following dashboard configuration at `localhost:3000`:

```eval_rst
.. literalinclude:: ../../examples/js_static_example/app.js
   :language: javascript
```

## Complete Dashboard Application
The app at `/examples/app.js` runs an example of a more robust application with datahandling, filtering, etc.

`npm run start` will from the react-dash repository will run the application.

## Boilerplate project
The boilerplate module includes the above application in a complete development environment suitable for doing dashbaord development.

    git clone https://github.com/NuCivic/react-dash-boilerplate.git
    cd react-dash-boilerplate
    npm install
    npm run init
    npm run start

This will spin up the example application, and you can use it as a starting point for developing your own app.


text:
All the commands in this guide require both the Azure CLI and `acs-engine`. Follow the [installation instructions to download acs-engine before continuing](../acsengine.md#install-acs-engine) or [compile from source](../acsengine.md#build-from-source).

For installation instructions see [the Azure CLI GitHub repository](https://github.com/Azure/azure-cli#installation) for the latest release.

This guide assumes you already have deployed a cluster using acs engine. For more details on how to do that see [deploy](./deploy.md).

## Scale
After a cluster has been deployed using acs engine the cluster can be interacted further by using the scale command. The scale command can add more nodes to an existing node pool or remove them. Nodes will always be added or removed from the end of the agent pool. Nodes will be cordoned and drained before deletion.

This guide will assume you have a cluster deployed and the output for the deployed cluster is stored at _output/mycluster. It will also assume there is a node pool named "agentpool1" in your cluster. ACS engine will default to storing the output at ./_output/dns-prefix from where the acs-engine command was ran.

To scale the cluster you will run a command like:

```
$ acs-engine scale --subscription-id 51ac25de-afdg-9201-d923-8d8e8e8e8e8e \
    --resource-group mycluster  --location westus2 \
    --deployment-dir _output/mycluster --new-node-count 5 \
    --node-pool agentpool1 --master-FQDN mycluster.westus2.cloudapp.azure.com
```

This command will look the the deployment directory to find info about the cluster currently deployed. Then it will generate and deploy a template deployment to update the cluster and add the new nodes. When it is done it will update the cluster definition in the deployment directory's apimodel.json to reflect the new node count.

### Parameters
|Parameter|Required|Description|
|---|---|---|
|subscription-id|yes|The subscription id the cluster is deployed in.|
|resource-group|yes|The resource group the cluster is deployed in.|
|location|yes|The location the resource group is in.|
|deployment-dir|yes|Relative path to the folder location for the output from the acs-engine deploy/generate command.|
|node-pool|depends|Required if there is more than one node pool. Which node pool should be scaled.|
|new-node-count|yes|Desired number of nodes in the node pool.|
|master-FQDN|depends|When scaling down a kuberentes cluster this is required. The master FDQN so that the nodes can be cordoned and drained before removal. This should be output as part of the create template or it can be found by looking at the public ip addresses in the resource group.|


text:
**Lucene Server** is an *Erlang* application that let's you manage and query documents using an **in-memory** [Lucene](http://lucene.apache.org) backend

## Usage
To start the application just run ``lucene_server:start().``

## Contact Us
For **questions** or **general comments** regarding the use of this library, please use our public
[hipchat room](https://www.hipchat.com/gpBpW3SsT).

If you find any **bugs** or have a **problem** while using this library, please [open an issue](https://github.com/inaka/lucene_server/issues/new) in this repo (or a pull request :)).

And you can check all of our open-source projects at [inaka.github.io](http://inaka.github.io)

## Adding/Deleting documents
To add documents use: ``lucene:add(Docs).`` where ``Docs :: [lucene:doc()]``
Each document is a ``proplist`` where the keys can be atoms, strings or binaries and the values can be numbers, atoms, strings or ``lucene:geo()``.
To delete documents use: ``lucene:del(Query).`` where ``Query`` is written respecting the [Lucene Query Syntax](http://lucene.apache.org/core/3_6_0/queryparsersyntax.html).

## Querying
To find documents according to a query use: ``lucene:match(Query, PageSize).``, ``lucene:match(Query, PageSize, SortFields)`` or ``lucene:match(Query, PageSize, SortFields, Timeout).`` where:
* ``Query`` is written respecting the [Lucene Query Syntax](http://lucene.apache.org/core/3_6_0/queryparsersyntax.html)
* ``PageSize`` is the number of results per page you expect.
* ``SortFields`` is a list of atoms that will determine the result sort order for equally scored results
* ``Timeout`` is the number of milliseconds to wait for a return. If no ``Timeout`` is specified, it defaults to 5000. Not to have a timeout, you should use the atom ``infinity``.
Both functions may return:
* the atom ``timeout`` if it took more than ``Timeout`` milliseconds to find the desired docs
* the first page of results together with metadata as described below

### Results
A results page looks like ``{Docs::[lucene:doc()], Data::lucene:metadata()}`` where:
* ``Docs`` is a list of no more than ``PageSize`` documents that match the ``Query``
* ``Data`` is a ``proplist`` that include the following fields:
  * ``next_page``: The token used to retrieve the following page (see below), if present
  * ``total_hits``: How many documents match the query across all pages
  * ``first_hit``: Which is the position of the first returned doc in the whole set of docs that match the query (e.g. if ``PageSize == 5``, for the first page ``first_hit == 1``; for the page #2, ``first_hit == 6``; etc.)

### Paging
To get the following page use: ``lucene:continue(PageToken, PageSize).`` or ``lucene:continue(PageToken, PageSize, Timeout).`` where ``PageToken`` comes from the metadata of the previous page and the rest of the parameters and results have the same types, format and meaning as in ``lucene:match/2`` or ``lucene:match/4`` functions.

## Special Data Types
Besides what Lucene already offers, *Lucene Server* provides support for indexing and querying some extra data types:

### Atoms
Atoms are treated as strings: You may add them as values in a document and query them using standard _Lucene Query Syntax_ for strings

### Numbers
*Lucene Server* lets you store integers and floats and then use them in range queries (i.e. ``<Field>:[<Min> TO <Max>]``) properly, respecting the field's data type instead of treating them as strings as Lucene does by default.

### Geo
*Lucene Server* provides support for managing and querying geo-spatial coordinates (i.e. latitude and longitude pairs).
* To construct a ``lucene:geo()`` object, use: ``lucene_utils:geo(Lat, Lng)`` where ``Lat`` and ``Lng`` are floating point numbers
* You can then use it as a value on a ``lucene:doc()``
* To find documents near a certain point, include the following term in your query: ``<Field>.near:<Lat>,<Lng>,<Miles>``. That query will filter documents within a ``<Miles>`` radio of ``<Lat>,<Lng>`` and also will rank results according to that distances (with closer docs ranking higher).

## Special Queries
### Calling an Erlang function
In the same way you can write ".near" queries, you can also write ".erlang" ones. The syntax is ``<Field>.erlang:<Module>:<Function>[:<Args>]``.
The function ``Module:Function`` is expected to comply with the following spec:
* If no args are provided: ``-spec Mod:Fun([term()]) -> [false | float()].``
* If Args are provided (and they should be written as a list): ``-spec Mod:Fun(type_of_arg1(), type_of_arg2(),... [term()]) -> [false | float()].``

The function will be called with the list of values for field ``Field`` and it is expected to return a list of results with the same length of the one received. For each element in the original list, the function may return (in the same place of the new list):
* ``false`` if it's not a match
* a ``float()`` representing the score of such a document


text:
#!/bin/bash
set -xv
#adam-example# ./do_coadd_batch.sh MACS1115+01 W-J-B 'all exposure ' 'none' OCFI 2>&1 | tee -a OUT-coadd_W-J-B.log2
#adam-example# ./do_coadd_batch.sh ${cluster} ${filter} 'good ' 'none' 'OCFR' 'yes' 'yes' 2>&1 | tee -a OUT-coadd_${cluster}.${filter}.log

### new coaddition script
###
### the astro-/photometry can be done via SCAMP (preferred) or
### ASTROMETRIX
###
### $Id: do_coadd_batch.sh,v 1.26 2011-03-29 22:50:58 anja Exp $

#adam: you have to deactivate the astroconda environment for some reason
source deactivate astroconda

. progs.ini > /tmp/prog.out 2>&1
. bash_functions.include > /tmp/bash_functions.include.out 2>&1

##################################

export cluster=$1
export filter=$2
# possible coaddition modes:
# - "all" : all images, needs to be run first!
# - "good" : only chips with not too elliptical PSF
# - "exposure" : one coadd per exposure
# - "3s" : coadds the SDSS 3s exposure
# - "gabodsid" : split by gabodsid
# - "rotation" : split by rotation and gabodsid
# - "pretty" : coadds the deep and 3s cluster exposure
requested_coadds=$3

## of the form: "good:((fjgj=gjg)AND(hghg=fjfjf)) all:(fjf=ssss)"
custom_conditions=$4   #optional, NEEDS TO BE A FILE!!!
use_ending=$5   #optional
keep_cats=$6    #optional  -> may be "yes"
keep_subs=$7    #optional  -> may be "yes"

###########################

if [ -n "${custom_conditions}" ]; then
    custom_conditions=`cat ${custom_conditions}`
fi

constructed_condition=
function constructConditions {
    coadd_type=$1
    standing_condition=$2   #always apply
    default_condition=$3    #apply if no custom conditions

    #look for custom conditions
    custom_condition=''
    for entry in ${custom_conditions}; do
        custom_condition=`echo ${entry} | awk -F':' '( $1 ~ /'${coadd_type}'/){print $2}'`
        if [ -n "${custom_condition}" ]; then
            break
        fi
    done

    if [ -z "${custom_condition}" ]; then
        custom_condition=${default_condition}
    fi

    if [ -z "${standing_condition}" ] && [ -z "${custom_condition}" ]; then
        constructed_condition=""
        return
    fi

    if [ -n "${standing_condition}" ] && [ -z "${custom_condition}" ]; then
        constructed_condition="${standing_condition};"
        return
    fi

    if [ -z "${standing_condition}" ] && [ -n "${custom_condition}" ]; then
        constructed_condition="${custom_condition};"
        return
    fi

    constructed_condition="(${standing_condition}AND${custom_condition});"
    return

}

######################################

export BONN_LOG=1
export BONN_TARGET=${cluster}
export BONN_FILTER=${filter}
#./BonnLogger.py clear

########################################

REDDIR=`pwd`
HEADDIR="${SUBARUDIR}/coadd_headers/"
#export SUBARUDIR=/gpfs/slac/kipac/fs1/u/awright/SUBARU

##########################################

NAXIS1=10000
NAXIS2=10000
IMAGESIZE="${NAXIS1},${NAXIS2}"
export PIXSCALEOUT=0.2

################################

ASTROMMETHOD=SCAMP
ASTROMETRYCAT=`cat ${SUBARUDIR}/Scamp_cat.list | grep "${cluster}" | awk '{if(cl==$1) print $2}' cl=${cluster}`
if [ -z "$ASTROMETRYCAT" ]; then
    #./BonnLogger.py comment "Can't find astrometry cat"
    exit 1
fi
#adam-tried# ASTROMADD="_scamp_${ASTROMETRYCAT}"
ASTROMADD="_scamp_photom_${ASTROMETRYCAT}"

#####################################

echo ${requested_coadds}
lookupfile=/gpfs/slac/kipac/fs1/u/awright/SUBARU/SUBARU.list
#adam-old# lookupfile=/nfs/slac/g/ki/ki05/anja/SUBARU/SUBARU.list
ra=`grep ${cluster} ${lookupfile} | awk '{if(cl==$1)print $3}' cl=${cluster}`
dec=`grep ${cluster} ${lookupfile} | awk '{if(cl==$1)print $4}' cl=${cluster}`
echo ${cluster} ${ra} ${dec}

#######################################

./setup_general.sh ${SUBARUDIR}/${cluster}/${filter}/SCIENCE instrument_$$
export INSTRUMENT=`cat instrument_$$`
if [[ ( -z ${INSTRUMENT} ) || ( "${INSTRUMENT}" = "UNKNOWN" ) ]]; then
    #adam# echo "setup_general failed. Probably due to BonnLogger"
    #adam# SET DEFAULT INSTRUMENT=SUBARU
    exit 5

fi
rm -f instrument_$$
. ${INSTRUMENT:?}.ini > /tmp/subaru.out 2>&1

if [ "${INSTRUMENT}" == "SUBARU" ]; then
  #old SUBARU.ini# CONFIG=`grep 'config=' ${INSTRUMENT}.ini | sed 's/config=//g'`
  CONFIG=`grep 'config=' ${INSTRUMENT}.ini | sed 's/export\ config=//g'`
  echo "CONFIG=" $CONFIG
fi

########./BonnLogger.py config \
########    cluster=${cluster} \
########    filter=${filter} \
########    config=${config} \
########    ending=${ending} \
########    astrommethod=${ASTROMMETHOD} \
########    astrometrycat=${ASTROMETRYCAT} \

######################
#   Find Ending
######################
  case ${filter} in
      "I" )
          ending=mos
          ;;
      "u" | "g" | "r" | "i" | "z" | "r_CALIB" )
          if [ -f ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/*_2*.fits ]; then
            testfile=`dfits ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/*_2*.fits | fitsort -d BADCCD | awk '{if($2!=1 && $1!~"sub.fits") print $0}' | awk '{if(NR==1) print $1}'`
          else
            testfile=`dfits ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/*_1*.fits | fitsort -d BADCCD | awk '{if($2!=1 && $1!~"sub.fits") print $0}' | awk '{if(NR==1) print $1}'`
          fi
          ending=C
          ;;
      "K" )
          ending=K
          ;;
      * )
          testfilename=`dfits ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/*_2*.fits | fitsort -d BADCCD | awk '{if($2!=1 && $1!~"sub.fits") print $0}' | awk '{if(NR==1) print $1}'`
          testfile=${SUBARUDIR}/${cluster}/${filter}/SCIENCE/${testfilename}
          echo ${testfile}
          base=`basename ${testfile} .fits`
          if [ -f $SUBARUDIR/$cluster/${filter}/SCIENCE/${base}I.fits ]; then
              testfile=$SUBARUDIR/$cluster/${filter}/SCIENCE/${base}I.fits
          fi
          if [ -f $SUBARUDIR/$cluster/${filter}/SCIENCE/${base}RI.fits ]; then
              testfile=$SUBARUDIR/$cluster/${filter}/SCIENCE/${base}RI.fits
          fi
          ending=`basename ${testfile} | awk -F'_2' '{print $2}' | awk -F'.' '{print $1}'`
          ;;
  esac

lastchar=`echo $ending | awk '{print substr($1,length)}'`
if [ "$lastchar" = "I" ]; then
    origEnding=`echo $ending | awk '{print substr($1,1,length-1)}'`
    for f in `ls $SUBARUDIR/$cluster/$filter/WEIGHTS/*${origEnding}.flag.fits`;
    do
        base=`basename $f ${origEnding}.flag.fits`
        ln -s $f $SUBARUDIR/$cluster/$filter/WEIGHTS/${base}${ending}.flag.fits
    done
fi

if [ -n "${use_ending}" ]; then
    ending=${use_ending}
fi

ZP=`dfits ${testfile} | fitsort -d ZP | awk '{print $2}'`
if [ "${ZP}" != "KEY_N/A" ] && [ "${INSTRUMENT}" == "MEGAPRIME" ]; then
        MAGZP=${ZP}
else
        MAGZP="-1.0"
fi

#####################

for coadd in ${requested_coadds}
do
        if [ ${coadd} == "all" ] && [ ${filter} != "K" ] && [ ${filter} != "I" ]; then
          #####################
          ./test_coadd_ready.sh ${SUBARUDIR}/${cluster} ${filter} ${ending} ${ASTROMETRYCAT} ${ASTROMADD}
          if [ $? -gt 0 ]; then
              echo "adam-Error: test_coadd_ready failed"
              exit 1
          fi
          ######################
          calib=`awk 'BEGIN{if("'${filter}'"~"CALIB") print "1"; else print "0"}'`
          if [ ${calib} -eq 0 ]; then
              constructConditions all "" "((((RA>(${ra}-0.5))AND(RA<(${ra}+0.5)))AND((DEC>(${dec}-0.5))AND(DEC<(${dec}+0.5))))AND(SEEING<1.9))"
          else
              constructConditions all "" "(((RA>(${ra}-0.5))AND(RA<(${ra}+0.5)))AND((DEC>(${dec}-0.5))AND(DEC<(${dec}+0.5))))"
          fi
          CONDITION=${constructed_condition}
          ./perform_coadd_swarp.sh ${SUBARUDIR}/${cluster}/${filter} SCIENCE ${cluster}_${coadd} MEDIAN
	  if [ "$?" -gt "0" ]; then exit $? ; fi
          #adam-old# ic -p 8 '16 1 %1 1e-6 < ?' ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_${coadd}/coadd.weight.fits > ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_${coadd}/coadd.flag.fits
	  if [ "$?" -gt "0" ]; then exit $? ; fi
          ./setup_general.sh ${SUBARUDIR}/${cluster}/${filter}/SCIENCE instrument_$$
          rm -f instrument_$$
          ./update_coadd_header.sh ${SUBARUDIR}/${cluster}/${filter} SCIENCE ${cluster}_${coadd} STATS coadd ${MAGZP} AB ${CONDITION}
          exit_stat=$?
          if [ "${exit_stat}" -gt "0" ]; then
              	echo "adam-Error: problem with update_coadd_header.sh!"
              	exit ${exit_stat};
          fi
          coaddmodes="${coaddmodes} all"

        ##############################
        elif [ ${coadd} == "gabodsid" ]; then
          GABODSIDS=`${P_LDACTOASC} -i ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/cat/chips.cat6 -t STATS -b -k GABODSID | sort | uniq | awk '{printf "%i ", $0}'`
	  ./adam_gabodsids_combiner.py ${GABODSIDS} > gabodsid_tmp_${cluster}_${filter}.log
	  cat gabodsid_tmp_${cluster}_${filter}.log |\
          {   while read GABODSID GABCOND

          do
            #adam-ask# I changed it to take CONFIG into account when considering the IMAGEID!=6 cut
	    if [ ${GABODSID} -ge 1309 ] && [ ${GABODSID} -lt 3470 ]; then
              echo "10_2 chip configuration"
              CONFIG="10_2"
            elif [ ${GABODSID} -ge 3470 ] && [ ${GABODSID} -lt 7000 ]; then
              echo "10_3 chip configuration"
              CONFIG="10_3"
	    else
              echo "adam-Error: no config recognized for GABODSID=${GABODSID}"
              exit 1;
            fi
            #if [ "${CONFIG}" == "10_3" ]; then
            #    constructConditions gabodsid "(GABODSID=${GABODSID})" ""
            #else
            #    #adam-tmp# constructConditions gabodsid "((IMAGEID!=6)AND(GABODSID=${GABODSID}))" ""
            #    constructConditions gabodsid "(GABODSID=${GABODSID})" ""
            #fi
            constructConditions gabodsid "${GABCOND}"
	    CONDITION=${constructed_condition}

            ./perform_coadd_swarp.sh ${SUBARUDIR}/${cluster}/${filter} SCIENCE ${cluster}_gabodsid${GABODSID}
	    if [ "$?" -gt "0" ]; then exit $? ; fi
            ./update_coadd_header.sh ${SUBARUDIR}/${cluster}/${filter} SCIENCE ${cluster}_gabodsid${GABODSID} STATS coadd ${MAGZP} AB ${CONDITION}
            exit_stat=$?
            if [ "${exit_stat}" -gt "0" ]; then
              	echo "adam-Error: problem with update_coadd_header.sh!"
              	exit ${exit_stat};
            fi
            ic -p 8 '16 1 %1 1e-6 < ?' ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_gabodsid${GABODSID}/coadd.weight.fits > ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_gabodsid${GABODSID}/coadd.flag.fits
	    if [ "$?" -gt "0" ]; then exit $? ; fi
            coaddmodes="${coaddmodes} gabodsid${GABODSID}"
          done  }

        ##############################
        elif [ ${coadd} == "good" ]; then
            #adam-ask# is this right? I mean you could be coadding two different configs here couldn't you (I'm not sure if we should in principle, but we do in practice)
            if [ "${CONFIG}" == "10_3" ]; then
                #adam# constructConditions good "" "((seeing_rh_al<0.8)AND(e_abs<0.08))"
                #adam# I have to have some kind of condition here. else I get an error
                constructConditions good "" "(seeing_rh_al<3.5)"
            else
                #adam# constructConditions good "(IMAGEID!=6)" "((seeing_rh_al<0.8)AND(e_abs<0.08))"
                #adam# this should only happen for MACS1226+21 10_2 "good" mode
                #adam-old# constructConditions good "(IMAGEID!=6)" "((EXPOSURE!=6)AND(EXPOSURE!=7))"

		constructConditions good "" "(seeing_rh_al<3.5)"
		#adam-tmp# constructConditions good "(IMAGEID!=6)" "(seeing_rh_al<3.5)"
            fi
            CONDITION=${constructed_condition}
            echo "${CONDITION}"
          ./perform_coadd_swarp.sh ${SUBARUDIR}/${cluster}/${filter} SCIENCE ${cluster}_${coadd}
	  if [ "$?" -gt "0" ]; then exit $? ; fi
          #adam-note# We can only read the flag values when detecting objects. Since detection is done on the "all" coadd, it's OK to have no rings in the "good" coadd
	  ic -p 8 '16 1 %1 1e-6 < ?' ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_${coadd}/coadd.weight.fits > ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_${coadd}/coadd.flag.fits
	  if [ "$?" -gt "0" ]; then exit $? ; fi
          ./update_coadd_header.sh ${SUBARUDIR}/${cluster}/${filter} SCIENCE ${cluster}_${coadd} CHIPS_STATS coadd ${MAGZP} AB ${CONDITION}
          exit_stat=$?
          if [ "${exit_stat}" -gt "0" ]; then
              	echo "adam-Error: problem with update_coadd_header.sh!"
              	exit ${exit_stat};
          fi
          coaddmodes="${coaddmodes} good"

        ##############################
        ### new rotation coadd: split by both night and rotation
        elif [ ${coadd} == "rotation" ]; then
          ROTATIONS=`${P_LDACTOASC} -i ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/cat/chips.cat6 -t STATS -b -k ROTATION | sort | uniq | awk '{printf "%i ", $0}'`
          ${P_LDACTOASC} -i ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/cat/chips.cat6 -t STATS -b -k GABODSID ROTATION | sort | uniq > gabrot_$$.dat
          while read GABODSID ROTATION
          do
            #adam-ask# definitely wrong the old way, right? I changed it to take CONFIG into account when considering the IMAGEID!=6 cut
            if [ "${CONFIG}" == "10_3" ]; then
                constructConditions rotation "((GABODSID=${GABODSID})AND(ROTATION=${ROTATION}))" ""
            else
                #adam-tmp# constructConditions rotation "(((IMAGEID!=6)AND(GABODSID=${GABODSID}))AND(ROTATION=${ROTATION}))" ""
                constructConditions rotation "((GABODSID=${GABODSID})AND(ROTATION=${ROTATION}))" ""
            fi
            CONDITION=${constructed_condition}
            echo ${CONDITION}
            ./prepare_coadd_swarp_chips.sh -m ${SUBARUDIR}/${cluster}/${filter} \
                                   -s SCIENCE \
                                   -e "${ending}.sub" \
                                   -n ${cluster}_gab${GABODSID}-rot${ROTATION} \
                                   -w ".sub" \
                                   -eh headers${ASTROMADD} \
                                   -r ${ra} \
                                   -d ${dec} \
                                   -i ${IMAGESIZE} \
                                   -p ${PIXSCALEOUT} \
                                   -l ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/cat/chips.cat8 CHIPS_STATS \
                                      ${CONDITION} \
                                      ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/${cluster}_gab${GABODSID}-rot${ROTATION}.cat
	    if [ "$?" -gt "0" ]; then exit $? ; fi
            ./parallel_manager.sh ./resample_coadd_swarp_chips_para.sh ${SUBARUDIR}/${cluster}/${filter} SCIENCE "${ending}.sub" ${cluster}_gab${GABODSID}-rot${ROTATION} ${HEADDIR} ${cluster}_all
	    if [ "$?" -gt "0" ]; then exit $? ; fi
            ./perform_coadd_swarp.sh ${SUBARUDIR}/${cluster}/${filter} SCIENCE ${cluster}_gab${GABODSID}-rot${ROTATION}
	    if [ "$?" -gt "0" ]; then exit $? ; fi
            ic -p 8 '16 1 %1 1e-6 < ?' ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_gab${GABODSID}-rot${ROTATION}/coadd.weight.fits > ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_gab${GABODSID}-rot${ROTATION}/coadd.flag.fits
	    if [ "$?" -gt "0" ]; then exit $? ; fi
            ./update_coadd_header.sh ${SUBARUDIR}/${cluster}/${filter} SCIENCE ${cluster}_gab${GABODSID}-rot${ROTATION} CHIPS_STATS coadd ${MAGZP} AB ${CONDITION}
            exit_stat=$?
            if [ "${exit_stat}" -gt "0" ]; then
              	echo "adam-Error: problem with update_coadd_header.sh!"
              	exit ${exit_stat};
            fi
            coaddmodes="${coaddmodes} gab${GABODSID}-rot${ROTATION}"
          done < gabrot_$$.dat

        ###################################
        elif [ ${coadd} == "exposure" ] && [ ${filter} != "K" ] && [ ${filter} != "I" ]; then
          calib=`awk 'BEGIN{if("'${filter}'"~"CALIB") print "1"; else print "0"}'`
          calib="0" #adam-tmp#
          if [ ${calib} -eq 0 ]; then
          ${P_LDACTOASC} -i ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/cat/chips.cat6 -t STATS -s -b -k EXPOSURE IMAGENAME > exposures_$$.list
            while read EXPOSURE IMAGENAME
            do
              constructConditions exposure "(EXPOSURE=${EXPOSURE})" ""
              CONDITION=${constructed_condition}
              ./perform_coadd_swarp.sh ${SUBARUDIR}/${cluster}/${filter} SCIENCE ${cluster}_${IMAGENAME}
	      if [ "$?" -gt "0" ]; then exit $? ; fi
              ic -p 8 '16 %1 %2 1e-6 < ?' ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_${IMAGENAME}/coadd.flag.fits ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_${IMAGENAME}/coadd.weight.fits | ic -p 8 '16 %1 %1 0 == ?' ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_${IMAGENAME}/coadd.flag.fits > ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_${IMAGENAME}/coadd.flag.temp.fits
	      if [ "$?" -gt "0" ]; then exit $? ; fi
              mv ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_${IMAGENAME}/coadd.flag.temp.fits ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_${IMAGENAME}/coadd.flag.fits
              ./update_coadd_header.sh ${SUBARUDIR}/${cluster}/${filter} SCIENCE ${cluster}_${IMAGENAME} STATS coadd ${MAGZP} AB ${CONDITION}
              exit_stat=$?
              if [ "${exit_stat}" -gt "0" ]; then
              	echo "adam-Error: problem with update_coadd_header.sh!"
              	exit ${exit_stat};
              fi
              coaddmodes="${coaddmodes} ${IMAGENAME}"
            done < exposures_$$.list
            rm -f exposures_$$.list
          fi

        ###################################
        elif [ ${coadd} == "3s" ]; then
          calib=`awk 'BEGIN{if("'${filter}'"~"CALIB") print "1"; else print "0"}'`
          if [ ${calib} -eq 1 ]; then
              ./perform_coadd_swarp.sh ${SUBARUDIR}/${cluster}/${filter} SCIENCE ${cluster}_${coadd}
	      if [ "$?" -gt "0" ]; then exit $? ; fi
              ./update_coadd_header.sh ${SUBARUDIR}/${cluster}/${filter} SCIENCE ${cluster}_${coadd} STATS coadd ${MAGZP} AB ${CONDITION}
              exit_stat=$?
              if [ "${exit_stat}" -gt "0" ]; then
              	echo "adam-Error: problem with update_coadd_header.sh!"
              	exit ${exit_stat};
              fi
              coaddmodes="${coaddmodes} 3s"
          fi

        fi
done

for coaddmode in ${coaddmodes}
do
        echo ${coaddmode}

        ### add header keywords
        value ${cluster}
        writekey ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_${coaddmode}/coadd.fits OBJECT "${VALUE} / Target" REPLACE
        value ${filter}
        writekey ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_${coaddmode}/coadd.fits FILTER "${VALUE} / Filter" REPLACE

        ### update photometry
        if [ -f "${SUBARUDIR}/${cluster}/${filter}/SCIENCE/cat/chips_phot.cat5" ]; then
            MAGZP=`${P_LDACTOASC} -i ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/cat/chips_phot.cat5 -t ABSPHOTOM -k COADDZP | tail -n 1`
            value ${MAGZP}
            writekey ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_${coaddmode}/coadd.fits MAGZP "${VALUE} / Magnitude zeropoint" REPLACE
        else
            MAGZP=-1.0
        fi

        ### make PSF plots, and write star reference catalog
        ./check_psf_coadd.sh ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_${coaddmode} coadd.fits
	if [ "$?" -gt "0" ]; then exit $? ; fi

        #  if [ ${filter} == "K" ]; then
        #      ./fit_phot_K.sh ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_${coaddmode} coadd.fits
        #  fi
done

if [ -f gabodsid_tmp_${cluster}_${filter}.log ]; then
	rm gabodsid_tmp_${cluster}_${filter}.log
fi

if [ ! -d ${SUBARUDIR}/${cluster}/coadds_together_${cluster} ]; then
	mkdir ${SUBARUDIR}/${cluster}/coadds_together_${cluster}
	mkdir ${SUBARUDIR}/${cluster}/coadds_together_${cluster}/flags
	mkdir ${SUBARUDIR}/${cluster}/coadds_together_${cluster}/weights
fi
for coadd_dir in `\ls -d ${SUBARUDIR}/${cluster}/${filter}/SCIENCE/coadd_${cluster}_*/`
do
	coadd_name=`basename ${coadd_dir} `
	ln -s ${coadd_dir}/coadd.fits ${SUBARUDIR}/${cluster}/coadds_together_${cluster}/${coadd_name}.${filter}.fits
	ln -s ${coadd_dir}/coadd.flag.fits ${SUBARUDIR}/${cluster}/coadds_together_${cluster}/flags/${coadd_name}.${filter}.flag.fits
	ln -s ${coadd_dir}/coadd.weight.fits ${SUBARUDIR}/${cluster}/coadds_together_${cluster}/weights/${coadd_name}.${filter}.weight.fits
done
echo "adam-look: ds9e ${SUBARUDIR}/${cluster}/coadds_together_${cluster}/coadd_${cluster}_*.fits &"
#adam# may want to run this to make plots of how good the exposures are (might want to throw out bad ones!): ./Plot_cluster_seeing_e1e2.py ${cluster}


text:
"""
    markupsafe
    ~~~~~~~~~~

    Implements a Markup string.

    :copyright: (c) 2010 by Armin Ronacher.
    :license: BSD, see LICENSE for more details.
"""
import re
from itertools import imap

__all__ = ['Markup', 'soft_unicode', 'escape', 'escape_silent']

_striptags_re = re.compile(r'(|<[^>]*>)')
_entity_re = re.compile(r'&([^;]+);')

class Markup(unicode):
    r"""Marks a string as being safe for inclusion in HTML/XML output without
    needing to be escaped.  This implements the `__html__` interface a couple
    of frameworks and web applications use.  :class:`Markup` is a direct
    subclass of `unicode` and provides all the methods of `unicode` just that
    it escapes arguments passed and always returns `Markup`.

    The `escape` function returns markup objects so that double escaping can't
    happen.

    The constructor of the :class:`Markup` class can be used for three
    different things:  When passed an unicode object it's assumed to be safe,
    when passed an object with an HTML representation (has an `__html__`
    method) that representation is used, otherwise the object passed is
    converted into a unicode string and then assumed to be safe:

    >>> Markup("Hello World!")
    Markup(u'Hello World!')
    >>> class Foo(object):
    ...  def __html__(self):
    ...   return 'foo'
    ...
    >>> Markup(Foo())
    Markup(u'foo')

    If you want object passed being always treated as unsafe you can use the
    :meth:`escape` classmethod to create a :class:`Markup` object:

    >>> Markup.escape("Hello World!")
    Markup(u'Hello <em>World</em>!')

    Operations on a markup string are markup aware which means that all
    arguments are passed through the :func:`escape` function:

    >>> em = Markup("%s")
    >>> em % "foo & bar"
    Markup(u'foo & bar')
    >>> strong = Markup("%(text)s")
    >>> strong % {'text': '<blink>hacker here</blink>'}
    Markup(u'<blink>hacker here</blink>')
    >>> Markup("Hello ") + "<foo>"
    Markup(u'Hello <foo>')
    """
    __slots__ = ()

    def __new__(cls, base=u'', encoding=None, errors='strict'):
        if hasattr(base, '__html__'):
            base = base.__html__()
        if encoding is None:
            return unicode.__new__(cls, base)
        return unicode.__new__(cls, base, encoding, errors)

    def __html__(self):
        return self

    def __add__(self, other):
        if hasattr(other, '__html__') or isinstance(other, basestring):
            return self.__class__(unicode(self) + unicode(escape(other)))
        return NotImplemented

    def __radd__(self, other):
        if hasattr(other, '__html__') or isinstance(other, basestring):
            return self.__class__(unicode(escape(other)) + unicode(self))
        return NotImplemented

    def __mul__(self, num):
        if isinstance(num, (int, long)):
            return self.__class__(unicode.__mul__(self, num))
        return NotImplemented
    __rmul__ = __mul__

    def __mod__(self, arg):
        if isinstance(arg, tuple):
            arg = tuple(imap(_MarkupEscapeHelper, arg))
        else:
            arg = _MarkupEscapeHelper(arg)
        return self.__class__(unicode.__mod__(self, arg))

    def __repr__(self):
        return '%s(%s)' % (
            self.__class__.__name__,
            unicode.__repr__(self)
        )

    def join(self, seq):
        return self.__class__(unicode.join(self, imap(escape, seq)))
    join.__doc__ = unicode.join.__doc__

    def split(self, *args, **kwargs):
        return map(self.__class__, unicode.split(self, *args, **kwargs))
    split.__doc__ = unicode.split.__doc__

    def rsplit(self, *args, **kwargs):
        return map(self.__class__, unicode.rsplit(self, *args, **kwargs))
    rsplit.__doc__ = unicode.rsplit.__doc__

    def splitlines(self, *args, **kwargs):
        return map(self.__class__, unicode.splitlines(self, *args, **kwargs))
    splitlines.__doc__ = unicode.splitlines.__doc__

    def unescape(self):
        r"""Unescape markup again into an unicode string.  This also resolves
        known HTML4 and XHTML entities:

        >>> Markup("Main » About").unescape()
        u'Main \xbb About'
        """
        from markupsafe._constants import HTML_ENTITIES
        def handle_match(m):
            name = m.group(1)
            if name in HTML_ENTITIES:
                return unichr(HTML_ENTITIES[name])
            try:
                if name[:2] in ('#x', '#X'):
                    return unichr(int(name[2:], 16))
                elif name.startswith('#'):
                    return unichr(int(name[1:]))
            except ValueError:
                pass
            return u''
        return _entity_re.sub(handle_match, unicode(self))

    def striptags(self):
        r"""Unescape markup into an unicode string and strip all tags.  This
        also resolves known HTML4 and XHTML entities.  Whitespace is
        normalized to one:

        >>> Markup("Main »  About").striptags()
        u'Main \xbb About'
        """
        stripped = u' '.join(_striptags_re.sub('', self).split())
        return Markup(stripped).unescape()

    @classmethod
    def escape(cls, s):
        """Escape the string.  Works like :func:`escape` with the difference
        that for subclasses of :class:`Markup` this function would return the
        correct subclass.
        """
        rv = escape(s)
        if rv.__class__ is not cls:
            return cls(rv)
        return rv

    def make_wrapper(name):
        orig = getattr(unicode, name)
        def func(self, *args, **kwargs):
            args = _escape_argspec(list(args), enumerate(args))
            _escape_argspec(kwargs, kwargs.iteritems())
            return self.__class__(orig(self, *args, **kwargs))
        func.__name__ = orig.__name__
        func.__doc__ = orig.__doc__
        return func

    for method in '__getitem__', 'capitalize', \
                  'title', 'lower', 'upper', 'replace', 'ljust', \
                  'rjust', 'lstrip', 'rstrip', 'center', 'strip', \
                  'translate', 'expandtabs', 'swapcase', 'zfill':
        locals()[method] = make_wrapper(method)

    # new in python 2.5
    if hasattr(unicode, 'partition'):
        def partition(self, sep):
            return tuple(map(self.__class__,
                             unicode.partition(self, escape(sep))))
        def rpartition(self, sep):
            return tuple(map(self.__class__,
                             unicode.rpartition(self, escape(sep))))

    # new in python 2.6
    if hasattr(unicode, 'format'):
        format = make_wrapper('format')

    # not in python 3
    if hasattr(unicode, '__getslice__'):
        __getslice__ = make_wrapper('__getslice__')

    del method, make_wrapper

def _escape_argspec(obj, iterable):
    """Helper for various string-wrapped functions."""
    for key, value in iterable:
        if hasattr(value, '__html__') or isinstance(value, basestring):
            obj[key] = escape(value)
    return obj

class _MarkupEscapeHelper(object):
    """Helper for Markup.__mod__"""

    def __init__(self, obj):
        self.obj = obj

    __getitem__ = lambda s, x: _MarkupEscapeHelper(s.obj[x])
    __str__ = lambda s: str(escape(s.obj))
    __unicode__ = lambda s: unicode(escape(s.obj))
    __repr__ = lambda s: str(escape(repr(s.obj)))
    __int__ = lambda s: int(s.obj)
    __float__ = lambda s: float(s.obj)

# we have to import it down here as the speedups and native
# modules imports the markup type which is define above.
try:
    from markupsafe._speedups import escape, escape_silent, soft_unicode
except ImportError:
    from markupsafe._native import escape, escape_silent, soft_unicode


text:
import sys
import math

import numpy        as np
import numpy.random as npr
import scipy.linalg as spla

from .abstract_sampler import AbstractSampler
from ..utils import param as hyperparameter_utils

class EllipticalSliceSampler(AbstractSampler):

    # No prior explicitly done here, that is included in the sampling
    def logprob(self, x, model):
        hyperparameter_utils.set_params_from_array(self.params, x)
        return model.log_binomial_likelihood() # no contribution from priors-- Gaussian prior built in to sampler

    def sample(self, model):
        if not model.has_data:
            return np.zeros(0) # TODO this should be a sample from the prior...

        prior_cov      = model.noiseless_kernel.cov(model.inputs)
        prior_cov_chol = spla.cholesky(prior_cov, lower=True)
        # Here get the Cholesky from model

        params_array = hyperparameter_utils.params_to_array(self.params)
        for i in xrange(self.thinning + 1):
            params_array, current_ll = elliptical_slice(
                params_array,
                self.logprob,
                prior_cov_chol,
                model.mean.value,
                model,
                **self.sampler_options
            )
            hyperparameter_utils.set_params_from_array(self.params, params_array)
        self.current_ll = current_ll # for diagnostics

# xx: the initial point
# sample_nu: a function that samples from the multivariate Gaussian prior
# log_like_fn: a function that computes the log likelihood of an input
# cur_log_like (optional): the current log likelihood
# angle_range: not sure
def elliptical_slice(xx, log_like_fn, prior_chol, prior_mean, *log_like_args, **sampler_args):
    cur_log_like = sampler_args.get('cur_log_like', None)
    angle_range = sampler_args.get('angle_range', 0)

    if cur_log_like is None:
        cur_log_like = log_like_fn(xx, *log_like_args)

    if np.isneginf(cur_log_like):
        raise Exception("Elliptical Slice Sampler: initial logprob is -inf for inputs %s" % xx)
    if np.isnan(cur_log_like):
        raise Exception("Elliptical Slice Sampler: initial logprob is NaN for inputs %s" % xx)

    nu = np.dot(prior_chol, npr.randn(xx.shape[0])) # don't bother adding mean here, would just subtract it at update step
    hh = np.log(npr.rand()) + cur_log_like
    # log likelihood threshold -- LESS THAN THE INITIAL LOG LIKELIHOOD

    # Set up a bracket of angles and pick a first proposal.
    # "phi = (theta'-theta)" is a change in angle.
    if angle_range <= 0:
        # Bracket whole ellipse with both edges at first proposed point
        phi = npr.rand()*2*math.pi
        phi_min = phi - 2*math.pi
        phi_max = phi
    else:
        # Randomly center bracket on current point
        phi_min = -angle_range*npr.rand();
        phi_max = phi_min + angle_range;
        phi = npr.rand()*(phi_max - phi_min) + phi_min;

    # Slice sampling loop
    while True:
        # Compute xx for proposed angle difference
        # and check if it's on the slice
        xx_prop = (xx-prior_mean)*np.cos(phi) + nu*np.sin(phi) + prior_mean

        cur_log_like = log_like_fn(xx_prop, *log_like_args)

        if cur_log_like > hh:
            # New point is on slice, ** EXIT LOOP **
            return xx_prop, cur_log_like

        # Shrink slice to rejected point
        if phi > 0:
            phi_max = phi
        elif phi < 0:
            phi_min = phi
        else:
            sys.stderr.write('Initial x: %s\n' % xx)
            # sys.stderr.write('initial log like = %f\n' % initial_log_like)
            sys.stderr.write('Proposed x: %s\n' % xx_prop)
            sys.stderr.write('ESS log lik = %f\n' % cur_log_like)
            raise Exception('BUG DETECTED: Shrunk to current position '
                            'and still not acceptable.');

        # Propose new angle difference
        phi = npr.rand()*(phi_max - phi_min) + phi_min

if __name__ == '__main__':

    from utils import priors
    import time

    print '2D Gaussian:'

    n = 1000000

    # Test on 2D Gaussian times another 2D Gaussian
    # one will be the "prior" and the other the "posterior"
    x_samples = np.zeros((2,n))
    x = np.zeros(2)

    prior_mu = np.array([-5, 2])
    prior_L = npr.randn(2,2)
    prior_L[0,1] = 0.0 # lower chol
    prior_cov = np.dot(prior_L,prior_L.T)

    like_mu = np.array([2, -1])
    like_L = npr.randn(2,2)
    like_L[0,1] = 0.0 # lower chol
    like_cov = np.dot(like_L, like_L.T)
    like = priors.MultivariateNormal(mu=like_mu, cov=like_cov)

    print 'Prior cov:'
    print prior_cov
    print 'Like cov:'
    print like_cov

    current_time = time.time()
    cur_ll = None
    for i in xrange(n):
        if i % 1000 == 0:
            print 'Elliptical Slice Sample %d/%d' % (i,n)
        x, cur_ll = elliptical_slice(x, like.logprob, prior_L, prior_mu, cur_log_like=cur_ll)
        x_samples[:,i] = x.copy()

    print 'Elliptical slice sampling took %f seconds' % (time.time() - current_time)

    # Formula for the actual mean and covariance matrix below came from
    # the wikipedia page on conjugate priors
    actual_cov = spla.inv(spla.inv(prior_cov) + spla.inv(like_cov))
    A = spla.cho_solve((prior_L, True), prior_mu)
    B = spla.cho_solve((like_L, True), like_mu)
    actual_mean = np.dot(actual_cov, A+B)

    print 'Actual mean:           %s' % actual_mean
    print 'Mean of ESS samples:   %s' % np.mean(x_samples,axis=1)

    print 'Actual Cov:'
    print actual_cov
    print 'Cov of ESS samples:'
    print np.cov(x_samples)

    # below: also compare with regular slice sampling (slower)

    # # Can also sample with regular slice sampling as another comparison
    # # Because I just compose them
    # from slice_sampler import slice_sample
    # pri = priors.MultivariateNormal(mu=prior_mu, cov=prior_cov)
    # post = priors.ProductOfPriors([pri, like])
    # xx_samples = np.zeros((2,n))
    # xx = np.zeros(2)

    # current_time = time.time()
    # for i in xrange(n):
    #     if i % 1000 == 0:
    #         print 'Slice Sample %d/%d' % (i,n)
    #     xx, cur_ll = slice_sample(xx, post.logprob)
    #     xx_samples[:,i] = xx.copy()

    # print 'Slice sampling took %f seconds' % (time.time() - current_time)
    # print ''

    # print 'Actual mean:           %s' % actual_mean
    # print 'Mean of ESS samples:   %s' % np.mean(x_samples,axis=1)
    # print 'Mean of slice samples: %s' % np.mean(xx_samples,axis=1)

    # print 'Actual Cov:'
    # print actual_cov
    # print 'Cov of ESS samples:'
    # print np.cov(x_samples)
    # print 'Cov of slice samples:'
    # print np.cov(xx_samples)


text:
/// <reference path="../../../client/@types/types.d.ts"/>
import * as _ from 'lodash';

// The following line is important to keep in that format so it can be rendered into the page
export const config: IDashboardConfig = /*return*/ {
  id: "bot_analytics_goals_tracking",
	name: "Bot Analytics Triggered Goals Dashboard",
	icon: "dashboard",
	url: "bot_analytics_goals_tracking",
  description: "Generic goal tracking dashboard",
	preview: "/images/bot-ai-cs.png",
	category: "Bots",
html: `

      This dashboard is built to view generic goals triggered during a conversation, similar to Goals that can be triggered on a web site in Google Analytics.

      Goals can be captured by capturing a custom event using the Node or C# telemetry modules. To do this, track a custom event with the event name "Goal".
      An event property called "Name", should then also be added to the event properties list to assign a name to the Goal.
      e.g. in C# you can track a goal using the following snippet.

      var eventProperties = new Dictionary<string, string>();

      eventProperties.Add("GoalName", "Your goal name");

      DefaultInstrumentation.TrackCustomEvent(context.Activity, "MBFEvent.GoalEvent", eventProperties);

      This dashboard contains a bar chart which will show the top 3 goals triggered and when the bar chart is clicked a full list of goals
      and their associated counts will be shown. You can then drill into the goals to see the conversations where the goals were triggered.

Getting the data to show

        To see all the capabilities of this dashboard, it is recommended to integrate you bot with one of the following:

        Node.js Telemetry Plugin

        C# Telemetry Plugin

        This will enable the bot to send additional telemetry information to Application Insights.

Additional Learnings

        This dashboard uses Application Insights Analytics.

        You can also run queries directly using API Explorer

  `,
	config: {
		connections: {},
		layout: {
			isDraggable: true,
			isResizable: true,
			rowHeight: 30,
			verticalCompact: false,
			cols: { lg: 12,md: 10,sm: 6,xs: 4,xxs: 2 },
			breakpoints: { lg: 1200,md: 996,sm: 768,xs: 480,xxs: 0 }
		}
	},
	dataSources: [
    {
			id: "timespan",
			type: "Constant",
			params: { values: ["24 hours","1 week","1 month","3 months"],selectedValue: "1 month" },
			calculated: (state, dependencies) => {
        var queryTimespan =
          state.selectedValue === '24 hours' ? 'PT24H' :
          state.selectedValue === '1 week' ? 'P7D' :
          state.selectedValue === '1 month' ? 'P30D' :
          'P90D';
        var granularity =
          state.selectedValue === '24 hours' ? '5m' :
          state.selectedValue === '1 week' ? '1d' : '1d';

        return { queryTimespan, granularity };
      }
		},
		{
			id: "filters",
			type: "ApplicationInsights/Query",
			dependencies: { timespan: "timespan",queryTimespan: "timespan:queryTimespan",granularity: "timespan:granularity" },
			params: {
				table: "customEvents",
				queries: {
					filterChannels: {
						query: () => `
              where name == 'MBFEvent.UserMessage' |
              extend channel=tostring(customDimensions.channel) |
              summarize channel_count=count() by channel |
              order by channel_count`,
						mappings: { channel: (val) => val || "unknown",channel_count: (val) => val || 0 },
						calculated: (filterChannels, dependencies, prevState) => {

              if (!filterChannels) { return; }

              // This code is meant to fix the following scenario:
              // When "Timespan" filter changes, to "channels-selected" variable
              // is going to be reset into an empty set.
              // For this reason, using previous state to copy filter
              const filters = filterChannels.map((x) => x.channel);
              let selectedValues = [];
              if (prevState['channels-selected'] !== undefined) {
								selectedValues = prevState['channels-selected'];
							}
              return {
                "channels-count": filterChannels,
                "channels-filters": filters,
                "channels-selected": selectedValues,
              };
            }
					}
				}
			}
		},
		{
			id: "ai",
			type: "ApplicationInsights/Query",
			dependencies: {
				timespan: "timespan",
				queryTimespan: "timespan:queryTimespan",
				granularity: "timespan:granularity",
				selectedChannels: "filters:channels-selected"
			},
			params: {
				table: "customEvents",
				queries: {
					goals_top_goals_count: {
						query: () => `
              extend GoalName=tostring(customDimensions.GoalName) |
              where name=='MBFEvent.GoalEvent' |
              summarize count=count() by GoalName | take 3`,
						mappings: { goal: (val) => val || "Unknown",count: (val) => val || 0 },
						filters: [{ dependency: "selectedChannels",queryProperty: "customDimensions.channel" }],
						calculated: (goals) => {
							return {
								"goals-bars": [ 'count' ]
							};
						}
					}
				}
			}
		}
  ],
	filters: [
    {
			type: "TextFilter",
			title: "Timespan",
			dependencies: { selectedValue: "timespan",values: "timespan:values" },
			actions: { onChange: "timespan:updateSelectedValue" },
			first: true
		},
		{
			type: "MenuFilter",
			title: "Channels",
			subtitle: "Select channels",
			icon: "forum",
			dependencies: { selectedValues: "filters:channels-selected",values: "filters:channels-filters" },
			actions: { onChange: "filters:updateSelectedValues:channels-selected" },
			first: true
		}
  ],
	elements: [
    {
			id: "goals",
			type: "BarData",
			title: "Goals Graph",
			subtitle: "Goals triggered per time",
			size: { w: 8,h: 8 },
			dependencies: { values: "ai:goals_top_goals_count",bars: "ai:goals-bars" },
			props: { nameKey: "GoalName" },
			actions: {
				onBarClick: {
					action: "dialog:goalsTriggeredDialog",
					params: { queryspan: "timespan:queryTimespan",selectedChannels: "filters:channels-selected" }
				}
			}
		}
  ],
	dialogs: [
  {
			id: "goalsTriggeredDialog",
			width: "60%",
			params: ["queryspan", "selectedChannels"],
			dataSources: [
				{
					id: "goals-data",
					type: "ApplicationInsights/Query",
          dependencies: { queryTimespan: "dialog_goalsTriggeredDialog:queryspan",
          selectedChannels: "dialog_goalsTriggeredDialog:selectedChannels" },
					params: {
            filters: [{ dependency: "selectedChannels",queryProperty: "customDimensions.channel" }],
						query: () => `customEvents |
              extend GoalName=tostring(customDimensions.GoalName) |
              where name=='MBFEvent.GoalEvent' |
              summarize count=count() by GoalName | order by GoalName asc`
					}
				}
			],
			elements: [
				{
					id: "goals-count-list",
					type: "Table",
					title: "Goals Triggered",
					size: { w: 12,h: 16 },
					dependencies: { values: "goals-data", selectedChannels:"dialog_goalsTriggeredDialog:selectedChannels" },
          props: { cols: [
            { header: "Goal Name",field: "GoalName" },
            { header: "Count",field: "count" },
            { type: "button",value: "chat",click: "openConversationsDialog" }] },
          actions: {
				    openConversationsDialog: {
              action: "dialog:goalConversations",
              params: { title: "args:GoalName", goal: "args:GoalName",
              queryspan: "timespan:queryTimespan", selectedChannels: "filters:channels-selected"}}
			    }
				}
			]
  },
  {
			id: "goalConversations",
			width: "60%",
			params: ["title","goal","queryspan","selectedChannels"],
			dataSources: [
				{
					id: "goal-conversations-data",
					type: "ApplicationInsights/Query",
          dependencies: {
            goal: "dialog_goalConversations:goal",
            queryTimespan: "dialog_goalConversations:queryspan",
            selectedChannels: "dialog_goalConversations:selectedChannels" },
					params: {
            filters: [{ dependency: "selectedChannels" ,queryProperty: "customDimensions.channel" }],
						query: ({ goal }) => `
              customEvents
              | extend conversation=tostring(customDimensions.conversationId), goal=customDimensions.GoalName
              | where name=='MBFEvent.GoalEvent' and goal =~ '${goal}'
              | summarize count=count(), maxTimestamp=max(timestamp) by conversation
              | order by maxTimestamp`,
						mappings: { id: (val, row, idx) => `Conversation ${idx}` }
					}
				}
			],
			elements: [
				{
					id: "conversations-list",
					type: "Table",
					title: "Conversations",
					size: { w: 12,h: 16 },
					dependencies: { values: "goal-conversations-data" },
					props: {
						cols: [
							{ header: "Conversation Id",field: "id" },
							{ header: "Last Message",field: "maxTimestamp",type: "time",format: "MMM-DD HH:mm:ss" },
							{ header: "Count",field: "count" },
							{ type: "button",value: "chat",click: "openMessagesDialog" }
						]
					},
					 actions: {
					 	openMessagesDialog: {
					 		action: "dialog:messages",
					 		params: {
					 			title: "args:id",
					 			conversation: "args:conversation",
					 			queryspan: "timespan:queryTimespan"
					 		}
					 	}
					 }
				}
			]
  },
  {
			id: "messages",
			width: "50%",
			params: ["title","conversation","intent","queryspan"],
			dataSources: [
				{
					id: "messages-data",
					type: "ApplicationInsights/Query",
					dependencies: {
						conversation: "dialog_messages:conversation",
						optional_intent: "dialog_messages:intent",
						queryTimespan: "dialog_messages:queryspan"
					},
					params: {
						query: ({ conversation, optional_intent }) => `
              customEvents
              | extend intent=tostring(customDimensions.intent),
                      conversation=tostring(customDimensions.conversationId),
                      eventTimestamp=tostring(customDimensions.timestamp),
                      userId=tostring(customDimensions.userId)
              | where name == "MBFEvent.UserMessage" and conversation == '${conversation}'
              | join kind= leftouter (
                  customEvents
                  | extend sentiment=tostring(customDimensions.score),
                          eventTimestamp=tostring(customDimensions.timestamp),
                          conversation=tostring(customDimensions.conversationId),
                          userId=tostring(customDimensions.userId)
                  | where name == "MBFEvent.Sentiment" and conversation == '${conversation}'
              ) on eventTimestamp, conversation, userId
              ${optional_intent && ` | union (
                    customEvents
                    | extend conversation=tostring(customDimensions.conversationId),
                            intent=tostring(customDimensions.intent),
                            eventTimestamp=tostring(customDimensions.timestamp),
                            userId=tostring(customDimensions.userId)
                    | where name == "MBFEvent.Intent" and intent == '${optional_intent}' and conversation == '${conversation}'
                )
              ` || ''}
              | union (
                  customEvents
                  | extend conversation=tostring(customDimensions.conversationId),
                          intent=tostring(customDimensions.intent),
                          eventTimestamp=tostring(customDimensions.timestamp),
                          userId=tostring(customDimensions.userId)
                  | where name == "MBFEvent.BotMessage" and conversation == '${conversation}'
              )
              | project timestamp, eventName=name, message=customDimensions.text, customDimensions.userName, userId, intent, sentiment
              | order by timestamp asc
              `
					},
					calculated: (state, dependencies) => {
            let { values } = state;

            if (!values) { return; }

            _.forEach(values, (msg, index) => {
              msg.message = msg.message;
              msg.icon = isNaN(parseInt(msg.sentiment)) ? '' :
                       msg.sentiment > 0.6 ? 'sentiment_satisfied' :
                       msg.sentiment < 0.4 ? 'sentiment_dissatisfied' : '';
              msg.color = isNaN(parseInt(msg.sentiment)) ? '' :
                       msg.sentiment > 0.6 ? '#AEEA00' :
                       msg.sentiment < 0.4 ? '#D50000' : '';

              if (msg.eventName === 'MBFEvent.UserMessage') {
                let i = +index;
                let j = i + 1;
                while(j <= i + 5 && j < values.length) {
                  let intent = values[j];
                  if (intent.eventName === 'MBFEvent.Intent' && intent.message === msg.message && intent.intent) {
                    msg.intent = intent.intent;
                    msg.message = '[' + msg.intent + '] ' + msg.message;
                    msg.eventName = msg.eventName + ' intent';
                    break;
                  }
                  j++;
                }
              }
            });

            let chat = values.filter(msg => msg.eventName !== 'MBFEvent.Intent');

            return { chat };
          }
				}
			],
			elements: [
				{
					id: "messages-list",
					type: "Table",
					title: "Messages",
					size: { w: 12,h: 16 },
					dependencies: { values: "messages-data:chat" },
					props: {
						rowClassNameField: "eventName",
						cols: [
							{ header: "Timestamp",width: "50px",field: "timestamp",type: "time",format: "MMM-DD HH:mm:ss" },
							{ width: "10px",field: "icon",type: "icon",color: "color" },
							{ header: "Message",width: "500px",field: "message" }
						]
					}
				}
			]
		}
  ]
}


text:
module MzML

  # An internal module containing useful regular expressions
  module RGX
    # The file byte offset of the start of the file index
    INDEX_OFFSET = /<indexListOffset>(\d+)<\/indexListOffset>/
    # The start of a either a spectrumList or chromatographList
    DATA_LIST_START =  /<(spectrum|chromatogram)List\s.*count\=["'](\d+)/m
    # The start spectrum or chromatogram element
    DATA_START = /<(spectrum|chromatogram)\s.*id=["']([^'"]+)["']/m
    # The end spectrum or chromatogram element
    DATA_END = /(<\/(spectrum|chromatogram)>)/
  end

  class UnsupportedFileFormat < Exception
  end
  class BadIdentifier < Exception
  end

  # The main mzML parser class, it is a subclass of the File class from the
  # Ruby standard library in that it places a read cursor on the mzML file,
  # and will skip around using byte-offsets. We utilize the index at the
  # end of mzML files to facilitate random access of spectra.
  #
  # The {#each} method will cycle through all of the spectrum in a file, starting
  # from the first one each time. If you would rather access the spectra randomly,
  # the {#spectrum_list} attribute contains the ordered list of specturm identifiers.
  # You can access the MzML::Spectrum objects by feeding these identifiers to the {#spectrum}
  # method.
  class Doc < ::File

    # Open a file handle to a mzML document
    def initialize(mz_fname)
      unless mz_fname =~ /\.mzML$/
        raise MzML::UnsupportedFileFormat.new  "File extension must be .\"mzML\""
      end
      super(mz_fname, "r")
      @fname = mz_fname
      @index = parse_index_list
      @spectrum_count = @spectrum_list.length
      @chromatogram_count = @chromatogram_list.length
      @current_spectrum_index = 0
      @current_chromatogram_index = 0
    end
    attr_reader :index, :fname, :spectrum_list, :spectrum_count, :chromatogram_list, :chromatogram_count

    # Fetch a {MzML::Chromatogram} from the file, given the identifier
    # @param chromatogram_id [String]
    # @return [MzML::Chromatogram]
    def chromatogram(chromatogram_id)
      if @index[:chromatogram].has_key? chromatogram_id
        self.seek @index[:chromatogram][chromatogram_id]
        return MzML::Chromatogram.new(parse_next)
      else
        raise MzML::BadIdentifier.new("Invalid ID '#{chromatogram_id}'")
      end
    end

    # Fetch a {MzML::Spectrum} from the file, given the identifier
    # @param spectrum_id [String]
    # @return [MzML::Spectrum]
    def spectrum(spectrum_id)
      if @index[:spectrum].has_key? spectrum_id
        self.seek @index[:spectrum][spectrum_id]
        return MzML::Spectrum.new(parse_next())
      else
        raise MzML::BadIdentifier.new("Invalid ID '#{spectrum_id}'")
      end
    end

    def each &block
      @spectrum_list.each do |spectrum_id|
        block.call(self.spectrum(spectrum_id))
        @current_spectrum_index += 1
      end
    end
    alias_method :each_spectrum, :each

    def each_chromatogram &block
      @chromatogram_list.each do |chromatogram_id|
        block.call(self.chromatogram(chromatogram_id))
        @current_chromatogram_index += 1
      end
    end

    def next
      if @current_spectrum_index < @spectrum_list.length
        @current_spectrum_index += 1
        self.spectrum(@spectrum_list[@current_spectrum_index - 1])
      else
        nil
      end
    end
    alias_method :next_spectrum, :next

    def next_chromatogram
      if @current_chromatogram_index < @chromatogram_list.length
        @current_chromatogram_index += 1
        self.chromatogram(@chromatogram_list[@current_chromatogram_index - 1])
      else
        nil
      end
    end

    def rewind
      super
      @current_spectrum_index = 0
      @current_chromatogram_index = 0
      self.pos
    end

    private
    # Parses the IndexList
    def parse_index_list
      self.seek(self.stat.size - 200)
      # parse the index offset
      tmp = self.read
      tmp  =~  MzML::RGX::INDEX_OFFSET
      offset = $1
      # if I didn't match anything, compute the index and return
      unless (offset)
        return compute_index_list
      end
      @index = {}
      @spectrum_list = []
      @chromatogram_list = []
      self.seek(offset.to_i)
      tmp = Nokogiri::XML.parse(self.read).root
      tmp.css("index").each do |idx|
        index_type = idx[:name].to_sym
        @index[index_type] = {}
        idx.css("offset").each do |o|
          @index[index_type][o[:idRef]] = o.text().to_i
          if index_type == :spectrum
            @spectrum_list << o[:idRef]
          else
            @chromatogram_list << o[:idRef]
          end
        end
      end
      self.rewind
      return @index
    end

    def compute_index_list
      @index = Hash.new {|h,k| h[k] = {} }
      # start at the beginning.
      self.rewind
      # fast forward to the first spectrum or chromatograph
      buffer = ''
      while !self.eof
        buffer += self.read(1024)
        if start_pos = buffer =~ MzML::RGX::DATA_START
          self.seek start_pos
          break
        end
      end

      # for each particular entity start to fill in the index
      buffer = ''
      rgx_start = /<(spectrum|chromatogram)\s.*id=["']([^"']+)["']/
      while !self.eof
        buffer += self.read(1024)
        if start_pos = buffer =~ rgx_start
          start_pos = self.pos - buffer.length + start_pos
          @index[$1.to_sym][$2] = start_pos
          buffer = ''
        end
      end
      return @index
    end

    def parse_next
      buffer = ''
      while(!self.eof)
        if end_pos = buffer =~ MzML::RGX::DATA_END
          extra_content = buffer.slice!((end_pos + $1.length)..-1)
          self.pos -= (extra_content.length)
          break
        end
        buffer += self.read(1024)
      end
      return Nokogiri::XML.parse(buffer).root
    end
  end
end


text:
package CXGN::BrAPI::v2::CallSets;

use Moose;
use Data::Dumper;
use SGN::Model::Cvterm;
use CXGN::Genotype::Search;
use JSON;
use CXGN::BrAPI::FileResponse;
use CXGN::BrAPI::Pagination;
use CXGN::BrAPI::JSONResponse;

extends 'CXGN::BrAPI::v2::Common';

sub search {
    my $self = shift;
    my $inputs = shift;
    my $c = $self->context;
    my $page_size = $self->page_size;
    my $page = $self->page;
    my $status = $self->status;
    my $sample_ids = $inputs->{sampleDbId} || ($inputs->{sampleDbIds} || ());
    my $sample_names = $inputs->{sampleName} || ($inputs->{sampleNames} || ());
    my $variantset_ids = $inputs->{variantSetDbId} || ($inputs->{variantSetDbIds} || ());
    my $study_ids = $inputs->{studyDbId} || ($inputs->{studyDbIds} || ());
    my $callset_ids = $inputs->{callSetDbId} || ($inputs->{callSetDbIds} || ());
    my $callset_names = $inputs->{callSetName} || ($inputs->{callSetNames} || ());
    my $germplasm_ids = $inputs->{germplasmDbId} || ($inputs->{germplasmDbIds} || ());
    my $germplasm_names = $inputs->{germplasmName} || ($inputs->{germplasmNames} || ());

    my @trial_ids;
    my @protocol_ids;
    my @accession_ids;
    my @callset_names;

    if ($callset_names){
        push @callset_names, @{$callset_names};
    }
    if ($sample_names){
        push @callset_names, @{$sample_names};
    }
    if ($study_ids){
        push @trial_ids, @{$study_ids};
    }
    if ($callset_ids){
        push @accession_ids, @{$callset_ids};
    }
    if ($sample_ids){
        push @accession_ids, @{$sample_ids};
    }
    if ($variantset_ids){
        foreach ( @{$variantset_ids} ){
            my @ids = split /p/, $_;
            push @trial_ids, $ids[0] ? $ids[0] : ();
            push @protocol_ids, $ids[1] ? $ids[1] : ();
        }
    }

    if (scalar @trial_ids == 0){
        my $trial_search = CXGN::Trial::Search->new({
            bcs_schema=>$self->bcs_schema,
            trial_design_list=>['genotype_data_project']
        });
        my ($data, $total_count) = $trial_search->search();

        foreach (@$data){
            push @trial_ids, $_->{trial_id};
        }
    }

    my $genotypes_search = CXGN::Genotype::Search->new({
        trial_list=>\@trial_ids,
        bcs_schema=>$self->bcs_schema,
        people_schema => $self->people_schema(),
        cache_root=>$c->config->{cache_file_path},
        genotypeprop_hash_select=>['DS', 'GT', 'NT'],
        accession_list=>\@accession_ids,
        protocol_id_list=>\@protocol_ids,
    });

    my $file_handle = $genotypes_search->get_cached_file_search_json($c->config->{cluster_shared_tempdir}, 1); #Metadata only returned

    my %geno;
    my @variantsets;
    my @studies;
    my $passes_search;
    my $start_index = $page*$page_size;
    my $end_index = $page*$page_size + $page_size - 1;
    my $counter = 0;
    my @data;

    open my $fh, "<&", $file_handle or die "Can't open output file: $!";
    my $header_line = <$fh>;

    while( <$fh> ) {
        my $gt = decode_json $_;

        $passes_search = 1;

        if ( $germplasm_ids && !grep { $_ eq $gt->{germplasmDbId}} @{$germplasm_ids} ) { $passes_search = 0;};
        if ( $germplasm_names && !grep { $_ eq $gt->{germplasmName}} @{$germplasm_names} ) { $passes_search = 0;};
        if ( scalar(@callset_names)>0 && !grep { $_ eq $gt->{stock_name}} @callset_names ) { $passes_search = 0;};

        if ($passes_search){

            if (! exists($geno{$gt->{stock_id}})){
                @variantsets = ();
                @studies = ();
            }
            my $variantset = $gt->{genotypingDataProjectDbId} . "p" . $gt->{analysisMethodDbId};
            push @variantsets, $variantset if !grep{/^$variantset$/}@variantsets;
            push @studies, qq|$gt->{genotypingDataProjectDbId}| if !grep{/^$gt->{genotypingDataProjectDbId}$/}@studies;

            $geno{$gt->{stock_id}} = {
                    callSetName => $gt->{stock_name},
                    germplasmDbId => $gt->{germplasmDbId},
                    variantSetDbIds=>[@variantsets],
                    studyDbIds=>[@studies],
            };
        }
    }

    foreach my $genoid (keys %geno) {
        if ($counter >= $start_index && $counter <= $end_index) {
            push @data, {
                additionalInfo=>{germplasmDbId=>qq|$geno{$genoid}{germplasmDbId}|},
                callSetDbId=>qq|$genoid|,
                callSetName=>qq|$geno{$genoid}{callSetName}|,
                created=>undef,
                sampleDbId=>qq|$genoid|,
                studyDbId=>$geno{$genoid}{studyDbIds},
                updated=>undef,
                variantSetDbIds=>$geno{$genoid}{variantSetDbIds},
            };
        }
        $counter++;
    }

    if (scalar @data < 1) { @data = ""};
    my %result = (data => \@data);
    my @data_files;
    my $pagination = CXGN::BrAPI::Pagination->pagination_response($counter,$page_size,$page);
    return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'CallSets result constructed');
}

sub detail {
    my $self = shift;
    my $inputs = shift;
    my $c = $self->context;
    my $page_size = $self->page_size;
    my $page = $self->page;
    my $status = $self->status;
    my $callset_id = $inputs->{callset_id};
    my $sep_phased = $inputs->{sep_phased};
    my $sep_unphased = $inputs->{sep_unphased};
    my $unknown_string = $inputs->{unknown_string};
    my $expand_homozygotes = $inputs->{expand_homozygotes};

    if ($sep_phased || $sep_unphased || $expand_homozygotes || $unknown_string){
        push @$status, {'error' => 'The following parameters are not implemented: expandHomozygotes, unknownString, sepPhased, sepUnphased'};
    }

    my $genotypes_search = CXGN::Genotype::Search->new({
        bcs_schema=>$self->bcs_schema,
        people_schema => $self->people_schema(),
        cache_root=>$c->config->{cache_file_path},
        accession_list=>[$callset_id],
        genotypeprop_hash_select=>['DS', 'GT', 'NT'],
        protocolprop_top_key_select=>[],
        protocolprop_marker_hash_select=>[]
    });
    my $file_handle = $genotypes_search->get_cached_file_search_json($c->config->{cluster_shared_tempdir}, 1);

    my $counter = 0;

    open my $fh, "<&", $file_handle or die "Can't open output file: $!";
    my $header_line = <$fh>;
    my @data;
    my %geno;
    my @variantsets;
    my @studies;

    while( <$fh> ) {
        my $gt = decode_json $_;

        if (! exists($geno{$gt->{stock_id}})){
            @variantsets = ();
            @studies = ();
        }
        my $variantset = $gt->{genotypingDataProjectDbId} . "p" . $gt->{analysisMethodDbId};
        push @variantsets, $variantset if !grep{/^$variantset$/}@variantsets;
        push @studies, qq|$gt->{genotypingDataProjectDbId}| if !grep{/^$gt->{genotypingDataProjectDbId}$/}@studies;

        $geno{$gt->{stock_id}} = {
                callSetName => $gt->{stock_name},
                germplasmDbId => $gt->{germplasmDbId},
                variantSetDbIds=>\@variantsets,
                studyDbIds=>[@studies],
        };
    }

    foreach my $genoid (keys %geno) {
        push @data, {
            additionalInfo=>{germplasmDbId=>qq|$geno{$genoid}{germplasmDbId}|},
            callSetDbId=>qq|$genoid|,
            callSetName=>qq|$geno{$genoid}{callSetName}|,
            created=>undef,
            sampleDbId=>qq|$genoid|,
            studyDbId=>$geno{$genoid}{studyDbIds},
            updated=>undef,
            variantSetDbIds=>$geno{$genoid}{variantSetDbIds},
        };
        $counter++;
    }

    if (scalar @data < 1) { @data = {}; };
    my @data_files;
    my $pagination = CXGN::BrAPI::Pagination->pagination_response($counter,$page_size,$page);
    return CXGN::BrAPI::JSONResponse->return_success(@data, $pagination, \@data_files, $status, 'CallSets detail result constructed');
}

sub calls {
    my $self = shift;
    my $inputs = shift;
    my $c = $self->context;
    my $page_size = $self->page_size;
    my $page = $self->page;
    my $status = $self->status;
    my @callset_id = $inputs->{callset_id};
    my $sep_phased = $inputs->{sep_phased};
    my $sep_unphased = $inputs->{sep_unphased};
    my $unknown_string = $inputs->{unknown_string};
    my $expand_homozygotes = $inputs->{expand_homozygotes};
    my $data_format = 'json'; # $inputs->{format};
    my $file_path = $inputs->{file_path};
    my $uri = $inputs->{file_uri};

    if ($sep_phased || $sep_unphased || $expand_homozygotes || $unknown_string){
        push @$status, { 'error' => 'The following parameters are not implemented: expandHomozygotes, unknownString, sepPhased, sepUnphased' };
    }

    my @data_files;
    my %result;

    if ($data_format ne 'json' && $data_format ne 'tsv' && $data_format ne 'csv') {
        push @$status, { 'error' => 'Unsupported Format Given. Supported values are: json, tsv, csv' };
    }

    my $genotypes_search = CXGN::Genotype::Search->new({
        bcs_schema=>$self->bcs_schema,
        people_schema => $self->people_schema(),
        cache_root=>$c->config->{cache_file_path},
        accession_list=>[@callset_id],
        genotypeprop_hash_select=>['DS', 'GT', 'NT'],
        protocolprop_top_key_select=>[],
        protocolprop_marker_hash_select=>[],
    });
    my $file_handle = $genotypes_search->get_cached_file_search_json($c->config->{cluster_shared_tempdir}, 0);

    my $start_index = $page*$page_size;
    my $end_index = $page*$page_size + $page_size - 1;
    my $counter = 0;

    open my $fh, "<&", $file_handle or die "Can't open output file: $!";
    my $header_line = <$fh>;
    my $marker_objects = decode_json $header_line;

    my @data;
    my @scores;
    while (my $gt_line = <$fh>) {
        my $gt = decode_json $gt_line;
        my $genotype = $gt->{selected_genotype_hash};
        my @ordered_refmarkers = sort keys(%$genotype);
        my $genotypeprop_id = $gt->{markerProfileDbId};

        foreach my $m (@ordered_refmarkers) {
            if ($counter >= $start_index && $counter <= $end_index) {
                my $geno = '';
                if (exists($genotype->{$m}->{'NT'}) && defined($genotype->{$m}->{'NT'})){
                    $geno = $genotype->{$m}->{'NT'};
                }
                elsif (exists($genotype->{$m}->{'GT'}) && defined($genotype->{$m}->{'GT'})){
                    $geno = $genotype->{$m}->{'GT'};
                }
                elsif (exists($genotype->{$m}->{'DS'}) && defined($genotype->{$m}->{'DS'})){
                    $geno = $genotype->{$m}->{'DS'};
                }
                push @scores, {
                    additionalInfo=>undef,
                    variantName=>qq|$m|,
                    variantDbId=>qq|$m|,
                    callSetDbId=>qq|$gt->{stock_id}|,
                    callSetName=>qq|$gt->{stock_name}|,
                    genotype=>{values=>$geno},
                    genotype_likelihood=>undef,
                    phaseSet=>undef,
                };
            }
            $counter++;
        }
    }

    my @scores_seen;
    if (!$data_format || $data_format eq 'json' ){

        %result = ( data=>\@scores,
            expandHomozygotes=>undef,
            sepPhased=>undef,
            sepUnphased=>undef,
            unknownString=>undef);

    } elsif ($data_format eq 'tsv' || $data_format eq 'csv' || $data_format eq 'xls') {

        # my @data = (['marker', 'markerprofileDbId', 'genotype'], @scores);

        # my $file_response = CXGN::BrAPI::FileResponse->new({
        #     absolute_file_path => $file_path,
        #     absolute_file_uri => $inputs->{main_production_site_url}.$uri,
        #     format => $data_format,
        #     data => \@data
        # });
        # @data_files = $file_response->get_datafiles();
    }

    my $pagination = CXGN::BrAPI::Pagination->pagination_response($counter,$page_size,$page);
    return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'Markerprofiles allelematrix result constructed');
}

1;


text:
require 'test_helper'

module Tilia
  module DavAcl
    module PrincipalBackend
      class SequelSqliteTest < Minitest::Test
        include AbstractSequelTest

        def sequel
          db = TestUtil.sqlite
          db.run('CREATE TABLE principals (id INTEGER PRIMARY KEY ASC, uri TEXT, email VARCHAR(80), displayname VARCHAR(80))')
          db.run('INSERT INTO principals VALUES (1, "principals/user","user@example.org","User")')
          db.run('INSERT INTO principals VALUES (2, "principals/group","group@example.org","Group")')

          db.run(<<SQL
CREATE TABLE groupmembers (
  id INTEGER PRIMARY KEY ASC,
  principal_id INT,
  member_id INT,
  UNIQUE(principal_id, member_id)
)
SQL
                )
          db.run('INSERT INTO groupmembers (principal_id,member_id) VALUES (2,1)')

          db
        end
      end
    end
  end
end


text:
/* TEMPLATE GENERATED TESTCASE FILE
Filename: CWE400_Resource_Exhaustion__connect_socket_for_loop_54e.c
Label Definition File: CWE400_Resource_Exhaustion.label.xml
Template File: sources-sinks-54e.tmpl.c
*/
/*
 * @description
 * CWE: 400 Resource Exhaustion
 * BadSource: connect_socket Read data using a connect socket (client side)
 * GoodSource: Assign count to be a relatively small number
 * Sinks: for_loop
 *    GoodSink: Validate count before using it as the loop variant in a for loop
 *    BadSink : Use count as the loop variant in a for loop
 * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files
 *
 * */

#include "std_testcase.h"

#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#include <direct.h>
#pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */
#define CLOSE_SOCKET closesocket
#else /* NOT _WIN32 */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define CLOSE_SOCKET close
#define SOCKET int
#endif

#define TCP_PORT 27015
#define IP_ADDRESS "127.0.0.1"
#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)

#ifndef OMITBAD

void CWE400_Resource_Exhaustion__connect_socket_for_loop_54e_badSink(int count)
{
    {
        size_t i = 0;
        /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */
        for (i = 0; i < (size_t)count; i++)
        {
            printLine("Hello");
        }
    }
}

#endif /* OMITBAD */

#ifndef OMITGOOD

/* goodG2B uses the GoodSource with the BadSink */
void CWE400_Resource_Exhaustion__connect_socket_for_loop_54e_goodG2BSink(int count)
{
    {
        size_t i = 0;
        /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */
        for (i = 0; i < (size_t)count; i++)
        {
            printLine("Hello");
        }
    }
}

/* goodB2G uses the BadSource with the GoodSink */
void CWE400_Resource_Exhaustion__connect_socket_for_loop_54e_goodB2GSink(int count)
{
    {
        size_t i = 0;
        /* FIX: Validate count before using it as the for loop variant */
        if (count > 0 && count <= 20)
        {
            for (i = 0; i < (size_t)count; i++)
            {
                printLine("Hello");
            }
        }
    }
}

#endif /* OMITGOOD */


text:
<!DOCTYPE html>

    <meta charset="utf-8">
    <title>Example 1</title>

Hello World

Welcome to HTML & CSS, basics of coding class.

Javascript in action →
<button style="display:inline-block;" id="btn">Rotate</button>


text:
Hur många är listade som avlidna ?

    SELECT COUNT(*), Status FROM CREW WHERE Status = 0;

Lista antal utav hela besättningen.

    SELECT COUNT(*), crewID FROM Crew;

Kunna söka på för en viss rang?

    SELECT * FROM Crew WHERE  Rank = 1;

Hur många som tillhör olika typer av avdelningar.

    SELECT COUNT(*), dept FROM Crew GROUP BY dept;

Hur många av olika rang.

    SELECT COUNT(*), rank FROM Crew GROUP BY rank;

Lista alla som tjänar mer än en viss (valfri) summa.

SELECT firstName, lastName, rank, rank.salary FROM Crew INNER JOIN Rank ON crew.rank = rank.rankID WHERE (rank.salary > 22000);


text:
package com.azure.resourcemanager.monitor;

import com.azure.core.http.HttpPipeline;
import com.azure.core.http.rest.PagedIterable;
import com.azure.resourcemanager.monitor.models.ActionGroup;
import com.azure.resourcemanager.test.utils.TestUtilities;
import com.azure.core.management.Region;
import com.azure.core.management.profile.AzureProfile;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ActionGroupsTests extends MonitorManagementTest {
    private String rgName = "";

    @Override
    protected void initializeClients(HttpPipeline httpPipeline, AzureProfile profile) {
        rgName = generateRandomResourceName("jMonitor_", 18);

        super.initializeClients(httpPipeline, profile);
    }

    @Override
    protected void cleanUpResources() {
        resourceManager.resourceGroups().beginDeleteByName(rgName);
    }

    @Test
    public void canCRUDActionGroups() throws Exception {

        ActionGroup ag =
            monitorManager
                .actionGroups()
                .define("simpleActionGroup")
                .withNewResourceGroup(rgName, Region.AUSTRALIA_SOUTHEAST)
                .defineReceiver("first")
                .withPushNotification("azurepush@outlook.com")
                .withEmail("justemail@outlook.com")
                .withSms("1", "4255655665")
                .withVoice("1", "2062066050")
                .withWebhook("https://www.rate.am")
                .attach()
                .defineReceiver("second")
                .withEmail("secondemail@outlook.com")
                .withWebhook("https://www.spyur.am")
                .attach()
                .create();
        Assertions.assertNotNull(ag);
        Assertions.assertEquals("simpleAction", ag.shortName());
        Assertions.assertNotNull(ag.pushNotificationReceivers());
        Assertions.assertEquals(1, ag.pushNotificationReceivers().size());
        Assertions.assertNotNull(ag.smsReceivers());
        Assertions.assertEquals(1, ag.smsReceivers().size());
        Assertions.assertNotNull(ag.voiceReceivers());
        Assertions.assertEquals(1, ag.voiceReceivers().size());
        Assertions.assertNotNull(ag.emailReceivers());
        Assertions.assertEquals(2, ag.emailReceivers().size());
        Assertions.assertNotNull(ag.webhookReceivers());
        Assertions.assertEquals(2, ag.webhookReceivers().size());
        Assertions.assertTrue(ag.emailReceivers().get(0).name().startsWith("first"));
        Assertions.assertTrue(ag.emailReceivers().get(1).name().startsWith("second"));

        ag
            .update()
            .defineReceiver("third")
            .withWebhook("https://www.news.am")
            .attach()
            .updateReceiver("first")
            .withoutSms()
            .parent()
            .withoutReceiver("second")
            .apply();

        Assertions.assertEquals(2, ag.webhookReceivers().size());
        Assertions.assertEquals(1, ag.emailReceivers().size());
        Assertions.assertEquals(0, ag.smsReceivers().size());

        ActionGroup agGet = monitorManager.actionGroups().getById(ag.id());
        Assertions.assertEquals("simpleAction", agGet.shortName());
        Assertions.assertEquals(2, agGet.webhookReceivers().size());
        Assertions.assertEquals(1, agGet.emailReceivers().size());
        Assertions.assertEquals(0, agGet.smsReceivers().size());

        monitorManager
            .actionGroups()
            .enableReceiver(agGet.resourceGroupName(), agGet.name(), agGet.emailReceivers().get(0).name());

        PagedIterable<ActionGroup> agListByRg = monitorManager.actionGroups().listByResourceGroup(rgName);
        Assertions.assertNotNull(agListByRg);
        Assertions.assertEquals(1, TestUtilities.getSize(agListByRg));

        PagedIterable<ActionGroup> agList = monitorManager.actionGroups().list();
        Assertions.assertNotNull(agListByRg);
        Assertions.assertTrue(TestUtilities.getSize(agListByRg) > 0);

        monitorManager.actionGroups().deleteById(ag.id());
        agListByRg = monitorManager.actionGroups().listByResourceGroup(rgName);
        Assertions.assertEquals(0, TestUtilities.getSize(agListByRg));

        resourceManager.resourceGroups().beginDeleteByName(rgName);
    }
}


text:
package com.intellij.java.codeInsight.editorActions;

import com.intellij.codeInsight.editorActions.JavadocTypedHandler;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * @author Denis Zhdanov
 */
public class JavadocTypedHandlerTest {

  private static final String CARET_MARKER = "<caret>";

  @Test
  public void correctEmptyTagStart() {
    doTest("<first></first><second><caret>", "second");
  }

  @Test
  public void standaloneBracket() {
    doTest("asdf ><caret>", null);
  }

  @Test
  public void emptyElement() {
    doTest("<tag/><caret>", null);
  }

  @Test
  public void closingTag() {
    doTest("<tag></tag><caret>", null);
  }

  @Test
  public void startTagOnNewLine() {
    doTest("<t\nag><caret>", null);
  }

  @Test
  public void tagWithAttribute() {
    doTest("<a href='www'><caret>", "a");
  }

  private static void doTest(String text, String expected) {
    StringBuilder normalized = new StringBuilder();
    int offset = text.indexOf(CARET_MARKER);
    normalized.append(text, 0, offset);
    normalized.append(text.substring(offset + CARET_MARKER.length()));
    CharSequence actual = JavadocTypedHandler.getTagName(normalized.toString(), offset);
    assertEquals(expected, actual);
  }
}


text:
package pipe.controllers;

import pipe.historyActions.annotation.ChangeAnnotationText;
import uk.ac.imperial.pipe.models.petrinet.Annotation;

import javax.swing.event.UndoableEditListener;

/**
 * Controller for the annotation model
 */
public final class AnnotationController extends AbstractPetriNetComponentController<Annotation> {
    /**
     * Constructor
     * @param component an annotation to control
     * @param listener listener for undo events
     */
    public AnnotationController(Annotation component, UndoableEditListener listener) {
        super(component, listener);
    }

    /**
     *
     * @return annotation text
     */
    public String getText() {
        return component.getText();
    }

    /**
     * Changes the annotations text.
     * Creates an undoable event
     * @param text new text for the annotation
     */
    public void setText(String text) {
        String oldText = component.getText();
        component.setText(text);
        registerUndoableEdit(new ChangeAnnotationText(component, oldText, text));
    }
}


text:
from django.test import TestCase

from feedme.models import Feed, FeedItem

class FeedMeTestCase(TestCase):

    def test_update_feed(self):
        feed = Feed(url='http://derek.stegelman.com/blog/feed/')
        feed.save()
        feed.update(force=True)
        self.assertTrue(FeedItem.objects.all().count(), 10)

    def test_me(self):
        self.assertTrue(True, True)