text:
package com.cmcti.cmts.domain.model;

import com.cmcti.cmts.domain.service.ClpSerializer;
import com.cmcti.cmts.domain.service.MerchantScopeLocalServiceUtil;

import com.liferay.portal.kernel.bean.AutoEscapeBeanHandler;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ProxyUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.model.BaseModel;
import com.liferay.portal.model.impl.BaseModelImpl;
import com.liferay.portal.util.PortalUtil;

import java.io.Serializable;

import java.lang.reflect.Method;

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

/**
 * @author richard
 */
public class MerchantScopeClp extends BaseModelImpl<MerchantScope>
	implements MerchantScope {
	public MerchantScopeClp() {
	}

	@Override
	public Class<?> getModelClass() {
		return MerchantScope.class;
	}

	@Override
	public String getModelClassName() {
		return MerchantScope.class.getName();
	}

	@Override
	public long getPrimaryKey() {
		return _merchantScopeId;
	}

	@Override
	public void setPrimaryKey(long primaryKey) {
		setMerchantScopeId(primaryKey);
	}

	@Override
	public Serializable getPrimaryKeyObj() {
		return _merchantScopeId;
	}

	@Override
	public void setPrimaryKeyObj(Serializable primaryKeyObj) {
		setPrimaryKey(((Long)primaryKeyObj).longValue());
	}

	@Override
	public Map<String, Object> getModelAttributes() {
		Map<String, Object> attributes = new HashMap<String, Object>();

		attributes.put("merchantScopeId", getMerchantScopeId());
		attributes.put("groupId", getGroupId());
		attributes.put("companyId", getCompanyId());
		attributes.put("userId", getUserId());
		attributes.put("userName", getUserName());
		attributes.put("createDate", getCreateDate());
		attributes.put("modifiedDate", getModifiedDate());
		attributes.put("cmtsId", getCmtsId());
		attributes.put("ifIndex", getIfIndex());
		attributes.put("merchantCode", getMerchantCode());

		return attributes;
	}

	@Override
	public void setModelAttributes(Map<String, Object> attributes) {
		Long merchantScopeId = (Long)attributes.get("merchantScopeId");

		if (merchantScopeId != null) {
			setMerchantScopeId(merchantScopeId);
		}

		Long groupId = (Long)attributes.get("groupId");

		if (groupId != null) {
			setGroupId(groupId);
		}

		Long companyId = (Long)attributes.get("companyId");

		if (companyId != null) {
			setCompanyId(companyId);
		}

		Long userId = (Long)attributes.get("userId");

		if (userId != null) {
			setUserId(userId);
		}

		String userName = (String)attributes.get("userName");

		if (userName != null) {
			setUserName(userName);
		}

		Date createDate = (Date)attributes.get("createDate");

		if (createDate != null) {
			setCreateDate(createDate);
		}

		Date modifiedDate = (Date)attributes.get("modifiedDate");

		if (modifiedDate != null) {
			setModifiedDate(modifiedDate);
		}

		Long cmtsId = (Long)attributes.get("cmtsId");

		if (cmtsId != null) {
			setCmtsId(cmtsId);
		}

		Integer ifIndex = (Integer)attributes.get("ifIndex");

		if (ifIndex != null) {
			setIfIndex(ifIndex);
		}

		String merchantCode = (String)attributes.get("merchantCode");

		if (merchantCode != null) {
			setMerchantCode(merchantCode);
		}
	}

	@Override
	public long getMerchantScopeId() {
		return _merchantScopeId;
	}

	@Override
	public void setMerchantScopeId(long merchantScopeId) {
		_merchantScopeId = merchantScopeId;

		if (_merchantScopeRemoteModel != null) {
			try {
				Class<?> clazz = _merchantScopeRemoteModel.getClass();

				Method method = clazz.getMethod("setMerchantScopeId", long.class);

				method.invoke(_merchantScopeRemoteModel, merchantScopeId);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public long getGroupId() {
		return _groupId;
	}

	@Override
	public void setGroupId(long groupId) {
		_groupId = groupId;

		if (_merchantScopeRemoteModel != null) {
			try {
				Class<?> clazz = _merchantScopeRemoteModel.getClass();

				Method method = clazz.getMethod("setGroupId", long.class);

				method.invoke(_merchantScopeRemoteModel, groupId);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public long getCompanyId() {
		return _companyId;
	}

	@Override
	public void setCompanyId(long companyId) {
		_companyId = companyId;

		if (_merchantScopeRemoteModel != null) {
			try {
				Class<?> clazz = _merchantScopeRemoteModel.getClass();

				Method method = clazz.getMethod("setCompanyId", long.class);

				method.invoke(_merchantScopeRemoteModel, companyId);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public long getUserId() {
		return _userId;
	}

	@Override
	public void setUserId(long userId) {
		_userId = userId;

		if (_merchantScopeRemoteModel != null) {
			try {
				Class<?> clazz = _merchantScopeRemoteModel.getClass();

				Method method = clazz.getMethod("setUserId", long.class);

				method.invoke(_merchantScopeRemoteModel, userId);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public String getUserUuid() throws SystemException {
		return PortalUtil.getUserValue(getUserId(), "uuid", _userUuid);
	}

	@Override
	public void setUserUuid(String userUuid) {
		_userUuid = userUuid;
	}

	@Override
	public String getUserName() {
		return _userName;
	}

	@Override
	public void setUserName(String userName) {
		_userName = userName;

		if (_merchantScopeRemoteModel != null) {
			try {
				Class<?> clazz = _merchantScopeRemoteModel.getClass();

				Method method = clazz.getMethod("setUserName", String.class);

				method.invoke(_merchantScopeRemoteModel, userName);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public Date getCreateDate() {
		return _createDate;
	}

	@Override
	public void setCreateDate(Date createDate) {
		_createDate = createDate;

		if (_merchantScopeRemoteModel != null) {
			try {
				Class<?> clazz = _merchantScopeRemoteModel.getClass();

				Method method = clazz.getMethod("setCreateDate", Date.class);

				method.invoke(_merchantScopeRemoteModel, createDate);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public Date getModifiedDate() {
		return _modifiedDate;
	}

	@Override
	public void setModifiedDate(Date modifiedDate) {
		_modifiedDate = modifiedDate;

		if (_merchantScopeRemoteModel != null) {
			try {
				Class<?> clazz = _merchantScopeRemoteModel.getClass();

				Method method = clazz.getMethod("setModifiedDate", Date.class);

				method.invoke(_merchantScopeRemoteModel, modifiedDate);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public long getCmtsId() {
		return _cmtsId;
	}

	@Override
	public void setCmtsId(long cmtsId) {
		_cmtsId = cmtsId;

		if (_merchantScopeRemoteModel != null) {
			try {
				Class<?> clazz = _merchantScopeRemoteModel.getClass();

				Method method = clazz.getMethod("setCmtsId", long.class);

				method.invoke(_merchantScopeRemoteModel, cmtsId);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public int getIfIndex() {
		return _ifIndex;
	}

	@Override
	public void setIfIndex(int ifIndex) {
		_ifIndex = ifIndex;

		if (_merchantScopeRemoteModel != null) {
			try {
				Class<?> clazz = _merchantScopeRemoteModel.getClass();

				Method method = clazz.getMethod("setIfIndex", int.class);

				method.invoke(_merchantScopeRemoteModel, ifIndex);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public String getMerchantCode() {
		return _merchantCode;
	}

	@Override
	public void setMerchantCode(String merchantCode) {
		_merchantCode = merchantCode;

		if (_merchantScopeRemoteModel != null) {
			try {
				Class<?> clazz = _merchantScopeRemoteModel.getClass();

				Method method = clazz.getMethod("setMerchantCode", String.class);

				method.invoke(_merchantScopeRemoteModel, merchantCode);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	public BaseModel<?> getMerchantScopeRemoteModel() {
		return _merchantScopeRemoteModel;
	}

	public void setMerchantScopeRemoteModel(
		BaseModel<?> merchantScopeRemoteModel) {
		_merchantScopeRemoteModel = merchantScopeRemoteModel;
	}

	public Object invokeOnRemoteModel(String methodName,
		Class<?>[] parameterTypes, Object[] parameterValues)
		throws Exception {
		Object[] remoteParameterValues = new Object[parameterValues.length];

		for (int i = 0; i < parameterValues.length; i++) {
			if (parameterValues[i] != null) {
				remoteParameterValues[i] = ClpSerializer.translateInput(parameterValues[i]);
			}
		}

		Class<?> remoteModelClass = _merchantScopeRemoteModel.getClass();

		ClassLoader remoteModelClassLoader = remoteModelClass.getClassLoader();

		Class<?>[] remoteParameterTypes = new Class[parameterTypes.length];

		for (int i = 0; i < parameterTypes.length; i++) {
			if (parameterTypes[i].isPrimitive()) {
				remoteParameterTypes[i] = parameterTypes[i];
			}
			else {
				String parameterTypeName = parameterTypes[i].getName();

				remoteParameterTypes[i] = remoteModelClassLoader.loadClass(parameterTypeName);
			}
		}

		Method method = remoteModelClass.getMethod(methodName,
				remoteParameterTypes);

		Object returnValue = method.invoke(_merchantScopeRemoteModel,
				remoteParameterValues);

		if (returnValue != null) {
			returnValue = ClpSerializer.translateOutput(returnValue);
		}

		return returnValue;
	}

	@Override
	public void persist() throws SystemException {
		if (this.isNew()) {
			MerchantScopeLocalServiceUtil.addMerchantScope(this);
		}
		else {
			MerchantScopeLocalServiceUtil.updateMerchantScope(this);
		}
	}

	@Override
	public MerchantScope toEscapedModel() {
		return (MerchantScope)ProxyUtil.newProxyInstance(MerchantScope.class.getClassLoader(),
			new Class[] { MerchantScope.class }, new AutoEscapeBeanHandler(this));
	}

	@Override
	public Object clone() {
		MerchantScopeClp clone = new MerchantScopeClp();

		clone.setMerchantScopeId(getMerchantScopeId());
		clone.setGroupId(getGroupId());
		clone.setCompanyId(getCompanyId());
		clone.setUserId(getUserId());
		clone.setUserName(getUserName());
		clone.setCreateDate(getCreateDate());
		clone.setModifiedDate(getModifiedDate());
		clone.setCmtsId(getCmtsId());
		clone.setIfIndex(getIfIndex());
		clone.setMerchantCode(getMerchantCode());

		return clone;
	}

	@Override
	public int compareTo(MerchantScope merchantScope) {
		long primaryKey = merchantScope.getPrimaryKey();

		if (getPrimaryKey() < primaryKey) {
			return -1;
		}
		else if (getPrimaryKey() > primaryKey) {
			return 1;
		}
		else {
			return 0;
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (!(obj instanceof MerchantScopeClp)) {
			return false;
		}

		MerchantScopeClp merchantScope = (MerchantScopeClp)obj;

		long primaryKey = merchantScope.getPrimaryKey();

		if (getPrimaryKey() == primaryKey) {
			return true;
		}
		else {
			return false;
		}
	}

	public Class<?> getClpSerializerClass() {
		return _clpSerializerClass;
	}

	@Override
	public int hashCode() {
		return (int)getPrimaryKey();
	}

	@Override
	public String toString() {
		StringBundler sb = new StringBundler(21);

		sb.append("{merchantScopeId=");
		sb.append(getMerchantScopeId());
		sb.append(", groupId=");
		sb.append(getGroupId());
		sb.append(", companyId=");
		sb.append(getCompanyId());
		sb.append(", userId=");
		sb.append(getUserId());
		sb.append(", userName=");
		sb.append(getUserName());
		sb.append(", createDate=");
		sb.append(getCreateDate());
		sb.append(", modifiedDate=");
		sb.append(getModifiedDate());
		sb.append(", cmtsId=");
		sb.append(getCmtsId());
		sb.append(", ifIndex=");
		sb.append(getIfIndex());
		sb.append(", merchantCode=");
		sb.append(getMerchantCode());
		sb.append("}");

		return sb.toString();
	}

	@Override
	public String toXmlString() {
		StringBundler sb = new StringBundler(34);

		sb.append("<model><model-name>");
		sb.append("com.cmcti.cmts.domain.model.MerchantScope");
		sb.append("</model-name>");

		sb.append(
			"<column><column-name>merchantScopeId</column-name><column-value><![CDATA[");
		sb.append(getMerchantScopeId());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>groupId</column-name><column-value><![CDATA[");
		sb.append(getGroupId());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>companyId</column-name><column-value><![CDATA[");
		sb.append(getCompanyId());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>userId</column-name><column-value><![CDATA[");
		sb.append(getUserId());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>userName</column-name><column-value><![CDATA[");
		sb.append(getUserName());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>createDate</column-name><column-value><![CDATA[");
		sb.append(getCreateDate());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>modifiedDate</column-name><column-value><![CDATA[");
		sb.append(getModifiedDate());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>cmtsId</column-name><column-value><![CDATA[");
		sb.append(getCmtsId());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>ifIndex</column-name><column-value><![CDATA[");
		sb.append(getIfIndex());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>merchantCode</column-name><column-value><![CDATA[");
		sb.append(getMerchantCode());
		sb.append("]]></column-value></column>");

		sb.append("</model>");

		return sb.toString();
	}

	private long _merchantScopeId;
	private long _groupId;
	private long _companyId;
	private long _userId;
	private String _userUuid;
	private String _userName;
	private Date _createDate;
	private Date _modifiedDate;
	private long _cmtsId;
	private int _ifIndex;
	private String _merchantCode;
	private BaseModel<?> _merchantScopeRemoteModel;
	private Class<?> _clpSerializerClass = com.cmcti.cmts.domain.service.ClpSerializer.class;
}


text:
<?php

namespace Drupal\Core\Routing;

use Drupal\Component\Utility\UrlHelper;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Provides helpers for redirect destinations.
 */
class RedirectDestination implements RedirectDestinationInterface {

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * The URL generator.
   *
   * @var \Drupal\Core\Routing\UrlGeneratorInterface
   */
  protected $urlGenerator;

  /**
   * The destination used by the current request.
   *
   * @var string
   */
  protected $destination;

  /**
   * Constructs a new RedirectDestination instance.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
   *   The URL generator.
   */
  public function __construct(RequestStack $request_stack, UrlGeneratorInterface $url_generator) {
    $this->requestStack = $request_stack;
    $this->urlGenerator = $url_generator;
  }

  /**
   * {@inheritdoc}
   */
  public function getAsArray() {
    return ['destination' => $this->get()];
  }

  /**
   * {@inheritdoc}
   */
  public function get() {
    if (!isset($this->destination)) {
      $query = $this->requestStack->getCurrentRequest()->query;
      if (UrlHelper::isExternal($query->get('destination'))) {
        $this->destination = '/';
      }
      elseif ($query->has('destination')) {
        $this->destination = $query->get('destination');
      }
      else {
        $this->destination = $this->urlGenerator->generateFromRoute('<current>', [], ['query' => UrlHelper::filterQueryParameters($query->all())]);
      }
    }

    return $this->destination;
  }

  /**
   * {@inheritdoc}
   */
  public function set($new_destination) {
    $this->destination = $new_destination;
    return $this;
  }

}

redpajama_set_name: RedPajamaGithub


text:
package com.jesus_crie.kankanbot.logging;

public enum LogFrom {

    COMMAND("[Command] "),
    LISTENER("[Listener] "),
    STARTUP("[Startup] "),
    RUNNING("[Running] "),
    MUSIC("[Music] "),
    WARFRAME("[Warframe] ");

    private String value;

    LogFrom (String v) {
        value = v;
    }
    @Override
    public String toString() { return value; }
}


text:
module Combine
  module ReaperFactory
    def self.build

    end
  end
end


text:
"""A simple web crawler -- classes implementing crawling logic."""

import asyncio
import cgi
from http.client import BadStatusLine
import logging
import re
import time
import urllib.parse

logger = logging.getLogger(__name__)

def unescape(s):
    """The inverse of cgi.escape()."""
    s = s.replace('"', '"').replace('>', '>').replace('<', '<')
    return s.replace('&', '&')  # Must be last.

class ConnectionPool:
    """A connection pool.

    To open a connection, use reserve().  To recycle it, use unreserve().

    The pool is mostly just a mapping from (host, port, ssl) tuples to
    lists of Connections.  The currently active connections are *not*
    in the data structure; get_connection() takes the connection out,
    and recycle_connection() puts it back in.  To recycle a
    connection, call conn.close(recycle=True).

    There are limits to both the overall pool and the per-key pool.
    """

    def __init__(self, max_pool=10, max_tasks=5):
        self.max_pool = max_pool  # Overall limit.
        self.max_tasks = max_tasks  # Per-key limit.
        self.loop = asyncio.get_event_loop()
        self.connections = {}  # {(host, port, ssl): [Connection, ...], ...}
        self.queue = []  # [Connection, ...]

    def close(self):
        """Close all connections available for reuse."""
        for conns in self.connections.values():
            for conn in conns:
                conn.close()
        self.connections.clear()
        self.queue.clear()

    @asyncio.coroutine
    def get_connection(self, host, port, ssl):
        """Create or reuse a connection."""
        port = port or (443 if ssl else 80)
        try:
            ipaddrs = yield from self.loop.getaddrinfo(host, port)
        except Exception as exc:
            logger.error('Exception %r for (%r, %r)', exc, host, port)
            raise
        logger.warn('* %s resolves to %s',
                    host, ', '.join(ip[4][0] for ip in ipaddrs))

        # Look for a reusable connection.
        for _, _, _, _, (h, p, *_) in ipaddrs:
            key = h, p, ssl
            conn = None
            conns = self.connections.get(key)
            while conns:
                conn = conns.pop(0)
                self.queue.remove(conn)
                if not conns:
                    del self.connections[key]
                if conn.stale():
                    logger.warn('closing stale connection %r', key)
                    conn.close()  # Just in case.
                else:
                    logger.warn('* Reusing pooled connection %r', key)
                    return conn

        # Create a new connection.
        conn = Connection(self, host, port, ssl)
        yield from conn.connect()
        logger.warn('* New connection %r', conn.key)
        return conn

    def recycle_connection(self, conn):
        """Make a connection available for reuse.

        This also prunes the pool if it exceeds the size limits.
        """
        conns = self.connections.setdefault(conn.key, [])
        conns.append(conn)
        self.queue.append(conn)

        if len(conns) > self.max_tasks:
            victims = conns  # Prune one connection for this key.
        elif len(self.queue) > self.max_pool:
            victims = self.queue  # Prune one connection for any key.
        else:
            return

        for victim in victims:
            if victim.stale():  # Prefer pruning the oldest stale connection.
                logger.warn('closing stale connection %r', victim.key)
                break
        else:
            victim = victims[0]
            logger.warn('closing oldest connection %r', victim.key)

        conns = self.connections[victim.key]
        conns.remove(victim)
        if not conns:
            del self.connections[victim.key]
        self.queue.remove(victim)
        victim.close()

class Connection:
    """A connection that can be recycled to the pool."""

    def __init__(self, pool, host, port, ssl):
        self.pool = pool
        self.host = host
        self.port = port
        self.ssl = ssl
        self.reader = None
        self.writer = None
        self.key = None

    def stale(self):
        return self.reader is None or self.reader.at_eof()

    @asyncio.coroutine
    def connect(self):
        self.reader, self.writer = yield from asyncio.open_connection(
            self.host, self.port, ssl=self.ssl)
        peername = self.writer.get_extra_info('peername')
        if peername:
            self.host, self.port = peername[:2]
        else:
            logger.warn('NO PEERNAME %r %r %r', self.host, self.port, self.ssl)
        self.key = self.host, self.port, self.ssl

    def close(self, recycle=False):
        if recycle and not self.stale():
            self.pool.recycle_connection(self)
        else:
            self.writer.close()
            self.pool = self.reader = self.writer = None

@asyncio.coroutine
def make_request(url, pool, *, method='GET', headers=None, version='1.1'):
    """Start an HTTP request.  Return a Connection."""
    parts = urllib.parse.urlparse(url)
    assert parts.scheme in ('http', 'https'), repr(url)
    ssl = parts.scheme == 'https'
    port = parts.port or (443 if ssl else 80)
    path = parts.path or '/'
    path = '%s?%s' % (path, parts.query) if parts.query else path

    logger.warn('* Connecting to %s:%s using %s for %s',
                parts.hostname, port, 'ssl' if ssl else 'tcp', url)
    conn = yield from pool.get_connection(parts.hostname, port, ssl)

    headers = dict(headers) if headers else {}  # Must use Cap-Words.
    headers.setdefault('User-Agent', 'asyncio-example-crawl/0.0')
    headers.setdefault('Host', parts.netloc)
    headers.setdefault('Accept', '*/*')
    lines = ['%s %s HTTP/%s' % (method, path, version)]
    lines.extend('%s: %s' % kv for kv in headers.items())
    for line in lines + ['']:
        logger.info('> %s', line)
    # TODO: close conn if this fails.
    conn.writer.write('\r\n'.join(lines + ['', '']).encode('latin-1'))

    return conn  # Caller must send body if desired, then call read_response().

@asyncio.coroutine
def read_response(conn):
    """Read an HTTP response from a connection."""

    @asyncio.coroutine
    def getline():
        line = (yield from conn.reader.readline()).decode('latin-1').rstrip()
        logger.info('< %s', line)
        return line

    status_line = yield from getline()
    status_parts = status_line.split(None, 2)
    if len(status_parts) != 3 or not status_parts[1].isdigit():
        logger.error('bad status_line %r', status_line)
        raise BadStatusLine(status_line)
    http_version, status, reason = status_parts
    status = int(status)

    headers = {}
    while True:
        header_line = yield from getline()
        if not header_line:
            break
        key, value = header_line.split(':', 1)
        # TODO: Continuation lines; multiple header lines per key..
        headers[key.lower()] = value.lstrip()

    if 'content-length' in headers:
        nbytes = int(headers['content-length'])
        output = asyncio.StreamReader()
        asyncio.async(length_handler(nbytes, conn.reader, output))
    elif headers.get('transfer-encoding') == 'chunked':
        output = asyncio.StreamReader()
        asyncio.async(chunked_handler(conn.reader, output))
    else:
        output = conn.reader

    return http_version[5:], status, reason, headers, output

@asyncio.coroutine
def length_handler(nbytes, input, output):
    """Async handler for reading a body given a Content-Length header."""
    while nbytes > 0:
        buffer = yield from input.read(min(nbytes, 256*1024))
        if not buffer:
            logger.error('premature end for content-length')
            output.set_exception(EOFError())
            return
        output.feed_data(buffer)
        nbytes -= len(buffer)
    output.feed_eof()

@asyncio.coroutine
def chunked_handler(input, output):
    """Async handler for reading a body using Transfer-Encoding: chunked."""
    logger.info('parsing chunked response')
    nblocks = 0
    nbytes = 0
    while True:
        size_header = yield from input.readline()
        if not size_header:
            logger.error('premature end of chunked response')
            output.set_exception(EOFError())
            return
        logger.debug('size_header = %r', size_header)
        parts = size_header.split(b';')
        size = int(parts[0], 16)
        nblocks += 1
        nbytes += size
        if size:
            logger.debug('reading chunk of %r bytes', size)
            block = yield from input.readexactly(size)
            assert len(block) == size, (len(block), size)
            output.feed_data(block)
        crlf = yield from input.readline()
        assert crlf == b'\r\n', repr(crlf)
        if not size:
            break
    logger.warn('chunked response had %r bytes in %r blocks', nbytes, nblocks)
    output.feed_eof()

class Fetcher:
    """Logic and state for one URL.

    When found in crawler.busy, this represents a URL to be fetched or
    in the process of being fetched; when found in crawler.done, this
    holds the results from fetching it.

    This is usually associated with a task.  This references the
    crawler for the connection pool and to add more URLs to its todo
    list.

    Call fetch() to do the fetching; results are in instance variables.
    """

    def __init__(self, url, crawler, max_redirect=10, max_tries=4):
        self.url = url
        self.crawler = crawler
        # We don't loop resolving redirects here -- we just use this
        # to decide whether to add the redirect URL to crawler.todo.
        self.max_redirect = max_redirect
        # But we do loop to retry on errors a few times.
        self.max_tries = max_tries
        # Everything we collect from the response goes here.
        self.task = None
        self.exceptions = []
        self.tries = 0
        self.conn = None
        self.status = None
        self.headers = None
        self.body = None
        self.next_url = None
        self.ctype = None
        self.pdict = None
        self.encoding = None
        self.urls = None
        self.new_urls = None

    @asyncio.coroutine
    def fetch(self):
        """Attempt to fetch the contents of the URL.

        If successful, and the data is HTML, extract further links and
        add them to the crawler.  Redirects are also added back there.
        """
        while self.tries < self.max_tries:
            self.tries += 1
            conn = None
            try:
                conn = yield from make_request(self.url, self.crawler.pool)
                _, status, _, headers, output = yield from read_response(conn)
                self.status, self.headers = status, headers
                self.body = yield from output.read()
                h_conn = headers.get('connection', '').lower()
                if h_conn != 'close':
                    conn.close(recycle=True)
                    conn = None
                if self.tries > 1:
                    logger.warn('try %r for %r success', self.tries, self.url)
                break
            except (BadStatusLine, OSError) as exc:
                self.exceptions.append(exc)
                logger.warn('try %r for %r raised %r',
                            self.tries, self.url, exc)
            finally:
                if conn is not None:
                    conn.close()
        else:
            # We never broke out of the while loop, i.e. all tries failed.
            logger.error('no success for %r in %r tries',
                         self.url, self.max_tries)
            return
        if status in (300, 301, 302, 303, 307) and headers.get('location'):
            next_url = headers['location']
            self.next_url = urllib.parse.urljoin(self.url, next_url)
            if self.max_redirect > 0:
                logger.warn('redirect to %r from %r', self.next_url, self.url)
                self.crawler.add_url(self.next_url, self.max_redirect-1)
            else:
                logger.error('redirect limit reached for %r from %r',
                             self.next_url, self.url)
        else:
            if status == 200:
                self.ctype = headers.get('content-type')
                self.pdict = {}
                if self.ctype:
                    self.ctype, self.pdict = cgi.parse_header(self.ctype)
                self.encoding = self.pdict.get('charset', 'utf-8')
                if self.ctype == 'text/html':
                    body = self.body.decode(self.encoding, 'replace')
                    # Replace href with (?:href|src) to follow image links.
                    self.urls = set(re.findall(r'(?i)href=["\']?([^\s"\'<>]+)',
                                               body))
                    if self.urls:
                        logger.warn('got %r distinct urls from %r',
                                    len(self.urls), self.url)
                    self.new_urls = set()
                    for url in self.urls:
                        url = unescape(url)
                        url = urllib.parse.urljoin(self.url, url)
                        url, frag = urllib.parse.urldefrag(url)
                        if self.crawler.add_url(url):
                            self.new_urls.add(url)

class Crawler:
    """Crawl a set of URLs.

    This manages three disjoint sets of URLs (todo, busy, done).  The
    data structures actually store dicts -- the values in todo give
    the redirect limit, while the values in busy and done are Fetcher
    instances.
    """
    def __init__(self, roots,
                 exclude=None, strict=True,  # What to crawl.
                 max_redirect=10, max_tries=4,  # Per-url limits.
                 max_tasks=10, max_pool=10,  # Global limits.
                 ):
        self.roots = roots
        self.exclude = exclude
        self.strict = strict
        self.max_redirect = max_redirect
        self.max_tries = max_tries
        self.max_tasks = max_tasks
        self.max_pool = max_pool
        self.todo = {}
        self.busy = {}
        self.done = {}
        self.pool = ConnectionPool(max_pool, max_tasks)
        self.root_domains = set()
        for root in roots:
            parts = urllib.parse.urlparse(root)
            host, port = urllib.parse.splitport(parts.netloc)
            if not host:
                continue
            if re.match(r'\A[\d\.]*\Z', host):
                self.root_domains.add(host)
            else:
                host = host.lower()
                if self.strict:
                    self.root_domains.add(host)
                else:
                    self.root_domains.add(host)
        for root in roots:
            self.add_url(root)
        self.governor = asyncio.Semaphore(max_tasks)
        self.termination = asyncio.Condition()
        self.t0 = time.time()
        self.t1 = None

    def close(self):
        """Close resources (currently only the pool)."""
        self.pool.close()

    def host_okay(self, host):
        """Check if a host should be crawled.

        A literal match (after lowercasing) is always good.  For hosts
        that don't look like IP addresses, some approximate matches
        are okay depending on the strict flag.
        """
        host = host.lower()
        if host in self.root_domains:
            return True
        if re.match(r'\A[\d\.]*\Z', host):
            return False
        if self.strict:
            return self._host_okay_strictish(host)
        else:
            return self._host_okay_lenient(host)

    def _host_okay_strictish(self, host):
        """Check if a host should be crawled, strict-ish version.

        This checks for equality modulo an initial 'www.' component.
        """
        host = host[4:] if host.startswith('www.') else 'www.' + host
        return host in self.root_domains

    def _host_okay_lenient(self, host):
        """Check if a host should be crawled, lenient version.

        This compares the last two components of the host.
        """
        return host in self.root_domains

    def add_url(self, url, max_redirect=None):
        """Add a URL to the todo list if not seen before."""
        if self.exclude and re.search(self.exclude, url):
            return False
        parts = urllib.parse.urlparse(url)
        if parts.scheme not in ('http', 'https'):
            logger.info('skipping non-http scheme in %r', url)
            return False
        host, port = urllib.parse.splitport(parts.netloc)
        if not self.host_okay(host):
            logger.info('skipping non-root host in %r', url)
            return False
        if max_redirect is None:
            max_redirect = self.max_redirect
        if url in self.todo or url in self.busy or url in self.done:
            return False
        logger.warn('adding %r %r', url, max_redirect)
        self.todo[url] = max_redirect
        return True

    @asyncio.coroutine
    def crawl(self):
        """Run the crawler until all finished."""
        with (yield from self.termination):
            while self.todo or self.busy:
                if self.todo:
                    url, max_redirect = self.todo.popitem()
                    fetcher = Fetcher(url,
                                      crawler=self,
                                      max_redirect=max_redirect,
                                      max_tries=self.max_tries,
                                      )
                    self.busy[url] = fetcher
                    fetcher.task = asyncio.Task(self.fetch(fetcher))
                else:
                    yield from self.termination.wait()
        self.t1 = time.time()

    @asyncio.coroutine
    def fetch(self, fetcher):
        """Call the Fetcher's fetch(), with a limit on concurrency.

        Once this returns, move the fetcher from busy to done.
        """
        url = fetcher.url
        with (yield from self.governor):
            try:
                yield from fetcher.fetch()  # Fetcher gonna fetch.
            finally:
                # Force GC of the task, so the error is logged.
                fetcher.task = None
        with (yield from self.termination):
            self.done[url] = fetcher
            del self.busy[url]
            self.termination.notify()


text:
package eu.bcvsolutions.idm.acc.config.domain;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder;

import eu.bcvsolutions.idm.acc.service.impl.DefaultPasswordFilterManager;
import eu.bcvsolutions.idm.core.api.service.Configurable;
import eu.bcvsolutions.idm.core.api.service.ConfigurationService;

/**
 * Configuration for password filter encoder
 *
 * @author Ondrej Kopr
 * @since 10.5.0
 *
 */
public interface PasswordFilterEncoderConfiguration extends Configurable {

	/**
	 * Configuration for initialize {@link SCryptPasswordEncoder}. Default values are from standard constructor.
	 * {@link PasswordEncoder} is initialized int {@link DefaultPasswordFilterManager} in constructor! All configuration options can
	 * be changed only with restart!
	 */
	String PROPERTY_SCRYPT_CPU_COST = ConfigurationService.IDM_PRIVATE_PROPERTY_PREFIX
			+ "acc.passFilter.scrypt.cpuCost";
	int DEFAULT_SCRYPT_CPU_COST = 16384;

	String PROPERTY_SCRYPT_MEMORY_COST = ConfigurationService.IDM_PRIVATE_PROPERTY_PREFIX
			+ "acc.passFilter.scrypt.memoryCost";
	int DEFAULT_SCRYPT_MEMORY_COST = 8;

	String PROPERTY_SCRYPT_PARALLELIZATION = ConfigurationService.IDM_PRIVATE_PROPERTY_PREFIX
			+ "acc.passFilter.scrypt.parallelization";
	int DEFAULT_SCRYPT_PARALLELIZATION = 1;

	String PROPERTY_SCRYPT_KEY_LENGTH = ConfigurationService.IDM_PRIVATE_PROPERTY_PREFIX
			+ "acc.passFilter.scrypt.keyLength";
	int DEFAULT_SCRYPT_KEY_LENGTH = 32;

	String PROPERTY_SCRYPT_SALT_LENGTH = ConfigurationService.IDM_PRIVATE_PROPERTY_PREFIX
			+ "acc.passFilter.scrypt.saltLength";
	int DEFAULT_SCRYPT_SALT_LENGTH = 64;

	@Override
	default String getConfigurableType() {
		// Password filter is there with shortcut because string password made all configuration properties confidential
		return "passFilter";
	}

	@Override
	default boolean isDisableable() {
		return false;
	}

	@Override
	default List<String> getPropertyNames() {
		List<String> properties = new ArrayList<>(10);

		properties.add(PROPERTY_SCRYPT_CPU_COST);
		properties.add(PROPERTY_SCRYPT_MEMORY_COST);
		properties.add(PROPERTY_SCRYPT_PARALLELIZATION);
		properties.add(PROPERTY_SCRYPT_KEY_LENGTH);
		properties.add(PROPERTY_SCRYPT_SALT_LENGTH);
		return properties;
	}

	/**
	 * Get CPU cost for Scrypt. For change the property is required restart whole application.
	 *
	 * @return
	 */
	int getScryptCpuCost();

	/**
	 * Get memory cost for Scrypt. For change the property is required restart whole application.
	 * @return
	 */
	int getScryptMemoryCost();

	/**
	 * Get parallelization for Scrypt. For change the property is required restart whole application.
	 * @return
	 */
	int getScryptParallelization();

	/**
	 * Get key length for Scrypt. For change the property is required restart whole application.
	 * @return
	 */
	int getScryptKeyLength();

	/**
	 * Get salt length for Scrypt. For change the property is required restart whole application.
	 * @return
	 */
	int getScryptSaltLength();
}


text:
generate_glyphdata
##################

.. automodule:: fontbakery.commands.generate_glyphdata
   :members:
   :undoc-members:


text:
package iso20022

// Set of elements used to provide further means of referencing a payment transaction.
type PaymentIdentification2 struct {

	// Unique identification as assigned by an instructing party for an instructed party to unambiguously identify the instruction.
	//
	// Usage: the  instruction identification is a point to point reference that can be used between the instructing party and the instructed party to refer to the individual instruction. It can be included in several messages related to the instruction.
	InstructionIdentification *Max35Text `xml:"InstrId,omitempty"`

	// Unique identification assigned by the initiating party to unambiguously identify the transaction. This identification is passed on, unchanged, throughout the entire end-to-end chain.
	//
	// Usage: The end-to-end identification can be used for reconciliation or to link tasks relating to the transaction. It can be included in several messages related to the transaction.
	//
	// Usage: In case there are technical limitations to carry on multiple references, the end-to-end identification must be carried on throughout the entire end-to-end chain.
	EndToEndIdentification *Max35Text `xml:"EndToEndId"`

	// Unique identification assigned by the first instructing agent to unambiguously identify the transaction and passed on, unchanged, throughout the entire interbank chain.
	//
	// Usage: The transaction identification can be used for reconciliation, tracking or to link tasks relating to the transaction on the interbank level.
	// Usage: The instructing agent has to make sure the transaction identification is unique for a pre-agreed period.
	TransactionIdentification *Max35Text `xml:"TxId"`
}

func (p *PaymentIdentification2) SetInstructionIdentification(value string) {
	p.InstructionIdentification = (*Max35Text)(&value)
}

func (p *PaymentIdentification2) SetEndToEndIdentification(value string) {
	p.EndToEndIdentification = (*Max35Text)(&value)
}

func (p *PaymentIdentification2) SetTransactionIdentification(value string) {
	p.TransactionIdentification = (*Max35Text)(&value)
}


text:
@implementation PCKMethodRedirector

+ (void)redirectSelector:(SEL)originalSelector forClass:(Class)klass to:(SEL)newSelector andRenameItTo:(SEL)renamedSelector {
    if ([klass instancesRespondToSelector:renamedSelector]) {
        return;
    }

    Method originalMethod = class_getInstanceMethod(klass, originalSelector);
    class_addMethod(klass, renamedSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));

    Method newMethod = class_getInstanceMethod(klass, newSelector);
    class_replaceMethod(klass, originalSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
}

@end


text:
package tpe.collections.iterate;

import java.awt.Point;
import java.util.Iterator;
import java.util.Random;

import de.smits_net.games.framework.board.Board;
import de.smits_net.games.framework.image.SimpleImage;
import de.smits_net.games.framework.sprite.Sprite;
import de.smits_net.games.framework.sprite.Sprite.BoundaryPolicy;

/**
 * Klasse, die eine beliebige Menge an Geld generiert..
 */
public class CoinGenerator implements Iterable<Sprite> {

    /** Zufallszahlen-Generator. */
    private static final Random RND = new Random();

    /** Das Spielfeld. */
    private Board board;

    /** Anzahl der Münzen, die erzeugt werden sollen. */
    private int number;

    /**
     * Erzeugt eine neue Instanz, die zu dem übergebenen Spielfeld gehört.
     *
     * @param board Das Spielfeld.
     * @param number Anzahl der Münzen.
     */
    public CoinGenerator(Board board, int number) {
        this.board = board;
        this.number = number;
    }

    /**
     *
     * Erzeugt einen neuen Iterator.
     *
     * @return Der Iterator.
     */
    public Iterator<Sprite> iterator() {
        return new Iterator<Sprite>() {

            int count = 0;

            public boolean hasNext() {
                return count < number;
            }

            public Sprite next() {
                count++;
                return createCoin();
            }
        };
    }

    /**
     * Legt eine zufällige Münze an.
     *
     * @return die Münze als Sprite.
     */
    private Sprite createCoin() {
        String asset;

        switch (RND.nextInt(8)) {
            case 0: asset = "assets/1c.png"; break;
            case 1: asset = "assets/2c.png"; break;
            case 3: asset = "assets/5c.png"; break;
            case 4: asset = "assets/10c.png"; break;
            case 5: asset = "assets/20c.png"; break;
            case 6: asset = "assets/50c.png"; break;
            case 7: asset = "assets/1e.png"; break;
            default: asset = "assets/2e.png"; break;
        }

        int xPos = RND.nextInt(board.getWidth()) - 20;
        int yPos = RND.nextInt(board.getHeight()) - 20;

        return new Sprite(board, new Point(xPos, yPos),
                BoundaryPolicy.NONE,
                new SimpleImage(asset));
    }
}


text:
<!DOCTYPE html>

	<meta charset="utf-8">
	<title>Interesting Survey results...</title>

	<meta name="author" content="raesene">

	<meta name="viewport" content="width=device-width, initial-scale=1.0">

	<link href="https://raesene.github.io/assets/resources/bootstrap/css/bootstrap.min.css" rel="stylesheet">
	<link href="https://raesene.github.io/assets/resources/font-awesome/css/font-awesome.min.css" rel="stylesheet">
	<link href="https://raesene.github.io/assets/resources/syntax/syntax.css" rel="stylesheet">
	<link href="https://raesene.github.io/assets/css/style.css" rel="stylesheet">

	<link rel="alternate" type="application/rss+xml" title="" href="https://raesene.github.io/feed.xml">

			<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
				Toggle navigation

			</button>

				Raesene's Ramblings

Home

Categories

Tags

		<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">

		</button>

Home

Categories

Tags

Top of Page

        Raesene's Ramblings

	Security Geek, Kubernetes, Docker, Ruby, Hillwalking

Interesting Survey results...

	   October
	   30th,

	   2005

Web Threats Keep Users Away

this story covers the results of a survey from webwatch, which indictates that people are cutting back on spending online as a result of fears about identity theft and phishing.

I think this kind of swing is one that a lot of companies (like banks) who save a lot of money as more of their customers transact over the net, will be very worried about.  If their customers start returning to using other channels like phone banking or branch banking, then that's likely to have a real impact on the bottom line for the companies involved...

One outcome I think it likely is a return of the AOL style "walled garden" Internet where only specific, vetted, sites are available to the user, with the service provider providing some assurance over the content...

Ordinary users are not and don't want to be savvy enough in the ways that the Internet works to be able to reliably detect phishing scams and to keep spyware off their PC's.  As a result, if the environment on the 'net keeps getting more hostile, it will be better for the ordinary users to take the hit and lose access to a lot of sites, in exchange for a greater level of safety...

					Misc. (51)

Share Post

          Twitter

          Facebook

          Google+

raesene

Security Geek, Kubernetes, Docker, Ruby, Hillwalking

← Previous

Next →

				© 2022 raesene with Jekyll. Theme: dbyll by dbtek.


text:
package org.apache.spark.shuffle.unsafe;

import org.apache.spark.util.collection.SortDataFormat;

final class UnsafeShuffleSortDataFormat extends SortDataFormat<PackedRecordPointer, long[]> {

  public static final UnsafeShuffleSortDataFormat INSTANCE = new UnsafeShuffleSortDataFormat();

  private UnsafeShuffleSortDataFormat() { }

  @Override
  public PackedRecordPointer getKey(long[] data, int pos) {
    // Since we re-use keys, this method shouldn't be called.
    throw new UnsupportedOperationException();
  }

  @Override
  public PackedRecordPointer newKey() {
    return new PackedRecordPointer();
  }

  @Override
  public PackedRecordPointer getKey(long[] data, int pos, PackedRecordPointer reuse) {
    reuse.set(data[pos]);
    return reuse;
  }

  @Override
  public void swap(long[] data, int pos0, int pos1) {
    final long temp = data[pos0];
    data[pos0] = data[pos1];
    data[pos1] = temp;
  }

  @Override
  public void copyElement(long[] src, int srcPos, long[] dst, int dstPos) {
    dst[dstPos] = src[srcPos];
  }

  @Override
  public void copyRange(long[] src, int srcPos, long[] dst, int dstPos, int length) {
    System.arraycopy(src, srcPos, dst, dstPos, length);
  }

  @Override
  public long[] allocate(int length) {
    return new long[length];
  }

}


text:
<?php
namespace tests\codeception\_pages\user;
use tests\codeception\_pages\BasePage;
/**
 * Represents login form page
 * @property \AcceptanceTester|\FunctionalTester $actor
 */
class LoginPage extends BasePage
{
    public $route = 'user/login';
    public static $url = '/user/login';

    public static $textContent = 'Otherwise, please fill out the following fields to login:';

    public static $formSelector = '#login-form';
    public static $errorSelector = '.help-block-error';

    /**
     * Login with the given details.
     * @param \AcceptanceTester|\FunctionalTester $I
     * @param string                              $email
     * @param string                              $password
     */
    public static function login($I, $email, $password)
    {
        $I->submitForm('#login-form', [
            'LoginForm[email]' => $email,
            'LoginForm[password]' => $password,
        ]);
    }

    /**
     * Login as a standard user.
     * @param \AcceptanceTester|\FunctionalTester $I
     */
    public static function loginAsUser($I)
    {
        self::login($I, 'user@placeholder.net', 'password');
    }

    /**
     * Login as a coordinator.
     * @param \AcceptanceTester|\FunctionalTester $I
     */
    public static function loginAsCoordinator($I)
    {
        self::login($I, 'coordinator@placeholder.net', 'password');
    }

    /**
     * Login as a manager.
     * @param \AcceptanceTester|\FunctionalTester $I
     */
    public static function loginAsManager($I)
    {
        self::login($I, 'manager@placeholder.net', 'password');
    }

}


text:
package com.mendeley.sdk.request.endpoint;

import com.mendeley.sdk.request.SignedInTest;

import java.util.Map;
import java.util.Set;

public class DocumentTypesRequestTest extends SignedInTest {

    public void test_getDocumentTypes_receivesCorrectDocumentTypes() throws Exception {
        final Map<String, String> types = getRequestFactory().newGetDocumentTypesRequest().run().resource;

        Set<String> keys = types.keySet();
        assertTrue("document types must contain journal", keys.contains("journal"));
        assertTrue("document types must contain book", keys.contains("book"));
        //...
    }

}


text:
#ifndef __LINUX_NETFILTER_H
#define __LINUX_NETFILTER_H

/* Responses from hook functions. */
#define NF_DROP 0
#define NF_ACCEPT 1
#define NF_STOLEN 2
#define NF_QUEUE 3
#define NF_REPEAT 4
#define NF_STOP 5
#define NF_MAX_VERDICT NF_STOP

/* we overload the higher bits for encoding auxiliary data such as the queue
 * number. Not nice, but better than additional function arguments. */
#define NF_VERDICT_MASK 0x0000ffff
#define NF_VERDICT_BITS 16

#define NF_VERDICT_QMASK 0xffff0000
#define NF_VERDICT_QBITS 16

#define NF_QUEUE_NR(x) (((x << NF_VERDICT_QBITS) & NF_VERDICT_QMASK) | NF_QUEUE)

/* only for userspace compatibility */
/* Generic cache responses from hook functions.
   <= 0x2000 is used for protocol-flags. */
#define NFC_UNKNOWN 0x4000
#define NFC_ALTERED 0x8000

enum nf_inet_hooks {
	NF_INET_PRE_ROUTING,
	NF_INET_LOCAL_IN,
	NF_INET_FORWARD,
	NF_INET_LOCAL_OUT,
	NF_INET_POST_ROUTING,
	NF_INET_NUMHOOKS,
};

#endif /*__LINUX_NETFILTER_H*/


text:
using System.Linq;
using OpenQA.Selenium;

namespace SQUnit
{
	public class QUnitTestSuite : TestSuiteBase
	{
		IWebElement _qunitTestsElement;

		public static readonly TestSuiteFactoryDelegate Factory = (driver, path) => new QUnitTestSuite(driver, path);

		public QUnitTestSuite(IWebDriver driver, string testFilePath)
			: base(driver, testFilePath)
		{
		}

		public override void Update()
		{
			var elements = Driver.FindElements(By.Id("qunit-tests")).ToArray();
			if (elements.Length == 0)
			{
				SaveScreenShot();
				var msg = string.Format(
					"The test file is missing the list of qunit tests - element '#qunit-tests' was not found. See [{0}] for details.",
					ScreenshotPath);
				throw new InvalidTestFileException(msg);
			}
			_qunitTestsElement = elements[0];
		}

		public override bool IsRunning()
		{
			return _qunitTestsElement.FindElements(By.CssSelector("li.running")).Any();
		}

		public override TestResult[] GetTestResults()
		{
			return _qunitTestsElement
				.FindElements(By.CssSelector("li[id^='test-output']"))
				.Select(ParseTestResult)
				.ToArray();
		}

		TestResult ParseTestResult(IWebElement testOutput)
		{
			var testName = testOutput.FindElement(By.ClassName("test-name")).Text;
			var resultClass = testOutput.GetAttribute("class");

			if (resultClass == "pass")
				return CreateTestResult(testName, true, string.Empty);

			if (resultClass == "fail")
				return CreateTestResult(testName, false, testOutput.FindElement(By.ClassName("fail")).Text);

			if (resultClass == "running")
				return CreateTestResult(testName, false, "The test did not finish within time limit.");

			return CreateTestResult(testName, false, "Unknown test class: '" + resultClass + "'");
		}
	}
}


text:
import glob
import os

from distutils.core import setup
from distutils.core import Extension

HAS_CORTO_INSTALLATION = False
CORTO_HOME = '/usr/local' if HAS_CORTO_INSTALLATION else os.path.join(os.getenv('HOME'), '.corto')

def get_corto_include_dirs():
    include_dirs = glob.glob(os.path.join(CORTO_HOME, 'include/corto/0.2/'))
    include_dirs.append(os.path.join(CORTO_HOME, "python-binding/src/"))
    for include_dir in include_dirs:
        assert os.path.exists(include_dir), '"{}" doesn\'t exist'.format(include_dir)
    return include_dirs

def get_corto_libraries():
    libraries = [
        'corto',
    ]
    for library in libraries:
        search_locations = [
            os.path.join(d, "lib" + library + ".so")
            for d in get_corto_library_dirs()
        ]
        assert any(
            map(os.path.exists, search_locations)
        ), "did not find {}, tried {}".format(library, search_locations)
    return libraries

def get_corto_library_dirs():
    library_dirs = glob.glob(os.path.join(CORTO_HOME, "lib/corto/0.2/*"))
    for library_dir in library_dirs:
        assert os.path.exists(library_dir), '"{}" doesn\'t exist'.format(library_dir)
    return library_dirs

def get_sources():
    return glob.glob("src/*.c")

def get_runtime_library_dirs():
    return get_corto_library_dirs()

corto_module = Extension(
    'cortopy',
    include_dirs=get_corto_include_dirs(),
    libraries=get_corto_libraries(),
    library_dirs=get_corto_library_dirs(),
    sources=get_sources(),
    runtime_library_dirs=get_runtime_library_dirs(),
)

setup(
    name='cortopy',
    version="0.1.0",
    ext_modules=[corto_module],
    author='Johnny Lee',
    author_email='jleeothon@gmail.com',
    license='MIT',
    description='Python binding for Corto',
    url='https://github.com/cortoproject/python-binding',
    classifiers=[
        'Environment :: Console',
        'Environment :: Web Environment',
        'Intended Audience :: Developers',
        'Intended Audience :: Information Technology',
        'License :: OSI Approved :: MIT License',
        'Operating System :: MacOS :: MacOS X',
        'Operating System :: POSIX :: Linux',
        'Programming Language :: C',
        'Programming Language :: Python',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Topic :: Internet',
    ],
)


text:
This guide will walk you through a deployment of a single-master/multi-worker Kubernetes cluster on CoreOS. We're going to:

- configure an etcd cluster for Kubernetes to use
- generate the required certificates for communication between Kubernetes components
- deploy a master node
- deploy worker nodes
- configure `kubectl` to work with our cluster
- deploy the DNS add-on
- deploy the network policy add-on

Working through this guide may take you a few hours, but it will give you good understanding of the moving pieces of your cluster and set you up for success in the long run. Let's get started.

## Deployment Options

The following variables will be used throughout this guide. Most of the provided defaults can safely be used, however some values such as `ETCD_ENDPOINTS` and `MASTER_HOST` will need to be customized to your infrastructure.

**MASTER_HOST**=_no default_

The address of the master node. In most cases this will be the publicly routable IP of the node. Worker nodes must be able to reach the master node(s) via this address on port 443. Additionally, external clients (such as an administrator using `kubectl`) will also need access, since this will run the Kubernetes API endpoint.

If you will be running a high-availability control-plane consisting of multiple master nodes, then `MASTER_HOST` will ideally be a network load balancer that sits in front of them. Alternatively, a DNS name can be configured which will resolve to the master IPs. How requests are routed to the master nodes will be an important consideration when creating the TLS certificates.

**ETCD_ENDPOINTS**=_no default_

List of etcd machines (`http://ip:port`), comma separated. If you're running a cluster of 5 machines, list them all here.

**POD_NETWORK**=10.2.0.0/16

The CIDR network to use for pod IPs.
Each pod launched in the cluster will be assigned an IP out of this range.
This network must be routable between all hosts in the cluster. In a default installation, the flannel overlay network will provide routing to this network.

**SERVICE_IP_RANGE**=10.3.0.0/24

The CIDR network to use for service cluster VIPs (Virtual IPs). Each service will be assigned a cluster IP out of this range. This must not overlap with any IP ranges assigned to the `POD_NETWORK`, or other existing network infrastructure. Routing to these VIPs is handled by a local kube-proxy service to each host, and are not required to be routable between hosts.

**K8S_SERVICE_IP**=10.3.0.1

The VIP (Virtual IP) address of the Kubernetes API Service. If the `SERVICE_IP_RANGE` is changed above, this must be set to the first IP in that range.

**DNS_SERVICE_IP**=10.3.0.10

The VIP (Virtual IP) address of the cluster DNS service. This IP must be in the range of the `SERVICE_IP_RANGE` and cannot be the first IP in the range. This same IP must be configured on all worker nodes to enable DNS service discovery.

## Deploy etcd Cluster

Kubernetes uses etcd for data storage and for cluster consensus between different software components. Your etcd cluster will be heavily utilized since all objects storing within and every scheduling decision is recorded. It's recommended that you run a multi-machine cluster on dedicated hardware (with fast disks) to gain maximum performance and reliability of this important part of your cluster. For development environments, a single etcd is ok.

### Single-Node/Development

You can simply start etcd via [cloud-config][cloud-config-etcd] when you create your CoreOS machine or start it manually.

If you are starting etcd manually, we need to first configure it to listen on all interfaces:

* Replace `${PUBLIC_IP}` with the etcd machines publicly routable IP address.

**/etc/systemd/system/etcd2.service.d/40-listen-address.conf**

```
[Service]
Environment=ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
Environment=ETCD_ADVERTISE_CLIENT_URLS=http://${PUBLIC_IP}:2379
```

Use the value of `ETCD_ADVERTISE_CLIENT_URLS` as the value of `ETCD_ENDPOINTS` in the rest of this guide.

Next, start etcd

```
$ sudo systemctl start etcd2
```

To ensure etcd starts after a reboot, enable it too:

```sh
$ sudo systemctl enable etcd2
Created symlink from /etc/systemd/system/multi-user.target.wants/etcd2.service to /usr/lib64/systemd/system/etcd2.service.
```

[cloud-config-etcd]: https://coreos.com/os/docs/latest/cloud-config.html#etcd2

### Multi-Node/Production

It is highly recommended that etcd is run as a dedicated cluster separately from Kubernetes components.

Use the [official etcd clustering guide](https://coreos.com/etcd/docs/latest/clustering.html) to decide how best to deploy etcd into your environment.

## Generate Kubernetes TLS Assets

The Kubernetes API has various methods for validating clients — this guide will configure the API server to use client certificate authentication.

This means it is necessary to have a Certificate Authority and generate the proper credentials. Generate the necessary assets from existing PKI infrastructure, or by following [these OpenSSL-based instructions](openssl.md) to create the needed certificates and keys.

In the following steps, it is assumed that you will have generated the following TLS assets:

**Root CA Public Key**

ca.pem

**API Server Public & Private Keys**

apiserver.pem

apiserver-key.pem

**Worker Node Public & Private Keys**

_You should have one certificate/key set for every worker node in the planned cluster._

${WORKER_FQDN}-worker.pem

${WORKER_FQDN}-worker-key.pem

**Cluster Admin Public & Private Keys**

admin.pem

admin-key.pem

Is your etcd cluster up and running? You need the IPs for the next step.

Did you generate all of the certificates? You will place these on disk next.

  Yes, ready to deploy the master node


text:
{-| Module      :  Database.HSQL.MySQL
    Copyright   :  (c) Krasimir Angelov 2003
    License     :  BSD-style

    Maintainer  :  ka2_mail@yahoo.com
    Stability   :  provisional
    Portability :  portable

    The module provides interface to MySQL database
-}
module Database.HSQL.MySQL(connect,module Database.HSQL) where

import Database.HSQL
import Database.HSQL.Types(Connection(..),Statement(stmtGetCol),ColDef
                          ,SqlType(SqlVarChar),fromSqlCStringLen)
import Foreign(nullPtr,free)
import Foreign.C(newCString,withCString)
import Control.Monad(when)
import Control.Concurrent.MVar(newMVar)

import DB.HSQL.MySQL.Type(MYSQL,MYSQL_RES)
import DB.HSQL.MySQL.Functions(handleSqlError,withStatement,mysql_query
                              ,mysql_close,mysql_use_result,mysql_next_result
                              ,mysql_list_fields,mysql_list_tables
                              ,mysql_init,mysql_real_connect
                              ,mysqlDefaultConnectFlags)

------------------------------------------------------------------------------
-- Connect/Disconnect
------------------------------------------------------------------------------
-- | Makes a new connection to the database server.
connect :: String   -- ^ Server name
        -> String   -- ^ Database name
        -> String   -- ^ User identifier
        -> String   -- ^ Authentication string (password)
        -> IO Connection
connect server database user authentication = do
	pMYSQL <- mysql_init nullPtr
	pServer <- newCString server
	pDatabase <- newCString database
	pUser <- newCString user
	pAuthentication <- newCString authentication
	res <- mysql_real_connect pMYSQL pServer pUser pAuthentication pDatabase 0 nullPtr mysqlDefaultConnectFlags
	free pServer
	free pDatabase
	free pUser
	free pAuthentication
	when (res == nullPtr) (handleSqlError pMYSQL)
	refFalse <- newMVar False
	let connection = Connection
		{ connDisconnect = mysql_close pMYSQL
		, connExecute = mysqlExecute pMYSQL
		, connQuery = mysqlQuery connection pMYSQL
		, connTables = mysqlTables connection pMYSQL
		, connDescribe = mysqlDescribe connection pMYSQL
		, connBeginTransaction = mysqlExecute pMYSQL "begin"
		, connCommitTransaction = mysqlExecute pMYSQL "commit"
		, connRollbackTransaction = mysqlExecute pMYSQL "rollback"
		, connClosed = refFalse	}
	return connection

-- |
mysqlQuery :: Connection -> MYSQL -> String -> IO Statement
mysqlQuery conn pMYSQL query = do
			res <- withCString query (mysql_query pMYSQL)
			when (res /= 0) (handleSqlError pMYSQL)
			pRes <- getFirstResult pMYSQL
			withStatement conn pMYSQL pRes
			where
			  getFirstResult :: MYSQL -> IO MYSQL_RES
			  getFirstResult pMYSQL = do
			    pRes <- mysql_use_result pMYSQL
			    if pRes == nullPtr
			      then do
			        res <- mysql_next_result pMYSQL
			        if res == 0
			          then getFirstResult pMYSQL
			          else return nullPtr
			      else return pRes

-- |
mysqlDescribe :: Connection -> MYSQL -> String -> IO [ColDef]
mysqlDescribe conn pMYSQL table = do
  pRes <- withCString table (\table -> mysql_list_fields pMYSQL table nullPtr)
  stmt <- withStatement conn pMYSQL pRes
  return (getFieldsTypes stmt)

-- |
mysqlTables :: Connection -> MYSQL -> IO [String]
mysqlTables conn pMYSQL = do
  pRes <- mysql_list_tables pMYSQL nullPtr
  stmt <- withStatement conn pMYSQL pRes
  -- SQLTables returns:
  -- Column name     #   Type
  -- Tables_in_xx      0   VARCHAR
  collectRows (\stmt -> do
    mb_v <- stmtGetCol stmt 0 ("Tables", SqlVarChar 0, False) fromSqlCStringLen
    return (case mb_v of { Nothing -> ""; Just a -> a })) stmt

-- |
mysqlExecute :: MYSQL -> String -> IO ()
mysqlExecute pMYSQL query = do
  res <- withCString query (mysql_query pMYSQL)
  when (res /= 0) (handleSqlError pMYSQL)


text:
require 'spec_helper'

describe Pair do

  before :each do
    @user1 = User.create(id: 2,
      first_name: 'User1',
      email: 'user1@berkeley.edu',
      admin: false,
      last_name: 'user_lastname',
      sid: '11111111',
      academic_title: 'Undergraduate',
      major: 'x',
      residency: 'x',
      gender: 'x',
      gender_preference: 'x',
      fluent_languages: ['x'],
      lang_additional_info: 'x',
      first_lang_preference: 'x',
      first_lang_proficiency: 'x',
      second_lang_preference: 'x',
      second_lang_proficiency: 'x',
      time_preference: ['x'],
      hours_per_week: '0',
      user_motivation: 'x',
      user_plan: 'x',
      group_leader: 'x',
      active: true
    )
    @user2 = User.create(id: 3,
      first_name: 'User2',
      email: 'user2@berkeley.edu',
      admin: false,
      last_name: 'user_lastname',
      sid: '22222222',
      academic_title: 'Undergraduate',
      major: 'x',
      residency: 'x',
      gender: 'x',
      gender_preference: 'x',
      fluent_languages: ['x'],
      lang_additional_info: 'x',
      first_lang_preference: 'x',
      first_lang_proficiency: 'x',
      second_lang_preference: 'x',
      second_lang_proficiency: 'x',
      time_preference: ['x'],
      hours_per_week: '0',
      user_motivation: 'x',
      user_plan: 'x',
      group_leader: 'x',
      active: true
    )
    @user3 = User.create(id: 4,
      first_name: 'User3',
      email: 'user3@berkeley.edu',
      admin: false,
      last_name: 'user_lastname',
      sid: '33333333',
      academic_title: 'Undergraduate',
      major: 'x',
      residency: 'x',
      gender: 'x',
      gender_preference: 'x',
      fluent_languages: ['x'],
      lang_additional_info: 'x',
      first_lang_preference: 'x',
      first_lang_proficiency: 'x',
      second_lang_preference: 'x',
      second_lang_proficiency: 'x',
      time_preference: ['x'],
      hours_per_week: '0',
      user_motivation: 'x',
      user_plan: 'x',
      group_leader: 'x',
      active: true
    )
    @user4 = User.create(id: 5,
      first_name: 'User3',
      email: 'user3@berkeley.edu',
      admin: false,
      last_name: 'user_lastname',
      sid: '33333333',
      academic_title: 'Undergraduate',
      major: 'x',
      residency: 'x',
      gender: 'x',
      gender_preference: 'x',
      fluent_languages: ['x'],
      lang_additional_info: 'x',
      first_lang_preference: 'x',
      first_lang_proficiency: 'x',
      second_lang_preference: 'x',
      second_lang_proficiency: 'x',
      time_preference: ['x'],
      hours_per_week: '0',
      user_motivation: 'x',
      user_plan: 'x',
      group_leader: 'x',
      active: true
    )

    @pair1 = Pair.create(id: 1, member1: @user1.id.to_s, member2: @user2.id.to_s, member3: '', languages: ["Chinese", "English"])
  end

  describe 'check if a user is in a pair' do
    it 'should correctly return that a user is in a pair' do
      expect(@pair1.has_member?(@user2.id.to_s)).to be_true
      expect(@pair1.has_member?(@user3.id.to_s)).to be_false
    end
  end

  describe 'get_languages' do
    it 'should return languages in a string?' do
#expect(@pair1.get_languages).to include("Chinese, English")
    end
  end

  describe 'check how many pairs exist' do
    it 'should correctly say how many pairs there are' do
      expect(Pair.total_pairs).eql?(1)
    end
  end

  describe 'check nil or empty method' do
    it 'should correctly determine whether a string is empty or not' do
      expect(Pair.check_nil_or_empty('yolo')).to eq(false)
      expect(Pair.check_nil_or_empty('')).to eq(true)
    end
  end

  describe 'remove_user_from_pair' do
    it 'should remove a specified user from a pair' do
      Pair.remove_user_from_pair(@pair1.id, @user1.id)
      expect(Pair.find(@pair1.id).member1).eql?('')
      Pair.add_user_to_pair(@pair1.id, @user1.id)
      expect(Pair.find(@pair1.id).member1).eql?(@user1.id)
      Pair.remove_user_from_pair(@pair1.id, @user2.id)
      expect(Pair.find(@pair1.id).member2).eql?('')
      Pair.add_user_to_pair(@pair1.id, @user2.id)
      Pair.add_user_to_pair(@pair1.id, @user3.id)
      Pair.remove_user_from_pair(@pair1.id, @user3.id)
      expect(Pair.find(@pair1.id).member3).eql?('')
    end
  end

  describe 'add_user_to_pair' do
    before :each do
      @pair2 = Pair.create(:member2 => @user2.id.to_s)
      @pair3 = Pair.create(:member1 => @user1.id.to_s)
    end
    it 'should add a specified user to a pair' do
      Pair.add_user_to_pair(@pair1.id, @user3.id)
      expect(Pair.find(@pair1.id).member3).to eq(@user3.id.to_s)
      Pair.should_receive(:remove_user_from_pair)
      Pair.remove_user_from_pair(@pair1.id, @user1.id)
      Pair.should_receive(:add_user_to_pair)
      Pair.add_user_to_pair(@pair1.id, @user1.id)
      expect(Pair.find(@pair1.id).member1).to eq(@user1.id.to_s)
      Pair.should_receive(:remove_user_from_pair)
      Pair.remove_user_from_pair(@pair1.id, @user2.id)
      Pair.should_receive(:add_user_to_pair)
      Pair.add_user_to_pair(@pair1.id, @user2.id)
      expect(Pair.find(@pair1.id).member2).to eq(@user2.id.to_s)
    end
    it 'should add a user at member1' do
      Pair.add_user_to_pair(@pair2.id, @user1.id)
      expect(Pair.find(@pair2.id).member1).to eq(@user1.id.to_s)
    end
    it 'should add a user at member2' do
      Pair.add_user_to_pair(@pair3.id, @user2.id)
      expect(Pair.find(@pair3.id).member2).to eq(@user2.id.to_s)
    end

  end

  describe 'verify_pair' do
    it 'should check for valid pairs' do
      Pair.verify_pair(@user3.id, @user4.id).should == true
    end
    it 'should return false if users in pairs' do
      Pair.verify_pair(@user2.id, @user4.id).should == true
    end
  end

  describe 'to_csv' do
    it 'should generate a csv file' do
    end
  end

=begin
  describe 'generate_pairs' do
    it 'should generate a csv file of pairs' do
      Pair.should_receive(:generate_pairs)
      Pair.generate_pairs
      expected_csv = File.read('script/test_pairs.csv')
      result_csv = File.read('script/final_pairs.csv')
      result_csv.should == expected_csv
    end
  end
=end

end


text:
webpackJsonp([2],[
/* 0 */
/***/ function(module, exports, __webpack_require__) {

	var common = __webpack_require__(1);
	__webpack_require__.e/* nsure */(1/* duplicate */, function(require) {
	    var shared = __webpack_require__(2);//载入两次?
	    shared("This is page B");
	});

/***/ }
]);


text:
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.

using System;
using System.Globalization;
using OpenLiveWriter.ApplicationFramework;
using OpenLiveWriter.CoreServices;
using OpenLiveWriter.Localization;

namespace OpenLiveWriter.PostEditor.Commands
{
    class AboutCommand : Command
    {
        public AboutCommand() : base(CommandId.About)
        {
        }

        public override string LabelTitle
        {
            get
            {
                return String.Format(CultureInfo.InvariantCulture, Res.Get(StringId.AboutAbout), ApplicationEnvironment.ProductNameQualified);
            }
        }

        public override string TooltipDescription
        {
            get
            {
                return TextHelper.StripHotkey(LabelTitle);
            }
        }
    }
}


text:
/**
 * Demonstates uses of Strings.
 */
public class StringsThings {

    public static void main(String[] args) {

        // Characters

        String fruit = "banana";
        char letter0 = fruit.charAt(0);

        if (letter0 == 'a') {
            System.out.println('?');
        }

        System.out.print("Roman alphabet: ");
        for (char c = 'A'; c <= 'Z'; c++) {
            System.out.print(c);
        }
        System.out.println();

        System.out.print("Greek alphabet: ");
        for (int i = 913; i <= 937; i++) {
            System.out.print((char) i);
        }
        System.out.println();

        // Strings are immutable

        String name = "Alan Turing";
        String upperName = name.toUpperCase();

        String text = "Computer Science is fun!";
        text = text.replace("Computer Science", "CS");

        // String traversal

        for (int i = 0; i < fruit.length(); i++) {
            char letter = fruit.charAt(i);
            System.out.println(letter);
        }

        for (char letter : fruit.toCharArray()) {
            System.out.println(letter);
        }

        int length = fruit.length();
        char last = fruit.charAt(length - 1);  // correct

        System.out.println(reverse(fruit));

        // Substrings

        System.out.println(fruit.substring(0));
        System.out.println(fruit.substring(2));
        System.out.println(fruit.substring(6));

        System.out.println(fruit.substring(0, 3));
        System.out.println(fruit.substring(2, 5));
        System.out.println(fruit.substring(6, 6));

        // The indexOf method

        int index = fruit.indexOf('a');
        int index2 = fruit.indexOf('a', 2);

        // String comparison

        String name1 = "Alan Turing";
        String name2 = "Ada Lovelace";
        if (name1.equals(name2)) {
            System.out.println("The names are the same.");
        }

        int diff = name1.compareTo(name2);
        if (diff == 0) {
            System.out.println("The names are the same.");
        } else if (diff < 0) {
            System.out.println("name1 comes before name2.");
        } else if (diff > 0) {
            System.out.println("name2 comes before name1.");
        }

        // Wrapper classes

        String str = "12345";
        int num = Integer.parseInt(str);

        num = 12345;
        str = Integer.toString(num);
    }

    /**
     * Reverses a string, returns a new String.
     */
    public static String reverse(String s) {
        String r = "";
        for (int i = s.length() - 1; i >= 0; i--) {
            r = r + s.charAt(i);
        }
        return r;
    }

}


text:
void OpticalMarkerSegmentCutOnCurrentTime();
void OpticalMarkerSegmentRemoveOnCurrentTime();

void OpticalMarkerGapChangeInterpolation(int newMode);

void OpticalDone();


text:
# Distributed Route Planning Project
### Readme for Spark + Kafka Streaming

 This directory provides a tar of the kafka version we're using, and the python code for the direct streaming spark application we're developing. A built Spark installation is assumed; we are using version 1.6.0. To work with the python code, copy it into the **examples/src/main/python/streaming/** directory of your spark installation and execute the following from the Spark directory to run:
```
 bin/spark-submit --jars \
      external/kafka-assembly/target/scala-*/spark-streaming-kafka-assembly-*.jar \
      examples/src/main/python/streaming/distributed_route_streaming.py localhost:9092 test
```
> Note that to run this on your local machine, you need to setup Kafka and create a producer first, see: http://kafka.apache.org/documentation.html#quickstart

### Troubleshooting

An issue that may arise when attempting to run the python code where the program cannot find the .jar files, if this happens try removing **scala-*** from the **external/kafka-assembly/target/scala-*/spark-streaming-kafka-assembly-*.jar** filepath.


text:
/* eslint-disable import/prefer-default-export */
/* eslint-disable import/no-extraneous-dependencies */

import '@kitware/vtk.js/favicon';

// Load the rendering pieces we want to use (for both WebGL and WebGPU)
import '@kitware/vtk.js/Rendering/Profiles/Geometry';
import '@kitware/vtk.js/Rendering/OpenGL/Glyph3DMapper';

import { throttle } from '@kitware/vtk.js/macros';
import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor';
import vtkConeSource from '@kitware/vtk.js/Filters/Sources/ConeSource';
import vtkCylinderSource from '@kitware/vtk.js/Filters/Sources/CylinderSource';
import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray';
import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow';
import vtkGlyph3DMapper from '@kitware/vtk.js/Rendering/Core/Glyph3DMapper';
import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper';
import vtkSphereSource from '@kitware/vtk.js/Filters/Sources/SphereSource';
import vtkCubeSource from '@kitware/vtk.js/Filters/Sources/CubeSource';
import vtkPolydata from '@kitware/vtk.js/Common/DataModel/PolyData';
import vtkMatrixBuilder from '@kitware/vtk.js/Common/Core/MatrixBuilder';
import { mat4 } from 'gl-matrix';
import vtkMath from '@kitware/vtk.js/Common/Core/Math';
import { FieldAssociations } from '@kitware/vtk.js/Common/DataModel/DataSet/Constants';
import { Representation } from '@kitware/vtk.js/Rendering/Core/Property/Constants';

// ----------------------------------------------------------------------------
// Constants
// ----------------------------------------------------------------------------

const WHITE = [1, 1, 1];
const GREEN = [0.1, 0.8, 0.1];

// ----------------------------------------------------------------------------
// Create DOM tooltip
// ----------------------------------------------------------------------------

const tooltipsElem = document.createElement('div');
tooltipsElem.style.position = 'absolute';
tooltipsElem.style.top = 0;
tooltipsElem.style.left = 0;
tooltipsElem.style.padding = '10px';
tooltipsElem.style.zIndex = 1;
tooltipsElem.style.background = 'white';
tooltipsElem.style.textAlign = 'center';

const positionTooltipElem = document.createElement('div');
const fieldIdTooltipElem = document.createElement('div');
const compositeIdTooltipElem = document.createElement('div');
const propIdTooltipElem = document.createElement('div');
tooltipsElem.appendChild(positionTooltipElem);
tooltipsElem.appendChild(propIdTooltipElem);
tooltipsElem.appendChild(fieldIdTooltipElem);
tooltipsElem.appendChild(compositeIdTooltipElem);

document.querySelector('body').appendChild(tooltipsElem);

// ----------------------------------------------------------------------------
// Create 4 objects
// - sphere
// - sphere rendered as big points (square)
// - cone
// - cylinder with sphere as point (glyph mapper: source=cylinder, glyph=sphere)
// ----------------------------------------------------------------------------

// Sphere -------------------------------------------------

const sphereSource = vtkSphereSource.newInstance({
  phiResolution: 30,
  thetaResolution: 30,
});

const sphereMapper = vtkMapper.newInstance();
const sphereActor = vtkActor.newInstance();
sphereActor.setMapper(sphereMapper);
sphereActor.getProperty().setEdgeVisibility(true);
sphereMapper.setInputConnection(sphereSource.getOutputPort());

// Cube -------------------------------------------------

const cubeSource = vtkCubeSource.newInstance({
  xLength: 1,
  yLength: 1,
  zLength: 1,
});

const cubeMapper = vtkMapper.newInstance();
const cubeActor = vtkActor.newInstance({ position: [-1, 0, 0] });
cubeActor.setMapper(cubeMapper);
cubeActor.getProperty().setEdgeVisibility(true);
cubeMapper.setInputConnection(cubeSource.getOutputPort());

// Sphere with point representation -----------------------

const spherePointsSource = vtkSphereSource.newInstance({
  phiResolution: 15,
  thetaResolution: 15,
  radius: 0.6,
});
const spherePointsMapper = vtkMapper.newInstance();
const spherePointsActor = vtkActor.newInstance({ position: [0, -1, 0] });
spherePointsActor.setMapper(spherePointsMapper);
spherePointsMapper.setInputConnection(spherePointsSource.getOutputPort());

// Use point representation
spherePointsActor.getProperty().setRepresentation(Representation.POINTS);
spherePointsActor.getProperty().setPointSize(20);

// Cone ---------------------------------------------------

const coneSource = vtkConeSource.newInstance({ resolution: 20 });
const coneMapper = vtkMapper.newInstance();
const coneActor = vtkActor.newInstance({ position: [1, 0, 0] });
coneActor.setMapper(coneMapper);
coneMapper.setInputConnection(coneSource.getOutputPort());

// Cylinder -----------------------------------------------

const cylinderSource = vtkCylinderSource.newInstance({
  resolution: 10,
  radius: 0.4,
  height: 0.6,
  direction: [1.0, 0.0, 0.0],
});
const cylinderMapper = vtkGlyph3DMapper.newInstance({
  scaling: true,
  scaleFactor: 0.25,
  scaleMode: vtkGlyph3DMapper.ScaleModes.SCALE_BY_MAGNITUDE,
  scaleArray: 'scale',
});
const cylinderActor = vtkActor.newInstance({ position: [0, 1, 0] });
const cylinderGlyph = sphereSource.getOutputData();
const cylinderPointSet = cylinderSource.getOutputData();
cylinderActor.setMapper(cylinderMapper);
cylinderMapper.setInputData(cylinderPointSet, 0);
cylinderMapper.setInputData(cylinderGlyph, 1);

// Add fields to cylinderPointSet
const scaleArray = new Float32Array(cylinderPointSet.getNumberOfPoints());
scaleArray.fill(0.5);
cylinderPointSet.getPointData().addArray(
  vtkDataArray.newInstance({
    name: 'scale',
    values: scaleArray,
  })
);

// PolyLines -------------------------------------------------

const polyLinesMapper = vtkMapper.newInstance();
const polyLinesData = vtkPolydata.newInstance();
const squarePoints = [-1, 2, 0, 0, 2, 0, 0, 1, 0, -1, 1, 0];
const trianglePoints = [1, 2, 0, 1, 1, 0, 2, 1.5, 0];
polyLinesData
  .getPoints()
  .setData(Float32Array.from([...squarePoints, ...trianglePoints]), 3);
polyLinesData
  .getLines()
  .setData(Uint16Array.from([5, 0, 1, 2, 3, 0, 4, 4, 5, 6, 4]));
polyLinesMapper.setInputData(polyLinesData);

const polyLines = vtkActor.newInstance();
polyLines.setMapper(polyLinesMapper);

// ----------------------------------------------------------------------------
// Create Picking pointer
// ----------------------------------------------------------------------------

const pointerSource = vtkSphereSource.newInstance({
  phiResolution: 15,
  thetaResolution: 15,
  radius: 0.01,
});
const pointerMapper = vtkMapper.newInstance();
const pointerActor = vtkActor.newInstance();
pointerActor.setMapper(pointerMapper);
pointerMapper.setInputConnection(pointerSource.getOutputPort());

// ----------------------------------------------------------------------------
// Create rendering infrastructure
// ----------------------------------------------------------------------------

const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance();
const renderer = fullScreenRenderer.getRenderer();
const renderWindow = renderer.getRenderWindow();
const interactor = renderWindow.getInteractor();
const apiSpecificRenderWindow = interactor.getView();

renderer.addActor(sphereActor);
renderer.addActor(cubeActor);
renderer.addActor(spherePointsActor);
renderer.addActor(coneActor);
renderer.addActor(cylinderActor);
renderer.addActor(pointerActor);
renderer.addActor(polyLines);

renderer.resetCamera();
renderWindow.render();

// ----------------------------------------------------------------------------
// Create hardware selector
// ----------------------------------------------------------------------------

const hardwareSelector = apiSpecificRenderWindow.getSelector();
hardwareSelector.setCaptureZValues(true);
// TODO: bug in FIELD_ASSOCIATION_POINTS mode
// hardwareSelector.setFieldAssociation(
//   FieldAssociations.FIELD_ASSOCIATION_POINTS
// );
hardwareSelector.setFieldAssociation(FieldAssociations.FIELD_ASSOCIATION_CELLS);

// ----------------------------------------------------------------------------
// Create Mouse listener for picking on mouse move
// ----------------------------------------------------------------------------

function eventToWindowXY(event) {
  // We know we are full screen => window.innerXXX
  // Otherwise we can use pixel device ratio or else...
  const { clientX, clientY } = event;
  const [width, height] = apiSpecificRenderWindow.getSize();
  const x = Math.round((width * clientX) / window.innerWidth);
  const y = Math.round(height * (1 - clientY / window.innerHeight)); // Need to flip Y
  return [x, y];
}

// ----------------------------------------------------------------------------

let needGlyphCleanup = false;
let lastProcessedActor = null;

const updatePositionTooltip = (worldPosition) => {
  if (lastProcessedActor) {
    positionTooltipElem.innerHTML = `Position: ${worldPosition
      .map((v) => v.toFixed(3))
      .join(' , ')}`;
  } else {
    positionTooltipElem.innerHTML = '';
  }
};

const updateAssociationTooltip = (type, id) => {
  if (type !== undefined && id !== undefined) {
    fieldIdTooltipElem.innerHTML = `${type} ID: ${id}`;
  } else {
    fieldIdTooltipElem.innerHTML = '';
  }
};

const updateCompositeAndPropIdTooltip = (compositeID, propID) => {
  if (compositeID !== undefined) {
    compositeIdTooltipElem.innerHTML = `Composite ID: ${compositeID}`;
  } else {
    compositeIdTooltipElem.innerHTML = '';
  }
  if (propID !== undefined) {
    propIdTooltipElem.innerHTML = `Prop ID: ${propID}`;
  } else {
    propIdTooltipElem.innerHTML = '';
  }
};

const updateCursor = (worldPosition) => {
  if (lastProcessedActor) {
    pointerActor.setVisibility(true);
    pointerActor.setPosition(worldPosition);
  } else {
    pointerActor.setVisibility(false);
  }
  renderWindow.render();
  updatePositionTooltip(worldPosition);
};

function processSelections(selections) {
  renderer.getActors().forEach((a) => a.getProperty().setColor(...WHITE));
  if (!selections || selections.length === 0) {
    lastProcessedActor = null;
    updateAssociationTooltip();
    updateCursor();
    updateCompositeAndPropIdTooltip();
    return;
  }

  const {
    worldPosition: rayHitWorldPosition,
    compositeID,
    prop,
    propID,
    attributeID,
  } = selections[0].getProperties();

  updateCompositeAndPropIdTooltip(compositeID, propID);

  let closestCellPointWorldPosition = [...rayHitWorldPosition];
  if (attributeID || attributeID === 0) {
    const input = prop.getMapper().getInputData();
    if (!input.getCells()) {
      input.buildCells();
    }

    // Get matrices to convert coordinates: (prop coordinates) <-> (world coordinates)
    const glTempMat = mat4.fromValues(...prop.getMatrix());
    mat4.transpose(glTempMat, glTempMat);
    const propToWorld = vtkMatrixBuilder.buildFromDegree().setMatrix(glTempMat);
    mat4.invert(glTempMat, glTempMat);
    const worldToProp = vtkMatrixBuilder.buildFromDegree().setMatrix(glTempMat);
    // Compute the position of the cursor in prop coordinates
    const propPosition = [...rayHitWorldPosition];
    worldToProp.apply(propPosition);

    if (
      hardwareSelector.getFieldAssociation() ===
      FieldAssociations.FIELD_ASSOCIATION_POINTS
    ) {
      // Selecting points
      closestCellPointWorldPosition = [
        ...input.getPoints().getTuple(attributeID),
      ];
      propToWorld.apply(closestCellPointWorldPosition);
      updateAssociationTooltip('Point', attributeID);
    } else {
      // Selecting cells
      const cellPoints = input.getCellPoints(attributeID);
      updateAssociationTooltip('Cell', attributeID);
      if (cellPoints) {
        const pointIds = cellPoints.cellPointIds;
        // Find the closest cell point, and use that as cursor position
        const points = Array.from(pointIds).map((pointId) =>
          input.getPoints().getPoint(pointId)
        );
        const distance = (pA, pB) =>
          vtkMath.distance2BetweenPoints(pA, propPosition) -
          vtkMath.distance2BetweenPoints(pB, propPosition);
        const sorted = points.sort(distance);
        closestCellPointWorldPosition = [...sorted[0]];
        propToWorld.apply(closestCellPointWorldPosition);
      }
    }
  }
  lastProcessedActor = prop;
  // Use closestCellPointWorldPosition or rayHitWorldPosition
  updateCursor(closestCellPointWorldPosition);

  // Make the picked actor green
  prop.getProperty().setColor(...GREEN);

  // We hit the glyph, let's scale the picked glyph
  if (prop === cylinderActor) {
    scaleArray.fill(0.5);
    scaleArray[compositeID] = 0.7;
    cylinderPointSet.modified();
    needGlyphCleanup = true;
  } else if (needGlyphCleanup) {
    needGlyphCleanup = false;
    scaleArray.fill(0.5);
    cylinderPointSet.modified();
  }
  renderWindow.render();
}

// ----------------------------------------------------------------------------

function pickOnMouseEvent(event) {
  if (interactor.isAnimating()) {
    // We should not do picking when interacting with the scene
    return;
  }
  const [x, y] = eventToWindowXY(event);

  pointerActor.setVisibility(false);
  hardwareSelector.getSourceDataAsync(renderer, x, y, x, y).then((result) => {
    if (result) {
      processSelections(result.generateSelection(x, y, x, y));
    } else {
      processSelections(null);
    }
  });
}
const throttleMouseHandler = throttle(pickOnMouseEvent, 20);

document.addEventListener('mousemove', throttleMouseHandler);