text:
<?php

namespace PHPExiftool\Driver\Tag\MXF;

use JMS\Serializer\Annotation\ExclusionPolicy;
use PHPExiftool\Driver\AbstractTag;

/**
 * @ExclusionPolicy("all")
 */
class InputSegmentCount extends AbstractTag
{

    protected $Id = '060e2b34.0101.0101.05300504.00000000';

    protected $Name = 'InputSegmentCount';

    protected $FullName = 'MXF::Main';

    protected $GroupName = 'MXF';

    protected $g0 = 'MXF';

    protected $g1 = 'MXF';

    protected $g2 = 'Video';

    protected $Type = 'int32s';

    protected $Writable = false;

    protected $Description = 'Input Segment Count';
}


text:
The sample code for this chapter contains this solution:

* CameraSample (CameraCaptureUI, capability)
* MapSample (MapControl, SplitView, Geolocator)
* SensorSample (Accelerometer, Compass, Gyrometer, Inclinometer, LightSensor, OrientationSensor)
* RollingMarble (Accelerometer)

All the samples of this chapter need Windows 10 and Visual Studio 2017.

To build and run the Windows 10 samples, please install:

* Visual Studio 2017 Update 5 with the .NET Core workload **Universal Windows Platform Development**.

For code comments and issues please check [Professional C#'s GitHub Repository](https://github.com/ProfessionalCSharp/ProfessionalCSharp7)

Please check my blog [csharp.christiannagel.com](https://csharp.christiannagel.com "csharp.christiannagel.com") for additional information for topics covered in the book.

Thank you!

redpajama_set_name: RedPajamaGithub


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IronFoundry.Warden.Tasks
{
    public class CommandResult
    {
        public int ExitCode { get; set; }
        public string StdOut { get; set; }
        public string StdErr { get; set; }
    }

}


text:
module Selfcaster
  VERSION = "0.0.1"
end


text:
layout: default
title: CAS - Ehcache Ticket Registry
category: Ticketing
---

# Ehcache v3 Ticket Registry

Ehcache 3.x integration is enabled by including the following dependency in the WAR overlay:

```xml
<dependency>
     <groupId>org.apereo.cas</groupId>
     <artifactId>cas-server-support-ehcache3-ticket-registry</artifactId>
     <version>${cas.version}</version>
</dependency>
```

This registry stores tickets using the [Ehcache 3.x](http://ehcache.org/) caching library
and [an optional Terracotta cluster](https://www.ehcache.org/documentation/3.3/clustered-cache.html).

## In-memory store with disk persistence

Ehcache 3.x doesn't support distributing caching without Terracotta so using it without pointing at a Terracotta
server or cluster doesn't support using more than one CAS server at a time, but the registry should survive restarts due
to the disk persistence.

### Terracotta Clustering

By pointing this Ehcache module at a Terracotta server then multiple CAS servers can share tickets. CAS uses `autocreate`
to create the Terracotta cluster configuration. An easy way to run a Terracotta server is to use the [docker container](https://github.com/Terracotta-OSS/docker).

```bash
docker run --rm --name tc-server -p 9410:9410 -d \
 --env OFFHEAP_RESOURCE1_NAME=main \
 --env OFFHEAP_RESOURCE2_NAME=extra \
 --env OFFHEAP_RESOURCE1_SIZE=256 \
 --env OFFHEAP_RESOURCE2_SIZE=16 \
terracotta/terracotta-server-oss:5.6.4
```

Running a Terracotta cluster on Kubernetes can be done easily using the Terracotta [helm chart](https://github.com/helm/charts/tree/master/stable/terracotta).

#### Configuration

To see the relevant list of CAS properties, please [review this guide](../configuration/Configuration-Properties.html#ehcache-3-ticket-registry).
CAS currently doesn't support or require an XML configuration to configure Ehcache.

### Eviction Policy

Ehcache can be configured as "eternal" in which case CAS's regular cleaning process will remove expired tickets. If the
eternal property is set to false then storage timeouts will be set based on the metadata for the individual caches.

# Ehcache v2 Ticket Registry

Due to the relatively unsupported status of the Ehcache 2.x code base, this module is deprecated and will likely be
removed in a future CAS release. Unlike the Ehcache 3.x library, it can replicate directly between CAS servers without
needing an external cache cluster (e.g. Terracotta in Ehcache 3.x).

Usage

This feature is deprecated and is scheduled to be removed in the future. If you can, consider using
the Ehcache v3 ticket registry functionality in CAS to handle this integration.

Ehcache integration is enabled by including the following dependency in the WAR overlay:

```xml
<dependency>
     <groupId>org.apereo.cas</groupId>
     <artifactId>cas-server-support-ehcache-ticket-registry</artifactId>
     <version>${cas.version}</version>
</dependency>
```

This registry stores tickets using [Ehcache](http://ehcache.org/) version 2.x library.

## Distributed Cache

Distributed caches are recommended for HA architectures since they offer fault tolerance in the ticket storage
subsystem. A single cache instance is created to house all types of tickets, and is synchronously replicated
across the cluster of nodes that are defined in the configuration.

### RMI Replication

Ehcache supports [RMI](https://docs.oracle.com/javase/tutorial/rmi/index.html)
replication for distributed caches composed of two or more nodes. To learn more about RMI
replication with Ehcache, [see this resource](http://ehcache.org/documentation/user-guide/rmi-replicated-caching).

#### Configuration

To see the relevant list of CAS properties, please [review this guide](../configuration/Configuration-Properties.html#ehcache-ticket-registry).

The Ehcache configuration for `ehcache-replicated.xml` mentioned in the config follows.
Note that `${ehcache.otherServer}` would be replaced by a system property: `-Dehcache.otherserver=cas2`.

```xml
<ehcache name="ehCacheTicketRegistryCache"
    updateCheck="false"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

    <diskStore path="java.io.tmpdir/cas"/>

    <!-- Automatic Peer Discovery
    <cacheManagerPeerProviderFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
    properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446, timeToLive=32"
    propertySeparator="," />
    -->

    <!-- Manual Peer Discovery -->
    <cacheManagerPeerProviderFactory
        class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
        properties="peerDiscovery=manual,rmiUrls=//${ehcache.otherServer}:41001/proxyGrantingTicketsCache| \
            //${ehcache.otherServer}:41001/ticketGrantingTicketsCache|//${ehcache.otherServer}:41001/proxyTicketsCache| \
            //${ehcache.otherServer}:41001/oauthCodesCache|//${ehcache.otherServer}:41001/samlArtifactsCache| \
            //${ehcache.otherServer}:41001/oauthDeviceUserCodesCache|//${ehcache.otherServer}:41001/samlAttributeQueryCache| \
            //${ehcache.otherServer}:41001/oauthAccessTokensCache|//${ehcache.otherServer}:41001/serviceTicketsCache| \
            //${ehcache.otherServer}:41001/oauthRefreshTokensCache|//${ehcache.otherServer}:41001/transientSessionTicketsCache| \
            //${ehcache.otherServer}:41001/oauthDeviceTokensCache" />

    <cacheManagerPeerListenerFactory
        class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
        properties="port=41001,remoteObjectPort=41002" />
</ehcache>
```

### Eviction Policy

Ehcache manages the internal eviction policy of cached objects via the idle and alive settings.
These settings control the general policy of the cache that is used to store various ticket types. In general,
you need to ensure the cache is alive long enough to support the individual expiration policy of tickets, and let
CAS clean the tickets as part of its own cleaner.

### Troubleshooting Guidelines

* You will need to ensure that network communication across CAS nodes is allowed and no firewall or other component
 is blocking traffic.

* If you are running this on a server with active firewalls, you will probably need to specify
a fixed `remoteObjectPort`, within the `cacheManagerPeerListenerFactory`.
* Depending on environment settings and version of Ehcache used, you may also have to adjust the
`shared` setting .
* Ensure that each cache manager specified a name that matches the Ehcache configuration itself.
* You may also need to adjust your expiration policy to allow for a larger time span, specially
for service tickets depending on network traffic and communication delay across CAS nodes particularly
in the event that a node is trying to join the cluster.


text:
<?php

namespace Utill\MQ\Factory;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class FactoryServiceLoginLog  implements FactoryInterface{

    public function createService(ServiceLocatorInterface $serviceLocator) {

        $publicKey = $serviceLocator->get('servicePublicKeyReader');

        $request = $serviceLocator->get('Request');
        $uri = $request->getUri();
        $scheme = $uri->getScheme();
        $host = $uri->getHost();
        $base = sprintf('%s://%s', $scheme, $host);

        $remoteAddr = $request->getServer('REMOTE_ADDR');
        $params = $request->getContent();
        $method = $request->getMethod();

        /**
        * sends login info to message queue
        * @author Mustafa Zeynel Dağlı
        * @todo after tests ,  thif feature will be added as a service manager entity
        */
        $PageEntryLogMQ = new \Utill\MQ\LoginLogoutMQ();
        $PageEntryLogMQ->setChannelProperties(array('queue.name' => \Utill\MQ\LoginLogoutMQ::QUEUE_NAME));
        $message = new \Utill\MQ\MessageMQ\MQMessageLoginLogout();
       ;

       $message->setMessageBody(array('message' => 'Kullanıcı authentication log servis!',
                                      //'s_date'  => date('l jS \of F Y h:i:s A'),
                                      'log_datetime'  => date('Y-m-d G:i:s '),
                                      'pk' => $publicKey,
                                      'url' => $base,
                                      'path' =>$host,
                                      'method' => $method,
                                      'ip' => $remoteAddr,
                                      'params' => serialize($params),
                                      'type_id' => \Utill\MQ\MessageMQ\MQMessageLoginLogout::LOGIN_OPERATAION,
                                      'logFormat' => 'database'));
       $message->setMessageProperties(array('delivery_mode' => 2,
                                            'content_type' => 'application/json'));
       $PageEntryLogMQ->setMessage($message->setMessage());
       $PageEntryLogMQ->basicPublish();

        return true;

    }

}


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<TITLE>
org.apache.tools.ant Class Hierarchy (Apache Ant API)
</TITLE>

<META NAME="date" CONTENT="2013-03-05">

<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../../stylesheet.css" TITLE="Style">

      Overview
      Package
      Class
    Tree
      Deprecated
      Index
      Help

 PREV
 NEXT

  FRAMES
 NO FRAMES

  All Classes

Hierarchy For Package org.apache.tools.ant

Package Hierarchies:
All Packages

Class Hierarchy

java.lang.Object

org.apache.tools.ant.AntTypeDefinition
org.apache.tools.ant.ArgumentProcessorRegistry
java.lang.ClassLoader

org.apache.tools.ant.AntClassLoader (implements org.apache.tools.ant.SubBuildListener)

org.apache.tools.ant.ComponentHelper
org.apache.tools.ant.DefaultLogger (implements org.apache.tools.ant.BuildLogger)

org.apache.tools.ant.NoBannerLogger

org.apache.tools.ant.Diagnostics
org.apache.tools.ant.DirectoryScanner (implements org.apache.tools.ant.FileScanner, org.apache.tools.ant.types.ResourceFactory, org.apache.tools.ant.types.selectors.SelectorScanner)

java.util.EventObject (implements java.io.Serializable)

org.apache.tools.ant.BuildEvent

java.io.InputStream (implements java.io.Closeable)

org.apache.tools.ant.DemuxInputStream

org.apache.tools.ant.IntrospectionHelper
org.apache.tools.ant.IntrospectionHelper.Creator
org.apache.tools.ant.Location (implements java.io.Serializable)

org.apache.tools.ant.MagicNames
org.apache.tools.ant.Main (implements org.apache.tools.ant.launch.AntMain)

java.io.OutputStream (implements java.io.Closeable, java.io.Flushable)

org.apache.tools.ant.DemuxOutputStream

org.apache.tools.ant.PathTokenizer
org.apache.tools.ant.Project (implements org.apache.tools.ant.types.ResourceFactory)

org.apache.tools.ant.ProjectComponent (implements java.lang.Cloneable)

org.apache.tools.ant.Task

org.apache.tools.ant.TaskAdapter (implements org.apache.tools.ant.TypeAdapter)

org.apache.tools.ant.UnknownElement

org.apache.tools.ant.ProjectHelper
org.apache.tools.ant.ProjectHelper.OnMissingExtensionPoint
org.apache.tools.ant.ProjectHelperRepository
org.apache.tools.ant.PropertyHelper (implements org.apache.tools.ant.property.GetProperty)

org.apache.tools.ant.RuntimeConfigurable (implements java.io.Serializable)

org.apache.tools.ant.Target (implements org.apache.tools.ant.TaskContainer)

org.apache.tools.ant.ExtensionPoint

org.apache.tools.ant.TaskConfigurationChecker
java.lang.Throwable (implements java.io.Serializable)

java.lang.Exception

java.lang.RuntimeException

org.apache.tools.ant.BuildException

org.apache.tools.ant.ExitStatusException
org.apache.tools.ant.UnsupportedAttributeException
org.apache.tools.ant.UnsupportedElementException

java.lang.SecurityException

org.apache.tools.ant.ExitException

org.apache.tools.ant.XmlLogger (implements org.apache.tools.ant.BuildLogger)

Interface Hierarchy

org.apache.tools.ant.ArgumentProcessor
org.apache.tools.ant.DynamicAttribute

org.apache.tools.ant.DynamicConfigurator (also extends org.apache.tools.ant.DynamicElement)

org.apache.tools.ant.DynamicAttributeNS

org.apache.tools.ant.DynamicConfiguratorNS (also extends org.apache.tools.ant.DynamicElementNS)

org.apache.tools.ant.DynamicElement

org.apache.tools.ant.DynamicConfigurator (also extends org.apache.tools.ant.DynamicAttribute)

org.apache.tools.ant.DynamicElementNS

org.apache.tools.ant.DynamicConfiguratorNS (also extends org.apache.tools.ant.DynamicAttributeNS)

org.apache.tools.ant.DynamicObjectAttribute
org.apache.tools.ant.Evaluable
java.util.EventListener

org.apache.tools.ant.BuildListener

org.apache.tools.ant.BuildLogger
org.apache.tools.ant.SubBuildListener

org.apache.tools.ant.Executor
org.apache.tools.ant.FileScanner
org.apache.tools.ant.PropertyHelper.Delegate

org.apache.tools.ant.PropertyHelper.PropertyEvaluator
org.apache.tools.ant.PropertyHelper.PropertySetter

org.apache.tools.ant.TaskContainer
org.apache.tools.ant.TypeAdapter

      Overview
      Package
      Class
    Tree
      Deprecated
      Index
      Help

 PREV
 NEXT

  FRAMES
 NO FRAMES

  All Classes


text:
var grunt = require('grunt');

exports['mincss'] = {
  main: function(test) {
    'use strict';

    test.expect(1);

    var expect = 'body{margin:0;font-size:18px}a{color:#00f}h1{font-size:48px;font-weight:700}';
    var result = grunt.file.read('tmp/style.css');
    test.equal(expect, result, 'should concat and minify an array of css files in order using clean-css');

    test.done();
  }
};


text:
import numpy
from numpy.testing import assert_allclose, assert_equal

import theano
from theano import tensor

from blocks.bricks import Tanh, Identity
from blocks.bricks.base import application
from blocks.bricks.recurrent import SimpleRecurrent, GatedRecurrent
from blocks.bricks.attention import SequenceContentAttention
from blocks.bricks.sequence_generators import (
    SequenceGenerator, Readout, TrivialEmitter,
    SoftmaxEmitter, LookupFeedback)
from blocks.filter import VariableFilter
from blocks.graph import ComputationGraph
from blocks.initialization import Orthogonal, IsotropicGaussian, Constant
from blocks.roles import AUXILIARY

class TestEmitter(TrivialEmitter):
    @application
    def cost(self, readouts, outputs):
        """Compute MSE."""
        return ((readouts - outputs) ** 2).sum(axis=readouts.ndim - 1)

def test_sequence_generator():
    """Test a sequence generator with no contexts and continuous outputs.

    Such sequence generators can be used to model e.g. dynamical systems.

    """
    floatX = theano.config.floatX
    rng = numpy.random.RandomState(1234)

    output_dim = 1
    dim = 20
    batch_size = 30
    n_steps = 10

    transition = SimpleRecurrent(activation=Tanh(), dim=dim,
                                 weights_init=Orthogonal())
    generator = SequenceGenerator(
        Readout(readout_dim=output_dim, source_names=["states"],
                emitter=TestEmitter()),
        transition,
        weights_init=IsotropicGaussian(0.1), biases_init=Constant(0.0),
        seed=1234)
    generator.initialize()

    # Test 'cost_matrix' method
    y = tensor.tensor3('y')
    mask = tensor.matrix('mask')
    costs = generator.cost_matrix(y, mask)
    assert costs.ndim == 2
    y_test = rng.uniform(size=(n_steps, batch_size, output_dim)).astype(floatX)
    m_test = numpy.ones((n_steps, batch_size), dtype=floatX)
    costs_val = theano.function([y, mask], [costs])(y_test, m_test)[0]
    assert costs_val.shape == (n_steps, batch_size)
    assert_allclose(costs_val.sum(), 115.593, rtol=1e-5)

    # Test 'cost' method
    cost = generator.cost(y, mask)
    assert cost.ndim == 0
    cost_val = theano.function([y, mask], [cost])(y_test, m_test)
    assert_allclose(cost_val, 3.8531, rtol=1e-5)

    # Test 'AUXILIARY' variable 'per_sequence_element' in 'cost' method
    cg = ComputationGraph([cost])
    var_filter = VariableFilter(roles=[AUXILIARY])
    aux_var_name = '_'.join([generator.name, generator.cost.name,
                             'per_sequence_element'])
    cost_per_el = [el for el in var_filter(cg.variables)
                   if el.name == aux_var_name][0]
    assert cost_per_el.ndim == 0
    cost_per_el_val = theano.function([y, mask], [cost_per_el])(y_test, m_test)
    assert_allclose(cost_per_el_val, 0.38531, rtol=1e-5)

    # Test 'generate' method
    states, outputs, costs = [variable.eval() for variable in
                              generator.generate(
                                  states=rng.uniform(
                                      size=(batch_size, dim)).astype(floatX),
                                  iterate=True, batch_size=batch_size,
                                  n_steps=n_steps)]
    assert states.shape == (n_steps, batch_size, dim)
    assert outputs.shape == (n_steps, batch_size, output_dim)
    assert costs.shape == (n_steps, batch_size)
    assert_allclose(outputs.sum(), -0.33683, rtol=1e-5)
    assert_allclose(states.sum(), 15.7909, rtol=1e-5)
    # There is no generation cost in this case, since generation is
    # deterministic
    assert_allclose(costs.sum(), 0.0)

def test_integer_sequence_generator():
    """Test a sequence generator with integer outputs.

    Such sequence generators can be used to e.g. model language.

    """
    floatX = theano.config.floatX
    rng = numpy.random.RandomState(1234)

    readout_dim = 5
    feedback_dim = 3
    dim = 20
    batch_size = 30
    n_steps = 10

    transition = GatedRecurrent(dim=dim, activation=Tanh(),
                                weights_init=Orthogonal())
    generator = SequenceGenerator(
        Readout(readout_dim=readout_dim, source_names=["states"],
                emitter=SoftmaxEmitter(theano_seed=1234),
                feedback_brick=LookupFeedback(readout_dim,
                                              feedback_dim)),
        transition,
        weights_init=IsotropicGaussian(0.1), biases_init=Constant(0),
        seed=1234)
    generator.initialize()

    # Test 'cost_matrix' method
    y = tensor.lmatrix('y')
    mask = tensor.matrix('mask')
    costs = generator.cost_matrix(y, mask)
    assert costs.ndim == 2
    costs_fun = theano.function([y, mask], [costs])
    y_test = rng.randint(readout_dim, size=(n_steps, batch_size))
    m_test = numpy.ones((n_steps, batch_size), dtype=floatX)
    costs_val = costs_fun(y_test, m_test)[0]
    assert costs_val.shape == (n_steps, batch_size)
    assert_allclose(costs_val.sum(), 482.827, rtol=1e-5)

    # Test 'cost' method
    cost = generator.cost(y, mask)
    assert cost.ndim == 0
    cost_val = theano.function([y, mask], [cost])(y_test, m_test)
    assert_allclose(cost_val, 16.0942, rtol=1e-5)

    # Test 'AUXILIARY' variable 'per_sequence_element' in 'cost' method
    cg = ComputationGraph([cost])
    var_filter = VariableFilter(roles=[AUXILIARY])
    aux_var_name = '_'.join([generator.name, generator.cost.name,
                             'per_sequence_element'])
    cost_per_el = [el for el in var_filter(cg.variables)
                   if el.name == aux_var_name][0]
    assert cost_per_el.ndim == 0
    cost_per_el_val = theano.function([y, mask], [cost_per_el])(y_test, m_test)
    assert_allclose(cost_per_el_val, 1.60942, rtol=1e-5)

    # Test generate
    states, outputs, costs = generator.generate(
        iterate=True, batch_size=batch_size, n_steps=n_steps)
    cg = ComputationGraph(states + outputs + costs)
    states_val, outputs_val, costs_val = theano.function(
        [], [states, outputs, costs],
        updates=cg.updates)()
    assert states_val.shape == (n_steps, batch_size, dim)
    assert outputs_val.shape == (n_steps, batch_size)
    assert outputs_val.dtype == 'int64'
    assert costs_val.shape == (n_steps, batch_size)
    assert_allclose(states_val.sum(), -17.854, rtol=1e-5)
    assert_allclose(costs_val.sum(), 482.868, rtol=1e-5)
    assert outputs_val.sum() == 629

    # Test masks agnostic results of cost
    cost1 = costs_fun([[1], [2]], [[1], [1]])[0]
    cost2 = costs_fun([[3, 1], [4, 2], [2, 0]],
                      [[1, 1], [1, 1], [1, 0]])[0]
    assert_allclose(cost1.sum(), cost2[:, 1].sum(), rtol=1e-5)

class TestTransition(SimpleRecurrent):
    def __init__(self, attended_dim, **kwargs):
        super(TestTransition, self).__init__(**kwargs)
        self.attended_dim = attended_dim

    @application(contexts=['attended', 'attended_mask'])
    def apply(self, *args, **kwargs):
        for context in TestTransition.apply.contexts:
            kwargs.pop(context)
        return super(TestTransition, self).apply(*args, **kwargs)

    @apply.delegate
    def apply_delegate(self):
        return super(TestTransition, self).apply

    def get_dim(self, name):
        if name == 'attended':
            return self.attended_dim
        if name == 'attended_mask':
            return 0
        return super(TestTransition, self).get_dim(name)

def test_with_attention():
    """Test a sequence generator with continuous outputs and attention."""
    rng = numpy.random.RandomState(1234)

    inp_dim = 2
    inp_len = 10
    attended_dim = 3
    attended_len = 11
    batch_size = 4
    n_steps = 30

    # For values
    def rand(size):
        return rng.uniform(size=size).astype(theano.config.floatX)

    # For masks
    def generate_mask(length, batch_size):
        mask = numpy.ones((length, batch_size), dtype=theano.config.floatX)
        # To make it look like read data
        for i in range(batch_size):
            mask[1 + rng.randint(0, length - 1):, i] = 0.0
        return mask

    output_vals = rand((inp_len, batch_size, inp_dim))
    output_mask_vals = generate_mask(inp_len, batch_size)
    attended_vals = rand((attended_len, batch_size, attended_dim))
    attended_mask_vals = generate_mask(attended_len, batch_size)

    transition = TestTransition(
        dim=inp_dim, attended_dim=attended_dim, activation=Identity())
    attention = SequenceContentAttention(
        state_names=transition.apply.states, match_dim=inp_dim)
    generator = SequenceGenerator(
        Readout(
            readout_dim=inp_dim,
            source_names=[transition.apply.states[0],
                          attention.take_glimpses.outputs[0]],
            emitter=TestEmitter()),
        transition=transition,
        attention=attention,
        weights_init=IsotropicGaussian(0.1), biases_init=Constant(0),
        add_contexts=False, seed=1234)
    generator.initialize()

    # Test 'cost_matrix' method
    attended = tensor.tensor3("attended")
    attended_mask = tensor.matrix("attended_mask")
    outputs = tensor.tensor3('outputs')
    mask = tensor.matrix('mask')
    costs = generator.cost_matrix(outputs, mask,
                                  attended=attended,
                                  attended_mask=attended_mask)
    costs_vals = costs.eval({outputs: output_vals,
                             mask: output_mask_vals,
                             attended: attended_vals,
                             attended_mask: attended_mask_vals})
    assert costs_vals.shape == (inp_len, batch_size)
    assert_allclose(costs_vals.sum(), 13.5042, rtol=1e-5)

    # Test `generate` method
    results = (
        generator.generate(n_steps=n_steps, batch_size=attended.shape[1],
                           attended=attended, attended_mask=attended_mask))
    assert len(results) == 5
    states_vals, outputs_vals, glimpses_vals, weights_vals, costs_vals = (
        theano.function([attended, attended_mask], results)
        (attended_vals, attended_mask_vals))
    assert states_vals.shape == (n_steps, batch_size, inp_dim)
    assert states_vals.shape == outputs_vals.shape
    assert glimpses_vals.shape == (n_steps, batch_size, attended_dim)
    assert weights_vals.shape == (n_steps, batch_size, attended_len)
    assert costs_vals.shape == (n_steps, batch_size)
    assert_allclose(states_vals.sum(), 23.4172, rtol=1e-5)
    # There is no generation cost in this case, since generation is
    # deterministic
    assert_allclose(costs_vals.sum(), 0.0, rtol=1e-5)
    assert_allclose(weights_vals.sum(), 120.0, rtol=1e-5)
    assert_allclose(glimpses_vals.sum(), 199.2402, rtol=1e-5)
    assert_allclose(outputs_vals.sum(), -11.6008, rtol=1e-5)

def test_softmax_emitter_initial_outputs():
    emitter = SoftmaxEmitter(3)
    emitter.readout_dim = 0
    assert_equal(emitter.initial_outputs(2).eval(),
                 3 * numpy.ones((2,), dtype='int64'))

def test_sequence_generator_with_lm():
    floatX = theano.config.floatX
    rng = numpy.random.RandomState(1234)

    readout_dim = 5
    feedback_dim = 3
    dim = 20
    batch_size = 30
    n_steps = 10

    transition = GatedRecurrent(dim=dim, activation=Tanh(),
                                weights_init=Orthogonal())
    language_model = SequenceGenerator(
        Readout(readout_dim=readout_dim, source_names=["states"],
                emitter=SoftmaxEmitter(theano_seed=1234),
                feedback_brick=LookupFeedback(readout_dim, dim,
                                              name='feedback')),
        SimpleRecurrent(dim, Tanh()),
        name='language_model')
    generator = SequenceGenerator(
        Readout(readout_dim=readout_dim, source_names=["states", "lm_states"],
                emitter=SoftmaxEmitter(theano_seed=1234),
                feedback_brick=LookupFeedback(readout_dim,
                                              feedback_dim)),
        transition,
        language_model=language_model,
        weights_init=IsotropicGaussian(0.1), biases_init=Constant(0),
        seed=1234)
    generator.initialize()

    # Test 'cost_matrix' method
    y = tensor.lmatrix('y')
    y.tag.test_value = numpy.zeros((15, batch_size), dtype='int64')
    mask = tensor.matrix('mask')
    mask.tag.test_value = numpy.ones((15, batch_size))

    costs = generator.cost_matrix(y, mask)
    assert costs.ndim == 2
    costs_fun = theano.function([y, mask], [costs])
    y_test = rng.randint(readout_dim, size=(n_steps, batch_size))
    m_test = numpy.ones((n_steps, batch_size), dtype=floatX)
    costs_val = costs_fun(y_test, m_test)[0]
    assert costs_val.shape == (n_steps, batch_size)
    assert_allclose(costs_val.sum(), 483.153, rtol=1e-5)

    # Test 'cost' method
    cost = generator.cost(y, mask)
    assert cost.ndim == 0
    cost_val = theano.function([y, mask], cost)(y_test, m_test)
    assert_allclose(cost_val, 16.105, rtol=1e-5)

    # Test 'AUXILIARY' variable 'per_sequence_element' in 'cost' method
    cg = ComputationGraph([cost])
    var_filter = VariableFilter(roles=[AUXILIARY])
    aux_var_name = '_'.join([generator.name, generator.cost.name,
                             'per_sequence_element'])
    cost_per_el = [el for el in var_filter(cg.variables)
                   if el.name == aux_var_name][0]
    assert cost_per_el.ndim == 0
    cost_per_el_val = theano.function([y, mask], [cost_per_el])(y_test, m_test)
    assert_allclose(cost_per_el_val, 1.61051, rtol=1e-5)

    # Test generate
    states, outputs, lm_states, costs = generator.generate(
        iterate=True, batch_size=batch_size, n_steps=n_steps)
    cg = ComputationGraph([states, outputs, costs])
    states_val, outputs_val, costs_val = theano.function(
        [], [states, outputs, costs],
        updates=cg.updates)()
    assert states_val.shape == (n_steps, batch_size, dim)
    assert outputs_val.shape == (n_steps, batch_size)
    assert outputs_val.dtype == 'int64'
    assert costs_val.shape == (n_steps, batch_size)
    assert_allclose(states_val.sum(), -4.88367, rtol=1e-5)
    assert_allclose(costs_val.sum(), 486.681, rtol=1e-5)
    assert outputs_val.sum() == 627

    # Test masks agnostic results of cost
    cost1 = costs_fun([[1], [2]], [[1], [1]])[0]
    cost2 = costs_fun([[3, 1], [4, 2], [2, 0]],
                      [[1, 1], [1, 1], [1, 0]])[0]
    assert_allclose(cost1.sum(), cost2[:, 1].sum(), rtol=1e-5)


text:
package org.apache.hadoop.hbase.security.provider;

import static java.util.Objects.requireNonNull;

import java.util.Collection;
import java.util.Objects;

import net.jcip.annotations.NotThreadSafe;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseInterfaceAudience;
import org.apache.hadoop.hbase.security.User;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.security.token.Token;
import org.apache.hadoop.security.token.TokenIdentifier;
import org.apache.yetus.audience.InterfaceAudience;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Default implementation of {@link AuthenticationProviderSelector} which can choose from the
 * authentication implementations which HBase provides out of the box: Simple, Kerberos, and
 * Delegation Token authentication.
 *
 * This implementation will ignore any {@link SaslAuthenticationProvider}'s which are available
 * on the classpath or specified in the configuration because HBase cannot correctly choose which
 * token should be returned to a client when multiple are present. It is expected that users
 * implement their own {@link AuthenticationProviderSelector} when writing a custom provider.
 *
 * This implementation is not thread-safe. {@link #configure(Configuration, Collection)} and
 * {@link #selectProvider(String, User)} is not safe if they are called concurrently.
 */
@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.AUTHENTICATION)
@NotThreadSafe
public class BuiltInProviderSelector implements AuthenticationProviderSelector {
  private static final Logger LOG = LoggerFactory.getLogger(BuiltInProviderSelector.class);

  Configuration conf;
  SimpleSaslClientAuthenticationProvider simpleAuth = null;
  GssSaslClientAuthenticationProvider krbAuth = null;
  DigestSaslClientAuthenticationProvider digestAuth = null;
  Text digestAuthTokenKind = null;

  @Override
  public void configure(
      Configuration conf, Collection<SaslClientAuthenticationProvider> providers) {
    if (this.conf != null) {
      throw new IllegalStateException("configure() should only be called once");
    }
    this.conf = Objects.requireNonNull(conf);

    for (SaslClientAuthenticationProvider provider : Objects.requireNonNull(providers)) {
      final String name = provider.getSaslAuthMethod().getName();
      if (SimpleSaslAuthenticationProvider.SASL_AUTH_METHOD.getName().contentEquals(name)) {
        if (simpleAuth != null) {
          throw new IllegalStateException(
              "Encountered multiple SimpleSaslClientAuthenticationProvider instances");
        }
        simpleAuth = (SimpleSaslClientAuthenticationProvider) provider;
      } else if (GssSaslAuthenticationProvider.SASL_AUTH_METHOD.getName().equals(name)) {
        if (krbAuth != null) {
          throw new IllegalStateException(
              "Encountered multiple GssSaslClientAuthenticationProvider instances");
        }
        krbAuth = (GssSaslClientAuthenticationProvider) provider;
      } else if (DigestSaslAuthenticationProvider.SASL_AUTH_METHOD.getName().equals(name)) {
        if (digestAuth != null) {
          throw new IllegalStateException(
              "Encountered multiple DigestSaslClientAuthenticationProvider instances");
        }
        digestAuth = (DigestSaslClientAuthenticationProvider) provider;
        digestAuthTokenKind = new Text(digestAuth.getTokenKind());
      } else {
        LOG.warn("Ignoring unknown SaslClientAuthenticationProvider: {}", provider.getClass());
      }
    }
    if (simpleAuth == null || krbAuth == null || digestAuth == null) {
      throw new IllegalStateException("Failed to load SIMPLE, KERBEROS, and DIGEST authentication "
          + "providers. Classpath is not sane.");
    }
  }

  @Override
  public Pair<SaslClientAuthenticationProvider, Token<? extends TokenIdentifier>> selectProvider(
      String clusterId, User user) {
    requireNonNull(clusterId, "Null clusterId was given");
    requireNonNull(user, "Null user was given");

    // Superfluous: we don't do SIMPLE auth over SASL, but we should to simplify.
    if (!User.isHBaseSecurityEnabled(conf)) {
      return new Pair<>(simpleAuth, null);
    }

    final Text clusterIdAsText = new Text(clusterId);

    // Must be digest auth, look for a token.
    // TestGenerateDelegationToken is written expecting DT is used when DT and Krb are both present.
    // (for whatever that's worth).
    for (Token<? extends TokenIdentifier> token : user.getTokens()) {
      // We need to check for two things:
      //   1. This token is for the HBase cluster we want to talk to
      //   2. We have suppporting client implementation to handle the token (the "kind" of token)
      if (clusterIdAsText.equals(token.getService()) &&
          digestAuthTokenKind.equals(token.getKind())) {
        return new Pair<>(digestAuth, token);
      }
    }
    // Unwrap PROXY auth'n method if that's what we have coming in.
    final UserGroupInformation currentUser = user.getUGI();
    // May be null if Hadoop AuthenticationMethod is PROXY
    final UserGroupInformation realUser = currentUser.getRealUser();
    if (currentUser.hasKerberosCredentials() ||
        (realUser != null && realUser.hasKerberosCredentials())) {
      return new Pair<>(krbAuth, null);
    }
    // This indicates that a client is requesting some authentication mechanism which the servers
    // don't know how to process (e.g. there is no provider which can support it). This may be
    // a bug or simply a misconfiguration of client *or* server.
    LOG.warn("No matching SASL authentication provider and supporting token found from providers"
        + " for user: {}", user);
    return null;
  }

}


text:
package object.jackson;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;

import java.util.List;

/**
 * Created by Administrator on 2015/2/5.
 */
public class ObjectToJsonStringEncoder extends MessageToMessageEncoder<Object> {
    private ObjectMapper mapper = new ObjectMapper();
    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
        out.add(mapper.writeValueAsString(msg));
    }
}


text:
class Mes::RejectCodePolicy < Mes::Policy::Base
end


text:
import { storiesOf } from "@kadira/storybook";
import centered from "../../../.storybook/decorators/centered";

import Toggle from "./index";

const story = storiesOf("Toggles", module)
  .addDecorator(centered);

story.add(
  "Basic Toggle", () => (
    <Toggle className="toggle__item" />
  )
);

story.add(
  "Toggle Active", () => (
    <Toggle className="toggle__item--active" />
  )
);

story.add(
  "Toggle Arrow", () => (
    <Toggle className="toggle__item toggle-arrow__item" />
  )
);

story.add(
  "Toggle Active & Arrow", () => (
    <Toggle className="toggle__item--active toggle-arrow__item" />
  )
);


text:
class LabelRule:
    """
    Rules for labeling a trendline.

    Takes in an "options" dictionary with the following keys:

    name and val are the name and integer code for the label

    change_type is the type of disturbance we're looking for.  Options are:
        FD - first disturbance
        GD - greatest disturbance
        LD - longest disturbance
        None - no rule

    all other arguments are two-item lists in the format (qualifier, val).

    onset_year lets us limit the time horizon we analyze.  Qualifier options:
        = - equal to
        >= - greater than or equal to
        <= - less than or equal to

    duration lets us filter by how long a disturbance is.  Qualifier options:
        > - greater than
        < - less than

    pre_threshold lets us filter by the pre-disturbance value.
    Qualifier options:
        > - greater than
        < - less than
    """

    def __init__(self, options):

        name = options.get('name')
        if not name:
            raise ValueError('name required')
        self.name = name

        val = options.get('val')
        if not val:
            raise ValueError('val required')
        self.val = val

        change_type = options.get('change_type')
        if change_type not in ['FD', 'GD', 'LD', None]:
            raise ValueError('Invalid change_type: %s' % change_type)
        self.change_type = change_type

        onset_year = options.get('onset_year')
        duration = options.get('duration')
        pre_threshold = options.get('pre_threshold')
        for param_name, param_val in zip(
                ['onset_year', 'duration', 'pre_threshold'],
                [onset_year, duration, pre_threshold]):

            if param_val:
                if type(param_val) != list or len(param_val) != 2:
                    raise ValueError('Parameter %s - invalid value: %s' % (
                        param_name, param_val
                    ))
                else:
                    setattr(self, param_name, param_val)
            else:
                setattr(self, param_name, None)

class TrendlinePoint:
    """
    Represents a single point in a trendline.  Contains lots of data
    about the value of the point, the fitted line going through it,
    and whether or not it's a spike and/or vertex.
    """
    def __init__(self, val_raw, val_fit, eqn_fit, eqn_right, index_date,
                 index_day, spike, vertex):
        self.val_raw = val_raw
        self.val_fit = val_fit
        self.eqn_fit = eqn_fit
        self.eqn_right = eqn_right
        self.index_date = index_date
        self.index_day = index_day
        self.spike = spike
        self.vertex = vertex

    def mr_label_output(self):
        """
        Returns a dictionary in the format
        {
            '<date>_<attr>': <val>,
            ...
        }

        e.g.
        {
            '2014-07-05_spike': True,
            ...
        }

        coerces booleans to False = 0, True = 1
        """
        d = {
            'val_raw': self.val_raw,
            'val_fit': self.val_fit,
            'eqn_fit_slope': self.eqn_fit[0],
            'eqn_fit_intercept': self.eqn_fit[1],
            'eqn_right_slope': self.eqn_right[0],
            'eqn_right_intercept': self.eqn_right[1],
            'spike': 1 if self.spike else 0,
            'vertex': 1 if self.vertex else 0
        }

        # prefix all keys by date
        date = self.index_date
        return dict([
            ('%s-%s' % (date, k), v)
            for k, v in d.iteritems()
        ])

class Trendline:
    """
    Represents a processed LandTrendr trendline
    """
    def __init__(self, points):
        self.points = points

    def __unicode__(self):
        vertices = filter(lambda x: x.vertex, self.points)
        return '\n'.join([
            ' | '.join([v.index_date, str(v.val_fit)]) for v in vertices
        ])

    def __str__(self):
        return unicode(self).encode('utf-8')

    def mr_label_output(self):
        """
        Outputs a dictionary in the format:
        {
            '<date>_<attr>': <val>,
            ...
        }

        e.g.
        {
            '2014-07-05_spike': True,
            ...
        }

        for all points
        """
        out = {}
        for p in self.points:
            out.update(p.mr_label_output())
        return out

    def parse_disturbances(self):
        """
        For a given trendline, for each segment between vertices, determine
        the stats for each "disturbance" and return a list of Disturbance
        objects
        """
        import utils
        it = iter(self.points)
        left_vertex = it.next()
        for p in it:
            if not p.vertex:
                continue
            start_yr = utils.parse_date(left_vertex.index_date).year
            end_yr = utils.parse_date(p.index_date).year
            yield Disturbance(
                start_yr,
                left_vertex.val_fit,
                left_vertex.val_fit - p.val_fit,
                end_yr - start_yr
            )
            left_vertex = p

    def match_rule(self, rule):
        """
        Given a LabelRule, determine if any of the disturbances in this
        trendline match it.  If so, return the disturbance.
        """
        disturbances = self.parse_disturbances()

        # Filter by year_onset, duration, and pre_disturbance_threshold
        matching_disturbances = []
        for d in disturbances:
            match = True

            if rule.onset_year:
                qualifier, yr = rule.onset_year
                if qualifier == '=' and d.onset_year != yr:
                    match = False
                elif qualifier == '<=' and d.onset_year > yr:
                    match = False
                elif qualifier == '>=' and d.onset_year < yr:
                    match = False

            if rule.duration:
                qualifier, yr_length = rule.duration
                if qualifier == '>' and d.duration <= yr_length:
                    match = False
                elif qualifier == '<' and d.duration >= yr_length:
                    match = False

            if rule.pre_threshold:
                qualifier, threshold = rule.threshold
                if qualifier == '>' and d.initial_val <= threshold:
                    match = False
                elif qualifier == '<' and d.initial_val >= threshold:
                    match = False

            if match:
                matching_disturbances.append(d)

        # Pick winner by change type
        winner = None
        for d in matching_disturbances:
            if winner is None:
                winner = d
            else:
                if rule.change_type == 'FD':
                    if d.onset_year < winner.onset_year:
                        winner = d
                elif rule.change_type == 'GD':
                    if d.magnitude > winner.magnitude:
                        winner = d
                elif rule.change_type == 'LD':
                    if d.duration > winner.duration:
                        winner = d

        return winner

class Disturbance:
    """
    Represents a disturance with an onset_year, initial_val, magnitude, and
    duration
    """
    def __init__(self, onset_year, initial_val, magnitude, duration):
        self.onset_year = onset_year
        self.initial_val = initial_val
        self.magnitude = magnitude
        self.duration = duration


text:
import random
import re
import os
import traceback
import urllib.parse
import importlib
import pathlib

def print_notice(s):
    print('\033[92m'+s+'\033[0m\n')

def path_exists(path):
    path = pathlib.Path(path)
    if not path.exists():
        answer = input("That path doesn't exist. Create it? [y/n]").strip().lower()
        if answer=='y':
            path.mkdir(parents=True, exist_ok=True)
            return True
        else:
            return False
    else:
        return True

class Question(object):
    def __init__(self, key, question, default, validation=None):
        self.key = key
        self.question = question
        self.default = default
        self.validation = validation

    def get_default(self, values):
        if callable(self.default):
            return self.default(values)
        else:
            return self.default

    def validate(self, value):
        return self.validation is None or self.validation(value)

class Command(object):

    dev = False

    dev_question = Question('DEBUG', 'Is this installation for development?', False)

    dev_values = {
        'DB_ENGINE': 'sqlite3',
        'DB_NAME': 'db.sqlite3',
        'PYTHON_EXEC': 'python3',
        'STATIC_ROOT': 'editor/static',
        'MEDIA_ROOT': 'media',
        'PREVIEW_PATH': 'editor/static/previews',
        'PREVIEW_URL': '/static/previews/',
        'SITE_TITLE': 'Numbas development',
        'ALLOW_REGISTRATION': True,
        'DEFAULT_FROM_EMAIL': '',
    }

    questions = [
        Question('NUMBAS_PATH', 'Path of the Numbas compiler:','/srv/numbas/compiler/', validation=path_exists),
        Question('DB_ENGINE', 'Which database engine are you using? (Common options: postgres, mysql, sqlite3)', lambda v: 'sqlite3' if v['DEBUG'] else 'mysql'),
        Question('STATIC_ROOT', 'Where are static files stored?','/srv/numbas/static/', validation=path_exists),
        Question('MEDIA_ROOT', 'Where are uploaded files stored?','/srv/numbas/media/', validation=path_exists),
        Question('PREVIEW_PATH', 'Where are preview exams stored?','/srv/numbas/previews/', validation=path_exists),
        Question('PREVIEW_URL', 'Base URL of previews:','/numbas-previews/'),
        Question('PYTHON_EXEC', 'Python command:','python3'),
        Question('SITE_TITLE', 'Title of the site:','Numbas'),
        Question('ALLOW_REGISTRATION', 'Allow new users to register themselves?', True),
        Question('DEFAULT_FROM_EMAIL', 'Address to send emails from:', ''),
    ]
    db_questions = [
        Question('DB_NAME', 'Name of the database:','numbas_editor'),
        Question('DB_USER', 'Database user:', 'numbas_editor'),
        Question('DB_PASSWORD', 'Database password:', ''),
        Question('DB_HOST', 'Database host:', 'localhost'),
    ]

    sqlite_template = """DATABASES = {{
    'default': {{
        'ENGINE': 'django.db.backends.{DB_ENGINE}',
        'NAME': os.path.join(BASE_DIR, '{DB_NAME}'),
    }}
}}"""

    other_db_template = """DATABASES = {{
    'default': {{
        'ENGINE': 'django.db.backends.{DB_ENGINE}',
        'NAME': '{DB_NAME}',
        'USER': '{DB_USER}',
        'PASSWORD': '{DB_PASSWORD}',
        'HOST': '{DB_HOST}',
    }}
}}"""

    def __init__(self):
        self.written_files = []

    def handle(self):
        print_notice("This script will configure the Numbas editor up to a point where you can open it in a web browser, based on your answers to the following questions.")

        self.get_values()

        self.write_files()

        import numbas.settings
        importlib.reload(numbas.settings)

        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "numbas.settings")

        print_notice("Now we'll check that everything works properly")

        self.run_management_command('check')

        if self.get_input('Would you like to automatically set up the database now?',True):
            self.run_management_command('migrate')

        import django
        django.setup()

        self.setup_site()

        from django.contrib.auth.models import User
        superusers = User.objects.filter(is_superuser=True)
        if superusers.exists():
            if self.get_input("There's already at least one admin user.\nWould you like to create another admin user now?",False):
                self.run_management_command('createsuperuser')
        else:
            if self.get_input('Would you like to create an admin user now?',True):
                self.run_management_command('createsuperuser')

        print_notice("Done!")

        if self.values['DEBUG']:
            print_notice("Run\n  python manage.py runserver\nto start a development server at http://localhost:8000.")
        else:
            self.run_management_command('collectstatic')
            print_notice("The Numbas editor is now set up. Once you've configured your web server, it'll be ready to use at http://{}".format(self.domain))

    def setup_site(self):
        from django.contrib.sites.models import Site
        try:
            domain = Site.objects.first().domain
        except Site.DoesNotExist:
            domain = 'localhost' if self.dev else 'numbas.example.com'

        if not self.dev:
            domain = self.get_input('What domain will the site be accessed from?', domain)

        try:
            url = urllib.parse.urlparse(domain)
            self.domain = url.netloc if url.netloc else domain
        except ValueError:
            self.domain = domain

        s, created = Site.objects.get_or_create(domain=self.domain)
        s.name = self.values['SITE_TITLE']
        self.rvalues['SITE_ID'] = str(s.id)
        s.save()

        self.sub_settings(confirm_overwrite=False)

        import numbas.settings
        importlib.reload(numbas.settings)

    def get_values(self):
        self.values = {}

        self.values['SECRET_KEY'] =''.join(random.SystemRandom().choice('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)') for i in range(50))
        self.values['PWD'] = os.getcwd()

        self.get_value(self.dev_question)

        self.dev = self.values['DEBUG']

        if self.dev:
            self.values.update(self.dev_values)

        for question in self.questions:
            self.get_value(question)
            if question.key=='DB_ENGINE':
                if 'sqlite' not in self.values[question.key]:
                    for question in self.db_questions:
                        self.get_value(question)
                else:
                    self.get_value(Question('DB_NAME', 'Name of the database file:','db.sqlite3'))

        def enrep(value):
            rep = repr(value)
            if isinstance(value,str):
                rep = rep[1:-1]
            return rep

        self.values['SITE_ID'] = self.get_default_value(Question('SITE_ID','','1'))
        self.rvalues = {key: enrep(value) for key, value in self.values.items()}

    def get_default_value(self, question):
        default = question.get_default(self.values)
        if pathlib.Path('numbas', 'settings.py').exists():
            import numbas.settings
            try:
                if question.key=='DB_ENGINE':
                    default = numbas.settings.DATABASES['default']['ENGINE'].replace('django.db.backends.', '')
                elif question.key[:3]=='DB_' and question.key[3:] in numbas.settings.DATABASES['default']:
                    default = numbas.settings.DATABASES['default'][question.key[3:]]
                else:
                    try:
                        default = getattr(numbas.settings, question.key)
                    except AttributeError:
                        default = numbas.settings.GLOBAL_SETTINGS[question.key]
                    if isinstance(default,list):
                        default = default[0] if len(default)==1 else ''
            except (AttributeError,KeyError):
                pass
        return default

    def get_value(self, question):
        if self.dev and question.key in self.dev_values:
            return
        self.values[question.key] = self.get_input(question.question, self.get_default_value(question), question.validation)

    def write_files(self):

        self.sub_settings()

        if not self.values['DEBUG']:
            self.sub_file(pathlib.Path('web', 'django.wsgi'),[ (r"sys.path.append\('(.*?)'\)", 'PWD') ])

        index_subs = [
            (r"Welcome to (the Numbas editor)", 'SITE_TITLE'),
        ]
        self.sub_file(pathlib.Path('editor', 'templates', 'index_message.html'), index_subs)

        self.sub_file(pathlib.Path('editor', 'templates', 'terms_of_use_content.html'), [])

        self.sub_file(pathlib.Path('editor', 'templates', 'privacy_policy_content.html'), [])

        if len(self.written_files):
            print_notice("The following files have been written. You should look at them now to see if you need to make any more changes.")
            for f in self.written_files:
                print_notice(' * '+str(f))
            print('')

    def sub_settings(self, confirm_overwrite=True):
        def set_database(m, rvalues):
            template = self.sqlite_template if 'sqlite' in rvalues['DB_ENGINE'] else self.other_db_template
            return template.format(**rvalues)

        settings_subs = [
            (r"^DEBUG = (True)", 'DEBUG'),
            (r"'NUMBAS_PATH': '(.*?)',", 'NUMBAS_PATH'),
            (r"^STATIC_ROOT = '(static/)'", 'STATIC_ROOT'),
            (r"^MEDIA_ROOT = '(media/)'", 'MEDIA_ROOT'),
            (r"'PREVIEW_PATH': '(.*?)'", 'PREVIEW_PATH'),
            (r"'PREVIEW_URL': '(.*?)',", 'PREVIEW_URL'),
            (r"'PYTHON_EXEC': '(.*?)',", 'PYTHON_EXEC'),
            (r"^SITE_TITLE = '(.*?)'", 'SITE_TITLE'),
            (r"^DATABASES = {.*?^}", set_database),
            (r"^SECRET_KEY = '(.*?)'", 'SECRET_KEY'),
            (r"^ALLOW_REGISTRATION = (True|False)", 'ALLOW_REGISTRATION'),
            (r"^DEFAULT_FROM_EMAIL = '(.*?)'", 'DEFAULT_FROM_EMAIL'),
            (r"^SITE_ID = (\d+)", 'SITE_ID'),
        ]
        self.sub_file(pathlib.Path('numbas', 'settings.py'), settings_subs, confirm_overwrite)

    def sub_file(self, fname, subs, confirm_overwrite=True):
        if fname.exists() and confirm_overwrite:
            overwrite = self.get_input("{} already exists. Overwrite it?".format(fname),True)
            if not overwrite:
                return

        self.written_files.append(fname)

        with open(str(fname)+'.dist') as f:
            text = f.read()

        for pattern, key in subs:
            pattern = re.compile(pattern, re.MULTILINE | re.DOTALL)
            if callable(key):
                text = self.sub_fn(text, pattern, key)
            else:
                text = self.sub(text,pattern,self.rvalues[key])

        with open(fname,'w') as f:
            f.write(text)
        print("Wrote",fname)

    def sub_fn(self, source, pattern, fn):
        m = pattern.search(source)
        if not m:
            raise Exception("Didn't find {}".format(pattern.pattern))
        start, end = m.span(0)
        out = fn(m, self.rvalues)
        return source[:start]+out+source[end:]

    def sub(self, source, pattern, value):
        def fix(m):
            t = m.group(0)
            start, end = m.span(1)
            ts,te = m.span(0)
            start -= ts
            end -= ts
            return t[:start]+value+t[end:]
        if not pattern.search(source):
            raise Exception("Didn't find {}".format(pattern.pattern))
        return pattern.sub(fix, source)

    def run_management_command(self, *args):
        from django.core.management import ManagementUtility
        args = ['manage.py'] + list(args)
        utility = ManagementUtility(args)
        try:
            utility.execute()
        except SystemExit:
            pass
        print('')

    def get_input(self, question, default, validation=None):
        v = None
        try:
            while v is None:
                if isinstance(default,bool):
                    if default is not None:
                        q = question+(' [Y/n]' if default else ' [y/N]')
                    else:
                        q = question
                    t = input(q+' ').strip().lower()
                    if t=='' and default is not None:
                        v = default
                    if t=='y':
                        v = True
                    if t=='n':
                        v = False
                else:
                    if default is not None:
                        q = "{} ['{}']".format(question,str(default))
                    else:
                        q = question
                    t = input(q+' ').strip()
                    if t=='' and default is not None:
                        v = default
                    if t:
                        v = t
                if validation is not None and not validation(v):
                    v = None
        except KeyboardInterrupt:
            print('')
            raise SystemExit
        print('')
        return v

if __name__ == '__main__':
    command = Command()
    try:
        command.handle()
    except Exception as e:
        traceback.print_exc()
        print_notice("The setup script failed. Look at the error message above for a description of why.")