text:
class PlayroomController < ApplicationController
  before_filter :authenticate_user!

  before_action :set_room
  before_action :set_character, only: [:playspace]

  # load_and_authorize_resource through: :community, class: "Room"

  def playspace
    @character = @room.character(current_user)
  end

  def get_main_chat_log
    @chat_log = Message.where(room_id: @room.id).asc(:created_at)
    respond_to do |format|
      format.json { render json: @chat_log.to_json }
    end
  end

  def get_characters
    @characters = @room.players.pluck(:character_id)
    respond_to do |format|
      format.json { render json: @characters.to_json }
    end
  end

  def get_illustrations
    @illustrations = {}
    @room.players.order(created_at: :desc).each do |player|
      @illustrations[player.character_id] = player.character.illustrations.order(name: :desc) if player.character.present?
    end

    respond_to do |format|
      format.json { render }
    end
  end

  def get_member_statuses
    html = render_to_string(partial: "member_status")
    render html: html
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_room
    @room =Room.find(params[:id])
    @community = Community.find(params[:community_id])
  end

  def set_character
    @character = @room.players.find_by(user: current_user).character
    return if @character.nil?
  end

end


text:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\grid\GridView;
use yii\helpers\Url;
?>

	<?php $form = ActiveForm::begin([
        'id' => 'locations-street-form',
        'errorSummaryCssClass' => 'error-summary alert alert-danger',
        'options' => ['class' => 'form-horizontal form-groups-bordered', 'enctype' => 'multipart/form-data'],
        'fieldConfig' => [
        'template' => "{label}\n
{input}
\n
{error}\n{hint}
",
        'labelOptions' => ['class' => 'col-sm-3 control-label'],
        ],
    ]); ?>

    <?php echo $form->errorSummary($model); ?>

    <?= $form->field($model, 'street', [
        'inputOptions'=>['class'=>'form-control input-sm']
    ])->textInput() ?>

            <?= \yii\helpers\Html::submitButton($model->isNewRecord ? 'Добавить' : 'Изменить', ['class' => 'btn btn-success']) ?>

    <?php ActiveForm::end(); ?>

redpajama_set_name: RedPajamaGithub


text:
package demo;

import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class GreeterTest {

    @Test
    public void itShouldSayHelloWorld() throws Exception {

        Greeter greeter = new Greeter();

        String greeting = greeter.greet();

        assertThat(greeting, is("Hello World!"));

    }
}


text:
from numpy.random import randn
from numpy import *

# generate some overlapping training vectors
num_vectors=5
vec_distance=1
traindat=concatenate((randn(2,num_vectors)-vec_distance,
	randn(2,num_vectors)+vec_distance), axis=1)
label_traindat=concatenate((-ones(num_vectors), ones(num_vectors)));

parameter_list = [[traindat,label_traindat]]

def evaluation_cross_validation_mkl_weight_storage(traindat=traindat, label_traindat=label_traindat):
    from shogun import CrossValidation, CrossValidationResult
    from shogun import ParameterObserverCV
    from shogun import ContingencyTableEvaluation, ACCURACY
    from shogun import StratifiedCrossValidationSplitting
    from shogun import BinaryLabels
    from shogun import RealFeatures, CombinedFeatures
    from shogun import GaussianKernel, CombinedKernel
    from shogun import LibSVM, MKLClassification

    # training data, combined features all on same data
    features=RealFeatures(traindat)
    comb_features=CombinedFeatures()
    comb_features.append_feature_obj(features)
    comb_features.append_feature_obj(features)
    comb_features.append_feature_obj(features)
    labels=BinaryLabels(label_traindat)

    # kernel, different Gaussians combined
    kernel=CombinedKernel()
    kernel.append_kernel(GaussianKernel(10, 0.1))
    kernel.append_kernel(GaussianKernel(10, 1))
    kernel.append_kernel(GaussianKernel(10, 2))

    # create mkl using libsvm, due to a mem-bug, interleaved is not possible
    svm=MKLClassification(LibSVM());
    svm.set_interleaved_optimization_enabled(False);
    svm.set_kernel(kernel);

    # splitting strategy for 5 fold cross-validation (for classification its better
    # to use "StratifiedCrossValidation", but the standard
    # "StratifiedCrossValidationSplitting" is also available
    splitting_strategy=StratifiedCrossValidationSplitting(labels, 5)

    # evaluation method
    evaluation_criterium=ContingencyTableEvaluation(ACCURACY)

    # cross-validation instance
    cross_validation=CrossValidation(svm, comb_features, labels,
        splitting_strategy, evaluation_criterium)
    cross_validation.set_autolock(False)

    # append cross vlaidation output classes
    mkl_storage=ParameterObserverCV()
    cross_validation.subscribe_to_parameters(mkl_storage)
    cross_validation.set_num_runs(3)

    # perform cross-validation
    result=cross_validation.evaluate()

    # print mkl weights
    weights = []
    for obs_index in range(mkl_storage.get_num_observations()):
        obs = mkl_storage.get_observation(obs_index)
        for fold_index in range(obs.get_num_folds()):
            fold = obs.get_fold(fold_index)
            machine = MKLClassification.obtain_from_generic(fold.get_trained_machine())
            w = machine.get_kernel().get_subkernel_weights()
            weights.append(w)

    print("mkl weights during cross--validation")
    print(weights)

if __name__=='__main__':
	print('Evaluation CrossValidationClassification')
	evaluation_cross_validation_mkl_weight_storage(*parameter_list[0])


text:
package v1

import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

// ScheduleSpec defines the specification for an Ark schedule
type ScheduleSpec struct {
	// Template is the definition of the Backup to be run
	// on the provided schedule
	Template BackupSpec `json:"template"`

	// Schedule is a Cron expression defining when to run
	// the Backup.
	Schedule string `json:"schedule"`
}

// SchedulePhase is a string representation of the lifecycle phase
// of an Ark schedule
type SchedulePhase string

const (
	// SchedulePhaseNew means the schedule has been created but not
	// yet processed by the ScheduleController
	SchedulePhaseNew SchedulePhase = "New"

	// SchedulePhaseEnabled means the schedule has been validated and
	// will now be triggering backups according to the schedule spec.
	SchedulePhaseEnabled SchedulePhase = "Enabled"

	// SchedulePhaseFailedValidation means the schedule has failed
	// the controller's validations and therefore will not trigger backups.
	SchedulePhaseFailedValidation SchedulePhase = "FailedValidation"
)

// ScheduleStatus captures the current state of an Ark schedule
type ScheduleStatus struct {
	// Phase is the current phase of the Schedule
	Phase SchedulePhase `json:"phase"`

	// LastBackup is the last time a Backup was run for this
	// Schedule schedule
	LastBackup metav1.Time `json:"lastBackup"`

	// ValidationErrors is a slice of all validation errors (if
	// applicable)
	ValidationErrors []string `json:"validationErrors"`
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// Schedule is an Ark resource that represents a pre-scheduled or
// periodic Backup that should be run.
type Schedule struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata"`

	Spec   ScheduleSpec   `json:"spec"`
	Status ScheduleStatus `json:"status,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// ScheduleList is a list of Schedules.
type ScheduleList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata"`
	Items           []Schedule `json:"items"`
}


text:
'use strict';

const THREE = require('three');
const OrbitControls = require('three-orbit-controls')(THREE)
const DAT = require('dat-gui');

const Stats = require('stats-js');

export default (callback, update) => {
    var gui = new DAT.GUI()

	// Setup the viewport.
    var viewport = {
        gui: gui,
    };

    var container = document.createElement("div");
    viewport.container = document.querySelector("#viewport").appendChild(container);

    // Stats.
    viewport.stats1 = new Stats();
    viewport.stats1.setMode(0); // Panel 0 = fps
    viewport.stats1.domElement.style.cssText = "position:absolute;top:0px;left:0px;";
    viewport.container.appendChild(viewport.stats1.domElement);
    viewport.stats2 = new Stats();
    viewport.stats2.setMode(1); // Panel 0 = fps
    viewport.stats2.domElement.style.cssText = "position:absolute;top:0px;left:80px;";
    viewport.container.appendChild(viewport.stats2.domElement);
    viewport.stats3 = new Stats();
    viewport.stats3.setMode(2); // Panel 0 = fps
    viewport.stats3.domElement.style.cssText = "position:absolute;top:0px;left:160px;";
    viewport.container.appendChild(viewport.stats3.domElement);

    viewport.scene = new THREE.Scene();
    viewport.scene.background = new THREE.Color(0x101010);

    viewport.camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 2000);
    viewport.camera.position.set(0.0, 35, 35 * 3.5);

    viewport.renderer = new THREE.WebGLRenderer({ antialias: false });
    viewport.renderer.setPixelRatio(window.devicePixelRatio);
    viewport.renderer.setSize(window.innerWidth, window.innerHeight);
    viewport.renderer.setFaceCulling(THREE.CullFaceNone);
    viewport.renderer.gammaInput = true;
    viewport.renderer.gammaOutput = false;
    viewport.renderer.shadowMap.enabled = true;

    var controls = new OrbitControls(viewport.camera, viewport.renderer.domElement);
    controls.enableDamping = true;
    controls.enableZoom = true;
    controls.target.set(0, 0, 0);
    controls.rotateSpeed = 0.3;
    controls.zoomSpeed = 1.0;
    controls.panSpeed = 2.0;

    viewport.container.appendChild(viewport.renderer.domElement);

    // resize the canvas when the window changes
    window.addEventListener('resize', function() {
        viewport.camera.aspect = window.innerWidth / window.innerHeight;
        viewport.camera.updateProjectionMatrix();
        viewport.renderer.setSize(window.innerWidth, window.innerHeight);
    }, false);

    // begin the animation loop
    (function tick() {
        viewport.stats1.begin();
        viewport.stats2.begin();
        viewport.stats3.begin();

        update(viewport.renderer);
        viewport.renderer.render(viewport.scene, viewport.camera);

        viewport.stats1.end();
        viewport.stats2.end();
        viewport.stats3.end();
        requestAnimationFrame(tick);
    })();

    // Pass all of the necessary renderer/scene/gui references to the callback for this.
	return callback( viewport );
}


text:
/****************************************************************************************/
/*                                                                                */
/*                                       Pegasus                                        */
/*                                                                                */
/****************************************************************************************/

//! \file	TimelineManagerProxy.cpp
//! \author	Karolyn Boulanger
//! \date	07th November 2013
//! \brief	Proxy object, used by the editor to interact with the timeline

//! \todo Why do we need this in Rel-Debug? TimelineManagerProxy should not even be compiled in REL mode
#if PEGASUS_ENABLE_PROXIES

#include "Pegasus/Timeline/Proxy/TimelineManagerProxy.h"
#include "Pegasus/Timeline/Proxy/TimelineProxy.h"
#include "Pegasus/Timeline/Proxy/LaneProxy.h"
#include "Pegasus/Core/Shared/CompilerEvents.h"
#include "Pegasus/Timeline/TimelineSource.h"
#include "Pegasus/Timeline/Proxy/TimelineScriptProxy.h"
#include "Pegasus/Timeline/TimelineManager.h"
#include "Pegasus/Timeline/Timeline.h"
#include "Pegasus/Timeline/Lane.h"
#include "Pegasus/AssetLib/Proxy/AssetProxy.h"
#include "Pegasus/AssetLib/Asset.h"
#include "Pegasus/Core/Shared/ISourceCodeProxy.h"
#include "Pegasus/Memory/MemoryManager.h"
#include "Pegasus/Utils/String.h"

using namespace Pegasus;

namespace Pegasus {
namespace Timeline {

TimelineManagerProxy::TimelineManagerProxy(TimelineManager * timelineManager)
    :   mTimelineManager(timelineManager)
{
    PG_ASSERTSTR(timelineManager != nullptr, "Trying to create a timeline proxy from an invalid timeline object");
}

//----------------------------------------------------------------------------------------

TimelineManagerProxy::~TimelineManagerProxy()
{
}

//----------------------------------------------------------------------------------------

unsigned int TimelineManagerProxy::GetRegisteredBlockNames(char classNames   [MAX_NUM_REGISTERED_BLOCKS][MAX_BLOCK_CLASS_NAME_LENGTH    + 1],
                                                    char editorStrings[MAX_NUM_REGISTERED_BLOCKS][MAX_BLOCK_EDITOR_STRING_LENGTH + 1]) const
{
    return mTimelineManager->GetRegisteredBlockNames(classNames, editorStrings);
}

ITimelineProxy* TimelineManagerProxy::GetCurrentTimeline() const
{
    Timeline* t = mTimelineManager->GetCurrentTimeline();
    if (t == nullptr)
    {
        return nullptr;
    }
    else
    {
        return t->GetProxy();
    }
}

void TimelineManagerProxy::Update()
{
    mTimelineManager->Update();
}

void TimelineManagerProxy::RegisterEventListener(Pegasus::Core::CompilerEvents::ICompilerEventListener * eventListener)
{
    mTimelineManager->RegisterEventListener(eventListener);
}

unsigned int TimelineManagerProxy::GetNextBlockGuid() const
{
    return mTimelineManager->GetNextBlockGuid();
}

}   // namespace Timeline
}   // namespace Pegasus

#else

PEGASUS_AVOID_EMPTY_FILE_WARNING

#endif  // PEGASUS_ENABLE_PROXIES


text:
/**
 * @author Paweł Wszoła (wszola.p@gmail.com)
 *
 */

angular.module('angularLazyImg', []);

angular.module('angularLazyImg').factory('LazyImgMagic', [
  '$window', '$rootScope', 'lazyImgConfig', 'lazyImgHelpers',
  function($window, $rootScope, lazyImgConfig, lazyImgHelpers){
    'use strict';

    var winDimensions, $win, images, isListening, options;
    var checkImagesT, saveWinOffsetT, containers;

    images = [];
    isListening = false;
    options = lazyImgConfig.getOptions();
    $win = angular.element($window);
    winDimensions = lazyImgHelpers.getWinDimensions();
    saveWinOffsetT = lazyImgHelpers.throttle(function(){
      winDimensions = lazyImgHelpers.getWinDimensions();
    }, 60);
    containers = [options.container || $win];

    function checkImages(){
      for(var i = images.length - 1; i >= 0; i--){
        var image = images[i];
        if(image && lazyImgHelpers.isElementInView(image.$elem[0], options.offset, winDimensions)){
          loadImage(image);
          images.splice(i, 1);
        }
      }
      if(images.length === 0){ stopListening(); }
    }

    checkImagesT = lazyImgHelpers.throttle(checkImages, 30);

    function listen(param){
      containers.forEach(function (container) {
        container[param]('scroll', checkImagesT);
        container[param]('touchmove', checkImagesT);
      });
      $win[param]('resize', checkImagesT);
      $win[param]('resize', saveWinOffsetT);
    }

    function startListening(){
      isListening = true;
      setTimeout(function(){
        checkImages();
        listen('on');
      }, 1);
    }

    function stopListening(){
      isListening = false;
      listen('off');
    }

    function removeImage(image){
      var index = images.indexOf(image);
      if(index !== -1) {
        images.splice(index, 1);
      }
    }

    function loadImage(photo){
      var img = new Image();
      img.onerror = function(){
        if(options.errorClass){
          photo.$elem.addClass(options.errorClass);
        }
        $rootScope.$emit('lazyImg:error', photo);
        options.onError(photo);
      };
      img.onload = function(){
        setPhotoSrc(photo.$elem, photo.src);
        if(options.successClass){
          photo.$elem.addClass(options.successClass);
        }
        $rootScope.$emit('lazyImg:success', photo);
        options.onSuccess(photo);
      };
      img.src = photo.src;
    }

    function setPhotoSrc($elem, src){
      if ($elem[0].nodeName.toLowerCase() === 'img') {
        $elem[0].src = src;
      } else {
        src=src.replace('"',"%22").replace("'","%27").replace("(","%28").replace(")","%29");
        $elem.css('background-image', 'url("' + src + '")');
      }
    }

    // PHOTO
    function Photo($elem){
      this.$elem = $elem;
    }

    Photo.prototype.setSource = function(source){
      this.src = source;
      images.unshift(this);
      if (!isListening){ startListening(); }
    };

    Photo.prototype.removeImage = function(){
      removeImage(this);
      if(images.length === 0){ stopListening(); }
    };

    Photo.prototype.checkImages = function(){
      checkImages();
    };

    Photo.addContainer = function (container) {
      stopListening();
      containers.push(container);
      startListening();
    };

    Photo.removeContainer = function (container) {
      stopListening();
      containers.splice(containers.indexOf(container), 1);
      startListening();
    };

    return Photo;

  }
]);

angular.module('angularLazyImg').provider('lazyImgConfig', function() {
  'use strict';

  this.options = {
    offset       : 100,
    errorClass   : null,
    successClass : null,
    onError      : function(){},
    onSuccess    : function(){}
  };

  this.$get = function() {
    var options = this.options;
    return {
      getOptions: function() {
        return options;
      }
    };
  };

  this.setOptions = function(options) {
    angular.extend(this.options, options);
  };
});
angular.module('angularLazyImg').factory('lazyImgHelpers', [
  '$window', function($window){
    'use strict';

    function getWinDimensions(){
      return {
        height: $window.innerHeight,
        width: $window.innerWidth
      };
    }

    function isElementInView(elem, offset, winDimensions) {
      var rect = elem.getBoundingClientRect();
      var bottomline = winDimensions.height + offset;
      return (
       rect.left >= 0 && rect.right <= winDimensions.width + offset && (
         rect.top >= 0 && rect.top <= bottomline ||
         rect.bottom <= bottomline && rect.bottom >= 0 - offset
        )
      );
    }

    // http://remysharp.com/2010/07/21/throttling-function-calls/
    function throttle(fn, threshhold, scope) {
      var last, deferTimer;
      return function () {
        var context = scope || this;
        var now = +new Date(),
            args = arguments;
        if (last && now < last + threshhold) {
          clearTimeout(deferTimer);
          deferTimer = setTimeout(function () {
            last = now;
            fn.apply(context, args);
          }, threshhold);
        } else {
          last = now;
          fn.apply(context, args);
        }
      };
    }

    return {
      isElementInView: isElementInView,
      getWinDimensions: getWinDimensions,
      throttle: throttle
    };

  }
]);
angular.module('angularLazyImg')
  .directive('lazyImg', [
    '$rootScope', 'LazyImgMagic', function ($rootScope, LazyImgMagic) {
      'use strict';

      function link(scope, element, attributes) {
        var lazyImage = new LazyImgMagic(element);
        attributes.$observe('lazyImg', function (newSource) {
          if (newSource) {
            // in angular 1.3 it might be nice to remove observer here
            lazyImage.setSource(newSource);
          }
        });
        scope.$on('$destroy', function () {
          lazyImage.removeImage();
        });
        $rootScope.$on('lazyImg.runCheck', function () {
          lazyImage.checkImages();
        });
        $rootScope.$on('lazyImg:refresh', function () {
          lazyImage.checkImages();
        });
      }

      return {
        link: link,
        restrict: 'A'
      };
    }
  ])
  .directive('lazyImgContainer', [
    'LazyImgMagic', function (LazyImgMagic) {
      'use strict';

      function link(scope, element) {
        LazyImgMagic.addContainer(element);
        scope.$on('$destroy', function () {
          LazyImgMagic.removeContainer(element);
        });
      }

      return {
        link: link,
        restrict: 'A'
      };
    }
  ]);


text:
package org.apache.shardingsphere.sharding.route.engine.type.standard;

import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.binder.SQLStatementContextFactory;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.DefaultSchema;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
import org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.schema.model.ColumnMetaData;
import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
import org.apache.shardingsphere.infra.parser.sql.SQLStatementParserEngine;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.engine.SQLRouteEngine;
import org.apache.shardingsphere.parser.rule.SQLParserRule;
import org.apache.shardingsphere.singletable.rule.SingleTableRule;
import org.apache.shardingsphere.sharding.route.engine.fixture.AbstractRoutingEngineTest;
import org.apache.shardingsphere.sharding.rule.ShardingRule;

import java.sql.Types;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;

public abstract class AbstractSQLRouteTest extends AbstractRoutingEngineTest {

    protected final RouteContext assertRoute(final String sql, final List<Object> parameters) {
        return assertRoute(sql, parameters, 1);
    }

    protected final RouteContext assertRoute(final String sql, final List<Object> parameters, final int routeUnitSize) {
        ShardingRule shardingRule = createAllShardingRule();
        SingleTableRule singleTableRule = createAllSingleTableRule(Collections.singletonList(shardingRule));
        ShardingSphereSchema schema = buildSchema();
        SQLParserRule sqlParserRule = createDefaultSQLParserRule();
        ConfigurationProperties props = new ConfigurationProperties(new Properties());
        SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine("MySQL", sqlParserRule);
        ShardingSphereRuleMetaData ruleMetaData = new ShardingSphereRuleMetaData(Collections.emptyList(), Arrays.asList(shardingRule, singleTableRule));
        ShardingSphereMetaData metaData = new ShardingSphereMetaData("sharding_db", mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), ruleMetaData, schema);
        Map<String, ShardingSphereMetaData> metaDataMap = Collections.singletonMap(DefaultSchema.LOGIC_NAME, metaData);
        SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance(metaDataMap, parameters, sqlStatementParserEngine.parse(sql, false), DefaultSchema.LOGIC_NAME);
        LogicSQL logicSQL = new LogicSQL(sqlStatementContext, sql, parameters);
        return new SQLRouteEngine(Arrays.asList(shardingRule, singleTableRule), props).route(logicSQL, metaData);
    }

    private ShardingSphereSchema buildSchema() {
        Map<String, TableMetaData> tableMetaDataMap = new HashMap<>(3, 1);
        tableMetaDataMap.put("t_order", new TableMetaData("t_order", Arrays.asList(new ColumnMetaData("order_id", Types.INTEGER, true, false, false),
                new ColumnMetaData("user_id", Types.INTEGER, false, false, false),
                new ColumnMetaData("status", Types.INTEGER, false, false, false)), Collections.emptySet()));
        tableMetaDataMap.put("t_order_item", new TableMetaData("t_order_item", Arrays.asList(new ColumnMetaData("item_id", Types.INTEGER, true, false, false),
                new ColumnMetaData("order_id", Types.INTEGER, false, false, false),
                new ColumnMetaData("user_id", Types.INTEGER, false, false, false),
                new ColumnMetaData("status", Types.VARCHAR, false, false, false),
                new ColumnMetaData("c_date", Types.TIMESTAMP, false, false, false)), Collections.emptySet()));
        tableMetaDataMap.put("t_other", new TableMetaData("t_other", Collections.singletonList(new ColumnMetaData("order_id", Types.INTEGER, true, false, false)), Collections.emptySet()));
        tableMetaDataMap.put("t_category", new TableMetaData("t_category"));
        return new ShardingSphereSchema(tableMetaDataMap);
    }
}


text:
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE FlexibleInstances #-}

module Eval (
  -- * Evaluator
  Eval,
  runEval,
  evalRec,

  -- * Evalaution state
  Dir,
  Step,
  Status(..),
  EvalState(..),
  Derivation(..),
  Trans,

  -- * Internal evaluation
  defaultEvalState,
  incIter,
  incDepth,
  decDepth,
  limitReached,
  failed,
  abort,

  -- * Classes
  Evalutable(..),
  Direction(..)
) where

import Control.Applicative
import Control.Monad.RWS
import Control.Monad.Reader as R

import Data.Data
import Data.Generics.Uniplate.Data

-------------------------------------------------------------------------------
-- Transformations
-------------------------------------------------------------------------------

type Witness = String
type Trans c a = a -> Eval c a (Witness, Maybe a)

-------------------------------------------------------------------------------
-- Derivations
-------------------------------------------------------------------------------

-- Derivation
data Step a = Step Witness a (Maybe a)
  deriving (Eq)

newtype Derivation a = Derivation { unDerivation :: [Step a] }
  deriving (Eq, Monoid)

instance Show a => Show (Step a) where
  show (Step rl x (Just y)) = rl ++ " : " ++ show x ++ " -> " ++ show y ++ "."
  show (Step rl x Nothing) = rl ++ " : " ++ show x  ++ "."

instance Show a => Show (Derivation a) where
  show (Derivation xs) = unlines (map show xs)

-------------------------------------------------------------------------------
-- Evaluation Classes
-------------------------------------------------------------------------------

class (Eq a, Data a, Show a) => Evalutable a where

-------------------------------------------------------------------------------
-- Evaluation State
-------------------------------------------------------------------------------

data Status = Success | Failed deriving (Eq, Ord, Show)

data EvalState c a = EvalState
    { depth        :: Integer
    , iter         :: Integer
    , status       :: Status
    , aborted      :: Bool
    , maxRecursion :: Integer
    , maxIteration :: Integer
    , self         :: a -> Eval c a a
    }

defaultEvalState :: EvalState c a
defaultEvalState = EvalState
    { depth        = 0
    , iter         = 0
    , status       = Success
    , aborted      = False
    , maxRecursion = 4096
    , maxIteration = 4096
    , self         = undefined
    }

incDepth :: Eval c a ()
incDepth = modify $ \s -> s { depth = depth s + 1 }

decDepth :: Eval c a ()
decDepth = modify $ \s -> s { depth = depth s - 1 }

incIter :: Eval c a ()
incIter = modify $ \s -> s { iter = iter s + 1 }

abort :: Eval c a ()
abort = modify $ \s -> s { aborted = True }

failed :: Eval c a ()
failed = modify $ \s -> s { status = Failed }

-------------------------------------------------------------------------------
-- Eval Loop
-------------------------------------------------------------------------------

-- Evaluation directives
data Direction = BottomUp | TopDown | Pass | Abort | Some [Bool]
type Dir c a = a -> ReaderT c IO Direction

type Eval c a r = (RWST
                     c               --  Evaluation context
                     (Derivation a)  --  Steps
                     (EvalState c a) --  Evaluation state
                     IO              --  Underlying IO
                     r)              --  Result

eval :: Evalutable a => Dir c a -> (a -> Eval c a (Witness, Maybe a)) -> a -> Eval c a a
eval d f x  = do
  ctx <- ask
  stop <- limitReached
  if stop then do
    failed
    return x
  else do
    dir <- lift $ runDir d x ctx
    case dir of

      -- transform children in bottom-up applicative order
      BottomUp -> do
        incDepth
        y <- descendM (eval d f) x
        decDepth
        step@(_, z) <- f x
        -- apply to the root
        case z of
          Just r -> do
            addStep (mkStep y step)
            incIter
            eval d f r
          -- If in normal form then halt
          Nothing -> return y

      TopDown -> do
        step@(_, y) <- f x
        -- apply to the root
        z <- case y of
          Just r -> do
            addStep (mkStep x step)
            incIter
            eval d f r
          -- If in normal form then halt
          Nothing -> return x
        incDepth
        res <- descendM (eval d f) z
        decDepth
        return res

      -- do not proceed to children
      Pass -> do
        step@(_, z) <- f x
        case z of
          Just r -> do
            addStep (mkStep x step)
            incIter
            eval d f r
          -- If in normal form then halt
          Nothing -> return x

      Some xs -> undefined

      Abort -> do
        abort
        return x

mkStep :: a -> (String, Maybe a) -> Step a
mkStep orig (rl, res) = Step rl orig res

limitReached :: Eval c a Bool
limitReached = do
  i <- gets depth
  j <- gets iter
  stop <- gets aborted
  maxi <- gets maxRecursion
  maxj <- gets maxIteration
  return $ stop || i > maxi || j > maxj

runDir :: Dir c a -> a -> c -> IO Direction
runDir d x = runReaderT (d x)

addStep :: Step a -> Eval c a ()
addStep step = do
  i <- gets depth
  when (i == 0) $ tell (Derivation [step])

runEval :: Evalutable a => c -> Dir c a
                        -> Trans c a
                        -> a -> IO (a, EvalState c a, Derivation a)
runEval ctx d f x = runRWST (eval d f x) ctx (defaultEvalState { self = eval d f })

-- | Embed a sub-evaluator within another evaluator, sharing the same initial state but yielding a pure result
-- to the external one and the extending the external state with the resulting artifacts.
--
-- @
--   Eval s a (Eval t a) --> Eval (join s t) a
--   +-------------+         +-------------+
--   |             |         |             |
--   |   +-----+   |         |             |
--   |   |  a  | --|---------|--->  a      |
--   |   +-----+   |         |             |
--   |             |         |             |
--   +-------------+         +-------------+
-- @
--
evalRec :: Evalutable a => a -> Eval c a (Maybe a)
evalRec x = do
  st <- get
  ctx <- ask
  (res, st', deriv) <- lift $ runRWST (self st $ x) ctx st
  if status st == Success then do
    tell deriv
    put st'
    return $ Just res
  else
    return Nothing


text:
Fae has a built in system to handle content blocks that are statically wired to pages in your site. This is for content that isn't tied to an object in your data model, e.g. home, about and terms content.

The system is just your basic inherited singleton with dynamic polymorphic associations. Kidding aside, the complexity of the system is hidden and "it just works™" if you use the generators and/or follow the conventions. This allows for dynamic content blocks that can be added without database migrations and wired up without static IDs!

* [Pages vs Content Blocks](#pages-vs-content-blocks)
* [Generating Pages](#generating-pages)
* [Adding Content Blocks](#adding-content-blocks)
* [Getting Your Content Blocks](#getting-your-content-blocks)
* [Invalid Content Block Names](#invalid-content-block-names)
* [Validations on Content Blocks](#validations-on-content-blocks)

---

## Pages vs Content Blocks

**Pages** are groups of **content blocks** based on the actual pages they appear on the site.

Content blocks are defined in the model and called in the form view. The `type` refers to the generator API (more information available in [the following section](#generating-pages))

| associated object | type | form helper |
| ----------------- | ---- | ----------- |
| Fae::TextField | string | fae_content_form |
| Fae::TextArea | text | fae_content_form |
| Fae::Image | image | fae_image_form |
| Fae::File | file | fae_file_form |

For the following example, we will use a page called `AboutUs`, which will have content blocks for `hero_image`, `title`, `introduction`, `body` and `annual_report`.

## Generating Pages

It is highly recommended you use the built in generator to add pages, especially if it's the first page in the admin. Let's do that for our example:

```bash
rails g fae:page AboutUs hero_image:image hero_text:string introduction:text body:text annual_report:file
```

This will generate...

`app/models/about_us_page.rb`
```ruby
class AboutUsPage < Fae::StaticPage

  @slug = 'about_us'

  # required to set the has_one associations, Fae::StaticPage will build these associations dynamically
  def self.fae_fields
    {
      hero_image: { type: Fae::Image },
      hero_text: { type: Fae::TextField },
      introduction: { type: Fae::TextArea },
      body: { type: Fae::TextArea },
      annual_report: { type: Fae::File }
    }
  end

end
```

`app/views/fae/pages/about_us.html.slim`
```ruby
= simple_form_for @item, url: fae.update_content_block_path(slug: @item.slug), method: :put do |f|
  header.content-header
    = render 'fae/shared/form_header', header: @item, f: f

  .content
    = fae_input f, :title

    = fae_image_form f, :hero_image
    = fae_content_form f, :hero_text
    = fae_content_form f, :introduction
    = fae_content_form f, :body
    = fae_file_form f, :annual_report
```

Since this is the first page the generator will create `app/controllers/admin/content_blocks_controller.rb`, otherwise it would just add to the `fae_pages` array.

```ruby
module Admin
  class ContentBlocksController < Fae::StaticPagesController

    private

    def fae_pages
      [AboutUsPage]
    end
  end
end
```

## Adding Content Blocks

Chances are you'll need to add content blocks to a page after it's been generated. To do so simply:

- add the new content blocks to `fae_fields` in the `AboutUsPage` model
- add the appropriate form elements to the form at `about_us.html.slim`
  - `fae_content_form` for `Fae::TextField` and `Fae::TextArea`
  - `fae_image_form` for `Fae::Image`
  - `fae_file_form` for `Fae::File`

## Getting Your Content Blocks

Each page generated is a singleton model and each content block is an association to a Fae model.

To get an instance of your page:

```ruby
@about_us_page = AboutUsPage.instance
```

Then to get content from a `Fae::TextField` and `Fae::TextArea`:

```ruby
# for `Fae::TextField` or `Fae::TextArea`
@about_us_page.hero_text.content
# ... or ...
@about_us_page.hero_text_content

# for `Fae::Image` or `Fae::File`
@about_us_page.hero_image.asset.url
# for `Fae::Image` only
@about_us_page.hero_image.alt
@about_us_page.hero_image.caption
```

## Invalid Content Block Names

Content blocks are just associations on the page model, which inherits from `Fae::Page`. Because of this, attributes on `Fae::Page` are invalid names for content blocks. These attributes are:

- title
- slug
- position
- on_prod
- on_stage
- created_at
- updated_at

## Validations on Content Blocks

Since content blocks are setup as associations, adding validations to them can be tricky. To make it easier we setup a method directly in `fae_fields` hash that will dynamically add the validations to the appropriate model and apply the `data-validate` attribute in the form so Judge can  do it's best to validate the content on the frontend.

To add validations to a content block, add a validates option with your rules on a specific content block in `fae_fields`. Format the rules just as you would normal model validations.

`app/models/about_us_page.rb`
```ruby
def self.fae_fields
  {
    hero_image: { type: Fae::Image },
    hero_text: {
      type: Fae::TextField,
      validates: { presence: true }
      },
    introduction: {
      type: Fae::TextArea,
      validates: {
          presence: true,
          length: {
            maximum: 100,
            message: 'should be brief (100 characters or less)'
            }
        },
      },
    body: { type: Fae::TextArea },
    annual_report: { type: Fae::File }
  }
end
```

Validations can only be applied to types `Fae::TextField` and `Fae::TextArea`.

## Linking to Pages Within Fae

Link to the edit screen of a page and its respective content blocks by adding an item to navigation items.

```ruby
def nav_items
  [
    ...
    {
      text: 'Pages', sublinks: [
        { text: 'All', path: fae.pages_path },
        # For example, if `@slug = 'home'`
        { text: 'Home', path: fae.edit_content_block_path('home') }
      ]
    }
    ...
  ]
end
```


text: This is the initial document for hapi doc processing.


text:
import collections
import itertools
import operator
import random
import time
import uuid

from neutron_lib import constants as p_const
from neutron_lib import exceptions
from oslo_config import cfg
from oslo_log import log as logging
import six
import tenacity

from neutron._i18n import _
from neutron.agent.common import ip_lib
from neutron.agent.common import utils
from neutron.agent.ovsdb import impl_idl
from neutron.common import constants as common_constants
from neutron.common import utils as common_utils
from neutron.conf.agent import ovs_conf
from neutron.plugins.ml2.drivers.openvswitch.agent.common \
    import constants

UINT64_BITMASK = (1 << 64) - 1

# Special return value for an invalid OVS ofport
INVALID_OFPORT = -1
UNASSIGNED_OFPORT = []

# OVS bridge fail modes
FAILMODE_SECURE = 'secure'
FAILMODE_STANDALONE = 'standalone'

# special values for cookies
COOKIE_ANY = object()

ovs_conf.register_ovs_agent_opts()

LOG = logging.getLogger(__name__)

OVS_DEFAULT_CAPS = {
    'datapath_types': [],
    'iface_types': [],
}

# It's default queue, all packets not tagged with 'set_queue' will go through
# this one
QOS_DEFAULT_QUEUE = 0

_SENTINEL = object()

CTRL_RATE_LIMIT_MIN = 100
CTRL_BURST_LIMIT_MIN = 25

def _ovsdb_result_pending(result):
    """Return True if ovsdb indicates the result is still pending."""
    # ovsdb can return '[]' for an ofport that has not yet been assigned
    return result == []

def _ovsdb_retry(fn):
    """Decorator for retrying when OVS has yet to assign an ofport.

    The instance's ovsdb_timeout is used as the max waiting time. This relies
    on the fact that instance methods receive self as the first argument.
    """
    @six.wraps(fn)
    def wrapped(*args, **kwargs):
        self = args[0]
        new_fn = tenacity.retry(
            reraise=True,
            retry=tenacity.retry_if_result(_ovsdb_result_pending),
            wait=tenacity.wait_exponential(multiplier=0.01, max=1),
            stop=tenacity.stop_after_delay(
                self.ovsdb_timeout))(fn)
        return new_fn(*args, **kwargs)
    return wrapped

class VifPort(object):
    def __init__(self, port_name, ofport, vif_id, vif_mac, switch):
        self.port_name = port_name
        self.ofport = ofport
        self.vif_id = vif_id
        self.vif_mac = vif_mac
        self.switch = switch

    def __str__(self):
        return ("iface-id=%s, vif_mac=%s, port_name=%s, ofport=%s, "
                "bridge_name=%s") % (
                    self.vif_id, self.vif_mac,
                    self.port_name, self.ofport,
                    self.switch.br_name)

class BaseOVS(object):

    def __init__(self):
        self.ovsdb_timeout = cfg.CONF.OVS.ovsdb_timeout
        self.ovsdb = impl_idl.api_factory()

    def add_manager(self, connection_uri, timeout=_SENTINEL):
        """Have ovsdb-server listen for manager connections

        :param connection_uri: Manager target string
        :param timeout: The Manager probe_interval timeout value
                        (defaults to ovsdb_timeout)
        """
        if timeout is _SENTINEL:
            timeout = cfg.CONF.OVS.ovsdb_timeout
        with self.ovsdb.transaction() as txn:
            txn.add(self.ovsdb.add_manager(connection_uri))
            if timeout:
                txn.add(
                    self.ovsdb.db_set('Manager', connection_uri,
                                      ('inactivity_probe', timeout * 1000)))

    def get_manager(self):
        return self.ovsdb.get_manager().execute()

    def remove_manager(self, connection_uri):
        self.ovsdb.remove_manager(connection_uri).execute()

    def add_bridge(self, bridge_name,
                   datapath_type=constants.OVS_DATAPATH_SYSTEM):
        br = OVSBridge(bridge_name, datapath_type=datapath_type)
        br.create()
        return br

    def delete_bridge(self, bridge_name):
        self.ovsdb.del_br(bridge_name).execute()

    def bridge_exists(self, bridge_name):
        return self.ovsdb.br_exists(bridge_name).execute()

    def port_exists(self, port_name):
        cmd = self.ovsdb.db_get('Port', port_name, 'name')
        return bool(cmd.execute(check_error=False, log_errors=False))

    def get_bridge_for_iface(self, iface):
        return self.ovsdb.iface_to_br(iface).execute()

    def get_bridges(self):
        return self.ovsdb.list_br().execute(check_error=True)

    def get_bridge_external_bridge_id(self, bridge):
        return self.ovsdb.br_get_external_id(bridge, 'bridge-id').execute()

    def set_db_attribute(self, table_name, record, column, value,
                         check_error=False, log_errors=True):
        self.ovsdb.db_set(table_name, record, (column, value)).execute(
            check_error=check_error, log_errors=log_errors)

    def clear_db_attribute(self, table_name, record, column):
        self.ovsdb.db_clear(table_name, record, column).execute()

    def db_get_val(self, table, record, column, check_error=False,
                   log_errors=True):
        return self.ovsdb.db_get(table, record, column).execute(
            check_error=check_error, log_errors=log_errors)

    @property
    def config(self):
        """A dict containing the only row from the root Open_vSwitch table

        This row contains several columns describing the Open vSwitch install
        and the system on which it is installed. Useful keys include:
            datapath_types: a list of supported datapath types
            iface_types: a list of supported interface types
            ovs_version: the OVS version
        """
        return self.ovsdb.db_list("Open_vSwitch").execute()[0]

    @property
    def capabilities(self):
        _cfg = self.config
        return {k: _cfg.get(k, OVS_DEFAULT_CAPS[k]) for k in OVS_DEFAULT_CAPS}

# Map from version string to on-the-wire protocol version encoding:
OF_PROTOCOL_TO_VERSION = {
    constants.OPENFLOW10: 1,
    constants.OPENFLOW11: 2,
    constants.OPENFLOW12: 3,
    constants.OPENFLOW13: 4,
    constants.OPENFLOW14: 5,
    constants.OPENFLOW15: 6,
}

def version_from_protocol(protocol):
    if protocol not in OF_PROTOCOL_TO_VERSION:
        raise Exception(_("unknown OVS protocol string, cannot compare: "
                          "%(protocol)s, (known: %(known)s)") %
                        {'protocol': protocol,
                         'known': list(OF_PROTOCOL_TO_VERSION)})
    return OF_PROTOCOL_TO_VERSION[protocol]

class OVSBridge(BaseOVS):
    def __init__(self, br_name, datapath_type=constants.OVS_DATAPATH_SYSTEM):
        super(OVSBridge, self).__init__()
        self.br_name = br_name
        self.datapath_type = datapath_type
        self._default_cookie = generate_random_cookie()
        self._highest_protocol_needed = constants.OPENFLOW10

    @property
    def default_cookie(self):
        return self._default_cookie

    def set_agent_uuid_stamp(self, val):
        self._default_cookie = val

    def set_controller(self, controllers):
        self.ovsdb.set_controller(self.br_name,
                                  controllers).execute(check_error=True)

    def del_controller(self):
        self.ovsdb.del_controller(self.br_name).execute(check_error=True)

    def get_controller(self):
        return self.ovsdb.get_controller(self.br_name).execute(
            check_error=True)

    def _set_bridge_fail_mode(self, mode):
        self.ovsdb.set_fail_mode(self.br_name, mode).execute(check_error=True)

    def set_secure_mode(self):
        self._set_bridge_fail_mode(FAILMODE_SECURE)

    def set_standalone_mode(self):
        self._set_bridge_fail_mode(FAILMODE_STANDALONE)

    def add_protocols(self, *protocols):
        self.ovsdb.db_add('Bridge', self.br_name,
                          'protocols', *protocols).execute(check_error=True)

    def use_at_least_protocol(self, protocol):
        """Calls to ovs-ofctl will use a protocol version >= 'protocol'"""
        self.add_protocols(protocol)
        self._highest_protocol_needed = max(self._highest_protocol_needed,
                                            protocol,
                                            key=version_from_protocol)

    def create(self, secure_mode=False):
        other_config = {
            'mac-table-size': str(cfg.CONF.OVS.bridge_mac_table_size)}
        with self.ovsdb.transaction() as txn:
            txn.add(
                self.ovsdb.add_br(self.br_name,
                                  datapath_type=self.datapath_type))
            # the ovs-ofctl commands below in run_ofctl use OF10, so we
            # need to ensure that this version is enabled ; we could reuse
            # add_protocols, but doing ovsdb.db_add avoids doing two
            # transactions
            txn.add(
                self.ovsdb.db_add('Bridge', self.br_name,
                                  'protocols', self._highest_protocol_needed))
            txn.add(
                self.ovsdb.db_set('Bridge', self.br_name,
                                  ('other_config', other_config)))
            if secure_mode:
                txn.add(self.ovsdb.set_fail_mode(self.br_name,
                                                 FAILMODE_SECURE))

    def destroy(self):
        self.delete_bridge(self.br_name)

    def add_port(self, port_name, *interface_attr_tuples):
        with self.ovsdb.transaction() as txn:
            txn.add(self.ovsdb.add_port(self.br_name, port_name))
            if interface_attr_tuples:
                txn.add(self.ovsdb.db_set('Interface', port_name,
                                          *interface_attr_tuples))
        return self.get_port_ofport(port_name)

    def replace_port(self, port_name, *interface_attr_tuples):
        """Replace existing port or create it, and configure port interface."""

        # NOTE(xiaohhui): If del_port is inside the transaction, there will
        # only be one command for replace_port. This will cause the new port
        # not be found by system, which will lead to Bug #1519926.
        self.ovsdb.del_port(port_name).execute()
        with self.ovsdb.transaction() as txn:
            txn.add(self.ovsdb.add_port(self.br_name, port_name,
                                        may_exist=False))
            # NOTE(mangelajo): Port is added to dead vlan (4095) by default
            # until it's handled by the neutron-openvswitch-agent. Otherwise it
            # becomes a trunk port on br-int (receiving traffic for all vlans),
            # and also triggers issues on ovs-vswitchd related to the
            # datapath flow revalidator thread, see lp#1767422
            txn.add(self.ovsdb.db_set(
                    'Port', port_name, ('tag', constants.DEAD_VLAN_TAG)))

            # TODO(mangelajo): We could accept attr tuples for the Port too
            # but, that could potentially break usage of this function in
            # stable branches (where we need to backport).
            # https://review.openstack.org/#/c/564825/4/neutron/agent/common/
            # ovs_lib.py@289
            if interface_attr_tuples:
                txn.add(self.ovsdb.db_set('Interface', port_name,
                                          *interface_attr_tuples))

    def delete_port(self, port_name):
        self.ovsdb.del_port(port_name, self.br_name).execute()

    def run_ofctl(self, cmd, args, process_input=None):
        full_args = ["ovs-ofctl", cmd,
                     "-O", self._highest_protocol_needed,
                     self.br_name] + args
        # TODO(kevinbenton): This error handling is really brittle and only
        # detects one specific type of failure. The callers of this need to
        # be refactored to expect errors so we can re-raise and they can
        # take appropriate action based on the type of error.
        for i in range(1, 11):
            try:
                return utils.execute(full_args, run_as_root=True,
                                     process_input=process_input)
            except Exception as e:
                if "failed to connect to socket" in str(e):
                    LOG.debug("Failed to connect to OVS. Retrying "
                              "in 1 second. Attempt: %s/10", i)
                    time.sleep(1)
                    continue
                LOG.error("Unable to execute %(cmd)s. Exception: "
                          "%(exception)s",
                          {'cmd': full_args, 'exception': e})
                break

    def count_flows(self):
        flow_list = self.run_ofctl("dump-flows", []).split("\n")[1:]
        return len(flow_list) - 1

    def remove_all_flows(self):
        self.run_ofctl("del-flows", [])

    @_ovsdb_retry
    def _get_port_val(self, port_name, port_val):
        return self.db_get_val("Interface", port_name, port_val)

    def get_port_ofport(self, port_name):
        """Get the port's assigned ofport, retrying if not yet assigned."""
        ofport = INVALID_OFPORT
        try:
            ofport = self._get_port_val(port_name, "ofport")
        except tenacity.RetryError:
            LOG.exception("Timed out retrieving ofport on port %s.",
                          port_name)
        return ofport

    def get_port_external_ids(self, port_name):
        """Get the port's assigned ofport, retrying if not yet assigned."""
        port_external_ids = dict()
        try:
            port_external_ids = self._get_port_val(port_name, "external_ids")
        except tenacity.RetryError:
            LOG.exception("Timed out retrieving external_ids on port %s.",
                          port_name)
        return port_external_ids

    def get_port_mac(self, port_name):
        """Get the port's mac address.

        This is especially useful when the port is not a neutron port.
        E.g. networking-sfc needs the MAC address of "patch-tun
        """
        return self.db_get_val("Interface", port_name, "mac_in_use")

    @_ovsdb_retry
    def _get_datapath_id(self):
        return self.db_get_val('Bridge', self.br_name, 'datapath_id')

    def get_datapath_id(self):
        try:
            return self._get_datapath_id()
        except tenacity.RetryError:
            # if ovs fails to find datapath_id then something is likely to be
            # broken here
            LOG.exception("Timed out retrieving datapath_id on bridge %s.",
                          self.br_name)
            raise RuntimeError(_('No datapath_id on bridge %s') % self.br_name)

    def do_action_flows(self, action, kwargs_list, use_bundle=False):
        # we can't mix strict and non-strict, so we'll use the first kw
        # and check against other kw being different
        strict = kwargs_list[0].get('strict', False)

        for kw in kwargs_list:
            if action is 'del':
                if kw.get('cookie') == COOKIE_ANY:
                    # special value COOKIE_ANY was provided, unset
                    # cookie to match flows whatever their cookie is
                    kw.pop('cookie')
                    if kw.get('cookie_mask'):  # non-zero cookie mask
                        raise Exception(_("cookie=COOKIE_ANY but cookie_mask "
                                          "set to %s") % kw.get('cookie_mask'))
                elif 'cookie' in kw:
                    # a cookie was specified, use it
                    kw['cookie'] = check_cookie_mask(kw['cookie'])
                else:
                    # nothing was specified about cookies, use default
                    kw['cookie'] = "%d/-1" % self._default_cookie
            else:
                if 'cookie' not in kw:
                    kw['cookie'] = self._default_cookie

            if action in ('mod', 'del'):
                if kw.pop('strict', False) != strict:
                    msg = ("cannot mix 'strict' and not 'strict' in a batch "
                           "call")
                    raise exceptions.InvalidInput(error_message=msg)
            else:
                if kw.pop('strict', False):
                    msg = "cannot use 'strict' with 'add' action"
                    raise exceptions.InvalidInput(error_message=msg)

        extra_param = ["--strict"] if strict else []

        if action == 'del' and {} in kwargs_list:
            # the 'del' case simplifies itself if kwargs_list has at least
            # one item that matches everything
            self.run_ofctl('%s-flows' % action, [])
        else:
            flow_strs = [_build_flow_expr_str(kw, action, strict)
                         for kw in kwargs_list]
            if use_bundle:
                extra_param.append('--bundle')
            self.run_ofctl('%s-flows' % action, extra_param + ['-'],
                           '\n'.join(flow_strs))

    def add_flow(self, **kwargs):
        self.do_action_flows('add', [kwargs])

    def mod_flow(self, **kwargs):
        self.do_action_flows('mod', [kwargs])

    def delete_flows(self, **kwargs):
        self.do_action_flows('del', [kwargs])

    def dump_flows_for_table(self, table):
        return self.dump_flows_for(table=table)

    def dump_flows_for(self, **kwargs):
        retval = None
        if "cookie" in kwargs:
            kwargs["cookie"] = check_cookie_mask(str(kwargs["cookie"]))
        flow_str = ",".join("=".join([key, str(val)])
            for key, val in kwargs.items())

        flows = self.run_ofctl("dump-flows", [flow_str])
        if flows:
            retval = '\n'.join(item for item in flows.splitlines()
                               if is_a_flow_line(item))
        return retval

    def dump_all_flows(self):
        return [f for f in self.run_ofctl("dump-flows", []).splitlines()
                if is_a_flow_line(f)]

    def deferred(self, **kwargs):
        return DeferredOVSBridge(self, **kwargs)

    def add_tunnel_port(self, port_name, remote_ip, local_ip,
                        tunnel_type=p_const.TYPE_GRE,
                        vxlan_udp_port=p_const.VXLAN_UDP_PORT,
                        dont_fragment=True,
                        tunnel_csum=False,
                        tos=None):
        attrs = [('type', tunnel_type)]
        # TODO(twilson) This is an OrderedDict solely to make a test happy
        options = collections.OrderedDict()
        vxlan_uses_custom_udp_port = (
            tunnel_type == p_const.TYPE_VXLAN and
            vxlan_udp_port != p_const.VXLAN_UDP_PORT
        )
        if vxlan_uses_custom_udp_port:
            options['dst_port'] = str(vxlan_udp_port)
        options['df_default'] = str(dont_fragment).lower()
        options['remote_ip'] = remote_ip
        options['local_ip'] = local_ip
        options['in_key'] = 'flow'
        options['out_key'] = 'flow'
        if tunnel_csum:
            options['csum'] = str(tunnel_csum).lower()
        if tos:
            options['tos'] = str(tos)
        attrs.append(('options', options))

        return self.add_port(port_name, *attrs)

    def add_patch_port(self, local_name, remote_name):
        attrs = [('type', 'patch'),
                 ('options', {'peer': remote_name})]
        return self.add_port(local_name, *attrs)

    def get_iface_name_list(self):
        # get the interface name list for this bridge
        return self.ovsdb.list_ifaces(self.br_name).execute(check_error=True)

    def get_port_name_list(self):
        # get the port name list for this bridge
        return self.ovsdb.list_ports(self.br_name).execute(check_error=True)

    def get_port_stats(self, port_name):
        return self.db_get_val("Interface", port_name, "statistics")

    def get_ports_attributes(self, table, columns=None, ports=None,
                             check_error=True, log_errors=True,
                             if_exists=False):
        port_names = ports or self.get_port_name_list()
        if not port_names:
            return []
        return (self.ovsdb.db_list(table, port_names, columns=columns,
                                   if_exists=if_exists).
                execute(check_error=check_error, log_errors=log_errors))

    # returns a VIF object for each VIF port
    def get_vif_ports(self, ofport_filter=None):
        edge_ports = []
        port_info = self.get_ports_attributes(
            'Interface', columns=['name', 'external_ids', 'ofport'],
            if_exists=True)
        for port in port_info:
            name = port['name']
            external_ids = port['external_ids']
            ofport = port['ofport']
            if ofport_filter and ofport in ofport_filter:
                continue
            if "iface-id" in external_ids and "attached-mac" in external_ids:
                p = VifPort(name, ofport, external_ids["iface-id"],
                            external_ids["attached-mac"], self)
                edge_ports.append(p)

        return edge_ports

    def get_vif_port_to_ofport_map(self):
        results = self.get_ports_attributes(
            'Interface', columns=['name', 'external_ids', 'ofport'],
            if_exists=True)
        port_map = {}
        for r in results:
            # fall back to basic interface name
            key = self.portid_from_external_ids(r['external_ids']) or r['name']
            try:
                port_map[key] = int(r['ofport'])
            except TypeError:
                # port doesn't yet have an ofport entry so we ignore it
                pass
        return port_map

    def get_vif_port_set(self):
        edge_ports = set()
        results = self.get_ports_attributes(
            'Interface', columns=['name', 'external_ids', 'ofport'],
            if_exists=True)
        for result in results:
            if result['ofport'] == UNASSIGNED_OFPORT:
                LOG.warning("Found not yet ready openvswitch port: %s",
                            result['name'])
            elif result['ofport'] == INVALID_OFPORT:
                LOG.warning("Found failed openvswitch port: %s",
                            result['name'])
            elif 'attached-mac' in result['external_ids']:
                port_id = self.portid_from_external_ids(result['external_ids'])
                if port_id:
                    edge_ports.add(port_id)
        return edge_ports

    def portid_from_external_ids(self, external_ids):
        if 'iface-id' in external_ids:
            return external_ids['iface-id']

    def get_port_tag_dict(self):
        """Get a dict of port names and associated vlan tags.

        e.g. the returned dict is of the following form::

            {u'int-br-eth2': [],
             u'patch-tun': [],
             u'qr-76d9e6b6-21': 1,
             u'tapce5318ff-78': 1,
             u'tape1400310-e6': 1}

        The TAG ID is only available in the "Port" table and is not available
        in the "Interface" table queried by the get_vif_port_set() method.

        """
        results = self.get_ports_attributes(
            'Port', columns=['name', 'tag'], if_exists=True)
        return {p['name']: p['tag'] for p in results}

    def get_vifs_by_ids(self, port_ids):
        interface_info = self.get_ports_attributes(
            "Interface", columns=["name", "external_ids", "ofport"],
            if_exists=True)
        by_id = {x['external_ids'].get('iface-id'): x for x in interface_info}
        result = {}
        for port_id in port_ids:
            result[port_id] = None
            if port_id not in by_id:
                LOG.info("Port %(port_id)s not present in bridge "
                         "%(br_name)s",
                         {'port_id': port_id, 'br_name': self.br_name})
                continue
            pinfo = by_id[port_id]
            if not self._check_ofport(port_id, pinfo):
                continue
            mac = pinfo['external_ids'].get('attached-mac')
            result[port_id] = VifPort(pinfo['name'], pinfo['ofport'],
                                      port_id, mac, self)
        return result

    @staticmethod
    def _check_ofport(port_id, port_info):
        if port_info['ofport'] in [UNASSIGNED_OFPORT, INVALID_OFPORT]:
            LOG.warning("ofport: %(ofport)s for VIF: %(vif)s "
                        "is not a positive integer",
                        {'ofport': port_info['ofport'], 'vif': port_id})
            return False
        return True

    def get_vif_port_by_id(self, port_id):
        ports = self.ovsdb.db_find(
            'Interface', ('external_ids', '=', {'iface-id': port_id}),
            ('external_ids', '!=', {'attached-mac': ''}),
            columns=['external_ids', 'name', 'ofport']).execute()
        for port in ports:
            if self.br_name != self.get_bridge_for_iface(port['name']):
                continue
            if not self._check_ofport(port_id, port):
                continue
            mac = port['external_ids'].get('attached-mac')
            return VifPort(port['name'], port['ofport'], port_id, mac, self)
        LOG.info("Port %(port_id)s not present in bridge %(br_name)s",
                 {'port_id': port_id, 'br_name': self.br_name})

    def delete_ports(self, all_ports=False):
        if all_ports:
            port_names = self.get_port_name_list()
        else:
            port_names = (port.port_name for port in self.get_vif_ports())

        for port_name in port_names:
            self.delete_port(port_name)

    def get_local_port_mac(self):
        """Retrieve the mac of the bridge's local port."""
        address = ip_lib.IPDevice(self.br_name).link.address
        if address:
            return address
        else:
            msg = _('Unable to determine mac address for %s') % self.br_name
            raise Exception(msg)

    def set_controllers_connection_mode(self, connection_mode):
        """Set bridge controllers connection mode.

        :param connection_mode: "out-of-band" or "in-band"
        """
        self.set_controller_field('connection_mode', connection_mode)

    def _set_egress_bw_limit_for_port(self, port_name, max_kbps,
                                      max_burst_kbps):
        with self.ovsdb.transaction(check_error=True) as txn:
            txn.add(self.ovsdb.db_set('Interface', port_name,
                                      ('ingress_policing_rate', max_kbps)))
            txn.add(self.ovsdb.db_set('Interface', port_name,
                                      ('ingress_policing_burst',
                                       max_burst_kbps)))

    def create_egress_bw_limit_for_port(self, port_name, max_kbps,
                                        max_burst_kbps):
        self._set_egress_bw_limit_for_port(
            port_name, max_kbps, max_burst_kbps)

    def get_egress_bw_limit_for_port(self, port_name):

        max_kbps = self.db_get_val('Interface', port_name,
                                   'ingress_policing_rate')
        max_burst_kbps = self.db_get_val('Interface', port_name,
                                         'ingress_policing_burst')

        max_kbps = max_kbps or None
        max_burst_kbps = max_burst_kbps or None

        return max_kbps, max_burst_kbps

    def delete_egress_bw_limit_for_port(self, port_name):
        if not self.port_exists(port_name):
            return
        self._set_egress_bw_limit_for_port(
            port_name, 0, 0)

    def find_qos(self, port_name):
        qos = self.ovsdb.db_find(
            'QoS',
            ('external_ids', '=', {'id': port_name}),
            columns=['_uuid', 'other_config']).execute(check_error=True)
        if qos:
            return qos[0]

    def find_queue(self, port_name, queue_type):
        queues = self.ovsdb.db_find(
            'Queue',
            ('external_ids', '=', {'id': port_name,
                                   'queue_type': str(queue_type)}),
            columns=['_uuid', 'other_config']).execute(check_error=True)
        if queues:
            return queues[0]

    def _update_bw_limit_queue(self, txn, port_name, queue_uuid, queue_type,
                               other_config):
        if queue_uuid:
            txn.add(self.ovsdb.db_set(
                'Queue', queue_uuid,
                ('other_config', other_config)))
        else:
            external_ids = {'id': port_name,
                            'queue_type': str(queue_type)}
            queue_uuid = txn.add(
                self.ovsdb.db_create(
                    'Queue', external_ids=external_ids,
                    other_config=other_config))
        return queue_uuid

    def _update_bw_limit_profile(self, txn, port_name, qos_uuid,
                                 queue_uuid, queue_type, qos_other_config):
        queues = {queue_type: queue_uuid}
        if qos_uuid:
            txn.add(self.ovsdb.db_set(
                'QoS', qos_uuid, ('queues', queues)))
            txn.add(self.ovsdb.db_set(
                'QoS', qos_uuid, ('other_config', qos_other_config)))
        else:
            external_ids = {'id': port_name}
            qos_uuid = txn.add(
                self.ovsdb.db_create(
                    'QoS', external_ids=external_ids,
                    type='linux-htb',
                    queues=queues,
                    other_config=qos_other_config))
        return qos_uuid

    def _update_bw_limit_profile_dpdk(self, txn, port_name, qos_uuid,
                                      other_config):
        if qos_uuid:
            txn.add(self.ovsdb.db_set(
                'QoS', qos_uuid, ('other_config', other_config)))
        else:
            external_ids = {'id': port_name}
            qos_uuid = txn.add(
                self.ovsdb.db_create(
                    'QoS', external_ids=external_ids, type='egress-policer',
                    other_config=other_config))
        return qos_uuid

    def _update_ingress_bw_limit_for_port(
            self, port_name, max_bw_in_bits, max_burst_in_bits):
        qos_other_config = {
            'max-rate': str(max_bw_in_bits)
        }
        queue_other_config = {
            'max-rate': str(max_bw_in_bits),
            'burst': str(max_burst_in_bits),
        }
        qos = self.find_qos(port_name)
        queue = self.find_queue(port_name, QOS_DEFAULT_QUEUE)
        qos_uuid = qos['_uuid'] if qos else None
        queue_uuid = queue['_uuid'] if queue else None
        with self.ovsdb.transaction(check_error=True) as txn:
            queue_uuid = self._update_bw_limit_queue(
                txn, port_name, queue_uuid, QOS_DEFAULT_QUEUE,
                queue_other_config
            )

            qos_uuid = self._update_bw_limit_profile(
                txn, port_name, qos_uuid, queue_uuid, QOS_DEFAULT_QUEUE,
                qos_other_config
            )

            txn.add(self.ovsdb.db_set(
                'Port', port_name, ('qos', qos_uuid)))

    def _update_ingress_bw_limit_for_dpdk_port(
            self, port_name, max_bw_in_bits, max_burst_in_bits):
        # cir and cbs should be set in bytes instead of bits
        qos_other_config = {
            'cir': str(max_bw_in_bits / 8),
            'cbs': str(max_burst_in_bits / 8)
        }
        qos = self.find_qos(port_name)
        qos_uuid = qos['_uuid'] if qos else None
        with self.ovsdb.transaction(check_error=True) as txn:
            qos_uuid = self._update_bw_limit_profile_dpdk(
                txn, port_name, qos_uuid, qos_other_config)
            txn.add(self.ovsdb.db_set(
                'Port', port_name, ('qos', qos_uuid)))

    def update_ingress_bw_limit_for_port(self, port_name, max_kbps,
                                         max_burst_kbps):
        max_bw_in_bits = max_kbps * common_constants.SI_BASE
        max_burst_in_bits = max_burst_kbps * common_constants.SI_BASE
        port_type = self._get_port_val(port_name, "type")
        if port_type in constants.OVS_DPDK_PORT_TYPES:
            self._update_ingress_bw_limit_for_dpdk_port(
                port_name, max_bw_in_bits, max_burst_in_bits)
        else:
            self._update_ingress_bw_limit_for_port(
                port_name, max_bw_in_bits, max_burst_in_bits)

    def get_ingress_bw_limit_for_port(self, port_name):
        max_kbps = None
        qos_max_kbps = None
        queue_max_kbps = None
        max_burst_kbit = None

        qos_res = self.find_qos(port_name)
        if qos_res:
            other_config = qos_res['other_config']
            max_bw_in_bits = other_config.get('max-rate')
            if max_bw_in_bits is not None:
                qos_max_kbps = int(max_bw_in_bits) / common_constants.SI_BASE

        queue_res = self.find_queue(port_name, QOS_DEFAULT_QUEUE)
        if queue_res:
            other_config = queue_res['other_config']
            max_bw_in_bits = other_config.get('max-rate')
            if max_bw_in_bits is not None:
                queue_max_kbps = int(max_bw_in_bits) / common_constants.SI_BASE
            max_burst_in_bits = other_config.get('burst')
            if max_burst_in_bits is not None:
                max_burst_kbit = (
                    int(max_burst_in_bits) / common_constants.SI_BASE)

        if qos_max_kbps == queue_max_kbps:
            max_kbps = qos_max_kbps
        else:
            LOG.warning("qos max-rate %(qos_max_kbps)s is not equal to "
                        "queue max-rate %(queue_max_kbps)s",
                        {'qos_max_kbps': qos_max_kbps,
                         'queue_max_kbps': queue_max_kbps})
        return max_kbps, max_burst_kbit

    def get_ingress_bw_limit_for_dpdk_port(self, port_name):
        max_kbps = None
        max_burst_kbit = None
        res = self.find_qos(port_name)
        if res:
            other_config = res['other_config']
            max_bw_in_bytes = other_config.get("cir")
            if max_bw_in_bytes is not None:
                max_kbps = common_utils.bits_to_kilobits(
                    common_utils.bytes_to_bits(int(float(max_bw_in_bytes))),
                    common_constants.SI_BASE)
            max_burst_in_bytes = other_config.get("cbs")
            if max_burst_in_bytes is not None:
                max_burst_kbit = common_utils.bits_to_kilobits(
                    common_utils.bytes_to_bits(int(float(max_burst_in_bytes))),
                    common_constants.SI_BASE)
        return max_kbps, max_burst_kbit

    def delete_ingress_bw_limit_for_port(self, port_name):
        qos = self.find_qos(port_name)
        queue = self.find_queue(port_name, QOS_DEFAULT_QUEUE)
        does_port_exist = self.port_exists(port_name)
        with self.ovsdb.transaction(check_error=True) as txn:
            if does_port_exist:
                txn.add(self.ovsdb.db_clear("Port", port_name, 'qos'))
            if qos:
                txn.add(self.ovsdb.db_destroy('QoS', qos['_uuid']))
            if queue:
                txn.add(self.ovsdb.db_destroy('Queue', queue['_uuid']))

    def set_controller_field(self, field, value):
        attr = [(field, value)]
        controllers = self.db_get_val('Bridge', self.br_name, 'controller')
        controllers = [controllers] if isinstance(
            controllers, uuid.UUID) else controllers
        with self.ovsdb.transaction(check_error=True) as txn:
            for controller_uuid in controllers:
                txn.add(self.ovsdb.db_set(
                    'Controller', controller_uuid, *attr))

    def set_controller_rate_limit(self, controller_rate_limit):
        """Set bridge controller_rate_limit

        :param controller_rate_limit: at least 100
        """
        if controller_rate_limit < CTRL_RATE_LIMIT_MIN:
            LOG.info("rate limit's value must be at least 100")
            controller_rate_limit = CTRL_RATE_LIMIT_MIN
        self.set_controller_field(
            'controller_rate_limit', controller_rate_limit)

    def set_controller_burst_limit(self, controller_burst_limit):
        """Set bridge controller_burst_limit

        :param controller_burst_limit: at least 25
        """
        if controller_burst_limit < CTRL_BURST_LIMIT_MIN:
            LOG.info("burst limit's value must be at least 25")
            controller_burst_limit = CTRL_BURST_LIMIT_MIN
        self.set_controller_field(
            'controller_burst_limit', controller_burst_limit)

    def __enter__(self):
        self.create()
        return self

    def __exit__(self, exc_type, exc_value, exc_tb):
        self.destroy()

class DeferredOVSBridge(object):
    '''Deferred OVSBridge.

    This class wraps add_flow, mod_flow and delete_flows calls to an OVSBridge
    and defers their application until apply_flows call in order to perform
    bulk calls. It wraps also ALLOWED_PASSTHROUGHS calls to avoid mixing
    OVSBridge and DeferredOVSBridge uses.
    This class can be used as a context, in such case apply_flows is called on
    __exit__ except if an exception is raised.
    This class is not thread-safe, that's why for every use a new instance
    must be implemented.
    '''
    ALLOWED_PASSTHROUGHS = 'add_port', 'add_tunnel_port', 'delete_port'

    def __init__(self, br, full_ordered=False,
                 order=('add', 'mod', 'del'), use_bundle=False):
        '''Constructor.

        :param br: wrapped bridge
        :param full_ordered: Optional, disable flow reordering (slower)
        :param order: Optional, define in which order flow are applied
        :param use_bundle: Optional, a bool whether --bundle should be passed
                           to all ofctl commands. Default is set to False.
        '''

        self.br = br
        self.full_ordered = full_ordered
        self.order = order
        if not self.full_ordered:
            self.weights = dict((y, x) for x, y in enumerate(self.order))
        self.action_flow_tuples = []
        self.use_bundle = use_bundle

    def __getattr__(self, name):
        if name in self.ALLOWED_PASSTHROUGHS:
            return getattr(self.br, name)
        raise AttributeError(name)

    def add_flow(self, **kwargs):
        self.action_flow_tuples.append(('add', kwargs))

    def mod_flow(self, **kwargs):
        self.action_flow_tuples.append(('mod', kwargs))

    def delete_flows(self, **kwargs):
        self.action_flow_tuples.append(('del', kwargs))

    def apply_flows(self):
        action_flow_tuples = self.action_flow_tuples
        self.action_flow_tuples = []
        if not action_flow_tuples:
            return

        if not self.full_ordered:
            action_flow_tuples.sort(key=lambda af: self.weights[af[0]])

        grouped = itertools.groupby(action_flow_tuples,
                                    key=operator.itemgetter(0))
        itemgetter_1 = operator.itemgetter(1)
        for action, action_flow_list in grouped:
            flows = list(map(itemgetter_1, action_flow_list))
            self.br.do_action_flows(action, flows, self.use_bundle)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        if exc_type is None:
            self.apply_flows()
        else:
            LOG.exception("OVS flows could not be applied on bridge %s",
                          self.br.br_name)

def _build_flow_expr_str(flow_dict, cmd, strict):
    flow_expr_arr = []
    actions = None

    if cmd == 'add':
        flow_expr_arr.append("hard_timeout=%s" %
                             flow_dict.pop('hard_timeout', '0'))
        flow_expr_arr.append("idle_timeout=%s" %
                             flow_dict.pop('idle_timeout', '0'))
        flow_expr_arr.append("priority=%s" %
                             flow_dict.pop('priority', '1'))
    elif 'priority' in flow_dict:
        if not strict:
            msg = _("Cannot match priority on flow deletion or modification "
                    "without 'strict'")
            raise exceptions.InvalidInput(error_message=msg)

    if cmd != 'del':
        if "actions" not in flow_dict:
            msg = _("Must specify one or more actions on flow addition"
                    " or modification")
            raise exceptions.InvalidInput(error_message=msg)
        actions = "actions=%s" % flow_dict.pop('actions')

    for key, value in flow_dict.items():
        if key == 'proto':
            flow_expr_arr.append(value)
        else:
            flow_expr_arr.append("%s=%s" % (key, str(value)))

    if actions:
        flow_expr_arr.append(actions)

    return ','.join(flow_expr_arr)

def generate_random_cookie():
    # The OpenFlow spec forbids use of -1
    return random.randrange(UINT64_BITMASK)

def check_cookie_mask(cookie):
    cookie = str(cookie)
    if '/' not in cookie:
        return cookie + '/-1'
    else:
        return cookie

def is_a_flow_line(line):
    # this is used to filter out from ovs-ofctl dump-flows the lines that
    # are not flow descriptions but mere indications of the type of openflow
    # message that was used ; e.g.:
    #
    # # ovs-ofctl dump-flows br-int
    # NXST_FLOW reply (xid=0x4):
    #  cookie=0xb7dff131a697c6a5, duration=2411726.809s, table=0, ...
    #  cookie=0xb7dff131a697c6a5, duration=2411726.786s, table=23, ...
    #  cookie=0xb7dff131a697c6a5, duration=2411726.760s, table=24, ...
    #
    return 'NXST' not in line and 'OFPST' not in line


text:
module.exports = async (db, location, limit = 30) => {
  const query = Object.assign({}, location === 'turkey' ? {} : { city: location });
  return db.get('repos').find(query, { limit, sort: { stars: -1 } });
};