<commit_before>
"""functions used to construct different architectures
"""

import tensorflow as tf
import numpy as np
import BasicConvLSTMCell

FLAGS = tf.app.flags.FLAGS

def _activation_summary(x):
  """Helper to create summaries for activations.

  Creates a summary that provides a histogram of activations.
  Creates a summary that measure the sparsity of activations.

  Args:
    x: Tensor
  Returns:
    nothing
  """
  tensor_name = x.op.name
  tf.histogram_summary(tensor_name + '/activations', x)
  tf.scalar_summary(tensor_name + '/sparsity', tf.nn.zero_fraction(x))

def _variable_on_cpu(name, shape, initializer):
  """Helper to create a Variable stored on CPU memory.

  Args:
    name: name of the variable
    shape: list of ints
    initializer: initializer for Variable

  Returns:
    Variable Tensor
  """
  with tf.device('/gpu:0'):
    var = tf.get_variable(name, shape, initializer=initializer)
  return var

def _variable_with_weight_decay(name, shape, stddev, wd):
  """Helper to create an initialized Variable with weight decay.

  Note that the Variable is initialized with a truncated normal distribution.
  A weight decay is added only if one is specified.

  Args:
    name: name of the variable
    shape: list of ints
    stddev: standard deviation of a truncated Gaussian
    wd: add L2Loss weight decay multiplied by this float. If None, weight
        decay is not added for this Variable.

  Returns:
    Variable Tensor
  """
  var = _variable_on_cpu(name, shape,
                         tf.truncated_normal_initializer(stddev=stddev))
  if wd:
    weight_decay = tf.mul(tf.nn.l2_loss(var), wd, name='weight_loss')
    weight_decay.set_shape([])
    tf.add_to_collection('losses', weight_decay)
  return var

def _conv_layer(inputs, kernel_size, stride, num_features, idx, linear = False):
  with tf.variable_scope('{0}_conv'.format(idx)) as scope:
    input_channels = inputs.get_shape()[3]

    weights = _variable_with_weight_decay('weights', shape=[kernel_size,kernel_size,input_channels,num_features],stddev=0.01, wd=FLAGS.weight_decay)
    biases = _variable_on_cpu('biases',[num_features],tf.constant_initializer(0.01))

    conv = tf.nn.conv2d(inputs, weights, strides=[1, stride, stride, 1], padding='SAME')
    conv_biased = tf.nn.bias_add(conv, biases)
    if linear:
      return conv_biased
    conv_rect = tf.nn.elu(conv_biased,name='{0}_conv'.format(idx))
    return conv_rect

def _transpose_conv_layer(inputs, kernel_size, stride, num_features, idx, linear = False):
  with tf.variable_scope('{0}_trans_conv'.format(idx)) as scope:
    input_channels = inputs.get_shape()[3]

    weights = _variable_with_weight_decay('weights', shape=[kernel_size,kernel_size,num_features,input_channels], stddev=0.01, wd=FLAGS.weight_decay)
    biases = _variable_on_cpu('biases',[num_features],tf.constant_initializer(0.01))
    batch_size = tf.shape(inputs)[0]
    output_shape = tf.pack([tf.shape(inputs)[0], tf.shape(inputs)[1]*stride, tf.shape(inputs)[2]*stride, num_features])
    conv = tf.nn.conv2d_transpose(inputs, weights, output_shape, strides=[1,stride,stride,1], padding='SAME')
    conv_biased = tf.nn.bias_add(conv, biases)
    if linear:
      return conv_biased
    conv_rect = tf.nn.elu(conv_biased,name='{0}_transpose_conv'.format(idx))
    return conv_rect

def _fc_layer(inputs, hiddens, idx, flat = False, linear = False):
  with tf.variable_scope('{0}_fc'.format(idx)) as scope:
    input_shape = inputs.get_shape().as_list()
    if flat:
      dim = input_shape[1]*input_shape[2]*input_shape[3]
      inputs_processed = tf.reshape(inputs, [-1,dim])
    else:
      dim = input_shape[1]
      inputs_processed = inputs

    weights = _variable_with_weight_decay('weights', shape=[dim,hiddens],stddev=0.01, wd=FLAGS.weight_decay)
    biases = _variable_on_cpu('biases', [hiddens], tf.constant_initializer(0.01))
    if linear:
      return tf.add(tf.matmul(inputs_processed,weights),biases,name=str(idx)+'_fc')

    ip = tf.add(tf.matmul(inputs_processed,weights),biases)
    return tf.nn.elu(ip,name=str(idx)+'_fc')

def _phase_shift(I, r):
    bsize, a, b, c = I.get_shape().as_list()
    bsize = tf.shape(I)[0] # Handling Dimension(None) type for undefined batch dim
    X = tf.reshape(I, (bsize, a, b, r, r))
    X = tf.transpose(X, (0, 1, 2, 4, 3))  # bsize, a, b, 1, 1
    X = tf.split(1, a, X)  # a, [bsize, b, r, r]
    X = tf.concat(2, [tf.squeeze(x) for x in X])  # bsize, b, a*r, r
    X = tf.split(1, b, X)  # b, [bsize, a*r, r]
    X = tf.concat(2, [tf.squeeze(x) for x in X])  # bsize, a*r, b*r
    return tf.reshape(X, (bsize, a*r, b*r, 1))

def PS(X, r, depth):
    Xc = tf.split(3, depth, X)
    X = tf.concat(3, [_phase_shift(x, r) for x in Xc])
    return X

def encoding_32x32x3(inputs, keep_prob):
  """Builds encoding part of ring net.
  Args:
    inputs: input to encoder
    keep_prob: dropout layer
  """
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice x_1 -> y_1
  x_1_image = inputs

  # conv1
  conv1 = _conv_layer(x_1_image, 8, 2, 64, "encode_1")
  # conv2
  conv2 = _conv_layer(conv1, 6, 2, 128, "encode_2")
  # conv3
  conv3 = _conv_layer(conv2, 6, 2, 128, "encode_3")
  # y_1
  y_1 = _fc_layer(conv3, FLAGS.compression_size, "encode_4", True)
  _activation_summary(y_1)
  return y_1

def encoding_32x32x1(inputs, keep_prob):
  """Builds encoding part of ring net.
  Args:
    inputs: input to encoder
    keep_prob: dropout layer
  """
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice x_1 -> y_1
  x_1_image = inputs

  # conv1
  conv1 = _conv_layer(x_1_image, 3, 2, 8, "encode_1")
  # conv2
  conv2 = _conv_layer(conv1, 3, 1, 16, "encode_2")
  # y_1
  y_1 = conv2
  _activation_summary(y_1)
  return y_1

def encoding_401x101x2(inputs, keep_prob):
  """Builds encoding part of ring net.
  Args:
    inputs: input to encoder
    keep_prob: dropout layer
  """
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice x_1 -> y_1
  x_1_image = inputs

  # conv1
  conv1 = _conv_layer(inputs, 3, 2, 64, "encode_1")
  # conv2
  conv2 = _conv_layer(conv1, 3, 1, 128, "encode_2")
  # conv3
  conv3 = _conv_layer(conv2, 3, 2, 256, "encode_3")
  # conv4
  conv4 = _conv_layer(conv3, 3, 1, 128, "encode_4")

  return conv4

def lstm_compression_32x32x3(y_1, hidden_state, keep_prob, encode=True):
  """Builds compressed dynamical system part of the net.
  Args:
    inputs: input to system
  """
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice y_1 -> y_2
  num_layers = FLAGS.num_layers

  y_1 = _fc_layer(y_1, FLAGS.lstm_size, "compression_1")

  with tf.variable_scope("compress_LSTM", initializer = tf.random_uniform_initializer(-0.01, 0.01)):
    with tf.device('/gpu:0'):
      lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(FLAGS.lstm_size, forget_bias=1.0)
      lstm_cell = tf.nn.rnn_cell.DropoutWrapper(lstm_cell, output_keep_prob=keep_prob)
      cell = tf.nn.rnn_cell.MultiRNNCell([lstm_cell] * num_layers)
      if hidden_state == None:
        batch_size = y_1.get_shape()[0]
        hidden_state = cell.zero_state(batch_size, tf.float32)

  y_2, new_state = cell(y_1, hidden_state)

  # residual connection
  #y_2 = y_2 + y_1

  y_2 = _fc_layer(y_2, FLAGS.compression_size, "compression_2")

  return y_2, new_state

def lstm_compression_32x32x1(y_1, hidden_state, keep_prob, encode=True):
  """Builds compressed dynamical system part of the net.
  Args:
    inputs: input to system
  """
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice y_1 -> y_2
  with tf.variable_scope("compress_LSTM", initializer = tf.random_uniform_initializer(-0.01, 0.01)):
    with tf.device('/gpu:0'):
      lstm_cell = BasicConvLSTMCell.BasicConvLSTMCell([int(y_1.get_shape()[1]),int(y_1.get_shape()[2])], [3,3], 16)
      if hidden_state == None:
        batch_size = y_1.get_shape()[0]
        hidden_state = lstm_cell.zero_state(batch_size, tf.float32)

  y_2, new_state = lstm_cell(y_1, hidden_state)

  return y_2, new_state

def lstm_compression_401x101x2(y_1, hidden_state, keep_prob, encode=True):
  """Builds compressed dynamical system part of the net.
  Args:
    inputs: input to system
  """
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice y_1 -> y_2
  with tf.variable_scope("compress_LSTM", initializer = tf.random_uniform_initializer(-0.01, 0.01)):
    with tf.device('/gpu:0'):
      lstm_cell = BasicConvLSTMCell.BasicConvLSTMCell([int(y_1.get_shape()[1]),int(y_1.get_shape()[2])], [3,3], 128)
      if hidden_state == None:
        batch_size = y_1.get_shape()[0]
        hidden_state = lstm_cell.zero_state(batch_size, tf.float32)

  y_2, new_state = lstm_cell(y_1, hidden_state)

  return y_2, new_state

def decoding_32x32x3(y_2):
  """Builds decoding part of ring net.
  Args:
    inputs: input to decoder
  """
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice y_3 -> x_2

  # fc21
  fc21 = _fc_layer(y_2, 2048, "decode_21")
  conv21 = tf.reshape(fc21, [-1, 4, 4, 128])
  # conv22
  conv22 = _transpose_conv_layer(conv21, 6, 2, 128, "decode_22")
  # conv23
  conv23 = _transpose_conv_layer(conv22, 6, 2, 64, "decode_23")
  # conv24
  conv24 = _transpose_conv_layer(conv23, 6, 2, 3, "decode_24")
  x_2 = tf.reshape(conv24, [-1, 32, 32, 3])
  return x_2

def decoding_32x32x1(y_2):
  """Builds decoding part of ring net.
  Args:
    inputs: input to decoder
  """
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice y_3 -> x_2

  # conv21
  conv21 = _transpose_conv_layer(y_2, 3, 1, 4, "decode_21")
  conv21 = tf.reshape(conv21, [-1, 16, 16, 4])
  conv21 = PS(conv21, 2, 1)
  #x_2 = x_2[:,:401,:101,:]
  x_2 = conv21
  return x_2

def decoding_401x101x2(y_2):
  """Builds decoding part of ring net.
  Args:
    inputs: input to decoder
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice y_3 -> x_2

  # fc21
  fc21 = _fc_layer(y_2, 17*5*128, "decode_21")
  conv21 = tf.reshape(fc21, [-1, 17, 5, 128])
  # conv22
  conv22 = _transpose_conv_layer(conv21, 6, 2, 128, "decode_22")
  # conv23
  conv23 = _transpose_conv_layer(conv22, 6, 3, 64, "decode_23")
  # conv24
  conv24 = _transpose_conv_layer(conv23, 8, 4, 2, "decode_24")
  x_2 = tf.reshape(conv24, [-1, 17*4*3*2, 5*4*3*2, 2])
  x_2 = x_2[:,:401,:101,:]
  return x_2
  """
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice y_3 -> x_2
  # conv21
  conv21 = _transpose_conv_layer(y_2, 3, 1, 128, "decode_21")
  # conv22
  conv22 = _transpose_conv_layer(conv21, 3, 1, 128, "decode_22")
  # conv23
  conv23 = _transpose_conv_layer(conv22, 3, 1, 64, "decode_23")
  # conv24
  conv24 = _transpose_conv_layer(conv23, 3, 1, 32, "decode_24", True)
  conv24 = tf.reshape(conv24, [-1, 101, 26, 32])
  conv24 = PS(conv24, 4, 2)
  #x_2 = x_2[:,:401,:101,:]
  x_2 = conv24[:,:401,:101,:]
  return x_2

<commit_msg>lkjdsf<commit_after>
"""functions used to construct different architectures
"""

import tensorflow as tf
import numpy as np
import BasicConvLSTMCell

FLAGS = tf.app.flags.FLAGS

def _activation_summary(x):
  """Helper to create summaries for activations.

  Creates a summary that provides a histogram of activations.
  Creates a summary that measure the sparsity of activations.

  Args:
    x: Tensor
  Returns:
    nothing
  """
  tensor_name = x.op.name
  tf.histogram_summary(tensor_name + '/activations', x)
  tf.scalar_summary(tensor_name + '/sparsity', tf.nn.zero_fraction(x))

def _variable_on_cpu(name, shape, initializer):
  """Helper to create a Variable stored on CPU memory.

  Args:
    name: name of the variable
    shape: list of ints
    initializer: initializer for Variable

  Returns:
    Variable Tensor
  """
  with tf.device('/gpu:0'):
    var = tf.get_variable(name, shape, initializer=initializer)
  return var

def _variable_with_weight_decay(name, shape, stddev, wd):
  """Helper to create an initialized Variable with weight decay.

  Note that the Variable is initialized with a truncated normal distribution.
  A weight decay is added only if one is specified.

  Args:
    name: name of the variable
    shape: list of ints
    stddev: standard deviation of a truncated Gaussian
    wd: add L2Loss weight decay multiplied by this float. If None, weight
        decay is not added for this Variable.

  Returns:
    Variable Tensor
  """
  var = _variable_on_cpu(name, shape,
                         tf.truncated_normal_initializer(stddev=stddev))
  if wd:
    weight_decay = tf.mul(tf.nn.l2_loss(var), wd, name='weight_loss')
    weight_decay.set_shape([])
    tf.add_to_collection('losses', weight_decay)
  return var

def _conv_layer(inputs, kernel_size, stride, num_features, idx, linear = False):
  with tf.variable_scope('{0}_conv'.format(idx)) as scope:
    input_channels = inputs.get_shape()[3]

    weights = _variable_with_weight_decay('weights', shape=[kernel_size,kernel_size,input_channels,num_features],stddev=0.01, wd=FLAGS.weight_decay)
    biases = _variable_on_cpu('biases',[num_features],tf.constant_initializer(0.01))

    conv = tf.nn.conv2d(inputs, weights, strides=[1, stride, stride, 1], padding='SAME')
    conv_biased = tf.nn.bias_add(conv, biases)
    if linear:
      return conv_biased
    conv_rect = tf.nn.elu(conv_biased,name='{0}_conv'.format(idx))
    return conv_rect

def _transpose_conv_layer(inputs, kernel_size, stride, num_features, idx, linear = False):
  with tf.variable_scope('{0}_trans_conv'.format(idx)) as scope:
    input_channels = inputs.get_shape()[3]

    weights = _variable_with_weight_decay('weights', shape=[kernel_size,kernel_size,num_features,input_channels], stddev=0.01, wd=FLAGS.weight_decay)
    biases = _variable_on_cpu('biases',[num_features],tf.constant_initializer(0.01))
    batch_size = tf.shape(inputs)[0]
    output_shape = tf.pack([tf.shape(inputs)[0], tf.shape(inputs)[1]*stride, tf.shape(inputs)[2]*stride, num_features])
    conv = tf.nn.conv2d_transpose(inputs, weights, output_shape, strides=[1,stride,stride,1], padding='SAME')
    conv_biased = tf.nn.bias_add(conv, biases)
    if linear:
      return conv_biased
    conv_rect = tf.nn.elu(conv_biased,name='{0}_transpose_conv'.format(idx))
    return conv_rect

def _fc_layer(inputs, hiddens, idx, flat = False, linear = False):
  with tf.variable_scope('{0}_fc'.format(idx)) as scope:
    input_shape = inputs.get_shape().as_list()
    if flat:
      dim = input_shape[1]*input_shape[2]*input_shape[3]
      inputs_processed = tf.reshape(inputs, [-1,dim])
    else:
      dim = input_shape[1]
      inputs_processed = inputs

    weights = _variable_with_weight_decay('weights', shape=[dim,hiddens],stddev=0.01, wd=FLAGS.weight_decay)
    biases = _variable_on_cpu('biases', [hiddens], tf.constant_initializer(0.01))
    if linear:
      return tf.add(tf.matmul(inputs_processed,weights),biases,name=str(idx)+'_fc')

    ip = tf.add(tf.matmul(inputs_processed,weights),biases)
    return tf.nn.elu(ip,name=str(idx)+'_fc')

def _phase_shift(I, r):
    bsize, a, b, c = I.get_shape().as_list()
    bsize = tf.shape(I)[0] # Handling Dimension(None) type for undefined batch dim
    X = tf.reshape(I, (bsize, a, b, r, r))
    X = tf.transpose(X, (0, 1, 2, 4, 3))  # bsize, a, b, 1, 1
    X = tf.split(1, a, X)  # a, [bsize, b, r, r]
    X = tf.concat(2, [tf.squeeze(x) for x in X])  # bsize, b, a*r, r
    X = tf.split(1, b, X)  # b, [bsize, a*r, r]
    X = tf.concat(2, [tf.squeeze(x) for x in X])  # bsize, a*r, b*r
    return tf.reshape(X, (bsize, a*r, b*r, 1))

def PS(X, r, depth):
    Xc = tf.split(3, depth, X)
    X = tf.concat(3, [_phase_shift(x, r) for x in Xc])
    return X

def encoding_32x32x3(inputs, keep_prob):
  """Builds encoding part of ring net.
  Args:
    inputs: input to encoder
    keep_prob: dropout layer
  """
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice x_1 -> y_1
  x_1_image = inputs

  # conv1
  conv1 = _conv_layer(x_1_image, 8, 2, 64, "encode_1")
  # conv2
  conv2 = _conv_layer(conv1, 6, 2, 128, "encode_2")
  # conv3
  conv3 = _conv_layer(conv2, 6, 2, 128, "encode_3")
  # y_1
  y_1 = _fc_layer(conv3, FLAGS.compression_size, "encode_4", True)
  _activation_summary(y_1)
  return y_1

def encoding_32x32x1(inputs, keep_prob):
  """Builds encoding part of ring net.
  Args:
    inputs: input to encoder
    keep_prob: dropout layer
  """
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice x_1 -> y_1
  x_1_image = inputs

  # conv1
  conv1 = _conv_layer(x_1_image, 3, 2, 8, "encode_1")
  # conv2
  conv2 = _conv_layer(conv1, 3, 1, 16, "encode_2")
  # y_1
  y_1 = conv2
  _activation_summary(y_1)
  return y_1

def encoding_401x101x2(inputs, keep_prob):
  """Builds encoding part of ring net.
  Args:
    inputs: input to encoder
    keep_prob: dropout layer
  """
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice x_1 -> y_1
  x_1_image = inputs

  # conv1
  conv1 = _conv_layer(inputs, 3, 2, 64, "encode_1")
  # conv2
  conv2 = _conv_layer(conv1, 3, 1, 128, "encode_2")
  # conv3
  conv3 = _conv_layer(conv2, 3, 2, 256, "encode_3")
  # conv4
  conv4 = _conv_layer(conv3, 3, 1, 128, "encode_4")

  return conv4

def lstm_compression_32x32x3(y_1, hidden_state, keep_prob, encode=True):
  """Builds compressed dynamical system part of the net.
  Args:
    inputs: input to system
  """
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice y_1 -> y_2
  num_layers = FLAGS.num_layers

  y_1 = _fc_layer(y_1, FLAGS.lstm_size, "compression_1")

  with tf.variable_scope("compress_LSTM", initializer = tf.random_uniform_initializer(-0.01, 0.01)):
    with tf.device('/gpu:0'):
      lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(FLAGS.lstm_size, forget_bias=1.0)
      lstm_cell = tf.nn.rnn_cell.DropoutWrapper(lstm_cell, output_keep_prob=keep_prob)
      cell = tf.nn.rnn_cell.MultiRNNCell([lstm_cell] * num_layers)
      if hidden_state == None:
        batch_size = y_1.get_shape()[0]
        hidden_state = cell.zero_state(batch_size, tf.float32)

  y_2, new_state = cell(y_1, hidden_state)

  # residual connection
  #y_2 = y_2 + y_1

  y_2 = _fc_layer(y_2, FLAGS.compression_size, "compression_2")

  return y_2, new_state

def lstm_compression_32x32x1(y_1, hidden_state, keep_prob, encode=True):
  """Builds compressed dynamical system part of the net.
  Args:
    inputs: input to system
  """
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice y_1 -> y_2
  with tf.variable_scope("compress_LSTM", initializer = tf.random_uniform_initializer(-0.01, 0.01)):
    with tf.device('/gpu:0'):
      lstm_cell = BasicConvLSTMCell.BasicConvLSTMCell([int(y_1.get_shape()[1]),int(y_1.get_shape()[2])], [3,3], 16)
      if hidden_state == None:
        batch_size = y_1.get_shape()[0]
        hidden_state = lstm_cell.zero_state(batch_size, tf.float32)

  y_2, new_state = lstm_cell(y_1, hidden_state)

  return y_2, new_state

def lstm_compression_401x101x2(y_1, hidden_state, keep_prob, encode=True):
  """Builds compressed dynamical system part of the net.
  Args:
    inputs: input to system
  """
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice y_1 -> y_2
  with tf.variable_scope("compress_LSTM", initializer = tf.random_uniform_initializer(-0.01, 0.01)):
    with tf.device('/gpu:0'):
      lstm_cell = BasicConvLSTMCell.BasicConvLSTMCell([int(y_1.get_shape()[1]),int(y_1.get_shape()[2])], [3,3], 128)
      if hidden_state == None:
        batch_size = y_1.get_shape()[0]
        hidden_state = lstm_cell.zero_state(batch_size, tf.float32)

  y_2, new_state = lstm_cell(y_1, hidden_state)

  return y_2, new_state

def decoding_32x32x3(y_2):
  """Builds decoding part of ring net.
  Args:
    inputs: input to decoder
  """
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice y_3 -> x_2

  # fc21
  fc21 = _fc_layer(y_2, 2048, "decode_21")
  conv21 = tf.reshape(fc21, [-1, 4, 4, 128])
  # conv22
  conv22 = _transpose_conv_layer(conv21, 6, 2, 128, "decode_22")
  # conv23
  conv23 = _transpose_conv_layer(conv22, 6, 2, 64, "decode_23")
  # conv24
  conv24 = _transpose_conv_layer(conv23, 6, 2, 3, "decode_24")
  x_2 = tf.reshape(conv24, [-1, 32, 32, 3])
  return x_2

def decoding_32x32x1(y_2):
  """Builds decoding part of ring net.
  Args:
    inputs: input to decoder
  """
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice y_3 -> x_2

  # conv21
  conv21 = _transpose_conv_layer(y_2, 3, 1, 4, "decode_21")
  conv21 = tf.reshape(conv21, [-1, 16, 16, 4])
  conv21 = PS(conv21, 2, 1)
  #x_2 = x_2[:,:401,:101,:]
  x_2 = conv21
  return x_2

def decoding_401x101x2(y_2):
  """Builds decoding part of ring net.
  Args:
    inputs: input to decoder
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice y_3 -> x_2

  # fc21
  fc21 = _fc_layer(y_2, 17*5*128, "decode_21")
  conv21 = tf.reshape(fc21, [-1, 17, 5, 128])
  # conv22
  conv22 = _transpose_conv_layer(conv21, 6, 2, 128, "decode_22")
  # conv23
  conv23 = _transpose_conv_layer(conv22, 6, 3, 64, "decode_23")
  # conv24
  x_2 = tf.reshape(conv24, [-1, 17*4*3*2, 5*4*3*2, 2])
  x_2 = x_2[:,:401,:101,:]
  return x_2
  """
  #--------- Making the net -----------
  # x_1 -> y_1 -> y_2 -> x_2
  # this peice y_3 -> x_2
  # conv21
  conv21 = _transpose_conv_layer(y_2, 3, 1, 128, "decode_21")
  # conv22
  conv22 = _transpose_conv_layer(conv21, 3, 1, 128, "decode_22")
  # conv23
  conv23 = _transpose_conv_layer(conv22, 3, 1, 64, "decode_23")
  # conv24
  conv24 = _transpose_conv_layer(conv23, 3, 1, 32, "decode_24", True)
  conv24 = tf.reshape(conv24, [-1, 101, 26, 32])
  conv24 = PS(conv24, 4, 2)
  #x_2 = x_2[:,:401,:101,:]
  x_2 = conv24[:,:401,:101,:]
  return x_2

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#  Copyright 2011 Takeshi KOMIYA
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import re
import math
import unicodedata
from blockdiag.utils import Box, XY
from blockdiag.utils.fontmap import FontInfo

def is_zenkaku(char):
    u"""
    Detect given character is Japanese ZENKAKU character

    >>> is_zenkaku(u"A")
    False
    >>> is_zenkaku(u"あ")
    True
    """
    char_width = unicodedata.east_asian_width(char)
    return char_width in u"WFA"

def zenkaku_len(string):
    u"""
    Count Japanese ZENKAKU characters from string

    >>> zenkaku_len(u"abc")
    0
    >>> zenkaku_len(u"あいう")
    3
    >>> zenkaku_len(u"あいc")
    2
    """
    return len([x for x in string  if is_zenkaku(x)])

def hankaku_len(string):
    u"""
    Count non Japanese ZENKAKU characters from string

    >>> hankaku_len(u"abc")
    3
    >>> hankaku_len(u"あいう")
    0
    >>> hankaku_len(u"あいc")
    1
    """
    return len([x for x in string  if not is_zenkaku(x)])

def string_width(string):
    u"""
    Measure rendering width of string.
    Count ZENKAKU-character as 2-point and non ZENKAKU-character as 1-point

    >>> string_width(u"abc")
    3
    >>> string_width(u"あいう")
    6
    >>> string_width(u"あいc")
    5
    """
    width = 0
    for c in string:
        char_width = unicodedata.east_asian_width(c)
        if char_width in u"WFA":
            width += 2
        else:
            width += 1

    return width

class TextFolder(object):
    def __init__(self, box, string, font, **kwargs):
        self.box = box
        self.string = string
        self.font = font
        self.scale = 1
        self.scale = 1
        self.halign = kwargs.get('halign', 'center')
        self.valign = kwargs.get('valign', 'center')
        self.padding = kwargs.get('padding', 12)
        self.line_spacing = kwargs.get('line_spacing', 2)

        if kwargs.get('adjustBaseline'):
            self.adjustBaseline = True
        else:
            self.adjustBaseline = False

        self._result = self._lines()

    def textsize(self, string):
        u"""
        Measure rendering size (width and height) of line.
        Returned size will not be exactly as rendered text size,
        Because this method does not use fonts to measure size.

        >>> box = [0, 0, 100, 50]
        >>> _font = FontInfo('serif', None, 11)
        >>> TextFolder(box, "", _font).textsize(u"abc")
        (19, 11)
        >>> TextFolder(box, "", _font).textsize(u"あいう")
        (33, 11)
        >>> TextFolder(box, "", _font).textsize(u"あいc")
        (29, 11)
        >>> font = FontInfo('serif', None, 24)
        >>> TextFolder(box, "", font).textsize(u"abc")
        (40, 24)
        >>> font = FontInfo('serif', None, 18)
        >>> TextFolder(box, "", font).textsize(u"あいう")
        (54, 18)
        """
        width = zenkaku_len(string) * self.font.size + \
                hankaku_len(string) * self.font.size * 0.55
        return (int(math.ceil(width)), self.font.size)

    def height(self):
        u"""
        Measure rendering height of text.

        If texts is heighter than bounding box,
        jut out lines will be cut off.

        >>> box = [0, 0, 100, 50]
        >>> _font = FontInfo('serif', None, 11)
        >>> TextFolder(box, u"abc", _font).height()
        11
        >>> TextFolder(box, u"abc\\ndef", _font).height()
        24
        >>> TextFolder(box, u"abc\\n\\ndef", _font).height()
        37
        >>> TextFolder(box, u"abc\\ndef\\nghi\\njkl", _font).height()
        50
        >>> TextFolder(box, u"abc\\ndef\\nghi\\njkl\\nmno", _font).height()
        50
        >>> font = FontInfo('serif', None, 24)
        >>> TextFolder(box, u"abc", font).height()
        24
        >>> TextFolder(box, u"abc\\ndef", _font, line_spacing=8).height()
        30
        >>> font = FontInfo('serif', None, 15)
        >>> TextFolder(box, u"abc\\ndef", font, line_spacing=8).height()
        38
        """
        height = 0
        for string in self._result:
            height += self.textsize(string)[1]

        if len(self._result) > 1:
            height += (len(self._result) - 1) * self.line_spacing

        return height

    @property
    def lines(self):
        size = XY(self.box[2] - self.box[0], self.box[3] - self.box[1])

        if self.valign == 'top':
            height = 0
        elif self.valign == 'bottom':
            height = size.y - self.height()
        else:
            height = int(math.ceil((size.y - self.height()) / 2.0))
        base_xy = XY(self.box[0], self.box[1])

        for string in self._result:
            textsize = self.textsize(string)

            halign = size.x - textsize[0] * self.scale
            if self.halign == 'left':
                x = self.padding
            elif self.halign == 'right':
                x = halign - self.padding
            else:
                x = int(math.ceil(halign / 2.0))

            if self.adjustBaseline:
                height += textsize[1]
            draw_xy = XY(base_xy.x + x, base_xy.y + height)

            yield string, draw_xy

            if self.adjustBaseline:
                height += self.line_spacing
            else:
                height += textsize[1] + self.line_spacing

    @property
    def outlinebox(self):
        corners = []
        for string, xy in self.lines:
            textsize = self.textsize(string)
            width = textsize[0] * self.scale
            height = textsize[1] * self.scale

            if self.adjustBaseline:
                xy = XY(xy.x, xy.y - textsize[1])

            corners.append(xy)
            corners.append(XY(xy.x + width, xy.y + height))

        if corners:
            x_margin = 4  # MAGIC number
            y_margin = 2  # MAGIC number
            box = Box(min(p.x for p in corners) - x_margin,
                      min(p.y for p in corners) - y_margin,
                      max(p.x for p in corners) + x_margin,
                      max(p.y for p in corners) + y_margin)
        else:
            box = [self.box[0], self.box[1], self.box[0], self.box[1]]

        return box

    def _splitlines(self):
        u"""
        Split text to lines as generator.
        Every line will be stripped.
        If text includes characters "\n", treat as line separator.

        >>> box = [0, 0, 100, 50]
        >>> ft = FontInfo('serif', None, 11)
        >>> [l for l in TextFolder(box, u"abc", ft)._splitlines()]
        [u'abc']
        >>> [l for l in TextFolder(box, u"abc\\ndef", ft)._splitlines()]
        [u'abc', u'def']
        >>> [l for l in TextFolder(box, u"abc\\\\ndef", ft)._splitlines()]
        [u'abc', u'def']
        >>> [l for l in TextFolder(box, u" abc \\n def ", ft)._splitlines()]
        [u'abc', u'def']
        >>> [l for l in TextFolder(box, u" \\nabc\\\\ndef", ft)._splitlines()]
        [u'abc', u'def']
        >>> [l for l in TextFolder(box, u" \\\\nab \\\\ncd", ft)._splitlines()]
        [u'', u'ab', u'cd']
        >>> [l for l in TextFolder(box, u"abc\\\\\\\\ndef", ft)._splitlines()]
        [u'abc\\\\ndef']
        >>> [l for l in TextFolder(box, u"abc\xa5\\\\ndef", ft)._splitlines()]
        [u'abc\\\\ndef']
        """
        string = re.sub('^\s*(.*?)\s*$', '\\1', self.string)
        string = re.sub('(?:\xa5|\\\\){2}', '\x00', string)
        string = re.sub('(?:\xa5|\\\\)n', '\n', string)
        for line in string.splitlines():
            yield re.sub('\x00', '\\\\', line).strip()

    def _lines(self):
        lines = []
        size = (self.box[2] - self.box[0], self.box[3] - self.box[1])

        height = 0
        truncated = 0
        for line in self._splitlines():
            while True:
                string = line.strip()
                for i in range(0, len(string)):
                    length = len(string) - i
                    metrics = self.textsize(string[0:length])

                    if metrics[0] <= size[0]:
                        break
                else:
                    length = 0
                    metrics = self.textsize(u" ")

                if size[1] < height + metrics[1]:
                    truncated = 1
                    break

                lines.append(string[0:length])
                height += metrics[1] + self.line_spacing

                line = string[length:]
                if line == "":
                    break

        # truncate last line.
        if len(lines) == 0:
            pass
        elif truncated:
            string = lines.pop()
            for i in range(0, len(string)):
                if i == 0:
                    truncated = string + ' ...'
                else:
                    truncated = string[0:-i] + ' ...'

                metrics = self.textsize(truncated)
                if metrics[0] <= size[0]:
                    lines.append(truncated)
                    break

        return lines
<commit_msg>Fix TextFolder does not layout text to center<commit_after># -*- coding: utf-8 -*-
#  Copyright 2011 Takeshi KOMIYA
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import re
import math
import unicodedata
from blockdiag.utils import Box, XY
from blockdiag.utils.fontmap import FontInfo

def is_zenkaku(char):
    u"""
    Detect given character is Japanese ZENKAKU character

    >>> is_zenkaku(u"A")
    False
    >>> is_zenkaku(u"あ")
    True
    """
    char_width = unicodedata.east_asian_width(char)
    return char_width in u"WFA"

def zenkaku_len(string):
    u"""
    Count Japanese ZENKAKU characters from string

    >>> zenkaku_len(u"abc")
    0
    >>> zenkaku_len(u"あいう")
    3
    >>> zenkaku_len(u"あいc")
    2
    """
    return len([x for x in string  if is_zenkaku(x)])

def hankaku_len(string):
    u"""
    Count non Japanese ZENKAKU characters from string

    >>> hankaku_len(u"abc")
    3
    >>> hankaku_len(u"あいう")
    0
    >>> hankaku_len(u"あいc")
    1
    """
    return len([x for x in string  if not is_zenkaku(x)])

def string_width(string):
    u"""
    Measure rendering width of string.
    Count ZENKAKU-character as 2-point and non ZENKAKU-character as 1-point

    >>> string_width(u"abc")
    3
    >>> string_width(u"あいう")
    6
    >>> string_width(u"あいc")
    5
    """
    width = 0
    for c in string:
        char_width = unicodedata.east_asian_width(c)
        if char_width in u"WFA":
            width += 2
        else:
            width += 1

    return width

class TextFolder(object):
    def __init__(self, box, string, font, **kwargs):
        self.box = box
        self.string = string
        self.font = font
        self.scale = 1
        self.scale = 1
        self.halign = kwargs.get('halign', 'center')
        self.valign = kwargs.get('valign', 'center')
        self.padding = kwargs.get('padding', 8)
        self.line_spacing = kwargs.get('line_spacing', 2)

        if kwargs.get('adjustBaseline'):
            self.adjustBaseline = True
        else:
            self.adjustBaseline = False

        self._result = self._lines()

    def textsize(self, string):
        u"""
        Measure rendering size (width and height) of line.
        Returned size will not be exactly as rendered text size,
        Because this method does not use fonts to measure size.

        >>> box = [0, 0, 100, 50]
        >>> _font = FontInfo('serif', None, 11)
        >>> TextFolder(box, "", _font).textsize(u"abc")
        (19, 11)
        >>> TextFolder(box, "", _font).textsize(u"あいう")
        (33, 11)
        >>> TextFolder(box, "", _font).textsize(u"あいc")
        (29, 11)
        >>> font = FontInfo('serif', None, 24)
        >>> TextFolder(box, "", font).textsize(u"abc")
        (40, 24)
        >>> font = FontInfo('serif', None, 18)
        >>> TextFolder(box, "", font).textsize(u"あいう")
        (54, 18)
        """
        width = zenkaku_len(string) * self.font.size + \
                hankaku_len(string) * self.font.size * 0.55
        return (int(math.ceil(width)), self.font.size)

    def height(self):
        u"""
        Measure rendering height of text.

        If texts is heighter than bounding box,
        jut out lines will be cut off.

        >>> box = [0, 0, 100, 50]
        >>> _font = FontInfo('serif', None, 11)
        >>> TextFolder(box, u"abc", _font).height()
        11
        >>> TextFolder(box, u"abc\\ndef", _font).height()
        24
        >>> TextFolder(box, u"abc\\n\\ndef", _font).height()
        37
        >>> TextFolder(box, u"abc\\ndef\\nghi\\njkl", _font).height()
        50
        >>> TextFolder(box, u"abc\\ndef\\nghi\\njkl\\nmno", _font).height()
        50
        >>> font = FontInfo('serif', None, 24)
        >>> TextFolder(box, u"abc", font).height()
        24
        >>> TextFolder(box, u"abc\\ndef", _font, line_spacing=8).height()
        30
        >>> font = FontInfo('serif', None, 15)
        >>> TextFolder(box, u"abc\\ndef", font, line_spacing=8).height()
        38
        """
        height = 0
        for string in self._result:
            height += self.textsize(string)[1]

        if len(self._result) > 1:
            height += (len(self._result) - 1) * self.line_spacing

        return height

    @property
    def lines(self):
        size = XY(self.box[2] - self.box[0], self.box[3] - self.box[1])

        if self.valign == 'top':
            height = self.line_spacing
        elif self.valign == 'bottom':
            height = size.y - self.height() - self.line_spacing
        else:
            height = int(math.ceil((size.y - self.height()) / 2.0))
        base_xy = XY(self.box[0], self.box[1])

        for string in self._result:
            textsize = self.textsize(string)

            halign = size.x - textsize[0] * self.scale
            if self.halign == 'left':
                x = self.padding
            elif self.halign == 'right':
                x = halign - self.padding
            else:
                x = int(math.ceil(halign / 2.0))

            if self.adjustBaseline:
                height += textsize[1]
            draw_xy = XY(base_xy.x + x, base_xy.y + height)

            yield string, draw_xy

            if self.adjustBaseline:
                height += self.line_spacing
            else:
                height += textsize[1] + self.line_spacing

    @property
    def outlinebox(self):
        corners = []
        for string, xy in self.lines:
            textsize = self.textsize(string)
            width = textsize[0] * self.scale
            height = textsize[1] * self.scale

            if self.adjustBaseline:
                xy = XY(xy.x, xy.y - textsize[1])

            corners.append(xy)
            corners.append(XY(xy.x + width, xy.y + height))

        if corners:
            box = Box(min(p.x for p in corners) - self.padding,
                      min(p.y for p in corners) - self.line_spacing,
                      max(p.x for p in corners) + self.padding,
                      max(p.y for p in corners) + self.line_spacing)
        else:
            box = [self.box[0], self.box[1], self.box[0], self.box[1]]

        return box

    def _splitlines(self):
        u"""
        Split text to lines as generator.
        Every line will be stripped.
        If text includes characters "\n", treat as line separator.

        >>> box = [0, 0, 100, 50]
        >>> ft = FontInfo('serif', None, 11)
        >>> [l for l in TextFolder(box, u"abc", ft)._splitlines()]
        [u'abc']
        >>> [l for l in TextFolder(box, u"abc\\ndef", ft)._splitlines()]
        [u'abc', u'def']
        >>> [l for l in TextFolder(box, u"abc\\\\ndef", ft)._splitlines()]
        [u'abc', u'def']
        >>> [l for l in TextFolder(box, u" abc \\n def ", ft)._splitlines()]
        [u'abc', u'def']
        >>> [l for l in TextFolder(box, u" \\nabc\\\\ndef", ft)._splitlines()]
        [u'abc', u'def']
        >>> [l for l in TextFolder(box, u" \\\\nab \\\\ncd", ft)._splitlines()]
        [u'', u'ab', u'cd']
        >>> [l for l in TextFolder(box, u"abc\\\\\\\\ndef", ft)._splitlines()]
        [u'abc\\\\ndef']
        >>> [l for l in TextFolder(box, u"abc\xa5\\\\ndef", ft)._splitlines()]
        [u'abc\\\\ndef']
        """
        string = re.sub('^\s*(.*?)\s*$', '\\1', self.string)
        string = re.sub('(?:\xa5|\\\\){2}', '\x00', string)
        string = re.sub('(?:\xa5|\\\\)n', '\n', string)
        for line in string.splitlines():
            yield re.sub('\x00', '\\\\', line).strip()

    def _lines(self):
        lines = []
        size = (self.box[2] - self.box[0], self.box[3] - self.box[1])

        height = 0
        truncated = 0
        for line in self._splitlines():
            while True:
                string = line.strip()
                for i in range(0, len(string)):
                    length = len(string) - i
                    metrics = self.textsize(string[0:length])

                    if metrics[0] <= size[0]:
                        break
                else:
                    length = 0
                    metrics = self.textsize(u" ")

                if size[1] < height + metrics[1]:
                    truncated = 1
                    break

                lines.append(string[0:length])
                height += metrics[1] + self.line_spacing

                line = string[length:]
                if line == "":
                    break

        # truncate last line.
        if len(lines) == 0:
            pass
        elif truncated:
            string = lines.pop()
            for i in range(0, len(string)):
                if i == 0:
                    truncated = string + ' ...'
                else:
                    truncated = string[0:-i] + ' ...'

                metrics = self.textsize(truncated)
                if metrics[0] <= size[0]:
                    lines.append(truncated)
                    break

        return lines
<|endoftext|>


<commit_before>from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse, parse_qs
from threading import Thread
from hangups.ui.utils import get_conv_name
from pyslack import SlackClient
try:
    import emoji
except ImportError:
    print("Error: You need to install the python emoji library!")

import ssl
import asyncio
import logging
import hangups

import re
from urllib.request import urlopen
import json

""" Slack plugin for listening to hangouts and slack and syncing messages between the two.
config.json will have to be configured as follows:
"slack": [{
  "certfile": null,
  "name": SERVER_NAME,
  "port": LISTENING_PORT,
  "key": SLACK_API_KEY,
  "channel": #SLACK_CHANNEL_NAME,
  "synced_conversations": ["CONV_ID1", "CONV_ID2"]
}]

You can (theoretically) set up as many slack sinks per bot as you like, by extending the list"""

def _initialise(Handlers, bot=None):
    if bot:
        _start_slack_sinks(bot)
    else:
        print("Slack sinks could not be initialized.")
    Handlers.register_handler(_handle_slackout)
    return []

def _start_slack_sinks(bot):
    # Start and asyncio event loop
    loop = asyncio.get_event_loop()

    slack_sink = bot.get_config_option('slack')
    itemNo = -1
    threads = []

    if isinstance(slack_sink, list):
        for sinkConfig in slack_sink:
            itemNo += 1

            try:
                certfile = sinkConfig["certfile"]
                if not certfile:
                    print(_("config.slack[{}].certfile must be configured").format(itemNo))
                    continue
                name = sinkConfig["name"]
                port = sinkConfig["port"]
            except KeyError as e:
                print(_("config.slack[{}] missing keyword").format(itemNo), e)
                continue

            # start up slack listener in a separate thread
            print("_start_slack_sinks()")
            t = Thread(target=start_listening, args=(
              bot,
              loop,
              name,
              port,
              certfile))

            t.daemon = True
            t.start()

            threads.append(t)

    message = _("_start_slack_sinks(): {} sink thread(s) started").format(len(threads))
    logging.info(message)

def start_listening(bot=None, loop=None, name="", port=8014, certfile=None):
    webhook = webhookReceiver

    if loop:
        asyncio.set_event_loop(loop)

    if bot:
        webhook._bot = bot

    try:
        httpd = HTTPServer((name, port), webhook)

        httpd.socket = ssl.wrap_socket(
          httpd.socket,
          certfile=certfile,
          server_side=True)

        sa = httpd.socket.getsockname()
        print(_("listener: slack sink on {}, port {}...").format(sa[0], sa[1]))

        httpd.serve_forever()
    except IOError:
        # do not run sink without https!
        print(_("listener: slack : pem file possibly missing or broken (== '{}')").format(certfile))
        httpd.socket.close()
    except OSError as e:
        # Could not connect to HTTPServer!
        print(_("listener: slack : requested access could not be assigned. Is something else using that port? (== '{}:{}')").format(name, port))
    except KeyboardInterrupt:
        httpd.socket.close()

def _slack_repeater_cleaner(bot, event, id):
    event.text = event.text.split(":", maxsplit=1)[1].strip()
    event.from_bot = False
    event._slack_no_repeat = True

class webhookReceiver(BaseHTTPRequestHandler):
    _bot = None

    def _handle_incoming(self, path, query_string, payload):
        path = path.split("/")
        conversation_id = path[1]
        if conversation_id is None:
            print(_("conversation id must be provided as part of path"))
            return

        if "text" in payload:
            try:
                text = emoji.emojize(str(payload["text"][0]), use_aliases=True)
            except NameError: # emoji library likely missing
                text = str(payload["text"][0])

            if "user_name" in payload:
                if "slackbot" not in str(payload["user_name"][0]):
                    text = self._remap_internal_slack_ids(text)
                    response = "" + str(payload["user_name"][0]) + ": " + text
                    self._scripts_push(conversation_id, response)

    def _remap_internal_slack_ids(self, text):
        text = self._slack_label_users(text)
        text = self._slack_label_channels(text)
        return text

    def _slack_label_users(self, text):
        for fragment in re.findall("<@[A-Z0-9]+>", text):
            id = fragment[2:-1]
            username = self._slack_get_label(id, "user")
            text = text.replace(fragment, username)
        return text

    def _slack_label_channels(self, text):
        for fragment in re.findall("<#[A-Z0-9]+>", text):
            id = fragment[2:-1]
            username = self._slack_get_label(id, "channel")
            text = text.replace(fragment, username)
        return text

    _slack_cache = {"user": {}, "channel": {}}

    def _slack_get_label(self, id, type_str):
        # hacky way to get the first token:
        slack_sink_configuration = self._bot.get_config_option('slack')
        token = slack_sink_configuration[0]["key"]

        prefix = "?"
        if type_str == "user":
            url = 'https://slack.com/api/users.info?token=' + token + '&user=' + id
            prefix = "@"
        elif type_str == "channel":
            url = 'https://slack.com/api/channels.info?token=' + token + '&channel=' + id
            prefix = "#"
        else:
            raise ValueError('unknown label type_str')

        label = "UNKNOWN"
        if id in self._slack_cache[type_str]:
            label = self._slack_cache[type_str][id]
            print("_slack_get_label(): from cache {} = {}".format(id, label))
        else:
            try:
                response = urlopen(url)
                json_string = str(response.read().decode('utf-8'))
                data = json.loads(json_string)
                if type_str in data:
                    label = data[type_str]["name"]
                    self._slack_cache[type_str][id] = label
                    print("_slack_get_label(): from API {} = {}".format(id, label))
            except Exception as e:
                print("EXCEPTION in _slack_get_label(): {}".format(e))

        return prefix + label

    def _scripts_push(self, conversation_id, message):
        webhookReceiver._bot.send_html_to_conversation(
            conversation_id,
            message + self._bot.call_shared("reprocessor.attach_reprocessor", _slack_repeater_cleaner),
            context = {'base': {'tags': ['slack', 'relay'], 'source': 'slack', 'importance': 50}})

    def do_POST(self):
        """
           receives post, handles it
       """
        print(_('receiving POST...'))
        data_string = self.rfile.read(int(self.headers['Content-Length'])).decode('UTF-8')
        self.send_response(200)
        message = bytes('OK', 'UTF-8')
        self.send_header("Content-type", "text")
        self.send_header("Content-length", str(len(message)))
        self.end_headers()
        self.wfile.write(message)
        print(_('connection closed'))

        # parse requested path + query string
        _parsed = urlparse(self.path)
        path = _parsed.path
        query_string = parse_qs(_parsed.query)

        print(_("incoming path: {}").format(path))

        # parse incoming data
        payload = parse_qs(data_string)

        print(_("payload {}").format(payload))

        self._handle_incoming(path, query_string, payload)

@asyncio.coroutine
def _handle_slackout(bot, event, command):
    if "_slack_no_repeat" in dir(event) and event._slack_no_repeat:
        return

    """forward messages to slack over webhook"""

    slack_sink = bot.get_config_option('slack')

    if isinstance(slack_sink, list):
        for sinkConfig in slack_sink:

            try:
                slackkey = sinkConfig["key"]
                channel = sinkConfig["channel"]
                convlist = sinkConfig["synced_conversations"]

                if event.conv_id in convlist:
                    fullname = event.user.full_name
                    response = yield from bot._client.getentitybyid([event.user_id.chat_id])
                    try:
                       photo_url = "http:" + response.entities[0].properties.photo_url
                    except Exception as e:
                        print("Slack: Could not pull avatar for {}".format(fullname))

                    client = SlackClient(slackkey)
                    client.chat_post_message(channel, event.text, username=fullname, icon_url=photo_url)
            except Exception as e:
                print("Could not handle slackout with key {} between {} and {}. is config.json properly configured?".format(slackkey,channel,convlist))
<commit_msg>add slack source name (name of user who fired the message)<commit_after>from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse, parse_qs
from threading import Thread
from hangups.ui.utils import get_conv_name
from pyslack import SlackClient
try:
    import emoji
except ImportError:
    print("Error: You need to install the python emoji library!")

import ssl
import asyncio
import logging
import hangups

import re
from urllib.request import urlopen
import json

""" Slack plugin for listening to hangouts and slack and syncing messages between the two.
config.json will have to be configured as follows:
"slack": [{
  "certfile": null,
  "name": SERVER_NAME,
  "port": LISTENING_PORT,
  "key": SLACK_API_KEY,
  "channel": #SLACK_CHANNEL_NAME,
  "synced_conversations": ["CONV_ID1", "CONV_ID2"]
}]

You can (theoretically) set up as many slack sinks per bot as you like, by extending the list"""

def _initialise(Handlers, bot=None):
    if bot:
        _start_slack_sinks(bot)
    else:
        print("Slack sinks could not be initialized.")
    Handlers.register_handler(_handle_slackout)
    return []

def _start_slack_sinks(bot):
    # Start and asyncio event loop
    loop = asyncio.get_event_loop()

    slack_sink = bot.get_config_option('slack')
    itemNo = -1
    threads = []

    if isinstance(slack_sink, list):
        for sinkConfig in slack_sink:
            itemNo += 1

            try:
                certfile = sinkConfig["certfile"]
                if not certfile:
                    print(_("config.slack[{}].certfile must be configured").format(itemNo))
                    continue
                name = sinkConfig["name"]
                port = sinkConfig["port"]
            except KeyError as e:
                print(_("config.slack[{}] missing keyword").format(itemNo), e)
                continue

            # start up slack listener in a separate thread
            print("_start_slack_sinks()")
            t = Thread(target=start_listening, args=(
              bot,
              loop,
              name,
              port,
              certfile))

            t.daemon = True
            t.start()

            threads.append(t)

    message = _("_start_slack_sinks(): {} sink thread(s) started").format(len(threads))
    logging.info(message)

def start_listening(bot=None, loop=None, name="", port=8014, certfile=None):
    webhook = webhookReceiver

    if loop:
        asyncio.set_event_loop(loop)

    if bot:
        webhook._bot = bot

    try:
        httpd = HTTPServer((name, port), webhook)

        httpd.socket = ssl.wrap_socket(
          httpd.socket,
          certfile=certfile,
          server_side=True)

        sa = httpd.socket.getsockname()
        print(_("listener: slack sink on {}, port {}...").format(sa[0], sa[1]))

        httpd.serve_forever()
    except IOError:
        # do not run sink without https!
        print(_("listener: slack : pem file possibly missing or broken (== '{}')").format(certfile))
        httpd.socket.close()
    except OSError as e:
        # Could not connect to HTTPServer!
        print(_("listener: slack : requested access could not be assigned. Is something else using that port? (== '{}:{}')").format(name, port))
    except KeyboardInterrupt:
        httpd.socket.close()

def _slack_repeater_cleaner(bot, event, id):
    event_tokens = event.text.split(":", maxsplit=1)
    event.text = event_tokens[1].strip()
    event.from_bot = False
    event._slack_no_repeat = True
    event._external_source = event_tokens[0].strip() + "@slack"

class webhookReceiver(BaseHTTPRequestHandler):
    _bot = None

    def _handle_incoming(self, path, query_string, payload):
        path = path.split("/")
        conversation_id = path[1]
        if conversation_id is None:
            print(_("conversation id must be provided as part of path"))
            return

        if "text" in payload:
            try:
                text = emoji.emojize(str(payload["text"][0]), use_aliases=True)
            except NameError: # emoji library likely missing
                text = str(payload["text"][0])

            if "user_name" in payload:
                if "slackbot" not in str(payload["user_name"][0]):
                    text = self._remap_internal_slack_ids(text)
                    response = "" + str(payload["user_name"][0]) + ": " + text
                    self._scripts_push(conversation_id, response)

    def _remap_internal_slack_ids(self, text):
        text = self._slack_label_users(text)
        text = self._slack_label_channels(text)
        return text

    def _slack_label_users(self, text):
        for fragment in re.findall("<@[A-Z0-9]+>", text):
            id = fragment[2:-1]
            username = self._slack_get_label(id, "user")
            text = text.replace(fragment, username)
        return text

    def _slack_label_channels(self, text):
        for fragment in re.findall("<#[A-Z0-9]+>", text):
            id = fragment[2:-1]
            username = self._slack_get_label(id, "channel")
            text = text.replace(fragment, username)
        return text

    _slack_cache = {"user": {}, "channel": {}}

    def _slack_get_label(self, id, type_str):
        # hacky way to get the first token:
        slack_sink_configuration = self._bot.get_config_option('slack')
        token = slack_sink_configuration[0]["key"]

        prefix = "?"
        if type_str == "user":
            url = 'https://slack.com/api/users.info?token=' + token + '&user=' + id
            prefix = "@"
        elif type_str == "channel":
            url = 'https://slack.com/api/channels.info?token=' + token + '&channel=' + id
            prefix = "#"
        else:
            raise ValueError('unknown label type_str')

        label = "UNKNOWN"
        if id in self._slack_cache[type_str]:
            label = self._slack_cache[type_str][id]
            print("_slack_get_label(): from cache {} = {}".format(id, label))
        else:
            try:
                response = urlopen(url)
                json_string = str(response.read().decode('utf-8'))
                data = json.loads(json_string)
                if type_str in data:
                    label = data[type_str]["name"]
                    self._slack_cache[type_str][id] = label
                    print("_slack_get_label(): from API {} = {}".format(id, label))
            except Exception as e:
                print("EXCEPTION in _slack_get_label(): {}".format(e))

        return prefix + label

    def _scripts_push(self, conversation_id, message):
        webhookReceiver._bot.send_html_to_conversation(
            conversation_id,
            message + self._bot.call_shared("reprocessor.attach_reprocessor", _slack_repeater_cleaner),
            context = {'base': {'tags': ['slack', 'relay'], 'source': 'slack', 'importance': 50}})

    def do_POST(self):
        """
           receives post, handles it
       """
        print(_('receiving POST...'))
        data_string = self.rfile.read(int(self.headers['Content-Length'])).decode('UTF-8')
        self.send_response(200)
        message = bytes('OK', 'UTF-8')
        self.send_header("Content-type", "text")
        self.send_header("Content-length", str(len(message)))
        self.end_headers()
        self.wfile.write(message)
        print(_('connection closed'))

        # parse requested path + query string
        _parsed = urlparse(self.path)
        path = _parsed.path
        query_string = parse_qs(_parsed.query)

        print(_("incoming path: {}").format(path))

        # parse incoming data
        payload = parse_qs(data_string)

        print(_("payload {}").format(payload))

        self._handle_incoming(path, query_string, payload)

@asyncio.coroutine
def _handle_slackout(bot, event, command):
    if "_slack_no_repeat" in dir(event) and event._slack_no_repeat:
        return

    """forward messages to slack over webhook"""

    slack_sink = bot.get_config_option('slack')

    if isinstance(slack_sink, list):
        for sinkConfig in slack_sink:

            try:
                slackkey = sinkConfig["key"]
                channel = sinkConfig["channel"]
                convlist = sinkConfig["synced_conversations"]

                if event.conv_id in convlist:
                    fullname = event.user.full_name
                    response = yield from bot._client.getentitybyid([event.user_id.chat_id])
                    try:
                       photo_url = "http:" + response.entities[0].properties.photo_url
                    except Exception as e:
                        print("Slack: Could not pull avatar for {}".format(fullname))

                    client = SlackClient(slackkey)
                    client.chat_post_message(channel, event.text, username=fullname, icon_url=photo_url)
            except Exception as e:
                print("Could not handle slackout with key {} between {} and {}. is config.json properly configured?".format(slackkey,channel,convlist))
<|endoftext|>


<commit_before>import numpy as np
from scipy.special import gamma, gammaln

def W(t, x, alpha, sigma, tau):
    if tau > 0:
        logout = np.log(alpha) + np.log(1. - np.exp(-tau * (x - t))) + (-1 - sigma) * np.log(t) + (-t * tau) - np.log(
            tau) - gammaln(1. - sigma)
    else:
        logout = np.log(alpha) - gammaln(1. - sigma) - np.log(sigma) + np.log(t ** (-sigma) - x ** (-sigma))
    return np.exp(logout)

def inv_W(t, x, alpha, sigma, tau):
    if tau > 0:
        out = t - 1. / tau * np.log(1. - gamma(1. - sigma) * x * tau / (alpha * t ** (-1. - sigma) * np.exp(-t * tau)))
    else:
        logout = -1. / sigma * np.log(t ** (-sigma) - sigma * gamma(1. - sigma) / alpha * x)
        out = np.exp(logout)
    return out

def GGPrnd(alpha, sigma, tau, T=0):
    """
    GGPrnd samples points of a generalised gamma process

    Samples the points of the GGP with Levy measure
      alpha/Gamma(1-sigma) * w^(-1-sigma) * exp(-tau*w)

    For sigma>=0, it samples points above the threshold T>0 using the adaptive
    thinning strategy described in Favaro and Teh (2013).

    Convert the same function used in BNPGraph matlab package by Francois Caron
    http://www.stats.ox.ac.uk/~caron/code/bnpgraph/index.html

    Reference:
    S. Favaro and Y.W. Teh. MCMC for normalized random measure mixture
        models. Statistical Science, vol.28(3), pp.335-359, 2013.
    :param alpha: positive scalar
    :param sigma: real in (-Inf, 1)
    :param tau: positive scalar
    :param T: truncation threshold; positive scalar
    :return:
        N: weights from the GGP
        T: threshold
    """

    # finite activity GGP, don't need to truncate
    if sigma < 0:
        rate = np.exp(np.log(alpha) - np.log(-sigma) + sigma + np.log(tau))
        K = np.random.poisson(rate)
        N = np.random.gamma(-sigma, scale=1. / tau, size=K)
        T = 0
        return N, T

    # infinite activity GGP
    if T == 0:
        # set the threshold automatically
        # Number of jumps of order alpha/sigma/Gamma(1-sigma) * T^{-sigma} for sigma > 0
        # and alpha*log(T) for sigma = 0
        if sigma > .1:
            Njumps = 20000
            T = np.exp(1. / sigma * (np.log(alpha) - np.log(sigma) - gammaln(1. - sigma) - np.log(Njumps)))
        else:
            T = 1e-10
            if sigma > 0:
                Njumps = np.floor(alpha / sigma / gamma(1. - sigma) * T ** (-sigma))
            else:
                Njumps = np.floor(-alpha * np.log(T))
    else:
        if T <= 0:
            raise ValueError("Threshold T must be strictly positive")
        if sigma > 1e-3:
            Njumps = np.floor(alpha / sigma / gamma(1. - sigma) * T ** (-sigma))
        else:
            Njumps = np.floor(-alpha * np.log(T))
        if Njumps > 1e7:
            raise Warning("Expected number of jumps = %d" % Njumps)

    # Adaptive thinning strategy
    N = np.zeros(np.ceil(Njumps + 3 * np.sqrt(Njumps)))
    k = 0
    t = T
    count = 0
    while True:
        e = -np.log(np.random.random())  # Sample exponential random variable of unit rate

        if e > W(t, np.inf, alpha, sigma, tau):
            N = N[0:k]
            return N, T
        else:
            t_new = inv_W(t, e, alpha, sigma, tau)

        if tau == 0 or np.log(np.random.random()) < (-1. - sigma) * np.log(t_new / t):
            # if tau>0, adaptive thinning - otherwise accept always
            N[k] = t_new
            k += 1

        t = t_new
        count += 1
        if count > 10e8:
            # If too many computation, we lower the threshold T and rerun
            T /= 10.
            N, T = GGPrnd(alpha, sigma, tau, T)

    return N, T
<commit_msg>fix index error in adaptive thinning.<commit_after>import numpy as np
from scipy.special import gamma, gammaln

def W(t, x, alpha, sigma, tau):
    if tau > 0:
        logout = np.log(alpha) + np.log(1. - np.exp(-tau * (x - t))) + (-1 - sigma) * np.log(t) + (-t * tau) - np.log(
            tau) - gammaln(1. - sigma)
    else:
        logout = np.log(alpha) - gammaln(1. - sigma) - np.log(sigma) + np.log(t ** (-sigma) - x ** (-sigma))
    return np.exp(logout)

def inv_W(t, x, alpha, sigma, tau):
    if tau > 0:
        out = t - 1. / tau * np.log(1. - gamma(1. - sigma) * x * tau / (alpha * t ** (-1. - sigma) * np.exp(-t * tau)))
    else:
        logout = -1. / sigma * np.log(t ** (-sigma) - sigma * gamma(1. - sigma) / alpha * x)
        out = np.exp(logout)
    return out

def GGPrnd(alpha, sigma, tau, T=0):
    """
    GGPrnd samples points of a generalised gamma process

    Samples the points of the GGP with Levy measure
      alpha/Gamma(1-sigma) * w^(-1-sigma) * exp(-tau*w)

    For sigma>=0, it samples points above the threshold T>0 using the adaptive
    thinning strategy described in Favaro and Teh (2013).

    Convert the same function used in BNPGraph matlab package by Francois Caron
    http://www.stats.ox.ac.uk/~caron/code/bnpgraph/index.html

    Reference:
    S. Favaro and Y.W. Teh. MCMC for normalized random measure mixture
        models. Statistical Science, vol.28(3), pp.335-359, 2013.
    :param alpha: positive scalar
    :param sigma: real in (-Inf, 1)
    :param tau: positive scalar
    :param T: truncation threshold; positive scalar
    :return:
        N: weights from the GGP
        T: threshold
    """

    # finite activity GGP, don't need to truncate
    if sigma < 0:
        rate = np.exp(np.log(alpha) - np.log(-sigma) + sigma + np.log(tau))
        K = np.random.poisson(rate)
        N = np.random.gamma(-sigma, scale=1. / tau, size=K)
        T = 0
        return N, T

    # infinite activity GGP
    if T == 0:
        # set the threshold automatically
        # Number of jumps of order alpha/sigma/Gamma(1-sigma) * T^{-sigma} for sigma > 0
        # and alpha*log(T) for sigma = 0
        if sigma > .1:
            Njumps = 20000
            T = np.exp(1. / sigma * (np.log(alpha) - np.log(sigma) - gammaln(1. - sigma) - np.log(Njumps)))
        else:
            T = 1e-10
            if sigma > 0:
                Njumps = np.floor(alpha / sigma / gamma(1. - sigma) * T ** (-sigma))
            else:
                Njumps = np.floor(-alpha * np.log(T))
    else:
        if T <= 0:
            raise ValueError("Threshold T must be strictly positive")
        if sigma > 1e-3:
            Njumps = np.floor(alpha / sigma / gamma(1. - sigma) * T ** (-sigma))
        else:
            Njumps = np.floor(-alpha * np.log(T))
        if Njumps > 1e7:
            raise Warning("Expected number of jumps = %d" % Njumps)

    # Adaptive thinning strategy
    N = np.zeros(np.ceil(Njumps + 3 * np.sqrt(Njumps)))
    k = 0
    t = T
    count = 0
    while True:
        e = -np.log(np.random.random())  # Sample exponential random variable of unit rate

        if e > W(t, np.inf, alpha, sigma, tau):
            N = N[0:k]
            return N, T
        else:
            t_new = inv_W(t, e, alpha, sigma, tau)

        if tau == 0 or np.log(np.random.random()) < (-1. - sigma) * np.log(t_new / t):
            # if tau>0, adaptive thinning - otherwise accept always
            if k > len(N):
                N = np.append(N, np.zeros(Njumps))
            N[k] = t_new
            k += 1

        t = t_new
        count += 1
        if count > 10e8:
            # If too many computation, we lower the threshold T and rerun
            T /= 10.
            N, T = GGPrnd(alpha, sigma, tau, T)

    return N, T
<|endoftext|>


<commit_before>from django.conf import settings
from django.test import TestCase
from django.core.urlresolvers import reverse

import json

data = """
{
  "before": "5aef35982fb2d34e9d9d4502f6ede1072793222d",
  "repository": {
  "url": "http://github.com/beetletweezers/tweezers",
    "name": "github",
    "description": "You're lookin' at it.",
    "watchers": 5,
    "forks": 2,
    "private": 1,
    "owner": {
      "email": "chris@ozmm.org",
      "name": "defunkt"
    }
  },
  "commits": [
    {
      "id": "41a212ee83ca127e3c8cf465891ab7216a705f59",
      "url": "http://github.com/defunkt/github/commit/41a212ee83ca127e3c8cf465891ab7216a705f59",
      "author": {
        "email": "chris@ozmm.org",
        "name": "Chris Wanstrath"
      },
      "message": "okay i give in",
      "timestamp": "2008-02-15T14:57:17-08:00",
      "added": ["filepath.rb"]
    },
    {
      "id": "de8251ff97ee194a289832576287d6f8ad74e3d0",
      "url": "http://github.com/defunkt/github/commit/de8251ff97ee194a289832576287d6f8ad74e3d0",
      "author": {
        "email": "chris@ozmm.org",
        "name": "Chris Wanstrath"
      },
      "message": "update pricing a tad",
      "timestamp": "2008-02-15T14:36:34-08:00"
    }
  ],
  "after": "de8251ff97ee194a289832576287d6f8ad74e3d0",
  "ref": "refs/heads/master"
}
"""

class Basic(TestCase):
    fixtures=['eric', 'test_data']

    def setUp(self):
        settings.CELERY_ALWAYS_EAGER = True

    def tearDown(self):
        settings.CELERY_ALWAYS_EAGER = False

    def test_github(self):
        resp = self.client.post('/github', {'payload': data})

<commit_msg>Spurce up tests a bit<commit_after>from django.conf import settings
from django.test import TestCase
from django.core.urlresolvers import reverse

import json

from projects.models import Conf

data = """
{
  "before": "5aef35982fb2d34e9d9d4502f6ede1072793222d",
  "repository": {
  "url": "http://github.com/beetletweezers/tweezers",
    "name": "github",
    "description": "You're lookin' at it.",
    "watchers": 5,
    "forks": 2,
    "private": 1,
    "owner": {
      "email": "chris@ozmm.org",
      "name": "defunkt"
    }
  },
  "commits": [
    {
      "id": "41a212ee83ca127e3c8cf465891ab7216a705f59",
      "url": "http://github.com/defunkt/github/commit/41a212ee83ca127e3c8cf465891ab7216a705f59",
      "author": {
        "email": "chris@ozmm.org",
        "name": "Chris Wanstrath"
      },
      "message": "okay i give in",
      "timestamp": "2008-02-15T14:57:17-08:00",
      "added": ["filepath.rb"]
    },
    {
      "id": "de8251ff97ee194a289832576287d6f8ad74e3d0",
      "url": "http://github.com/defunkt/github/commit/de8251ff97ee194a289832576287d6f8ad74e3d0",
      "author": {
        "email": "chris@ozmm.org",
        "name": "Chris Wanstrath"
      },
      "message": "update pricing a tad",
      "timestamp": "2008-02-15T14:36:34-08:00"
    }
  ],
  "after": "de8251ff97ee194a289832576287d6f8ad74e3d0",
  "ref": "refs/heads/master"
}
"""

class Basic(TestCase):
    fixtures=['eric', 'test_data']

    def setUp(self):
        settings.CELERY_ALWAYS_EAGER = True

    def tearDown(self):
        settings.CELERY_ALWAYS_EAGER = False

    def test_github(self):
        resp = self.client.post('/github', {'payload': data})
        self.assertEqual(Conf.objects.count(), 1)
        conf = Conf.objects.all()[0]
        self.assertEqual(conf.theme, 'default')
        self.assertTrue(conf.path is not None)

<|endoftext|>


<commit_before># Copyright 2014 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Once nested repositories work, this file should cease to exist.

load("@io_bazel_rules_go//go/private:common.bzl", "MINIMUM_BAZEL_VERSION")
load("@io_bazel_rules_go//go/private:compat/compat_repo.bzl", "go_rules_compat")
load("@io_bazel_rules_go//go/private:skylib/lib/versions.bzl", "versions")
load("@io_bazel_rules_go//go/private:nogo.bzl", "DEFAULT_NOGO", "go_register_nogo")
load("@io_bazel_rules_go//go/platform:list.bzl", "GOOS_GOARCH")
load("@io_bazel_rules_go//proto:gogo.bzl", "gogo_special_proto")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

def go_rules_dependencies():
    """See /go/workspace.rst#go-rules-dependencies for full documentation."""
    if getattr(native, "bazel_version", None):
        versions.check(MINIMUM_BAZEL_VERSION, bazel_version = native.bazel_version)

    # Compatibility layer, needed to support older versions of Bazel.
    _maybe(
        go_rules_compat,
        name = "io_bazel_rules_go_compat",
    )

    # Needed for nogo vet checks and go/packages.
    _maybe(
        http_archive,
        name = "org_golang_x_tools",
        # master, as of 2019-01-15
        urls = ["https://codeload.github.com/golang/tools/zip/bf090417da8b6150dcfe96795325f5aa78fff718"],
        strip_prefix = "tools-bf090417da8b6150dcfe96795325f5aa78fff718",
        type = "zip",
        patches = [
            "@io_bazel_rules_go//third_party:org_golang_x_tools-gazelle.patch",
            "@io_bazel_rules_go//third_party:org_golang_x_tools-extras.patch",
        ],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix golang.org/x/tools
    )

    # Proto dependencies
    _maybe(
        git_repository,
        name = "com_github_golang_protobuf",
        remote = "https://github.com/golang/protobuf",
        commit = "aa810b61a9c79d51363740d207bb46cf8e620ed5",  # v1.2.0, as of 2018-09-28
        patches = [
            "@io_bazel_rules_go//third_party:com_github_golang_protobuf-gazelle.patch",
            "@io_bazel_rules_go//third_party:com_github_golang_protobuf-extras.patch",
        ],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix github.com/golang/protobuf -proto disable_global
    )

    # bazel_skylib is a dependency of com_google_protobuf.
    # Nothing in rules_go may depend on bazel_skylib, since it won't be declared
    # when go/def.bzl is loaded. The vendored copy of skylib in go/private/skylib
    # may be used instead.
    _maybe(
        http_archive,
        name = "bazel_skylib",
        sha256 = "54ee22e5b9f0dd2b42eb8a6c1878dee592cfe8eb33223a7dbbc583a383f6ee1a",
        strip_prefix = "bazel-skylib-0.6.0",
        urls = ["https://github.com/bazelbuild/bazel-skylib/archive/0.6.0.zip"],
        type = "zip",
    )
    _maybe(
        http_archive,
        name = "com_google_protobuf",
        strip_prefix = "protobuf-3.6.1.3",
        sha256 = "9510dd2afc29e7245e9e884336f848c8a6600a14ae726adb6befdb4f786f0be2",
        # v3.6.1.3 as of 2019-01-15
        urls = ["https://github.com/protocolbuffers/protobuf/archive/v3.6.1.3.zip"],
        type = "zip",
    )
    _maybe(
        git_repository,
        name = "com_github_mwitkow_go_proto_validators",
        remote = "https://github.com/mwitkow/go-proto-validators",
        commit = "0950a79900071e9f3f5979b78078c599376422fd",  # master, as of 2019-01-15
        patches = ["@io_bazel_rules_go//third_party:com_github_mwitkow_go_proto_validators-gazelle.patch"],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix github.com/mwitkow/go-proto-validators -proto disable
    )
    _maybe(
        git_repository,
        name = "com_github_gogo_protobuf",
        remote = "https://github.com/gogo/protobuf",
        commit = "4cbf7e384e768b4e01799441fdf2a706a5635ae7",  # v1.2.0, as of 2019-01-15
        patches = ["@io_bazel_rules_go//third_party:com_github_gogo_protobuf-gazelle.patch"],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix github.com/gogo/protobuf -proto legacy
    )
    _maybe(
        gogo_special_proto,
        name = "gogo_special_proto",
    )

    # GRPC dependencies
    _maybe(
        git_repository,
        name = "org_golang_x_net",
        remote = "https://github.com/golang/net",
        commit = "915654e7eabcea33ae277abbecf52f0d8b7a9fdc",  # master as of 2019-01-15
        patches = ["@io_bazel_rules_go//third_party:org_golang_x_net-gazelle.patch"],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix golang.org/x/net
    )
    _maybe(
        git_repository,
        name = "org_golang_x_text",
        remote = "https://github.com/golang/text",
        commit = "f21a4dfb5e38f5895301dc265a8def02365cc3d0",  # v0.3.0, latest as of 2019-01-15
        patches = ["@io_bazel_rules_go//third_party:org_golang_x_text-gazelle.patch"],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix golang.org/x/text
    )
    _maybe(
        git_repository,
        name = "org_golang_x_sys",
        remote = "https://github.com/golang/sys",
        commit = "2be51725563103c17124a318f1745b66f2347acb",  # master as of 2019-01-15
        patches = ["@io_bazel_rules_go//third_party:org_golang_x_sys-gazelle.patch"],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix golang.org/x/sys
    )
    _maybe(
        git_repository,
        name = "org_golang_google_grpc",
        remote = "https://github.com/grpc/grpc-go",
        commit = "df014850f6dee74ba2fc94874043a9f3f75fbfd8",  # v1.17.0, latest as of 2019-01-15
        patches = [
            "@io_bazel_rules_go//third_party:org_golang_google_grpc-gazelle.patch",
            "@io_bazel_rules_go//third_party:org_golang_google_grpc-crosscompile.patch",
        ],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix google.golang.org/grpc -proto disable
    )
    _maybe(
        git_repository,
        name = "org_golang_google_genproto",
        remote = "https://github.com/google/go-genproto",
        commit = "db91494dd46c1fdcbbde05e5ff5eb56df8f7d79a",  # master as of 2019-01-15
        patches = ["@io_bazel_rules_go//third_party:org_golang_google_genproto-gazelle.patch"],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix google.golang.org/genproto -proto disable_global
    )
    _maybe(
        http_archive,
        name = "go_googleapis",
        # master as of 2019-01-17
        urls = ["https://codeload.github.com/googleapis/googleapis/zip/0ac60e21a1aa86c07c1836865b35308ba8178b05"],
        strip_prefix = "googleapis-0ac60e21a1aa86c07c1836865b35308ba8178b05",
        type = "zip",
        patches = [
            "@io_bazel_rules_go//third_party:go_googleapis-directives.patch",
            "@io_bazel_rules_go//third_party:go_googleapis-gazelle.patch",
            "@io_bazel_rules_go//third_party:go_googleapis-fix.patch",
        ],
        patch_args = ["-p1"],
    )

    # Needed for examples
    _maybe(
        git_repository,
        name = "com_github_golang_glog",
        remote = "https://github.com/golang/glog",
        commit = "23def4e6c14b4da8ac2ed8007337bc5eb5007998",  # master as of 2019-01-15
        patches = ["@io_bazel_rules_go//third_party:com_github_golang_glog-gazelle.patch"],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix github.com/golang/glog
    )
    _maybe(
        git_repository,
        name = "com_github_kevinburke_go_bindata",
        remote = "https://github.com/kevinburke/go-bindata",
        commit = "06af60a4461b70d84a2b173d92f9f425d78baf55",  # v3.11.0, latest as of 2019-01-15
        patches = ["@io_bazel_rules_go//third_party:com_github_kevinburke_go_bindata-gazelle.patch"],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix github.com/kevinburke/go-bindata
    )

    # This may be overridden by go_register_toolchains, but it's not mandatory
    # for users to call that function (they may declare their own @go_sdk and
    # register their own toolchains).
    _maybe(
        go_register_nogo,
        name = "io_bazel_rules_nogo",
        nogo = DEFAULT_NOGO,
    )

def _maybe(repo_rule, name, **kwargs):
    if name not in native.existing_rules():
        repo_rule(name = name, **kwargs)
<commit_msg>Make googleapis work on FreeBSD. (#1954)<commit_after># Copyright 2014 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Once nested repositories work, this file should cease to exist.

load("@io_bazel_rules_go//go/private:common.bzl", "MINIMUM_BAZEL_VERSION")
load("@io_bazel_rules_go//go/private:compat/compat_repo.bzl", "go_rules_compat")
load("@io_bazel_rules_go//go/private:skylib/lib/versions.bzl", "versions")
load("@io_bazel_rules_go//go/private:nogo.bzl", "DEFAULT_NOGO", "go_register_nogo")
load("@io_bazel_rules_go//go/platform:list.bzl", "GOOS_GOARCH")
load("@io_bazel_rules_go//proto:gogo.bzl", "gogo_special_proto")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

def go_rules_dependencies():
    """See /go/workspace.rst#go-rules-dependencies for full documentation."""
    if getattr(native, "bazel_version", None):
        versions.check(MINIMUM_BAZEL_VERSION, bazel_version = native.bazel_version)

    # Compatibility layer, needed to support older versions of Bazel.
    _maybe(
        go_rules_compat,
        name = "io_bazel_rules_go_compat",
    )

    # Needed for nogo vet checks and go/packages.
    _maybe(
        http_archive,
        name = "org_golang_x_tools",
        # master, as of 2019-01-15
        urls = ["https://codeload.github.com/golang/tools/zip/bf090417da8b6150dcfe96795325f5aa78fff718"],
        strip_prefix = "tools-bf090417da8b6150dcfe96795325f5aa78fff718",
        type = "zip",
        patches = [
            "@io_bazel_rules_go//third_party:org_golang_x_tools-gazelle.patch",
            "@io_bazel_rules_go//third_party:org_golang_x_tools-extras.patch",
        ],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix golang.org/x/tools
    )

    # Proto dependencies
    _maybe(
        git_repository,
        name = "com_github_golang_protobuf",
        remote = "https://github.com/golang/protobuf",
        commit = "aa810b61a9c79d51363740d207bb46cf8e620ed5",  # v1.2.0, as of 2018-09-28
        patches = [
            "@io_bazel_rules_go//third_party:com_github_golang_protobuf-gazelle.patch",
            "@io_bazel_rules_go//third_party:com_github_golang_protobuf-extras.patch",
        ],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix github.com/golang/protobuf -proto disable_global
    )

    # bazel_skylib is a dependency of com_google_protobuf.
    # Nothing in rules_go may depend on bazel_skylib, since it won't be declared
    # when go/def.bzl is loaded. The vendored copy of skylib in go/private/skylib
    # may be used instead.
    _maybe(
        http_archive,
        name = "bazel_skylib",
        sha256 = "54ee22e5b9f0dd2b42eb8a6c1878dee592cfe8eb33223a7dbbc583a383f6ee1a",
        strip_prefix = "bazel-skylib-0.6.0",
        urls = ["https://github.com/bazelbuild/bazel-skylib/archive/0.6.0.zip"],
        type = "zip",
    )
    _maybe(
        http_archive,
        name = "com_google_protobuf",
        strip_prefix = "protobuf-3.6.1.3",
        sha256 = "9510dd2afc29e7245e9e884336f848c8a6600a14ae726adb6befdb4f786f0be2",
        # v3.6.1.3 as of 2019-01-15
        urls = ["https://github.com/protocolbuffers/protobuf/archive/v3.6.1.3.zip"],
        type = "zip",
    )
    _maybe(
        git_repository,
        name = "com_github_mwitkow_go_proto_validators",
        remote = "https://github.com/mwitkow/go-proto-validators",
        commit = "0950a79900071e9f3f5979b78078c599376422fd",  # master, as of 2019-01-15
        patches = ["@io_bazel_rules_go//third_party:com_github_mwitkow_go_proto_validators-gazelle.patch"],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix github.com/mwitkow/go-proto-validators -proto disable
    )
    _maybe(
        git_repository,
        name = "com_github_gogo_protobuf",
        remote = "https://github.com/gogo/protobuf",
        commit = "4cbf7e384e768b4e01799441fdf2a706a5635ae7",  # v1.2.0, as of 2019-01-15
        patches = ["@io_bazel_rules_go//third_party:com_github_gogo_protobuf-gazelle.patch"],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix github.com/gogo/protobuf -proto legacy
    )
    _maybe(
        gogo_special_proto,
        name = "gogo_special_proto",
    )

    # GRPC dependencies
    _maybe(
        git_repository,
        name = "org_golang_x_net",
        remote = "https://github.com/golang/net",
        commit = "915654e7eabcea33ae277abbecf52f0d8b7a9fdc",  # master as of 2019-01-15
        patches = ["@io_bazel_rules_go//third_party:org_golang_x_net-gazelle.patch"],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix golang.org/x/net
    )
    _maybe(
        git_repository,
        name = "org_golang_x_text",
        remote = "https://github.com/golang/text",
        commit = "f21a4dfb5e38f5895301dc265a8def02365cc3d0",  # v0.3.0, latest as of 2019-01-15
        patches = ["@io_bazel_rules_go//third_party:org_golang_x_text-gazelle.patch"],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix golang.org/x/text
    )
    _maybe(
        git_repository,
        name = "org_golang_x_sys",
        remote = "https://github.com/golang/sys",
        commit = "2be51725563103c17124a318f1745b66f2347acb",  # master as of 2019-01-15
        patches = ["@io_bazel_rules_go//third_party:org_golang_x_sys-gazelle.patch"],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix golang.org/x/sys
    )
    _maybe(
        git_repository,
        name = "org_golang_google_grpc",
        remote = "https://github.com/grpc/grpc-go",
        commit = "df014850f6dee74ba2fc94874043a9f3f75fbfd8",  # v1.17.0, latest as of 2019-01-15
        patches = [
            "@io_bazel_rules_go//third_party:org_golang_google_grpc-gazelle.patch",
            "@io_bazel_rules_go//third_party:org_golang_google_grpc-crosscompile.patch",
        ],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix google.golang.org/grpc -proto disable
    )
    _maybe(
        git_repository,
        name = "org_golang_google_genproto",
        remote = "https://github.com/google/go-genproto",
        commit = "db91494dd46c1fdcbbde05e5ff5eb56df8f7d79a",  # master as of 2019-01-15
        patches = ["@io_bazel_rules_go//third_party:org_golang_google_genproto-gazelle.patch"],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix google.golang.org/genproto -proto disable_global
    )
    _maybe(
        http_archive,
        name = "go_googleapis",
        # master as of 2019-01-17
        urls = ["https://codeload.github.com/googleapis/googleapis/zip/0ac60e21a1aa86c07c1836865b35308ba8178b05"],
        strip_prefix = "googleapis-0ac60e21a1aa86c07c1836865b35308ba8178b05",
        type = "zip",
        patches = [
            "@io_bazel_rules_go//third_party:go_googleapis-directives.patch",
            "@io_bazel_rules_go//third_party:go_googleapis-gazelle.patch",
            "@io_bazel_rules_go//third_party:go_googleapis-fix.patch",
        ],
        patch_args = ["-E", "-p1"],
    )

    # Needed for examples
    _maybe(
        git_repository,
        name = "com_github_golang_glog",
        remote = "https://github.com/golang/glog",
        commit = "23def4e6c14b4da8ac2ed8007337bc5eb5007998",  # master as of 2019-01-15
        patches = ["@io_bazel_rules_go//third_party:com_github_golang_glog-gazelle.patch"],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix github.com/golang/glog
    )
    _maybe(
        git_repository,
        name = "com_github_kevinburke_go_bindata",
        remote = "https://github.com/kevinburke/go-bindata",
        commit = "06af60a4461b70d84a2b173d92f9f425d78baf55",  # v3.11.0, latest as of 2019-01-15
        patches = ["@io_bazel_rules_go//third_party:com_github_kevinburke_go_bindata-gazelle.patch"],
        patch_args = ["-p1"],
        # gazelle args: -go_prefix github.com/kevinburke/go-bindata
    )

    # This may be overridden by go_register_toolchains, but it's not mandatory
    # for users to call that function (they may declare their own @go_sdk and
    # register their own toolchains).
    _maybe(
        go_register_nogo,
        name = "io_bazel_rules_nogo",
        nogo = DEFAULT_NOGO,
    )

def _maybe(repo_rule, name, **kwargs):
    if name not in native.existing_rules():
        repo_rule(name = name, **kwargs)
<|endoftext|>


<commit_before>import sys
import yaml

import rethinkdb as r

if len(sys.argv) != 2:
    print("Hey, thats not how you launch this...")
    print("%s <config file>") % sys.argv[0]
    sys.exit(1)

# Open Config File and Parse Config Data
configfile = sys.argv[1]
cfh = open(configfile, "r")
config = yaml.safe_load(cfh)
cfh.close()

# Establish Connection
host = config['rethink_host']
database = config['rethink_db']
auth_key = config['rethink_authkey']
conn = r.connect(host, 28015, auth_key=auth_key).repl()

# Drop Database
r.db_drop(database).run(conn)

print "Done!"
<commit_msg>- fixed `mgmtscripts/drop_db.py`<commit_after>import sys
import yaml

import rethinkdb as r

from runbookdb import RunbookDB

if len(sys.argv) != 2:
    print("Hey, thats not how you launch this...")
    print("%s <config file>") % sys.argv[0]
    sys.exit(1)

# Open Config File and Parse Config Data
configfile = sys.argv[1]
with open(configfile, "r") as h:
    config = yaml.safe_load(h)

# Establish Connection
host = config['rethink_host']
database = config['rethink_db']

db=RunbookDB(configfile)
conn=db.connect()

# Drop Database
r.db_drop(database).run(conn)

print "Done!"
<|endoftext|>