text:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrateur.geriat3000">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Message"
            android:label="@string/title_activity_message"
            android:parentActivityName=".MainActivity"
            android:theme="@style/AppTheme">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.administrateur.geriat3000.MainActivity" />
        </activity>
    </application>

</manifest>


text:
<?php

namespace Meudinheiro\Common;

class Meudinheiro {

    private static $credentials;

    private static $factory;

    /**
     *
     * @param type $credentials
     */
    public static function factory($credentials){

        if(!self::$factory)
            self::$factory = new Factory($credentials);

        return self::$factory;

    }

}

redpajama_set_name: RedPajamaGithub


text:
using System.IO;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

public class DataManager : MonoBehaviour, IGameManager {

    private const string DATA_FILE = "game.dat";
    /// Data Keys
    private const string INVENTORY_KEY = "inventory";
    private const string HEALTH_KEY = "health";
    private const string MAX_HEALTH_KEY = "maxHealth";
    private const string CURRENT_LEVEL_KEY = "curLevel";
    private const string MAX_LEVEL_KEY = "maxLevel";

    public ManagerStatus Status { get; private set; }

    private NetworkService NetworkService;
    private string Filename;

    public void Startup(NetworkService service) {
        Debug.Log("Starting Data manager...");

        NetworkService = service;
        Filename = Path.Combine(Application.persistentDataPath, DATA_FILE);
        Status = ManagerStatus.Started;
    }

    public void SaveGameState() {
        Dictionary<string, object> gameState = new Dictionary<string, object>
        {
            { INVENTORY_KEY, Managers.Inventory.GetData() },
            { HEALTH_KEY,   Managers.Player.Health },
            { MAX_HEALTH_KEY, Managers.Player.MaxHealth },
            { CURRENT_LEVEL_KEY, Managers.Mission.CurLevel },
            { MAX_LEVEL_KEY, Managers.Mission.MaxLevel }
        };

        FileStream stream = File.Create(Filename);
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(stream, gameState);
        stream.Close();
    }

    public void LoadGameState() {
        if (!File.Exists(Filename)) {
            Debug.Log("No saved game");
            return;
        }

        Dictionary<string, object> gameState;

        FileStream stream = File.Open(Filename, FileMode.Open);
        BinaryFormatter formatter = new BinaryFormatter();
        gameState = formatter.Deserialize(stream) as Dictionary<string, object>;
        stream.Close();

        Managers.Inventory.UpdateData(gameState[INVENTORY_KEY] as Dictionary<string, int>);
        Managers.Player.UpdateData((int) gameState[HEALTH_KEY], (int) gameState[MAX_HEALTH_KEY]);
        Managers.Mission.UpdateData((int)gameState[CURRENT_LEVEL_KEY], (int)gameState[MAX_LEVEL_KEY]);
        Managers.Mission.RestartLevel();
    }
}


text:
layout: kanji
v4: 1835
v6: 1977
kanji: 眠
keyword: sleep
elements: sleep, eye, people, mouth, family name
strokes: 10
image: E79CA0
on-yomi: ミン
kun-yomi: ねむ.る、ねむ.い
permalink: /rtk/眠/
prev: 民
next: 捕
---

1) [PepeSeco] 26-9-2006(225): In an Orwellian sense, the eye watching the people never sleeps.

2) [Raichu] 8-7-2007(63): People need to close their eyes and sleep.

3) [astridtops] 16-12-2006(34): The little boy from The Sixth Sense couldn't sleep because before his very eyes he saw a dead crowd of people everywhere.

4) [nest0r] 13-4-2008(28): I sleep with one eye open, because the people are dangerous and may revolt at any time, you can't trust the unwashed masses.

5) [snallygaster] 25-5-2008(19): (inspired by PepeSeco's) The Orwellian-style government has a mysterious floating eye that floats silently through the night, looking in windows, watching the people while they sleep.


text:
<?php
namespace Neutron\FormBundle\Tests\Form\Type;

use Symfony\Component\Form\Tests\Extension\Core\Type\TypeTestCase;

use Neutron\FormBundle\Form\Type\DatePickerType;

use Neutron\FormBundle\Tests\Form\Extension\TypeExtensionTest;

use Neutron\FormBundle\Form\Type\DateRangePickerType;

class DateRangePickerTypeTest extends TypeTestCase
{

    public function testDefault()
    {
        $form = $this->factory->create('neutron_daterangepicker');

        $form->bind(array('first_date' => '2012-12-21', 'second_date' => '2012-12-22'));
        $form->createView();
        $data = $form->getData();

        $this->assertCount(2, $data);
        $this->assertInstanceOf('\DateTime', $data['first_date']);
        $this->assertInstanceOf('\DateTime', $data['second_date']);
    }

    protected function getExtensions()
    {
    	return array(
			new TypeExtensionTest(
				array(
				    new DateRangePickerType(),
				    new DatePickerType()
				)
			)
    	);
    }

}


text: fascgi request and response


text:
RSpec.describe "Tables" do

  before(:context) do

    report = ODFReport::Report.new("spec/templates/specs.odt") do |r|

      r.add_table('TABLE_02', []) do |t|
        t.add_column(:column_01, :id)
        t.add_column(:column_02, :name)
      end

      r.add_table('TABLE_03', [], skip_if_empty: true) do |t|
        t.add_column(:column_01, :id)
        t.add_column(:column_02, :name)
      end

    end

    report.generate("spec/result/specs.odt")

    @data = Inspector.new("spec/result/specs.odt")

  end

  context "with Empty collection" do

    it "should not remove table" do
      table2 = @data.xml.xpath(".//table:table[@table:name='TABLE_02']")
      expect(table2).not_to be_empty
    end

    it "should remove table if required" do
      table3 = @data.xml.xpath(".//table:table[@table:name='TABLE_03']")
      expect(table3).to be_empty
    end

  end

end


text:
using System;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using GroupDocs.Demo.Annotation.Mvc.AnnotationResults.Data;

namespace GroupDocs.Demo.Annotation.Mvc.AnnotationResults
{
    [DataContract]
    public class DeleteReplyResult : Result
    {
        [XmlElement("replyGuid")]
        [DataMember(Name = "replyGuid")]
        public string ReplyGuid { get; set; }

        [XmlElement("annotationGuid")]
        [DataMember(Name = "annotationGuid")]
        public string Guid { get; set; }

        [XmlElement("replies")]
        [DataMember(Name = "replies")]
        public AnnotationReplyInfo[] Replies { get; set; }

        public DateTime ServerTime { get; set; }
    }
}


text:
package com.instructure.canvasapi.model;

import android.os.Parcel;

import com.google.gson.annotations.SerializedName;

import java.util.Date;

public class ResetParent extends CanvasModel<ResetParent> {

    @SerializedName("parent_id")
    private String parentId;

    private String token;

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    @Override
    public String getComparisonString() {
        return null;
    }

    @Override
    public Date getComparisonDate() {
        return null;
    }

    @Override
    public long getId() {
        return parentId.hashCode();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.parentId);
        dest.writeString(this.token);
    }

    public ResetParent() {
    }

    protected ResetParent(Parcel in) {
        this.parentId = in.readString();
        this.token = in.readString();
    }

    public static final Creator<ResetParent> CREATOR = new Creator<ResetParent>() {
        @Override
        public ResetParent createFromParcel(Parcel source) {
            return new ResetParent(source);
        }

        @Override
        public ResetParent[] newArray(int size) {
            return new ResetParent[size];
        }
    };
}


text:
module Interfaces

  class GunzipFilter < CmdFilter

    def available_files dir
      Dir.glob File.join(dir,'*.{gz}')
    end

    def target_pathes path
      path.gsub /\.(gz)$/, ''
    end

    def cmd path
      "gunzip -c #{path} > #{target_pathes path}"
    end
  end

end


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

  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <link rel="stylesheet" type="text/css" href="definitionen.css">

server_get_serverid_by_ip($session_id, $ipaddress);

Description:

 Returns server information by its IP.

Input Variables:

 $session_id, $ipaddress

Parameters (in $params):

 None.

Output:

 Returns an array with the server parameter's values.

SQL Interaction:

 SELECT server_id FROM server_ip WHERE ip_address  = '$ipaddress' LIMIT 1 "


text:
#ifndef QIMAGE_H
#define QIMAGE_H

#include <QtGui/qtransform.h>
#include <QtGui/qpaintdevice.h>
#include <QtGui/qrgb.h>
#include <QtCore/qbytearray.h>
#include <QtCore/qrect.h>
#include <QtCore/qstring.h>

QT_BEGIN_HEADER

QT_BEGIN_NAMESPACE

QT_MODULE(Gui)

class QIODevice;
class QStringList;
class QMatrix;
class QTransform;
class QVariant;
template <class T> class QList;
template <class T> class QVector;

struct QImageData;
class QImageDataMisc; // internal
#ifndef QT_NO_IMAGE_TEXT
class Q_GUI_EXPORT QImageTextKeyLang {
public:
    QImageTextKeyLang(const char* k, const char* l) : key(k), lang(l) { }
    QImageTextKeyLang() { }

    QByteArray key;
    QByteArray lang;

    bool operator< (const QImageTextKeyLang& other) const
        { return key < other.key || (key==other.key && lang < other.lang); }
    bool operator== (const QImageTextKeyLang& other) const
        { return key==other.key && lang==other.lang; }
    inline bool operator!= (const QImageTextKeyLang &other) const
        { return !operator==(other); }
};
#endif //QT_NO_IMAGE_TEXT

class Q_GUI_EXPORT QImage : public QPaintDevice
{
public:
    enum InvertMode { InvertRgb, InvertRgba };
    enum Format {
        Format_Invalid,
        Format_Mono,
        Format_MonoLSB,
        Format_Indexed8,
        Format_RGB32,
        Format_ARGB32,
        Format_ARGB32_Premultiplied,
        Format_RGB16,
        Format_ARGB8565_Premultiplied,
        Format_RGB666,
        Format_ARGB6666_Premultiplied,
        Format_RGB555,
        Format_ARGB8555_Premultiplied,
        Format_RGB888,
        Format_RGB444,
        Format_ARGB4444_Premultiplied,
#if 0
        // reserved for future use
        Format_RGB15,
        Format_Grayscale16,
        Format_Grayscale8,
        Format_Grayscale4,
        Format_Grayscale4LSB,
        Format_Grayscale2,
        Format_Grayscale2LSB
#endif
#ifndef qdoc
        NImageFormats
#endif
    };

    QImage();
    QImage(const QSize &size, Format format);
    QImage(int width, int height, Format format);
    QImage(uchar *data, int width, int height, Format format);
    QImage(const uchar *data, int width, int height, Format format);
    QImage(uchar *data, int width, int height, int bytesPerLine, Format format);
    QImage(const uchar *data, int width, int height, int bytesPerLine, Format format);

#ifndef QT_NO_IMAGEFORMAT_XPM
    explicit QImage(const char * const xpm[]);
#endif
    explicit QImage(const QString &fileName, const char *format = 0);
#ifndef QT_NO_CAST_FROM_ASCII
    explicit QImage(const char *fileName, const char *format = 0);
#endif

    QImage(const QImage &);
    ~QImage();

    QImage &operator=(const QImage &);
#ifdef Q_COMPILER_RVALUE_REFS
    inline QImage &operator=(QImage &&other)
    { qSwap(d, other.d); return *this; }
#endif
    inline void swap(QImage &other) { qSwap(d, other.d); }

    bool isNull() const;

    int devType() const;

    bool operator==(const QImage &) const;
    bool operator!=(const QImage &) const;
    operator QVariant() const;
    void detach();
    bool isDetached() const;

    QImage copy(const QRect &rect = QRect()) const;
    inline QImage copy(int x, int y, int w, int h) const
        { return copy(QRect(x, y, w, h)); }

    Format format() const;

    QImage convertToFormat(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) const Q_REQUIRED_RESULT;
    QImage convertToFormat(Format f, const QVector<QRgb> &colorTable, Qt::ImageConversionFlags flags = Qt::AutoColor) const Q_REQUIRED_RESULT;

    int width() const;
    int height() const;
    QSize size() const;
    QRect rect() const;

    int depth() const;
#ifdef QT_DEPRECATED
    QT_DEPRECATED int numColors() const;
#endif
    int colorCount() const;
    int bitPlaneCount() const;

    QRgb color(int i) const;
    void setColor(int i, QRgb c);
#ifdef QT_DEPRECATED
    QT_DEPRECATED void setNumColors(int);
#endif
    void setColorCount(int);

    bool allGray() const;
    bool isGrayscale() const;

    uchar *bits();
    const uchar *bits() const;
    const uchar *constBits() const;
#ifdef QT_DEPRECATED
    QT_DEPRECATED int numBytes() const;
#endif
    int byteCount() const;

    uchar *scanLine(int);
    const uchar *scanLine(int) const;
    const uchar *constScanLine(int) const;
    int bytesPerLine() const;

    bool valid(int x, int y) const;
    bool valid(const QPoint &pt) const;

    int pixelIndex(int x, int y) const;
    int pixelIndex(const QPoint &pt) const;

    QRgb pixel(int x, int y) const;
    QRgb pixel(const QPoint &pt) const;

    void setPixel(int x, int y, uint index_or_rgb);
    void setPixel(const QPoint &pt, uint index_or_rgb);

    QVector<QRgb> colorTable() const;
    void setColorTable(const QVector<QRgb> colors);

    void fill(uint pixel);
    void fill(const QColor &color);
    void fill(Qt::GlobalColor color);

    bool hasAlphaChannel() const;
    void setAlphaChannel(const QImage &alphaChannel);
    QImage alphaChannel() const;
    QImage createAlphaMask(Qt::ImageConversionFlags flags = Qt::AutoColor) const;
#ifndef QT_NO_IMAGE_HEURISTIC_MASK
    QImage createHeuristicMask(bool clipTight = true) const;
#endif
    QImage createMaskFromColor(QRgb color, Qt::MaskMode mode = Qt::MaskInColor) const;

    inline QImage scaled(int w, int h, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
                        Qt::TransformationMode mode = Qt::FastTransformation) const
        { return scaled(QSize(w, h), aspectMode, mode); }
    QImage scaled(const QSize &s, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
                 Qt::TransformationMode mode = Qt::FastTransformation) const;
    QImage scaledToWidth(int w, Qt::TransformationMode mode = Qt::FastTransformation) const;
    QImage scaledToHeight(int h, Qt::TransformationMode mode = Qt::FastTransformation) const;
    QImage transformed(const QMatrix &matrix, Qt::TransformationMode mode = Qt::FastTransformation) const;
    static QMatrix trueMatrix(const QMatrix &, int w, int h);
    QImage transformed(const QTransform &matrix, Qt::TransformationMode mode = Qt::FastTransformation) const;
    static QTransform trueMatrix(const QTransform &, int w, int h);
    QImage mirrored(bool horizontally = false, bool vertically = true) const;
    QImage rgbSwapped() const;
    void invertPixels(InvertMode = InvertRgb);

    bool load(QIODevice *device, const char* format);
    bool load(const QString &fileName, const char* format=0);
    bool loadFromData(const uchar *buf, int len, const char *format = 0);
    inline bool loadFromData(const QByteArray &data, const char* aformat=0)
        { return loadFromData(reinterpret_cast<const uchar *>(data.constData()), data.size(), aformat); }

    bool save(const QString &fileName, const char* format=0, int quality=-1) const;
    bool save(QIODevice *device, const char* format=0, int quality=-1) const;

    static QImage fromData(const uchar *data, int size, const char *format = 0);
    inline static QImage fromData(const QByteArray &data, const char *format = 0)
        { return fromData(reinterpret_cast<const uchar *>(data.constData()), data.size(), format); }

    int serialNumber() const;
    qint64 cacheKey() const;

    QPaintEngine *paintEngine() const;

    // Auxiliary data
    int dotsPerMeterX() const;
    int dotsPerMeterY() const;
    void setDotsPerMeterX(int);
    void setDotsPerMeterY(int);
    QPoint offset() const;
    void setOffset(const QPoint&);
#ifndef QT_NO_IMAGE_TEXT
    QStringList textKeys() const;
    QString text(const QString &key = QString()) const;
    void setText(const QString &key, const QString &value);

#ifdef QT_DEPRECATED
    QT_DEPRECATED QString text(const char* key, const char* lang=0) const;
    QT_DEPRECATED QList<QImageTextKeyLang> textList() const;
    QT_DEPRECATED QStringList textLanguages() const;
    QT_DEPRECATED QString text(const QImageTextKeyLang&) const;
    QT_DEPRECATED void setText(const char* key, const char* lang, const QString&);
#endif
#endif

#ifdef QT3_SUPPORT
    enum Endian { BigEndian, LittleEndian, IgnoreEndian };
    QT3_SUPPORT_CONSTRUCTOR QImage(int width, int height, int depth, int numColors=0, Endian bitOrder=IgnoreEndian);
    QT3_SUPPORT_CONSTRUCTOR QImage(const QSize&, int depth, int numColors=0, Endian bitOrder=IgnoreEndian);
    QT3_SUPPORT_CONSTRUCTOR QImage(uchar *data, int w, int h, int depth, const QRgb *colortable, int numColors, Endian bitOrder);
#ifdef Q_WS_QWS
    QT3_SUPPORT_CONSTRUCTOR QImage(uchar *data, int w, int h, int depth, int pbl, const QRgb *colortable, int numColors, Endian bitOrder);
#endif
    inline QT3_SUPPORT Endian bitOrder() const {
        Format f = format();
        return f == Format_Mono ? BigEndian : (f == Format_MonoLSB ? LittleEndian : IgnoreEndian);
    }
    QT3_SUPPORT QImage convertDepth(int, Qt::ImageConversionFlags flags = Qt::AutoColor) const;
    QT3_SUPPORT QImage convertDepthWithPalette(int, QRgb* p, int pc, Qt::ImageConversionFlags flags = Qt::AutoColor) const;
    QT3_SUPPORT QImage convertBitOrder(Endian) const;
    QT3_SUPPORT bool hasAlphaBuffer() const;
    QT3_SUPPORT void setAlphaBuffer(bool);
    QT3_SUPPORT uchar **jumpTable();
    QT3_SUPPORT const uchar * const *jumpTable() const;
    inline QT3_SUPPORT void reset() { *this = QImage(); }
    static inline QT3_SUPPORT Endian systemByteOrder()
        { return QSysInfo::ByteOrder == QSysInfo::BigEndian ? BigEndian : LittleEndian; }
    inline QT3_SUPPORT QImage swapRGB() const { return rgbSwapped(); }
    inline QT3_SUPPORT QImage mirror(bool horizontally = false, bool vertically = true) const
        { return mirrored(horizontally, vertically); }
    QT3_SUPPORT bool create(const QSize&, int depth, int numColors=0, Endian bitOrder=IgnoreEndian);
    QT3_SUPPORT bool create(int width, int height, int depth, int numColors=0, Endian bitOrder=IgnoreEndian);
    inline QT3_SUPPORT QImage xForm(const QMatrix &matrix) const { return transformed(QTransform(matrix)); }
    inline QT3_SUPPORT QImage smoothScale(int w, int h, Qt::AspectRatioMode mode = Qt::IgnoreAspectRatio) const
        { return scaled(QSize(w, h), mode, Qt::SmoothTransformation); }
    inline QImage QT3_SUPPORT smoothScale(const QSize &s, Qt::AspectRatioMode mode = Qt::IgnoreAspectRatio) const
        { return scaled(s, mode, Qt::SmoothTransformation); }
    inline QT3_SUPPORT QImage scaleWidth(int w) const { return scaledToWidth(w); }
    inline QT3_SUPPORT QImage scaleHeight(int h) const { return scaledToHeight(h); }
    inline QT3_SUPPORT void invertPixels(bool invertAlpha) { invertAlpha ? invertPixels(InvertRgba) : invertPixels(InvertRgb); }
    inline QT3_SUPPORT QImage copy(int x, int y, int w, int h, Qt::ImageConversionFlags) const
        { return copy(QRect(x, y, w, h)); }
    inline QT3_SUPPORT QImage copy(const QRect &rect, Qt::ImageConversionFlags) const
        { return copy(rect); }
    static QT3_SUPPORT Endian systemBitOrder();
    inline QT3_SUPPORT_CONSTRUCTOR QImage(const QByteArray &data)
        { d = 0; *this = QImage::fromData(data); }
#endif

protected:
    virtual int metric(PaintDeviceMetric metric) const;

private:
    friend class QWSOnScreenSurface;
    QImageData *d;

    friend class QRasterPixmapData;
    friend class QBlittablePixmapData;
    friend class QPixmapCacheEntry;
    friend Q_GUI_EXPORT qint64 qt_image_id(const QImage &image);
    friend const QVector<QRgb> *qt_image_colortable(const QImage &image);

public:
    typedef QImageData * DataPtr;
    inline DataPtr &data_ptr() { return d; }
};

Q_DECLARE_SHARED(QImage)
Q_DECLARE_TYPEINFO(QImage, Q_MOVABLE_TYPE);

// Inline functions...

Q_GUI_EXPORT_INLINE bool QImage::valid(const QPoint &pt) const { return valid(pt.x(), pt.y()); }
Q_GUI_EXPORT_INLINE int QImage::pixelIndex(const QPoint &pt) const { return pixelIndex(pt.x(), pt.y());}
Q_GUI_EXPORT_INLINE QRgb QImage::pixel(const QPoint &pt) const { return pixel(pt.x(), pt.y()); }
Q_GUI_EXPORT_INLINE void QImage::setPixel(const QPoint &pt, uint index_or_rgb) { setPixel(pt.x(), pt.y(), index_or_rgb); }

// QImage stream functions

#if !defined(QT_NO_DATASTREAM)
Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QImage &);
Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QImage &);
#endif

#ifdef QT3_SUPPORT
Q_GUI_EXPORT QT3_SUPPORT void bitBlt(QImage* dst, int dx, int dy, const QImage* src,
                                     int sx=0, int sy=0, int sw=-1, int sh=-1, Qt::ImageConversionFlags flags = Qt::AutoColor);
#endif

QT_END_NAMESPACE

QT_END_HEADER

#endif // QIMAGE_H


text:
Swarming Server
==============

Contains the AppEngine code to do task scheduling and the bot code to run on the
task executors. See the design doc and main pages for more information.

Uploading to AppEngine
----------------------

  $ python tools/update.py -A my-swarming-server

Documentation
-------------

Design doc:
https://code.google.com/p/swarming/wiki/SwarmingDesign

Reference:
https://code.google.com/p/swarming/wiki/SwarmingUserGuide

Technical details about the implementation can be found in server/README.md.


text:
'''
Created on Feb 6, 2015

@author: cmccully
'''
from __future__ import absolute_import, division, print_function
from future.utils import iteritems
import time
import om10
import numpy as np
import re
import json
import os
import pandas as pd
import copy
import gzip
import shutil
from lsst.utils import getPackageDir
from lsst.sims.utils import SpecMap, defaultSpecMap
from lsst.sims.catUtils.baseCatalogModels import GalaxyTileCompoundObj
from lsst.sims.catUtils.matchSED import matchBase
from lsst.sims.photUtils import Bandpass, BandpassDict, Sed
from lsst.sims.utils import radiansFromArcsec
from lsst.sims.catUtils.supernovae import SNObject

__all__ = ['sprinklerCompound', 'sprinkler']
class sprinklerCompound(GalaxyTileCompoundObj):
    objid = 'sprinklerCompound'
    objectTypeId = 66
    cached_sprinkling = False
    agn_cache_file = None
    sne_cache_file = None
    defs_file = None
    sed_path = None

    def _final_pass(self, results):
        #From the original GalaxyTileCompoundObj final pass method
        for name in results.dtype.fields:
            if 'raJ2000' in name or 'decJ2000' in name:
                results[name] = np.radians(results[name])

        # the stored procedure on fatboy that queries the galaxies
        # constructs galtileid by taking
        #
        # tileid*10^8 + galid
        #
        # this causes galtileid to be so large that the uniqueIDs in the
        # Twinkles InstanceCatalogs are too large for PhoSim to handle.
        # Since Twinkles is only focused on one tile on the sky, we will remove
        # the factor of 10^8, making the uniqueIDs a more manageable size
        # results['galtileid'] = results['galtileid']#%100000000

        #Use Sprinkler now
        sp = sprinkler(results, self.mjd, self.specFileMap, self.sed_path,
                       density_param=1.0,
                       cached_sprinkling=self.cached_sprinkling,
                       agn_cache_file=self.agn_cache_file,
                       sne_cache_file=self.sne_cache_file,
                       defs_file=self.defs_file)
        results = sp.sprinkle()

        return results

class sprinkler():
    def __init__(self, catsim_cat, visit_mjd, specFileMap, sed_path,
                 om10_cat='twinkles_lenses_v2.fits',
                 sne_cat='dc2_sne_cat.csv', density_param=1., cached_sprinkling=False,
                 agn_cache_file=None, sne_cache_file=None, defs_file=None,
                 write_sn_sed=True):
        """
        Parameters
        ----------
        catsim_cat: catsim catalog
            The results array from an instance catalog.
        visit_mjd: float
            The mjd of the visit
        specFileMap:
            This will tell the instance catalog where to write the files
        sed_path: str
            This tells where to write out SNe SED files
        om10_cat: optional, defaults to 'twinkles_lenses_v2.fits
            fits file with OM10 catalog
        sne_cat: optional, defaults to 'dc2_sne_cat.csv'
        density_param: `np.float`, optioanl, defaults to 1.0
            the fraction of eligible agn objects that become lensed and should
            be between 0.0 and 1.0.
        cached_sprinkling: boolean
            If true then pick from a preselected list of galtileids
        agn_cache_file: str
        sne_cache_file: str
        defs_file: str
        write_sn_sed: boolean
            Controls whether or not to actually write supernova
            SEDs to disk (default=True)

        Returns
        -------
        input_catalog:
            results array with lens systems added.
        """
        t_start = time.time()
        twinklesDir = getPackageDir('Twinkles')
        om10_cat = os.path.join(twinklesDir, 'data', om10_cat)
        self.write_sn_sed = write_sn_sed
        self.catalog_column_names = catsim_cat.dtype.names
        # ****** THIS ASSUMES THAT THE ENVIRONMENT VARIABLE OM10_DIR IS SET *******
        lensdb = om10.DB(catalog=om10_cat, vb=False)
        self.lenscat = lensdb.lenses.copy()
        self.density_param = density_param
        self.bandpassDict = BandpassDict.loadTotalBandpassesFromFiles(bandpassNames=['i'])
        self.lsst_band_indexes = {'u':0, 'g':1, 'r':2, 'i':3, 'z':4, 'y':5}

        self.sne_catalog = pd.read_csv(os.path.join(twinklesDir, 'data', sne_cat))
        #self.sne_catalog = self.sne_catalog.iloc[:101] ### Remove this after testing
        self.used_systems = []
        self._visit_mjd = visit_mjd
        self.sn_obj = SNObject(0., 0.)
        self.write_dir = specFileMap.subdir_map['(^specFileGLSN)']
        self.sed_path = sed_path

        self.cached_sprinkling = cached_sprinkling
        if self.cached_sprinkling is True:
            if ((agn_cache_file is None) | (sne_cache_file is None)):
                raise AttributeError('Must specify cache files if using cached_sprinkling.')
            #agn_cache_file = os.path.join(twinklesDir, 'data', 'test_agn_galtile_cache.csv')
            self.agn_cache = pd.read_csv(agn_cache_file)
            #sne_cache_file = os.path.join(twinklesDir, 'data', 'test_sne_galtile_cache.csv')
            self.sne_cache = pd.read_csv(sne_cache_file)
        else:
            self.agn_cache = None
            self.sne_cache = None

        if defs_file is None:
            self.defs_file = os.path.join(twinklesDir, 'data', 'catsim_defs.csv')
        else:
            self.defs_file = defs_file

        self.sedDir = getPackageDir('sims_sed_library')

        self.imSimBand = Bandpass()
        self.imSimBand.imsimBandpass()
        #self.LRG_name = 'Burst.25E09.1Z.spec'
        #self.LRG = Sed()
        #self.LRG.readSED_flambda(str(galDir + self.LRG_name))
        #return

        #Calculate imsimband magnitudes of source galaxies for matching

        agn_fname = str(getPackageDir('sims_sed_library') + '/agnSED/agn.spec.gz')
        src_iband = self.lenscat['MAGI_IN']
        src_z = self.lenscat['ZSRC']
        self.src_mag_norm = []
        for src, s_z in zip(src_iband, src_z):
            agn_sed = Sed()
            agn_sed.readSED_flambda(agn_fname)
            agn_sed.redshiftSED(s_z, dimming=True)
            self.src_mag_norm.append(matchBase().calcMagNorm([src],
                                                             agn_sed,
                                                             self.bandpassDict))
        #self.src_mag_norm = matchBase().calcMagNorm(src_iband,
        #                                            [agn_sed]*len(src_iband),
        #
        #                                            self.bandpassDict)

        has_sn_truth_params = False
        for name in self.catalog_column_names:
            if 'sn_truth_params' in name:
                has_sn_truth_params = True
                break

        self.defs_dict = {}
        self.logging_is_sprinkled = False
        self.store_sn_truth_params = False
        with open(self.defs_file, 'r') as f:
            for line in f:
                line_defs = line.strip().split(',')
                if len(line_defs) > 1:
                    if 'is_sprinkled' in line_defs[1]:
                        self.logging_is_sprinkled = True
                    if 'sn_truth_params' in line_defs[1] and has_sn_truth_params:
                        self.store_sn_truth_params = True
                    if len(line_defs) == 2:
                        self.defs_dict[line_defs[0]] = line_defs[1]
                    else:
                        self.defs_dict[line_defs[0]] = tuple((ll for ll in line_defs[1:]))

        duration = time.time()-t_start
        duration /= 3600.0
        print('initialized sprinkler in %e hours' % duration)

    @property
    def visit_mjd(self):
        return self._visit_mjd

    @visit_mjd.setter
    def visit_mjd(self, val):
        self._visit_mjd = val

    def sprinkle(self, input_catalog, catalog_band):
        # Define a list that we can write out to a text file
        lenslines = []
        # For each galaxy in the catsim catalog
        if isinstance(self.defs_dict['galtileid'], tuple):
            galid_dex = self.defs_dict['galtileid'][0]
        else:
            galid_dex = self.defs_dict['galtileid']

        agn_magnorm_dex = self.defs_dict['galaxyAgn_magNorm']
        agn_magnorm_array  = np.array([row[agn_magnorm_dex] for row in input_catalog])
        nan_magnorm = np.isnan(agn_magnorm_array)

        if self.cached_sprinkling:
            if not hasattr(self, '_unq_agn_gid'):
                self._unq_agn_gid = np.unique(self.agn_cache['galtileid'].values)
                self._unq_sne_gid = np.unique(self.sne_cache['galtileid'].values)

            galtileid_array = np.array([row[galid_dex] for row in input_catalog])
            valid_agn = np.where(np.logical_and(np.logical_not(nan_magnorm),
                                                np.in1d(galtileid_array,
                                                        self._unq_agn_gid,
                                                        assume_unique=True)))[0]

            valid_sne = np.where(np.logical_and(nan_magnorm,
                                                np.in1d(galtileid_array,
                                                        self._unq_sne_gid,
                                                        assume_unique=True)))[0]
        else:
            valid_agn = np.where(np.logical_not(nan_magnorm))[0]
            valid_sne = np.where(nan_magnorm)[0]

        new_rows = []
        # print("Running sprinkler. Catalog Length: ", len(input_catalog))
        for rowNum in valid_agn:
            row = input_catalog[rowNum]
            galtileid = row[galid_dex]

            if not self.cached_sprinkling:
                candidates = self.find_lens_candidates(row[self.defs_dict['galaxyAgn_redshift']],
                                                       row[self.defs_dict['galaxyAgn_magNorm']])
                rng = np.random.RandomState(galtileid % (2**32 -1))
                pick_value = rng.uniform()

                if len(candidates) == 0 or pick_value>self.density_param:
                    # If there aren't any lensed sources at this redshift from
                    # OM10 move on the next object
                    continue

                # Randomly choose one the lens systems
                # (can decide with or without replacement)
                # Sort first to make sure the same choice is made every time

                candidates = candidates[np.argsort(candidates['twinklesId'])]
                newlens = rng.choice(candidates)
            else:
                twinkles_sys_cache = self.agn_cache.query('galtileid == %i' % galtileid)['twinkles_system'].values[0]
                newlens = self.lenscat[np.where(self.lenscat['twinklesId'] == twinkles_sys_cache)[0]][0]

            #varString = json.loads(row[self.defs_dict['galaxyAgn_varParamStr']])
            # varString[self.defs_dict['pars']]['t0_mjd'] = 59300.0
            #row[self.defs_dict['galaxyAgn_varParamStr']] = json.dumps(varString)

            # Append the lens galaxy
            # For each image, append the lens images
            default_lensrow = None
            if newlens['NIMG'] > 0:
                default_lensrow = row.copy()
                default_lensrow[self.defs_dict['galaxyDisk_majorAxis']] = 0.0
                default_lensrow[self.defs_dict['galaxyDisk_minorAxis']] = 0.0
                default_lensrow[self.defs_dict['galaxyDisk_positionAngle']] = 0.0
                default_lensrow[self.defs_dict['galaxyDisk_internalAv']] = 0.0
                default_lensrow[self.defs_dict['galaxyDisk_magNorm']] = 999.
                default_lensrow[self.defs_dict['galaxyDisk_sedFilename']] = None
                default_lensrow[self.defs_dict['galaxyBulge_majorAxis']] = 0.0
                default_lensrow[self.defs_dict['galaxyBulge_minorAxis']] = 0.0
                default_lensrow[self.defs_dict['galaxyBulge_positionAngle']] = 0.0
                default_lensrow[self.defs_dict['galaxyBulge_internalAv']] = 0.0
                default_lensrow[self.defs_dict['galaxyBulge_magNorm']] = 999.
                default_lensrow[self.defs_dict['galaxyBulge_sedFilename']] = None
                default_lensrow[self.defs_dict['galaxyBulge_redshift']] = newlens['ZSRC']
                default_lensrow[self.defs_dict['galaxyDisk_redshift']] = newlens['ZSRC']
                default_lensrow[self.defs_dict['galaxyAgn_redshift']] = newlens['ZSRC']

            for i in range(newlens['NIMG']):
                lensrow = default_lensrow.copy()
                # XIMG and YIMG are in arcseconds
                # raPhSim and decPhoSim are in radians
                # Shift all parts of the lensed object,
                # not just its agn part
                delta_dec = np.radians(newlens['YIMG'][i] / 3600.0)
                delta_ra = np.radians(newlens['XIMG'][i] / 3600.0)
                lens_ra = lensrow[self.defs_dict['raJ2000']]
                lens_dec = lensrow[self.defs_dict['decJ2000']]
                lensrow[self.defs_dict['raJ2000']] = lens_ra + delta_ra/np.cos(lens_dec)
                lensrow[self.defs_dict['decJ2000']] = lens_dec + delta_dec
                mag_adjust = 2.5*np.log10(np.abs(newlens['MAG'][i]))
                lensrow[self.defs_dict['galaxyAgn_magNorm']] -= mag_adjust
                varString = json.loads(lensrow[self.defs_dict['galaxyAgn_varParamStr']])
                varString[self.defs_dict['pars']]['t0Delay'] = newlens['DELAY'][i]
                varString[self.defs_dict['varMethodName']] = 'applyAgnTimeDelay'
                lensrow[self.defs_dict['galaxyAgn_varParamStr']] = json.dumps(varString)

                if self.logging_is_sprinkled:
                    lensrow[self.defs_dict['galaxyAgn_is_sprinkled']] = 1
                    lensrow[self.defs_dict['galaxyBulge_is_sprinkled']] = 1
                    lensrow[self.defs_dict['galaxyDisk_is_sprinkled']] = 1

                #To get back twinklesID in lens catalog from phosim catalog id number
                #just use np.right_shift(phosimID-28, 10). Take the floor of the last
                #3 numbers to get twinklesID in the twinkles lens catalog and the remainder is
                #the image number minus 1.
                if not isinstance(self.defs_dict['galtileid'], tuple):
                    lensrow[self.defs_dict['galtileid']] = ((lensrow[self.defs_dict['galtileid']]+int(1.5e10))*100000 +
                                                             newlens['twinklesId']*8 + i)
                else:
                    for col_name in self.defs_dict['galtileid']:

                        lensrow[col_name] = ((lensrow[col_name]+int(1.5e10))*100000 +
                                              newlens['twinklesId']*8 + i)

                new_rows.append(lensrow)

            #Now manipulate original entry to be the lens galaxy with desired properties
            #Start by deleting Disk and AGN properties
            if not np.isnan(row[self.defs_dict['galaxyDisk_magNorm']]):
                row[self.defs_dict['galaxyDisk_majorAxis']] = 0.0
                row[self.defs_dict['galaxyDisk_minorAxis']] = 0.0
                row[self.defs_dict['galaxyDisk_positionAngle']] = 0.0
                row[self.defs_dict['galaxyDisk_internalAv']] = 0.0
                row[self.defs_dict['galaxyDisk_magNorm']] = 999.
                row[self.defs_dict['galaxyDisk_sedFilename']] = None
            row[self.defs_dict['galaxyAgn_magNorm']] = None
            row[self.defs_dict['galaxyDisk_magNorm']] = 999.
            row[self.defs_dict['galaxyAgn_sedFilename']] = None
            #Now insert desired Bulge properties
            row[self.defs_dict['galaxyBulge_sedFilename']] = newlens['lens_sed']
            row[self.defs_dict['galaxyBulge_redshift']] = newlens['ZLENS']
            row[self.defs_dict['galaxyDisk_redshift']] = newlens['ZLENS']
            row[self.defs_dict['galaxyAgn_redshift']] = newlens['ZLENS']
            row_lens_sed = Sed()
            row_lens_sed.readSED_flambda(os.path.join(self.sedDir,
                                                      newlens['lens_sed']))

            row_lens_sed.redshiftSED(newlens['ZLENS'], dimming=True)
            # Get the correct magnorm to maintain galaxy colors
            row[self.defs_dict['galaxyBulge_magNorm']] = newlens['sed_magNorm'][self.lsst_band_indexes[catalog_band]]
            row[self.defs_dict['galaxyBulge_majorAxis']] = radiansFromArcsec(newlens['REFF'] / np.sqrt(1 - newlens['ELLIP']))
            row[self.defs_dict['galaxyBulge_minorAxis']] = radiansFromArcsec(newlens['REFF'] * np.sqrt(1 - newlens['ELLIP']))
            #Convert orientation angle to west of north from east of north by *-1.0 and convert to radians
            row[self.defs_dict['galaxyBulge_positionAngle']] = newlens['PHIE']*(-1.0)*np.pi/180.0
            row[self.defs_dict['galaxyBulge_internalAv']] = newlens['lens_av']
            row[self.defs_dict['galaxyBulge_internalRv']] = newlens['lens_rv']

            if self.logging_is_sprinkled:
                row[self.defs_dict['galaxyAgn_is_sprinkled']] = 1
                row[self.defs_dict['galaxyBulge_is_sprinkled']] = 1
                row[self.defs_dict['galaxyDisk_is_sprinkled']] = 1

            #Replace original entry with new entry
            input_catalog[rowNum] = row

        for rowNum in valid_sne:
            row = input_catalog[rowNum]
            galtileid = row[galid_dex]
            if self.cached_sprinkling is True:
                if galtileid in self.sne_cache['galtileid'].values:
                    use_system = self.sne_cache.query('galtileid == %i' % galtileid)['twinkles_system'].values
                    use_df = self.sne_catalog.query('twinkles_sysno == %i' % use_system)
                    self.used_systems.append(use_system)
                else:
                    continue
            else:
                lens_sne_candidates = self.find_sne_lens_candidates(row[self.defs_dict['galaxyDisk_redshift']])
                candidate_sysno = np.unique(lens_sne_candidates['twinkles_sysno'])
                num_candidates = len(candidate_sysno)
                if num_candidates == 0:
                    continue
                used_already = np.array([sys_num in self.used_systems for sys_num in candidate_sysno])
                unused_sysno = candidate_sysno[~used_already]
                if len(unused_sysno) == 0:
                    continue
                rng2 = np.random.RandomState(galtileid % (2**32 -1))
                use_system = rng2.choice(unused_sysno)
                use_df = self.sne_catalog.query('twinkles_sysno == %i' % use_system)

            default_lensrow = row.copy()
            default_lensrow[self.defs_dict['galaxyDisk_majorAxis']] = 0.0
            default_lensrow[self.defs_dict['galaxyDisk_minorAxis']] = 0.0
            default_lensrow[self.defs_dict['galaxyDisk_positionAngle']] = 0.0
            default_lensrow[self.defs_dict['galaxyDisk_internalAv']] = 0.0
            default_lensrow[self.defs_dict['galaxyDisk_magNorm']] = 999.
            default_lensrow[self.defs_dict['galaxyDisk_sedFilename']] = None
            default_lensrow[self.defs_dict['galaxyBulge_majorAxis']] = 0.0
            default_lensrow[self.defs_dict['galaxyBulge_minorAxis']] = 0.0
            default_lensrow[self.defs_dict['galaxyBulge_positionAngle']] = 0.0
            default_lensrow[self.defs_dict['galaxyBulge_internalAv']] = 0.0
            default_lensrow[self.defs_dict['galaxyBulge_magNorm']] = 999.
            default_lensrow[self.defs_dict['galaxyBulge_sedFilename']] = None
            varString = 'None'
            default_lensrow[self.defs_dict['galaxyAgn_varParamStr']] = varString

            for i in range(len(use_df)):
                lensrow = default_lensrow.copy()
                delta_ra = np.radians(use_df['x'].iloc[i] / 3600.0)
                delta_dec = np.radians(use_df['y'].iloc[i] / 3600.0)
                lens_ra = lensrow[self.defs_dict['raJ2000']]
                lens_dec = lensrow[self.defs_dict['decJ2000']]
                lensrow[self.defs_dict['raJ2000']] = lens_ra + delta_ra/np.cos(lens_dec)
                lensrow[self.defs_dict['decJ2000']] = lens_dec + delta_dec
                # varString = json.loads(lensrow[self.defs_dict['galaxyAgn_varParamStr']])
                z_s = use_df['zs'].iloc[i]
                lensrow[self.defs_dict['galaxyBulge_redshift']] = z_s
                lensrow[self.defs_dict['galaxyDisk_redshift']] = z_s
                lensrow[self.defs_dict['galaxyAgn_redshift']] = z_s
                #To get back twinklesID in lens catalog from phosim catalog id number
                #just use np.right_shift(phosimID-28, 10). Take the floor of the last
                #3 numbers to get twinklesID in the twinkles lens catalog and the remainder is
                #the image number minus 1.
                if not isinstance(self.defs_dict['galtileid'], tuple):
                   lensrow[self.defs_dict['galtileid']] = ((lensrow[self.defs_dict['galtileid']]+int(1.5e10))*100000 +
                                                            use_system*8 + i)
                else:
                    for col_name in self.defs_dict['galtileid']:
                        lensrow[col_name] = ((lensrow[col_name]+int(1.5e10))*100000 +
                                              use_system*8 + i)

                (add_to_cat, sn_magnorm,
                 sn_fname, sn_param_dict) = self.create_sn_sed(use_df.iloc[i],
                                                               lensrow[self.defs_dict['raJ2000']],
                                                               lensrow[self.defs_dict['decJ2000']],
                                                               self.visit_mjd,
                                                               write_sn_sed=self.write_sn_sed)

                if self.store_sn_truth_params:
                    add_to_cat = True
                    lensrow[self.defs_dict['galaxyAgn_sn_truth_params']] = json.dumps(sn_param_dict)
                    lensrow[self.defs_dict['galaxyAgn_sn_t0']] = sn_param_dict['t0']

                lensrow[self.defs_dict['galaxyAgn_sedFilename']] = sn_fname
                lensrow[self.defs_dict['galaxyAgn_magNorm']] = sn_magnorm #This will need to be adjusted to proper band
                mag_adjust = 2.5*np.log10(np.abs(use_df['mu'].iloc[i]))
                lensrow[self.defs_dict['galaxyAgn_magNorm']] -= mag_adjust

                if self.logging_is_sprinkled:
                    lensrow[self.defs_dict['galaxyAgn_is_sprinkled']] = 1
                    lensrow[self.defs_dict['galaxyBulge_is_sprinkled']] = 1
                    lensrow[self.defs_dict['galaxyDisk_is_sprinkled']] = 1

                if add_to_cat is True:
                    new_rows.append(lensrow)

                #Now manipulate original entry to be the lens galaxy with desired properties
                #Start by deleting Disk and AGN properties
            if not np.isnan(row[self.defs_dict['galaxyDisk_magNorm']]):
                row[self.defs_dict['galaxyDisk_majorAxis']] = 0.0
                row[self.defs_dict['galaxyDisk_minorAxis']] = 0.0
                row[self.defs_dict['galaxyDisk_positionAngle']] = 0.0
                row[self.defs_dict['galaxyDisk_internalAv']] = 0.0
                row[self.defs_dict['galaxyDisk_magNorm']] = 999.
                row[self.defs_dict['galaxyDisk_sedFilename']] = None
            row[self.defs_dict['galaxyAgn_magNorm']] = None
            row[self.defs_dict['galaxyDisk_magNorm']] = 999.
            row[self.defs_dict['galaxyAgn_sedFilename']] = None
            #Now insert desired Bulge properties
            row[self.defs_dict['galaxyBulge_sedFilename']] = use_df['lensgal_sed'].iloc[0]
            row[self.defs_dict['galaxyBulge_redshift']] = use_df['zl'].iloc[0]
            row[self.defs_dict['galaxyDisk_redshift']] = use_df['zl'].iloc[0]
            row[self.defs_dict['galaxyAgn_redshift']] = use_df['zl'].iloc[0]
            row[self.defs_dict['galaxyBulge_magNorm']] = use_df['lensgal_magnorm_%s' % catalog_band].iloc[0]
            # row[self.defs_dict['galaxyBulge_magNorm']] = matchBase().calcMagNorm([newlens['APMAG_I']], self.LRG, self.bandpassDict) #Changed from i band to imsimband
            row[self.defs_dict['galaxyBulge_majorAxis']] = radiansFromArcsec(use_df['lensgal_reff'].iloc[0] / np.sqrt(1 - use_df['e'].iloc[0]))
            row[self.defs_dict['galaxyBulge_minorAxis']] = radiansFromArcsec(use_df['lensgal_reff'].iloc[0] * np.sqrt(1 - use_df['e'].iloc[0]))
            #Convert orientation angle to west of north from east of north by *-1.0 and convert to radians
            row[self.defs_dict['galaxyBulge_positionAngle']] = use_df['theta_e'].iloc[0]*(-1.0)*np.pi/180.0
            row[self.defs_dict['galaxyBulge_internalAv']] = use_df['lens_av'].iloc[0]
            row[self.defs_dict['galaxyBulge_internalRv']] = use_df['lens_rv'].iloc[0]

            if self.logging_is_sprinkled:
                row[self.defs_dict['galaxyAgn_is_sprinkled']] = 1
                row[self.defs_dict['galaxyBulge_is_sprinkled']] = 1
                row[self.defs_dict['galaxyDisk_is_sprinkled']] = 1

            #Replace original entry with new entry
            input_catalog[rowNum] = row

        if len(new_rows)>0:
            input_catalog = np.append(input_catalog, new_rows)

        return input_catalog

    def find_lens_candidates(self, galz, gal_mag):
        # search the OM10 catalog for all sources +- 0.1 dex in redshift
        # and within .25 mags of the CATSIM source
        w = np.where((np.abs(np.log10(self.lenscat['ZSRC']) - np.log10(galz)) <= 0.1) &
                     (np.abs(self.src_mag_norm - gal_mag) <= .25))[0]
        lens_candidates = self.lenscat[w]

        return lens_candidates

    def find_sne_lens_candidates(self, galz):

        w = np.where((np.abs(np.log10(self.sne_catalog['zs']) - np.log10(galz)) <= 0.1))
        lens_candidates = self.sne_catalog.iloc[w]

        return lens_candidates

    def create_sn_sed(self, system_df, sn_ra, sn_dec, sed_mjd, write_sn_sed=True):

        sn_param_dict = copy.deepcopy(self.sn_obj.SNstate)
        sn_param_dict['_ra'] = sn_ra
        sn_param_dict['_dec'] = sn_dec
        sn_param_dict['z'] = system_df['zs']
        sn_param_dict['c'] = system_df['c']
        sn_param_dict['x0'] = system_df['x0']
        sn_param_dict['x1'] = system_df['x1']
        sn_param_dict['t0'] = system_df['t_start']
        #sn_param_dict['t0'] = 62746.27  #+1500. ### For testing only

        current_sn_obj = self.sn_obj.fromSNState(sn_param_dict)
        current_sn_obj.mwEBVfromMaps()
        wavelen_max = 1800.
        wavelen_min = 30.
        wavelen_step = 0.1
        sn_sed_obj = current_sn_obj.SNObjectSED(time=sed_mjd,
                                                wavelen=np.arange(wavelen_min, wavelen_max,
                                                                  wavelen_step))
        flux_500 = sn_sed_obj.flambda[np.where(sn_sed_obj.wavelen >= 499.99)][0]

        if flux_500 > 0.:
            add_to_cat = True
            sn_magnorm = current_sn_obj.catsimBandMag(self.imSimBand, sed_mjd)
            sn_name = None
            if write_sn_sed:
                sn_name = 'specFileGLSN_%i_%i_%.4f.txt' % (system_df['twinkles_sysno'],
                                                           system_df['imno'], sed_mjd)
                sed_filename = '%s/%s' % (self.sed_path, sn_name)
                sn_sed_obj.writeSED(sed_filename)
                with open(sed_filename, 'rb') as f_in, gzip.open(str(sed_filename + '.gz'), 'wb') as f_out:
                    shutil.copyfileobj(f_in, f_out)
                os.remove(sed_filename)
        else:
            add_to_cat = False
            sn_magnorm = np.nan
            sn_name = None

        return add_to_cat, sn_magnorm, sn_name, current_sn_obj.SNstate

    def update_catsim(self):
        # Remove the catsim object
        # Add lensed images to the catsim given source brightness and magnifications
        # Add lens galaxy to catsim
        return

    def catsim_to_phosim(self):
        # Pass this catsim to phosim to make images
        return


text:
import time
import heapq
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from vahun.neuroplot import DrawNN
import Levenshtein

class LogReader:
    def __init__(self,logdir="/mnt/store/velkey/logs",sep="\t"):
        self.logdir=logdir
        self.delimiter=sep
        self.lognames=self.read_all_log_folders()
        self.detaildata=[]
        self.traindata=[]
        self.fulltraineddata=[]
        self.accuracydata=[]
        for folder in self.lognames:
            if os.path.exists(self.logdir+"/"+folder+"/"+"details.log"):
                self.detaildata.append([folder]+self.read_linebyline(folder,"details.log"))
            else:
                self.detaildata.append(None)
                self.traindata.append(self.read_train(folder))
            if os.path.exists(self.logdir+"/"+folder+"/"+"full_trained.log"):
                self.fulltraineddata.append(self.read_linebyline(folder,"full_trained.log"))
            else:
                self.fulltraineddata.append(None)
            if os.path.exists(self.logdir+"/"+folder+"/"+"accuracy.log"):
                self.accuracydata.append(self.read_linebyline(folder,"accuracy.log"))
            else:
                self.accuracydata.append(None)

    def read_all_log_folders(self):
        lista=[]
        for filename in os.listdir(self.logdir):
            lista.append(filename)
        return lista
    def read_train(self,folder):
        train_logs=[]
        if os.path.exists(self.logdir+"/"+folder+"/"+"train.log"):
            with open(self.logdir+"/"+folder+"/"+"train.log") as f:
                for line in f:
                    if "START" in line:
                        temptrain=[]
                    elif "STOP" in line:
                        train_logs.append(temptrain)
                    else:
                        temptrain.append(line.rstrip('\n').split(self.delimiter))
        return train_logs
    def read_linebyline(self,folder,logname):
        logs=[]
        if os.path.exists(self.logdir+"/"+folder+"/"+logname):
            with open(self.logdir+"/"+folder+"/"+logname) as f:
                for line in f:
                    logs.append(line.rstrip('\n').split(self.delimiter))
        else:
            print("No logfile found")
        return logs

    def plot_fulltrains(self):
        for i in range(len(self.detaildata)):
            print(self.detaildata[i])
            lenlist=[]
            trainlist=[]
            testlist=[]
            sumlist=[]
            if self.fulltraineddata[i]!=None:
                for j in range(len(self.fulltraineddata[i])):
                    lenlist.append(len(self.fulltraineddata[i][j])-12)
                    trainlist.append(float(self.fulltraineddata[i][j][3]))
                    testlist.append(float(self.fulltraineddata[i][j][7]))
                    sumlist.append(sum(list(map(int, self.fulltraineddata[i][j][12:]))))
                plt.plot(lenlist,trainlist,'ro',lenlist,testlist,'g^')

                plt.show()
    def plot_accuracy(self,filter="",plot=True,draw=True):
        for i in range(len(self.detaildata)):

            lenlist=[]
            trainlist=[]
            testlist=[]
            sumlist=[]
            wordlist=[]
            max_accuracy=0
            if self.accuracydata[i]!=None and filter in self.detaildata[i][6][1]:
                print(self.detaildata[i])
                for j in range(len(self.accuracydata[i])):
                    lenlist.append(len(self.accuracydata[i][j])-12)
                    trainlist.append(float(self.accuracydata[i][j][3]))
                    testlist.append(float(self.accuracydata[i][j][7]))
                    wordlist.append(float(self.accuracydata[i][j][8]))
                    sumlist.append(sum(list(map(int, self.accuracydata[i][j][12:]))))
                    if testlist[-1]>max_accuracy:
                        max_accuracy=testlist[-1]
                        best_config=list(map(int, self.accuracydata[i][j][12:]))
                if plot:
                    plt.plot(sumlist,trainlist,'ro',sumlist,testlist,'g^',sumlist,wordlist,'bx')
                print("Max test accuracy:",max_accuracy,
                     "\nMax train accuracy:",max(trainlist),
                     "\nWith config: ",best_config)
                if plot:
                    plt.show()
                if draw:
                    DrawNN([int(best_config[-1]/10)]+[int(item/10) for item in best_config]).draw()
    def get_full_table(self):
        lenlist=[]
        uniq=[]
        names=[]
        trainlist=[]
        testlist=[]
        configs=[]
        testwordlist=[]
        wordnum=[]
        typelist=[]
        encodings=[]
        validlist=[]
        levenshteins_tr=[]
        levenshteins_te=[]
        levenshteins_v=[]
        max_accuracy=0
        for i in range(len(self.detaildata)):
            if self.accuracydata[i]!=None:
                for j in range(len(self.accuracydata[i])):
                    names.append(self.detaildata[i][0])
                    wordnum.append(self.detaildata[i][3][1])

                    uniq.append("uniq" in self.detaildata[i][6][1] or "uniq" in self.detaildata[i][0])
                    typelist.append("var" in self.detaildata[i][6][1] or "var" in self.detaildata[i][0])
                    lenlist.append(len(self.accuracydata[i][j])-12)
                    trainlist.append(float(self.accuracydata[i][j][3]))
                    validlist.append(float(self.accuracydata[i][j][5]))
                    testlist.append(float(self.accuracydata[i][j][7]))
                    testwordlist.append(float(self.accuracydata[i][j][8]))
                    levenshteins_tr.append(float(self.accuracydata[i][j][9]))
                    levenshteins_v.append(float(self.accuracydata[i][j][10]))
                    levenshteins_te.append(float(self.accuracydata[i][j][11]))
                    configs.append(list(map(int, self.accuracydata[i][j][12:])))
                    encodings.append(min(configs[-1]))

        dt = [('Experiment', names),
         ('Encoded_len',encodings),
         ('Uniq_words', wordnum),
         ('Variational',typelist),
         ('Uniq',uniq),
         ('Layernum', lenlist),
         ('Train_char_acc', trainlist),
         ('Valid_char_acc', validlist),
         ('Test_char_acc', testlist),
         ('Test_word_acc', testwordlist),
         ('Test_Leven_avg', levenshteins_te),
         ('Train_Leven_avg', levenshteins_tr),
         ('Valid_Leven_avg', levenshteins_v),
         ('Layers', configs)]

        return pd.DataFrame.from_items(dt)


text:
import uasyncio as asyncio
import utime
import io
MP_STREAM_POLL_RD = const(1)
MP_STREAM_POLL = const(3)
MP_STREAM_ERROR = const(-1)

class MillisecTimer(io.IOBase):
    def __init__(self):
        self.end = 0
        self.sreader = asyncio.StreamReader(self)

    def __iter__(self):
        await self.sreader.readline()

    def __call__(self, ms):
        self.end = utime.ticks_add(utime.ticks_ms(), ms)
        return self

    def readline(self):
        return b'\n'

    def ioctl(self, req, arg):
        ret = MP_STREAM_ERROR
        if req == MP_STREAM_POLL:
            ret = 0
            if arg & MP_STREAM_POLL_RD:
                if utime.ticks_diff(utime.ticks_ms(), self.end) >= 0:
                    ret |= MP_STREAM_POLL_RD
        return ret


text:
<?php
return array (
  'Delete space' => 'Space löschen',
  'Are you sure, that you want to delete this space? All published content will be removed!' => 'Bist Du sicher, dass du diesen Space löschen möchtest? Alle veröffentlichten Inhalte werden entfernt!',
  'Delete' => 'Löschen',
  'Please provide your password to continue!' => 'Bitte gib dein Passwort ein, um fortzufahren!',
);


text:
using System;

namespace AmplifyShaderEditor
{
	[Serializable]
	[NodeAttributes( "Exp2", "Operators", "Base-2 exponential of scalars and vectors" )]
	public sealed class Exp2OpNode : SingleInputOp
	{
		protected override void CommonInit( int uniqueId )
		{
			base.CommonInit( uniqueId );
			m_opName = "exp2";
			m_previewShaderGUID = "ceb70ed5423a36647a504a41de7dbfe6";
			m_inputPorts[ 0 ].CreatePortRestrictions(	WirePortDataType.OBJECT,
														WirePortDataType.FLOAT,
														WirePortDataType.FLOAT2,
														WirePortDataType.FLOAT3,
														WirePortDataType.FLOAT4,
														WirePortDataType.COLOR,
														WirePortDataType.INT );
		}
	}
}


text:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="about">about</string>
    <!-- Keys -->
    <string name="hourglass_key_analog">ANALOG_WATCHFACE</string>

    <!-- Debugging -->
    <string name="hourglass_debug_mode">DEBUG_MODE</string>
    <string name="hourglass_ambient">DEBUG_AMBIENT_MODE</string>
    <string name="hourglass_debug_date">DEBUG_DATE</string>
    <string name="hourglass_debug_date_year">DEBUG_YR</string>
    <string name="hourglass_debug_date_month">DEBUG_MN</string>
    <string name="hourglass_debug_date_date">DEBUG_DT</string>
    <string name="hourglass_debug_time">DEBUG_TIME</string>
    <string name="hourglass_debug_time_hour">DEBUG_HOUR</string>
    <string name="hourglass_debug_time_minute">DEBUG_MIN</string>
    <string name="hourglass_debug_time_second">DEBUG_SEC</string>

    <!-- Settings -->
    <string name="hourglass_analog_smooth_scroll">SMOOTH_SCROLL</string>
    <string name="hourglass_analog_smooth_scroll_title">Smooth Seconds Hand</string>
    <string name="hourglass_date_format">DATE_FORMAT</string>
    <string name="hourglass_time_format">TIME_FORMAT</string>

    <string name="hourglass_date_date1">dd</string>
    <string name="hourglass_date_date2">EEEE</string>
    <string name="hourglass_date_date3">EEE dd</string>
    <string name="hourglass_date_date4">MMM dd</string>
    <string name="hourglass_date_date5">MMMM dd</string>
    <string name="hourglass_date_date6">dd MMMM</string>
    <string name="hourglass_date_date7">EEE, MMMM dd</string>
    <string name="hourglass_date_date8">EEE, dd MMMM</string>
    <string name="hourglass_date_date9">EEE, dd MMMM yyyy</string>
    <string name="hourglass_date_date10">MM/dd/yy</string>
    <string name="hourglass_date_date11">dd/MM/yy</string>
    <string-array name="hourglass_date_format_list">
        <item>@string/hourglass_date_date1</item>
        <item>@string/hourglass_date_date2</item>
        <item>@string/hourglass_date_date3</item>
        <item>@string/hourglass_date_date4</item>
        <item>@string/hourglass_date_date5</item>
        <item>@string/hourglass_date_date6</item>
        <item>@string/hourglass_date_date7</item>
        <item>@string/hourglass_date_date8</item>
        <item>@string/hourglass_date_date9</item>
        <item>@string/hourglass_date_date10</item>
        <item>@string/hourglass_date_date11</item>
    </string-array>

    <string name="hourglass_time_time1">hh:mm:ss</string>
    <string name="hourglass_time_time2">hh:mm</string>
    <string name="hourglass_time_time3">kk:mm:ss</string>
    <string name="hourglass_time_time4">kk:mm</string>
    <string name="hourglass_time_time5">hh:mmaa</string>
    <string-array name="hourglass_time_format_list">
        <item>@string/hourglass_time_time1</item>
        <item>@string/hourglass_time_time2</item>
        <item>@string/hourglass_time_time3</item>
        <item>@string/hourglass_time_time4</item>
        <item>@string/hourglass_time_time5</item>
    </string-array>

    <string name="hourglass_message_mobile_resume">mobile-on</string>
    <string name="hourglass_message_mobile_stop">mobile-off</string>

    <string name="hourglass_data_phone_battery">PBATTERY</string>
</resources>


text:
package com.infinityraider.adventurersartifacts.artifacts.mantastyle;

import com.infinityraider.infinitylib.network.MessageBase;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import net.minecraftforge.fml.relauncher.Side;

public class MessageTrackPlayerUpdate extends MessageBase<IMessage> {
    private double deltaX;
    private double deltaY;
    private double deltaZ;

    public MessageTrackPlayerUpdate() {
        super();
    }

    public MessageTrackPlayerUpdate(double deltaX, double deltaY, double deltaZ) {
        this();
        this.deltaX = deltaX;
        this.deltaY = deltaY;
        this.deltaZ = deltaZ;
    }

    @Override
    public Side getMessageHandlerSide() {
        return Side.SERVER;
    }

    @Override
    protected void processMessage(MessageContext ctx) {
        if(ctx.getServerHandler().playerEntity != null) {
            EntityReplicate.updatePlayerMovement(ctx.getServerHandler().playerEntity, this.deltaX, this.deltaY, this.deltaZ);
        }
    }

    @Override
    protected IMessage getReply(MessageContext ctx) {
        return null;
    }
}


text:
rsnapshot
=========

This role installs and configures the backup utility `rsnapshot` and an optional NFS share.

Requirements
------------

None.

Role Variables
--------------

The default backup directory (where backups are stored) can be overwritten with:

```yaml
backup_directory: /mnt/backup
```

There are default retain counters available:

```yaml
backup_retain_hourly: 24
backup_retain_daily: 7
backup_retain_weekly: 4
backup_retain_monthly: 12
```

The list of backup sources is defined by:

```yaml
backup_list:
  - /home/
  - /etc/
  - /usr/local/
  - /var/lib/unifi/

```

If you want to backup to an NFS server set the following variables:

```yaml
# Backup NFS share.
backup_nfs_share: ''
backup_nfs_mount: '{{ backup_directory }}'
```

Dependencies
------------

* apt

Example Playbook
----------------

```yaml
---
    - hosts: all
      roles:
         - backup
```

License
-------

MIT

Author Information
------------------

* confirm IT solutions, Dominique Barton


text:
<?php

namespace Eschmar;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Parser;
use Ifsnop\Mysqldump as IMysqldump;
use Aws\S3\S3Client;

/**
 * Command for dumping a mysql database and writing it to Amazon S3.
 *
 * @author Marcel Eschmann
 **/
class S3DumpCommand extends Command
{
    /**
     * Config file name.
     *
     * @var string
     **/
    protected $config_file = 's3dump.yml';

    /**
     * Default timestamp format for filenames.
     *
     * @var string
     **/
    protected $timestamp_format = 'Y-m-d_H-i-s';

    /**
     * Default local dump target location.
     *
     * @var string
     **/
    protected $dump_dir = 'dumps/';

    /**
     * Bucket target location.
     *
     * @var string
     **/
    protected $bucket_dir;

    /**
     * Configuration read from config file.
     *
     * @var array
     **/
    protected $config;

    /**
     * CLI Output.
     *
     * @var OutputInterface
     **/
    protected $output;

    /**
     * Queue of databases to dump.
     *
     * @var array
     **/
    protected $queue;

    /**
     * Command input configuration.
     *
     * @return void
     * @author Marcel Eschmann
     **/
    protected function configure()
    {
        $this
            ->setName('dump')
            ->setDescription('Dumps a MySQL database and writes it to Amazon S3')
            ->addArgument('config', InputArgument::OPTIONAL, 'Use this yaml config file.')
            ->addArgument('location', InputArgument::OPTIONAL, 'Write dumps to this directory (with trailing slash).')
            ->addOption(
               'skip-s3',
               null,
               InputOption::VALUE_NONE,
               'If set, the dump will not be uploaded to Amazon S3 and remain in the target directory.'
            )
            ->addOption(
               'bucket-dir',
               null,
               InputOption::VALUE_OPTIONAL,
               'Where should the dumps be stored (e.g. "dumps/")?',
               ''
            )
        ;
    }

    /**
     * Command execution.
     *
     * @return void
     * @author Marcel Eschmann
     **/
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->output = $output;
        $this->bucket_dir = $input->getOption('bucket-dir');
        $output->writeln("");

        // make sure the bucket dir has a trailing slash
        if ($this->bucket_dir != '') {
            $this->bucket_dir = rtrim($this->bucket_dir, '/\\') . '/';
        }

        if ($input->getArgument('config')) {
            $this->config_file = $input->getArgument('config');
        }

        if (!file_exists($this->config_file)) {
            $output->writeln(" \033[1;31m[ERROR]: No {$this->config_file} file found.\033[0;0m");
            return;
        }

        try {
            $yaml = new Parser();
            $this->config = $yaml->parse(file_get_contents($this->config_file));
        } catch (\Exception $e) {
            $output->writeln(" \033[1;31m[ERROR]: Not able to parse {$this->config_file}. Please check for syntax errors.\033[0;0m");
            return;
        }

        // check database configuration
        if (!$this->generateQueue()) {
            $output->writeln(" \033[1;31m[ERROR]: Invalid database configuration. Check {$this->config_file}.\033[0;0m");
            return;
        }

        // check amazon s3 configuration
        $s3 = !$input->getOption('skip-s3');
        if ($s3 && (!isset($this->config['aws']['s3']) || !$this->checkForKeys($this->config['aws']['s3'], array('key', 'secret', 'bucket')))) {
            $output->writeln(" \033[1;31m[ERROR]: Invalid amazon s3 configuration. Check {$this->config_file}.\033[0;0m");
            return;
        }

        if ($input->getArgument('location')) {
            $this->dump_dir = $input->getArgument('location');
        }

        // check if target directory exists
        if (!is_dir($this->dump_dir)) {
            mkdir($this->dump_dir, 0777, true);
        }

        // execute dump for each database
        foreach ($this->queue as $db) {
            $this->dump($db, $s3);
        }

        // delete temporary dumps directory
        if ($s3 && !$this->destroyDir($this->dump_dir)) {
            $output->writeln(" \033[1;31m[ERROR]: Unable to delete temporary folder.\033[0;0m");
        }
    }

    /**
     * Parses the yaml database config and fills the queue.
     *
     * @return boolean
     * @author Marcel Eschmann
     **/
    protected function generateQueue()
    {
        if (!isset($this->config['database'])) {
            return false;
        }

        foreach ($this->config['database'] as $config) {
            if (!$this->checkForKeys($config, array('user', 'password', 'name'))) {
                return false;
            }

            $this->queue[] = $config;
        }

        return true;
    }

    /**
     * Batch check for array keys.
     *
     * @return boolean
     * @author Marcel Eschmann
     **/
    protected function checkForKeys($haystack, $needles)
    {
        foreach ($needles as $needle) {
            if (!array_key_exists($needle, $haystack)) {
                return false;
            }
        }

        return true;
    }

    /**
     * Deletes a folder and all of its contents
     *
     * @return boolean
     * @author Marcel Eschamnn
     **/
    protected function destroyDir($path)
    {
        if (!is_dir($path)) {
            return false;
        }

        try {
            $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::CHILD_FIRST);
            foreach ($files as $file) {
                if (!in_array($file->getBasename(), ['.', '..'])) {
                    if ($file->isDir()) {
                        $this->destroyDir($file->getPathname());
                    }else {
                        unlink($file->getPathname());
                    }
                }
            }
        } catch (\Exception $e) {
            return false;
        }

        return rmdir($path);
    }

    /**
     * Dumps a single database.
     *
     * @return boolean
     * @author Marcel Eschmann
     **/
    protected function dump($db, $s3 = true)
    {
        $start = time();

        // generate filename
        $now = new \DateTime();
        $filename = $db['name'] . '_' . $now->format($this->timestamp_format) . '.sql.gz';

        // execute mysql dump
        try {
            $dump = new IMysqldump\Mysqldump(
                $db['name'],
                $db['user'],
                $db['password'],
                'localhost',
                'mysql',
                ['compress' => 'Gzip']
            );

            $dump->start($this->dump_dir . $filename);

        } catch (\Exception $e) {
            $this->output->writeln(" \033[1;31m[ERROR]: " . $e->getMessage() . ".\033[0;0m");
            return false;
        }

        $end = time();
        $elapsed = $end-$start;
        $this->output->writeln("\033[0;0m ...generated \033[36m{$this->dump_dir}$filename\033[0;0m in \033[36m$elapsed\033[0;0m seconds.");

        // write dump to amazon s3 if chosen
        if ($s3) {
            if (!is_file($this->dump_dir.$filename)) {
                $this->output->writeln(" \033[1;31m[ERROR]: Unable to find dumped file.\033[0;0m");
                return false;
            }

            try {
                $client = S3Client::factory(array(
                    'key'    => $this->config['aws']['s3']['key'],
                    'secret' => $this->config['aws']['s3']['secret']
                ));

                $result = $client->putObject([
                    'Key' => $this->bucket_dir . $db['name'] . '/' . $filename,
                    'Bucket' => $this->config['aws']['s3']['bucket'],
                    'Body' => fopen($this->dump_dir.$filename, 'r'),
                    'ContentType' => 'application/gzip'
                ]);

                $this->output->writeln("\033[0;0m ...wrote \033[36m$filename\033[0;0m to \033[36mS3\033[0;0m.");

            } catch (\Exception $e) {
                $this->output->writeln(" \033[1;31m[AWS ERROR]: " . $e->getMessage());
                $this->output->writeln(" \033[36mSkipping S3...\033[0;0m");
            }
        }
    }

} // END class S3DumpCommand extends Command


text:
retries=5
hyperkube_image="{{ .Config.HyperkubeImage.RepoWithTag }}"
my_kubernetes_version="{{ .Config.HyperkubeImage.Tag }}"
myhostname=$(hostname -f)
disable_webhooks="{{ .Values.disableWebhooks }}"
webhooks_save_path="/srv/kubernetes"

kubectl() {
    /usr/bin/docker run -i --rm -v /etc/kubernetes:/etc/kubernetes:ro -v ${webhooks_save_path}:${webhooks_save_path}:rw --net=host ${hyperkube_image} /hyperkube kubectl --kubeconfig=/etc/kubernetes/kubeconfig/admin.yaml "$@"
}

kubectl_with_retries() {
  local tries=0
  local result_text=""
  local return_code=0

  while [ "$tries" -lt "$retries" ]; do
    result_text=$(kubectl "$@")
    return_code=$?
    if [ "$return_code" -eq "0" ]; then
      echo "${result_text}"
      break
    fi
    sleep 10
    tries=$((tries+1))
  done
  return $return_code
}

log() {
  echo "$@" >&2
}

get_masters() {
  kubectl get nodes -l kubernetes.io/role=master --no-headers -o custom-columns=NAME:metadata.name,VERSION:status.nodeInfo.kubeletVersion | awk '{printf "%s:%s\n", $1, $2}'
}

valid_version() {
  match=$(echo $1 | awk -e '(/^v[0-9]+\.[0-9]+\.[0-9]+/){print "match"}')
  [[ "$match" == "match" ]]
}

version_jumps() {
  # only a minor release change is NOT a version jump
  if [[ "${1%.*}" != "${2%.*}" ]]; then
    return 0
  fi
  return 1
}

# stop a controller by writing a special kube-aws disable service configmap
disable_controller() {
  local controller=$1
  local version=$2

  local request="$(cat <<EOT
apiVersion: v1
kind: ConfigMap
metadata:
  name: kube-aws-migration-disable-${controller}
  namespace: kube-system
data:
  kubernetesVersion: ${version}
  disable: "kube-controller-manager kube-scheduler kube-apiserver"
EOT
)"

  log "Creating disable service configmap kube-system/kube-aws-migration-disable-${controller}"
  echo "${request}" | kubectl_with_retries -n kube-system apply -f - || return 1
  return 0
}

find_pod() {
  local name=$1
  local host=$2

  kubectl -n kube-system get pod "${name}-${host}" --no-headers -o wide --ignore-not-found
}

node_running() {
  local node=$1

  ready=$(kubectl -n kube-system get node "${node}" --no-headers --ignore-not-found | awk '{print $2}')
  if [[ "${ready}" == "Ready" ]]; then
    return 0
  fi

  return 1
}

wait_stopped_or_timeout() {
  local controllers=$1
  log ""
  log "WAITING FOR ALL MATCHED CONTROLLERS TO STOP:-"
  log "${controllers}"
  log ""
  local max_wait=300
  local wait=0

  local test=1
  while [ "$test" -eq "1" ]; do
    test=0

    for cont in $controllers; do
      if node_running $cont; then
        test=1
      fi
    done

    if [ "$test" -eq "1" ]; then
      if [[ "${wait}" -ge "${max_wait}" ]]; then
        log "Wait for controllers timed out after ${wait} seconds."
        break
      fi
      log "Controllers still active, waiting 5 seconds..."
      wait=$[$wait+5]
      sleep 5
    else
      log "All target controllers are now inactive."
    fi
  done
}

save_webhooks() {
  local type=$1
  local file=$2

  echo "Storing and removing all ${type} webhooks"
  if [[ -s "${file}.index" ]]; then
    echo "${file}.index already saved"
  else
    local hooks=$(kubectl get ${type}webhookconfigurations -o custom-columns=NAME:.metadata.name --no-headers)
    local count=$(echo "${hooks}" | wc -w | sed -e 's/ //g')
    echo "Found ${count} ${type} webhooks..."
    if [[ -n "${hooks}" ]]; then
      echo -n "${hooks}" >${file}.index
      for h in ${hooks}; do
        echo "backing up ${type} webhook ${h}..."
        kubectl get ${type}webhookconfiguration ${h} -o yaml --export >${file}.${type}.${h}.yaml
        echo "deleting $type webhook ${h}..."
        ensuredelete ${file}.${type}.${h}.yaml
      done
    fi
  fi
}

ensuredelete() {
  kubectl delete --cascade=true --ignore-not-found=true -f $(echo "$@" | tr ' ' ',')
}

# MAIN

if ! $(valid_version ${my_kubernetes_version}); then
  log "My kubernetes version ${my_kubernetes_version} is invalid - aborting!"
  exit 1
fi

while ! kubectl get ns kube-system; do
  echo "waiting for apiserver to be available..."
  sleep 3
done

# Disable all mutating and validating webhooks because they can interfere with the stack migration)
if [[ "${disable_webhooks}" == "true" ]]; then
  echo "Storing and removing all validating and mutating webhooks..."
  mkdir -p ${webhooks_save_path}
  save_webhooks validating ${webhooks_save_path}/validating_webhooks
  save_webhooks mutating ${webhooks_save_path}/mutating_webhooks
fi

log ""
log "CHECKING CONTROLLER VERSIONS..."
log ""
found=""
for controller in $(get_masters); do
  controller_name=$(echo "${controller%%:*}")
  controller_version=$(echo "${controller##*:}")
  if [[ "${controller_name}" != "$myhostname" ]]; then
    if ! $(valid_version ${controller_version}); then
      log "Controller ${controller_name} has an invalid version number ${controller_version}!"
      continue
    fi

    if $(version_jumps ${my_kubernetes_version} ${controller_version}); then
      log "Detected a version jump on ${controller_name}: my version is ${my_kubernetes_version} and theirs is ${controller_version}"
      log "Disabling kube-apiserver, kube-scheduler and kube-controller-manager..."
      if [[ -z "${found}" ]]; then
        found="${controller_name}"
      else
        found="${found} ${controller_name}"
      fi
      disable_controller ${controller_name} ${controller_version}
    else
      log "No version jump on ${controller_name}: my version is ${my_kubernetes_version} and theirs is ${controller_version}"
    fi
  fi
done

if [[ -n "${found}" ]]; then
    log ""
    log "WAITING FOR FOUND CONTROLLERS TO STOP..."
    log ""
    wait_stopped_or_timeout "${found}"
fi
exit 0


text:
class PathTask : public Task {
public:
    PathTask();
    virtual ~PathTask();
    PathTask(Model* cameraModel, TurnTask* turnTask, SpeedTask* speedTask);

    void execute();

private:
    Logger* logger = new Logger("PathTask");

    CameraModel* cameraModel;
    TurnTask* turnTask;
    SpeedTask* speedTask;

    bool moving;
    bool done;

    float forwardSpeed;

    // distance in pixels considered to be in line with the sub
    float alignThreshold;
    int imgWidth, imgHeight;

    // Helper functions
    void setSpeed(float amount);
    void stop();
    void rotate(float amount);
    void moveTo(cv::Point2f pos);
};

#endif // PATHTASK_H


text:
class Binding
  def source_location
    eval '[__FILE__, __LINE__.to_i]'
  end unless method_defined?(:source_location)
end


text:
// Type definitions for dashdash 1.14
// Project: https://github.com/trentm/node-dashdash#readme
// Definitions by: Adam Voss <https://github.com/adamvoss>
//                 Piotr Błażejewicz <https://github.com/peterblazejewicz>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

export class Parser {
    /**  Allow interspersed arguments. @default true */
    interpersed: boolean;

    /** Don't allow unknown flags. @default true */
    allowUnknown: boolean;

    constructor(config: ParserConfiguration);

    /**  Return a string suitable for a Bash completion file for this tool. */
    bashCompletion(args: BashCompletionConfiguration): string;

    /**
     * Return help output for the current options.
     *
     * E.g.: if the current options are:
     *      [{names: ['help', 'h'], type: 'bool', help: 'Show help and exit.'}]
     * then this would return:
     *      '  -h, --help     Show help and exit.\n'
     */
    help(config?: HelpConfiguration): string;

    /** Parse options from the given argv. */
    parse(inputs?: string[] | ParsingConfiguration): Results;
}

/**
 * Add a new option type.
 */
export function addOptionType(optionType: OptionType): void;

export function bashCompletionFromOptions(args: BashCompletionConfiguration): string;

export function bashCompletionSpecFromOptions(args: BashCompletionSpecConfiguration): string;

export function createParser(config: ParserConfiguration): Parser;

export function getOptionType(name: string): OptionType;

/**
 * Parse argv with the given options.
 *
 * @param config A merge of all the available fields from
 *      `dashdash.Parser` and `dashdash.Parser.parse`: options, interspersed,
 *      argv, env, slice.
 */
export function parse(config: ParsingConfiguration): Results;

export interface Results {
    [key: string]: any;
    _order: Arg[];
    _args: string[];
}

export interface Arg {
    name: string;
    value: any;
    from: string;
}

/**
 * Return a synopsis string for the given option spec.
 *
 * Examples:
 *      > synopsisFromOpt({names: ['help', 'h'], type: 'bool'});
 *      '[ --help | -h ]'
 *      > synopsisFromOpt({name: 'file', type: 'string', helpArg: 'FILE'});
 *      '[ --file=FILE ]'
 */
export function synopsisFromOpt(o: Option): string;

export type Option = OptionWithoutAliases | OptionWithAliases;

export interface ParsingConfiguration {
    /**
     * The argv to parse. Defaults to `process.argv`.
     */
    argv?: string[] | undefined;

    /**
     * The index into argv at which options/args begin.  Default is 2, as appropriate for `process.argv`.
     */
    slice?: number | undefined;

    /**
     * The env to use for 'env' entries in the option specs. Defaults to `process.env`.
     */
    env?: any; // NodeJS.ProcessEnv;

    options?: Array<Option | Group> | undefined;
}

export interface ParserConfiguration {
    /**
     * Array of option specs.
     */
    options: Array<Option | Group>;

    /**
     * Whether to throw on unknown options.
     * If false, then unknown args are included in the _args array.
     * Default: false
     */
    allowUnknown?: boolean | undefined;

    /**
     * Whether to allow interspersed arguments (non-options) and options.
     *
     * E.g.:
     *   node tool.js arg1 arg2 -v
     *
     * '-v' is after some args here. If `interspersed: false` then '-v'
     *  would not be parsed out. Note that regardless of `interspersed`
     * the presence of '--' will stop option parsing, as all good
     * option parsers should.
     *
     * Default: true
     */
    interspersed?: boolean | undefined;
}

export interface OptionWithoutAliases extends OptionBase {
    /**
     * The option name
     */
    name: string;
}

export interface OptionWithAliases extends OptionBase {
    /**
     * The option name and aliases. The first name (if more than one given) is the key for the parsed `opts` object.
     */
    names: string[];
}

export interface OptionBase {
    /**
     * One of: bool, string, number, integer, positiveInteger, arrayOfBool, arrayOfString,
     * arrayOfNumber, arrayOfInteger, arrayOfPositiveInteger, arrayOfDate,
     * date (epoch seconds, e.g. 1396031701, or ISO 8601 format YYYY-MM-DD[THH:MM:SS[.sss][Z]], e.g. "2014-03-28T18:35:01.489Z").
     * You can add your own custom option types with `dashdash.addOptionType`
     * These names attempt to match with asserts on `assert-plus`.
     */
    type: string;

    /**
     * This is used for Bash completion for an option argument.
     * If not specified, then the value of type is used. Any string may be specified, but only the following values have meaning:
     *  - none: Provide no completions.
     *  - file: Bash's default completion (i.e. complete -o default), which includes filenames.
     *  - Any string FOO for which a function complete_FOO Bash function is defined.
     * This is for custom completions for a given tool.
     * Typically these custom functions are provided in the specExtra argument to dashdash.bashCompletionFromOptions().
     */
    completionType?: string | undefined;

    /**
     * An environment variable name (or names) that can be used as a fallback for this option.
     * An environment variable is only used as a fallback, i.e. it is ignored if the associated option is given in `argv`.
     */
    env?: string | string[] | undefined;

    /**
     * Used for parser.help() output.
     */
    help?: string | undefined;

    /**
     * Used in help output as the placeholder for the option argument.
     */
    helpArg?: string | undefined;

    /**
     * Set this to false to have that option's help not be text wrapped in <parser>.help() output.
     */
    helpWrap?: boolean | undefined;

    /**
     * A default value used for this option, if the option isn't specified in argv.
     */
    default?: string | undefined;

    /**
     * If true, help output will not include this option.
     */
    hidden?: boolean | undefined;
}

export interface Group {
    group: string;
}

export interface OptionType {
    name: string;
    /**
     * Whether this type of option takes an
     * argument on process.argv. Typically this is true for all but the
     * "bool" type.
     */
    takesArg: boolean;
    /**
     * Required iff `takesArg === true`. The string to show in generated help for options of this type.
     */
    helpArg?: string | undefined;
    /**
     * parser that takes a string argument and returns an instance of the
     * appropriate type, or throws an error if the arg is invalid.
     */
    parseArg(option: Option, optstr: string, arg: string): any;
    /**
     * Set to true if this is an 'arrayOf' type
     * that collects multiple usages of the option in process.argv and puts results in an array.
     */
    array?: boolean | undefined;
    arrayFlatten?: boolean | undefined;
    /**
     * Default value for options of this type, if no default is specified in the option type usage.
     */
    default?: any;
    completionType?: any;
}

export interface BashCompletionConfiguration {
    /**
     * The tool name.
     */
    name: string;

    /**
     * The array of dashdash option specs.
     */
    options?: Array<Option | Group> | undefined;

    /**
     * Extra Bash code content to add
     * to the end of the "spec". Typically this is used to append Bash
     * "complete_TYPE" functions for custom option types.
     */
    specExtra?: string | undefined;

    /**
     * Array of completion types for positional args (i.e. non-options).
     * If not given, positional args will use Bash's 'default' completion.
     */
    argtypes?: string[] | undefined;
}

export interface BashCompletionSpecConfiguration {
    /**
     * The array of dashdash option specs.
     */
    options: Array<Option | Group>;

    /**
     * A context string for the "local cmd*"
     * vars in the spec. By default it is the empty string. When used to
     * scope for completion on a *sub-command*.
     */
    context?: string | undefined;

    /**
     * By default
     * hidden options and subcmds are "excluded". Here excluded means they
     * won't be offered as a completion, but if used, their argument type
     * will be completed. "Hidden" options and subcmds are ones with the
     * `hidden: true` attribute to exclude them from default help output.
     */
    includeHidden?: boolean | undefined;

    /**
     * Array of completion types for positional args (i.e. non-options).
     * If not given, positional args will use Bash's 'default' completion.
     */
    argtypes?: string[] | undefined;
}

export interface HelpConfiguration {
    /**
     * Set to a number (for that many spaces) or a string for the literal indent.
     * Default: 4
     */
    indent?: number | string | undefined;

    /**
     * Set to a number (for that many spaces) or a string for the literal indent.
     * This indent applies to group heading lines, between normal option lines.
     * Default: half length of `indent`
     */
    headingIndent?: number | string | undefined;

    /**
     * By default the names are sorted to put the short opts first (i.e. '-h, --help' preferred to '--help, -h').
     * Set to 'none' to not do this sorting.
     * Default: 'length'
     */
    nameSort?: string | undefined;

    /**
     * Note that reflow is just done on whitespace so a long token in the option help can overflow maxCol.
     * Default: 80
     */
    maxCol?: number | undefined;

    /**
     * If not set a reasonable value will be determined between minHelpCol and maxHelpCol.
     */
    helpCol?: number | undefined;

    /**
     * Default: 20
     */
    minHelpCol?: number | undefined;

    /**
     * Default: 40
     */
    maxHelpCol?: number | undefined;

    /**
     * Set to `false` to have option `help` strings not be textwrapped to the helpCol..maxCol range.
     * Default: true
     */
    helpWrap?: boolean | undefined;

    /**
     * If the option has associated environment variables (via the env option spec attribute), then append mentioned of those envvars to the help string.
     * Default: false
     */
    includeEnv?: boolean | undefined;

    /**
     * If the option has a default value (via the default option spec attribute, or a default on the option's type), then a "Default: VALUE" string will be appended to the help string.
     * Default: false
     */
    includeDefault?: boolean | undefined;
}


text:
namespace bubblefs {
namespace mybdcom {

// simple thread implement
class Thread {
public:
    Thread() {
      memset(&tid_, 0, sizeof(tid_));
    }
    static int64_t CurrentThreadId() {
        return static_cast<int64_t>(pthread_self());
    }
    static void Exit() {
        pthread_exit(nullptr);
    }
    static void YieldCurrentThread() {
        sched_yield();
    }
    static void Sleep(struct timespec &duration) {
        struct timespec sleep_time, remaining;

        // Break the duration into seconds and nanoseconds.
        // NOTE: TimeDelta's microseconds are int64s while timespec's
        // nanoseconds are longs, so this unpacking must prevent overflow.
        sleep_time.tv_sec = duration.tv_sec;
        sleep_time.tv_nsec = duration.tv_nsec;  // nanoseconds

        while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR)
          sleep_time = remaining;
    }
    const int64_t GetThreadId() const {
        return static_cast<int64_t>(tid_);
    }
    bool IsStarted() const {
        return (0 != tid_);
    }
    bool AmSelf() const {
        return (pthread_self() == tid_);
    }
    bool Start(std::function<void ()> thread_proc) {
        user_proc_ = thread_proc;
        int ret = pthread_create(&tid_, nullptr, ProcWrapper, this);
        return (ret == 0);
    }
    typedef void* (Proc)(void*);
    bool Start(Proc proc, void* arg) {
        int ret = pthread_create(&tid_, nullptr, proc, arg);
        return (ret == 0);
    }
    bool Start(Proc proc, void* arg, size_t stack_size, bool joinable = true);
    bool Join() {
        int ret = pthread_join(tid_, nullptr);
        return (ret == 0);
    }
    bool Kill(int signal) {
      int ret = 0;
      if (tid_)
        ret = pthread_kill(tid_, signal);
      return (ret == 0);
    }
    bool Detach() {
      int ret = pthread_detach(tid_);
      return (ret == 0);
    }
private:
    static void* ProcWrapper(void* arg) {
        reinterpret_cast<Thread*>(arg)->user_proc_();
        return nullptr;
    }
    void set_thread_attrs();
    DISALLOW_COPY_AND_ASSIGN(Thread);
private:
    std::function<void ()> user_proc_;
    pthread_t tid_;
};

bool Thread::Start(Proc proc, void* arg, size_t stack_size, bool joinable) {
  pthread_attr_t attributes;
  pthread_attr_init(&attributes);
  // Pthreads are joinable by default, so only specify the detached
  // attribute if the thread should be non-joinable.
  if (!joinable) {
    pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED);
  }
  // Get a better default if available.
  if (stack_size <= 0)
    stack_size = 2 * (1 << 23);  // 2 times 8192K (the default stack size on Linux).
  pthread_attr_setstacksize(&attributes, stack_size);

  // The child thread will inherit our signal mask.
  // Set our signal mask to the set of signals we want to block ?

  int ret = pthread_create(&tid_, nullptr, proc, arg);

  pthread_attr_destroy(&attributes);
  return (ret == 0);
}

} // namespace mybdcom
} // namespace bubblefs

#endif // BUBBLEFS_UTILS_BDCOM_THREAD_H_