text:
"""Julia set generator without optional PIL-based image drawing"""
import time

# area of complex space to investigate
x1, x2, y1, y2 = -1.8, 1.8, -1.8, 1.8
c_real, c_imag = -0.62772, -.42193

@profile
def calculate_z_serial_purepython(maxiter, zs, cs):
    """Calculate output list using Julia update rule"""
    output = [0] * len(zs)
    for i in range(len(zs)):
        n = 0
        z = zs[i]
        c = cs[i]
        while abs(z) < 2 and n < maxiter:
            z = z * z + c
            n += 1
        output[i] = n
    return output

def calc_pure_python(desired_width, max_iterations):
    """Create a list of complex co-ordinates (zs) and complex parameters (cs), build Julia set and display"""
    x_step = (float(x2 - x1) / float(desired_width))
    y_step = (float(y1 - y2) / float(desired_width))
    x = []
    y = []
    ycoord = y2
    while ycoord > y1:
        y.append(ycoord)
        ycoord += y_step
    xcoord = x1
    while xcoord < x2:
        x.append(xcoord)
        xcoord += x_step
    # set width and height to the generated pixel counts, rather than the
    # pre-rounding desired width and height
    # build a list of co-ordinates and the initial condition for each cell.
    # Note that our initial condition is a constant and could easily be removed,
    # we use it to simulate a real-world scenario with several inputs to our function
    zs = []
    cs = []
    for ycoord in y:
        for xcoord in x:
            zs.append(complex(xcoord, ycoord))
            cs.append(complex(c_real, c_imag))

    print "Length of x:", len(x)
    print "Total elements:", len(zs)
    start_time = time.time()
    output = calculate_z_serial_purepython(max_iterations, zs, cs)
    end_time = time.time()
    secs = end_time - start_time
    print calculate_z_serial_purepython.func_name + " took", secs, "seconds"

    # this assert would fail as we're using 100 max_iterations, not 300
    #assert sum(output) == 33219980  # this sum is expected for 1000^2 grid with 300 iterations

print "NOTE WE'RE USING JUST 100 MAX_ITER, NOT 300, TO SAVE TIME!"
calc_pure_python(desired_width=1000, max_iterations=100)


text: Comandi I/O FRDM-K64 tramite protocollo Ethernet

redpajama_set_name: RedPajamaGithub


text:
<?php

namespace HMS\Factories\Banking;

use HMS\Entities\Banking\Bank;
use HMS\Repositories\Banking\BankRepository;

class BankFactory
{
    /**
     * @var BankRepository
     */
    protected $bankRepository;

    /**
     * @param BankRepository $bankRepository
     */
    public function __construct(BankRepository $bankRepository)
    {
        $this->bankRepository = $bankRepository;
    }

    /**
     * Function to instantiate a new BankFactory from given params.
     *
     * @param string $name
     * @param string $sortCode
     * @param string $accountNumber
     * @param string $accountName
     * @param string $type
     *
     * @return Bank
     */
    public function create(
        string $name,
        string $sortCode,
        string $accountNumber,
        string $accountName,
        string $type
    ) {
        $_bank = new Bank();

        $_bank->setName($name);
        $_bank->setSortCode($sortCode);
        $_bank->setAccountNumber($accountNumber);
        $_bank->setAccountName($accountName);
        $_bank->setType($type);

        return $_bank;
    }
}


text:
GOFDesignPatterns
=================

Exemplos de Design Patterns - Curso Fundamentos em Arquitetura de Software com .NET C#
http://www.mbcorp.com.br/?page_id=2081


text:
package com.autelhome.multiroom.util;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class InMemoryEventStoreTest {

    private final EventPublisher publisher = mock(EventPublisher.class);
    private final InMemoryEventStore testSubject = new InMemoryEventStore(publisher);

    @Test
    public void saveEvents() throws Exception {
        final UUID aggregateId = UUID.randomUUID();
        final Event event1 = mock(Event.class);
        final Event event2 = mock(Event.class);

        final List<Event> events1 = Arrays.asList(event1, event2);
        testSubject.saveEvents(aggregateId, events1, 1984);

        verify(event1).setVersion(1985);
        verify(event2).setVersion(1986);
        verify(publisher).publish(event1);
        verify(publisher).publish(event2);

        final Event event3 = mock(Event.class);
        final Event event4 = mock(Event.class);
        final List<Event> events2 = Arrays.asList(event3, event4);
        testSubject.saveEvents(aggregateId, events2, 1986);

        verify(event3).setVersion(1987);
        verify(event4).setVersion(1988);
        verify(publisher).publish(event3);
        verify(publisher).publish(event4);
    }

    @Test(expected = ConcurrencyException.class)
    public void saveEventsNotLatestVersion() throws Exception {
        final UUID aggregateId = UUID.randomUUID();
        final Event event1 = mock(Event.class);
        final Event event2 = mock(Event.class);

        final List<Event> events1 = Arrays.asList(event1, event2);
        testSubject.saveEvents(aggregateId, events1, 1984);

        verify(event1).setVersion(1985);
        verify(event2).setVersion(1986);
        verify(publisher).publish(event1);
        verify(publisher).publish(event2);

        final Event event3 = mock(Event.class);
        final Event event4 = mock(Event.class);
        final List<Event> events2 = Arrays.asList(event3, event4);
        testSubject.saveEvents(aggregateId, events2, 2001);
    }

    @Test
    public void getEventsForAggregate() throws Exception {
        final Event event1 = mock(Event.class);
        final Event event2 = mock(Event.class);

        final List<Event> events1 = Arrays.asList(event1, event2);
        final UUID aggregateId1 = UUID.randomUUID();
        testSubject.saveEvents(aggregateId1, events1, 1984);

        final Event event3 = mock(Event.class);
        final Event event4 = mock(Event.class);
        final List<Event> events2 = Arrays.asList(event3, event4);
        final UUID aggregateId2 = UUID.randomUUID();
        testSubject.saveEvents(aggregateId2, events2, 1986);

        final Event event5 = mock(Event.class);
        final Event event6 = mock(Event.class);
        final List<Event> events3 = Arrays.asList(event5, event6);
        testSubject.saveEvents(aggregateId2, events3, 1988);

        final List<Event> actual1 = testSubject.getEventsForAggregate(aggregateId1);
        assertThat(actual1).isEqualTo(Arrays.asList(event1, event2));

        final List<Event> actual2 = testSubject.getEventsForAggregate(aggregateId2);
        assertThat(actual2).isEqualTo(Arrays.asList(event3, event4, event5, event6));

     }

    @Test(expected = AggregateNotFoundException.class)
    public void getEventsForUnknownAggregate() throws Exception {
        testSubject.getEventsForAggregate(UUID.randomUUID());
    }
}


text:
require 'test_helper'

class PostersControllerTest < ActionController::TestCase
  setup do
    @poster = posters(:one)
  end

  test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:posters)
  end

  test "should get new" do
    get :new
    assert_response :success
  end

  test "should create poster" do
    assert_difference('Poster.count') do
      post :create, poster: { content: @poster.content, title: @poster.title }
    end

    assert_redirected_to poster_path(assigns(:poster))
  end

  test "should show poster" do
    get :show, id: @poster
    assert_response :success
  end

  test "should get edit" do
    get :edit, id: @poster
    assert_response :success
  end

  test "should update poster" do
    patch :update, id: @poster, poster: { content: @poster.content, title: @poster.title }
    assert_redirected_to poster_path(assigns(:poster))
  end

  test "should destroy poster" do
    assert_difference('Poster.count', -1) do
      delete :destroy, id: @poster
    end

    assert_redirected_to posters_path
  end
end


text:
package com.example.androidgame1;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.opengl.GLES20;
import android.opengl.GLUtils;
import android.util.Log;

/**
 * Loads textures. Remembers their handles.
 * Reloads all previously loaded textures.
 * Textures are not expected to be invalidated individually...
 */
public class TextureLoader {

	public static class TexturePack {
		public String name;

		// Animation Name to Animation Map
		public HashMap<String, TextureAnimation> textureAnimations;

		public TexturePack() {
			this.name = "";
			this.textureAnimations = new HashMap<String, TextureAnimation>();
		}
	}

	public static class TextureAnimation {
		public String name;
		public ArrayList<TextureFrame> textureFrames;

		public TextureAnimation() {
			textureFrames = new ArrayList<TextureFrame>();
		}

		public void addFrame(int frameNumber, int glHandle) {
			TextureFrame frame = new TextureFrame(frameNumber, glHandle);
			textureFrames.add(frame);
		}

		/**
		 * Return the frame in the animation with the highest frame number but below
		 * the a certain number
		 *
		 * @param limit
		 * @return
		 */
		public TextureFrame maxFrameUnderCriteria(int limit) {
			//int winningFrameNumber = textureFrames.get(0).frameNumber;
			//int winningFrameGLHandle = textureFrames.get(0).glTexture;

			TextureFrame maxFrame = textureFrames.get(0);

			for (int frameIndex = 0; frameIndex < textureFrames.size(); frameIndex++) {

				TextureFrame currentFrame = textureFrames.get(frameIndex);

				if ((currentFrame.frameNumber > maxFrame.frameNumber) && (currentFrame.frameNumber <= limit)) {
					maxFrame = currentFrame;
				}
			}

			return maxFrame;
		}
	}

	/**
	 * Name GLTexture
	 */
	public static class TextureFrame {
		public int frameNumber;

		public int glTexture;
		//public boolean rotated;

		public TextureFrame(int frameNumber, int glTexture) {
			this.frameNumber = frameNumber;

			this.glTexture = glTexture;
			//this.rotated = rotated;
		}

	}

	public static class LetterTexture {
		public char character;
		public Bitmap bitmap;

		public int glTexture;
		public float widthRatio;
	}

	/** For storing OpenGL texture ids **/
	//private HashMap<String, Texture> textures;

	public HashMap<Character, LetterTexture> letterTextures = new HashMap<Character, LetterTexture>(64);

	/**
	 * Deprecate?
	 */
	public HashMap<String, TexturePack> texturePacks;

	/**
	 * Examples:

	 *

	 *
"Troops/Moving"

	 *
"Troops/Idling"

	 *

	 */
	public HashMap<String, TextureAnimation> animations;

	/** For getting resources **/
	private Context context;

	/** Replacement for this **/
	private TextureLoader m = this;

	public TextureLoader(Context context) {
		m.context = context;
		//m.textures = new HashMap<String, Texture>();
		m.texturePacks = new HashMap<String, TexturePack>();

		m.animations = new HashMap<String, TextureAnimation>();
	}

	public Bitmap getBitmapFromPath(String texturePath, boolean rotated) {
		return null;
	}

	/**
	 * Read a JSON config file. Format must be a mapping
	 * @param fileName
	 * @return
	 * @throws IOException
	 * @throws FileNotFoundException
	 * @throws JSONException
	 */
	public JSONObject readJSONFile(String fileName) throws IOException, FileNotFoundException, JSONException {

			Log.i("CONFIG CONFIG", "CONFIG CONFIG: " + fileName);

			InputStream inputStream = m.context.getAssets().open(fileName);

			InputStreamReader reader = new InputStreamReader(inputStream);
			BufferedReader bufferedReader = new BufferedReader(reader);
			StringBuilder out = new StringBuilder();

			String line;

			while ((line = bufferedReader.readLine()) != null) {
				out.append(line);
			}

			JSONTokener jsonTokener = new JSONTokener(out.toString());

			Log.i("JSON", "JSON: " + out.toString());

			return (JSONObject) jsonTokener.nextValue();
	}

	public void loadAllAssetsInFolder(String folderName, boolean rotated) {
		try {
			String[] animations = m.context.getAssets().list(folderName);
			Log.i("ANIMATIONS FOLDER", "ANIMATIONS FOLDER " + animations.length);

			for (int i = 0; i < animations.length; i++) {

				boolean rotationConfiguration = rotated;

				try {
					JSONObject obj = readJSONFile(folderName + "/" + animations[i] + "/" + "config.json");
					rotationConfiguration = obj.getBoolean("axisAligned");
				} catch (JSONException e) {
					e.printStackTrace();
				} catch (FileNotFoundException fe) {
					// Do nothing
				}

				loadTextureFromAssets(folderName + "/" + animations[i], rotationConfiguration);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Loads all bitmaps in the double nested directory
	 * Example: Troop > Dying > Frame1
	 * TODO: Clean up this code
	 *
	 * @param texturePackName
	 * @param rotated
	 * @return
	 */
	public int loadTextureFromAssets(String texturePackName, boolean rotated) {

		Log.i("LOADTEXTUREFROMASSETS", "Loading");
		try {
			TexturePack texturePack = new TexturePack();
			texturePack.name = texturePackName;

			Log.i("LOADTEXTUREFROMASSETS", "texturePackName: " + texturePackName);

			// type > (states) > 1,2,3
			String[] animationStates = m.context.getAssets().list(texturePackName);

			// animations > type > (loop through states) > 1,2,3
			for (int i = 0; i < animationStates.length; i++) {

				Log.i("rotationConfig1", "rotationConfig1: " + rotated);

				TextureAnimation animation = new TextureAnimation();
				animation.name = animationStates[i];

				texturePack.textureAnimations.put(animationStates[i], animation);
				animations.put(texturePackName + "/" + animationStates[i], animation);

				Log.i("LOADTEXTUREFROMASSETS", "animations key: " + texturePackName + "/" + animationStates[i]);
				Log.i("LOADTEXTUREFROMASSETS", "textureAnimationName: " + animation.name);

				// animations > type > states > (1,2,3)
				String[] bitmapFrameNames = m.context.getAssets().list(texturePackName + "/" + animationStates[i]);

				//Log.i("LOADTEXTUREFROMASSETS", "bitmapFrameNames length: " + bitmapFrameNames.length);

				for (int f = 0; f < bitmapFrameNames.length; f++) {
					String bitmapFileLocation = texturePackName + "/" + animationStates[i] + "/" + bitmapFrameNames[f];

					Bitmap bitmap = BitmapFactory.decodeStream(m.context.getAssets().open(bitmapFileLocation));
					Log.i("generate from rotationConfig", "generate from rotationConfig: " + rotated);
					int glHandle = this.generateGLTextureFromBitmap(bitmap, rotated);

					int frameNumber = Integer.parseInt( bitmapFrameNames[f].substring(0, bitmapFrameNames[f].lastIndexOf('.')) );
					animation.addFrame(frameNumber, glHandle);

					Log.i("LOADTEXTUREFROMASSETS", "frameNumber: " + frameNumber);
					Log.i("LOADTEXTUREFROMASSETS", "animation.textureFrames length: " + animation.name + ", " + animation.textureFrames.size());
				}

				Collections.sort(animation.textureFrames, new Comparator<TextureFrame>() {
					@SuppressLint("NewApi")
					@Override
					public int compare(TextureFrame lhs, TextureFrame rhs) { return Integer.compare(lhs.frameNumber, rhs.frameNumber); }
				});

				texturePacks.put(texturePackName, texturePack);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return 0;
	}

	/**
	 * Loads all letters of alphabet
	 */
	public void loadLetterTextures() {
		StringBuilder sb = new StringBuilder(1);
		String alphabet = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-*/=[]{}\"\'<>!@#$%^&*()?,.";

		for (int i = 0; i < alphabet.length(); i++) {
			// reset builder
			sb.setLength(0);
			sb.append(alphabet.charAt(i));

			// generate bitmap from single letter
			Bitmap bitmap = generateTextBitmap(sb);
			int texture = generateGLTextureFromBitmap(bitmap, false);

			// create the letter texture
			LetterTexture letterTexture = new LetterTexture();
			letterTexture.bitmap = bitmap;
			letterTexture.character = alphabet.charAt(i);
			letterTexture.glTexture = texture;
			letterTexture.widthRatio = (float)bitmap.getWidth() / bitmap.getHeight();

			letterTextures.put(alphabet.charAt(i), letterTexture);
		}
	}

	/**
	 * TODO: Figure out how to save aspect ratio of text
	 * http://stackoverflow.com/questions/8799290/convert-string-text-to-bitmap
	 * @param text
	 * @return
	 */
	public Bitmap generateTextBitmap(StringBuilder text) {
		Paint paint = new Paint();
		paint.setTextSize(144);
		paint.setAntiAlias(true);
		paint.setColor(Color.rgb(240, 240, 240));
		paint.setAlpha(170);
		//center version
		//paint.setTextAlign(Paint.Align.CENTER);
		paint.setTextAlign(Paint.Align.LEFT);

		int width = (int) (paint.measureText((CharSequence)text, 0 , text.length()) + 0.5f);
		float baseline = (int) (-paint.ascent() + 0.5f);
		int height = (int) (baseline + paint.descent() + 0.5f );

		Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(image);
		//canvas.drawText((CharSequence)text, 0, text.length(), width/2, baseline, paint);

		// At height 0, the text is drawn with the baseline on the top border
		//center version
		//canvas.drawText((CharSequence)text, 0, text.length(), width/2, height - paint.descent(), paint);

		// explicit version
		//canvas.drawText((CharSequence)text, 0, text.length(), 0, height - paint.descent(), paint);
		canvas.drawText((CharSequence)text, 0, text.length(), 0, baseline, paint);

		return image;
	}

	public int generateGLTextureFromBitmap(Bitmap bitmap, boolean rotated) {

		if (rotated == true) {
			//Matrix matrix = new Matrix();
			//matrix.postRotate(90);

			// Since bitmap is readonly, we create a mutable one
			Bitmap result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
			Canvas tempCanvas = new Canvas(result);

			// Draw bitmap onto mutable bitmap
			tempCanvas.rotate(90, bitmap.getWidth()/2, bitmap.getHeight()/2);
			tempCanvas.drawBitmap(bitmap, 0, 0, null);

			// Swap!
			bitmap = result;
		}

		int[] textureHandle = new int[1];

		GLES20.glGenTextures(1, textureHandle, 0);
		GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);

		GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
		GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
		GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
		bitmap.recycle();

		if (textureHandle[0] == 0)
	    {
	        throw new RuntimeException("Error loading texture.");
	    }

		//Log.d("TextureHandle", Integer.toString(textureHandle[0]) );

		return textureHandle[0];
	}

	/**
	 * Creates OpenGL textures out of the resource.

	 * Preconditions: Needs to have OpenGL context
	 * textureLoader.loadTexture(UNIT1_TEXTURE, R.drawable.sprite1, true);
	 *
	 * @param textureName
	 * @param resource
	 */
//	public int loadTexture(String textureName, int resource, boolean rotated) {
//		Bitmap bitmap = BitmapFactory.decodeResource(m.context.getResources(), resource);
//
//		if (rotated == true) {
//			//Matrix matrix = new Matrix();
//			//matrix.postRotate(90);
//
//			// Since bitmap is readonly, we create a mutable one
//			Bitmap result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
//			Canvas tempCanvas = new Canvas(result);
//
//			// Draw bitmap onto mutable bitmap
//			tempCanvas.rotate(90, bitmap.getWidth()/2, bitmap.getHeight()/2);
//			tempCanvas.drawBitmap(bitmap, 0, 0, null);
//
//			// Swap!
//			bitmap = result;
//		}
//
//		int[] textureHandle = new int[1];
//
//		GLES20.glGenTextures(1, textureHandle, 0);
//		GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);
//
//		GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
//		GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
//		GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
//        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
//
//        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
//		bitmap.recycle();
//
//		if (textureHandle[0] == 0)
//	    {
//	        throw new RuntimeException("Error loading texture.");
//	    }
//
//		if (textureName.length() != 0 && textureName != null) {
//			Texture texture = new Texture(textureName, resource, textureHandle[0], rotated);
//			m.textures.put(textureName, texture);
//		}
//
//		Log.d("TextureHandle", Integer.toString(textureHandle[0]) );
//
//		return textureHandle[0];
//	}
//
//	public Texture getTexture(String textureName) {
//		return m.textures.get(textureName);
//	}
//
//	public int getTextureGL(String textureName) {
//		return m.textures.get(textureName).glTexture;
//	}
//
//	public void reloadAllTextures() {
//		for (Texture texture : m.textures.values()) {
//			m.loadTexture(null, texture.resourceID, texture.rotated);
//		}
//	}
}


text:
import os
import unittest
import sys

import inmembrane
import inmembrane.tests
from inmembrane import helpers
from inmembrane.plugins import signalp_scrape_web

from inmembrane.tests.PluginTestBase import PluginTestBase

class TestSignalpScrapeWeb(PluginTestBase):
    _plugin_name = "signalp_scrape_web"

    def test_signalp4(self):
        if not self.params['signalp4_bin']:
            self.params['signalp4_bin'] = 'signalp_scrape_web'
        self.params['fasta'] = "input.fasta"
        self.params['signalp4_organism'] = 'gram+'
        signalp_scrape_web.annotate(self.params, self.proteins)

        self.expected_output = {
            u'SPy_0252': True,
            u'SPy_2077': False,
            u'SPy_0317': True,
            u'sp|B7LNW7': True,
        }
        for seqid in self.expected_output:
            self.assertEqual(
                self.expected_output[seqid], self.proteins[seqid]['is_signalp'])
        self.assertEqual(self.proteins[u'sp|B7LNW7']['signalp_cleave_position'],
                         22)

if __name__ == '__main__':
    unittest.main()


text:
from tasks.base_tasks import (RepoFileUnzipTask, GeoFile2TempTableTask, SimplifiedTempTableTask, TagsTask,
                              ColumnsTask, TableTask)
from tasks.tags import SectionTags, SubsectionTags, LicenseTags, BoundaryTags
from tasks.meta import GEOM_REF, OBSTag, OBSColumn, OBSTable, current_session
from tasks.util import copyfile
from lib.timespan import get_timespan

from luigi import WrapperTask
from collections import OrderedDict
import os

class ODLSourceTags(TagsTask):
    def version(self):
        return 1

    def tags(self):
        return[
            OBSTag(id='uk_os_opendata_source',
                   name='UK OS OpenData',
                   type='source',
                   description='''
                               Contains Ordnance Survey data (c) Crown copyright and database right 2015
                               Contains Royal Mail data (c) Royal Mail copyright and database right 2015
                               Contains National Statistics data (c) Crown copyright and database right 2015
                               ''')
                   ]

class DownloadPostcodeBoundaries(RepoFileUnzipTask):

    # Source: https://www.opendoorlogistics.com/downloads/
    URL = 'https://www.opendoorlogistics.com/wp-content/uploads/Data/UK-postcode-boundaries-Jan-2015.zip'

    def get_url(self):
        return self.URL

class ImportPostcodeDistricts(GeoFile2TempTableTask):

    def requires(self):
        return DownloadPostcodeBoundaries()

    def input_files(self):
        return os.path.join(self.input().path, 'Distribution', 'Districts.shp')

class SimplifiedPostcodeDistricts(SimplifiedTempTableTask):
    def requires(self):
        return ImportPostcodeDistricts()

class PostcodeDistrictsColumns(ColumnsTask):

    def version(self):
        return 1

    def requires(self):
        return {
            'subsections': SubsectionTags(),
            'sections': SectionTags(),
            'source': ODLSourceTags(),
            'license': LicenseTags(),
            'boundary': BoundaryTags(),
        }

    def columns(self):
        input_ = self.input()
        license = input_['license']['uk_ogl']
        source = input_['source']['uk_os_opendata_source']
        boundary_type = input_['boundary']
        geom = OBSColumn(
            id='pd_geo',
            type='Geometry',
            name='Reconstructed UK Postcode Districts',
            description='Reconstructed UK Postcode Districts',
            weight=8,
            tags=[input_['subsections']['boundary'], input_['sections']['uk'], source, license,
                  boundary_type['cartographic_boundary'], boundary_type['interpolation_boundary']]
        )
        geomref = OBSColumn(
            id='pd_id',
            type='Text',
            name='Postcode District ID',
            weight=0,
            targets={geom: GEOM_REF},
        )
        geomname = OBSColumn(
            id='pd_name',
            type='Text',
            name='Postcode District ID',
            weight=1,
            targets={geom: GEOM_REF},
            tags=[input_['sections']['uk'], input_['subsections']['boundary'], source, license]
        )

        return OrderedDict([
            ('the_geom', geom),
            ('geographycode', geomref),
            ('name', geomname)
        ])

    @staticmethod
    def geoname_column():
        return 'geographycode'

    @staticmethod
    def geoid_column():
        return 'geographycode'

class PostcodeDistricts(TableTask):

    def requires(self):
        return {
            'columns': PostcodeDistrictsColumns(),
            'data': SimplifiedPostcodeDistricts(),
        }

    def version(self):
        return 1

    def table_timespan(self):
        return get_timespan('2015')

    # TODO: https://github.com/CartoDB/bigmetadata/issues/435
    def targets(self):
        return {
            OBSTable(id='.'.join([self.schema(), self.name()])): GEOM_REF,
        }

    def columns(self):
        input_ = self.input()
        cols = OrderedDict()
        cols.update(input_['columns'])
        return cols

    def populate(self):
        session = current_session()

        query = '''
                INSERT INTO {output}
                SELECT ST_MakeValid(wkb_geometry), name, name
                FROM {input}
                '''.format(output=self.output().table,
                           input=self.input()['data'].table)

        session.execute(query)

    @staticmethod
    def geoid_column():
        return PostcodeDistrictsColumns.geoid_column()

    @staticmethod
    def geoname_column():
        return PostcodeDistrictsColumns.geoname_column()

class ImportPostcodeSectors(GeoFile2TempTableTask):

    def requires(self):
        return DownloadPostcodeBoundaries()

    def input_files(self):
        return os.path.join(self.input().path, 'Distribution', 'Sectors.shp')

class SimplifiedPostcodeSectors(SimplifiedTempTableTask):
    def requires(self):
        return ImportPostcodeSectors()

class PostcodeSectorsColumns(ColumnsTask):

    def version(self):
        return 1

    def requires(self):
        return {
            'subsections': SubsectionTags(),
            'sections': SectionTags(),
            'source': ODLSourceTags(),
            'license': LicenseTags(),
            'boundary': BoundaryTags(),
        }

    def columns(self):
        input_ = self.input()
        license = input_['license']['uk_ogl']
        source = input_['source']['uk_os_opendata_source']
        boundary_type = input_['boundary']
        geom = OBSColumn(
            id='ps_geo',
            type='Geometry',
            name='Reconstructed UK Postcode Sectors',
            description='Reconstructed UK Postcode Sectors',
            weight=8,
            tags=[input_['subsections']['boundary'], input_['sections']['uk'], source, license,
                  boundary_type['cartographic_boundary'], boundary_type['interpolation_boundary']]
        )
        geomref = OBSColumn(
            id='ps_id',
            type='Text',
            name='Postcode Sector ID',
            weight=0,
            targets={geom: GEOM_REF},
        )
        geomname = OBSColumn(
            id='ps_name',
            type='Text',
            name='Postcode Sector ID',
            weight=1,
            targets={geom: GEOM_REF},
            tags=[input_['sections']['uk'], input_['subsections']['boundary'], source, license]
        )

        return OrderedDict([
            ('the_geom', geom),
            (self.geoid_column(), geomref),
            (self.geoname_column(), geomname)
        ])

    @staticmethod
    def geoname_column():
        return 'geographyname'

    @staticmethod
    def geoid_column():
        return 'geographycode'

class PostcodeSectors(TableTask):

    def requires(self):
        return {
            'columns': PostcodeSectorsColumns(),
            'data': SimplifiedPostcodeSectors(),
        }

    def version(self):
        return 1

    def table_timespan(self):
        return get_timespan('2015')

    # TODO: https://github.com/CartoDB/bigmetadata/issues/435
    def targets(self):
        return {
            OBSTable(id='.'.join([self.schema(), self.name()])): GEOM_REF,
        }

    def columns(self):
        input_ = self.input()
        cols = OrderedDict()
        cols.update(input_['columns'])
        return cols

    def populate(self):
        session = current_session()

        query = '''
                INSERT INTO {output}
                SELECT ST_MakeValid(wkb_geometry), name, name
                FROM {input}
                '''.format(output=self.output().table,
                           input=self.input()['data'].table)

        session.execute(query)

    @staticmethod
    def geoid_column():
        return PostcodeSectorsColumns.geoid_column()

    @staticmethod
    def geoname_column():
        return PostcodeSectorsColumns.geoname_column()

class ODLWrapper(WrapperTask):
    def requires(self):
        yield PostcodeDistricts()
        yield PostcodeSectors()


text:
void lookAt(const float *eye,const float *look);

#endif


text:
"""Config file with field references."""

from ml_collections import config_dict

def get_config():
  cfg = config_dict.ConfigDict()
  cfg.ref = config_dict.FieldReference(123)
  cfg.ref_nodefault = config_dict.placeholder(int)
  return cfg


text:
package cpolicy

import (
	"bytes"
	"encoding/gob"
	"encoding/json"
	"errors"
	"fmt"
	"sync"

	"github.com/intelsdi-x/snap/core/ctypes"
	"github.com/intelsdi-x/snap/pkg/ctree"
)

type ProcessingErrors struct {
	errors []error
	mutex  *sync.Mutex
}

func NewProcessingErrors() *ProcessingErrors {
	return &ProcessingErrors{
		errors: []error{},
		mutex:  &sync.Mutex{},
	}
}

func (p *ProcessingErrors) Errors() []error {
	p.mutex.Lock()
	defer p.mutex.Unlock()
	return p.errors
}

func (p *ProcessingErrors) HasErrors() bool {
	return len(p.errors) > 0
}

func (p *ProcessingErrors) AddError(e error) {
	p.mutex.Lock()
	defer p.mutex.Unlock()
	p.errors = append(p.errors, e)
}

type ConfigPolicyNode struct {
	rules map[string]Rule
	mutex *sync.Mutex
}

func NewPolicyNode() *ConfigPolicyNode {
	return &ConfigPolicyNode{
		rules: make(map[string]Rule),
		mutex: &sync.Mutex{},
	}
}

func (c *ConfigPolicyNode) CopyRules() ([]Rule, error) {
	rules := []Rule{}
	for _, rule := range c.rules {
		var err error
		switch rule.(type) {
		case *BoolRule:
			var newBoolRule *BoolRule
			if rule.Default() != nil {
				newBoolRule, err = NewBoolRule(rule.Key(), rule.Required(), rule.Default().(ctypes.ConfigValueBool).Value)
			} else {
				newBoolRule, err = NewBoolRule(rule.Key(), rule.Required())
			}
			rules = append(rules, newBoolRule)
		case *StringRule:
			var newStringRule *StringRule
			if rule.Default() != nil {
				newStringRule, err = NewStringRule(rule.Key(), rule.Required(), rule.Default().(ctypes.ConfigValueStr).Value)
			} else {
				newStringRule, err = NewStringRule(rule.Key(), rule.Required())
			}
			rules = append(rules, newStringRule)
		case *FloatRule:
			var newFloatRule *FloatRule
			if rule.Default() != nil {
				newFloatRule, err = NewFloatRule(rule.Key(), rule.Required(), rule.Default().(ctypes.ConfigValueFloat).Value)
			} else {
				newFloatRule, err = NewFloatRule(rule.Key(), rule.Required())
			}
			rules = append(rules, newFloatRule)
		case *IntRule:
			var newIntRule *IntRule
			if rule.Default() != nil {
				newIntRule, err = NewIntegerRule(rule.Key(), rule.Required(), rule.Default().(ctypes.ConfigValueInt).Value)
			} else {
				newIntRule, err = NewIntegerRule(rule.Key(), rule.Required())
			}
			rules = append(rules, newIntRule)
		default:
			return []Rule{}, errors.New(fmt.Sprint("Unknown rule type"))
		}

		if err != nil {
			return []Rule{}, errors.New(fmt.Sprintf("Could not create rule %s type %s ", rule.Key(), rule.Type()))
		}
	}
	return rules, nil
}

// UnmarshalJSON unmarshals JSON into a ConfigPolicyNode
func (c *ConfigPolicyNode) UnmarshalJSON(data []byte) error {
	m := map[string]interface{}{}
	decoder := json.NewDecoder(bytes.NewReader(data))
	if err := decoder.Decode(&m); err != nil {
		return err
	}
	if rs, ok := m["rules"]; ok {
		if rules, ok := rs.(map[string]interface{}); ok {
			addRulesToConfigPolicyNode(rules, c)
		}
	}
	return nil
}

func (c *ConfigPolicyNode) MarshalJSON() ([]byte, error) {
	return json.Marshal(&struct {
		Rules map[string]Rule `json:"rules"`
	}{
		Rules: c.rules,
	})
}

func (c *ConfigPolicyNode) GobEncode() ([]byte, error) {
	w := new(bytes.Buffer)
	encoder := gob.NewEncoder(w)
	if err := encoder.Encode(&c.rules); err != nil {
		return nil, err
	}
	return w.Bytes(), nil
}

func (c *ConfigPolicyNode) GobDecode(buf []byte) error {
	c.mutex = &sync.Mutex{}
	r := bytes.NewBuffer(buf)
	decoder := gob.NewDecoder(r)
	return decoder.Decode(&c.rules)
}

// Adds a rule to this policy node
func (p *ConfigPolicyNode) Add(rules ...Rule) {
	p.mutex.Lock()
	defer p.mutex.Unlock()
	for _, r := range rules {
		p.rules[r.Key()] = r
	}
}

type RuleTableSlice []RuleTable

type RuleTable struct {
	Name     string      `json:"name"`
	Type     string      `json:"type"`
	Default  interface{} `json:"default,omitempty"`
	Required bool        `json:"required"`
	Minimum  interface{} `json:"minimum,omitempty"`
	Maximum  interface{} `json:"maximum,omitempty"`
}

func (p *ConfigPolicyNode) RulesAsTable() RuleTableSlice {
	p.mutex.Lock()
	defer p.mutex.Unlock()

	rt := make([]RuleTable, 0, len(p.rules))
	for _, r := range p.rules {
		rt = append(rt, RuleTable{
			Name:     r.Key(),
			Type:     r.Type(),
			Default:  r.Default(),
			Required: r.Required(),
			Minimum:  r.Minimum(),
			Maximum:  r.Maximum(),
		})
	}
	return rt
}

func (c *ConfigPolicyNode) HasRules() bool {
	if len(c.rules) > 0 {
		return true
	}
	return false
}

// Defaults returns a map[string]ctypes.ConfigValue for all of the rules that
// have defaults.
func (c *ConfigPolicyNode) Defaults() map[string]ctypes.ConfigValue {
	defaults := map[string]ctypes.ConfigValue{}
	for name, rule := range c.rules {
		if def := rule.Default(); def != nil {
			defaults[name] = def
		}
	}
	return defaults
}

// Validates and returns a processed policy node or nil and error if validation has failed
func (c *ConfigPolicyNode) Process(m map[string]ctypes.ConfigValue) (*map[string]ctypes.ConfigValue, *ProcessingErrors) {
	c.mutex.Lock()
	defer c.mutex.Unlock()
	pErrors := NewProcessingErrors()
	// Loop through each rule and process
	for key, rule := range c.rules {
		// items exists for rule
		if cv, ok := m[key]; ok {
			// Validate versus matching data
			e := rule.Validate(cv)
			if e != nil {
				pErrors.AddError(e)
			}
		} else {
			// If it was required add error
			if rule.Required() {
				e := fmt.Errorf("required key missing (%s)", key)
				pErrors.AddError(e)
			} else {
				// If default returns we should add it
				cv := rule.Default()
				if cv != nil {
					m[key] = cv
				}

			}
		}
	}

	if pErrors.HasErrors() {
		return nil, pErrors
	}
	return &m, pErrors
}

// AddDefaults validates and returns a processed policy node or nil and error if validation has failed
func (c *ConfigPolicyNode) AddDefaults(m map[string]ctypes.ConfigValue) (*map[string]ctypes.ConfigValue, *ProcessingErrors) {
	c.mutex.Lock()
	defer c.mutex.Unlock()
	pErrors := NewProcessingErrors()
	// Loop through each rule and process
	for key, rule := range c.rules {
		// items exists for rule
		if _, ok := m[key]; ok {
			pErrors.AddError(fmt.Errorf("The key \"%v\" already has a default policy for this plugin", key))
		} else {
			// If it was required add error
			if !rule.Required() {
				// If default returns we should add it
				cv := rule.Default()
				if cv != nil {
					m[key] = cv
				}
			}
		}
	}

	if pErrors.HasErrors() {
		return nil, pErrors
	}
	return &m, pErrors
}

// Merges a ConfigPolicyNode on top of this one (overwriting items where it occurs).
func (c ConfigPolicyNode) Merge(n ctree.Node) ctree.Node {
	// Because Add only allows the ConfigPolicyNode type we
	// are safe to convert ctree.Node interface to ConfigPolicyNode
	cd := n.(*ConfigPolicyNode)
	// For the rules in the passed ConfigPolicyNode(converted) add each rule to
	// this ConfigPolicyNode overwriting where needed.
	no := NewPolicyNode()
	for _, r := range c.rules {
		no.Add(r)
	}

	for _, r := range cd.rules {

		no.Add(r)
	}
	// Return modified version of ConfigPolicyNode(as ctree.Node)
	return no
}

// addRulesToConfigPolicyNode accepts a map of empty interfaces that will be
// marshalled into rules which will be added to the ConfigPolicyNode provided
// as the second argument.  This function is called used by the UnmarshalJSON
// for ConfigPolicy and ConfigPolicyNode.
func addRulesToConfigPolicyNode(rules map[string]interface{}, cpn *ConfigPolicyNode) error {
	for k, rule := range rules {
		if rule, ok := rule.(map[string]interface{}); ok {
			req, _ := rule["required"].(bool)
			switch rule["type"] {
			case "integer":
				r, _ := NewIntegerRule(k, req)
				if d, ok := rule["default"]; ok {
					// json encoding an int results in a float when decoding
					def_, _ := d.(float64)
					def := int(def_)
					r.default_ = &def
				}
				if m, ok := rule["minimum"]; ok {
					min_, _ := m.(float64)
					min := int(min_)
					r.minimum = &min
				}
				if m, ok := rule["maximum"]; ok {
					max_, _ := m.(float64)
					max := int(max_)
					r.maximum = &max
				}
				cpn.Add(r)
			case "string":
				r, _ := NewStringRule(k, req)
				if d, ok := rule["default"]; ok {
					def, _ := d.(string)
					if def != "" {
						r.default_ = &def
					}
				}

				cpn.Add(r)
			case "bool":
				r, _ := NewBoolRule(k, req)
				if d, ok := rule["default"]; ok {
					def, _ := d.(bool)
					r.default_ = &def
				}

				cpn.Add(r)
			case "float":
				r, _ := NewFloatRule(k, req)
				if d, ok := rule["default"]; ok {
					def, _ := d.(float64)
					r.default_ = &def
				}
				if m, ok := rule["minimum"]; ok {
					min, _ := m.(float64)
					r.minimum = &min
				}
				if m, ok := rule["maximum"]; ok {
					max, _ := m.(float64)
					r.maximum = &max
				}
				cpn.Add(r)
			default:
				return errors.New("unknown type")
			}
		}
	}
	return nil
}