text:
#include <unordered_map>
#include <cstring> // for memcmp
#include "nmmintrin.h" // for sse4.2 hardware crc checksum
#include <rte_config.h>
#include <rte_mempool.h>
#include <rte_mbuf.h>
#include <rte_errno.h>
#include <rte_spinlock.h>
#include <map>
#include <iostream> // for std::endl
#include <string>
#include <sparsehash/sparse_hash_map>
#include <time.h>
#include "siphash_cookie.h"

typedef struct sparse_hash_map_cookie_key {
	uint32_t ip_src;
	uint32_t ip_dst;
	uint16_t tcp_src;
	uint16_t tcp_dst;

	bool operator==(const sparse_hash_map_cookie_key& rhs) const {
		return
       	this->ip_src == rhs.ip_src &&
       	this->ip_dst == rhs.ip_dst &&
       	this->tcp_src == rhs.tcp_src &&
        this->tcp_dst == rhs.tcp_dst
        ;
    }
} sparse_hash_map_cookie_key;

struct eq_sparse_hash_map_cookie_key{
	bool operator()(const sparse_hash_map_cookie_key& lhs, const sparse_hash_map_cookie_key& rhs) const {
		return
       	lhs.ip_src == rhs.ip_src &&
       	lhs.ip_dst == rhs.ip_dst &&
       	lhs.tcp_src == rhs.tcp_src &&
        lhs.tcp_dst == rhs.tcp_dst
        ;
	}
};

namespace std {
	template <> struct hash<sparse_hash_map_cookie_key>{
		struct sipkey *key = sip_tokey((struct sipkey *) malloc(sizeof(struct sipkey)), "9c16887667f84fd8b81c9ae6cfb3f569");

   		inline size_t operator()(const sparse_hash_map_cookie_key& ft) const {
			// fastest hash possible, but no perf increase
			//uint32_t hash = 0;
			//hash = _mm_crc32_u32(hash, ft.ip_src);
			//hash = _mm_crc32_u32(hash, ft.ip_dst);
			//hash = _mm_crc32_u16(hash, ft.tcp_dst);
			//hash = _mm_crc32_u16(hash, ft.tcp_src);
			//return hash;

			// crypto hash, slower but more resilient to certain attacks
			struct siphash state;

			sip24_init(&state, key);

			unsigned char * dst = new unsigned char[8]();

			sip24_update(&state, "2796094800294027ef5d4ab7a6bff233", 16); // salt
			sip24_update(&state, sip_tobin(dst, ft.ip_src), 4);
			sip24_update(&state, sip_tobin(dst, ft.ip_dst), 4);
			sip24_update(&state, sip_tobin(dst, ft.tcp_src), 2);
			sip24_update(&state, sip_tobin(dst, ft.tcp_dst), 2);

			uint32_t result = (uint32_t) sip24_final(&state);
			return result;
		}
	};
}

// the flags
#define RESET 	0x01
#define CLOSED 	0x02
#define L_VER	0x04
#define R_VER	0x08
#define L_FIN	0x10
#define R_FIN	0x20
#define STALLED	0x40
typedef struct sparse_hash_map_cookie_value {
	uint32_t diff;
	uint8_t flags;
	void* stalled; // pointer to stalled mbuf
} sparse_hash_map_cookie_value;

using sparse_hash_map_cookie = google::sparse_hash_map<sparse_hash_map_cookie_key, sparse_hash_map_cookie_value*, std::hash<sparse_hash_map_cookie_key> , eq_sparse_hash_map_cookie_key>;
using namespace std;

typedef struct sparse_hash_maps_cookie {
	sparse_hash_map_cookie *current;
	sparse_hash_map_cookie *old;
	clock_t last_swap;
} sparse_hash_maps_cookie;

extern "C" {
	/* Google HashMap Sparsehash */
#define	SWAP_INTERVAL 30
	void mg_sparse_hash_map_cookie_swap(sparse_hash_maps_cookie *maps) {
		clock_t time = clock();
		if ( ((double) time - maps->last_swap) > ((double) SWAP_INTERVAL * CLOCKS_PER_SEC) ) {
			printf("swapping\n");

			printf("%f freeing unsent stalled mbufs %f\n", (double) clock(), (double) maps->old->size());
			// free mbufs in old map that will not be sent to prevent memory leaks
			auto it = maps->old->begin();
			while (!(it == maps->old->end())) {
				// only free the buf when there actually is a stalled buf
				if (it->second->flags & STALLED && it->second->stalled) {
					// only free the buf it wasnt copied to current -> perform lookup
					if (maps->current->find(it->first) != maps->current->end()) {
						it->second->flags ^= STALLED;
						rte_pktmbuf_free((rte_mbuf*)it->second->stalled);
						it->second->stalled = NULL;
					}
				}
				it++;
			}
			printf("%f done freeing\n", (double) clock());

			delete maps->old;
			maps->old = maps->current;
			maps->current = new sparse_hash_map_cookie(0);
			maps->last_swap = time;
			printf("done swapping\n");
		}
	}

	sparse_hash_maps_cookie* mg_sparse_hash_map_cookie_create(uint32_t size){
		sparse_hash_maps_cookie *maps = new sparse_hash_maps_cookie;

		sparse_hash_map_cookie *tmp = new sparse_hash_map_cookie(size);
		sparse_hash_map_cookie_key k;
		memset(&k, 0, sizeof(sparse_hash_map_cookie_key));
		tmp->set_deleted_key(k);

		maps->current = tmp;

		tmp = new sparse_hash_map_cookie(size);
		memset(&k, 0, sizeof(sparse_hash_map_cookie_key));
		tmp->set_deleted_key(k);

		maps->old = tmp;

		maps->last_swap = clock();

		return maps;
	}

	/* Insert on setLeftVerified
	 * Always insert into current
	 * Stores the Ack Number for later calculation of the diff in the diff field
	 * Sets the leftVerified flag
	 * If entry already present:
	 *  connection is already left verified,
	 *  hence, this packet and the syn we send next is duplicated
	 *  option A: drop it
	 *  		disadvantage: original syn might have gotten lost (server busy, ...)
	 *  option B (chosen): send again
	 *  		we assume the Ack number has not changed (which it obviously shouldn't)
	 * 		if it has changed, something is wrong
	 * 		hence, we assume the first Ack number to be the correct one and don't update it here
	 */
	void mg_sparse_hash_map_cookie_insert(sparse_hash_maps_cookie *maps, sparse_hash_map_cookie_key *k, uint32_t ack) {
		//printf("insert %d\n", k->tcp_src);
		auto m = maps->current;
		auto it = m->find(*k);
		// not existing yet
        if (it == m->end() ){
 			sparse_hash_map_cookie_value *tmp = new sparse_hash_map_cookie_value;
			tmp->diff = ack;
			tmp->flags = L_VER;
			tmp->stalled = 0;
			(*m)[*k] = tmp;
			//printf("Entry: %d %d %p\n", tmp->diff, tmp->flags, tmp->stalled);
			mg_sparse_hash_map_cookie_swap(maps);
			return;
		} else {
			//printf("NOT inserted, but reused\n");
			// entry exists, but was not verified (connections closed)

 			sparse_hash_map_cookie_value *tmp = (*m)[*k];
			tmp->diff = ack;
			tmp->flags = L_VER;
			tmp->stalled = 0;
			//printf("Entry: %d %d %p\n", tmp->diff, tmp->flags, tmp->stalled);

			mg_sparse_hash_map_cookie_swap(maps);
		}

    };

	/* Finalize an entry on setRightVerified
	 * Find the entry and check that flags are correct (only leftVerified set)
	 * Calculate and store diff from seq number and stored ack number
	 * Set rightVerified flag
	 */
	sparse_hash_map_cookie_value* mg_sparse_hash_map_cookie_finalize(sparse_hash_maps_cookie *maps, sparse_hash_map_cookie_key *k, uint32_t seq) {
		//printf("finalize %d\n", k->tcp_src);
		auto m = maps->current;
		auto it = m->find(*k);
		if (it == m->end() ) {
			//printf("fin not found in current, checking old\n");
			m = maps->old;
			it = m->find(*k);
			if (unlikely(it == m->end())) {
				//printf("fin also not found here\n");
				mg_sparse_hash_map_cookie_swap(maps);
				return 0;
			}
			// copy and proceed normally
			//printf("right found-> copy\n");
			(*(maps->current))[*k] = (*m)[*k];
		}

 		sparse_hash_map_cookie_value *tmp = (*m)[*k];

		// Check that flags are correct
		// Only leftVerified must be set
		if ( unlikely((tmp->flags & (L_VER | R_VER)) != L_VER) ) {
			mg_sparse_hash_map_cookie_swap(maps);
			//printf("ignoring second syn ack\n");
			return tmp;
		}

		tmp->diff = seq - tmp->diff + 1;
		tmp->flags = tmp->flags | R_VER;
		//printf("Entry: %d %d\n", tmp->diff, tmp->flags);
		//printf("Got a stalled buf: %p flags %d\n", tmp->stalled, tmp->flags & STALLED);
		mg_sparse_hash_map_cookie_swap(maps);
		return tmp;
	};

	/* Find and update on isVerified
	 * If it is verified, update the timestamp bits
	 * Return the value struct
	 */
	sparse_hash_map_cookie_value* mg_sparse_hash_map_cookie_find_update(sparse_hash_maps_cookie *maps, sparse_hash_map_cookie_key *k, bool reset, bool left_fin, bool right_fin, bool ack) {
		//printf("is verified %d\n", k->tcp_src);
		auto m = maps->current;
		auto it = m->find(*k);
		if (it == m->end() ) {
			//printf("upd not found in current, checking old\n");
			m = maps->old;
			it = m->find(*k);
			if (unlikely(it == m->end())) {
				//printf("upd also not found here\n");
				mg_sparse_hash_map_cookie_swap(maps);
				//printf("not existing\n");
				return 0;
			}
			// copy and proceed normally
			//printf("find found-> copy\n");
			(*(maps->current))[*k] = (*m)[*k];
		}

		sparse_hash_map_cookie_value *tmp = (*m)[*k];

		// if only left verified we are waiting for right verified
		// in this case stall, indicated by setting flags in a new struct to 0
		if (unlikely((tmp->flags & (L_VER | R_VER)) == L_VER)) {
			if (unlikely(tmp->flags & STALLED)) {
				//printf("already got stalled buf\n");
				return 0;
			}
			mg_sparse_hash_map_cookie_swap(maps);
			tmp->flags = tmp->flags | STALLED;
			return tmp;
		}
		// Check verified flags (both 4 8 must be set)
		if (unlikely((tmp->flags & (L_VER | R_VER)) != (L_VER | R_VER))) {
			mg_sparse_hash_map_cookie_swap(maps);
			//printf("wrong flags\n");
			return 0;
		}

		// check reset flag
		// if it is set the connection is dead and we do nothing
		if (unlikely(tmp->flags & RESET)) {
			//printf("RESET set, act as if not verified\n");
			return 0;
		}
		// set reset flag
		if (unlikely(reset)) {
			tmp->flags = tmp->flags | RESET;
		}

		// check whether conenction was closed via teardown
		if (unlikely(tmp->flags & CLOSED)) {
			//printf("closed via teardown, return\n");
			return 0;
		}
		//check fin flags
		// if both are set and this is an ack, assume this is the last ack of the teardown
		if (unlikely(((tmp->flags & (L_FIN | R_FIN)) == (L_FIN | R_FIN)) && ack)) {
			//printf("final ack of connection, next will be discarded\n");
			tmp->flags = tmp->flags | CLOSED; // close connection
		}

		// set fin flags
		if (unlikely(left_fin)) {
			//printf("set left fin\n");
			tmp->flags = tmp->flags | L_FIN;
		}
		if (unlikely(right_fin)) {
			//printf("set right fin\n");
			tmp->flags = tmp->flags | R_FIN;
		}

		mg_sparse_hash_map_cookie_swap(maps);

		return tmp;
	};
}


text:
package org.iotp.iothub.server.security;

import org.iotp.infomgt.data.id.DeviceId;

public class DeviceAuthResult {

    private final boolean success;
    private final DeviceId deviceId;
    private final String errorMsg;

    public static DeviceAuthResult of(DeviceId deviceId) {
        return new DeviceAuthResult(true, deviceId, null);
    }

    public static DeviceAuthResult of(String errorMsg) {
        return new DeviceAuthResult(false, null, errorMsg);
    }

    private DeviceAuthResult(boolean success, DeviceId deviceId, String errorMsg) {
        super();
        this.success = success;
        this.deviceId = deviceId;
        this.errorMsg = errorMsg;
    }

    public boolean isSuccess() {
        return success;
    }

    public DeviceId getDeviceId() {
        return deviceId;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    @Override
    public String toString() {
        return "DeviceAuthResult [success=" + success + ", deviceId=" + deviceId + ", errorMsg=" + errorMsg + "]";
    }

}

redpajama_set_name: RedPajamaGithub


text:
'Copyright 2019 Esri

'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.
Imports ESRI.ArcGIS.Schematic
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Geometry

Imports ContainerManagerVB.My

Public Class StationsInContainers
	Inherits ESRI.ArcGIS.Desktop.AddIns.Extension

	' The OnAfterLoadDiagram is used to define the relations between elements
	' contained in a diagram. It is called by the AfterLoadDiagram
	' event defined in the schematic project. The diagram contains Stations and
	' Containers elements. For the Station element type, a particular attribute
	' named RelatedFeeder has been created. This attribute is used to identify
	' the container the station is related to. Near the top of the procedure,
	' a new schematic relation is created (SchematicContainerManager). The
	' ISchematicRelationControllerEdit CreateRelation method is used to specify
	' that the station is related to its container. The value set for the
	' RelatedFeeder attribute specifies whether or not the station is related
	' to a container.

    Private m_DatasetMgr As SchematicDatasetManager
    Private s_extension As StationsInContainers

    Protected Overrides Sub OnStartup()

        s_extension = Me
        m_DatasetMgr = New SchematicDatasetManager
    End Sub

    Protected Overrides Sub OnShutdown()

        ResetEvents(False)

        s_extension = Nothing
        m_DatasetMgr = Nothing

    End Sub

    Protected Overrides Function OnSetState(ByVal state As ESRI.ArcGIS.Desktop.AddIns.ExtensionState) As Boolean
        ' Optionally check for a license here
        Me.State = state

        If state = ESRI.ArcGIS.Desktop.AddIns.ExtensionState.Enabled Then
            ResetEvents(True)
        Else
            ResetEvents(False)
        End If

        Return MyBase.OnSetState(state)
    End Function

    Protected Overrides Function OnGetState() As ESRI.ArcGIS.Desktop.AddIns.ExtensionState
        Return Me.State
    End Function

    Public Sub OnStartEditLayer(ByVal schLayer As ESRI.ArcGIS.Schematic.ISchematicLayer)
        If schLayer IsNot Nothing And Me.State = ESRI.ArcGIS.Desktop.AddIns.ExtensionState.Enabled Then
            Updatecontainers(schLayer)
        End If
    End Sub

    Public Sub OnStopEditLayer(ByVal schLayer As ESRI.ArcGIS.Schematic.ISchematicLayer)
        If schLayer IsNot Nothing And Me.State = ESRI.ArcGIS.Desktop.AddIns.ExtensionState.Enabled Then
            Updatecontainers(schLayer)
        End If
    End Sub

    ' Private methods
    Private Sub ResetEvents(ByVal bAdd As Boolean)

        ' make sure the extension is turned on
        If (s_extension Is Nothing Or m_DatasetMgr Is Nothing Or Me.State = ESRI.ArcGIS.Desktop.AddIns.ExtensionState.Unavailable) Then Return

        ' Reset event handlers
        If (bAdd = True And Me.State = ESRI.ArcGIS.Desktop.AddIns.ExtensionState.Enabled) Then
            AddHandler m_DatasetMgr.OnStartEditLayer, AddressOf OnStartEditLayer
            AddHandler m_DatasetMgr.OnStopEditLayer, AddressOf OnStopEditLayer
        Else
            RemoveHandler m_DatasetMgr.OnStartEditLayer, AddressOf OnStartEditLayer
            RemoveHandler m_DatasetMgr.OnStopEditLayer, AddressOf OnStopEditLayer
        End If

        ' Process the opened schematic layers in the maps
        ProcessMaps()
    End Sub

    Private Sub ProcessMaps()

        Dim Maps As IMaps

        Maps = ContainerManagerVB.My.ArcMap.Document.Maps

        ' get the Maps
        Dim lNbMaps As Long = Maps.Count
        Dim i As Integer

        For i = 0 To lNbMaps - 1
            Dim Map As IMap

            Map = Maps.Item(i)
            If Map Is Nothing Then Continue For
            ' browse its layers for a schematic layer
            Dim Layers As IEnumLayer
            Layers = Map.Layers(Nothing, False)
            Dim Layer As ILayer

            Layers.Reset()
            Layer = Layers.Next()
            While (Layer IsNot Nothing)
                Dim schLayer As ISchematicLayer
                schLayer = TryCast(Layer, ISchematicLayer)
                Updatecontainers(schLayer)
                Layer = Layers.Next()
            End While
        Next
    End Sub

    Private Sub Updatecontainers(ByVal schLayer As ESRI.ArcGIS.Schematic.ISchematicLayer)

        If (schLayer Is Nothing) Then Return

        ' get the inMemorydiagram if any
        Dim inMemoryDiagram As ISchematicInMemoryDiagram
        inMemoryDiagram = schLayer.SchematicInMemoryDiagram
        If (inMemoryDiagram Is Nothing) Then Return

        Dim bCreate As Boolean = False

        ' create or remove relations between containers and their contents
        If (Me.State = ESRI.ArcGIS.Desktop.AddIns.ExtensionState.Enabled) Then bCreate = True

        Dim schemContainerClass As ISchematicElementClass = Nothing
        Dim schemElementClass As ISchematicElementClass
        Dim schemStationClass As ISchematicElementClass = Nothing
        Dim enumElementsInContainer As IEnumSchematicInMemoryFeature
        Dim enumContainerElements As IEnumSchematicInMemoryFeature
        Dim schemFeature As ISchematicInMemoryFeature = Nothing
        Dim schemContainerFeature As ISchematicInMemoryFeature = Nothing
        Dim feederOID As Object
        Dim containerNameID As String
        Dim enumElementClass As IEnumSchematicElementClass
        Dim schemAttributeContainer As ISchematicAttributeContainer
        Dim schemAttributeRelatedFeeder As ISchematicAttribute
        Dim schemElement As ISchematicElement = Nothing
        Dim schemRelationController As ISchematicRelationController
        Dim schemRelationControllerEdit As ISchematicRelationControllerEdit
        Dim colContElem As New Collection

        ' Getting SchematicFeature Class Stations and Containers
        enumElementClass = inMemoryDiagram.SchematicDiagramClass.AssociatedSchematicElementClasses
        enumElementClass.Reset()
        schemElementClass = enumElementClass.Next
        While (schemElementClass IsNot Nothing)
            If schemElementClass.Name = "Stations" Then schemStationClass = schemElementClass
            If schemElementClass.Name = "Containers" Then schemContainerClass = schemElementClass
            If (schemStationClass IsNot Nothing AndAlso schemContainerClass IsNot Nothing) Then Exit While

            schemElementClass = enumElementClass.Next
        End While
        ' go out if schemStationClass or schemContainerClass are null
        If (schemStationClass Is Nothing OrElse schemContainerClass Is Nothing) Then Return

        ' Getting the Stations elements that will be displayed in the containers
        enumElementsInContainer = inMemoryDiagram.GetSchematicInMemoryFeaturesByClass(schemStationClass)
        If (enumElementsInContainer Is Nothing) Then Return

        ' Creating the Schematic Container Manager
        schemRelationController = New SchematicRelationController

        ' Creating the Schematic Container Editor that will be used to define the relation between the stations and their container
        schemRelationControllerEdit = schemRelationController

        ' Defining each Container element as a schematic container
        enumContainerElements = inMemoryDiagram.GetSchematicInMemoryFeaturesByClass(schemContainerClass)

        ' Add Container Element to a collection
        enumContainerElements.Reset()
        schemContainerFeature = enumContainerElements.Next
        While (schemContainerFeature IsNot Nothing)
            colContElem.Add(schemContainerFeature, schemContainerFeature.Name)
            schemContainerFeature = enumContainerElements.Next
        End While

        ' Setting the relation between each station and its related container
        enumElementsInContainer.Reset()
        schemFeature = enumElementsInContainer.Next

        While (schemFeature IsNot Nothing)
            ' The relation is specified by the RelatedFeeder attribute value defined for each station
            schemAttributeContainer = CType(schemFeature.SchematicElementClass, ISchematicAttributeContainer)
            schemAttributeRelatedFeeder = schemAttributeContainer.GetSchematicAttribute("RelatedFeeder")

            If schemAttributeRelatedFeeder IsNot Nothing Then
                feederOID = schemAttributeRelatedFeeder.GetValue(CType(schemFeature, ISchematicObject))
                If feederOID IsNot Nothing Then
                    containerNameID = "Container-" & feederOID.ToString()

                    Try
                        ' Retrieve Container Element in the collection
                        schemContainerFeature = CType(colContElem(containerNameID), ISchematicInMemoryFeature)

                        If (bCreate) Then
                            ' Create relation
                            schemRelationControllerEdit.CreateRelation(schemFeature, schemContainerFeature)
                        Else
                            ' delete child relation
                            schemRelationControllerEdit.DeleteRelation(schemFeature)
                        End If
                    Catch
                    End Try
                End If
            End If

            schemContainerFeature = Nothing
            schemFeature = enumElementsInContainer.Next
        End While

        If (bCreate = False) Then

            ' Force container geometry
            enumContainerElements.Reset()
            schemContainerFeature = enumContainerElements.Next()
            While (schemContainerFeature IsNot Nothing)
                Try
                    'set an empty geometry
                    ' container does not have content at this stage
                    Dim emptyRectangle As New Polygon()
                    Dim ContainerGeometry As IGeometry = CType(emptyRectangle, IGeometry)
                    schemContainerFeature.Shape = ContainerGeometry
                Catch
                End Try

                schemContainerFeature = enumContainerElements.Next()
            End While
        End If

        Dim activeView As IActiveView
        activeView = CType(ContainerManagerVB.My.ArcMap.Document.FocusMap, IActiveView)
        If activeView IsNot Nothing Then
            activeView.ContentsChanged()
            activeView.Refresh()
        End If

    End Sub

End Class


text:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference
        android:title="Fondo de pantalla estático"
        android:key="toggle_scrolling_wallpaper"
        android:defaultValue="false"
        android:summaryOn="Activado \n*Fondo de pantalla estático"
        android:summaryOff="Desactivado \n*Fondo de pantalla en movimiento" />
    <PreferenceCategory android:layout="@layout/preferences_category" android:title="Colores Launcher">
        <CheckBoxPreference
            android:title="Texto en Negro"
            android:key="need_dark_font"
            android:defaultValue="false"
            android:summaryOn="Activado \n*Color de texto en negro en Escritorio"
            android:summaryOff="Desactivado" />
        <com.hctrom.romcontrol.prefs.ColorPickerPreference
            android:title="Color del texto Widget"
            android:key="launcher_widgets_text_color"
            android:summary="Elegir el color del texto del Launcher en el apartado Widgets"
            android:defaultValue="#ffffff"
            alphaSlider="true"
            hexValue="true" />
        <SwitchPreference
            android:title="Activar color personalizado"
            android:key="link_launcher_icons_text_color"
            android:summaryOn="Activado"
            android:summaryOff="Desactivado" />
        <com.hctrom.romcontrol.prefs.ColorPickerPreference
            android:title="Color del texto"
            android:dependency="link_launcher_icons_text_color"
            android:key="launcher_icons_text_color"
            android:summary="Elegir el color del texto del Launcher"
            android:defaultValue="#ffffff"
            alphaSlider="true"
            hexValue="true" />
    </PreferenceCategory>
    <PreferenceCategory android:layout="@layout/preferences_category" android:title="Gestos de escritorio">
        <com.hctrom.romcontrol.prefs.MyListPreference
            android:widgetLayout="@layout/round_more_icon"
            android:entries="@array/launcher_gesture_entries"
            android:title="Doble toque"
            android:key="doubletap"
            android:summary="%s"
            android:entryValues="@array/launcher_gesture_values" />
        <com.hctrom.romcontrol.prefs.MyListPreference
            android:widgetLayout="@layout/round_more_icon"
            android:entries="@array/launcher_gesture_entries"
            android:title="Deslizar hacia arriba"
            android:key="swipe_up"
            android:summary="%s"
            android:entryValues="@array/launcher_gesture_values" />
        <com.hctrom.romcontrol.prefs.MyListPreference
            android:widgetLayout="@layout/round_more_icon"
            android:entries="@array/launcher_gesture_entries"
            android:title="Deslizar hacia abajo"
            android:key="swipe_down"
            android:summary="%s"
            android:entryValues="@array/launcher_gesture_values" />
    </PreferenceCategory>
    <PreferenceCategory android:layout="@layout/preferences_category" android:title="Efectos de transicion de escritorio">
        <com.hctrom.romcontrol.prefs.MyListPreference
            android:widgetLayout="@layout/round_more_icon"
            android:entries="@array/tw_effect_entries"
            android:title="Efectos de transición en escritorio"
            android:key="tw_page_effect"
            android:defaultValue="1"
            android:entryValues="@array/tw_effect_values" />
    </PreferenceCategory>
    <PreferenceCategory android:layout="@layout/preferences_category" android:title="Utilidades TW">
        <PreferenceScreen
            android:icon="@drawable/ic_6thgear"
            android:summary="Restablecer preferencias y establecer acción deslizante en 6thGear TWSwipe"
            android:title="6thGear TWSwipe">
            <intent
                android:icon="@drawable/ic_6thgear"
                android:targetClass="com.wubydax.geartwswipe.ResetDialogActivity"
                android:targetPackage="com.wubydax.geartwswipe" />
        </PreferenceScreen>
        <PreferenceScreen
            android:icon="@drawable/ic_wiziconizer"
            android:key="com.wiziconizer.HomeActivity"
            android:summary="Selecciona tu paquete de iconos para TW Launcher"
            android:title="WizIconizer" />
    </PreferenceCategory>
</PreferenceScreen>


text:
package moss.idsca.ac.eventos;

import ws.Evento;
import moss.idsca.ac.App;
import moss.idsca.ac.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

public class EventosDetail extends Activity {

	private App app;
	private Evento evento;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		app = (App) getApplication();

		setContentView(R.layout.evento_detail);
		WebView webview = (WebView) findViewById(R.id.webView1);
		TextView txtNome = (TextView) findViewById(R.id.txtNome);
		TextView txtData = (TextView) findViewById(R.id.txtData);
		TextView txtLocalNome = (TextView) findViewById(R.id.txtLocalNome);
		TextView txtLocalMorada = (TextView) findViewById(R.id.txtLocalMorada);

		evento = app.getEvento();

		txtNome.setText(evento.getNome());
		txtData.setText(evento.getDatahora());
		txtLocalNome.setText(evento.getLocal().getNome());
		txtLocalMorada.setText(evento.getLocal().getMorada());

		webview.loadUrl(evento.getImagemUrl());
	}

	public void showMap(View view) {

		String url = "http://maps.google.com/maps?q="
				+ evento.getLocal().getLatitude() + ","
				+ evento.getLocal().getLongitude()+"&t=m&z=16&vpsrc=0";

		Intent i = new Intent(Intent.ACTION_VIEW);
		i.setData(Uri.parse(url));
		startActivity(i);
	}

}


text:
__author__ = "Mathieu Fenniak"

import dbapi as DBAPI
pg8000_dbapi = DBAPI

from interface import *
from types import Bytea


text:
// Copyright NVIDIA Corporation 2011-2015
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//  * Neither the name of NVIDIA CORPORATION nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <dp/util/Observer.h>

#include <algorithm>

namespace dp
{
  namespace util
  {

    Payload::~Payload()
    {
    }

    /************************************************************************/
    /* Subject                                                              */
    /************************************************************************/

    Subject::~Subject()
    {
      Observers::iterator it, itEnd = m_observers.end();

      for ( it = m_observers.begin(); it != itEnd;++it )
      {
        if( it->first )
        {
          it->first->onDestroyed( *this, it->second );
        }
      }
    }

    void Subject::attach( Observer *observer, Payload *payload )
    {
      m_observers.push_back( ObserverEntry(observer, payload) );
    }

    void Subject::detach( Observer *observer, Payload *payload )
    {
      Observers::iterator it = std::find(m_observers.begin(), m_observers.end(), std::make_pair(observer, payload) );
      if ( it != m_observers.end() )
      {
        // mark this entry as invalid instead of deleting it
        it->first = nullptr;
      }
    }

    bool Subject::isAttached( Observer * observer, Payload * payload ) const
    {
      return( std::find( m_observers.begin(), m_observers.end(), std::make_pair(observer, payload) ) != m_observers.end() );
    }

    void Subject::notify( const Event &event )
    {
      // notify the list ob observers, cleaning the list of observers set to nullptr on the way
      // don't clean the list first and notify then, this would mean iterating the list of observers twice
      if( !m_observers.empty() )
      {
        size_t idx;
        size_t last = m_observers.size() - 1;
        size_t deletedElements = 0;

        for ( idx = 0; idx <= last; ++idx )
        {
          if( !m_observers[idx].first )
          {
            // observer at idx was detached, find a valid observer beginning at the end of the vector
            while( !m_observers[last].first && last > idx )
            {
              --last;
              ++deletedElements;
            }

            if( last != idx )
            {
              // move the last valid observer to position idx
              m_observers[idx] = m_observers[last];

              // abandon the last observer
              --last;
              ++deletedElements;
            }
            else
            {
              ++deletedElements;
              // no valid observers left, leave the for loop
              break;
            }
          }

          m_observers[idx].first->onNotify( event, m_observers[idx].second );
        }

        if( deletedElements )
        {
          m_observers.resize( m_observers.size() - deletedElements );
        }
      }
    }

    /************************************************************************/
    /* SubjectTrackingObserver                                              */
    /************************************************************************/
    /*
    SubjectTrackingObserver::~SubjectTrackingObserver()
    {
      Subjects::iterator it;
      Subjects::iterator it_end = m_subjects.end();
      for( it = m_subjects.begin(); it != it_end; ++it )
      {
        it->m_subject->detach( this, it->m_payload );
      }
    }

    void SubjectTrackingObserver::addSubject( Subject& subject, Payload* payload )
    {
      payload->subjectPosition = m_subjects.size();
      m_subjects.push_back( SubjectEntry(&subject, payload) );

      subject.attach( this, payload );

      doAddSubject( subject, payload );
    }

    void SubjectTrackingObserver::removeSubject( Subject& subject, Payload* payload )
    {
      doRemoveSubject( subject, payload );

      subject.detach( this, payload );

      DP_ASSERT( m_subjects[payload->subjectPosition].m_subject == &subject );
      DP_ASSERT( m_subjects[payload->subjectPosition].m_payload == payload );
      removeSubjectEntry( payload->subjectPosition );
    }

    void SubjectTrackingObserver::onDestroyed( const Subject& subject, Payload* payload )
    {
      doOnDestroyed( subject, payload );

      DP_ASSERT( m_subjects[payload->subjectPosition].m_subject == &subject );
      DP_ASSERT( m_subjects[payload->subjectPosition].m_payload == payload );
      removeSubjectEntry( payload->subjectPosition );
    }

    void SubjectTrackingObserver::doAddSubject( Subject& subject, Payload* payload )
    {
    }

    void SubjectTrackingObserver::doRemoveSubject( Subject& subject, Payload* payload )
    {
    }

    void SubjectTrackingObserver::doOnDestroyed( const Subject& subject, Payload* payload )
    {
    }

    void SubjectTrackingObserver::removeSubjectEntry( size_t position )
    {
      // move the last subject of the vector to the subject's position, if subject is not the last
      if( position != m_subjects.size() - 1 )
      {
        // move entry
        m_subjects[position] = m_subjects.back();

        // update it's position
        m_subjects[position].m_payload->subjectPosition = position;
      }

      m_subjects.resize( m_subjects.size() - 1 );
    }
    */
  } // namespace util
} // namespace dp


text:
use strict;
use warnings FATAL => 'all';

local $| = 1; # unbuffered mode

my $r = shift;

print "Content-Type: text/html\n\n";
print "yet another boring test string";

# This line passes a bucket brigade with a single bucket FLUSH
# it was causing problems in the mod_deflate filter which was trying to
# deflate empty output buffer, (the previous print has already flushed
# all the output) (the fix in mod_deflate.c was to check whether the
# buffer is full)
print "";


text:
/* eslint-disable max-nested-callbacks */

const beaker = require('../../../src/test-utils');

describe('test-utils', () => {
    beforeEach(() => {
        jasmine.clock().install();
    });

    afterEach(() => {
        jasmine.clock().uninstall();
    });

    describe('.e2e.getUrl()', () => {
        let testConfig;

        beforeEach(() => {
            testConfig = {
                http: {
                    entryPoint: 'services',
                    host: 'localhost',
                    port: '80',
                },
            };
        });

        it('returns proper URL', () => {
            expect(beaker.e2e.getUrl(testConfig)).toEqual('http://localhost:80/services');
        });

        it('removes double forward slash from entry point', () => {
            testConfig.http.entryPoint = '/services';
            expect(beaker.e2e.getUrl(testConfig)).toEqual('http://localhost:80/services');
        });

        it('removes double forward slash between entry point and extra', () => {
            testConfig.http.entryPoint = 'services/';
            expect(beaker.e2e.getUrl(testConfig, '/view-service')).toEqual('http://localhost:80/services/view-service');
        });
    });

    describe('.wait()', () => {
        let doneSpy;
        beforeEach(() => {
            doneSpy = jasmine.createSpy('doneSpy');
            beaker.wait(doneSpy, 500);
        });

        it('does not call done yet', () => {
            expect(doneSpy).not.toHaveBeenCalled();
        });

        describe('after timeout passes', () => {
            beforeEach(() => {
                jasmine.clock().tick(500);
            });

            it('calls done', () => {
                expect(doneSpy).toHaveBeenCalled();
            });
        });
    });
});


text:
import os
import sys
import tempfile
from datetime import timedelta

import matplotlib
from kombu import Exchange, Queue

matplotlib.use('Agg')

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

DEBUG = True

DOMAIN_NAME = "http://neurovault.org"

TEMPLATE_DEBUG = DEBUG

ADMINS = (
    (('Chris', 'krzysztof.gorgolewski@gmail.com'))
)

MANAGERS = ADMINS

SITE_ID = 1

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'postgres',
        # The following settings are not used with sqlite3:
        'USER': 'postgres',
        'HOST': 'db',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '5432',        # Set to empty string for default.
    }
}

# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = ["*"]

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'Europe/Berlin'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/var/www/example.com/media/"
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/public/media/'

PRIVATE_MEDIA_ROOT = '/var/www/image_data'
PRIVATE_MEDIA_URL = '/media/images'

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = '/var/www/static'

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'neurovault.apps.statmaps.middleware.CollectionRedirectMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'neurovault.urls'

TEST_RUNNER = 'django.test.runner.DiscoverRunner'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'neurovault.wsgi.application'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': (),
        'OPTIONS': {'context_processors': ("django.contrib.auth.context_processors.auth",
                                            "django.core.context_processors.debug",
                                            "django.core.context_processors.i18n",
                                            "django.core.context_processors.media",
                                            "django.core.context_processors.static",
                                            "django.core.context_processors.tz",
                                            "django.contrib.messages.context_processors.messages",
                                            'django.core.context_processors.request'),
                    'loaders': ('hamlpy.template.loaders.HamlPyFilesystemLoader',
                                'hamlpy.template.loaders.HamlPyAppDirectoriesLoader',
                                'django.template.loaders.filesystem.Loader',
                                'django.template.loaders.app_directories.Loader',
                                )}
    }
]

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'neurovault.apps.main',
    'neurovault.apps.statmaps',
    'neurovault.apps.users',
    'django.contrib.sitemaps',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
     #'django.contrib.admindocs',
    'social.apps.django_app.default',
    'rest_framework',
    'taggit',
    'crispy_forms',
    'coffeescript',
    'taggit_templatetags',
    #'south',
    'dbbackup',
    'polymorphic',
    'djcelery',
    'django_cleanup',
    'file_resubmit',
    'django_mailgun',
    'django_hstore',
    'guardian',
    'oauth2_provider',
    'fixture_media'
)

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
# LOGGING = {

#     'version': 1,
#     'disable_existing_loggers': False,
#     'filters': {
#         'require_debug_false': {
#             '()': 'django.utils.log.RequireDebugFalse'
#         }
#     },
#     'handlers': {
#         'mail_admins': {
#             'level': 'ERROR',
#             'filters': ['require_debug_false'],
#             'class': 'django.utils.log.AdminEmailHandler'
#         }
#     },
#     'loggers': {
#         'django.request': {
#             'handlers': ['mail_admins'],
#             'level': 'ERROR',
#             'propagate': True,
#         },
#     }
# }

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'social.backends.facebook.FacebookOAuth2',
    'social.backends.google.GoogleOAuth2',
    'guardian.backends.ObjectPermissionBackend',
)

SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.social_details',
    'social.pipeline.social_auth.social_uid',
    'social.pipeline.social_auth.auth_allowed',
    'social.pipeline.social_auth.social_user',
    'social.pipeline.user.get_username',
    'social.pipeline.social_auth.associate_by_email',  # <--- enable this one
    'social.pipeline.user.create_user',
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details'
)

SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']

SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer'

REST_FRAMEWORK = {
    # Use hyperlinked styles by default.
    # Only used if the `serializer_class` attribute is not set on a view.
    'DEFAULT_MODEL_SERIALIZER_CLASS':
        'rest_framework.serializers.HyperlinkedModelSerializer',

    # LimitOffsetPagination will allow to set a ?limit= and ?offset=
    # variable in the URL.
    'DEFAULT_PAGINATION_CLASS':
        'neurovault.api.pagination.StandardResultPagination',

    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
    ],

    'DEFAULT_AUTHENTICATION_CLASSES': (
        'oauth2_provider.ext.rest_framework.OAuth2Authentication',
    ),
    'DEFAULT_RENDERER_CLASSES': (
        'neurovault.api.utils.ExplicitUnicodeJSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ),
    'UNICODE_JSON': True,
}

OAUTH2_PROVIDER = {
    'REQUEST_APPROVAL_PROMPT': 'auto'
}

LOGIN_REDIRECT_URL = '/my_collections/'
#LOGIN_URL          = '/login-form/'
#LOGIN_ERROR_URL    = '/login-error/'

CRISPY_TEMPLATE_PACK = 'bootstrap3'

DBBACKUP_STORAGE = 'dbbackup.storage.dropbox_storage'
DBBACKUP_TOKENS_FILEPATH = '/home/filo/dbtokens'
DBBACKUP_POSTGRES_BACKUP_COMMAND = 'export PGPASSWORD=neurovault\n pg_dump --username={adminuser} --host={host} --port={port} {databasename} >'

# For Apache, use 'sendfile.backends.xsendfile'
# For Nginx, use 'sendfile.backends.nginx'
# For Devserver, use 'sendfile.backends.development'
SENDFILE_BACKEND = 'sendfile.backends.development'

PRIVATE_MEDIA_REDIRECT_HEADER = 'X-Accel-Redirect'

PYCORTEX_DATASTORE = os.path.join(BASE_DIR,'pycortex_data')

CACHES = {
            'default': {
                'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            },
            "file_resubmit": {
                'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
                "LOCATION": '/tmp/file_resubmit/'
            }
          }

# Mandrill config
EMAIL_BACKEND = 'django_mailgun.MailgunBackend'
MAILGUN_ACCESS_KEY = 'key-3ax6xnjp29jd6fds4gc373sgvjxteol0' # replace with a real key in production
MAILGUN_SERVER_NAME = 'samples.mailgun.org'# replace with 'neurovault.org' in production
DEFAULT_FROM_EMAIL = "noreply@neurovault.org"

if os.path.exists('/usr/local/share/pycortex/db/fsaverage'):
    STATICFILES_DIRS = (
                        ('pycortex-resources', '/usr/local/lib/python2.7/site-packages/cortex/webgl/resources'),
                        ('pycortex-ctmcache', '/usr/local/share/pycortex/db/fsaverage/cache')
                        )

# Celery config
BROKER_URL = 'redis://redis:6379/0'
CELERY_RESULT_BACKEND = 'djcelery.backends.database:DatabaseBackend'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_DEFAULT_QUEUE = 'default'
CELERY_QUEUES = (
    Queue('default', Exchange('default'), routing_key='default'),
)
CELERY_IMPORTS = ('neurovault.apps.statmaps.tasks', )

CELERYBEAT_SCHEDULE = {
    'anima_crawl_every day': {
        'task': 'crawl_anima',
        'schedule': timedelta(days=1)
    },
}

CELERY_TIMEZONE = 'Europe/Berlin'

ANONYMOUS_USER_ID = -1

DEFAULT_OAUTH_APPLICATION_ID = -1
DEFAULT_OAUTH_APP_NAME = 'DefaultOAuthApp'
DEFAULT_OAUTH_APP_OWNER_ID = -2
DEFAULT_OAUTH_APP_OWNER_USERNAME = 'DefaultAppOwner'
OAUTH_PERSONAL_TOKEN_LENGTH = 40

# Bogus secret key.
try:
    from secrets import *
except ImportError:
    from bogus_secrets import *

try:
    from local_settings import *
except ImportError:
    pass

# freesurfer/pycortex environment
os.environ["XDG_CONFIG_HOME"] = PYCORTEX_DATASTORE
os.environ["FREESURFER_HOME"] = "/opt/freesurfer"
os.environ["SUBJECTS_DIR"] = os.path.join(os.environ["FREESURFER_HOME"],"subjects")
os.environ["FSLOUTPUTTYPE"] = "NIFTI_GZ"

# provToolbox path
os.environ["PATH"] += os.pathsep + '/path/to/lib/provToolbox/bin'

#CELERYBEAT_SCHEDULE = {
#    'run_make_correlation_df': {
#        'task': 'neurovault.apps.statmaps.tasks...',
#        'schedule': timedelta(minutes=30),
#    },
#}
# or manage periodic schedule in django admin
#CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'

if "test" in sys.argv or "benchmark" in sys.argv:
    test_media_root = os.path.join(tempfile.mkdtemp(prefix="neurovault_test_"))
    PRIVATE_MEDIA_ROOT = test_media_root
    CELERY_ALWAYS_EAGER = True
    CELERY_EAGER_PROPAGATES_EXCEPTIONS = True

TAGGIT_CASE_INSENSITIVE=True

FIXTURE_DIRS = (
    'apps/statmaps/fixtures/',
)

MEDIA_ROOT = PRIVATE_MEDIA_ROOT


text:
classdef Tree < handle
    % Tree implementation

    % Copyright 2012 Clayton Ernst, Andrew Hagen, Eric Lee and Andreas Kotowicz
    % Written for Engineering 177 Spring 2010, Final Project. Professor:
    % Andy Packard, UC Berkeley.

    properties (SetAccess = private)
        myChildren
    end

    properties
        myInfo
    end

    methods
        function T = Tree(info, children)
            if (nargin == 1)
                T.myChildren = {};
            else
                T.myChildren = children;
            end
            T.myInfo = info;
        end

        function set.myChildren(T,newChildren)
            if isa(newChildren,'cell')
                for i = 1:length(newChildren)
                    if not(isa(newChildren{i},'classInheritance.Tree'))
                        error('myChildren must be a cell array of trees')
                    end
                end
                T.myChildren = newChildren;
            else
                error('myChildren must be a cell array of trees')
            end
        end

        function addChild(T,newTree)
            if isa(newTree,'classInheritance.Tree')
                T.myChildren = horzcat(T.myChildren, {newTree});
            elseif isa(newTree,'cell')
                T.myChildren = horzcat(T.myChildren, newTree);
            end
        end

        function numNodes = countNodes(T)
            numNodes = 0;
            if isempty(T.myChildren)
                numNodes = 1;
            else
                for i = 1:length(T.myChildren)
                    numNodes = numNodes + countNodes(T.myChildren{i});
                end
                numNodes = numNodes + 1;
            end
        end

        function treeDepth = depth(T)
            depthArray = zeros(1,length(T.myChildren));
            if isempty(T.myChildren)
                treeDepth = 1;
            else
                for i = 1:length(T.myChildren)
                    depthArray(i) = 1 + depth(T.myChildren{i});
                end
                treeDepth = max(depthArray);
            end
        end

        function nodeList = breadthTrav(T)
            nodeList{1} = T;
            k = 1;
            kBefore = k;
            tempT = {T};
            for treeLevel = 2:depth(T)
                for currentTree = 1:length(tempT)
                    if not(isempty(tempT{currentTree}.myChildren))
                        for i = 1:length(tempT{currentTree}.myChildren)
                            nextTree = tempT{currentTree}.myChildren{i};
                            if not(cellfun(@(t) (t.equals(nextTree)), nodeList))
                                nodeList{k+i} = tempT{currentTree}.myChildren{i};
                            else
                                k = k - 1;
                            end
                        end
                        k = k + length(tempT{currentTree}.myChildren);
                    end
                end
                tempT = {nodeList{ kBefore+1 : k }};
                kBefore = k;
            end
        end

        function bool = equals(T,otherT)
            bool = (strcmp(T.myInfo,otherT.myInfo));
        end

        function CNID = getCNID(T)
            % CNID is a cell array. CNID{1} is the Connection Matrix and
            % CNID{2} is the ID array.
            nodeList = T.breadthTrav;
            numNodes = length(nodeList);
            if (numNodes == 1)
                CN = 1;
                IDs = nodeList{1}.myInfo;
            else
                CN = zeros(numNodes);
                IDs = cell(1,numNodes);
                s = struct;
                for i = 1:numNodes
                    nodeName = nodeList{i}.myInfo;
                    s.(nodeName) = i;
                end
                for i = 1:numNodes
                    currentNode = nodeList{i};
                    IDs{i} = currentNode.myInfo;
                    for x = 1:length(currentNode.myChildren)
                        currentChild = currentNode.myChildren{x};
                        CN(i,s.(currentChild.myInfo)) = 1;
                    end
                end
            end
            CNID = {CN IDs};
        end
    end
end

% Class Inheritance Browser Copyright Clayton Ernst, Andrew Hagen, Eric Lee and Andreas Kotowicz 2012.


text:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/share_deck"
        android:title="@string/share_deck"/>
    <item
        android:id="@+id/delete_deck"
        android:title="@string/delete_deck"/>
</menu>


text:
using boost::program_options::variables_map;
using boost::program_options::options_description;
using boost::program_options::value;

using std::chrono::seconds;

using hpx::init;
using hpx::finalize;

using hpx::threads::pending;
using hpx::threads::suspended;
using hpx::threads::get_self_id;
using hpx::threads::get_self;
using hpx::threads::set_thread_state;

using hpx::util::high_resolution_timer;

///////////////////////////////////////////////////////////////////////////////
int hpx_main(variables_map& vm)
{
    {
        std::cout << "waiting for 5 seconds\n";

        high_resolution_timer t;

        // Schedule a wakeup in 5 seconds.
        set_thread_state(get_self_id(), seconds(5), pending);

        // Suspend this HPX thread.
        hpx::this_thread::suspend(suspended);

        std::cout << "woke up after " << t.elapsed() << " seconds\n";
    }

    finalize();
    return 0;
}

///////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
    // Configure application-specific options.
    options_description
       desc_commandline("Usage: " HPX_APPLICATION_STRING " [options]");

    // Initialize and run HPX.
    return init(desc_commandline, argc, argv);
}


text: Full national results »


text:
Household members relationship to YOU?

What is {{ person.name }}'s relationship to you?

    <select name="relationship" id="relation_{{ $index }}" ng-model="person.relation" ng-options="opt.value as opt.name for opt in relationshipOptions"></select>

Continue


text:
package discovery

import (
	"fmt"
	"html/template"
	"sort"
	"strings"

	"google.golang.org/protobuf/proto"

	querypb "vitess.io/vitess/go/vt/proto/query"
	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
	"vitess.io/vitess/go/vt/topo/topoproto"
)

// TabletsCacheStatus is the current tablets for a cell/target.
type TabletsCacheStatus struct {
	Cell         string
	Target       *querypb.Target
	TabletsStats TabletStatsList
}

// TabletStatsList is used for sorting.
type TabletStatsList []*TabletHealth

// Len is part of sort.Interface.
func (tsl TabletStatsList) Len() int {
	return len(tsl)
}

// Less is part of sort.Interface
func (tsl TabletStatsList) Less(i, j int) bool {
	name1 := topoproto.TabletAliasString(tsl[i].Tablet.Alias)
	name2 := topoproto.TabletAliasString(tsl[j].Tablet.Alias)
	return name1 < name2
}

// Swap is part of sort.Interface
func (tsl TabletStatsList) Swap(i, j int) {
	tsl[i], tsl[j] = tsl[j], tsl[i]
}

func (tsl TabletStatsList) deepEqual(other TabletStatsList) bool {
	if len(tsl) != len(other) {
		return false
	}
	for i, th := range tsl {
		o := other[i]
		if !th.DeepEqual(o) {
			return false
		}
	}
	return true
}

// StatusAsHTML returns an HTML version of the status.
func (tcs *TabletsCacheStatus) StatusAsHTML() template.HTML {
	tLinks := make([]string, 0, 1)
	if tcs.TabletsStats != nil {
		sort.Sort(tcs.TabletsStats)
	}
	for _, ts := range tcs.TabletsStats {
		color := "green"
		extra := ""
		if ts.LastError != nil {
			color = "red"
			extra = fmt.Sprintf(" (%v)", ts.LastError)
		} else if !ts.Serving {
			color = "red"
			extra = " (Not Serving)"
		} else if ts.Target.TabletType == topodatapb.TabletType_PRIMARY {
			extra = fmt.Sprintf(" (PrimaryTermStartTime: %v)", ts.PrimaryTermStartTime)
		} else {
			extra = fmt.Sprintf(" (RepLag: %v)", ts.Stats.ReplicationLagSeconds)
		}
		name := topoproto.TabletAliasString(ts.Tablet.Alias)
		tLinks = append(tLinks, fmt.Sprintf(`<a href="%s" style="color:%v">%v</a>%v`, ts.getTabletDebugURL(), color, name, extra))
	}
	return template.HTML(strings.Join(tLinks, "<br>"))
}

func (tcs *TabletsCacheStatus) deepEqual(otcs *TabletsCacheStatus) bool {
	return tcs.Cell == otcs.Cell &&
		proto.Equal(tcs.Target, otcs.Target) &&
		tcs.TabletsStats.deepEqual(otcs.TabletsStats)
}

// TabletsCacheStatusList is used for sorting.
type TabletsCacheStatusList []*TabletsCacheStatus

// Len is part of sort.Interface.
func (tcsl TabletsCacheStatusList) Len() int {
	return len(tcsl)
}

// Less is part of sort.Interface
func (tcsl TabletsCacheStatusList) Less(i, j int) bool {
	return tcsl[i].Cell+"."+tcsl[i].Target.Keyspace+"."+tcsl[i].Target.Shard+"."+string(tcsl[i].Target.TabletType) <
		tcsl[j].Cell+"."+tcsl[j].Target.Keyspace+"."+tcsl[j].Target.Shard+"."+string(tcsl[j].Target.TabletType)
}

// Swap is part of sort.Interface
func (tcsl TabletsCacheStatusList) Swap(i, j int) {
	tcsl[i], tcsl[j] = tcsl[j], tcsl[i]
}

func (tcsl TabletsCacheStatusList) deepEqual(other TabletsCacheStatusList) bool {
	if len(tcsl) != len(other) {
		return false
	}
	for i, tcs := range tcsl {
		otcs := other[i]
		if !tcs.deepEqual(otcs) {
			return false
		}
	}
	return true
}


text:
import React from "react";
import {unmountComponentAtNode} from "react-dom";

import {mount} from "enzyme";
import chai from "chai";
import sinon from "sinon";

import {MegadraftPlugin} from "megadraft";
import TableBlock from "../src/Block";
import TableManagerModal from "../src/TableManagerModal";
import {ValidTableConfig} from "./fixtures";

const expect = chai.expect;

describe("Table Block", function () {

  const container = {
    remove: sinon.spy(),
    plugin: sinon.spy(),
    setReadOnly: sinon.spy(),
    updateData: sinon.spy()
  };

  beforeEach(function() {
    const data = Object.assign({}, ValidTableConfig);
    this.block = mount(<TableBlock container={container} blockProps={container} data={data} />);
    this.popin = this.block.find(TableManagerModal);
  });

  afterEach(function() {
    container.updateData.resetHistory();

    unmountComponentAtNode(document);
    document.body.innerHTML = "";
  });

  it("editable popin should be close", function() {
    expect(this.popin.prop("isOpen")).to.be.false;
  });

  it("editable popin should open when EditButton was clicked", function() {
    const editButton = this.block.find(MegadraftPlugin.BlockAction).first();

    editButton.simulate("click");

    expect(this.block.find(TableManagerModal).prop("isOpen")).to.be.true;
  });

  describe("on save", function() {

    beforeEach(function() {
      this.block.setState({isEditing:true});
      expect(this.block.find(TableManagerModal).prop("isOpen")).to.be.true;
      const addButton = document.querySelector(".table-manager-modal__add-button");
      addButton.click();
    });

    it("should call updateData", function() {
      expect(container.updateData.calledOnce).to.be.true;
    });

    it("should close popin", function() {
      this.block.update();
      expect(this.block.find(TableManagerModal).prop("isOpen")).to.be.false;
    });

  });
});


text:
package org.batfish.datamodel.answers;

import static org.batfish.datamodel.Names.escapeNameIfNeeded;
import static org.batfish.datamodel.answers.AutoCompleteUtils.autoCompleteSourceLocation;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import org.batfish.common.CompletionMetadata;
import org.batfish.datamodel.Ip;
import org.batfish.datamodel.Prefix;
import org.batfish.datamodel.answers.InputValidationNotes.Validity;
import org.batfish.datamodel.questions.Variable;
import org.batfish.referencelibrary.ReferenceLibrary;
import org.batfish.role.NodeRolesData;
import org.batfish.specifier.parboiled.Grammar;
import org.batfish.specifier.parboiled.ParboiledInputValidator;

/** A utility class to validate question inputs */
@ParametersAreNonnullByDefault
public final class InputValidationUtils {

  /**
   * Validates user-provided input for questions. It is meant to do something interesting only for
   * variable types that are top-level (e.g., nodeSpec), and not sub parts (e.g., address group
   * names).
   */
  @Nonnull
  public static InputValidationNotes validate(
      Variable.Type varType,
      String query,
      CompletionMetadata completionMetadata,
      NodeRolesData nodeRolesData,
      ReferenceLibrary referenceLibrary) {

    switch (varType) {
      case APPLICATION_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.APPLICATION_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      case BGP_PEER_PROPERTY_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.BGP_PEER_PROPERTY_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      case BGP_PROCESS_PROPERTY_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.BGP_PROCESS_PROPERTY_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      case BGP_ROUTE_STATUS_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.BGP_ROUTE_STATUS_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      case BGP_SESSION_COMPAT_STATUS_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.BGP_SESSION_COMPAT_STATUS_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      case OSPF_SESSION_STATUS_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.OSPF_SESSION_STATUS_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      case BGP_SESSION_STATUS_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.BGP_SESSION_STATUS_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      case BGP_SESSION_TYPE_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.BGP_SESSION_TYPE_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      case FILTER_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.FILTER_SPECIFIER, query, completionMetadata, nodeRolesData, referenceLibrary);
      case INTERFACE_PROPERTY_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.INTERFACE_PROPERTY_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      case INTERFACES_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.INTERFACE_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      case IP:
        return validateIp(query);
      case IP_PROTOCOL_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.IP_PROTOCOL_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      case IP_SPACE_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.IP_SPACE_SPECIFIER, query, completionMetadata, nodeRolesData, referenceLibrary);
      case IPSEC_SESSION_STATUS_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.IPSEC_SESSION_STATUS_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      case LOCATION_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.LOCATION_SPECIFIER, query, completionMetadata, nodeRolesData, referenceLibrary);
      case MLAG_ID_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.MLAG_ID_SPECIFIER, query, completionMetadata, nodeRolesData, referenceLibrary);
      case NAMED_STRUCTURE_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.NAMED_STRUCTURE_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      case NODE_NAME:
        return validateNodeName(query, completionMetadata.getNodes().keySet());
      case NODE_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.NODE_SPECIFIER, query, completionMetadata, nodeRolesData, referenceLibrary);
      case NODE_PROPERTY_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.NODE_PROPERTY_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      case OSPF_INTERFACE_PROPERTY_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.OSPF_INTERFACE_PROPERTY_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      case OSPF_PROCESS_PROPERTY_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.OSPF_PROCESS_PROPERTY_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      case PREFIX:
        return validatePrefix(query);
      case ROUTING_PROTOCOL_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.ROUTING_PROTOCOL_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      case ROUTING_POLICY_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.ROUTING_POLICY_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      case SINGLE_APPLICATION_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.SINGLE_APPLICATION_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      case SOURCE_LOCATION:
        return validateSourceLocation(query, false, completionMetadata);
      case TRACEROUTE_SOURCE_LOCATION:
        return validateSourceLocation(query, true, completionMetadata);
      case VXLAN_VNI_PROPERTY_SPEC:
        return ParboiledInputValidator.validate(
            Grammar.VXLAN_VNI_PROPERTY_SPECIFIER,
            query,
            completionMetadata,
            nodeRolesData,
            referenceLibrary);
      default:
        return new InputValidationNotes(Validity.VALID, ImmutableList.of());
    }
  }

  @VisibleForTesting
  @Nonnull
  static InputValidationNotes validateSourceLocation(
      String query, boolean tracerouteSource, CompletionMetadata completionMetadata) {
    Validity validity =
        autoCompleteSourceLocation(query, tracerouteSource, completionMetadata).stream()
                .anyMatch(suggestion -> suggestion.getText().equals(query))
            ? Validity.VALID
            : Validity.INVALID;
    return new InputValidationNotes(validity, ImmutableList.of());
  }

  public static String getErrorMessage(String grammarName, int startIndex) {
    return String.format("Cannot parse input as %s at index %d", grammarName, startIndex);
  }

  public static String getErrorMessage(IllegalArgumentException exception) {
    return exception.getMessage();
  }

  public static String getErrorMessageEmptyNameRegex(String nameRegex, String nameType) {
    return String.format("Regex /%s/ does not match any %s", nameRegex, nameType);
  }

  public static String getErrorMessageMissingName(String name, String nameType) {
    return String.format(
        "%s %s does not exist", capitalizeFirstChar(nameType), escapeNameIfNeeded(name));
  }

  /**
   * This function considers node role dimension as a book type, and node roles as groups within it
   */
  public static String getErrorMessageMissingGroup(
      String group, String groupType, String book, String bookType) {
    return String.format(
        "%s %s does not exist in %s %s",
        capitalizeFirstChar(groupType),
        escapeNameIfNeeded(group),
        bookType,
        escapeNameIfNeeded(book));
  }

  /** This function considers node role dimension as a book type, in addition to reference books */
  public static String getErrorMessageMissingBook(String book, String bookType) {
    return String.format(
        "%s %s does not exist", capitalizeFirstChar(bookType), escapeNameIfNeeded(book));
  }

  private static String capitalizeFirstChar(String nameType) {
    if (nameType.length() > 1) {
      return nameType.substring(0, 1).toUpperCase() + nameType.substring(1);
    }
    return nameType;
  }

  @VisibleForTesting
  static InputValidationNotes validateIp(String query) {
    try {
      Ip ip = Ip.parse(query);
      return new InputValidationNotes(Validity.VALID, ip.toString());
    } catch (Exception e) {
      return new InputValidationNotes(Validity.INVALID, e.getMessage());
    }
  }

  @VisibleForTesting
  static InputValidationNotes validatePrefix(String query) {
    try {
      Prefix pfx = Prefix.parse(query);
      return new InputValidationNotes(Validity.VALID, pfx.toString());
    } catch (Exception e) {
      return new InputValidationNotes(Validity.INVALID, e.getMessage());
    }
  }

  @VisibleForTesting
  static InputValidationNotes validateNodeName(String query, Set<String> snapshotHostnames) {
    // TODO: distinguish between no-match and syntactically invalid queries
    if (snapshotHostnames.contains(query.toLowerCase())) {
      return new InputValidationNotes(Validity.VALID, query);
    } else {
      return new InputValidationNotes(Validity.NO_MATCH, "");
    }
  }
}


text:
"""Type classes for LIT inputs and outputs.

These are simple dataclasses used in model.input_spec() and model.output_spec()
to describe the semantics of the model outputs, while allowing clients to still
use flexible data structures.

These are used by the LIT framework to configure front-end components and to
enable different generation and visualization modules. For example, the input
spec allows LIT to automatically generate input forms for common types like text
segments or class labels, while the output spec describes how the model output
should be rendered.
"""
import abc
from typing import Any, Dict, List, NewType, Optional, Sequence, Text, Tuple, Union

import attr

JsonDict = Dict[Text, Any]
Input = JsonDict  # TODO(lit-dev): stronger typing using NewType
IndexedInput = NewType("IndexedInput", JsonDict)  # has keys: id, data, meta
ExampleId = Text
TokenTopKPredsList = List[List[Tuple[str, float]]]

##
# Base classes, for common functionality and type grouping.
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class LitType(metaclass=abc.ABCMeta):
  """Base class for LIT Types."""
  required: bool = True  # for input fields, mark if required by the model.
  annotated: bool = False  # If this type is created from an Annotator.
  # TODO(lit-dev): Add defaults for all LitTypes
  default = None  # an optional default value for a given type.

  def is_compatible(self, other):
    """Check equality, ignoring some fields."""
    # We allow this class to be a subclass of the other.
    if not isinstance(self, type(other)):
      return False
    d1 = attr.asdict(self)
    d1.pop("required", None)
    d2 = attr.asdict(other)
    d2.pop("required", None)
    return d1 == d2

  def to_json(self) -> JsonDict:
    """Used by serialize.py."""
    d = attr.asdict(self)
    d["__class__"] = "LitType"
    d["__name__"] = self.__class__.__name__
    # All parent classes, from method resolution order (mro).
    # Use this to check inheritance on the frontend.
    d["__mro__"] = [a.__name__ for a in self.__class__.__mro__]
    return d

  @staticmethod
  def from_json(d: JsonDict):
    """Used by serialize.py."""
    cls = globals()[d.pop("__name__")]  # class by name from this module
    del d["__mro__"]
    return cls(**d)

Spec = Dict[Text, LitType]

# Attributes that should be treated as a reference to other fields.
FIELD_REF_ATTRIBUTES = frozenset(
    {"parent", "align", "align_in", "align_out", "grad_for"})

def _remap_leaf(leaf: LitType, keymap: Dict[str, str]) -> LitType:
  """Remap any field references on a LitType."""
  d = attr.asdict(leaf)  # mutable
  d = {
      k: (keymap.get(v, v) if k in FIELD_REF_ATTRIBUTES else v)
      for k, v in d.items()
  }
  return leaf.__class__(**d)

def remap_spec(spec: Spec, keymap: Dict[str, str]) -> Spec:
  """Rename fields in a spec, with a best-effort to also remap field references."""
  ret = {}
  for k, v in spec.items():
    new_key = keymap.get(k, k)
    new_value = _remap_leaf(v, keymap)
    ret[new_key] = new_value
  return ret

##
# Concrete type clases

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class String(LitType):
  """User-editable text input.

  All automated edits are disabled for this type.

  Mainly used for string inputs that have special formatting, and should only
  be edited manually.
  """
  default: Text = ""

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class TextSegment(LitType):
  """Text input (untokenized), a single string."""
  default: Text = ""

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class ImageBytes(LitType):
  """An image, an encoded base64 ascii string (starts with 'data:image...')."""
  pass

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class GeneratedText(TextSegment):
  """Generated (untokenized) text."""
  # Name of a TextSegment field to evaluate against
  parent: Optional[Text] = None

ScoredTextCandidates = List[Tuple[str, Optional[float]]]

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class GeneratedTextCandidates(TextSegment):
  """Multiple candidates for GeneratedText; values are List[(text, score)]."""
  # Name of a TextSegment field to evaluate against
  parent: Optional[Text] = None

  @staticmethod
  def top_text(value: ScoredTextCandidates) -> str:
    return value[0][0] if len(value) else ""

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class ReferenceTexts(LitType):
  """Multiple candidates for TextSegment; values are List[(text, score)]."""
  pass

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class URL(TextSegment):
  """TextSegment that should be interpreted as a URL."""
  pass

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class SearchQuery(TextSegment):
  """TextSegment that should be interpreted as a search query."""
  pass

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class Tokens(LitType):
  """Tokenized text, as List[str]."""
  default: List[Text] = attr.Factory(list)
  # Name of a TextSegment field from the input
  # TODO(b/167617375): should we use 'align' here?
  parent: Optional[Text] = None
  mask_token: Optional[Text] = None  # optional mask token for input

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class TokenTopKPreds(LitType):
  """Predicted tokens, as from a language model.

  Data should be a List[List[Tuple[str, float]]], where the inner list contains
  (word, probability) in descending order.
  """
  align: Text = None  # name of a Tokens field in the model output
  parent: Optional[Text] = None

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class Scalar(LitType):
  """Scalar value, a single float or int."""
  min_val: float = 0
  max_val: float = 1
  default: float = 0
  step: float = .01

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class RegressionScore(Scalar):
  """Regression score, a single float."""
  # name of a Scalar or RegressionScore field in input
  parent: Optional[Text] = None

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class ReferenceScores(LitType):
  """Score of one or more target sequences, as List[float]."""
  # name of a TextSegment or ReferenceTexts field in the input
  parent: Optional[Text] = None

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class CategoryLabel(LitType):
  """Category or class label, a single string."""
  # Optional vocabulary to specify allowed values.
  # If omitted, any value is accepted.
  vocab: Optional[Sequence[Text]] = None  # label names

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class MulticlassPreds(LitType):
  """Multiclass predicted probabilities, as <float>[num_labels]."""
  # Vocabulary is required here for decoding model output.
  # Usually this will match the vocabulary in the corresponding label field.
  vocab: Sequence[Text]                 # label names
  null_idx: Optional[int] = None        # vocab index of negative (null) label
  parent: Optional[Text] = None         # CategoryLabel field in input
  autosort: Optional[bool] = False      # Enable automatic sorting

  @property
  def num_labels(self):
    return len(self.vocab)

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class SequenceTags(LitType):
  """Sequence tags, aligned to tokens.

  The data should be a list of string labels, one for each token.
  """
  align: Text  # name of Tokens field

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class SpanLabels(LitType):
  """Span labels, a List[dtypes.SpanLabel] aligned to tokens.

  Span labels can cover more than one token, may not cover all tokens in the
  sentence, and may overlap with each other.
  """
  align: Text  # name of Tokens field
  parent: Optional[Text] = None

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class EdgeLabels(LitType):
  """Edge labels, a List[dtypes.EdgeLabel] between pairs of spans.

  This is a general form for structured prediction output; each entry consists
  of (span1, span2, label). See
  https://arxiv.org/abs/1905.06316 (Tenney et al. 2019) and
  https://github.com/nyu-mll/jiant/tree/master/probing#data-format for more
  details.
  """
  align: Text  # name of Tokens field

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class MultiSegmentAnnotations(LitType):
  """Very general type for in-line text annotations, as List[AnnotationCluster].

  This is a more general version of SpanLabel, EdgeLabel, and other annotation
  types, designed to represent annotations that may span multiple segments.

  The basic unit is dtypes.AnnotationCluster, which contains a label, optional
  score, and one or more SpanLabel annotations, each of which points to a
  specific segment from the input.

  TODO(lit-dev): by default, spans are treated as bytes in this context.
  Make this configurable, if some spans need to refer to tokens instead.
  """
  exclusive: bool = False  # if true, treat as candidate list
  background: bool = False  # if true, don't emphasize in visualization

##
# Model internals, for interpretation.

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class Embeddings(LitType):
  """Embeddings or model activations, as fixed-length <float>[emb_dim]."""
  pass

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class Gradients(LitType):
  """Gradients with respect to embeddings."""
  grad_for: Optional[Text] = None  # name of Embeddings field
  # Name of the field in the input that can be used to specify the target class
  # for the gradients.
  grad_target_field_key: Optional[Text] = None

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class TokenEmbeddings(LitType):
  """Per-token embeddings, as <float>[num_tokens, emb_dim]."""
  align: Optional[Text] = None  # name of a Tokens field

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class TokenGradients(LitType):
  """Gradients with respect to per-token inputs, as <float>[num_tokens, emb_dim]."""
  align: Optional[Text] = None  # name of a Tokens field
  grad_for: Optional[Text] = None  # name of TokenEmbeddings field
  # Name of the field in the input that can be used to specify the target class
  # for the gradients.
  grad_target_field_key: Optional[Text] = None

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class ImageGradients(LitType):
  """Gradients with respect to per-pixel inputs, as a multidimensional array."""
  # Name of the field in the input for which the gradients are computed.
  align: Optional[Text] = None
  # Name of the field in the input that can be used to specify the target class
  # for the gradients.
  grad_target_field_key: Optional[Text] = None

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class AttentionHeads(LitType):
  """One or more attention heads, as <float>[num_heads, num_tokens, num_tokens]."""
  # input and output Tokens fields; for self-attention these can be the same
  align_in: Text
  align_out: Text

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class SubwordOffsets(LitType):
  """Offsets to align input tokens to wordpieces or characters, as List[int].

  offsets[i] should be the index of the first wordpiece for input token i.
  """
  align_in: Text  # name of field in data spec
  align_out: Text  # name of field in model output spec

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class SparseMultilabel(LitType):
  """Sparse multi-label represented as a list of strings, as List[str]."""
  vocab: Optional[Sequence[Text]] = None  # label names
  default: Sequence[Text] = []
  # TODO(b/162269499) Migrate non-comma separators to custom type.
  separator: Text = ","  # Used for display purposes.

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class SparseMultilabelPreds(LitType):
  """Sparse multi-label predictions represented as a list of tuples.

  The tuples are of the label and the score. So as a List[(str, float)].
  """
  vocab: Optional[Sequence[Text]] = None  # label names
  parent: Optional[Text] = None
  default: Sequence[Text] = []

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class FieldMatcher(LitType):
  """For matching spec fields.

  The front-end will perform spec matching and fill in the vocab field
  accordingly. UI will materialize this to a dropdown-list.
  Use MultiFieldMatcher when your intent is selecting more than one field in UI.
  """
  spec: Text  # which spec to check, 'dataset', 'input', or 'output'.
  types: Union[Text, Sequence[Text]]  # types of LitType to match in the spec.
  vocab: Optional[Sequence[Text]] = None  # names matched from the spec.

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class MultiFieldMatcher(LitType):
  """For matching spec fields.

  The front-end will perform spec matching and fill in the vocab field
  accordingly. UI will materialize this to multiple checkboxes. Use this when
  the user needs to pick more than one field in UI.
  """
  spec: Text  # which spec to check, 'dataset', 'input', or 'output'.
  types: Union[Text, Sequence[Text]]  # types of LitType to match in the spec.
  vocab: Optional[Sequence[Text]] = None  # names matched from the spec.
  default: Sequence[Text] = []  # default names of selected items.
  select_all: bool = False  # Select all by default (overriddes default).

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class TokenSalience(LitType):
  """Metadata about a returned token salience map, returned as dtypes.TokenSalience."""
  autorun: bool = False  # If the saliency technique is automatically run.
  signed: bool  # If the returned values are signed.

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class FeatureSalience(LitType):
  """Metadata about a returned feature salience map, returned as dtypes.FeatureSalience."""
  autorun: bool = True  # If the saliency technique is automatically run.
  signed: bool  # If the returned values are signed.

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class ImageSalience(LitType):
  """Metadata about a returned image saliency.

  The data is returned as an image in the base64 URL encoded format, e.g.,
  data:image/jpg;base64,w4J3k1Bfa...
  """
  autorun: bool = False  # If the saliency technique is automatically run.

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class SequenceSalience(LitType):
  """Metadata about a returned sequence salience map, returned as dtypes.SequenceSalienceMap."""
  autorun: bool = False  # If the saliency technique is automatically run.
  signed: bool  # If the returned values are signed.

@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class Boolean(LitType):
  """Boolean value."""
  default: bool = False