text:
package org.kaazing.gateway.service.broadcast;

import static java.lang.String.format;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URI;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.kaazing.gateway.service.ServiceContext;
import org.kaazing.gateway.service.ServiceFactory;
import org.kaazing.gateway.service.ServiceProperties;
import org.kaazing.gateway.util.scheduler.SchedulerProvider;
import org.kaazing.test.util.MethodExecutionTrace;

public class BroadcastServiceTest {
    @Rule
    public TestRule testExecutionTrace = new MethodExecutionTrace();

    private BroadcastService service;

    @Before
    public void setup() {
        service = (BroadcastService)ServiceFactory.newServiceFactory().newService("broadcast");
        service.setSchedulerProvider(new SchedulerProvider());
    }

    @Test
    public void testReconnect() throws Exception {
        final ServiceContext serviceContext = new TestServiceContext(service, "test-broadcast", URI.create("tcp://localhost:9880"), URI.create("tcp://localhost:9897"));

        // set up the configuration -- empty properties so the values just default
        service.setConfiguration(new Properties());

        TestBackendService backend = new TestBackendService();
        Thread t = new Thread(backend, "BackendServiceThread");
        t.start();

        service.init(serviceContext);
        service.start();

        // wait for initial connection to back-end
        CountDownLatch latch = backend.getLatch();
        if (!latch.await(5, TimeUnit.SECONDS)) {
            Assert.fail("Failed to establish connection to backend in 5 seconds");
        }

        // quit back-end
        backend.stop();
        t.join();

        // restart back-end, wait for service to connect
        backend = new TestBackendService();
        t = new Thread(backend, "BackendServiceThread2");
        t.start();

        latch = backend.getLatch();
        if (!latch.await(5, TimeUnit.SECONDS)) {
            // Default reconnect is 3 seconds, so connection should get established within 3 seconds, make it 5 to be sure
            Assert.fail("Failed to reconnect to backend in 5 seconds");
        }

        backend.stop();
        t.join();

        service.stop();
        service.destroy();
    }

    @Test
    public void testBroadcast() throws Exception {
        final TestServiceContext serviceContext = new TestServiceContext(service, "test-broadcast", URI.create("tcp://localhost:9880"), null);
        ServiceProperties serviceProperties = serviceContext.getProperties();
        serviceProperties.put("accept", "tcp://localhost:9090");

        // set up the configuration -- empty properties so the values just default
        service.setConfiguration(new Properties());

        service.init(serviceContext);
        service.start();

        TestBackendProducer producer = new TestBackendProducer();
        Thread t = new Thread(producer, "BackendProducerThread");
        t.start();

        // wait for initial connection from producer
        if (!producer.getLatch().await(5, TimeUnit.SECONDS)) {
            Assert.fail("Failed to receive connection from producer in 5 seconds");
        }

        TestClient c1 = new TestClient(1);
        TestClient c2 = new TestClient(2);
        TestClient c3 = new TestClient(3);

        Thread tc1 = new Thread(c1, "Client 1");
        Thread tc2 = new Thread(c2, "Client 2");
        Thread tc3 = new Thread(c3, "Client 3");

        tc1.start();
        tc2.start();
        tc3.start();

        if (!c1.getLatch().await(5, TimeUnit.SECONDS)) {
            Assert.fail("Client 1 failed to receive message in 5 seconds");
        }
        if (!c2.getLatch().await(5, TimeUnit.SECONDS)) {
            Assert.fail("Client 2 failed to receive message in 5 seconds");
        }
        if (!c3.getLatch().await(5, TimeUnit.SECONDS)) {
            Assert.fail("Client 3 failed to receive message in 5 seconds");
        }

        c1.stop();
        c2.stop();
        c3.stop();

        tc1.join();
        tc2.join();
        tc3.join();

        // quit back-end
        producer.stop();
        t.join();

        service.stop();
        service.destroy();
    }

    @Test
    public void testSlowConsumer() throws Exception {
        final TestServiceContext serviceContext = new TestServiceContext(service, "test-broadcast", URI.create("tcp://localhost:9880"), null);
        ServiceProperties serviceProperties = serviceContext.getProperties();
        serviceProperties.put("accept", "tcp://localhost:9090");

        // set up the configuration -- empty properties so the values just default
        Properties slowConsumerProps = new Properties();
        slowConsumerProps.setProperty("org.kaazing.gateway.server.service.broadcast.MAXIMUM_PENDING_BYTES", "40000"); // FIXME: why this number?
        service.setConfiguration(slowConsumerProps);

        service.init(serviceContext);
        service.start();

        FastTestBackendProducer producer = new FastTestBackendProducer();
        Thread t = new Thread(producer, "FastBackendProducerThread");

        SlowTestClient c1 = new SlowTestClient(1);
        Thread tc1 = new Thread(c1, "SlowClient 1");

        try {
            t.start();

            // wait for initial connection from producer
            if (!producer.getLatch().await(5, TimeUnit.SECONDS)) {
                Assert.fail("Failed to receive connection from producer in 5 seconds");
            }

            tc1.start();

            if (!c1.getLatch().await(5, TimeUnit.SECONDS)) {
                Assert.fail("SlowClient 1 failed to connect in 5 seconds");
            }

            // Wait up to 15 seconds for slow client to get killed, after that the test has timed out and failure is asserted
            tc1.join(15 * 1000);
            if (tc1.isAlive()) {
                c1.stop();
                tc1.join();
                Assert.fail("SlowClient 1 was not terminated in 30 seconds");
            }
	} finally {
            // quit back-end
            producer.stop();
            t.join();
	}

        service.stop();
        service.destroy();
    }

    private class TestBackendService implements Runnable {
        // a simple Backend that has a listener (ServerSocket) and has an API for producing a message
        private ServerSocket s;
        private boolean running = false;
        private CountDownLatch latch = new CountDownLatch(1);

        private TestBackendService() {
            try {
                s = new ServerSocket(9897);
                running = true;
            } catch (IOException ex) {
                throw new RuntimeException("Failed to create TestBackendSerivce", ex);
            }
        }

        @Override
        public void run() {
            try {
                Socket socket = s.accept();
                OutputStream os = socket.getOutputStream();

                latch.countDown(); // someone connected, count down
                while (isRunning()) {
                    // The bytes for ">|<"
                    os.write(new byte[] { 0x3E, 0x7C, 0x3C});
                    try {
                        Thread.sleep(500); // sending 2 messages / second
                    } catch (InterruptedException interEx) {
                        // ignore
                    }
                }
                socket.close();
            } catch (IOException ex) {
                throw new RuntimeException("Issue in TestBackendService.run()", ex);
            }
        }

        private CountDownLatch getLatch() {
            return latch;
        }

        private boolean isRunning() {
            return running;
        }

        private void stop() {
            running = false;
            try {
                s.close();
            } catch (IOException ex) {
                throw new RuntimeException("Failed to close ServerSocket", ex);
            }
        }
    }

    private class TestBackendProducer implements Runnable {
        private CountDownLatch latch = new CountDownLatch(1);
        private boolean running = true;

        @Override
        public void run() {
            try {
                Socket socket = new Socket();
                socket.connect(new InetSocketAddress("localhost", 9090));
                OutputStream os = socket.getOutputStream();

                latch.countDown(); // someone connected, count down
                while (running == true) {
                    // The bytes for ">|<"
                    os.write(new byte[] { 0x3E, 0x7C, 0x3C});
                    try {
                        Thread.sleep(500); // sending 2 messages / second
                    } catch (InterruptedException interEx) {
                        // ignore
                    }
                }
                socket.close();
            } catch (IOException ex) {
                throw new RuntimeException("Issue in TestBackendProducer.run()", ex);
            }
        }

        private void stop() {
            running = false;
        }

        private CountDownLatch getLatch() {
            return latch;
        }
    }

    private class TestClient implements Runnable {
        private CountDownLatch latch = new CountDownLatch(1);
        private boolean running = true;
        private int clientNumber;

        private TestClient(int num) {
            clientNumber = num;
        }

        @Override
        public void run() {
            try {
                Socket socket = new Socket();
                socket.connect(new InetSocketAddress("localhost", 9880));
                InputStream in = socket.getInputStream();

                // read a message, countdown the latch, then keep reading messages until stop is called
                byte[] b = new byte[3];
                int numBytes = in.read(b);

                if (numBytes == 3) {
                    System.out.println(format("TestClient %d:  received message %s", clientNumber, new String(b)));
                    latch.countDown(); // someone connected, count down
                } else {
                    System.out.println(format("Failure in TestClient %d:  read returned %d", clientNumber, numBytes));
                }

                while (running == true) {
                    numBytes = in.read(b);
                    if (numBytes < 0) {
                        System.out.println(format("TestClient %d:  EOF, done reading, quitting client"));
                        break;
                    }

                    if (numBytes == 3) {
                        System.out.println(format("TestClient %d:  received message %s", clientNumber, new String(b)));
                    } else {
                        System.out.println(format("Failure in TestClient %d:  read returned %d", clientNumber, numBytes));
                    }
                }
                socket.close();
            } catch (IOException ex) {
                throw new RuntimeException("Issue in TestClient.run()", ex);
            }
        }

        private void stop() {
            running = false;
        }

        private CountDownLatch getLatch() {
            return latch;
        }
    }

    private class FastTestBackendProducer implements Runnable {
        private CountDownLatch latch = new CountDownLatch(1);
        private boolean running = true;

        @Override
        public void run() {
            try {
                Socket socket = new Socket();
                socket.connect(new InetSocketAddress("localhost", 9090));
                OutputStream os = socket.getOutputStream();

                // Some diagnostics for the test
                System.out.println(format("FastTestBackendProducer receive buffer size: %d", socket.getReceiveBufferSize()));
                System.out.println(format("FastTestBackendProducer send buffer size: %d", socket.getSendBufferSize()));

                // The bytes for ">|<"
                byte[] packet = new byte[] { 0x3E, 0x7C, 0x3C };
                int messagesPerSecond = 20;
                int testLengthTime = 4;
                long targetBytes = 2 * (socket.getReceiveBufferSize() + socket.getSendBufferSize());
                long batchSize = targetBytes / (messagesPerSecond * testLengthTime);
                long totalBytesSent = 0;
                long bytesSent = 0;

                System.out.println(format("FastTestBackendProducer targetBytes: %d", targetBytes));
                System.out.println(format("FastTestBackendProducer batch size: %d", batchSize));

                long startTime = System.currentTimeMillis();
                latch.countDown(); // someone connected, count down
                while (running) {
                    while (bytesSent < batchSize) {
                        os.write(packet);
                        bytesSent += packet.length;
                    }

                    totalBytesSent += bytesSent;
                    bytesSent = 0;
                    long delta = (System.currentTimeMillis() - startTime);

                    // In the event the test fails, print some diagnostics
                    System.out.println(format("Time: %d  Producer, sent %d bytes", delta, totalBytesSent));
                    try {
                        Thread.sleep(1000 / messagesPerSecond); // sending 20 messages / second
                    } catch (InterruptedException interEx) {
                        // ignore
                    }
                }
                socket.close();
            } catch (IOException ex) {
                throw new RuntimeException("Issue in TestBackendProducer.run()", ex);
            }
        }

        private void stop() {
            running = false;
        }

        private CountDownLatch getLatch() {
            return latch;
        }
    }

    private class SlowTestClient implements Runnable {
        private CountDownLatch latch = new CountDownLatch(1);
        private int clientNumber;
        private SlowTestClient(int num) {
            clientNumber = num;
        }

        public void stop() {
        }

        @Override
        public void run() {
            try {
                Socket socket = new Socket();
                socket.connect(new InetSocketAddress("localhost", 9880));
                System.out.println(format("SlowTestClient %d: socket is %s", clientNumber, socket.toString()));
                System.out.println(format("SlowTestClient receive buffer size: %d", socket.getReceiveBufferSize()));
                System.out.println(format("SlowTestClient send buffer size: %d", socket.getSendBufferSize()));
                InputStream in = socket.getInputStream();
                OutputStream os = socket.getOutputStream();
                latch.countDown(); // SlowTestClient successfully connected, now won't read

                int iteration = 0;
                while (!socket.isClosed() && socket.isConnected() && !socket.isInputShutdown()) {
                    // take little naps, mmmmmmmmm naps....
                    try {
                        Thread.sleep(1000);
                        System.out.println(format("SlowTestClient %d: iteration #%d has %d bytes available", clientNumber, iteration++, in.available()));
                        try {
                            os.write(new byte[] { 0x21 }); // write '!' back to the Gateway
                        } catch (IOException writeIOEx) {
                            // Expected as this is the way the socket is tested for being closed, just quit the loop
                            break;
                        }
                    } catch (InterruptedException interEx) {
                        System.out.println(format("SlowTestClient %d: someone woke me from my slumber!", clientNumber));
                        break;
                    }
                }

                System.out.println(format("SlowTestClient %d: connection closed, that'll teach me", clientNumber));

                socket.close();
            } catch (IOException ex) {
                throw new RuntimeException("Issue in TestClient.run()", ex);
            }
        }

        private CountDownLatch getLatch() {
            return latch;
        }
    }
}


text:
package com.xcysoft.foundation.cppt.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.newcapec.framework.core.exception.asserts.AssertObject;
import cn.newcapec.framework.core.handler.MultiViewResource;
import cn.newcapec.framework.core.rest.Msg;
import cn.newcapec.framework.core.utils.fileUtils.SysConfigUtil;
import cn.newcapec.framework.core.utils.pagesUtils.Page;
import cn.newcapec.framework.core.utils.pagesUtils.PageContext;
import cn.newcapec.framework.core.utils.pagesUtils.PageView;

import com.xcysoft.foundation.cppt.biz.ActionService;

/**
 *
 * @author huangxin
 *
 */
@Controller
@Scope("prototype")
@RequestMapping(value = "/action")
@SuppressWarnings("all")
public class ActionController extends MultiViewResource {

	@Autowired
	private ActionService actionService;

	@RequestMapping(value = "index", method = RequestMethod.GET)
	public ModelAndView indexUI(ModelMap modelMap) {
		modelMap.put("cdn", SysConfigUtil.get("html.cdn"));
		modelMap.put("virtualPath", SysConfigUtil.get("html.virtualPath"));

		Page page = actionService.findList(null);
		PageView<Map<String, Object>> pageView = new PageView<Map<String, Object>>(
				PageContext.getPageSize(), PageContext.getOffset());
		pageView.setQueryResult(page);
		modelMap.put("pageView", pageView);

		return toView(getUrl("action.indexUI"), modelMap);
	}

	@RequestMapping(value = "list.do", method = RequestMethod.GET)
	public Msg list(final HttpServletRequest request) {
		return doExpAssert(new AssertObject() {
			@Override
			public void AssertMethod(Msg msg) {
				Page page = actionService.findList(getJsonObject());
				PageView<Map<String, Object>> pageView = new PageView<Map<String, Object>>(
						PageContext.getPageSize(), PageContext.getOffset());
				pageView.setQueryResult(page);

				Map<String, Object> modelMap = new HashMap<String, Object>();
				modelMap.put("pageView", pageView);

				msg.setData(toHtml(getUrl("action.indexUI.listUI"), modelMap));
				msg.setSuccess(true);
			}
		});
	}
}

redpajama_set_name: RedPajamaGithub


text:
package com.google.template.soy.sharedpasses;

import com.google.template.soy.base.SoySyntaxException;
import com.google.template.soy.shared.internal.SharedTestUtils;
import com.google.template.soy.soytree.SoyFileSetNode;

import junit.framework.TestCase;

/**
 * Unit tests for CheckSoyDocVisitor.
 *
 * @author Kai Huang
 */
public class CheckSoyDocVisitorTest extends TestCase {

  public void testMatchingSimple() throws SoySyntaxException {

    // ------ No params ------
    String soyDoc = "";
    String templateBody = "Hello world!";
    runTemplateTestHelper(soyDoc, templateBody);  // should not throw exception

    // ------ Only 'print' statements ------
    soyDoc = "@param boo @param foo @param? goo @param moo";
    templateBody = "{$boo}{$foo.goo |noAutoescape}{2 * $goo[round($moo)]}";
    runTemplateTestHelper(soyDoc, templateBody);  // should not throw exception

    // ------ Simple 'if' statement with nested 'print' statement ------
    soyDoc =
        "@param boo Something scary.\n" +
        "@param? goo Something slimy.\n";
    templateBody =
        "{if $boo.foo}\n" +
        "  Slimy {$goo}.\n" +
        "{/if}\n";
    runTemplateTestHelper(soyDoc, templateBody);  // should not throw exception
  }

  public void testMatchingWithAdvancedStmts() throws SoySyntaxException {

    // ------ 'if', 'elseif', 'else', '/if' ------
    String soyDoc = "@param boo @param foo";
    String templateBody =
        "{if $boo}\n" +
        "  Scary.\n" +
        "{elseif $foo.goo}\n" +
        "  Slimy.\n" +
        "{else}\n" +
        "  {$foo.moo}\n" +
        "{/if}\n";
    runTemplateTestHelper(soyDoc, templateBody);  // should not throw exception

    // ------ 'switch', 'case', 'default', '/switch' ------
    soyDoc = "@param boo @param foo @param moo @param too @param zoo";
    templateBody =
        "{switch $boo}\n" +
        "{case $foo.goo}\n" +
        "  Slimy.\n" +
        "{case 0, $moo, $too}\n" +
        "  Soggy.\n" +
        "{default}\n" +
        "  Not {$zoo}.\n" +
        "{/switch}\n";
    runTemplateTestHelper(soyDoc, templateBody);  // should not throw exception

    // ------ 'foreach', 'ifempty', '/foreach' ------
    soyDoc = "@param moose @param? meese";
    templateBody =
        "{foreach $moo in $moose}\n" +
        "  Cow says {$moo}.\n" +
        "{ifempty}\n" +
        "  No {$meese}.\n" +
        "{/foreach}\n";
    runTemplateTestHelper(soyDoc, templateBody);  // should not throw exception

    // ------ 'for', '/for' ------
    soyDoc = "@param boo";
    templateBody =
        "{for $i in range(length($boo))}\n" +
        "  {$i + 1}: {$boo[$i]}.\n" +
        "{/for}\n";
    runTemplateTestHelper(soyDoc, templateBody);  // should not throw exception
  }

  public void testCalls() throws SoySyntaxException {

    String fileContent1 =
        "{namespace boo}\n" +
        "\n" +
        "/**\n" +
        " * @param? goo @param too @param woo @param? zoo\n" +
        " * @param gee\n" +  // no 'mee' is okay because user may not want to list it (chrisn)
        " * @param maa\n" +  // no 'gaa' is okay because it may be optional in 'baa.faa'
        " * @param transParam \n" +  // okay (not required) because it's used in transitive callee
        " */\n" +
        "{template name=\".foo\"}\n" +
        "  {call name=\".fee\" data=\"$goo.moo\" /}\n" +
        "  {call name=\".fee\" data=\"$too\"}\n" +
        "    {param key=\"gee\" value=\"$woo.hoo\" /}\n" +
        "    {param key=\"mee\"}\n" +
        "      {$zoo}\n" +
        "    {/param}\n" +
        "  {/call}\n" +
        "  {call name=\".fee\" data=\"all\" /}\n" +
        "  {call name=\"baa.faa\" data=\"all\" /}\n" +
        "  {call .transitive1 data=\"all\" /}\n" +
        "{/template}\n" +
        "\n" +
        "/** @param gee @param mee */\n" +
        "{template name=\".fee\"}\n" +
        "  {$gee}{$mee}\n" +
        "{/template}\n" +
        "\n" +
        "/** */\n" +
        "{template .transitive1}\n" +
        "  {call .transitive2 data=\"all\" /}\n" +
        "{/template}\n" +
        "\n" +
        "/** @param transParam */\n" +
        "{template .transitive2}\n" +
        "  {$transParam}\n" +
        "{/template}\n";

    String fileContent2 =
        "{namespace baa}\n" +
        "\n" +
        "/** @param gaa @param maa */\n" +
        "{template name=\".faa\"}\n" +
        "  {$gaa}{$maa}\n" +
        "{/template}\n";

    runSoyFilesTestHelper(fileContent1, fileContent2);
  }

  public void testCallWithMissingParam() throws SoySyntaxException {

    String fileContent =
        "{namespace boo}\n" +
        "\n" +
        "/** @param a */\n" +
        "{template .caller}\n" +
        "  {call .callee}\n" +
        "    {param x: $a /}\n" +
        "  {/call}\n" +
        "{/template}\n" +
        "\n" +
        "/** @param x @param y */\n" +
        "{template .callee}\n" +
        "  {$x}{$y}\n" +
        "{/template}\n";

    // This is actually not reported as an error right now because param 'y' in the callee may be
    // optional, even though the SoyDoc does not list the param as optional (many people don't use
    // the @param? tag at all).
    runSoyFilesTestHelper(fileContent);
  }

  public void testUndeclaredParam() throws SoySyntaxException {

    String soyDoc = "@param foo";
    String templateBody = "{$boo.foo}";
    try {
      runTemplateTestHelper(soyDoc, templateBody);
      fail();
    } catch (SoySyntaxException sse) {
      assertTrue(sse.getMessage().contains(
          "Found references to data keys that are not declared in SoyDoc: [boo]"));
    }
  }

  public void testUnusedParam() throws SoySyntaxException {

    String soyDoc = "@param boo @param? foo";
    String templateBody = "{$boo.foo}";
    try {
      runTemplateTestHelper(soyDoc, templateBody);
      fail();
    } catch (SoySyntaxException sse) {
      assertTrue(sse.getMessage().contains(
          "Found params declared in SoyDoc but not used in template: [foo]"));
    }
  }

  public void testUnusedParamInCallWithAllData() throws SoySyntaxException {

    String fileContent =
        "{namespace boo}\n" +
        "\n" +
        "/**\n" +
        " * @param moo\n" +
        " * @param zoo\n" +  // 'zoo' is not used, even in call to .goo
        " */\n" +
        "{template name=\".foo\"}\n" +
        "  {call .goo data=\"all\" /}\n" +
        "{/template}\n" +
        "\n" +
        "/** @param moo */\n" +
        "{template .goo}\n" +
        "  {$moo}\n" +
        "{/template}\n";

    try {
      runSoyFilesTestHelper(fileContent);
      fail();
    } catch (SoySyntaxException sse) {
      assertTrue(sse.getMessage().contains(
          "Found params declared in SoyDoc but not used in template: [zoo]"));
    }
  }

  public void testWithExternalCallWithAllData() throws SoySyntaxException {

    String fileContent =
        "{namespace boo}\n" +
        "\n" +
        "/**\n" +
        " * @param zoo\n" +  // 'zoo' is okay because it may be used in 'goo.moo'
        " */\n" +
        "{template .foo}\n" +
        "  {call goo.moo data=\"all\" /}\n" +
        "{/template}\n";

    runSoyFilesTestHelper(fileContent);
  }

  public void testUnusedParamWithRecursiveCall() throws SoySyntaxException {

    String soyDoc = "@param boo @param foo";
    String templateBody = "{call name=\".foo\" data=\"all\" /}";
    try {
      runTemplateTestHelper(soyDoc, templateBody);
      fail();
    } catch (SoySyntaxException sse) {
      assertTrue(sse.getMessage().contains(
          "Found params declared in SoyDoc but not used in template: [boo, foo]"));
    }
  }

  public void testUnusedParamInDelegateTemplate() throws SoySyntaxException {

    String fileContent =
        "{namespace boo}\n" +
        "\n" +
        "/**\n" +
        " * @param zoo\n" +  // 'zoo' may be needed in other implementations of the same delegate
        " */\n" +
        "{deltemplate MagicButton}\n" +
        "  blah\n" +
        "{/deltemplate}\n";

    runSoyFilesTestHelper(fileContent);
  }

  public void testDelegateCallVariant() throws SoySyntaxException {

    String fileContent = "" +
        "{namespace boo}\n" +
        "\n" +
        "/**\n" +
        " * @param variant\n" +
        " */\n" +
        "{template .foo}\n" +
        "  {delcall MagicButton variant=\"$variant\" /}\n" +
        "{/template}\n";

    runSoyFilesTestHelper(fileContent);
  }

  public void testOnlyCheckFilesInV2() throws SoySyntaxException {

    String fileContent0 =
        "{namespace boo0}\n" +
        "\n" +  // file is missing SoyDoc
        "{template .foo0}\n" +
        "  {$goo0.moo0}\n" +
        "{/template}\n";

    String fileContent1 =
        "{namespace boo1}\n" +
        "\n" +
        "/** Template 1 */\n" +
        "{template .foo1}\n" +
        "  {$goo1}\n" +
        "  {$goo1.moo1()}\n" +  // file is not all V2 syntax due to this expression
        "{/template}\n";

    String fileContent2 =
        "{namespace boo2}\n" +
        "\n" +
        "/** Template 2 */\n" +
        "{template .foo2}\n" +
        "  {$goo2.moo2}\n" +
        "{/template}\n";

    try {
      runSoyFilesTestHelper(fileContent0, fileContent1, fileContent2);
      fail();
    } catch (SoySyntaxException sse) {
      assertTrue(sse.getMessage().contains(
          "Found references to data keys that are not declared in SoyDoc: [goo2]"));
    }
  }

  private static void runTemplateTestHelper(String soyDoc, String templateBody)
      throws SoySyntaxException {

    String testFileContent =
        "{namespace boo}\n" +
        "\n" +
        "/** " + soyDoc + " */\n" +
        "{template name=\".foo\"}\n" +
        templateBody + "\n" +
        "{/template}\n";

    runSoyFilesTestHelper(testFileContent);
  }

  private static void runSoyFilesTestHelper(String... soyFileContents)
      throws SoySyntaxException {

    SoyFileSetNode soyTree = SharedTestUtils.parseSoyFiles(soyFileContents);

    (new CheckSoyDocVisitor(false)).exec(soyTree);
  }

}


text:
<?php
namespace AdminModule\DefaultModule;

use \Nette\Application\UI\Form;
/**
 * Changing the user privileges
 *
 * @author Patrick Kusebauch
 * @version 2.00, 09. 11. 2014
 *
 * @Secured
 * @Resource("Admin:Default:Privilege")
 */
class PrivilegePresenter extends BasePresenter
{
	/** @persistent */
	public $user_id = NULL;

    /** @var  \Models\Privilege */
    protected $privileges;

    /** @var  \Models\User */
    protected $users;

    /**
     * Injects connection to user model
     *
     * @param \Models\User $user
     * @throws \Nette\InvalidStateException
     */
    public function injectUser(\Models\User $user){
        if ($this->users) {
            throw new \Nette\InvalidStateException('User has already been set');
        }
        $this->users = $user;
    }

    /**
     * Injects connection to privilege model
     *
     * @param \Models\Privilege $privilege
     * @throws \Nette\InvalidStateException
     */
    public function injectPrivilege(\Models\Privilege $privilege){
        if ($this->privileges) {
            throw new \Nette\InvalidStateException('Privilege has already been set');
        }
        $this->privileges = $privilege;
    }
	/**
     * Overview of privileges
     *
	 * @Privilege("default")
	 */
	public function actionDefault($user_id)
	{
		$user = $this->privileges->get($this->user_id);
		$this->template->privilege = $user;
		if($user) {
			$this['pickUserForm']->setDefaults(['user_id' => $user_id]);
			$this['changePrivilegeForm']->setDefaults($user->toArray());
		}
	}

    /**
     * Form for modifying privileges
     *
     * @Privilege("edit")
     * @Action("default")
     */
    protected function createComponentChangePrivilegeForm()
    {
        $form = new Form;
        $form->addProtection('Vypršel časový limit, odešlete formulář znovu');
        $form->addSelect('base', 'Obecně', [
            'člen' => 'Člen']);
        $form->addSelect('news', 'Aktuality', [
            'vstup' => 'Vstup',
            'vytváření/editace' => 'Vytváření/Editace',
            'zobrazování' => 'Zobrazování',
            'mazání' => 'Mazání'])
            ->setPrompt('Zakázat přístup');
        $form->addSelect('event', 'Akce/Kalendář', [
            'vstup' => 'Vstup',
            'vytváření/editace' => 'Vytváření/Editace',
            'zobrazování' => 'Zobrazování',
            'mazání' => 'Mazání'])
            ->setPrompt('Zakázat přístup');
        $form->addSelect('chronicle', 'Kronika/Historie oddílu', [
            'vstup' => 'Vstup',
            'vytváření/editace' => 'Vytváření/Editace',
            'popisky' => 'Popisky',
            'generování' => 'Generování',
            'zobrazování' => 'Zobrazování',
            'mazání' => 'Mazání'])
            ->setPrompt('Zakázat přístup');
        $form->addSelect('vip', 'VIP Kronika', [
            'vstup' => 'Vstup',
            'vytváření/editace' => 'Vytváření/Editace',
            'popisky' => 'Popisky',
            'generování' => 'Generování',
            'zobrazování' => 'Zobrazování',
            'mazání' => 'Mazání'])
            ->setPrompt('Zakázat přístup');
        $form->addSelect('hlasinek', 'Hlásínek', [
            'vstup' => 'Vstup',
            'vytváření' => 'Vytváření',
            'mazání' => 'Mazání'])
            ->setPrompt('Zakázat přístup');
        $form->addSelect('registration', 'Organizace/Registrace', [
            'vstup' => 'Vstup',
            'vytváření/editace' => 'Vytváření/Editace',
            'mazání' => 'Mazání'])
            ->setPrompt('Zakázat přístup');
        $form->addSelect('guestbook', 'Kniha návštěv', [
            'vstup' => 'Vstup',
            'vytváření/editace' => 'Vytváření/Editace',
            'zobrazování' => 'Zobrazování',
            'mazání' => 'Mazání'])
            ->setPrompt('Zakázat přístup');
        $form->addSelect('privilege', 'Práva', [
            'vstup' => 'Vstup',
            'vytváření/editace' => 'Vytváření/Editace',
            'mazání' => 'Mazání'])
            ->setPrompt('Zakázat přístup');
        $form->addSubmit('send', 'Změnit práva');
        $form->onSuccess[] = $this->changePrivilegeFormSucceded;
        return $form;
    }

    /**
     * Processing of form for changing privileges
     *
     * @Privilege("edit")
     * @param Form $form
     */
    public function changePrivilegeFormSucceded(Form $form)
    {
        $values = $form->getValues();
        $this->privileges->get($this->user_id)->update($values);
        $this->flashMessage('Přístupová práva byla úspěšně změněna');
        $this->user_id = NULL;
        $this->redirect('default');
    }

    /**
     * Form to pick a member to modify privileges
     *
	 * @Action("default")
	 * @Privilege("default")
	 */
	protected function createComponentPickUserForm()
	{
		$form = new Form;
		$form->addSelect('user_id', 'Zobrazit práva pro uživatele:',
		        $this->users->getOrderedUsers()->fetchPairs('id', 'username')
            )->setPrompt('Vyberte uživatele')
			->setAttribute('onChange', 'submit()');
		$form->onSuccess[] = $this->pickUserFormSucceded;
		return $form;
	}

    /**
     * Processing of Pick User form
     *
     * @Privilege("default")
     *
     * @param Form $form
     */
    public function pickUserFormSucceded(Form $form)
	{
		$values = $form->getValues();
		$this->redirect('default', $values->user_id);
	}
}


text:
package org.elasticsearch.xpack.security.authc;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.ElasticsearchSecurityException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.common.cache.Cache;
import org.elasticsearch.common.cache.CacheBuilder;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Setting.Property;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.node.Node;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportRequest;
import org.elasticsearch.xpack.core.security.authc.Authentication;
import org.elasticsearch.xpack.core.security.authc.AuthenticationFailureHandler;
import org.elasticsearch.xpack.core.security.authc.AuthenticationServiceField;
import org.elasticsearch.xpack.core.security.authc.AuthenticationToken;
import org.elasticsearch.xpack.core.security.authc.Realm;
import org.elasticsearch.xpack.core.security.authc.support.AuthenticationContextSerializer;
import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine.EmptyAuthorizationInfo;
import org.elasticsearch.xpack.core.security.user.AnonymousUser;
import org.elasticsearch.xpack.core.security.user.User;
import org.elasticsearch.xpack.security.audit.AuditTrail;
import org.elasticsearch.xpack.security.audit.AuditTrailService;
import org.elasticsearch.xpack.security.audit.AuditUtil;
import org.elasticsearch.xpack.security.authc.service.ServiceAccountService;
import org.elasticsearch.xpack.security.operator.OperatorPrivileges.OperatorPrivilegesService;
import org.elasticsearch.xpack.security.support.SecurityIndexManager;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;

import static org.elasticsearch.xpack.security.support.SecurityIndexManager.isIndexDeleted;
import static org.elasticsearch.xpack.security.support.SecurityIndexManager.isMoveFromRedToNonRed;

/**
 * An authentication service that delegates the authentication process to its configured {@link Realm realms}.
 * This service also supports request level caching of authenticated users (i.e. once a user authenticated
 * successfully, it is set on the request context to avoid subsequent redundant authentication process)
 */
public class AuthenticationService {

    static final Setting<Boolean> SUCCESS_AUTH_CACHE_ENABLED = Setting.boolSetting(
        "xpack.security.authc.success_cache.enabled",
        true,
        Property.NodeScope
    );
    private static final Setting<Integer> SUCCESS_AUTH_CACHE_MAX_SIZE = Setting.intSetting(
        "xpack.security.authc.success_cache.size",
        10000,
        Property.NodeScope
    );
    private static final Setting<TimeValue> SUCCESS_AUTH_CACHE_EXPIRE_AFTER_ACCESS = Setting.timeSetting(
        "xpack.security.authc.success_cache.expire_after_access",
        TimeValue.timeValueHours(1L),
        Property.NodeScope
    );
    private static final Logger logger = LogManager.getLogger(AuthenticationService.class);

    private final Realms realms;
    private final AuditTrailService auditTrailService;
    private final AuthenticationFailureHandler failureHandler;
    private final ThreadContext threadContext;
    private final Cache<String, Realm> lastSuccessfulAuthCache;
    private final AtomicLong numInvalidation = new AtomicLong();
    private final AuthenticatorChain authenticatorChain;

    public AuthenticationService(
        Settings settings,
        Realms realms,
        AuditTrailService auditTrailService,
        AuthenticationFailureHandler failureHandler,
        ThreadPool threadPool,
        AnonymousUser anonymousUser,
        TokenService tokenService,
        ApiKeyService apiKeyService,
        ServiceAccountService serviceAccountService,
        OperatorPrivilegesService operatorPrivilegesService
    ) {
        this.realms = realms;
        this.auditTrailService = auditTrailService;
        this.failureHandler = failureHandler;
        this.threadContext = threadPool.getThreadContext();
        if (SUCCESS_AUTH_CACHE_ENABLED.get(settings)) {
            this.lastSuccessfulAuthCache = CacheBuilder.<String, Realm>builder()
                .setMaximumWeight(Integer.toUnsignedLong(SUCCESS_AUTH_CACHE_MAX_SIZE.get(settings)))
                .setExpireAfterAccess(SUCCESS_AUTH_CACHE_EXPIRE_AFTER_ACCESS.get(settings))
                .build();
        } else {
            this.lastSuccessfulAuthCache = null;
        }

        final String nodeName = Node.NODE_NAME_SETTING.get(settings);
        this.authenticatorChain = new AuthenticatorChain(
            settings,
            operatorPrivilegesService,
            anonymousUser,
            new AuthenticationContextSerializer(),
            new ServiceAccountAuthenticator(serviceAccountService, nodeName),
            new OAuth2TokenAuthenticator(tokenService),
            new ApiKeyAuthenticator(apiKeyService, nodeName),
            new RealmsAuthenticator(nodeName, numInvalidation, lastSuccessfulAuthCache)
        );
    }

    /**
     * Authenticates the user that is associated with the given request. If the user was authenticated successfully (i.e.
     * a user was indeed associated with the request and the credentials were verified to be valid), the method returns
     * the user and that user is then "attached" to the request's context.
     * This method will authenticate as the anonymous user if the service is configured to allow anonymous access.
     *
     * @param request The request to be authenticated
     */
    public void authenticate(RestRequest request, ActionListener<Authentication> authenticationListener) {
        authenticate(request, true, authenticationListener);
    }

    /**
     * Authenticates the user that is associated with the given request. If the user was authenticated successfully (i.e.
     * a user was indeed associated with the request and the credentials were verified to be valid), the method returns
     * the user and that user is then "attached" to the request's context.
     * This method will optionally, authenticate as the anonymous user if the service is configured to allow anonymous access.
     *
     * @param request The request to be authenticated
     * @param allowAnonymous If {@code false}, then authentication will not fallback to anonymous.
     *                               If {@code true}, then authentication will fallback to anonymous, if this service is
     *                               configured to allow anonymous access.
     */
    public void authenticate(RestRequest request, boolean allowAnonymous, ActionListener<Authentication> authenticationListener) {
        final Authenticator.Context context = new Authenticator.Context(
            threadContext,
            new AuditableRestRequest(auditTrailService.get(), failureHandler, threadContext, request),
            null,
            allowAnonymous,
            realms
        );
        authenticatorChain.authenticateAsync(context, authenticationListener);
    }

    /**
     * Authenticates the user that is associated with the given message. If the user was authenticated successfully (i.e.
     * a user was indeed associated with the request and the credentials were verified to be valid), the method returns
     * the user and that user is then "attached" to the message's context. If no user was found to be attached to the given
     * message, then the given fallback user will be returned instead.
     * @param action       The action of the message
     * @param transportRequest      The request to be authenticated
     * @param fallbackUser The default user that will be assumed if no other user is attached to the message. May not
    *                      be {@code null}.
     */
    public void authenticate(String action, TransportRequest transportRequest, User fallbackUser, ActionListener<Authentication> listener) {
        Objects.requireNonNull(fallbackUser, "fallback user may not be null");
        final Authenticator.Context context = new Authenticator.Context(
            threadContext,
            new AuditableTransportRequest(auditTrailService.get(), failureHandler, threadContext, action, transportRequest),
            fallbackUser,
            false,
            realms
        );
        authenticatorChain.authenticateAsync(context, listener);
    }

    /**
     * Authenticates the user that is associated with the given message. If the user was authenticated successfully (i.e.
     * a user was indeed associated with the request and the credentials were verified to be valid), the method returns
     * the user and that user is then "attached" to the message's context.
     * If no user or credentials are found to be attached to the given message, and the caller allows anonymous access
     * ({@code allowAnonymous} parameter), and this service is configured for anonymous access,
     * then the anonymous user will be returned instead.
     * @param action       The action of the message
     * @param transportRequest      The request to be authenticated
     * @param allowAnonymous Whether to permit anonymous access for this request (this only relevant if the service is
     *                       configured for anonymous access).
     */
    public void authenticate(
        String action,
        TransportRequest transportRequest,
        boolean allowAnonymous,
        ActionListener<Authentication> listener
    ) {
        final Authenticator.Context context = new Authenticator.Context(
            threadContext,
            new AuditableTransportRequest(auditTrailService.get(), failureHandler, threadContext, action, transportRequest),
            null,
            allowAnonymous,
            realms
        );
        authenticatorChain.authenticateAsync(context, listener);
    }

    /**
     * Authenticates the user based on the contents of the token that is provided as parameter. This will not look at the values in the
     * ThreadContext for Authentication.
     *  @param action  The action of the message
     * @param transportRequest The message that resulted in this authenticate call
     * @param token   The token (credentials) to be authenticated
     */
    public void authenticate(
        String action,
        TransportRequest transportRequest,
        AuthenticationToken token,
        ActionListener<Authentication> listener
    ) {
        final Authenticator.Context context = new Authenticator.Context(
            threadContext,
            new AuditableTransportRequest(auditTrailService.get(), failureHandler, threadContext, action, transportRequest),
            null,
            true,
            realms
        );
        context.addAuthenticationToken(token);
        authenticatorChain.authenticateAsync(context, listener);
    }

    public void expire(String principal) {
        if (lastSuccessfulAuthCache != null) {
            numInvalidation.incrementAndGet();
            lastSuccessfulAuthCache.invalidate(principal);
        }
    }

    public void expireAll() {
        if (lastSuccessfulAuthCache != null) {
            numInvalidation.incrementAndGet();
            lastSuccessfulAuthCache.invalidateAll();
        }
    }

    public void onSecurityIndexStateChange(SecurityIndexManager.State previousState, SecurityIndexManager.State currentState) {
        if (lastSuccessfulAuthCache != null) {
            if (isMoveFromRedToNonRed(previousState, currentState)
                || isIndexDeleted(previousState, currentState)
                || Objects.equals(previousState.indexUUID, currentState.indexUUID) == false) {
                expireAll();
            }
        }
    }

    // pkg private method for testing
    long getNumInvalidation() {
        return numInvalidation.get();
    }

    abstract static class AuditableRequest {

        final AuditTrail auditTrail;
        final AuthenticationFailureHandler failureHandler;
        final ThreadContext threadContext;

        AuditableRequest(AuditTrail auditTrail, AuthenticationFailureHandler failureHandler, ThreadContext threadContext) {
            this.auditTrail = auditTrail;
            this.failureHandler = failureHandler;
            this.threadContext = threadContext;
        }

        abstract void realmAuthenticationFailed(AuthenticationToken token, String realm);

        abstract ElasticsearchSecurityException tamperedRequest();

        abstract ElasticsearchSecurityException exceptionProcessingRequest(Exception e, @Nullable AuthenticationToken token);

        abstract ElasticsearchSecurityException authenticationFailed(AuthenticationToken token);

        abstract ElasticsearchSecurityException anonymousAccessDenied();

        abstract ElasticsearchSecurityException runAsDenied(Authentication authentication, AuthenticationToken token);

        abstract void authenticationSuccess(Authentication authentication);

    }

    static class AuditableTransportRequest extends AuditableRequest {

        private final String action;
        private final TransportRequest transportRequest;
        private final String requestId;

        AuditableTransportRequest(
            AuditTrail auditTrail,
            AuthenticationFailureHandler failureHandler,
            ThreadContext threadContext,
            String action,
            TransportRequest transportRequest
        ) {
            super(auditTrail, failureHandler, threadContext);
            this.action = action;
            this.transportRequest = transportRequest;
            // There might be an existing audit-id (e.g. generated by the rest request) but there might not be (e.g. an internal action)
            this.requestId = AuditUtil.getOrGenerateRequestId(threadContext);
        }

        @Override
        void authenticationSuccess(Authentication authentication) {
            auditTrail.authenticationSuccess(requestId, authentication, action, transportRequest);
        }

        @Override
        void realmAuthenticationFailed(AuthenticationToken token, String realm) {
            auditTrail.authenticationFailed(requestId, realm, token, action, transportRequest);
        }

        @Override
        ElasticsearchSecurityException tamperedRequest() {
            auditTrail.tamperedRequest(requestId, action, transportRequest);
            return new ElasticsearchSecurityException("failed to verify signed authentication information");
        }

        @Override
        ElasticsearchSecurityException exceptionProcessingRequest(Exception e, @Nullable AuthenticationToken token) {
            if (token != null) {
                auditTrail.authenticationFailed(requestId, token, action, transportRequest);
            } else {
                auditTrail.authenticationFailed(requestId, action, transportRequest);
            }
            return failureHandler.exceptionProcessingRequest(transportRequest, action, e, threadContext);
        }

        @Override
        ElasticsearchSecurityException authenticationFailed(AuthenticationToken token) {
            auditTrail.authenticationFailed(requestId, token, action, transportRequest);
            return failureHandler.failedAuthentication(transportRequest, token, action, threadContext);
        }

        @Override
        ElasticsearchSecurityException anonymousAccessDenied() {
            auditTrail.anonymousAccessDenied(requestId, action, transportRequest);
            return failureHandler.missingToken(transportRequest, action, threadContext);
        }

        @Override
        ElasticsearchSecurityException runAsDenied(Authentication authentication, AuthenticationToken token) {
            auditTrail.runAsDenied(requestId, authentication, action, transportRequest, EmptyAuthorizationInfo.INSTANCE);
            return failureHandler.failedAuthentication(transportRequest, token, action, threadContext);
        }

        @Override
        public String toString() {
            return "transport request action [" + action + "]";
        }

    }

    static class AuditableRestRequest extends AuditableRequest {

        private final RestRequest request;
        private final String requestId;

        AuditableRestRequest(
            AuditTrail auditTrail,
            AuthenticationFailureHandler failureHandler,
            ThreadContext threadContext,
            RestRequest request
        ) {
            super(auditTrail, failureHandler, threadContext);
            this.request = request;
            // There should never be an existing audit-id when processing a rest request.
            this.requestId = AuditUtil.generateRequestId(threadContext);
        }

        @Override
        void authenticationSuccess(Authentication authentication) {
            auditTrail.authenticationSuccess(requestId, authentication, request);
        }

        @Override
        void realmAuthenticationFailed(AuthenticationToken token, String realm) {
            auditTrail.authenticationFailed(requestId, realm, token, request);
        }

        @Override
        ElasticsearchSecurityException tamperedRequest() {
            auditTrail.tamperedRequest(requestId, request);
            return new ElasticsearchSecurityException("rest request attempted to inject a user");
        }

        @Override
        ElasticsearchSecurityException exceptionProcessingRequest(Exception e, @Nullable AuthenticationToken token) {
            if (token != null) {
                auditTrail.authenticationFailed(requestId, token, request);
            } else {
                auditTrail.authenticationFailed(requestId, request);
            }
            return failureHandler.exceptionProcessingRequest(request, e, threadContext);
        }

        @Override
        ElasticsearchSecurityException authenticationFailed(AuthenticationToken token) {
            auditTrail.authenticationFailed(requestId, token, request);
            return failureHandler.failedAuthentication(request, token, threadContext);
        }

        @Override
        ElasticsearchSecurityException anonymousAccessDenied() {
            auditTrail.anonymousAccessDenied(requestId, request);
            return failureHandler.missingToken(request, threadContext);
        }

        @Override
        ElasticsearchSecurityException runAsDenied(Authentication authentication, AuthenticationToken token) {
            auditTrail.runAsDenied(requestId, authentication, request, EmptyAuthorizationInfo.INSTANCE);
            return failureHandler.failedAuthentication(request, token, threadContext);
        }

        @Override
        public String toString() {
            return "rest request uri [" + request.uri() + "]";
        }
    }

    public static void addSettings(List<Setting<?>> settings) {
        settings.add(AuthenticationServiceField.RUN_AS_ENABLED);
        settings.add(SUCCESS_AUTH_CACHE_ENABLED);
        settings.add(SUCCESS_AUTH_CACHE_MAX_SIZE);
        settings.add(SUCCESS_AUTH_CACHE_EXPIRE_AFTER_ACCESS);
    }
}


text:
module Language.LSP.Test.Replay
  ( -- replaySession
  )
where

import           Prelude hiding (id)
import           Control.Concurrent
import           Control.Monad.IO.Class
import qualified Data.ByteString.Lazy.Char8    as B
import qualified Data.Text                     as T
import           Language.LSP.Types
import           Language.LSP.Types.Lens as LSP
import           Data.Aeson
import           Data.Default
import           Data.List
import           Data.Maybe
import           Control.Lens hiding (List)
import           Control.Monad
import           System.FilePath
import           System.IO
import           Language.LSP.Test
import           Language.LSP.Test.Compat
import           Language.LSP.Test.Files
import           Language.LSP.Test.Decoding
import           Language.LSP.Test.Server
import           Language.LSP.Test.Session

{-
-- | Replays a captured client output and
-- makes sure it matches up with an expected response.
-- The session directory should have a captured session file in it
-- named "session.log".
-- You can get these capture files from 'Language.Haskell.LSP.resCaptureFile' in
-- haskell-lsp.
replaySession :: String -- ^ The command to run the server.
              -> FilePath -- ^ The recorded session directory.
              -> IO ()
replaySession serverExe sessionDir = do

  entries <- B.lines <$> B.readFile (sessionDir </> "session.log")

  -- decode session
  let unswappedEvents = map (fromJust . decode) entries

  withServer serverExe False $ \serverIn serverOut serverProc -> do

    pid <- getProcessID serverProc
    events <- swapCommands pid <$> swapFiles sessionDir unswappedEvents

    let clientEvents = filter isClientMsg events
        serverEvents = filter isServerMsg events
        clientMsgs = map (\(FromClient _ msg) -> msg) clientEvents
        serverMsgs = filter (not . shouldSkip) $ map (\(FromServer _ msg) -> msg) serverEvents
        requestMap = getRequestMap clientMsgs

    reqSema <- newEmptyMVar
    rspSema <- newEmptyMVar
    passSema <- newEmptyMVar
    mainThread <- myThreadId

    sessionThread <- liftIO $ forkIO $
      runSessionWithHandles serverIn serverOut serverProc
                            (listenServer serverMsgs requestMap reqSema rspSema passSema mainThread)
                            def
                            fullCaps
                            sessionDir
                            (return ()) -- No finalizer cleanup
                            (sendMessages clientMsgs reqSema rspSema)
    takeMVar passSema
    killThread sessionThread

  where
    isClientMsg (FromClient _ _) = True
    isClientMsg _                = False

    isServerMsg (FromServer _ _) = True
    isServerMsg _                = False

sendMessages :: [FromClientMessage] -> MVar LspId -> MVar LspIdRsp -> Session ()
sendMessages [] _ _ = return ()
sendMessages (nextMsg:remainingMsgs) reqSema rspSema =
  handleClientMessage request response notification nextMsg
 where
  -- TODO: May need to prevent premature exit notification being sent
  notification msg@(NotificationMessage _ Exit _) = do
    liftIO $ putStrLn "Will send exit notification soon"
    liftIO $ threadDelay 10000000
    sendMessage msg

    liftIO $ error "Done"

  notification msg@(NotificationMessage _ m _) = do
    sendMessage msg

    liftIO $ putStrLn $ "Sent a notification " ++ show m

    sendMessages remainingMsgs reqSema rspSema

  request msg@(RequestMessage _ id m _) = do
    sendRequestMessage msg
    liftIO $ putStrLn $  "Sent a request id " ++ show id ++ ": " ++ show m ++ "\nWaiting for a response"

    rsp <- liftIO $ takeMVar rspSema
    when (responseId id /= rsp) $
      error $ "Expected id " ++ show id ++ ", got " ++ show rsp

    sendMessages remainingMsgs reqSema rspSema

  response msg@(ResponseMessage _ id _) = do
    liftIO $ putStrLn $ "Waiting for request id " ++ show id ++ " from the server"
    reqId <- liftIO $ takeMVar reqSema
    if responseId reqId /= id
      then error $ "Expected id " ++ show reqId ++ ", got " ++ show reqId
      else do
        sendResponse msg
        liftIO $ putStrLn $ "Sent response to request id " ++ show id

    sendMessages remainingMsgs reqSema rspSema

sendRequestMessage :: (ToJSON a, ToJSON b) => RequestMessage ClientMethod a b -> Session ()
sendRequestMessage req = do
  -- Update the request map
  reqMap <- requestMap <$> ask
  liftIO $ modifyMVar_ reqMap $
    \r -> return $ updateRequestMap r (req ^. LSP.id) (req ^. method)

  sendMessage req

isNotification :: FromServerMessage -> Bool
isNotification (NotPublishDiagnostics      _) = True
isNotification (NotLogMessage              _) = True
isNotification (NotShowMessage             _) = True
isNotification (NotCancelRequestFromServer _) = True
isNotification _                              = False

listenServer :: [FromServerMessage]
             -> RequestMap
             -> MVar LspId
             -> MVar LspIdRsp
             -> MVar ()
             -> ThreadId
             -> Handle
             -> SessionContext
             -> IO ()
listenServer [] _ _ _ passSema _ _ _ = putMVar passSema ()
listenServer expectedMsgs reqMap reqSema rspSema passSema mainThreadId serverOut ctx = do

  msgBytes <- getNextMessage serverOut
  let msg = decodeFromServerMsg reqMap msgBytes

  handleServerMessage request response notification msg

  if shouldSkip msg
    then listenServer expectedMsgs reqMap reqSema rspSema passSema mainThreadId serverOut ctx
    else if inRightOrder msg expectedMsgs
      then listenServer (delete msg expectedMsgs) reqMap reqSema rspSema passSema mainThreadId serverOut ctx
      else let remainingMsgs = takeWhile (not . isNotification) expectedMsgs
                ++ [head $ dropWhile isNotification expectedMsgs]
               exc = ReplayOutOfOrder msg remainingMsgs
            in liftIO $ throwTo mainThreadId exc

  where
  response :: ResponseMessage a -> IO ()
  response res = do
    putStrLn $ "Got response for id " ++ show (res ^. id)

    putMVar rspSema (res ^. id) -- unblock the handler waiting to send a request

  request :: RequestMessage ServerMethod a b -> IO ()
  request req = do
    putStrLn
      $  "Got request for id "
      ++ show (req ^. id)
      ++ " "
      ++ show (req ^. method)

    putMVar reqSema (req ^. id) -- unblock the handler waiting for a response

  notification :: NotificationMessage ServerMethod a -> IO ()
  notification n = putStrLn $ "Got notification " ++ show (n ^. method)

-- TODO: QuickCheck tests?
-- | Checks wether or not the message appears in the right order
-- @ N1 N2 N3 REQ1 N4 N5 REQ2 RES1 @
-- given N2, notification order doesn't matter.
-- @ N1 N3 REQ1 N4 N5 REQ2 RES1 @
-- given REQ1
-- @ N1 N3 N4 N5 REQ2 RES1 @
-- given RES1
-- @ N1 N3 N4 N5 XXXX RES1 @ False!
-- Order of requests and responses matter
inRightOrder :: FromServerMessage -> [FromServerMessage] -> Bool

inRightOrder _ [] = error "Why is this empty"

inRightOrder received (expected : msgs)
  | received == expected               = True
  | isNotification expected            = inRightOrder received msgs
  | otherwise                          = False

-- | Ignore logging notifications since they vary from session to session
shouldSkip :: FromServerMessage -> Bool
shouldSkip (NotLogMessage  _) = True
shouldSkip (NotShowMessage _) = True
shouldSkip (ReqShowMessage _) = True
shouldSkip _                  = False

-- | Swaps out any commands uniqued with process IDs to match the specified process ID
swapCommands :: Int -> [Event] -> [Event]
swapCommands _ [] = []

swapCommands pid (FromClient t (ReqExecuteCommand req):xs) =  FromClient t (ReqExecuteCommand swapped):swapCommands pid xs
  where swapped = params . command .~ newCmd $ req
        newCmd = swapPid pid (req ^. params . command)

swapCommands pid (FromServer t (RspInitialize rsp):xs) = FromServer t (RspInitialize swapped):swapCommands pid xs
  where swapped = case newCommands of
          Just cmds -> result . _Right . LSP.capabilities . executeCommandProvider . _Just . commands .~ cmds $ rsp
          Nothing -> rsp
        oldCommands = rsp ^? result . _Right . LSP.capabilities . executeCommandProvider . _Just . commands
        newCommands = fmap (fmap (swapPid pid)) oldCommands

swapCommands pid (x:xs) = x:swapCommands pid xs

hasPid :: T.Text -> Bool
hasPid = (>= 2) . T.length . T.filter (':' ==)
swapPid :: Int -> T.Text -> T.Text
swapPid pid t
  | hasPid t = T.append (T.pack $ show pid) $ T.dropWhile (/= ':') t
  | otherwise = t
-}


text: docker logs --follow=true --tail=25 redis-service


text:
<?php

namespace Omnipay\Curopayments\Message;

use Omnipay\Tests\TestCase;

class IdealPurchaseRequestTest extends TestCase
{
    public function setUp()
    {
        $this->request = new IdealPurchaseRequest($this->getHttpClient(), $this->getHttpRequest());
        $this->request->initialize([
                'siteId' => '123',
                'secretKey' => 'secret',
                'amount' => '12.00',
                'ref' => 'test',
        ]);
    }

    public function testGetData()
    {
        $data = $this->request->getData();
        $this->assertSame('ideal', $data['option']);
    }
}


text:
package testsshagent

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"
	"syscall"
	"testing"
)

// SSHAdd runs ssh-add in the specified directory to connect to the
// agent sockname to add the specified keys.
func SSHAdd(dir, sockname string, args ...string) (string, error) {
	cmd := exec.Command("ssh-add", args...)
	cmd.Dir = dir
	cmd.Env = []string{"SSH_AUTH_SOCK=" + sockname}
	output, err := cmd.CombinedOutput()
	if err != nil {
		return string(output), fmt.Errorf("failed %v: to add ssh keys: %v: %s", strings.Join(cmd.Args, " "), err, output)
	}
	return string(output), nil
}

// StartSSHAgent starts an ssh agent and returns a cleanup function and
// the socket name to use for connecting to the agent.
func StartSSHAgent() (func(), string, error) {
	cmd := exec.Command("ssh-agent")
	output, err := cmd.CombinedOutput()
	if err != nil {
		return nil, "", err
	}
	lines := strings.Split(string(output), "\n")
	first := lines[0]
	addr := strings.TrimPrefix(first, "SSH_AUTH_SOCK=")
	addr = strings.TrimSuffix(addr, "; export SSH_AUTH_SOCK;")
	second := lines[1]
	pidstr := strings.TrimPrefix(second, "SSH_AGENT_PID=")
	pidstr = strings.TrimSuffix(pidstr, "; export SSH_AGENT_PID;")
	pid, err := strconv.ParseInt(pidstr, 10, 64)
	if err != nil {
		return func() {}, "", fmt.Errorf("failed to parse pid from %v", second)
	}
	cleanup := func() {
		syscall.Kill(int(pid), syscall.SIGTERM)
		if testing.Verbose() {
			fmt.Println(string(output))
			fmt.Printf("killing: %v\n", int(pid))
		}
	}
	return cleanup, addr, nil
}

// StartPreconfiguredAgent starts an ssh agent and preconfigures it with
// the supplied keys.
// It returns a cleanup function, the socket name for the agent,
// the directory that the keys are created in and an error.
func StartPreconfiguredAgent(keydir string, keys ...string) (func(), string, error) {
	cleanup, addr, err := StartSSHAgent()
	if err != nil {
		return func() {}, "", err
	}
	for _, k := range keys {
		if err := os.Chmod(filepath.Join(keydir, k), 0600); err != nil {
			panic(err)
		}
	}
	if _, err := SSHAdd(keydir, addr, keys...); err != nil {
		return func() {}, "", err
	}
	list, _ := SSHAdd(keydir, addr, "-l")
	fmt.Printf("%s\n", list)
	return cleanup, addr, nil
}


text:
<?xml version="1.0" encoding="utf-8"?>
<!--Copyright 2010 NetCatch Team

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->
<manifest package="net.leifandersen.mobile.android.netcatch"
	android:versionCode="1" xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="0.0.1">
	<application android:name=".other.GlobalVars" android:icon="@drawable/icon" android:label="@string/app_name"
		android:debuggable="true">
		<activity android:label="@string/app_name" android:name=".activities.NCMain"
			android:theme="@android:style/Theme.NoTitleBar">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>

		<activity android:theme="@android:style/Theme.NoTitleBar"
			android:name=".activities.ShowsListActivity" />

		<activity android:name=".activities.EpisodesListActivity"
			android:theme="@android:style/Theme.NoTitleBar">
			<intent-filter android:label="@string/app_name">
				<action android:name="android.intent.action.VIEW"/>
				<category android:name="android.intent.category.BROWSABLE"/>
				<category android:name="android.intent.category.DEFAULT"/>
				<data android:mimeType="application/rss+xml" android:scheme="http"/>
			</intent-filter>
			<intent-filter android:label="@string/app_name">
				<action android:name="android.intent.action.VIEW"/>
				<category android:name="android.intent.category.BROWSABLE"/>
				<category android:name="android.intent.category.DEFAULT"/>
				<data android:mimeType="application/atom+xml" android:scheme="http"/>
			</intent-filter>
			<intent-filter android:label="@string/app_name">
				<action android:name="android.intent.action.VIEW"/>
				<category android:name="android.intent.category.BROWSABLE"/>
				<category android:name="android.intent.category.DEFAULT"/>
				<data android:mimeType="application/xml" android:scheme="http"/>
			</intent-filter>
			<intent-filter android:label="@string/app_name">
				<action android:name="android.intent.action.VIEW"/>
				<category android:name="android.intent.category.BROWSABLE"/>
				<category android:name="android.intent.category.DEFAULT"/>
				<data android:mimeType="text/xml" android:scheme="http"/>
			</intent-filter>
			<intent-filter android:label="@string/app_name">
				<action android:name="android.intent.action.VIEW"/>
				<category android:name="android.intent.category.BROWSABLE"/>
				<category android:name="android.intent.category.DEFAULT"/>
				<data android:scheme="http" android:host="*" android:pathPattern=".*\\.xml"/>
			</intent-filter>
		</activity>

		<activity android:name=".activities.QueueListActivity"
			android:theme="@android:style/Theme.NoTitleBar" />

		<activity android:name=".activities.ColorChoiceDialogActivity"
			android:theme="@android:style/Theme.NoTitleBar">
			<intent-filter>
				<action
					android:name="net.leifandersen.mobile.android.netcatch.activities.ColorChoiceDialogActivity" />
				<category android:name="android.intent.category.DEFAULT" />
			</intent-filter>
		</activity>

		<activity android:name=".activities.Preferences" />
		<activity android:name=".activities.EpisodeActivity"></activity>
		<activity android:name=".activities.NewEpisodeListActivity"
			android:theme="@android:style/Theme.NoTitleBar"></activity>

		<service android:name=".services.RSSService">
		</service>
		<service android:name=".services.UnsubscribeService"></service>
		<service android:name=".services.MediaDownloadService"></service>
		<service android:name=".services.MediaPlayerService"></service>
		<receiver android:name=".recievers.BootCompletedReceiver">
			<intent-filter>
				<action android:name="android.intent.action.BOOT_COMPLETED"></action>
			</intent-filter>
		</receiver>
		<receiver android:name=".recievers.BootCompletedReceiver"></receiver>

		<provider android:name=".providers.ShowsProvider"
			android:authorities="net.leifandersen.provider.Shows" />

	</application>

	<uses-sdk android:targetSdkVersion="9" android:minSdkVersion="7" />

	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	<uses-permission android:name="android.permission.INTERNET" />
	<uses-permission android:name="android.permission.READ_PHONE_STATE" />
	<uses-permission android:name="android.permission.WAKE_LOCK" />
	<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

	<supports-screens android:smallScreens="true"
		android:normalScreens="true" android:largeScreens="true"
		android:anyDensity="true" />
</manifest>


text:
package com.mechjack.dispatch.componentregistry;

import com.mechjack.Component;

@Component
public class MockStartableComponent
    implements MockableStartableComponent {

  private boolean wasStarted;
  private boolean wasStopped;

  @Override
  public boolean wasStarted() {
    return this.wasStarted;
  }

  @Override
  public boolean wasStopped() {
    return this.wasStopped;
  }

  @Override
  public void start() {
    this.wasStarted = true;
  }

  @Override
  public void stop() {
    this.wasStopped = true;
  }

}


text:
import sys

from owlmixin import OwlMixin, TOption
from owlmixin.owlcollections import TList

from jumeaux.addons.final import FinalExecutor
from jumeaux.utils import jinja2_format, get_jinja2_format_error, when_optional_filter
from jumeaux.logger import Logger
from jumeaux.models import FinalAddOnPayload, Notifier, FinalAddOnReference, Report
from jumeaux.notification_handlers import create_notification_handler

logger: Logger = Logger(__name__)
LOG_PREFIX = "[final/notify]"

class Notify(OwlMixin):
    notifier: str
    message: str
    when: TOption[str]

class Config(OwlMixin):
    notifies: TList[Notify] = []

def send(message: str, notifier: Notifier) -> TOption[str]:
    logger.info_lv1(notifier.logging_message)
    return create_notification_handler(notifier).notify(message)

def need_to_notify(notify: Notify, report: Report) -> bool:
    if when_optional_filter(notify.when, report.to_dict(ignore_none=False)):
        logger.info_lv3(
            f"{LOG_PREFIX} Notify by {notify.notifier}. (notify.when => {notify.when.get_or('None')})"
        )
        return True
    else:
        logger.info_lv3(
            f"{LOG_PREFIX} Don't Notify by {notify.notifier}. (notify.when => {notify.when.get_or('None')}"
        )
        return False

class Executor(FinalExecutor):
    def __init__(self, config: dict):
        self.config: Config = Config.from_dict(config or {})

        errors: TList[str] = self.config.notifies.map(
            lambda x: get_jinja2_format_error(x.message).get()
        ).filter(lambda x: x is not None)
        if errors:
            logger.error(f"{LOG_PREFIX} Illegal format in `notifies[*].message`.")
            logger.error(f"{LOG_PREFIX} Please check your configuration yaml files.")
            logger.error(f"{LOG_PREFIX} --- Error messages ---")
            errors.map(lambda x: logger.error(f"{LOG_PREFIX}   * `{x}`"))
            logger.error(f"{LOG_PREFIX} ---------------------", exit=True)

    def exec(self, payload: FinalAddOnPayload, reference: FinalAddOnReference) -> FinalAddOnPayload:
        if reference.notifiers.is_none():
            logger.error(f"{LOG_PREFIX} There are no notifiers. Please set notifiers in config.")
            logger.error(
                f"{LOG_PREFIX} See https://tadashi-aikawa.github.io/jumeaux/ja/getstarted/configuration/"
            )
            sys.exit(1)

        errors: TList[TOption[str]] = self.config.notifies.filter(
            lambda n: need_to_notify(n, payload.report)
        ).map(
            lambda x: send(
                jinja2_format(x.message, payload.report.to_dict(ignore_none=False)),
                reference.notifiers.get()
                .get(x.notifier)
                .get(),  # TODO: The case that notifier not found
            )
        )
        if errors.reject(lambda m: m.is_none()):
            errors.map(lambda m: m.map(logger.error))
            sys.exit(1)

        return payload


text:
package io.github.daviddenton.finagle.aws

import java.time.Clock

import com.twitter.finagle.Filter
import com.twitter.finagle.http.{Request, Response}
import io.github.daviddenton.finagle.aws.AwsHeaders.DATE
import org.jboss.netty.handler.codec.http.HttpHeaders.Names
import org.jboss.netty.handler.codec.http.HttpHeaders.Names.{CONTENT_LENGTH, HOST}

object AwsCredentialFilter {
  def apply(host: String, clock: Clock, signer: AwsSignatureV4Signer): Filter[Request, Response, Request, Response] = Filter.mk {
    (req, svc) => {
      val date = AwsRequestDate(clock.instant())
      req.headerMap(HOST) = host
      req.headerMap(CONTENT_LENGTH) = req.content.length.toString
      req.headerMap(DATE) = date.full

      val canonicalRequest = AwsCanonicalRequest(req)
      req.headerMap(Names.AUTHORIZATION) = signer.authHeader(canonicalRequest, date)
      req.headerMap(AwsHeaders.CONTENT_SHA256) = canonicalRequest.payloadHash
      svc(req)
    }
  }
}


text:
BEGIN;

CREATE TABLE paths (
    id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    path TEXT NOT NULL
);
COMMENT ON TABLE paths IS
'TABLE paths contains the path string for every path in the units table.';

ALTER TABLE units ADD COLUMN path_id INTEGER;

END;


text:
from bs4 import BeautifulSoup
import urllib2
import re
import csv
import os.path

if not os.path.isfile("imdb_indian_ta.csv"):
    imdb_indian_file = open("imdb_indian_ta.csv","ab+")
    writer = csv.writer(imdb_indian_file)
    writer.writerow(["id","name","duration","genre","titleYear","releaseDate","director","language","budget","cast","link"])
else:
    imdb_indian_file = open("imdb_indian_ta.csv","ab+")
    writer = csv.writer(imdb_indian_file)

imdb_indian_file.flush()
movieDurationRegex = re.compile('[0-9]+')
movieLinkHead = "http://www.imdb.com"
#movieData = []

def getMovieDetails(movieLink):
    try:
        movieSoup = BeautifulSoup(urllib2.urlopen(movieLinkHead+movieLink).read())
        titleYear = movieSoup.find(id="titleYear").contents[1].contents[0]

        releaseDate = movieSoup.find(title="See more release dates")
        if releaseDate is None:
            releaseDate = None
        else:
            releaseDate = releaseDate.contents[1]['content']

        try:
            director = movieSoup.find(itemprop="director").contents[1].contents[0].contents[0]
        except:
            director = ""

        titleDetails = movieSoup.find(id="titleDetails")
        languageItem = titleDetails.find("h4",text="Language:")

        if languageItem is None:
            language=''
        else:

            language = languageItem.next_sibling.next_sibling.contents[0]
        #print language
        budget = 0

        budgetItem = titleDetails.find("h4",text="Budget:")
        if budgetItem is not None:
            budget = re.sub('[a-zA-Z, ]','',budgetItem.next_sibling).strip()

        cast = []
        castItems = movieSoup.find("table","cast_list")
        if castItems is not None:
            for castItem in castItems.find_all("span",itemprop="name"):
                cast.append(castItem.contents[0])

        return {"titleYear":titleYear,"releaseDate":releaseDate,"director":director.strip(),"budget":budget,"language":language.strip(),"cast":cast}
    except:
        return None

def cleanValue(value):
    return re.sub('\n','',value).strip()

def getMoviesPage():
    #pageUrl = 'http://www.imdb.com/search/title?countries=in&countries=IN&title_type=feature&explore=countries&sort=alpha,asc&view=advanced&page=201'
    pageUrl = 'http://www.imdb.com/search/title?title_type=feature&languages=ta&sort=alpha,asc&page=1&ref_=adv_nxt'
    id = 1
    pageId=1
    while True:
        movieData = []
        print "Processing page:"+str(pageId)
        page = urllib2.urlopen(pageUrl)
        soup = BeautifulSoup(page.read())
        for movieItem in soup.find_all('div','lister-item-content'):
            nameLink = movieItem.find('a')
            name = nameLink.contents[0]
            print str(id)+":"+name
            duration = movieItem.find('span','runtime')
            if duration is None:
                duration = 0
            else:
                duration = re.search(movieDurationRegex,duration.contents[0]).group(0)

            genre = movieItem.find('span','genre')
            if genre is None:
                genre = ""
            else:
                genre = cleanValue(genre.contents[0])

            link = nameLink['href']

            movieDetails = getMovieDetails(link)
            if movieDetails is None:
                continue
            movieFull ={'id':id,'name':name,'duration':duration,'link':movieLinkHead+link,'genre':genre}
            movieFull.update(movieDetails)

            movieData.append(movieFull)
            id +=1

        writeCsv(movieData)
        #break

        pageItem = soup.find('a','lister-page-next next-page')
        if pageItem is None:
            break
        else:
            pageUrl = 'http://www.imdb.com/search/title'+pageItem['href']
        pageId += 1

def writeCsv(movieData):

    for movie in movieData:
        try:
            cast = ",".join(movie["cast"])
            writer.writerow([movie["id"],movie["name"],movie["duration"],movie["genre"],movie["titleYear"],movie["releaseDate"],movie["director"],movie["language"],movie["budget"],cast,movie["link"]])
        except:
            continue
    imdb_indian_file.flush()
    return

if __name__=='__main__':
    getMoviesPage()
    imdb_indian_file.close()
    #print movieData


text: require 'refinery/dashboard'


text:
"""Docstrings

Now that we have defined our own functions, it
makes sense to talk about how to document them
properly.  Earlier, it was briefly mentioned that
comments are not the favored tool for creating
documentation in Python: **docstrings** are.

A string becomes a docstring when it is the first
statement in a module, class, or function, simply
by virtue of its position. It does not need to be
assigned to anything.

The |pydoc| utility and |help| function each
format these docstrings and display them when
requested (outside the browser).

At the command line, for example, you can type

  pydoc list

And get a nice help page made up mostly of module
docstrings.
"""

__doc__ = """Short description of the module.

A longer description of the module. This docstring can
be accessed in the module-global __doc__ variable (in full
Python environments, not this one).
"""

def a_complex_function(a, b, c):
  """Do a complex operation on a, b, and c.

  This will do amazing things with a, b, and c. Just watch.

  Args:
    a: A boolean value (see above).
    b: A boolean value (again, see above).
    c: A sequence.

  Returns:
    Nothing - awesomeness needs no return value.
  """
  print("Shhh: it's actually not all that complex:")
  print(a, b, c)
  # No return statement, or an empty return statement,
  # will implicitly return None.

print("Result of a complex function:")
print(a_complex_function(True, False, [1,2,3,4]))

print("The function docstring:")
print(a_complex_function.__doc__)


text:
import sys, glob, string

try:
    from xlrd import open_workbook, XL_CELL_EMPTY, XL_CELL_BLANK, XL_CELL_TEXT, XL_CELL_NUMBER, cellname
    null_cell_types = (XL_CELL_EMPTY, XL_CELL_BLANK)
except ImportError:
    # older version
    from xlrd import open_workbook, XL_CELL_EMPTY, XL_CELL_TEXT, XL_CELL_NUMBER
    null_cell_types = (XL_CELL_EMPTY, )

def cells_all_junk(cells, is_rubbish=None):
    """\
    Return True if all cells in the sequence are junk.
    What qualifies as junk:
    -- empty cell
    -- blank cell
    -- zero-length text
    -- text is all whitespace
    -- number cell and is 0.0
    -- text cell and is_rubbish(cell.value) returns True.
    """
    for cell in cells:
        if cell.ctype in null_cell_types:
            continue
        if cell.ctype == XL_CELL_TEXT:
            if not cell.value:
                continue
            if cell.value.isspace():
                continue
        if cell.ctype == XL_CELL_NUMBER:
            if not cell.value:
                continue
        if is_rubbish is not None and is_rubbish(cell):
            continue
        return False
    return True

def ispunc(c, s=set(unicode(string.punctuation))):
    """Return True if c is a single punctuation character"""
    return c in s

def number_of_good_rows(sheet, checker=None, nrows=None, ncols=None):
    """Return 1 + the index of the last row with meaningful data in it."""
    if nrows is None: nrows = sheet.nrows
    if ncols is None: ncols = sheet.ncols
    for rowx in xrange(nrows - 1, -1, -1):
        if not cells_all_junk(sheet.row_slice(rowx, 0, ncols), checker):
            return rowx + 1
    return 0

def number_of_good_cols(sheet, checker=None, nrows=None, ncols=None):
    """Return 1 + the index of the last column with meaningful data in it."""
    if nrows is None: nrows = sheet.nrows
    if ncols is None: ncols = sheet.ncols
    for colx in xrange(ncols - 1, -1, -1):
        if not cells_all_junk(sheet.col_slice(colx, 0, nrows), checker):
            return colx+1
    return 0

def safe_encode(ustr, encoding):
    try:
        return ustr.encode(encoding)
    except (UnicodeEncodeError, UnicodeError):
        return repr(ustr)

def check_file(fname, verbose, do_punc=False, fmt_info=0, encoding='ascii', onesheet=''):
    print
    print fname
    if do_punc:
        checker = ispunc
    else:
        checker = None
    try:
        book = open_workbook(fname, formatting_info=fmt_info, on_demand=True)
    except TypeError:
        try:
            book = open_workbook(fname, formatting_info=fmt_info)
        except TypeError:
            # this is becoming ridiculous
            book = open_workbook(fname)
    totold = totnew = totnotnull = 0
    if onesheet is None or onesheet == "":
        shxrange = range(book.nsheets)
    else:
        try:
            shxrange = [int(onesheet)]
        except ValueError:
            shxrange = [book.sheet_names().index(onesheet)]
    for shx in shxrange:
        sheet = book.sheet_by_index(shx)
        ngoodrows = number_of_good_rows(sheet, checker)
        ngoodcols = number_of_good_cols(sheet, checker, nrows=ngoodrows)
        oldncells = sheet.nrows * sheet.ncols
        newncells = ngoodrows * ngoodcols
        totold += oldncells
        totnew += newncells
        nnotnull = 0
        sheet_density_pct_s = ''
        if verbose >= 2:
            colxrange = range(ngoodcols)
            for rowx in xrange(ngoodrows):
                rowtypes = sheet.row_types(rowx)
                for colx in colxrange:
                    if rowtypes[colx] not in null_cell_types:
                        nnotnull += 1
            totnotnull += nnotnull
            sheet_density_pct = (nnotnull * 100.0) / max(1, newncells)
            sheet_density_pct_s = "; den = %5.1f%%" % sheet_density_pct
        if verbose >= 3:
            # which rows have non_empty cells in the right-most column?
            lastcolx = sheet.ncols - 1
            for rowx in xrange(sheet.nrows):
                cell = sheet.cell(rowx, lastcolx)
                if cell.ctype != XL_CELL_EMPTY:
                    print "%s (%d, %d): type %d, value %r" % (
                        cellname(rowx, lastcolx), rowx, lastcolx, cell.ctype, cell.value)
        if (verbose
            or ngoodrows != sheet.nrows
            or ngoodcols != sheet.ncols
            or (verbose >= 2 and ngoodcells and sheet_density_pct < 90.0)
            ):
            if oldncells:
                pctwaste = (1.0 - float(newncells) / oldncells) * 100.0
            else:
                pctwaste = 0.0
            shname_enc = safe_encode(sheet.name, encoding)
            print "sheet #%2d: RxC %5d x %3d => %5d x %3d; %4.1f%% waste%s (%s)" \
                % (shx, sheet.nrows, sheet.ncols,
                    ngoodrows, ngoodcols, pctwaste, sheet_density_pct_s, shname_enc)
        if hasattr(book, 'unload_sheet'):
            book.unload_sheet(shx)
    if totold:
        pctwaste = (1.0 - float(totnew) / totold) * 100.0
    else:
        pctwaste = 0.0
    print "%d cells => %d cells; %4.1f%% waste" % (totold, totnew, pctwaste)

def main():
    import optparse
    usage = "%prog [options] input-file-patterns"
    oparser = optparse.OptionParser(usage)
    oparser.add_option(
        "-v", "--verbosity",
        type="int", default=0,
        help="level of information and diagnostics provided")
    oparser.add_option(
        "-p", "--punc",
        action="store_true", default=False,
        help="treat text cells containing only 1 punctuation char as rubbish")
    oparser.add_option(
        "-e", "--encoding",
        default='',
        help="encoding for text output")
    oparser.add_option(
        "-f", "--formatting",
        action="store_true", default=False,
        help="parse formatting information in the input files")
    oparser.add_option(
        "-s", "--onesheet",
        default="",
        help="restrict output to this sheet (name or index)")
    options, args = oparser.parse_args(sys.argv[1:])
    if len(args) < 1:
        oparser.error("Expected at least 1 arg, found %d" % len(args))
    encoding = options.encoding
    if not encoding:
        encoding = sys.stdout.encoding
    if not encoding:
        encoding = sys.getdefaultencoding()
    for pattern in args:
        for fname in glob.glob(pattern):
            try:
                check_file(fname,
                    options.verbosity, options.punc,
                    options.formatting, encoding, options.onesheet)
            except:
                e1, e2 = sys.exc_info()[:2]
                print "*** File %s => %s:%s" % (fname, e1.__name__, e2)

if __name__ == "__main__":
    main()


text:
using IdentityServer4.Validation;
using Microsoft.Extensions.Logging;
using System;

namespace IdentityServerHost.Extensions
{
    public class ParameterizedScopeParser : DefaultScopeParser
    {
        public ParameterizedScopeParser(ILogger<DefaultScopeParser> logger) : base(logger)
        {
        }

        public override void ParseScopeValue(ParseScopeContext scopeContext)
        {
            const string transactionScopeName = "transaction";
            const string separator = ":";
            const string transactionScopePrefix = transactionScopeName + separator;

            var scopeValue = scopeContext.RawValue;

            if (scopeValue.StartsWith(transactionScopePrefix))
            {
                // we get in here with a scope like "transaction:something"
                var parts = scopeValue.Split(separator, StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length == 2)
                {
                    scopeContext.SetParsedValues(transactionScopeName, parts[1]);
                }
                else
                {
                    scopeContext.SetError("transaction scope missing transaction parameter value");
                }
            }
            else if (scopeValue != transactionScopeName)
            {
                // we get in here with a scope not like "transaction"
                base.ParseScopeValue(scopeContext);
            }
            else
            {
                // we get in here with a scope exactly "transaction", which is to say we're ignoring it
                // and not including it in the results
                scopeContext.SetIgnore();
            }
        }
    }
}