text:
package org.robolectric.shadows;

import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.TestRunners;

import static org.junit.Assert.assertEquals;

@RunWith(TestRunners.MultiApiWithDefaults.class)
public class ShadowResultReceiverTest {
  @Test
  public void callingSend_shouldCallOverridenOnReceiveResultWithTheSameArguments() throws Exception {
    TestResultReceiver testResultReceiver = new TestResultReceiver(null);
    Bundle bundle = new Bundle();

    testResultReceiver.send(5, bundle);
    assertEquals(5, testResultReceiver.resultCode);
    assertEquals(bundle, testResultReceiver.resultData);
  }

  static class TestResultReceiver extends ResultReceiver {
    int resultCode;
    Bundle resultData;

    public TestResultReceiver(Handler handler) {
      super(handler);
    }

    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
      this.resultCode = resultCode;
      this.resultData = resultData;
    }
  }
}


text:
package gads

import (
	"encoding/base64"
	"encoding/xml"
)

type MediaService struct {
	Auth
}

func NewMediaService(auth *Auth) *MediaService {
	return &MediaService{Auth: *auth}
}

type Dimensions struct {
	Name   string `xml:"key,omitempty"` // "FULL", "SHRUNKEN", "PREVIEW", "VIDEO_THUMBNAIL"
	Width  int64  `xml:"value>width,omitempty"`
	Height int64  `xml:"value>height,omitempty"`
}

type ImageUrl struct {
	Key   string `xml:"key,omitempty"`
	Value string `xml:"value,omitempty"`
}

// Media represents an audio, image or video file.
type Media struct {
	Type         string       `xml:"http://www.w3.org/2001/XMLSchema-instance type,attr"`
	Id           int64        `xml:"mediaId,omitempty"`
	MediaType    string       `xml:"type"` // "AUDIO", "DYNAMIC_IMAGE", "ICON", "IMAGE", "STANDARD_ICON", "VIDEO"
	ReferenceId  int64        `xml:"referenceId,omitempty"`
	Dimensions   []Dimensions `xml:"dimensions"`
	Urls         []ImageUrl   `xml:"urls"`
	MimeType     string       `xml:"mimeType,omitempty"` // "IMAGE_JPEG", "IMAGE_GIF", "IMAGE_PNG", "FLASH", "TEXT_HTML", "PDF", "MSWORD", "MSEXCEL", "RTF", "AUDIO_WAV", "AUDIO_MP3"
	SourceUrl    string       `xml:"sourceUrl,omitempty"`
	Name         string       `xml:"name"`
	FileSize     int64        `xml:"fileSize,omitempty"`   // File size in bytes
	CreationTime string       `xml:"createTime,omitempty"` // format is YYYY-MM-DD HH:MM:SS+TZ

	// Audio / Video
	DurationMillis   int64  `xml:"durationMillis,omitempty"`
	StreamingUrl     string `xml:"streamingUrl,omitempty"`
	ReadyToPlayOnWeb bool   `xml:"readyToPlayOnTheWeb,omitempty"`

	// Image
	Data string `xml:"data,omitempty"` // base64Binary encoded raw image data

	// Video
	IndustryStandardCommercialIdentifier string `xml:"industryStandardCommercialIdentifier,omitempty"`
	AdvertisingId                        string `xml:"advertisingId,omitempty"`
	YouTubeVideoIdString                 string `xml:"youTubeVideoIdString,omitempty"`
}

func NewAudio(name, mediaType string) (image Media) {
	return Media{
		Name:      name,
		Type:      "Audio",
		MediaType: mediaType,
	}
}

func NewImage(name, mediaType string, data []byte) (image Media) {
	imageData := base64.StdEncoding.EncodeToString(data)
	return Media{
		Name:      name,
		Type:      "Image",
		MediaType: mediaType,
		Data:      imageData,
	}
}

func NewImageWithoutDataEncoding(name, mediaType string, dataBase64 string) (image Media) {
	return Media{
		Name:      name,
		Type:      "Image",
		MediaType: mediaType,
		Data:      dataBase64,
	}
}

func NewVideo(mediaType string) (image Media) {
	return Media{
		Type:      "Video",
		MediaType: mediaType,
	}
}

func (s *MediaService) Get(selector Selector) (medias []Media, totalCount int64, err error) {
	selector.XMLName = xml.Name{"", "serviceSelector"}
	respBody, err := s.Auth.request(
		mediaServiceUrl,
		"get",
		struct {
			XMLName xml.Name
			Sel     Selector
		}{
			XMLName: xml.Name{
				Space: baseUrl,
				Local: "get",
			},
			Sel: selector,
		},
	)
	if err != nil {
		return medias, totalCount, err
	}
	getResp := struct {
		Size   int64   `xml:"rval>totalNumEntries"`
		Medias []Media `xml:"rval>entries"`
	}{}
	err = xml.Unmarshal([]byte(respBody), &getResp)
	if err != nil {
		return medias, totalCount, err
	}
	return getResp.Medias, getResp.Size, err
}

func (s *MediaService) Query(query string) (medias []Media, totalCount int64, err error) {
	return medias, totalCount, ERROR_NOT_YET_IMPLEMENTED
}

func (s *MediaService) Upload(medias []Media) (uploadedMedias []Media, err error) {
	upload := struct {
		XMLName xml.Name
		Medias  []Media `xml:"media"`
	}{
		XMLName: xml.Name{
			Space: baseUrl,
			Local: "upload",
		},
		Medias: medias,
	}
	respBody, err := s.Auth.request(mediaServiceUrl, "upload", upload)
	if err != nil {
		return uploadedMedias, err
	}
	uploadResp := struct {
		Medias []Media `xml:"rval"`
	}{}
	err = xml.Unmarshal([]byte(respBody), &uploadResp)
	if err != nil {
		return uploadedMedias, err
	}
	return uploadResp.Medias, err
}

redpajama_set_name: RedPajamaGithub


text:
package org.elasticsearch.index;

import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.MetadataCreateDataStreamService;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.mapper.DataStreamTimestampFieldMapper;
import org.elasticsearch.index.mapper.DateFieldMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.MappingLookup;
import org.elasticsearch.index.mapper.MetadataFieldMapper;
import org.elasticsearch.index.mapper.RoutingFieldMapper;
import org.elasticsearch.index.mapper.TimeSeriesIdFieldMapper;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toSet;

/**
 * "Mode" that controls which behaviors and settings an index supports.
 * <p>
 * For the most part this class concentrates on validating settings and
 * mappings. Most different behavior is controlled by forcing settings
 * to be set or not set and by enabling extra fields in the mapping.
 */
public enum IndexMode {
    STANDARD {
        @Override
        void validateWithOtherSettings(Map<Setting<?>, Object> settings) {
            settingRequiresTimeSeries(settings, IndexMetadata.INDEX_ROUTING_PATH);
            settingRequiresTimeSeries(settings, IndexSettings.TIME_SERIES_START_TIME);
            settingRequiresTimeSeries(settings, IndexSettings.TIME_SERIES_END_TIME);
        }

        private void settingRequiresTimeSeries(Map<Setting<?>, Object> settings, Setting<?> setting) {
            if (false == Objects.equals(setting.getDefault(Settings.EMPTY), settings.get(setting))) {
                throw new IllegalArgumentException("[" + setting.getKey() + "] requires [" + IndexSettings.MODE.getKey() + "=time_series]");
            }
        }

        @Override
        public void validateMapping(MappingLookup lookup) {};

        @Override
        public void validateAlias(@Nullable String indexRouting, @Nullable String searchRouting) {}

        @Override
        public void validateTimestampFieldMapping(boolean isDataStream, MappingLookup mappingLookup) throws IOException {
            if (isDataStream) {
                MetadataCreateDataStreamService.validateTimestampFieldMapping(mappingLookup);
            }
        }

        @Override
        public Map<String, Object> getDefaultMapping() {
            return Collections.emptyMap();
        }

        @Override
        public MetadataFieldMapper buildTimeSeriesIdFieldMapper() {
            // non time-series indices must not have a TimeSeriesIdFieldMapper
            return null;
        }
    },
    TIME_SERIES {
        @Override
        void validateWithOtherSettings(Map<Setting<?>, Object> settings) {
            if (settings.get(IndexMetadata.INDEX_ROUTING_PARTITION_SIZE_SETTING) != Integer.valueOf(1)) {
                throw new IllegalArgumentException(error(IndexMetadata.INDEX_ROUTING_PARTITION_SIZE_SETTING));
            }
            for (Setting<?> unsupported : TIME_SERIES_UNSUPPORTED) {
                if (false == Objects.equals(unsupported.getDefault(Settings.EMPTY), settings.get(unsupported))) {
                    throw new IllegalArgumentException(error(unsupported));
                }
            }
            settingRequiresTimeSeries(settings, IndexMetadata.INDEX_ROUTING_PATH);
            settingRequiresTimeSeries(settings, IndexSettings.TIME_SERIES_START_TIME);
            settingRequiresTimeSeries(settings, IndexSettings.TIME_SERIES_END_TIME);
        }

        private void settingRequiresTimeSeries(Map<Setting<?>, Object> settings, Setting<?> setting) {
            if (Objects.equals(setting.getDefault(Settings.EMPTY), settings.get(setting))) {
                throw new IllegalArgumentException("[" + IndexSettings.MODE.getKey() + "=time_series] requires [" + setting.getKey() + "]");
            }
        }

        private String error(Setting<?> unsupported) {
            return tsdbMode() + " is incompatible with [" + unsupported.getKey() + "]";
        }

        @Override
        public void validateMapping(MappingLookup lookup) {
            if (((RoutingFieldMapper) lookup.getMapper(RoutingFieldMapper.NAME)).required()) {
                throw new IllegalArgumentException(routingRequiredBad());
            }
        }

        @Override
        public void validateAlias(@Nullable String indexRouting, @Nullable String searchRouting) {
            if (indexRouting != null || searchRouting != null) {
                throw new IllegalArgumentException(routingRequiredBad());
            }
        }

        @Override
        public void validateTimestampFieldMapping(boolean isDataStream, MappingLookup mappingLookup) throws IOException {
            MetadataCreateDataStreamService.validateTimestampFieldMapping(mappingLookup);
        }

        @Override
        public Map<String, Object> getDefaultMapping() {
            return DEFAULT_TIME_SERIES_TIMESTAMP_MAPPING;
        }

        private String routingRequiredBad() {
            return "routing is forbidden on CRUD operations that target indices in " + tsdbMode();
        }

        private String tsdbMode() {
            return "[" + IndexSettings.MODE.getKey() + "=time_series]";
        }

        @Override
        public MetadataFieldMapper buildTimeSeriesIdFieldMapper() {
            return TimeSeriesIdFieldMapper.INSTANCE;
        }
    };

    public static final Map<String, Object> DEFAULT_TIME_SERIES_TIMESTAMP_MAPPING = Map.of(
        MapperService.SINGLE_MAPPING_NAME,
        Map.of(
            DataStreamTimestampFieldMapper.NAME,
            Map.of("enabled", true),
            "properties",
            Map.of(DataStreamTimestampFieldMapper.DEFAULT_PATH, Map.of("type", DateFieldMapper.CONTENT_TYPE))
        )
    );

    private static final List<Setting<?>> TIME_SERIES_UNSUPPORTED = List.of(
        IndexSortConfig.INDEX_SORT_FIELD_SETTING,
        IndexSortConfig.INDEX_SORT_ORDER_SETTING,
        IndexSortConfig.INDEX_SORT_MODE_SETTING,
        IndexSortConfig.INDEX_SORT_MISSING_SETTING
    );

    static final List<Setting<?>> VALIDATE_WITH_SETTINGS = List.copyOf(
        Stream.concat(
            Stream.of(
                IndexMetadata.INDEX_ROUTING_PARTITION_SIZE_SETTING,
                IndexMetadata.INDEX_ROUTING_PATH,
                IndexSettings.TIME_SERIES_START_TIME,
                IndexSettings.TIME_SERIES_END_TIME
            ),
            TIME_SERIES_UNSUPPORTED.stream()
        ).collect(toSet())
    );

    abstract void validateWithOtherSettings(Map<Setting<?>, Object> settings);

    /**
     * Validate the mapping for this index.
     */
    public abstract void validateMapping(MappingLookup lookup);

    /**
     * Validate aliases targeting this index.
     */
    public abstract void validateAlias(@Nullable String indexRouting, @Nullable String searchRouting);

    /**
     * validate timestamp mapping for this index.
     */
    public abstract void validateTimestampFieldMapping(boolean isDataStream, MappingLookup mappingLookup) throws IOException;

    /**
     * get default mapping for this index.
     * @return
     */
    public abstract Map<String, Object> getDefaultMapping();

    /**
     * Return an instance of the {@link TimeSeriesIdFieldMapper} that generates
     * the _tsid field. The field mapper will be added to the list of the metadata
     * field mappers for the index.
     */
    public abstract MetadataFieldMapper buildTimeSeriesIdFieldMapper();
}


text:
<?php

namespace Brasa\AfiliacionBundle\Repository;

/**
 * AfiFacturaDetalleCursoRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class AfiPagoCursoDetalleRepository extends \Doctrine\ORM\EntityRepository
{
    public function listaDql($codigoPagoCurso) {
        $em = $this->getEntityManager();
        $dql   = "SELECT pcd FROM BrasaAfiliacionBundle:AfiPagoCursoDetalle pcd WHERE pcd.codigoPagoCursoDetallePk <> 0 AND pcd.codigoPagoCursoFk = " . $codigoPagoCurso;
        $dql .= " ORDER BY pcd.codigoPagoCursoDetallePk";
        return $dql;
    }

    public function eliminar($arrSeleccionados) {
        $em = $this->getEntityManager();
        if(count($arrSeleccionados) > 0) {
            foreach ($arrSeleccionados AS $codigo) {
                $ar = $em->getRepository('BrasaAfiliacionBundle:AfiPagoCursoDetalle')->find($codigo);
                $arCursoDetalle = $em->getRepository('BrasaAfiliacionBundle:AfiCursoDetalle')->find($ar->getCodigoCursoDetalleFk());
                $arCursoDetalle->setEstadoPagado(0);
                $em->persist($arCursoDetalle);
                $em->remove($ar);
            }
            $em->flush();
        }
    }
}


text:
class CQChartsSymbolSet;
class CQCharts;

class CQChartsSymbolSetMgr : public QObject {
  Q_OBJECT

 public:
  explicit CQChartsSymbolSetMgr(CQCharts *charts);

 ~CQChartsSymbolSetMgr();

  CQCharts *charts() const { return charts_; }

  //! add symbol set
  void addSymbolSet(CQChartsSymbolSet *symbolSet);

  //! has symbol set of name
  bool hasSymbolSet(const QString &name) const;

  //! get symbol set by name
  CQChartsSymbolSet *symbolSet(const QString &name) const;

  //! get nth symbol set
  CQChartsSymbolSet *symbolSet(int i) const;

  int numSymbolSets() const;

  QStringList symbolSetNames() const;

 private:
  using SymbolSets      = std::vector<CQChartsSymbolSet *>;
  using NamedSymbolSets = std::map<QString, CQChartsSymbolSet *>;

  CQCharts*       charts_ { nullptr };
  SymbolSets      symbolSets_;
  NamedSymbolSets namedSymbolSets_;
};

//---

class CQChartsSymbolSet {
 public:
  struct SymbolData {
    CQChartsSymbol symbol;

    SymbolData() = default;

    explicit SymbolData(const CQChartsSymbol &symbol) :
     symbol(symbol) {
    }
  };

 public:
  CQChartsSymbolSet() = default;

  explicit CQChartsSymbolSet(const QString &name);

  //! get name
  const QString &name() const { return name_; }

  //! add symbol
  CQChartsSymbol addSymbol(const CQChartsSymbol &symbol);

  int numSymbols() const;

  SymbolData interpI(int i) const;
  SymbolData interpI(int i, int imin, int imax) const;

  static int interpIRange(int i, int imin, int imax);

  CQChartsSymbol symbol(int i) const;

  //! get nth symbol
  const SymbolData &symbolData(int i) const;

  QStringList symbolNames() const;

  //---

  bool moveUp  (int i);
  bool moveDown(int i);

 private:
  using Symbols = std::vector<SymbolData>;

  QString name_;
  Symbols symbols_;
};

#endif


text:
==================
== Introduction ==
==================

I found this project in http://ngmodules.org/. So I catch this project for use it in my private project. However in the previous version, it contains a some problems, mainly as Responsive and Linkage. That this why, I change this project for separate Functionality from Interafce.

For instance, In new version, you can use this Table with another CSS Frameworks, like Bootstrap, Material Design and So on. Besides that you can use various ways to activate responsiveness, like Footable, JQueryMobile or Bootstrap. In this example I user Footable but you are free to use another way without lose thes functionalities.

It's no longer important and a fucntions for Paginations, it's may work in server or local mode. In local mode, you don't need implement aditional code. But in server code, you need add you API Service call to return data.

================
== How To Use ==
================

1. Add the required references.
	1.1 AngularJS

		```
		<!-- Latest compiled and minified JavaScript from AngularJS -->
		<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
		<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>
		```

	1.2 JQuery

		```
		<!-- Latest compiled and minified JavaScript from JQuery -->
		<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
		```

	1.3 MobileNgTable

		```
		<!-- Latest compiled and minified JavaScript from MobileNgTable -->
		<script src="https://rawgit.com/tiagocarmosantos/mobileNgTable/master/lib/mobileNgTable.js"></script>
		```

2. Add the Optional references.
	2.1 JQuery

		```
		<!-- Latest compiled and minified CSS from JQuery -->
		<link rel="stylesheet" href="https://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
		```

	2.2 BootStrap

		```
		<!-- Latest compiled and minified CSS from BootStrap -->
  		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
		```

	2.3 FooTable

		```
		<!-- Latest compiled and minified JavaScript from FooTable -->
		<script src="https://rawgit.com/tiagocarmosantos/mobileNgTable/master/sample/footable/footable.js" ></script>
		<script src="https://rawgit.com/tiagocarmosantos/mobileNgTable/master/sample/footable/footable.sort.js" ></script>
		<script src="https://rawgit.com/tiagocarmosantos/mobileNgTable/master/sample/footable/footable.filter.js" ></script>
		```

3. Add this code to your Document or Code HTML.

	```
	<mobile-ng-table ng-if="contatos.length > 0" load-Repeat="contatosNgTable"></mobile-ng-table>
	```

4. Add this code to your AngularJS Controller file.

    ```
    $scope.contatosNgTable = {
        data: 'contatos',
        class: 'ui-responsive table table-hover table-striped table-bordered table-condensed table-list table-stroke footable',
        search: 'pesquisarContato',
        select: 'selecionado',
        visualisable: 'visualizavel',
        editable: 'editavel',
        deletable: 'deletavel',
        columnDefs: [{
            field: 'serial',
            displayName: 'Serial',
            hide: 'phone,tablet',
            sort: true,
            width: "10%"
        }, {
            field: 'nome',
            displayName: 'Nome',
            sort: true,
            width: "50%"
        }, {
            field: 'telefone',
            displayName: 'Telefone',
            hide: 'phone,tablet',
            sort: true,
            width: '20%'
        }, {
            field: 'data',
            displayName: 'Data',
            sort: true,
            width: '20%',
            filter: 'date'
        }],
        pager: { enable: true, startPage: 1, limitPerPage: 2, sizes: [2, 5, 8, 10], type:'local', class: 'pager' },
        rowSelectClick: function (e, row) {
	    console.log('O Item foi Selecionado!');
            return true;
        },
        rowDeleteClick: function (e, row) {
            event.preventDefault();
            console.log('O Item foi Deletado!');
            return false;
        },
        rowEditClick: function (e, row) {
            event.preventDefault();
            console.log('O Item foi Editado!');
            return false;
        },
        rowViewClick: function (e, row) {
            event.preventDefault();
            console.log('O Item foi Visualizado!');
            return false;
        },
        pagerReloadClick: function (e, pagerConfig) {
        	console.log('A Tabela foi Atualizada!');
        }
    };
    ```

4.1 In above code, where contains "data: 'contatos'" you need use for 'contatos' your array objects. Like this.

	```
	$scope.contatos = [{ "id": 1, "nome": "Jean", "telefone": "98621-4487", "data": "2017-02-11T02:15:12.356Z",
      				"operadora": { "nome": "Oi", "codigo": 14, "categoria": "Celular", "preco": 2}, "cor": "blue",
				"selecionado": false, "visualizavel": true, "editavel": true, "deletavel": true }];

	Atention: In array object I create four important attributes, them are use by the component to relationship with your respective functions: Select, View, Edit and Delete. But any this is required.
		[{ "selecionado": false, "visualizavel": true, "editavel": true, "deletavel": true }]
	```

5. Finally, add this code to End your AngularJS Controller file.

    ```
    (function initController() {
        //$timeout(function () { $('table').trigger('footable_redraw'); }, 0);
        $timeout(function () { $('.footable').footable(); }, 0);
    })();
    ```

=====================
== API Description ==
=====================

1. Attributes.

	1.1 Data - A required field, use it to link your JSON array objects.
	1.2 Class - A optional field, use it to define your Table CSS.
	1.3 Search - A optional field, use it to link your variable scope from search field like TextField.
	1.4 Select - A optional field, use it to link your JSON object field that define selectable item.
	1.5 Visualisable - A optional field, use it to link your JSON object field that define visualisable item.
	1.6 Editable - A optional field, use it to link your JSON object field that define editable item.
	1.7 Deletable - A optional field, use it to link your JSON object field that define deletable item.
	1.8 ColumnDefs - A required field, use it to define your columns definitions.
    		1.8.1 Field - A required field, use it to link your JSON object field that define value item.
    		1.8.2 DisplayName - A required field, use it to define your header column name.
    		1.8.3 Width - A required field, use it to define your column width.
    		1.8.4 Hide - A required field, use it to define your column hide from FooTable.
    		1.8.5 Filter - A required field, use it to define your AngularJS filter column.
		1.8.6 Sort - A required field, use it to define your order column from FooTable.
	1.9 Pager - A optional field, use it to define your pager toolbar.
		1.9.1 Enable - A required field, use it to display or not your Pager Toolbar.
		1.9.2 StartPage - A required field, use it to define your start page.
		1.9.3 LimitPerPage - A required field, use it to define your amount itens per page.
		1.9.4 Sizes - A required field, use it to define your options LimitPerPage.
		1.9.5 Type - A required field, use it to define your type pagination.
			a - 'local' you don't need nothing else.
			b - 'server' you need define the method pagerReloadClick. See in Item 2.5.
		1.9.6 Class - A required field, use it to define your Pager Toolbar CSS.

2. Methods

	2.1 rowSelectClick: function (e, row) - A optional method, use it to define a behavior to select item event click.
	2.2 rowDeleteClick: function (e, row) - A optional method, use it to define a behavior to delete item event click.
	2.3 rowEditClick: function (e, row) - A optional method, use it to define a behavior to edit item event click.
	2.4 rowViewClick: function (e, row) - A optional method, use it to define a behavior to view item event click.
	2.5 pagerReloadClick: function (e, row) - A optional method, use it to define a behavior to pager's itens event click, if your define pager type like 'server'. Otherwise don't need define it.

3. Samples - Live Demos

	3.1 Sample 01 - FooTable and BootStrap
		https://rawgit.com/tiagocarmosantos/mobileNgTable/master/sample/index.html
	3.2 Sample 02 - JQuery Mobile and BootStrap
		https://rawgit.com/tiagocarmosantos/mobileNgTable/master/sample/index02.html


text:
import { Button as AriakitButton } from "ariakit/button";
import type { ButtonProps as AriakitButtonProps } from "ariakit/button";
import { forwardRef, cloneElement, Children, memo } from "react";
import { ChevronDownIcon } from "@heroicons/react/24/solid";

export type ButtonColor = "primary" | "neutral";
export type ButtonVariant = "contained" | "outline";

export interface ButtonProps
  extends Omit<AriakitButtonProps<"button">, "className"> {
  color?: ButtonColor;
  variant?: ButtonVariant;
}

const variantClassNames: Record<ButtonVariant, Record<ButtonColor, string>> = {
  contained: {
    primary:
      "color-white border-transparent bg-primary-600 hover:bg-primary-700 active:bg-primary-800 aria-expanded:bg-primary-800",
    neutral:
      "color-white border-transparent bg-neutral-600 hover:bg-neutral-700 active:bg-neutral-800 aria-expanded:bg-neutral-800",
  },
  outline: {
    primary: "color-primary-300 border-primary-300 bg-transparent",
    neutral: "color-neutral-300 border-neutral-300 bg-transparent",
  },
};

export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
  ({ color = "primary", variant = "contained", children, ...props }, ref) => {
    const colorClassNames = variantClassNames[variant];
    if (!colorClassNames) {
      throw new Error(`Invalid variant: ${variant}`);
    }
    const variantClassName = colorClassNames[color];
    if (!variantClassName) {
      throw new Error(`Invalid color: ${color}`);
    }
    return (
      <AriakitButton
        ref={ref}
        as="button"
        className={`${variantClassName} align-center inline-flex whitespace-nowrap rounded-lg border py-2 px-3 font-sans text-sm font-medium leading-none transition disabled:opacity-70 [&:is(button)]:cursor-default`}
        {...props}
      >
        {children}
      </AriakitButton>
    );
  }
);

export interface ButtonIconProps {
  children: React.ReactElement;
}

export const ButtonIcon = ({ children }: ButtonIconProps) => {
  return cloneElement(Children.only(children), {
    className: "h-[1em] w-[1em] mr-2",
  });
};

export const ButtonArrow = memo(() => {
  return <ChevronDownIcon className="ml-2 mr-[-4px] h-[1em] w-[1em]" />;
});


text:
.host {
	BACKGROUND-POSITION: 0px -25px; FONT-SIZE: 80%; BACKGROUND-IMAGE: url(/gant/images/yellowGreenBack.png); BACKGROUND-REPEAT: repeat-x; FONT-FAMILY: verdana, helvetica, arial, sans-serif; HEIGHT: 75px; BACKGROUND-COLOR: #ffffaa; TEXT-ALIGN: right
}
.canoo {
	RIGHT: 20px; POSITION: absolute; TOP: 4px
}
.logo {
    LEFT: 20px; POSITION: relative; TOP: -84px
}

.white {
	COLOR: #ffffff
}
.navigation {
    padding-top: 20px;
    padding-left: 10px;
    width: 220px;
	BACKGROUND-COLOR: #ffffaa
}
.index {
	BACKGROUND-COLOR: #ffffff
}
.index-passed {
	COLOR: #004400
}
.index-failed {
	FONT-WEIGHT: bold; COLOR: #ff0000
}
.index-header {
	FONT-WEIGHT: bold
}
.link {
	FONT-SIZE: 10pt; COLOR: #336699; FONT-FAMILY: arial,helvetica,sans-serif; TEXT-DECORATION: none
}
.tab-table {
	MARGIN: 0em 0em 0.5em
}
.tabs {
	PADDING-RIGHT: 2em; PADDING-LEFT: 2em; FONT-WEIGHT: bold; FONT-SIZE: 8pt; PADDING-BOTTOM: 0em; COLOR: #000000; PADDING-TOP: 0em; FONT-FAMILY: arial,helvetica,sans-serif; BACKGROUND-COLOR: #ccccff
}
.tabs-link {
	COLOR: #000000; TEXT-DECORATION: none
}
.tabs-link:visited {
	COLOR: #000000; TEXT-DECORATION: none
}
.tabs-selected {
	PADDING-RIGHT: 2em; PADDING-LEFT: 2em; FONT-WEIGHT: bold; FONT-SIZE: 8pt; PADDING-BOTTOM: 0em; COLOR: #000000; PADDING-TOP: 0em; FONT-FAMILY: arial,helvetica,sans-serif
}
.tabs-selected {
	BORDER-RIGHT: inset; BORDER-TOP: inset; BORDER-LEFT: inset; BORDER-BOTTOM: inset
}
.header-title {
	FONT-WEIGHT: bold; FONT-SIZE: 12pt; COLOR: #336699; FONT-FAMILY: arial,helvetica,sans-serif
}
.header-label {
	FONT-WEIGHT: bold
}
.header-data {
	FONT-SIZE: 10pt; COLOR: #336699; FONT-FAMILY: arial,helvetica,sans-serif
}
.modifications-data {
	FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: arial,helvetica,sans-serif
}
.modifications-sectionheader {
	FONT-SIZE: 10pt; COLOR: #336699; FONT-FAMILY: arial,helvetica,sans-serif; BACKGROUND-COLOR: #ffffaa
}
.modifications-oddrow {
	BACKGROUND-COLOR: #ccccff
}
.modifications-evenrow {
	BACKGROUND-COLOR: #ffffcc
}
.changelists-oddrow {
	BACKGROUND-COLOR: #ccccff
}
.changelists-evenrow {
	BACKGROUND-COLOR: #ffffcc
}
.changelists-file-spacer {
	BACKGROUND-COLOR: #ffffff
}
.changelists-file-evenrow {
	BACKGROUND-COLOR: #eeeeee
}
.changelists-file-oddrow {
	BACKGROUND-COLOR: #ffffee
}
.changelists-file-header {
	FONT-SIZE: 8pt; COLOR: #ffffff; FONT-FAMILY: arial,helvetica,sans-serif; BACKGROUND-COLOR: #666666
}
.compile-data {
	FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: arial,helvetica,sans-serif
}
.compile-error-data {
	FONT-SIZE: 8pt; COLOR: #ff0000; FONT-FAMILY: arial,helvetica,sans-serif
}
.compile-warn-data {
	FONT-SIZE: 8pt; COLOR: #cc9900; FONT-FAMILY: arial,helvetica,sans-serif
}
.compile-sectionheader {
	FONT-SIZE: 10pt; COLOR: #336699; FONT-FAMILY: arial,helvetica,sans-serif; BACKGROUND-COLOR: #ffffaa
}
.distributables-data {
	FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: arial,helvetica,sans-serif
}
.distributables-sectionheader {
	FONT-SIZE: 10pt; COLOR: #336699; FONT-FAMILY: arial,helvetica,sans-serif; BACKGROUND-COLOR: #ffffaa
}
.distributables-oddrow {
	BACKGROUND-COLOR: #ccccff
}
.unittests-sectionheader {
	FONT-SIZE: 10pt; COLOR: #336699; FONT-FAMILY: arial,helvetica,sans-serif; BACKGROUND-COLOR: #ffffaa
}
.unittests-oddrow {
	BACKGROUND-COLOR: #ccccff
}
.unittests-data {
	FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: arial,helvetica,sans-serif
}
.unittests-error {
	FONT-SIZE: 8pt; COLOR: #901090; FONT-FAMILY: arial,helvetica,sans-serif
}
.unittests-failure {
	FONT-SIZE: 8pt; COLOR: #ff0000; FONT-FAMILY: arial,helvetica,sans-serif
}
.checkstyle-oddrow {
	BACKGROUND-COLOR: #ccccff
}
.checkstyle-data {
	FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: arial,helvetica,sans-serif
}
.checkstyle-sectionheader {
	FONT-SIZE: 10pt; COLOR: #336699; FONT-FAMILY: arial,helvetica,sans-serif; BACKGROUND-COLOR: #ffffaa
}


text:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="1.0" xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd">

  <named-query name="get.cg.member.by.name">
    <query><![CDATA[SELECT s FROM CGMemberEntity s WHERE name = :name]]></query>
  </named-query>

  <!-- Uncomment the following if the database used is H2 -->
  <!--
    <named-native-query name="get.all.ids.of.payed.bills">
      <query><![CDATA[SELECT id FROM Bill WHERE payed = true]]></query>
    </named-native-query>
 -->
  <!-- Uncomment the following if the database used is Oracle 11g -->
  <!--
    <named-native-query name="get.all.ids.of.payed.bills">
      <query><![CDATA[SELECT id FROM Bill WHERE payed = 1]]></query>
    </named-native-query>
   -->

  <!-- UnComment the following if database used is MS SQL Server 2008 -->
    <!--
    <named-native-query name="get.all.ids.of.payed.bills">
      <query><![CDATA[SELECT id FROM Bill WHERE payed = 1]]></query>
    </named-native-query>
    -->

</entity-mappings>


text:
package com.shirkit.countcraft.network.server;

import com.shirkit.countcraft.CountCraft;
import com.shirkit.countcraft.api.ICounterContainer;
import com.shirkit.countcraft.api.ISideAware;
import com.shirkit.countcraft.api.count.IComplexCounter;
import com.shirkit.countcraft.api.count.ICounter;
import com.shirkit.countcraft.api.integration.ICounterFinder;
import com.shirkit.countcraft.api.side.SideController;

import cpw.mods.fml.common.network.ByteBufUtils;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;

public class UpdateServerMessage implements IMessage {

	public static class Handler extends AbstractServerMessageHandler<UpdateServerMessage> {
		@Override
		public IMessage handleServerMessage(EntityPlayer player, UpdateServerMessage message, MessageContext ctx) {
			TileEntity entity = player.worldObj.getTileEntity(message.x, message.y, message.z);

			ICounterContainer counter = null;
			if (entity instanceof ICounterContainer)
				counter = (ICounterContainer) entity;
			else {
				for (ICounterFinder listener : CountCraft.instance.finders) {
					ICounterContainer te = listener.getCounterContainerFrom(entity);
					if (te != null) {
						counter = te;
						break;
					}
				}
			}

			if (counter != null && counter.getCounter() != null) {
				if (message.data.hasKey(ICounter.ACTIVE_TAG))
					counter.getCounter().setActive(message.data.getBoolean(ICounter.ACTIVE_TAG));

				if (message.data.hasKey(SideController.SIDES_TAG)) {
					if (entity instanceof ISideAware) {
						ISideAware iSideAware = (ISideAware) entity;
						iSideAware.getSideController().readFromNBT(message.data);
					}
				}

				if (message.data.hasKey(IComplexCounter.COMPLEX_TAG)) {
					if (counter.getCounter() instanceof IComplexCounter) {
						IComplexCounter counter2 = (IComplexCounter) counter.getCounter();
						counter2.setComplex(message.data.getBoolean(IComplexCounter.COMPLEX_TAG));
					}
				}

				player.worldObj.notifyBlocksOfNeighborChange(message.x, message.y, message.z, player.worldObj.getBlock(message.x, message.y, message.z));
			}
			return null;
		}
	}
	private NBTTagCompound data;

	private int x, y, z;

	public UpdateServerMessage() {
	}

	public UpdateServerMessage(int x, int y, int z, NBTTagCompound nbt) {
		this.x = x;
		this.y = y;
		this.z = z;
		this.data = nbt;
	}

	@Override
	public void fromBytes(ByteBuf buffer) {
		this.x = buffer.readInt();
		this.y = buffer.readInt();
		this.z = buffer.readInt();

		data = ByteBufUtils.readTag(buffer);
	}

	@Override
	public void toBytes(ByteBuf buffer) {
		buffer.writeInt(this.x);
		buffer.writeInt(this.y);
		buffer.writeInt(this.z);

		ByteBufUtils.writeTag(buffer, data);
	}
}


text:
! { dg-do compile }
! { dg-options "-std=gnu -ffixed-form -Wline-truncation" }
! PR39229 No warning of truncated lines if a continuation line follows
      ! expected: no warning by default (as column 73+ is often used for )
      ! comments in fixed-form source code.
      ! however, with -wline-truncation there shall be a warning.
      implicit none
        call foo([11, 22, 33, 44, 55, 66, 770, 9900, 1100, 1100, 120], 12 warn
     &          , 'hello')
      print *, min(35
     1                                        ,                        25 warn
     2  )
      contains
      subroutine foo(a,n,s)
        integer :: a(*), n, i
        character(len=*) :: s
        do i = 1, n
          print *, s, a(i)
        end do
      end subroutine foo
      end
! { dg-warning "Line truncated" " " { target *-*-* } 8 }
! { dg-warning "Line truncated" " " { target *-*-* } 11 }


text:
"""This module is deprecated. Please use :mod:`airflow.providers.microsoft.azure.sensors.cosmos`."""

import warnings

from airflow.providers.microsoft.azure.sensors.cosmos import AzureCosmosDocumentSensor  # noqa

warnings.warn(
    "This module is deprecated. Please use `airflow.providers.microsoft.azure.sensors.cosmos`.",
    DeprecationWarning,
    stacklevel=2,
)


text:
# frozen_string_literal: true

require "test_helper"
require "mysql2"
require "semian/mysql2"

class TestMysql2 < Minitest::Test
  ERROR_TIMEOUT = 5
  ERROR_THRESHOLD = 1
  SEMIAN_OPTIONS = {
    name: :testing,
    tickets: 1,
    timeout: 0,
    error_threshold: ERROR_THRESHOLD,
    success_threshold: 2,
    error_timeout: ERROR_TIMEOUT,
  }

  def setup
    @proxy = Toxiproxy[:semian_test_mysql]
    Semian.destroy(:mysql_testing)
  end

  def test_semian_identifier
    assert_equal(:mysql_foo, FakeMysql.new(semian: { name: "foo" }).semian_identifier)
    assert_equal(:"mysql_localhost:3306", FakeMysql.new.semian_identifier)
    assert_equal(:"mysql_127.0.0.1:3306", FakeMysql.new(host: "127.0.0.1").semian_identifier)
    assert_equal(:"mysql_example.com:42", FakeMysql.new(host: "example.com", port: 42).semian_identifier)
  end

  def test_semian_can_be_disabled
    resource = Mysql2::Client.new(
      host: SemianConfig["toxiproxy_upstream_host"],
      port: SemianConfig["mysql_toxiproxy_port"],
      semian: false,
    ).semian_resource

    assert_instance_of(Semian::UnprotectedResource, resource)
  end

  def test_connection_errors_open_the_circuit
    @proxy.downstream(:latency, latency: 2200).apply do
      ERROR_THRESHOLD.times do
        assert_raises(::Mysql2::Error) do
          connect_to_mysql!
        end
      end

      assert_raises(::Mysql2::CircuitOpenError) do
        connect_to_mysql!
      end
    end
  end

  def test_query_errors_does_not_open_the_circuit
    client = connect_to_mysql!
    (ERROR_THRESHOLD * 2).times do
      assert_raises(::Mysql2::Error) do
        client.query("ERROR!")
      end
    end
  end

  def test_read_timeout_error_open_the_circuit
    client = connect_to_mysql!

    ERROR_THRESHOLD.times do
      assert_raises(Mysql2::Error) do
        # Should raise an exception like -
        # Mysql2::Error Exception: [mysql_testing] Timeout waiting for a response
        # from the last query. (waited 2 seconds)
        client.query("SELECT sleep(5)")
      end
    end

    assert_raises(Mysql2::CircuitOpenError) do
      client.query("SELECT sleep(5)")
    end

    # After Mysql2::CircuitOpenError check regular queries are working fine.
    query_string = "1 + 1"
    result = nil
    Timecop.travel(ERROR_TIMEOUT + 1) do
      result = client.query("SELECT #{query_string};")
    end

    assert_equal(2, result.first[query_string])
  end

  def test_connect_instrumentation
    notified = false
    subscriber = Semian.subscribe do |event, resource, scope, adapter|
      next unless event == :success

      notified = true

      assert_equal(Semian[:mysql_testing], resource)
      assert_equal(:connection, scope)
      assert_equal(:mysql, adapter)
    end

    connect_to_mysql!

    assert(notified, "No notifications has been emitted")
  ensure
    Semian.unsubscribe(subscriber)
  end

  def test_resource_acquisition_for_connect
    connect_to_mysql!

    Semian[:mysql_testing].acquire do
      error = assert_raises(Mysql2::ResourceBusyError) do
        connect_to_mysql!
      end

      assert_equal(:mysql_testing, error.semian_identifier)
    end
  end

  def test_network_errors_are_tagged_with_the_resource_identifier
    client = connect_to_mysql!
    @proxy.down do
      error = assert_raises(::Mysql2::Error) do
        client.query("SELECT 1 + 1;")
      end

      assert_equal(client.semian_identifier, error.semian_identifier)
    end
  end

  def test_other_mysql_errors_are_not_tagged_with_the_resource_identifier
    client = connect_to_mysql!

    error = assert_raises(Mysql2::Error) do
      client.query("SYNTAX ERROR!")
    end

    assert_nil(error.semian_identifier)
  end

  def test_resource_timeout_on_connect
    @proxy.downstream(:latency, latency: 500).apply do
      background { connect_to_mysql! }

      assert_raises(Mysql2::ResourceBusyError) do
        connect_to_mysql!
      end
    end
  end

  def test_circuit_breaker_on_connect
    @proxy.downstream(:latency, latency: 500).apply do
      background { connect_to_mysql! }

      ERROR_THRESHOLD.times do
        assert_raises(Mysql2::ResourceBusyError) do
          connect_to_mysql!
        end
      end
    end

    yield_to_background

    assert_raises(Mysql2::CircuitOpenError) do
      connect_to_mysql!
    end

    Timecop.travel(ERROR_TIMEOUT + 1) do
      connect_to_mysql!
    end
  end

  def test_query_instrumentation
    client = connect_to_mysql!

    notified = false
    subscriber = Semian.subscribe do |event, resource, scope, adapter|
      notified = true

      assert_equal(:success, event)
      assert_equal(Semian[:mysql_testing], resource)
      assert_equal(:query, scope)
      assert_equal(:mysql, adapter)
    end

    client.query("SELECT 1 + 1;")

    assert(notified, "No notifications has been emitted")
  ensure
    Semian.unsubscribe(subscriber)
  end

  def test_resource_acquisition_for_query
    client = connect_to_mysql!

    Semian[:mysql_testing].acquire do
      assert_raises(Mysql2::ResourceBusyError) do
        client.query("SELECT 1 + 1;")
      end
    end
  end

  def test_semian_allows_rollback
    client = connect_to_mysql!

    client.query("START TRANSACTION;")

    Semian[:mysql_testing].acquire do
      client.query("ROLLBACK;")
    end
  end

  def test_semian_allows_rollback_with_marginalia
    client = connect_to_mysql!

    client.query("START TRANSACTION;")

    Semian[:mysql_testing].acquire do
      client.query("/*foo:bar*/ ROLLBACK;")
    end
  end

  def test_semian_allows_commit
    client = connect_to_mysql!

    client.query("START TRANSACTION;")

    Semian[:mysql_testing].acquire do
      client.query("COMMIT;")
    end
  end

  def test_query_whitelisted_returns_false_for_binary_sql
    binary_query = File.read(File.expand_path("../../fixtures/binary.sql", __FILE__))
    client = connect_to_mysql!

    refute(client.send(:query_whitelisted?, binary_query))
  end

  def test_semian_allows_rollback_to_safepoint
    client = connect_to_mysql!

    client.query("START TRANSACTION;")
    client.query("SAVEPOINT foobar;")

    Semian[:mysql_testing].acquire do
      client.query("ROLLBACK TO foobar;")
    end

    client.query("ROLLBACK;")
  end

  def test_semian_allows_release_savepoint
    client = connect_to_mysql!

    client.query("START TRANSACTION;")
    client.query("SAVEPOINT foobar;")

    Semian[:mysql_testing].acquire do
      client.query("RELEASE SAVEPOINT foobar;")
    end

    client.query("ROLLBACK;")
  end

  def test_resource_timeout_on_query
    client = connect_to_mysql!
    client2 = connect_to_mysql!

    @proxy.downstream(:latency, latency: 500).apply do
      background { client2.query("SELECT 1 + 1;") }

      assert_raises(Mysql2::ResourceBusyError) do
        client.query("SELECT 1 + 1;")
      end
    end
  end

  def test_circuit_breaker_on_query
    client = connect_to_mysql!
    client2 = connect_to_mysql!

    @proxy.downstream(:latency, latency: 2200).apply do
      background { client2.query("SELECT 1 + 1;") }

      ERROR_THRESHOLD.times do
        assert_raises(Mysql2::ResourceBusyError) do
          client.query("SELECT 1 + 1;")
        end
      end
    end

    yield_to_background

    assert_raises(Mysql2::CircuitOpenError) do
      client.query("SELECT 1 + 1;")
    end

    Timecop.travel(ERROR_TIMEOUT + 1) do
      assert_equal(2, client.query("SELECT 1 + 1 as sum;").to_a.first["sum"])
    end
  end

  def test_unconfigured
    client = Mysql2::Client.new(
      host: SemianConfig["toxiproxy_upstream_host"],
      port: SemianConfig["mysql_toxiproxy_port"],
    )

    assert_equal(2, client.query("SELECT 1 + 1 as sum;").to_a.first["sum"])
  end

  def test_ping_on_closed_connection_dont_break_the_circuit
    client = connect_to_mysql!
    client.close

    (ERROR_THRESHOLD * 2).times do
      assert_equal(false, client.ping)
    end
  end

  def test_pings_are_circuit_broken
    client = connect_to_mysql!

    def client.raw_ping
      @real_pings ||= 0
      @real_pings += 1
      super
    end

    @proxy.downstream(:latency, latency: 2200).apply do
      ERROR_THRESHOLD.times do
        client.ping
      end

      assert_equal(false, client.ping)
    end

    assert_equal(ERROR_THRESHOLD, client.instance_variable_get(:@real_pings))
  end

  def test_changes_timeout_when_half_open_and_configured
    client = connect_to_mysql!(half_open_resource_timeout: 1)

    @proxy.downstream(:latency, latency: 3000).apply do
      (ERROR_THRESHOLD * 2).times do
        assert_raises(Mysql2::Error) do
          client.query("SELECT 1 + 1;")
        end
      end
    end

    assert_raises(Mysql2::CircuitOpenError) do
      client.query("SELECT 1 + 1;")
    end

    Timecop.travel(ERROR_TIMEOUT + 1) do
      @proxy.downstream(:latency, latency: 1500).apply do
        assert_raises(Mysql2::Error) do
          client.query("SELECT 1 + 1;")
        end
      end
    end

    Timecop.travel(ERROR_TIMEOUT * 2 + 1) do
      client.query("SELECT 1 + 1;")
      client.query("SELECT 1 + 1;")

      # Timeout has reset to the normal 2 seconds now that Circuit is closed
      @proxy.downstream(:latency, latency: 1500).apply do
        client.query("SELECT 1 + 1;")
      end
    end

    assert_equal(2, client.query_options[:connect_timeout])
    assert_equal(2, client.query_options[:read_timeout])
    assert_equal(2, client.query_options[:write_timeout])
  end

  def test_circuit_open_errors_do_not_trigger_the_circuit_breaker
    mysql_connection_error = defined?(Mysql2::Error::ConnectionError) ? Mysql2::Error::ConnectionError : Mysql2::Error
    @proxy.down do
      3.times do
        assert_raises(Mysql2::Error) do
          connect_to_mysql!
        end

        assert_equal(mysql_connection_error, Semian[:mysql_testing].circuit_breaker.last_error.class)
      end
    end
  end

  private

  def connect_to_mysql!(semian_options = {})
    Mysql2::Client.new(
      connect_timeout: 2,
      read_timeout: 2,
      write_timeout: 2,
      reconnect: true,
      host: SemianConfig["toxiproxy_upstream_host"],
      port: SemianConfig["mysql_toxiproxy_port"],
      semian: SEMIAN_OPTIONS.merge(semian_options),
    )
  end

  class FakeMysql < Mysql2::Client
    private

    def connect(*)
    end
  end
end


text:
'''
Module: load_data
    Loads the paths and table information from which all other data is computed.
    This is the first script run in the loading pipeline.
'''
from __future__ import division, print_function
from hscom import __common__
(print, print_, print_on, print_off,
 rrr, profile, printDBG) = __common__.init(__name__, '[ld2]', DEBUG=False)
# Standard
from os.path import join, exists, splitext
import os
import re
import shutil
import sys
# Science
import numpy as np
from PIL import Image
# Hotspotter
import DataStructures as ds
from hscom import helpers
from hscom import helpers as util
from hscom import tools

# GLOBALS

VERBOSE_LOAD_DATA = True

CHIP_TABLE_FNAME = 'chip_table.csv'
NAME_TABLE_FNAME = 'name_table.csv'
IMAGE_TABLE_FNAME = 'image_table.csv'

# TODO: Allow alternative internal directories
RDIR_INTERNAL_ALTS = ['.hs_internals']
RDIR_INTERNAL2 = '_hsdb'
RDIR_IMG2 = 'images'

# paths relative to dbdir
RDIR_IMG      = RDIR_IMG2
RDIR_INTERNAL = RDIR_INTERNAL2
RDIR_COMPUTED = join(RDIR_INTERNAL2, 'computed')
RDIR_CHIP     = join(RDIR_COMPUTED, 'chips')
RDIR_RCHIP    = join(RDIR_COMPUTED, 'temp')
RDIR_CACHE    = join(RDIR_COMPUTED, 'cache')
RDIR_FEAT     = join(RDIR_COMPUTED, 'feats')
RDIR_RESULTS  = join(RDIR_COMPUTED, 'results')
RDIR_QRES     = join(RDIR_COMPUTED, 'query_results')

UNKNOWN_NAME = '____'

# DRIVER CODE

#@profile  # This is perfectly fast .2 seconds on GZ
@util.indent_decor('[ld2.load_csv]')
def load_csv_tables(db_dir, allow_new_dir=True):
    '''
    Big function which loads the csv tables from a datatabase directory
    Returns HotspotterDirs and HotspotterTables
    '''
    if 'vdd' in sys.argv:
        helpers.vd(db_dir)
    print('=============================')
    print('[ld2] Loading hotspotter csv tables: %r' % db_dir)
    hs_dirs = ds.HotspotterDirs(db_dir)
    hs_tables = ds.HotspotterTables()
    #exec(hs_dirs.execstr('hs_dirs'))
    #print(hs_dirs.execstr('hs_dirs'))
    img_dir      = hs_dirs.img_dir
    internal_dir = hs_dirs.internal_dir
    db_dir       = hs_dirs.db_dir
    # --- Table Names ---
    chip_table   = join(internal_dir, CHIP_TABLE_FNAME)
    name_table   = join(internal_dir, NAME_TABLE_FNAME)
    image_table  = join(internal_dir, IMAGE_TABLE_FNAME)  # TODO: Make optional
    # --- CHECKS ---
    has_dbdir   = helpers.checkpath(db_dir)
    has_imgdir  = helpers.checkpath(img_dir)
    has_chiptbl = helpers.checkpath(chip_table)
    has_nametbl = helpers.checkpath(name_table)
    has_imgtbl  = helpers.checkpath(image_table)

    # ChipTable Header Markers
    header_numdata = '# NumData '
    header_csvformat_re = '# *ChipID,'
    v12_csvformat_re = r'#[ 0-9]*\) '
    # Default ChipTable Header Variables
    chip_csv_format = ['ChipID', 'ImgID',  'NameID',   'roi[tl_x  tl_y  w  h]',  'theta']
    v12_csv_format = ['instance_id', 'image_id', 'name_id', 'roi']

    # TODO DETECT OLD FORMATS HERE
    db_version = 'current'
    isCurrentVersion = all([has_dbdir, has_imgdir, has_chiptbl, has_nametbl, has_imgtbl])
    print('[ld2] isCurrentVersion=%r' % isCurrentVersion)
    IS_VERSION_1_OR_2 = False

    if not isCurrentVersion:
        helpers.checkpath(db_dir, verbose=True)
        helpers.checkpath(img_dir, verbose=True)
        helpers.checkpath(chip_table, verbose=True)
        helpers.checkpath(name_table, verbose=True)
        helpers.checkpath(image_table, verbose=True)
        import db_info

        def assign_alternate(tblname, optional=False):
            path = join(db_dir, tblname)
            if helpers.checkpath(path, verbose=True):
                return path
            path = join(db_dir, '.hs_internals', tblname)
            if helpers.checkpath(path, verbose=True):
                return path
            if optional:
                return None
            else:
                raise Exception('bad state=%r' % tblname)
        #
        if db_info.has_v2_gt(db_dir):
            IS_VERSION_1_OR_2 = True
            db_version = 'hotspotter-v2'
            print('[ld2] has %s database format' % db_version)
            chip_csv_format = []
            header_csvformat_re = v12_csvformat_re
            chip_table  = assign_alternate('instance_table.csv')
            name_table  = assign_alternate('name_table.csv')
            image_table = assign_alternate('image_table.csv')
        #
        elif db_info.has_v1_gt(db_dir):
            IS_VERSION_1_OR_2 = True
            db_version = 'hotspotter-v1'
            print('[ld2] has %s database format' % db_version)
            chip_csv_format = []
            header_csvformat_re = v12_csvformat_re
            chip_table  = assign_alternate('animal_info_table.csv')
            name_table  = assign_alternate('name_table.csv', optional=True)
            image_table = assign_alternate('image_table.csv', optional=True)
        #
        elif db_info.has_ss_gt(db_dir):
            db_version = 'stripespotter'
            print('[ld2] has %s database format' % db_version)
            chip_table = join(db_dir, 'SightingData.csv')

            chip_csv_format = ['imgindex', 'original_filepath', 'roi', 'animal_name']
            header_csvformat_re = '#imgindex,'
            #raise NotImplementedError('stripe spotter conversion')
            if not helpers.checkpath(chip_table, verbose=True):
                raise Exception('bad state chip_table=%r' % chip_table)
        else:
            try:
                db_version = 'current'  # Well almost
                chip_table  = assign_alternate(CHIP_TABLE_FNAME)
                name_table  = assign_alternate(NAME_TABLE_FNAME)
                image_table = assign_alternate(IMAGE_TABLE_FNAME)
            except Exception:
                if db_info.has_partial_gt(db_dir):
                    print('[ld2] detected incomplete database')
                    raise NotImplementedError('partial database recovery')
                elif allow_new_dir:
                    print('[ld2] detected new dir')
                    hs_dirs.ensure_dirs()
                    return hs_dirs, hs_tables, 'newdb'
                else:
                    import traceback
                    print(traceback.format_exc())
                    print('[ld2] I AM IN A BAD STATE!')
                    errmsg  = ''
                    errmsg += ('\n\n!!!!!\n\n')
                    errmsg += ('  ! The data tables seem to not be loaded')
                    errmsg += (' Files in internal dir: %r' % internal_dir)
                    for fname in os.listdir(internal_dir):
                        errmsg += ('   ! fname')
                    errmsg += ('\n\n!!!!!\n\n')
                    print(errmsg)
                    raise Exception(errmsg)
    if not helpers.checkpath(chip_table):
        raise Exception('bad state chip_table=%r' % chip_table)
    print('[ld2] detected %r' % db_version)
    hs_dirs.ensure_dirs()
    print('-------------------------')
    print('[ld2] Loading database tables: ')
    cid_lines  = []
    line_num   = 0
    csv_line   = ''
    csv_fields = []

    # RCOS TODO: We need a more general csv read function
    # which can handle all of these little corner cases delt with here.
    # ------------------
    # --- READ NAMES ---
    # ------------------
    nx2_name = [UNKNOWN_NAME, UNKNOWN_NAME]
    nid2_nx  = {0: 0, 1: 1}

    def add_name(name, nid=None):
        nx = len(nx2_name)
        nx2_name.append(name)
        if nid is not None:
            nid2_nx[nid] = nx
        return nx
    try:
        if name_table is None:
            raise IOError('name_table will be given in chip table.')
        print('[ld2] Loading name table: %r' % name_table)
        name_lines = open(name_table, 'r')
        for line_num, csv_line in enumerate(name_lines):
            csv_line = csv_line.strip('\n\r\t ')
            if len(csv_line) == 0 or csv_line.find('#') == 0:
                continue
            csv_fields = [_.strip(' ') for _ in csv_line.strip('\n\r ').split(',')]
            nid = int(csv_fields[0])
            name = csv_fields[1]
            add_name(name, nid)
        name_lines.close()
        if VERBOSE_LOAD_DATA:
            print('[ld2] * Loaded %r names (excluding unknown names)' % (len(nx2_name) - 2))
            print('[ld2] * Done loading name table')
    except IOError as ex:
        print('IOError: %r' % ex)
        print('[ld2.name] loading without name table')
        #raise
    except Exception as ex:
        print('[ld2.name] ERROR %r' % ex)
        #print('[ld2.name] ERROR name_tbl parsing: %s' % (''.join(cid_lines)))
        print('[ld2.name] ERROR on line number:  %r' % (line_num))
        print('[ld2.name] ERROR on line:         %r' % (csv_line))
        print('[ld2.name] ERROR on fields:       %r' % (csv_fields))

    # -------------------
    # --- READ IMAGES ---
    # -------------------
    gx2_gname = []
    gx2_aif   = []
    gid2_gx = {}  # this is not used. It can probably be removed

    def add_image(gname, aif, gid):
        gx = len(gx2_gname)
        gx2_gname.append(gname)
        gx2_aif.append(aif)
        if gid is not None:
            # this is not used. It can probably be removed
            gid2_gx[gid] = gx
        return gx
    try:
        print('[ld2] Loading images')
        # Load Image Table
        # <LEGACY CODE>
        if image_table is None:
            raise IOError('image_table will be given in chip table')
        if VERBOSE_LOAD_DATA:
            print('[ld2] * Loading image table: %r' % image_table)
        gid_lines = open(image_table, 'r').readlines()
        for line_num, csv_line in enumerate(gid_lines):
            csv_line = csv_line.strip('\n\r\t ')
            if len(csv_line) == 0 or csv_line.find('#') == 0:
                continue
            csv_fields = [_.strip(' ') for _ in csv_line.strip('\n\r ').split(',')]
            gid = int(csv_fields[0])
            # You have 3 csv files. Format == gid, gname.ext, aif
            if len(csv_fields) == 3:
                gname = csv_fields[1]
                aif   = csv_fields[2].lower() in ['true', '1']  # convert to bool correctly
            # You have 4 csv fields. Format == gid, gname, ext, aif
            elif len(csv_fields) == 4:
                gname = '.'.join(csv_fields[1:3])
                aif   =  csv_fields[3].lower() in ['true', '1']
            else:
                gname = 'ERROR'
                aif = False
            add_image(gname, aif, gid)
        nTableImgs = len(gx2_gname)
        fromTableNames = set(gx2_gname)
        if VERBOSE_LOAD_DATA:
            print('[ld2] * table specified %r images' % nTableImgs)
            # </LEGACY CODE>
            # Load Image Directory
            print('[ld2] * Loading image directory: %r' % img_dir)
        nDirImgs = 0
        nDirImgsAlready = 0
        for fname in os.listdir(img_dir):
            if len(fname) > 4 and fname[-4:].lower() in ['.jpg', '.png', '.tiff']:
                if fname in fromTableNames:
                    nDirImgsAlready += 1
                    continue
                add_image(fname, False, None)
                nDirImgs += 1
        if VERBOSE_LOAD_DATA:
            print('[ld2] * dir specified %r images' % nDirImgs)
            print('[ld2] * %r were already specified in the table' % nDirImgsAlready)
            print('[ld2] * Loaded %r images' % len(gx2_gname))
            print('[ld2] * Done loading images')
    except IOError as ex:
        print('IOError: %r' % ex)
        print('[ld2.img] loading without image table')
        #if '--strict' in sys.argv:
            #raise
    except Exception as ex:
        print('[ld2!.img] ERROR %r' % ex)
        #print('[ld2.img] ERROR image_tbl parsing: %s' % (''.join(cid_lines)))
        print('[ld2!.img] ERROR on line number:  %r' % (line_num))
        print('[ld2!.img] ERROR on line:         %r' % (csv_line))
        print('[ld2!.img] ERROR on fields:       %r' % (csv_fields))
        raise

    try:
        # ------------------
        # --- READ CHIPS ---
        # ------------------
        print('[ld2] Loading chip table: %r' % chip_table)
        # Load Chip Table Header
        cid_lines = open(chip_table, 'r').readlines()
        num_data   = -1
        # Parse Chip Table Header
        for line_num, csv_line in enumerate(cid_lines):
            #print('[LINE %4d] %r' % (line_num, csv_line))
            csv_line = csv_line.strip('\n\r\t ')
            if len(csv_line) == 0:
                #print('[LINE %4d] BROKEN' % (line_num))
                continue
            csv_line = csv_line.strip('\n')
            if csv_line.find('#') != 0:
                #print('[LINE %4d] BROKEN' % (line_num))
                break  # Break after header
            if re.search(header_csvformat_re, csv_line) is not None:
                #print('[LINE %4d] SEARCH' % (line_num))
                # Specified Header Variables
                if IS_VERSION_1_OR_2:
                    #print(csv_line)
                    end_ = csv_line.find('-')
                    if end_ != -1:
                        end_ = end_ - 1
                        #print('end_=%r' % end_)
                        fieldname = csv_line[5:end_]
                    else:
                        fieldname = csv_line[5:]
                    #print(fieldname)
                    fieldname = fieldname.strip(' ')
                    chip_csv_format += [fieldname]

                else:
                    chip_csv_format = [_.strip() for _ in csv_line.strip('#').split(',')]
                #print('[ld2] read chip_csv_format: %r' % chip_csv_format)
            if csv_line.find(header_numdata) == 0:
                #print('[LINE %4d] NUM_DATA' % (line_num))
                num_data = int(csv_line.replace(header_numdata, ''))
        if IS_VERSION_1_OR_2 and len(chip_csv_format) == 0:
            chip_csv_format = v12_csv_format
        if VERBOSE_LOAD_DATA:
            print('[ld2] * num_chips: %r' % num_data)
            print('[ld2] * chip_csv_format: %r ' % chip_csv_format)
        #print('[ld2.chip] Header Columns: %s\n    ' % '\n   '.join(chip_csv_format))

        def tryindex(list, valid_items):
            # Return the index of the first valid item
            for item in valid_items:
                try:
                    return list.index(item)
                except ValueError:
                    pass
            return None

        #class AllocedList(object):
            #def __init__(self, list_=[], nAlloc=0):
                #self._len = 0

            #def __add__(self, other):
                #pass

        # Hotspotter Chip Tables
        cx2_cid   = []
        cx2_nx    = []
        cx2_gx    = []
        cx2_roi   = []
        cx2_theta = []

        # Define standard properties
        # The following variables specify the column index of the property
        # if it exists in the CSV table.
        cid_x   = tryindex(chip_csv_format, ['ChipID', 'imgindex', 'instance_id'])
        gid_x   = tryindex(chip_csv_format, ['ImgID', 'image_id'])
        nid_x   = tryindex(chip_csv_format, ['NameID', 'name_id'])
        roi_x   = tryindex(chip_csv_format, ['roi[tl_x  tl_y  w  h]', 'roi'])
        theta_x = tryindex(chip_csv_format, ['theta'])
        # new fields
        gname_x = tryindex(chip_csv_format, ['Image', 'image_name', 'original_filepath'])
        name_x  = tryindex(chip_csv_format, ['Name', 'animal_name'])
        # A list of the standard
        standard_xs_ = [cid_x, gid_x, gname_x, nid_x, name_x, roi_x, theta_x]
        standard_xs = [x for x in standard_xs_ if x is not None]

        # Make sure all required fields are there
        if cid_x is None:
            raise Exception('Improper format. Cannot find cid_x')
        if gid_x is None and gname_x is None:
            raise Exception('Improper format. Cannot find gid_x or gname_x')
        if nid_x is None and name_x is None:
            raise Exception('Improper format. Cannot find nid_x or name_x')

        # All nonstandard properties are unknown and belong in the prop_dict
        # get csv indexes which are unknown properties
        prop_x_list  = np.setdiff1d(range(len(chip_csv_format)), standard_xs).tolist()
        px2_prop_key = [chip_csv_format[x] for x in prop_x_list]
        prop_dict = {}
        for prop in iter(px2_prop_key):
            prop_dict[prop] = []

        # Print header parsing status
        if VERBOSE_LOAD_DATA:
            print('[ld2] * num_user_properties: %d' % (len(prop_dict.keys())))
            print('[ld2] * num_standard_properties: %d / %d' %
                  (len(standard_xs), len(standard_xs_)))

        #def parse_integer(csv_fields, field_x):
            #try:
                #int_ = int(csv_fields[field_x])
                #return int_
            #except ValueError:
                #print('[ld2!] field_x = %r' % field_x)
                #print('[ld2!] csv_fields = %r' % csv_fields)
                #print('[ld2!] csv_fields[field_x] = %r' % csv_fields[field_x])
                #print(chip_csv_format)
                #raise

        def parse_intlist_field(csv_field):
            list_str = csv_field.strip('[').strip(']')
            list_ = [int(round(float(_))) for _ in list_str.split()]
            return list_

        # Read each noncommnted line in the chip table
        for line_num, csv_line in enumerate(cid_lines):
            csv_line = csv_line.strip('\n\r\t ')
            if len(csv_line) == 0 or csv_line.find('#') == 0:
                continue
            # Split each line into a list of fields
            csv_fields = [_.strip(' ') for _ in csv_line.strip('\n\r ').split(',')]

            # Parse nonstandard properties first
            for px, x in enumerate(prop_x_list):
                prop = px2_prop_key[px]
                prop_val = csv_fields[x]
                prop_dict[prop].append(prop_val)

            # Parse Chip ID
            cid = int(csv_fields[cid_x])

            # Parse Chip ROI
            if roi_x is not None:
                roi = parse_intlist_field(csv_fields[roi_x])
            else:
                assert gname_x is not None, 'roi_x AND gname_x cannot be None'

            # Parse Chip theta
            if theta_x is not None:
                theta = float(csv_fields[theta_x])
            else:
                theta = 0

            # Parse Image ID/X
            if gid_x is not None:
                gid = int(csv_fields[gid_x])
                gx  = gid2_gx[gid]
            elif gname_x is not None:
                gname = csv_fields[gname_x]
                # LEGACY HACK
                if db_version != 'current':
                    if db_version == 'stripespotter':
                        if not exists(gname):
                            gname = 'img-%07d.jpg' % cid
                    else:
                        # This is so hacky. The gpath in hospotter-v2 doesnt have
                        # extensions
                        name, ext = splitext(gname)
                        if ext not in helpers.IMG_EXTENSIONS:
                            if 'img_extension' in prop_dict:
                                gname = gname + '.' + prop_dict['img_extension'][-1]
                # /LEGACY HACK
                try:
                    gx = gx2_gname.index(gname)
                except ValueError:
                    gx = add_image(gname, False, None)

            # LEGACY HACK
            if len(roi) == 0:
                # Entire image is the roi
                print('[ld2] Converting %s database' % db_version)
                gpath = join(db_dir, RDIR_IMG, gname)
                w, h = Image.open(gpath).size
                roi = [1, 1, w, h]
            # /LEGACY HACK

            # Parse Name ID/X
            if nid_x is not None:
                nid = int(csv_fields[nid_x])
                nx = nid2_nx[nid]
            elif name_x is not None:
                name = csv_fields[name_x]
                try:
                    nx = nx2_name.index(name)
                except ValueError:
                    nx = add_name(name)
            # Append info to cid lists
            cx2_cid.append(cid)
            cx2_gx.append(gx)
            cx2_nx.append(nx)
            cx2_roi.append(roi)
            cx2_theta.append(theta)
    except Exception as ex:
        print('[chip.ld2] ERROR %r' % ex)
        #print('[chip.ld2] ERROR parsing: %s' % (''.join(cid_lines)))
        print('[chip.ld2] ERROR reading header:  %r' % (line_num))
        print('[chip.ld2] ERROR on line number:  %r' % (line_num))
        print('[chip.ld2] ERROR on line:         %r' % (csv_line))
        print('[chip.ld2] ERROR on fields:       %r' % (csv_fields))
        raise

    if VERBOSE_LOAD_DATA:
        print('[ld2] * Loaded: %r chips' % (len(cx2_cid)))
        print('[ld2] * Done loading chip table')

    # Return all information from load_tables
    #hs_tables.gid2_gx = gid2_gx
    #hs_tables.nid2_nx  = nid2_nx
    hs_tables.init(gx2_gname, gx2_aif,
                   nx2_name,
                   cx2_cid, cx2_nx, cx2_gx,
                   cx2_roi, cx2_theta, prop_dict)

    print('[ld2] Done Loading hotspotter csv tables: %r' % (db_dir))
    if 'vcd' in sys.argv:
        helpers.vd(hs_dirs.computed_dir)
    return hs_dirs, hs_tables, db_version

# Make Table Functions
# Returns the formated csv table text

def make_csv_table(column_labels=None, column_list=[], header='', column_type=None):
    if len(column_list) == 0:
        print('[ld2] No columns')
        return header
    column_len = [len(col) for col in column_list]
    num_data = column_len[0]
    if num_data == 0:
        print('[ld2.make_csv_table()] No data. (header=%r)' % (header,))
        return header
    if any([num_data != clen for clen in column_len]):
        print('[lds] column_labels = %r ' % (column_labels,))
        print('[lds] column_len = %r ' % (column_len,))
        print('[ld2] inconsistent column lengths')
        return header

    if column_type is None:
        column_type = [type(col[0]) for col in column_list]

    csv_rows = []
    csv_rows.append(header)
    csv_rows.append('# NumData %r' % num_data)

    column_maxlen = []
    column_str_list = []

    if column_labels is None:
        column_labels = [''] * len(column_list)

    def _toint(c):
        try:
            if np.isnan(c):
                return 'nan'
        except TypeError as ex:
            print('------')
            print('[ld2] TypeError %r ' % ex)
            print('[ld2] _toint(c) failed')
            print('[ld2] c = %r ' % c)
            print('[ld2] type(c) = %r ' % type(c))
            print('------')
            raise
        return ('%d') % int(c)

    for col, lbl, coltype in iter(zip(column_list, column_labels, column_type)):
        if tools.is_list(coltype):
            col_str  = [str(c).replace(',', ' ').replace('.', '') for c in iter(col)]
        elif tools.is_float(coltype):
            col_str = [('%.2f') % float(c) for c in iter(col)]
        elif tools.is_int(coltype):
            col_str = [_toint(c) for c in iter(col)]
        elif tools.is_str(coltype):
            col_str = [str(c) for c in iter(col)]
        else:
            col_str  = [str(c) for c in iter(col)]
        col_lens = [len(s) for s in iter(col_str)]
        max_len  = max(col_lens)
        max_len  = max(len(lbl), max_len)
        column_maxlen.append(max_len)
        column_str_list.append(col_str)

    _fmtfn = lambda maxlen: ''.join(['%', str(maxlen + 2), 's'])
    fmtstr = ','.join([_fmtfn(maxlen) for maxlen in column_maxlen])
    csv_rows.append('# ' + fmtstr % tuple(column_labels))
    for row in zip(*column_str_list):
        csv_rows.append('  ' + fmtstr % row)

    csv_text = '\n'.join(csv_rows)
    return csv_text

def make_flat_table(hs, cx_list):
    # Valid chip tables
    if len(cx_list) == 0:
        return ''
    cx2_cid   = hs.tables.cx2_cid[cx_list]
    # Use the indexes as ids (FIXME: Just go back to g/n-ids)
    cx2_gname = hs.cx2_gname(cx_list)
    cx2_name  = hs.cx2_name(cx_list)
    try:
        cx2_roi   = hs.tables.cx2_roi[cx_list]
    except IndexError as ex:
        print(ex)
        cx2_roi = np.array([])
    cx2_theta = hs.tables.cx2_theta[cx_list]
    prop_dict = {propkey: [cx2_propval[cx] for cx in iter(cx_list)]
                 for (propkey, cx2_propval) in hs.tables.prop_dict.iteritems()}
    # Turn the chip indexes into a DOCUMENTED csv table
    header = '# flat table'
    column_labels = ['ChipID', 'Image', 'Name', 'roi[tl_x  tl_y  w  h]', 'theta']
    column_list   = [cx2_cid, cx2_gname, cx2_name, cx2_roi, cx2_theta]
    column_type   = [int, int, int, list, float]
    if not prop_dict is None:
        for key, val in prop_dict.iteritems():
            column_labels.append(key)
            column_list.append(val)
            column_type.append(str)

    chip_table = make_csv_table(column_labels, column_list, header, column_type)
    return chip_table

def make_chip_csv(hs, cx_list):
    # Valid chip tables
    if len(cx_list) == 0:
        return ''
    cx2_cid   = hs.tables.cx2_cid[cx_list]
    # Use the indexes as ids (FIXME: Just go back to g/n-ids)
    cx2_gx   = hs.tables.cx2_gx[cx_list] + 1  # FIXME
    cx2_nx   = hs.tables.cx2_nx[cx_list]   # FIXME
    try:
        cx2_roi   = hs.tables.cx2_roi[cx_list]
    except IndexError as ex:
        print(ex)
        cx2_roi = np.array([])
    cx2_theta = hs.tables.cx2_theta[cx_list]
    prop_dict = {propkey: [cx2_propval[cx] for cx in iter(cx_list)]
                 for (propkey, cx2_propval) in hs.tables.prop_dict.iteritems()}
    # Turn the chip indexes into a DOCUMENTED csv table
    header = '# chip table'
    column_labels = ['ChipID', 'ImgID', 'NameID', 'roi[tl_x  tl_y  w  h]', 'theta']
    column_list   = [cx2_cid, cx2_gx, cx2_nx, cx2_roi, cx2_theta]
    column_type   = [int, int, int, list, float]
    if not prop_dict is None:
        for key, val in prop_dict.iteritems():
            column_labels.append(key)
            column_list.append(val)
            column_type.append(str)

    chip_table = make_csv_table(column_labels, column_list, header, column_type)
    return chip_table

def make_image_csv(hs, gx_list):
    'return an image table csv string'
    if len(gx_list) == 0:
        return ''
    gx2_gid   = np.array(gx_list) + 1  # FIXME
    gx2_gname = hs.tables.gx2_gname[gx_list]
    try:
        gx2_aif   = hs.tables.gx2_aif[gx_list]
    except Exception as ex:
        print(ex)
        gx2_aif = np.zeros(len(gx2_gid), dtype=np.uint32)
    # Make image_table.csv
    header = '# image table'
    column_labels = ['gid', 'gname', 'aif']  # do aif for backwards compatibility
    column_list   = [gx2_gid, gx2_gname, gx2_aif]
    image_table = make_csv_table(column_labels, column_list, header)
    return image_table

def make_name_csv(hs, nx_list):
    'returns an name table csv string'
    if len(nx_list) == 0:
        return ''
    nx_list_  = np.setdiff1d(nx_list, [0, 1])   # dont write ____ for backcomp
    nx2_name  = hs.tables.nx2_name[nx_list_]
    # Make name_table.csv
    header = '# name table'
    column_labels = ['nid', 'name']
    column_list   = [nx_list_, nx2_name]
    name_table = make_csv_table(column_labels, column_list, header)
    return name_table

# Write Table Functions
# Makes and writes csv files to disk

def write_csv_tables(hs):
    'Saves the tables to disk'
    print('[ld2] Writing csv tables')
    # Output directories
    internal_dir = hs.dirs.internal_dir
    # Create backup # RCOS TODO: Should be a preference
    CREATE_BACKUP = True
    if CREATE_BACKUP:
        backup_csv_tables(hs, force_backup=True)
    # Get valid indexes
    valid_cx = hs.get_valid_cxs()
    valid_gx = hs.get_valid_gxs()
    valid_nx = hs.get_valid_nxs()
    # Make table from indexes
    chip_table  = make_chip_csv(hs, valid_cx)
    image_table = make_image_csv(hs, valid_gx)
    name_table  = make_name_csv(hs, valid_nx)
    # Make csv filenames
    chip_table_fpath  = join(internal_dir, CHIP_TABLE_FNAME)
    name_table_fpath  = join(internal_dir, NAME_TABLE_FNAME)
    image_table_fpath = join(internal_dir, IMAGE_TABLE_FNAME)
    # write csv files
    print('[ld2] Writing chip table')
    helpers.write_to(chip_table_fpath, chip_table)
    print('[ld2] Writing name table')
    helpers.write_to(name_table_fpath, name_table)
    print('[ld2] Writing image table')
    helpers.write_to(image_table_fpath, image_table)

def write_flat_table(hs):
    dbdir = hs.dirs.db_dir
    # Make flat table
    valid_cx = hs.get_valid_cxs()
    flat_table  = make_flat_table(hs, valid_cx)
    flat_table_fpath  = join(dbdir, 'flat_table.csv')
    # Write flat table
    print('[ld2] Writing flat table')
    helpers.write_to(flat_table_fpath, flat_table)

def backup_csv_tables(hs, force_backup=False):
    internal_dir = hs.dirs.internal_dir
    backup_dir = join(internal_dir, 'backup_v0.1.0')
    if not exists(backup_dir) or force_backup:
        helpers.ensuredir(backup_dir)
        timestamp = helpers.get_timestamp(use_second=True)

        def do_backup(fname):
            src = join(internal_dir, fname)
            dst_fname = ('%s_bak-' + timestamp + '%s') % splitext(fname)
            dst = join(backup_dir, dst_fname)
            if exists(src):
                shutil.copy(src, dst)
        do_backup(CHIP_TABLE_FNAME)
        do_backup(NAME_TABLE_FNAME)
        do_backup(IMAGE_TABLE_FNAME)


text: nyaa anime torrent tracker access library for golang


text: Rails.application.config.session_store :cookie_store, key: '_test4_session'


text:
/**
	For simple applications, you might define all of your views in this file.
	For more complex applications, you might choose to separate these kind definitions
	into multiple files under this folder and require() as needed.
*/

var
	kind = require('enyo/kind'),
	FittableRows = require('layout/FittableRows'),
	Toolbar = require('onyx/Toolbar'),
	Scroller = require('enyo/Scroller'),
	Button = require('onyx/Button');

module.exports = kind({
	kind: FittableRows,
	fit: true,
	components:[
		{kind: Toolbar, content: 'Hello World'},
		{kind: Scroller, fit: true, components: [
			{name: 'main', allowHtml: true}
		]},
		{kind: Toolbar, components: [
			{kind: Button, content: 'Tap me', ontap: 'helloWorldTap'}
		]}
	],
	helloWorldTap: function(inSender, inEvent) {
		this.$.main.addContent('The button was tapped.<br/>');
	}
});


text:
namespace danciben
{
    partial class Form3
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.delete = new System.Windows.Forms.Button();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // textBox1
            //
            this.textBox1.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.textBox1.Location = new System.Drawing.Point(131, 116);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(373, 248);
            this.textBox1.TabIndex = 0;
            //
            // button1
            //
            this.button1.Image = global::danciben.Properties.Resources.return2;
            this.button1.Location = new System.Drawing.Point(131, 431);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(88, 30);
            this.button1.TabIndex = 1;
            this.button1.Text = "保存";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // delete
            //
            this.delete.Image = global::danciben.Properties.Resources.return22;
            this.delete.Location = new System.Drawing.Point(418, 430);
            this.delete.Name = "delete";
            this.delete.Size = new System.Drawing.Size(86, 30);
            this.delete.TabIndex = 2;
            this.delete.Text = "删除";
            this.delete.UseVisualStyleBackColor = true;
            this.delete.Click += new System.EventHandler(this.delete_Click);
            //
            // textBox2
            //
            this.textBox2.Location = new System.Drawing.Point(131, 42);
            this.textBox2.Multiline = true;
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(373, 39);
            this.textBox2.TabIndex = 3;
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(131, 22);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(41, 12);
            this.label1.TabIndex = 4;
            this.label1.Text = "重要度";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(131, 88);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(53, 12);
            this.label2.TabIndex = 5;
            this.label2.Text = "计划内容";
            //
            // Form3
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackgroundImage = global::danciben.Properties.Resources.return22;
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.ClientSize = new System.Drawing.Size(642, 508);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.delete);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form3";
            this.Text = "计划本";
            this.Load += new System.EventHandler(this.Form3_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button delete;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
    }
}


text:
package org.jboss.aerogear.unifiedpush.jpa;

import net.jakubholy.dbunitexpress.EmbeddedDbTesterRule;
import org.jboss.aerogear.unifiedpush.api.FlatPushMessageInformation;
import org.jboss.aerogear.unifiedpush.api.Variant;
import org.jboss.aerogear.unifiedpush.api.VariantErrorStatus;
import org.jboss.aerogear.unifiedpush.dao.FlatPushMessageInformationDao;
import org.jboss.aerogear.unifiedpush.dao.PageResult;
import org.jboss.aerogear.unifiedpush.dao.VariantDao;
import org.jboss.aerogear.unifiedpush.dto.MessageMetrics;
import org.jboss.aerogear.unifiedpush.utils.DaoDeployment;
import org.jboss.aerogear.unifiedpush.utils.DateUtils;
import org.jboss.aerogear.unifiedpush.utils.TestUtils;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(Arquillian.class)
public class FlatPushMessageInformationDaoTest {

    @Inject
    private EntityManager entityManager;

    @Inject
    private FlatPushMessageInformationDao pushMessageInformationDao;

    @Inject
    private VariantDao variantDao;

    private String pushMessageInformationID = "1";

    @Deployment
    public static JavaArchive createDeployment() {
        return DaoDeployment.createDeployment();
    }

    @Rule
    public EmbeddedDbTesterRule testDb = new EmbeddedDbTesterRule("FlatPushMessageInformation.xml");

    @Before
    public void setUp() {
        // start the shindig
        entityManager.getTransaction().begin();

    }

    private void flushAndClear() {
        // flush to be sure that it's in the database
        entityManager.flush();
        // clear the cache otherwise finding the entity will not perform a select but get the entity from cache
        entityManager.clear();
    }

    @After
    public void tearDown() {
        entityManager.getTransaction().rollback();
    }

    @Test
    public void createPushMessageInformation() {

        FlatPushMessageInformation pushMessageInformation = pushMessageInformationDao.find(pushMessageInformationID);

        assertThat(pushMessageInformation).isNotNull();
        assertThat(pushMessageInformation.getSubmitDate()).isNotNull();
    }

    @Test
    public void addJsonToPushMessageInformation() {
        FlatPushMessageInformation pushMessageInformation = pushMessageInformationDao.find(pushMessageInformationID);
        pushMessageInformation.setRawJsonMessage("{\"alert\" : \"hello\"}");
        pushMessageInformationDao.update(pushMessageInformation);

        flushAndClear();

        pushMessageInformation = pushMessageInformationDao.find(pushMessageInformationID);

        assertThat(pushMessageInformation.getRawJsonMessage()).isEqualTo("{\"alert\" : \"hello\"}");
        assertThat(pushMessageInformation.getSubmitDate()).isNotNull();
    }

    @Test
    public void addClientIdentifierToPushMessageInformation() {
        FlatPushMessageInformation pushMessageInformation = pushMessageInformationDao.find(pushMessageInformationID);
        pushMessageInformation.setClientIdentifier("Java Sender Client");
        pushMessageInformationDao.update(pushMessageInformation);

        flushAndClear();

        pushMessageInformation = pushMessageInformationDao.find(pushMessageInformationID);

        assertThat(pushMessageInformation.getClientIdentifier()).isEqualTo("Java Sender Client");
        assertThat(pushMessageInformation.getSubmitDate()).isNotNull();
    }

    @Test
    public void findByPushApplicationID() {
        int page = 0;
        int pageSize = 20;
        PageResult<FlatPushMessageInformation, MessageMetrics> messageInformations = pushMessageInformationDao.findAllForPushApplication("231231231", null, Boolean.TRUE, page, pageSize);
        assertThat(messageInformations.getResultList()).isNotEmpty();
        assertThat(messageInformations.getResultList()).hasSize(2);
    }

    @Test
    public void countMessagesPerLoginName() {

        final String loginName = "admin";

        long number = pushMessageInformationDao.getNumberOfPushMessagesForLoginName(loginName);
        assertThat(number).isEqualTo(3);

        for (int i = 0; i < 1000; i++) {

            FlatPushMessageInformation pmi = new FlatPushMessageInformation();
            pmi.setPushApplicationId("231231231");

            pushMessageInformationDao.create(pmi);
        }

        number = pushMessageInformationDao.getNumberOfPushMessagesForLoginName(loginName);
        assertThat(number).isEqualTo(1003);

        // a few more for different PushApplication...
        for (int i = 0; i < 1000; i++) {

            FlatPushMessageInformation pmi = new FlatPushMessageInformation();
            pmi.setPushApplicationId("231231232");

            pushMessageInformationDao.create(pmi);
        }

        number = pushMessageInformationDao.getNumberOfPushMessagesForLoginName(loginName);
        assertThat(number).isEqualTo(2003);

        // check for all
        assertThat(pushMessageInformationDao.getNumberOfPushMessagesForApplications()).isEqualTo(2003);
    }

    @Test
    public void countMessagesPerApplication() {
        assertThat(pushMessageInformationDao.getNumberOfPushMessagesForPushApplication("231231231")).isEqualTo(2);
        assertThat(pushMessageInformationDao.getNumberOfPushMessagesForPushApplication("231231232")).isEqualTo(1);
    }

    @Test
    public void findMostBusyVariants() {
        List<FlatPushMessageInformation> lastActivity = pushMessageInformationDao.findLatestActivity("admin", 3);
        assertThat(lastActivity).hasSize(3);
    }

    @Test
    public void findAllBusyVariants() {
        List<FlatPushMessageInformation> lastActivity = pushMessageInformationDao.findLatestActivity("admin", 5);
        assertThat(lastActivity).hasSize(3); // we just have three... - but asked for five
    }

    @Test
    public void findMostBusyVariantsForOnlyTwo() {
        final FlatPushMessageInformation pushMessageInformation = pushMessageInformationDao.find("3");
        entityManager.remove(pushMessageInformation);
        List<FlatPushMessageInformation> lastActivity = pushMessageInformationDao.findLatestActivity("admin", 3);
        assertThat(lastActivity).hasSize(2);

        lastActivity = pushMessageInformationDao.findLatestActivity(3);
        assertThat(lastActivity).hasSize(2);
    }

    @Test
    public void findVariantIDsWithWarnings() {
        // all warnings:
        final List<String> variantIDsWithWarnings = pushMessageInformationDao.findVariantIDsWithWarnings();

        assertThat(variantIDsWithWarnings).hasSize(4);
        assertThat(variantIDsWithWarnings).contains("1", "2", "3", "4");
    }

    @Test
    public void findVariantIDsWithWarningsForDude() {
        // all warnings:
        final List<String> variantIDsWithWarnings = pushMessageInformationDao.findVariantIDsWithWarnings("dude");

        assertThat(variantIDsWithWarnings).hasSize(1);
        assertThat(variantIDsWithWarnings).containsOnly("4");
    }

    @Test
    public void findVariantIDsWithWarningsForKalle() {
        // all warnings:
        final List<String> variantIDsWithWarnings = pushMessageInformationDao.findVariantIDsWithWarnings("kalle");

        assertThat(variantIDsWithWarnings).isNotNull();
        assertThat(variantIDsWithWarnings).isEmpty();
    }

    @Test
    public void addTwoErrors() {
        FlatPushMessageInformation fmpi = pushMessageInformationDao.find("1");
        Variant variant = variantDao.find("1");
        VariantErrorStatus ves = new VariantErrorStatus(fmpi, variant, "error");

        ves.setVariant(variant);
        ves.setPushMessageInformation(fmpi);

        fmpi.getErrors().add(ves);
    }

    @Test
    public void ascendingDateOrdering() {

        PageResult<FlatPushMessageInformation, MessageMetrics> messageInformations =
                pushMessageInformationDao.findAllForPushApplication("231231231", null, Boolean.TRUE, 0, 25);
        final List<FlatPushMessageInformation> list = messageInformations.getResultList();
        assertThat(list).hasSize(2);

        assertThat(list.get(0).getSubmitDate()).isBefore(list.get(1).getSubmitDate());
    }

    @Test
    public void descendingDateOrdering() {
        PageResult<FlatPushMessageInformation, MessageMetrics> messageInformations =
                pushMessageInformationDao.findAllForPushApplication("231231231", null, Boolean.FALSE, 0, 25);
        final List<FlatPushMessageInformation> list = messageInformations.getResultList();
        assertThat(list).hasSize(2);

        assertThat(list.get(0).getSubmitDate()).isAfter(list.get(1).getSubmitDate());
    }

    @Test
    public void testSearchString() {
        PageResult<FlatPushMessageInformation, MessageMetrics> messageInformations =
                pushMessageInformationDao.findAllForPushApplication("231231231", "foo", Boolean.TRUE, 0, 25);
        final List<FlatPushMessageInformation> list = messageInformations.getResultList();
        assertThat(list).hasSize(1);
    }

    @Test
    public void testLongRawJsonPayload() {
        FlatPushMessageInformation largePushMessageInformation = new FlatPushMessageInformation();
        largePushMessageInformation.setPushApplicationId("231231231");
        largePushMessageInformation.setRawJsonMessage(TestUtils.longString(4500));
        pushMessageInformationDao.create(largePushMessageInformation);
    }

    @Test(expected = PersistenceException.class)
    public void testTooLongRawJsonPayload() {
        FlatPushMessageInformation largePushMessageInformation = new FlatPushMessageInformation();
        largePushMessageInformation.setPushApplicationId("231231231");
        largePushMessageInformation.setRawJsonMessage(TestUtils.longString(4501));
        pushMessageInformationDao.create(largePushMessageInformation);
        flushAndClear();
    }

    @Test
    public void deleteOldPushMessageInformations() {

        List<FlatPushMessageInformation> messageInformations = pushMessageInformationDao.findAllForPushApplication("231231231", Boolean.TRUE);
        assertThat(messageInformations).hasSize(2);

        pushMessageInformationDao.deletePushInformationOlderThan(DateUtils.calculatePastDate(0));

        flushAndClear();

        messageInformations = pushMessageInformationDao.findAllForPushApplication("231231231", Boolean.TRUE);
        assertThat(messageInformations).hasSize(0);
    }
}