text:
import helpers.SIPPHelper;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * Created by blockost on 29/04/17.
 */
public class SIPPHelperTest {

    @Test
    public void testSIPP() {
        assertEquals("Special", SIPPHelper.toString('X', 0));
        assertEquals("SUV", SIPPHelper.toString('F', 1));
    }
}

redpajama_set_name: RedPajamaGithub


text:
package com.intellij.util;

public interface CatchingConsumer<T, E extends Throwable> extends Consumer<T> {
  void consume(E e);
}


text:
<?php

namespace Mu\Modes;

use Mu\Collections\IntervalCollection;

abstract class AbstractMode
{
    /** @var string */
    private $name;

    /** @var IntervalCollection */
    private $intervals;

    final public function __construct(string $name, IntervalCollection $intervals)
    {
        $this->name = $name;
        $this->intervals = $intervals;
    }

    final public function getIntervals(): IntervalCollection
    {
        return $this->intervals;
    }

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }
}


text:
describe("Arrays", function() {

  it("returns a unique set with the added set() function", function() {
    var someArrayWithDuplicates = [1,2,3,4,1,2,1,3,9];
    var originalArrayLength = someArrayWithDuplicates.length;
    var resultArray = someArrayWithDuplicates.set();
    expect(resultArray.length).toBeLessThan(originalArrayLength);
  });

  it("even when the set is made of arrays", function() {
    var someArrayWithDuplicates = [[1,2],[1,2]];
    var originalArrayLength = someArrayWithDuplicates.length;
    var resultArray = someArrayWithDuplicates.set();
    expect(resultArray.length).toBeLessThan(originalArrayLength);
  });

  it("can calculate a closure when given a Dependency Set", function() {
    var someArrayOfAttributes = ['b','c','n'];
    var someDependencySet = new DependencySet();
    var someDependency = new FunctionalDependency(['a','b','c'],['a','n']);
    someDependencySet.add(someDependency);
    expect(someArrayOfAttributes.closure(someDependencySet)).toEqual(['b','c','n']);
  });

});


text:
<?php

declare(strict_types=1);

namespace Tests\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;

use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonbObjectAgg;
use Tests\MartinGeorgiev\Doctrine\Fixtures\Entity\ContainsTexts;

class JsonbObjectAggTest extends TestCase
{
    protected function getStringFunctions(): array
    {
        return [
            'JSONB_OBJECT_AGG' => JsonbObjectAgg::class,
        ];
    }

    protected function getExpectedSqlStatements(): array
    {
        return [
            'SELECT jsonb_object_agg(c0_.text1, c0_.text2) AS sclr_0 FROM ContainsTexts c0_',
        ];
    }

    protected function getDqlStatements(): array
    {
        return [
            \sprintf('SELECT JSONB_OBJECT_AGG(e.text1, e.text2) FROM %s e', ContainsTexts::class),
        ];
    }
}


text:
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using GalaSoft.MvvmLight.Command;
using Microsoft.Win32;
using SkHttpClient.Annotations;
using SkHttpClient.Model;

namespace SkHttpClient.ViewModel
{
    public class DeleteSkViewModel : SkViewModel
    {
        protected override async void PerformNavigate()
        {
            try
            {
                var uri = new Uri(Address);
                var host = uri.Host;
                var resource = uri.AbsolutePath;
                int port = uri.Port;

                var sc = new SkClient(host, port);

                var request = RequestFactory.CreateDelete(resource);
                using (var ms = new MemoryStream())
                {
                    var response = await sc.ExecuteAsync(request, ms, progressIndicator);
                    Status = response.StatusCode + " " + response.ReasonPhase;
                    Headers.Clear();
                    foreach (var header in response.Headers)
                    {
                        Headers.Add(header.Key + ": " + header.Value);
                    }
                    if (response.Viewable)
                        Html = response.PayloadString;
                    else
                    {
                        Html = "";
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
    }
}


text:
ActiveRecord::Schema.define(version: 20140616131835) do

  create_table "newsletters", force: true do |t|
    t.text     "to"
    t.integer  "blog_id"
    t.string   "status"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "subject"
    t.string   "language"
  end

  create_table "objectbases", force: true do |t|
    t.string   "name"
    t.string   "email"
    t.string   "company_name"
    t.string   "company_type"
    t.string   "address"
    t.string   "website"
    t.string   "description"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", force: true do |t|
    t.string   "name"
    t.string   "email"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "password_digest"
    t.string   "remember_token"
    t.boolean  "admin"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["remember_token"], name: "index_users_on_remember_token"

end


text:
"""

"""
from datetime import datetime
from json import loads
import logging
from threading import Thread
from urllib.parse import urljoin
import os

from flask import Flask
import requests
from sqlalchemy import create_engine, schema, event, Column, Integer, String, ForeignKey, DateTime, func, Float
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import sessionmaker, relationship, scoped_session, backref
import tornado.web
import tornado.ioloop
from flask.ext.restless import APIManager as FlaskRestlessManager

from tornado_restless import ApiManager as TornadoRestlessManager

__author__ = 'Martin Martimeo <martin@martimeo.de>'
__date__ = '21.08.13'

class TestBase(object):
    """
        Base class for all tests

        sets up tornado_restless and flask_restless
    """

    config = {
        'dns': 'sqlite:///test.lite',
        'encoding': 'utf-8',
        'tornado': {'port': 7600}
    }

    def setUp(self):

        self.setUpAlchemy()
        self.setUpModels()
        self.setUpFlask()
        self.setUpTornado()
        self.setUpRestless()

    def setUpFlask(self):
        """
            Create Flask application
        """

        app = Flask(__name__)
        app.config['DEBUG'] = True
        app.config['TESTING'] = True
        app.config['SQLALCHEMY_DATABASE_URI'] = self.config['dns']

        self.flask = app

    def setUpTornado(self):
        """
            Create Tornado application
        """
        app = tornado.web.Application([])
        app.listen(self.config['tornado']['port'])
        self.tornado = app

    def setUpRestless(self):
        """
            Create blueprints
        """
        Session = self.alchemy['Session']

        self.api = {'tornado': TornadoRestlessManager(application=self.tornado, session_maker=Session),
                    'flask': FlaskRestlessManager(self.flask, session=Session())}

        for model, methods in self.models.values():
            if methods == "all":
                self.api['tornado'].create_api(model, methods=TornadoRestlessManager.METHODS_ALL)
                self.api['flask'].create_api(model, methods=TornadoRestlessManager.METHODS_ALL)
            else:
                self.api['tornado'].create_api(model)
                self.api['flask'].create_api(model)

        class TornadoThread(Thread):

            def run(self):
                try:
                    tornado.ioloop.IOLoop.instance().start()
                finally:
                    del self._target

        self.threads = {'tornado': TornadoThread(target=self),
                        'flask': self.flask.test_client()}
        self.threads['tornado'].start()

    def curl_tornado(self, url, method='get', assert_for=200, **kwargs):
        url = urljoin('http://localhost:%u' % self.config['tornado']['port'], url)
        r = getattr(requests, method)(url, **kwargs)
        if assert_for == 200:
            r.raise_for_status()
        else:
            assert assert_for == r.status_code
        try:
            try:
                return r.json()
            except ValueError:
                return None
        finally:
            r.close()

    def curl_flask(self, url, method='get', assert_for=200, **kwargs):

        # Map request parameter params to environ param query_string
        if 'params' in kwargs:
            kwargs['query_string'] = kwargs['params']
            del kwargs['params']

        r = getattr(self.threads['flask'], method)(url, **kwargs)
        assert assert_for == r.status_code
        return loads(r.data.decode(self.config['encoding']))

    def setUpAlchemy(self):
        """
            Init SQLAlchemy engine
        """
        engine = create_engine(self.config['dns'])
        metadata = schema.MetaData()
        Session = scoped_session(sessionmaker(bind=engine))
        Base = declarative_base(metadata=metadata)

        self.alchemy = {'Base': Base, 'Session': Session, 'engine': engine}

    def setUpModels(self):
        """
            Create models
        """

        Base = self.alchemy['Base']
        Session = self.alchemy['Session']
        engine = self.alchemy['engine']

        class City(Base):
            __tablename__ = "cities"

            _plz = Column(String(6), primary_key=True)

            name = Column(String, unique=True)

        class Person(Base):
            __tablename__ = 'persons'

            _id = Column(Integer, primary_key=True)
            name = Column(String, unique=True)
            birth = Column(DateTime)

            @hybrid_property
            def age(self):
                return (datetime.now() - self.birth).days / 365.25

            @age.expression
            def age(self):
                return func.now() - self.birth

            def __init__(self, name, age):
                self.name = name
                self.birth = datetime.now().replace(year=datetime.now().year - age)

        class City2Person(Base):
            __tablename__ = 'city2persons'

            _city = Column(ForeignKey(City._plz), primary_key=True)
            city = relationship(City, lazy="joined", backref=backref('persons', lazy="dynamic"))

            _user = Column(ForeignKey(Person._id), primary_key=True)
            user = relationship(Person, lazy="joined", backref=backref('cities', lazy="dynamic"))

            def __init__(self, city, user):
                self._city = city._plz
                self._user = user._id

        class Computer(Base):
            __tablename__ = 'computers'

            _id = Column(Integer, primary_key=True)

            cpu = Column(Float)
            ram = Column(Float)

            _user = Column(ForeignKey(Person._id))
            user = relationship(Person, backref='computers')

        Base.metadata.drop_all(engine)
        Base.metadata.create_all(engine)

        self.models = {'Person': (Person, "all"), 'Computer': (Computer, "all"), 'City': (City, "read")}

        frankfurt = City(_plz=60400 ,name="Frankfurt")
        berlin = City(_plz=10800, name="Berlin")

        self.citites = [frankfurt, berlin]

        anastacia = Person('Anastacia', 44)
        bernd = Person('Bernd', 48)
        claudia = Person('Claudia', 20)
        dennise = Person('Dennise', 14)
        emil = Person('Emil', 81)
        feris = Person('Feris', 10)

        self.persons = {p.name: p for p in [anastacia, bernd, claudia, dennise, emil, feris]}

        a1 = Computer(user=anastacia, cpu=3.2, ram=4)
        a2 = Computer(user=anastacia, cpu=12, ram=4)
        b1 = Computer(user=bernd, cpu=12, ram=8)
        e1 = Computer(user=emil, cpu=1.6, ram=2)
        e2 = Computer(user=emil, cpu=3.4, ram=4)

        self.computers = [a1, a2, b1, e1, e2]

        session = Session()
        session.add_all(self.citites)
        session.add_all(self.persons.values())
        session.add_all(self.computers)
        session.commit()

        session.refresh(bernd)
        session.refresh(anastacia)
        self.assocs = [City2Person(frankfurt, bernd), City2Person(frankfurt, anastacia), City2Person(berlin, bernd)]
        session.add_all(self.assocs)
        session.commit()

    def tearDown(self):

        self.tearDownTornado()
        self.tearDownAlchemy()

    def tearDownAlchemy(self):

        Base = self.alchemy['Base']
        engine = self.alchemy['engine']

        Base.metadata.drop_all(engine)

        del self.alchemy

        os.unlink('test.lite')

    def tearDownTornado(self):

        self.config['tornado']['port'] += 1

        def stop():
            """
                Stop the IOLoop
            """
            tornado.ioloop.IOLoop.instance().stop()

        tornado.ioloop.IOLoop.instance().add_callback(stop)
        self.threads['tornado'].join()

    def subsetOf(self, a, b):
        """
            Test wether a is an subset of b (or b an superset of a)
        """

        if type(a) != type(b):
            logging.error("Type not equal of a,b")
            return False

        if isinstance(a, dict) or hasattr(a, "items"):
            for (key, value) in a.items():
                if not self.subsetOf(value, b[key]):
                    return False
            else:
                return True

        if isinstance(a, list) or hasattr(a, "__iter__"):
            for element in a:
                if element not in b:
                    return False
            else:
                return True

        return a == b

if __name__ == "__main__":

    logging.basicConfig(level=logging.DEBUG)
    base = TestBase()
    base.setUp()
    try:
        base.threads["tornado"].join()
    except KeyboardInterrupt:
        base.tearDown()


text:
<?php
class User extends CI_Controller {

        public function __construct()
        {
                parent::__construct();
                $this->load->model('user_model');
                $this->load->helper('url_helper');
        }

        public function login() {

        }

        public function check_username($username){
            return $this->user_model->check_username;
        }

        public function check_email($email){
            return $this->user_model->check_email;
        }

}


text:
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _jqmin = require("jqmin");

var _jqmin2 = _interopRequireDefault(_jqmin);

var _AX6UICore2 = require("./AX6UICore");

var _AX6UICore3 = _interopRequireDefault(_AX6UICore2);

var _AX6Util = require("./AX6Util");

var _AX6Util2 = _interopRequireDefault(_AX6Util);

var _AX6Info = require("./AX6Info");

var _AX6Info2 = _interopRequireDefault(_AX6Info);

var _AX6UIFormatter_formatter = require("./AX6UIFormatter/AX6UIFormatter_formatter");

var _AX6UIFormatter_formatter2 = _interopRequireDefault(_AX6UIFormatter_formatter);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

/* ~~~~~~~~~~~~~~~~~~ end of import  ~~~~~~~~~~~~~~~~~~~~ */

var formatter = {};

var setSelectionRange = function setSelectionRange(input, pos) {
  if (typeof pos == "undefined") {
    pos = input.value.length;
  }
  if (input.setSelectionRange) {
    input.focus();
    input.setSelectionRange(pos, pos);
  } else if (input.createTextRange) {
    var range = input.createTextRange();
    range.collapse(true);
    range.moveEnd('character', pos);
    range.moveStart('character', pos);
    range.select();
  } else if (input.selectionStart) {
    input.focus();
    input.selectionStart = pos;
    input.selectionEnd = pos;
  }
};
var formatterEvent = {
  'focus': function focus(opts, optIdx, e) {
    if (!opts.$input.data("__originValue__")) opts.$input.data("__originValue__", opts.$input.val());
  },
  /* 키 다운 이벤트에서 입력할 수 없는 키 입력을 방어 */
  'keydown': function keydown(opts, optIdx, e) {
    var isStop = false;
    if (!opts.enterableKeyCodes) {} else if (e.which && opts.enterableKeyCodes[e.which]) {} else if (!e.metaKey && !e.ctrlKey && !e.shiftKey) {
      //console.log(e.which, opts.enterableKeyCodes);
      isStop = true;
    }
    if (isStop) _AX6Util2.default.stopEvent(e);
  },
  /* 키 업 이벤트에서 패턴을 적용 */
  'keyup': function keyup(opts, optIdx, e) {
    var elem = opts.$input.get(0),
        elemFocusPosition = void 0,
        beforeValue = void 0,
        newValue = void 0,
        selection = void 0,
        selectionLength = void 0;

    if ('selectionStart' in elem) {
      // Standard-compliant browsers
      elemFocusPosition = elem.selectionStart;
    } else if (document.selection) {
      // IE
      //elem.focus();
      selection = document.selection.createRange();
      selectionLength = document.selection.createRange().text.length;
      selection.moveStart('character', -elem.value.length);
      elemFocusPosition = selection.text.length - selectionLength;
    }

    beforeValue = elem.value;
    if (opts.pattern in this.customFormatter) {
      newValue = this.customFormatter[opts.pattern].getPatternValue.call(this, opts, optIdx, e, elem.value);
    } else if (opts.pattern in _AX6UIFormatter_formatter2.default) {
      newValue = _AX6UIFormatter_formatter2.default[opts.pattern].getPatternValue.call(this, opts, optIdx, e, elem.value);
    } else {
      newValue = beforeValue;
    }

    if (newValue != beforeValue) {
      opts.$input.val(newValue).trigger("change");
      setSelectionRange(elem, elemFocusPosition + newValue.length - beforeValue.length);
    }
  },
  'blur': function blur(opts, optIdx, e, _force) {
    var elem = opts.$input.get(0),
        beforeValue = void 0,
        newValue = void 0;

    opts.$input.removeData("__originValue__");

    beforeValue = elem.value;
    if (opts.pattern in this.customFormatter) {
      newValue = this.customFormatter[opts.pattern].getPatternValue.call(this, opts, optIdx, e, elem.value, 'blur');
    } else if (opts.pattern in _AX6UIFormatter_formatter2.default) {
      newValue = _AX6UIFormatter_formatter2.default[opts.pattern].getPatternValue.call(this, opts, optIdx, e, elem.value, 'blur');
    } else {
      newValue = beforeValue;
    }

    if (_force) {
      opts.$input.val(newValue);
    } else {
      if (newValue != beforeValue) {
        opts.$input.val(newValue).trigger("change");
      }
    }
  }
};
var bindFormatterTarget = function bindFormatterTarget(opts, optIdx) {
  if (!opts.pattern) {
    if (opts.$target.get(0).tagName == "INPUT") {
      opts.pattern = opts.$target.attr('data-ax6formatter');
    } else {
      opts.pattern = opts.$target.find('input[type="text"]').attr('data-ax6formatter');
    }
    if (!opts.pattern) {
      console.log(_AX6Info2.default.getError("ax6formatter", "501", "bind"));
      console.log(opts.target);
      return this;
    }
  }

  var re = /[^\(^\))]+/gi,
      matched = opts.pattern.match(re);

  opts.pattern = matched[0];
  opts.patternArgument = matched[1] || "";

  // 함수타입
  if (opts.pattern in this.customFormatter) {
    opts.enterableKeyCodes = this.customFormatter[opts.pattern].getEnterableKeyCodes.call(this, opts, optIdx);
  } else if (opts.pattern in _AX6UIFormatter_formatter2.default) {
    opts.enterableKeyCodes = _AX6UIFormatter_formatter2.default[opts.pattern].getEnterableKeyCodes.call(this, opts, optIdx);
  }

  opts.$input.off('focus.ax6formatter').on('focus.ax6formatter', formatterEvent.focus.bind(this, this.queue[optIdx], optIdx)).off('keydown.ax6formatter').on('keydown.ax6formatter', formatterEvent.keydown.bind(this, this.queue[optIdx], optIdx)).off('keyup.ax6formatter').on('keyup.ax6formatter', formatterEvent.keyup.bind(this, this.queue[optIdx], optIdx)).off('blur.ax6formatter').on('blur.ax6formatter', formatterEvent.blur.bind(this, this.queue[optIdx], optIdx));

  formatterEvent.blur.call(this, this.queue[optIdx], optIdx);

  return this;
};
var unbindFormatterTarget = function unbindFormatterTarget(opts, optIdx) {
  opts.$input.off('focus.ax6formatter').off('keydown.ax6formatter').off('keyup.ax6formatter').off('blur.ax6formatter');

  return this;
};
var getQueIdx = function getQueIdx(boundID) {
  if (!_AX6Util2.default.isString(boundID)) {
    boundID = (0, _jqmin2.default)(boundID).data("data-formatter");
  }
  /*
   if (!U.isString(boundID)) {
   console.log(info.getError("ax6formatter", "402", "getQueIdx"));
   return;
   }
   */
  return _AX6Util2.default.search(this.queue, function () {
    return this.id == boundID;
  });
};
/* ~~~~~~~~~~~~~~~~~~ end of private  ~~~~~~~~~~~~~~~~~~~~ */

/**
 * @class
 */

var AX6UIFormatter = function (_AX6UICore) {
  _inherits(AX6UIFormatter, _AX6UICore);

  /**
   * @constructor
   * @param config
   * @param {Object} [config.formatter]
   * @example
   * ```js
   * var formatter = new Formatter();
   *
   * // Extend formatter
   * var myFormatter = new Formatter({
     *  formatter: {
     *      "mystyle": {
     *          getEnterableKeyCodes: function (_opts) {
     *              var enterableKeyCodes = {
     *                  '189': '-' // eventKeyCode
     *              };
     *              return jQuery.extend(enterableKeyCodes, {});
     *          }
     *          getPatternValue: function (_opts, optIdx, e, val, eType) {
     *              val = val.replace(/\D/g, "");
     *              var regExpPattern = /^([0-9]{2})\-?([0-9]{2})?\-?([0-9]{2})?\-?([0-9]{2})?/;
     *              return val.replace(regExpPattern, function (a, b) {
     *                  var nval = [arguments[1]];
     *                  if (arguments[2]) nval.push(arguments[2]);
     *                  if (arguments[3]) nval.push(arguments[3]);
     *                  if (arguments[4]) nval.push(arguments[4]);
     *                  return nval.join("-");
     *              });
     *          }
     *      }
     *  }
     * });
   * ```
   */
  function AX6UIFormatter(config) {
    _classCallCheck(this, AX6UIFormatter);

    /**
     * @member {JSON}
     * @param config
     * @param [config.animateTime=250]
     */
    var _this = _possibleConstructorReturn(this, (AX6UIFormatter.__proto__ || Object.getPrototypeOf(AX6UIFormatter)).call(this));

    _this.config = {
      animateTime: 250
    };
    _jqmin2.default.extend(true, _this.config, config);

    // 멤버 변수 초기화
    /**
     * @member
     * @type {Array}
     */
    _this.queue = [];
    _this.openTimer = null;
    _this.closeTimer = null;

    _this.init();
    return _this;
  }

  /**
   * @method
   */

  _createClass(AX6UIFormatter, [{
    key: "init",
    value: function init() {

      // init 호출 여부
      this.initOnce();
    }

    /**
     * @method
     * @return {AX6UIFormatter}
     */

  }, {
    key: "initOnce",
    value: function initOnce() {
      if (this.initialized) return this;
      this.initialized = true;
    }

    /**
     * @method
     * @param {Object} opts
     * @param {Element} opts.target
     * @return {AX6UIFormatter}
     */

  }, {
    key: "bind",
    value: function bind(opts) {
      var formatterConfig = {},
          optIdx = void 0;

      // 사용자 포메터 체크
      this.customFormatter = AX6UIFormatter.getFormatter();

      _jqmin2.default.extend(true, formatterConfig, this.config);
      if (opts) _jqmin2.default.extend(true, formatterConfig, opts);
      opts = formatterConfig;

      if (!opts.target) {
        console.log(_AX6Info2.default.getError("ax6formatter", "401", "bind"));
        return this;
      }
      opts.$target = (0, _jqmin2.default)(opts.target);
      if (!opts.$target.get(0)) {
        console.log(_AX6Info2.default.getError("ax6formatter", "401", "can't found target element"));
        return this;
      }

      if (opts.$target.get(0).tagName == "INPUT") {
        opts.$input = opts.$target;
      } else {
        opts.$input = opts.$target.find('input[type="text"]');
        if (opts.$input.length > 1) {
          opts.$input.each(function () {
            opts.target = this;
            self.bind(opts);
          });
          return this;
        }
      }

      opts.$input = opts.$target.get(0).tagName == "INPUT" ? opts.$target : opts.$target.find('input[type="text"]');

      if (!opts.id) opts.id = opts.$input.data("ax6-formatter");

      if (!opts.id) {
        opts.id = 'ax6-formatter-' + _AX6UICore3.default.getInstanceId();
        opts.$input.data("ax6-formatter", opts.id);
      }
      optIdx = _AX6Util2.default.search(this.queue, function () {
        return this.id == opts.id;
      });

      if (optIdx === -1) {
        this.queue.push(opts);
        bindFormatterTarget.call(this, this.queue[this.queue.length - 1], this.queue.length - 1);
      } else {
        this.queue[optIdx] = opts;
        bindFormatterTarget.call(this, this.queue[optIdx], optIdx);
      }

      return this;
    }

    /**
     * @method
     * @return {AX6UIFormatter}
     */

  }, {
    key: "formatting",
    value: function formatting() {
      var queIdx = _AX6Util2.default.isNumber(boundID) ? boundID : getQueIdx.call(this, boundID);
      if (queIdx === -1) {
        var i = this.queue.length;
        while (i--) {
          formatterEvent.blur.call(this, this.queue[i], i, null, true);
        }
      } else {
        formatterEvent.blur.call(this, this.queue[queIdx], queIdx, null, true);
      }
      return this;
    }

    /**
     * @method
     * @param opts
     * @return {AX6UIFormatter}
     */

  }, {
    key: "unbind",
    value: function unbind(opts) {
      var self = this;
      var optIdx = void 0;

      if (!opts.target) {
        console.log(_AX6Info2.default.getError("ax6formatter", "401", "unbind"));
        return this;
      }
      opts.$target = (0, _jqmin2.default)(opts.target);

      if (opts.$target.get(0).tagName == "INPUT") {
        opts.$input = opts.$target;
      } else {
        opts.$input = opts.$target.find('input[type="text"]');
        if (opts.$input.length > 1) {
          opts.$input.each(function () {
            opts.target = this;
            self.unbind(opts);
          });
          return this;
        }
      }

      opts.$input = opts.$target.get(0).tagName == "INPUT" ? opts.$target : opts.$target.find('input[type="text"]');
      opts.id = opts.$input.data("ax6-formatter");

      if (opts.id) {
        optIdx = _AX6Util2.default.search(this.queue, function () {
          return this.id == opts.id;
        });

        unbindFormatterTarget.call(this, this.queue[optIdx]);
        this.queue.splice(optIdx, 1);
      }

      return this;
    }

    /**
     * @static
     * @param _formatter
     */

  }], [{
    key: "setFormatter",
    value: function setFormatter(_formatter) {
      return formatter = Object.assign(formatter, _formatter);
    }

    /**
     * @static
     * @return {{}}
     */

  }, {
    key: "getFormatter",
    value: function getFormatter() {
      return formatter || {};
    }

    /**
     * @static
     * @return {{}}
     */

  }, {
    key: "getCtrlKeys",
    value: function getCtrlKeys() {
      return _AX6UIFormatter_formatter2.default.ctrlKeys;
    }

    /**
     * @static
     * @return {{}}
     */

  }, {
    key: "getNumKeys",
    value: function getNumKeys() {
      return _AX6UIFormatter_formatter2.default.numKeys;
    }
  }]);

  return AX6UIFormatter;
}(_AX6UICore3.default);

exports.default = AX6UIFormatter;


text:
//go:build !windows
// +build !windows

package transformation

import (
	"bytes"
	"context"
	"crypto/aes"
	"fmt"
	"strings"
	"testing"
	"time"

	"github.com/gogo/protobuf/proto"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/apimachinery/pkg/util/uuid"
	"k8s.io/apiserver/pkg/features"
	"k8s.io/apiserver/pkg/storage/value"
	aestransformer "k8s.io/apiserver/pkg/storage/value/encrypt/aes"
	kmstypes "k8s.io/apiserver/pkg/storage/value/encrypt/envelope/kmsv2/v2alpha1"
	kmsv2mock "k8s.io/apiserver/pkg/storage/value/encrypt/envelope/testing/v2alpha1"
	utilfeature "k8s.io/apiserver/pkg/util/feature"
	featuregatetesting "k8s.io/component-base/featuregate/testing"
	kmsv2api "k8s.io/kms/apis/v2alpha1"
)

type envelopekmsv2 struct {
	providerName string
	rawEnvelope  []byte
	plainTextDEK []byte
}

func (r envelopekmsv2) prefix() string {
	return fmt.Sprintf("k8s:enc:kms:v2:%s:", r.providerName)
}

func (r envelopekmsv2) prefixLen() int {
	return len(r.prefix())
}

func (r envelopekmsv2) cipherTextDEK() ([]byte, error) {
	o := &kmstypes.EncryptedObject{}
	if err := proto.Unmarshal(r.rawEnvelope[r.startOfPayload(r.providerName):], o); err != nil {
		return nil, err
	}
	return o.EncryptedDEK, nil
}

func (r envelopekmsv2) startOfPayload(_ string) int {
	return r.prefixLen()
}

func (r envelopekmsv2) cipherTextPayload() ([]byte, error) {
	o := &kmstypes.EncryptedObject{}
	if err := proto.Unmarshal(r.rawEnvelope[r.startOfPayload(r.providerName):], o); err != nil {
		return nil, err
	}
	return o.EncryptedData, nil
}

func (r envelopekmsv2) plainTextPayload(secretETCDPath string) ([]byte, error) {
	block, err := aes.NewCipher(r.plainTextDEK)
	if err != nil {
		return nil, fmt.Errorf("failed to initialize AES Cipher: %v", err)
	}
	ctx := context.Background()
	dataCtx := value.DefaultContext([]byte(secretETCDPath))
	aesgcmTransformer := aestransformer.NewGCMTransformer(block)
	data, err := r.cipherTextPayload()
	if err != nil {
		return nil, fmt.Errorf("failed to get cipher text payload: %v", err)
	}
	plainSecret, _, err := aesgcmTransformer.TransformFromStorage(ctx, data, dataCtx)
	if err != nil {
		return nil, fmt.Errorf("failed to transform from storage via AESGCM, err: %w", err)
	}

	return plainSecret, nil
}

// TestKMSv2Provider is an integration test between KubeAPI, ETCD and KMSv2 Plugin
// Concretely, this test verifies the following integration contracts:
// 1. Raw records in ETCD that were processed by KMSv2 Provider should be prefixed with k8s:enc:kms:v2:<plugin name>:
// 2. Data Encryption Key (DEK) should be generated by envelopeTransformer and passed to KMS gRPC Plugin
// 3. KMS gRPC Plugin should encrypt the DEK with a Key Encryption Key (KEK) and pass it back to envelopeTransformer
// 4. The cipherTextPayload (ex. Secret) should be encrypted via AES GCM transform
// 5. kmstypes.EncryptedObject structure should be serialized and deposited in ETCD
func TestKMSv2Provider(t *testing.T) {
	defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.KMSv2, true)()

	encryptionConfig := `
kind: EncryptionConfiguration
apiVersion: apiserver.config.k8s.io/v1
resources:
  - resources:
    - secrets
    providers:
    - kms:
       apiVersion: v2
       name: kms-provider
       cachesize: 1000
       endpoint: unix:///@kms-provider.sock
`

	providerName := "kms-provider"
	pluginMock, err := kmsv2mock.NewBase64Plugin("@kms-provider.sock")
	if err != nil {
		t.Fatalf("failed to create mock of KMSv2 Plugin: %v", err)
	}

	go pluginMock.Start()
	if err := kmsv2mock.WaitForBase64PluginToBeUp(pluginMock); err != nil {
		t.Fatalf("Failed start plugin, err: %v", err)
	}
	defer pluginMock.CleanUp()

	test, err := newTransformTest(t, encryptionConfig)
	if err != nil {
		t.Fatalf("failed to start KUBE API Server with encryptionConfig\n %s, error: %v", encryptionConfig, err)
	}
	defer test.cleanUp()

	test.secret, err = test.createSecret(testSecret, testNamespace)
	if err != nil {
		t.Fatalf("Failed to create test secret, error: %v", err)
	}

	// Since Data Encryption Key (DEK) is randomly generated (per encryption operation), we need to ask KMS Mock for it.
	plainTextDEK := pluginMock.LastEncryptRequest()

	secretETCDPath := test.getETCDPath()
	rawEnvelope, err := test.getRawSecretFromETCD()
	if err != nil {
		t.Fatalf("failed to read %s from etcd: %v", secretETCDPath, err)
	}

	envelopeData := envelopekmsv2{
		providerName: providerName,
		rawEnvelope:  rawEnvelope,
		plainTextDEK: plainTextDEK,
	}

	wantPrefix := string(envelopeData.prefix())
	if !bytes.HasPrefix(rawEnvelope, []byte(wantPrefix)) {
		t.Fatalf("expected secret to be prefixed with %s, but got %s", wantPrefix, rawEnvelope)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	ciphertext, err := envelopeData.cipherTextDEK()
	if err != nil {
		t.Fatalf("failed to get ciphertext DEK from KMSv2 Plugin: %v", err)
	}
	decryptResponse, err := pluginMock.Decrypt(ctx, &kmsv2api.DecryptRequest{Uid: string(types.UID(uuid.NewUUID())), Ciphertext: ciphertext})
	if err != nil {
		t.Fatalf("failed to decrypt DEK, %v", err)
	}
	dekPlainAsWouldBeSeenByETCD := decryptResponse.Plaintext

	if !bytes.Equal(plainTextDEK, dekPlainAsWouldBeSeenByETCD) {
		t.Fatalf("expected plainTextDEK %v to be passed to KMS Plugin, but got %s",
			plainTextDEK, dekPlainAsWouldBeSeenByETCD)
	}

	plainSecret, err := envelopeData.plainTextPayload(secretETCDPath)
	if err != nil {
		t.Fatalf("failed to transform from storage via AESGCM, err: %v", err)
	}

	if !strings.Contains(string(plainSecret), secretVal) {
		t.Fatalf("expected %q after decryption, but got %q", secretVal, string(plainSecret))
	}

	secretClient := test.restClient.CoreV1().Secrets(testNamespace)
	// Secrets should be un-enveloped on direct reads from Kube API Server.
	s, err := secretClient.Get(ctx, testSecret, metav1.GetOptions{})
	if err != nil {
		t.Fatalf("failed to get Secret from %s, err: %v", testNamespace, err)
	}
	if secretVal != string(s.Data[secretKey]) {
		t.Fatalf("expected %s from KubeAPI, but got %s", secretVal, string(s.Data[secretKey]))
	}
}

func TestKMSv2Healthz(t *testing.T) {
	defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.KMSv2, true)()

	encryptionConfig := `
kind: EncryptionConfiguration
apiVersion: apiserver.config.k8s.io/v1
resources:
  - resources:
    - secrets
    providers:
    - kms:
       apiVersion: v2
       name: provider-1
       endpoint: unix:///@kms-provider-1.sock
    - kms:
       apiVersion: v2
       name: provider-2
       endpoint: unix:///@kms-provider-2.sock
`

	pluginMock1, err := kmsv2mock.NewBase64Plugin("@kms-provider-1.sock")
	if err != nil {
		t.Fatalf("failed to create mock of KMS Plugin #1: %v", err)
	}

	if err := pluginMock1.Start(); err != nil {
		t.Fatalf("Failed to start kms-plugin, err: %v", err)
	}
	defer pluginMock1.CleanUp()
	if err := kmsv2mock.WaitForBase64PluginToBeUp(pluginMock1); err != nil {
		t.Fatalf("Failed to start plugin #1, err: %v", err)
	}

	pluginMock2, err := kmsv2mock.NewBase64Plugin("@kms-provider-2.sock")
	if err != nil {
		t.Fatalf("Failed to create mock of KMS Plugin #2: err: %v", err)
	}
	if err := pluginMock2.Start(); err != nil {
		t.Fatalf("Failed to start kms-plugin, err: %v", err)
	}
	defer pluginMock2.CleanUp()
	if err := kmsv2mock.WaitForBase64PluginToBeUp(pluginMock2); err != nil {
		t.Fatalf("Failed to start KMS Plugin #2: err: %v", err)
	}

	test, err := newTransformTest(t, encryptionConfig)
	if err != nil {
		t.Fatalf("Failed to start kube-apiserver, error: %v", err)
	}
	defer test.cleanUp()

	// Name of the healthz check is calculated based on a constant "kms-provider-" + position of the
	// provider in the config.

	// Stage 1 - Since all kms-plugins are guaranteed to be up, healthz checks for:
	// healthz/kms-provider-0 and /healthz/kms-provider-1 should be OK.
	mustBeHealthy(t, "kms-provider-0", test.kubeAPIServer.ClientConfig)
	mustBeHealthy(t, "kms-provider-1", test.kubeAPIServer.ClientConfig)

	// Stage 2 - kms-plugin for provider-1 is down. Therefore, expect the health check for provider-1
	// to fail, but provider-2 should still be OK
	pluginMock1.EnterFailedState()
	mustBeUnHealthy(t, "kms-provider-0", test.kubeAPIServer.ClientConfig)
	mustBeHealthy(t, "kms-provider-1", test.kubeAPIServer.ClientConfig)
	pluginMock1.ExitFailedState()

	// Stage 3 - kms-plugin for provider-1 is now up. Therefore, expect the health check for provider-1
	// to succeed now, but provider-2 is now down.
	// Need to sleep since health check chases responses for 3 seconds.
	pluginMock2.EnterFailedState()
	mustBeHealthy(t, "kms-provider-0", test.kubeAPIServer.ClientConfig)
	mustBeUnHealthy(t, "kms-provider-1", test.kubeAPIServer.ClientConfig)
}


text:
CREATE TABLE Customer (
    id     integer,
    name    varchar(40),
    PRIMARY KEY(id)
);

insert into Customer(id, name) values (1, 'kimi');
insert into Customer(id, name) values (2, 'think');


text:
<?php

class theme_tiny_core_renderer extends core_renderer {

        /**
         * The standard tags (typically performance information and validation links,
         * if we are in developer debug mode) that should be output in the footer area
         * of the page. Designed to be called in theme layout.php files.
         *
         * @return string HTML fragment.
         */
        public function standard_footer_html() {
            global $CFG, $SCRIPT;

            // This function is normally called from a layout.php file in {@link core_renderer::header()}
            // but some of the content won't be known until later, so we return a placeholder
            // for now. This will be replaced with the real content in {@link core_renderer::footer()}.
            $output = $this->unique_performance_info_token;
            if ($this->page->devicetypeinuse == 'legacy') {
                // The legacy theme is in use print the notification
                $output .= html_writer::tag('div', get_string('legacythemeinuse'), array('class'=>'legacythemeinuse'));
            }

            // Get links to switch device types (only shown for users not on a default device)
            $output .= $this->theme_switch_links();

            if (!empty($CFG->debugpageinfo)) {
                $output .= '
  This page is: ' . $this->page->debug_summary() . '
';
            }
            if (debugging(null, DEBUG_DEVELOPER) and has_capability('moodle/site:config', context_system::instance())) {  // Only in developer mode
                // Add link to profiling report if necessary
                if (function_exists('profiling_is_running') && profiling_is_running()) {
                    $txt = get_string('profiledscript', 'admin');
                    $title = get_string('profiledscriptview', 'admin');
                    $url = $CFG->wwwroot . '/admin/tool/profiling/index.php?script=' . urlencode($SCRIPT);
                    $link= '' . $txt . '';
                    $output .= '
' . $link . '
';
                }
                $output .= '
wwwroot.'/'.$CFG->admin.'/purgecaches.php?confirm=1&sesskey='.sesskey().'">  '.get_string('purgecaches', 'admin').'
';
            }
            if (!empty($CFG->debugvalidators)) {
                // NOTE: this is not a nice hack, $PAGE->url is not always accurate and $FULLME neither, it is not a bug if it fails. --skodak
                $output .= '

  Validate HTML

  Section 508 Check

  WCAG 1 (2,3) Check

';
            }
            if (!empty($CFG->additionalhtmlfooter)) {
                $output .= "\n".$CFG->additionalhtmlfooter;
            }
            return $output;
        }

        /**
         * Return the 'back' link that normally appears in the footer.
         *
         * @return string HTML fragment.
         */
        public function home_link() {
            global $CFG, $SITE;

            if ($this->page->pagetype == 'site-index') {
                // Special case for site home page - please do not remove
                return '
' .
                       '' .
                       'pix_url('moodlelogo') . '" alt="moodlelogo" />
';

            } else if (!empty($CFG->target_release) && $CFG->target_release != $CFG->release) {
                // Special case for during install/upgrade.
                return '
'.
                       '' .
                       'pix_url('moodlelogo') . '" alt="moodlelogo" />
';

            } else if ($this->page->course->id == $SITE->id || strpos($this->page->pagetype, 'course-view') === 0) {
                return '
wwwroot . '/">  ' .
                        get_string('home') . '
';

            } else {
                return '
wwwroot . '/course/view.php?id=' . $this->page->course->id . '">  ' .
                        format_string($this->page->course->shortname, true, array('context' => $this->page->context)) . '
';
            }
        }

        /**
         * Return the standard string that says whether you are logged in (and switched
         * roles/logged in as another user).
         * @param bool $withlinks if false, then don't include any links in the HTML produced.
         * If not set, the default is the nologinlinks option from the theme config.php file,
         * and if that is not set, then links are included.
         * @return string HTML fragment.
         */
        public function login_info($withlinks = null) {
            global $USER, $CFG, $DB, $SESSION;

            if (during_initial_install()) {
                return '';
            }

            if (is_null($withlinks)) {
                $withlinks = empty($this->page->layout_options['nologinlinks']);
            }

            $loginpage = ((string)$this->page->url === get_login_url());
            $course = $this->page->course;
            if (session_is_loggedinas()) {
                $realuser = session_get_realuser();
                $fullname = fullname($realuser, true);
                if ($withlinks) {
                    $realuserinfo = " [wwwroot/course/loginas.php?id=$course->id&sesskey=".sesskey()."\">$fullname] ";
                } else {
                    $realuserinfo = " [$fullname] ";
                }
            } else {
                $realuserinfo = '';
            }

            $loginurl = get_login_url();

            if (empty($course->id)) {
                // $course->id is not defined during installation
                return '';
            } else if (isloggedin()) {
                $context = context_course::instance($course->id);

                $fullname = fullname($USER, true);
                // Since Moodle 2.0 this link always goes to the public profile page (not the course profile page)
                if ($withlinks) {
                    $username = "wwwroot/user/profile.php?id=$USER->id\">$fullname";
                } else {
                    $username = $fullname;
                }
                if (is_mnet_remote_user($USER) and $idprovider = $DB->get_record('mnet_host', array('id'=>$USER->mnethostid))) {
                    if ($withlinks) {
                        $username .= " from wwwroot}\">{$idprovider->name}";
                    } else {
                        $username .= " from {$idprovider->name}";
                    }
                }
                if (isguestuser()) {
                    $loggedinas = $realuserinfo.get_string('loggedinasguest');
                    if (!$loginpage && $withlinks) {
                        $loggedinas .= "   ".get_string('login').'';
                    }
                } else if (is_role_switched($course->id)) { // Has switched roles
                    $rolename = '';
                    if ($role = $DB->get_record('role', array('id'=>$USER->access['rsw'][$context->path]))) {
                        $rolename = ': '.format_string($role->name);
                    }
                    $loggedinas = get_string('loggedinas', 'moodle', $username).$rolename;
                    if ($withlinks) {
                        $loggedinas .= " (wwwroot/course/view.php?id=$course->id&switchrole=0&sesskey=".sesskey()."\">".get_string('switchrolereturn').')';
                    }
                } else {
                    $loggedinas = $realuserinfo.get_string('loggedinas', 'moodle', $username);
                    if ($withlinks) {
                        $loggedinas .= '  wwwroot/login/logout.php?sesskey='.sesskey().'">'.get_string('logout').'  ';
                    }
                }
            } else {
                $loggedinas = get_string('loggedinnot', 'moodle');
                if (!$loginpage && $withlinks) {
                    $loggedinas .= "   ".get_string('login').'';
                }
            }

            $loggedinas = '
'.$loggedinas.'
';

            if (isset($SESSION->justloggedin)) {
                unset($SESSION->justloggedin);
                if (!empty($CFG->displayloginfailures)) {
                    if (!isguestuser()) {
                        if ($count = count_login_failures($CFG->displayloginfailures, $USER->username, $USER->lastlogin)) {
                            $loggedinas .= '
';
                            if (empty($count->accounts)) {
                                $loggedinas .= get_string('failedloginattempts', '', $count);
                            } else {
                                $loggedinas .= get_string('failedloginattemptsall', '', $count);
                            }
                            if (file_exists("$CFG->dirroot/report/log/index.php") and has_capability('report/log:view', context_system::instance())) {
                                $loggedinas .= ' wwwroot.'/report/log/index.php'.
                                                     '?chooselog=1&id=1&modid=site_errors">'.get_string('logs').'';
                            }
                            $loggedinas .= '
';
                        }
                    }
                }
            }

            return $loggedinas;
    }

         /**
         * Redirects the user by any means possible given the current state
         *
         * This function should not be called directly, it should always be called using
         * the redirect function in lib/weblib.php
         *
         * The redirect function should really only be called before page output has started
         * however it will allow itself to be called during the state STATE_IN_BODY
         *
         * @param string $encodedurl The URL to send to encoded if required
         * @param string $message The message to display to the user if any
         * @param int $delay The delay before redirecting a user, if $message has been
         *         set this is a requirement and defaults to 3, set to 0 no delay
         * @param boolean $debugdisableredirect this redirect has been disabled for
         *         debugging purposes. Display a message that explains, and don't
         *         trigger the redirect.
         * @return string The HTML to display to the user before dying, may contain
         *         meta refresh, javascript refresh, and may have set header redirects
         */
        public function redirect_message($encodedurl, $message, $delay, $debugdisableredirect) {
            global $CFG;
            $url = str_replace('&', '&', $encodedurl);

            switch ($this->page->state) {
                case moodle_page::STATE_BEFORE_HEADER :
                    // No output yet it is safe to delivery the full arsenal of redirect methods
                    if (!$debugdisableredirect) {
                        // Don't use exactly the same time here, it can cause problems when both redirects fire at the same time.
                        $this->metarefreshtag = '<meta http-equiv="refresh" content="'. $delay .'; url='. $encodedurl .'" />'."\n";
                        $this->page->requires->js_function_call('document.location.replace', array($url), false, ($delay + 3));
                    }
                    $output = $this->header();
                    break;
                case moodle_page::STATE_PRINTING_HEADER :
                    // We should hopefully never get here
                    throw new coding_exception('You cannot redirect while printing the page header');
                    break;
                case moodle_page::STATE_IN_BODY :
                    // We really shouldn't be here but we can deal with this
                    debugging("You should really redirect before you start page output");
                    if (!$debugdisableredirect) {
                        $this->page->requires->js_function_call('document.location.replace', array($url), false, $delay);
                    }
                    $output = $this->opencontainers->pop_all_but_last();
                    break;
                case moodle_page::STATE_DONE :
                    // Too late to be calling redirect now
                    throw new coding_exception('You cannot redirect after the entire page has been generated');
                    break;
            }
            $output .= $this->notification($message, 'redirectmessage');
            $output .= '
'. get_string('continue') .'
';
            if ($debugdisableredirect) {
                $output .= '
Error output, so disabling automatic redirect.
';
            }
            $output .= $this->footer();
            return $output;
    }

        /**
         * Returns HTML to display a "Turn editing on/off" button in a form.
         *
         * @param moodle_url $url The URL + params to send through when clicking the button
         * @return string HTML the button
         */
        public function edit_button(moodle_url $url) {

            $url->param('sesskey', sesskey());
            if ($this->page->user_is_editing()) {
                $url->param('edit', 'off');
                $text = '';
            } else {
                $url->param('edit', 'on');
                $text = '';
            }

            return ($text);
    }

    /*
    * The standard navigation bar (breadcrumb)
    * shows the course category
    * For this theme the course category has been removed
    */
        public function navbar() {
            $items = $this->page->navbar->get_items();

            $htmlblocks = array();
            // Iterate the navarray and display each node
            $itemcount = count($items);
            $separator = get_separator();
            for ($i=0;$i < $itemcount;$i++) {
                $item = $items[$i];
                if ($item->type == "0" || $item->type == "30") {
                    continue;
                }
                $item->hideicon = true;
                if ($i===0) {
                    $content = html_writer::tag('li', $this->render($item));
                } else {
                    $content = html_writer::tag('li', $separator.$this->render($item));
                }
                $htmlblocks[] = $content;
            }
            //accessibility: heading for navbar list (MDL-20446)
            $navbarcontent = html_writer::tag('span', get_string('pagepath'), array('class'=>'accesshide'));
            $navbarcontent .= html_writer::tag('ul', join('', $htmlblocks));
            // XHTML
            return $navbarcontent;
        }

       /*
        * this renders the bootstrap top menu
        */

        protected function render_custom_menu(custom_menu $menu) {
        global $OUTPUT, $USER;
            // If the menu has no children return an empty string
        if (!$menu->has_children()) {
            return '';
        }
        // Initialise this custom menu

        $content = html_writer::start_tag('ul', array('class'=>'nav'));

        // Render each child
        foreach ($menu->get_children() as $item) {
        $content .= $this->render_custom_menu_item($item);
        }

        // Close the open tags
        // $content .= html_writer::end_tag('ul');

        // Return the custom menu
        return $content;
        }

       /*
        * This code renders the custom menu items for the
        * bootstrap dropdown menu
        */

        protected function render_custom_menu_item(custom_menu_item $menunode) {
        // Required to ensure we get unique trackable id's
        static $submenucount = 0;

        if ($menunode->has_children()) {
            $content = html_writer::start_tag('li', array('class'=>'dropdown'));
        // If the child has menus render it as a sub menu
            $submenucount++;
        if ($menunode->get_url() !== null) {
            $url = $menunode->get_url();
        } else {
            $url = '#cm_submenu_'.$submenucount;
        }

          //$content .= html_writer::link($url, $menunode->get_text(), array('title'=>,));
            $content .= html_writer::start_tag('a', array('href'=>$url,'class'=>'dropdown-toggle','data-toggle'=>'dropdown'));
            $content .= html_writer::start_tag('i', array('class'=>"icon-leaf icon-white"));
            $content .= html_writer::end_tag('i').' ';
            $content .= $menunode->get_title().' ';
            $content .= html_writer::start_tag('b', array('class'=>'caret'));
            $content .= html_writer::end_tag('b');
            $content .= html_writer::end_tag('a');
            $content .= html_writer::start_tag('ul', array('class'=>'dropdown-menu'));
            foreach ($menunode->get_children() as $menunode) {
            $content .= $this->render_custom_menu_item($menunode);
        }
            $content .= html_writer::end_tag('ul');
        } else {
            $content = html_writer::start_tag('li');
        // The node doesn't have children so produce a final menuitem

        if ($menunode->get_url() !== null) {
            $url = $menunode->get_url();
        } else {
            $url = '#';
        }
        $content .= html_writer::link($url, $menunode->get_text(), array('title'=>$menunode->       get_title()));
        }
        $content .= html_writer::end_tag('li');
        // Return the sub menu
        return $content;
        }

       /*
        * This code replaces the standard moodle icons
        * with a icon sprite that is included in bootstrap
        * If the icon is not listed in the $icons array
        * the original Moodle icon will be shown
        */

        static $icons = array(
            'docs' => 'question-sign',
            'book' => 'book',
            'chapter' => 'file',
            'spacer' => 'spacer',
            'generate' => 'gift',
            'add' => 'plus',
            't/hide' => 'eye-open',
            'i/hide' => 'eye-open',
            't/show' => 'eye-close',
            'i/show' => 'eye-close',
            't/add' => 'plus',
            't/right' => 'arrow-right',
            't/left' => 'arrow-left',
            't/up' => 'arrow-up',
            't/down' => 'arrow-down',
            't/edit' => 'edit',
            't/editstring' => 'pencil',
            't/copy' => 'retweet',
            't/delete' => 'remove',
            'i/edit' => 'edit',
            'i/settings' => 'list-alt',
            'i/grades' => 'grades',
            'i/group' => 'user',
            't/groupn' => 'remove-circle',
            't/groups' => 'ok-circle',
            't/groupv' => 'ok-circle',
            't/switch_plus' => 'plus-sign',
            't/switch_minus' => 'minus-sign',
            'i/filter' => 'filter',
            't/move' => 'move',
            'i/move_2d' => 'move',
            'i/backup' => 'cog',
            'i/restore' => 'cog',
            'i/return' => 'repeat',
            'i/reload' => 'refresh',
            'i/roles' => 'user',
            'i/user' => 'user',
            'i/users' => 'user',
            'i/publish' => 'publish',
            'i/navigationitem' => 'chevron-right' );

        public function block_controls($controls) {
            if (empty($controls)) {
                return '';
            }
            $controlshtml = array();
            foreach ($controls as $control) {
                $controlshtml[] = self::a(array('href'=>$control['url'], 'title'=>$control['caption']), self::moodle_icon($control['icon']));
            }
            return self::div(array('class'=>'commands'), implode($controlshtml));
        }

        protected static function a($attributes, $content) {
            return html_writer::tag('a', $content, $attributes);
        }

        protected static function div($attributes, $content) {
            return html_writer::tag('div', $content, $attributes);
        }

        protected static function span($attributes, $content) {
            return html_writer::tag('span', $content, $attributes);
        }

        protected static function icon($name, $text=null) {
            if (!$text) {$text = $name;}
            return " ";
        }
        protected static function moodle_icon($name) {
            return self::icon(self::$icons[$name]);
        }
        public function icon_help() {
            return self::icon('question-sign');
        }

        public function action_icon($url, pix_icon $pixicon, component_action $action = null, array $attributes = null, $linktext=false) {
            if (!($url instanceof moodle_url)) {
                $url = new moodle_url($url);
            }
            $attributes = (array)$attributes;

            if (empty($attributes['class'])) {
                // let ppl override the class via $options
                $attributes['class'] = 'action-icon';
            }

            $icon = $this->render($pixicon);

            if ($linktext) {
                $text = $pixicon->attributes['alt'];
            } else {
                $text = '';
            }

            return $this->action_link($url, $text.$icon, $action, $attributes);
        }

        protected function render_pix_icon(pix_icon $icon) {

            if (isset(self::$icons[$icon->pix])) {
                return self::icon(self::$icons[$icon->pix]);
            } else {
                //return parent::render_pix_icon($icon);
                return 'pix.'">';
            }

        }

}


text:
package happynewmoonwithreport.opcode.bitwise.F64;

import happynewmoonwithreport.WasmInstanceInterface;
import happynewmoonwithreport.WasmRuntimeException;
import happynewmoonwithreport.WasmStack;
import happynewmoonwithreport.type.F64;
import java.util.UUID;

/**
 * Return the absolute value of the input.
 *

 *
Source:

 *
Operator:

 *

 * Return the absolute value of the input.
 *

 *
 * https://webassembly.github.io/spec/core/exec/numerics.html#op-fabs
 *
 *
 *
 *
Note: the documentation below is the same for all unary operators.

 *
Execution:

 *  https://webassembly.github.io/spec/core/exec/instructions.html#exec-unop
 *
 *
t.unop - Unary Operator

 *
 *

 *

 * 			Assert: due to validation, a value of value type t is on the top of the stack.
 *

 *

 * 			Pop the value t.const c1 from the stack.
 *

 *

 * 			If unopt(c1) is defined, then:
 *

 *

 * 					Let c be a possible result of computing unopt(c1).
 *

 *

 * 					Push the value t.const c to the stack.
 *

 *

 *

 *

 * 			Else:
 *

 *

 * 					Trap.
 *

 *

 *

 *

 */
public class F64_abs {
	private final String opCodeName = getClass().getName();
	private final String t1Type = "F64";

	private WasmInstanceInterface instance;

	private F64_abs() {
		super();
	}

	public F64_abs(WasmInstanceInterface instance) {
		this();
		this.instance = instance;
	}

	/**
	 * Execute the opcode.
	 */
	public void execute() {
		WasmStack<Object> stack = instance.stack();

		// Assert: due to validation, a value of value type t is on the top of the stack.
		if ((stack.peek() instanceof F64) == false) {
			throw new WasmRuntimeException(UUID.fromString("cbd437d1-65ba-40de-8978-20f781de727e"),
				opCodeName + ":"
				+ " Value type is incorrect. Value should be of type " + "'" +t1Type  + "'" +"."
				+ " The input type is " + "'" + stack.peek().getClass().getSimpleName() + "'."
				+ " The input value is " + "'" + stack.peek().toString() + "'.");

		}
		// Pop the value t1.const c1 from the stack.
		F64 c1 = (F64) stack.pop();

		//Let c be a possible result of computing unopt(c1).
		F64 c = c1.abs();

		// Push the value t.const c to the stack.
		stack.push(c);

		// No need to trap.
	}
}


text:
title: 从源码构建 Flink
nav-parent_id: flinkdev
nav-pos: 20
---
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you 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.
-->

This page covers how to build Flink {{ site.version }} from sources.

* This will be replaced by the TOC
{:toc}

## Build Flink

In order to build Flink you need the source code. Either [download the source of a release]({{ site.download_url }}) or [clone the git repository]({{ site.github_url }}).

In addition you need **Maven 3** and a **JDK** (Java Development Kit). Flink requires **at least Java 8** to build.

*NOTE: Maven 3.3.x can build Flink, but will not properly shade away certain dependencies. Maven 3.2.5 creates the libraries properly.
To build unit tests use Java 8u51 or above to prevent failures in unit tests that use the PowerMock runner.*

To clone from git, enter:

{% highlight bash %}
git clone {{ site.github_url }}
{% endhighlight %}

The simplest way of building Flink is by running:

{% highlight bash %}
mvn clean install -DskipTests
{% endhighlight %}

This instructs [Maven](http://maven.apache.org) (`mvn`) to first remove all existing builds (`clean`) and then create a new Flink binary (`install`).

To speed up the build you can skip tests, QA plugins, and JavaDocs:

{% highlight bash %}
mvn clean install -DskipTests -Dfast
{% endhighlight %}

## 构建PyFlink

#### 先决条件

1. 构建Flink

    如果您想构建一个可用于pip安装的PyFlink包,您需要先构建Flink工程,如[构建Flink](#build-flink)中所述。

2. Python的版本为3.5, 3.6 或者 3.7.

    ```shell
    $ python --version
    # the version printed here must be 3.5, 3.6 or 3.7
    ```

3. 构建PyFlink的Cython扩展模块(可选的)

    为了构建PyFlink的Cython扩展模块,您需要C编译器。在不同操作系统上安装C编译器的方式略有不同:

    * **Linux** Linux操作系统通常预装有GCC。否则,您需要手动安装。例如,您可以在Ubuntu或Debian上使用命令`sudo apt-get install build-essential`安装。

    * **Mac OS X** 要在Mac OS X上安装GCC,您需要下载并安装 [Xcode命令行工具](https://developer.apple.com/downloads/index.action),该工具可在Apple的开发人员页面中找到。

    您还需要使用以下命令安装依赖项:

    ```shell
    $ python -m pip install -r flink-python/dev/dev-requirements.txt
    ```

#### 安装

进入Flink源码根目录,并执行以下命令,构建PyFlink的源码发布包和wheel包:

{% highlight bash %}
cd flink-python; python setup.py sdist bdist_wheel
{% endhighlight %}

构建好的源码发布包和wheel包位于`./flink-python/dist/`目录下。它们均可使用pip安装,比如:

{% highlight bash %}
python -m pip install dist/*.tar.gz
{% endhighlight %}

## Dependency Shading

Flink [shades away](https://maven.apache.org/plugins/maven-shade-plugin/) some of the libraries it uses, in order to avoid version clashes with user programs that use different versions of these libraries. Among the shaded libraries are *Google Guava*, *Asm*, *Apache Curator*, *Apache HTTP Components*, *Netty*, and others.

The dependency shading mechanism was recently changed in Maven and requires users to build Flink slightly differently, depending on their Maven version:

**Maven 3.1.x and 3.2.x**
It is sufficient to call `mvn clean install -DskipTests` in the root directory of Flink code base.

**Maven 3.3.x**
The build has to be done in two steps: First in the base directory, then in the distribution project:

{% highlight bash %}
mvn clean install -DskipTests
cd flink-dist
mvn clean install
{% endhighlight %}

*Note:* To check your Maven version, run `mvn --version`.

{% top %}

## Hadoop Versions

Please see the [Hadoop integration section]({{ site.baseurl }}/ops/deployment/hadoop.html) on how to handle Hadoop classes and versions.

## Scala Versions

{% info %} Users that purely use the Java APIs and libraries can *ignore* this section.

Flink has APIs, libraries, and runtime modules written in [Scala](http://scala-lang.org). Users of the Scala API and libraries may have to match the Scala version of Flink with the Scala version of their projects (because Scala is not strictly backwards compatible).

Since version 1.7 Flink builds with Scala version 2.11 (default) and 2.12.

To build FLink against Scala 2.12, issue the following command:
{% highlight bash %}
mvn clean install -DskipTests -Dscala-2.12
{% endhighlight %}

{% top %}

## Encrypted File Systems

If your home directory is encrypted you might encounter a `java.io.IOException: File name too long` exception. Some encrypted file systems, like encfs used by Ubuntu, do not allow long filenames, which is the cause of this error.

The workaround is to add:

{% highlight xml %}
<args>
    <arg>-Xmax-classfile-name</arg>
    <arg>128</arg>
</args>
{% endhighlight %}

in the compiler configuration of the `pom.xml` file of the module causing the error. For example, if the error appears in the `flink-yarn` module, the above code should be added under the `<configuration>` tag of `scala-maven-plugin`. See [this issue](https://issues.apache.org/jira/browse/FLINK-2003) for more information.

{% top %}


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

		    <link rel="stylesheet" type="text/css" href="Styles/branding.css"/>
		    		<title>Open WSP solution file as .CAB</title>
				<meta name="Description" content="Displays the contents of the wsp solution file." />

								SharePoint Software Factory

								Recipe Index

								SPSF Helpers

								Helpers

											Exclude item from SourceControl

											Run MSBuild Target

											Get AssemblyName

											Get Installed SharePoint Version

Open WSP solution file as .CAB

											Open in Browser

											Open SPSF Help

											About SPSF

Related Links

Open WSP solution file as .CAB

Displays the contents of the wsp solution file.

Authors

Torsten Mandelkow

Version history

1.0 Initial Recipe

						Disclaimer: The views and opinions expressed in this documentation and in SPSF are those of the authors and do not necessarily reflect the opinions and recommendations of Microsoft or any member of Microsoft. All trademarks, service marks, collective marks, copyrights, registered names, and marks used or cited by this documentation are the property of their respective owners.

						SharePoint Software Factory, Version 4.1.3.2705, GPLv2, see http://spsf.codeplex.com for more information


text:
class AddDescToSlides < ActiveRecord::Migration[6.0]
  def change
    reversible do |dir|
      dir.up do
        Slide.add_translation_fields! desc: :text
      end

      dir.down do
        remove_column :slide_translations, :desc
      end
    end
  end
end


text:
import React from 'react'

import { Images, ComponentTable } from './styles'

export class ExplainScreen extends React.Component {

  constructor (props) {
    super(props)

    this.state = {
      selectedIndex: 0,
      intervalDuration: 2500
    }
  }

  componentDidMount () {
    this.startAnimation()
  }

  componentWillUnmount () {
    this.stopAnimation()
  }

  addEventListeners (index) {
    return {
      onMouseEnter: () => {
        if (!this.focused) {
          this.stopAnimation()
          this.setState({
            selectedIndex: index
          })
        }
      },
      onMouseLeave: () => {
        if (!this.focused) {
          this.startAnimation()
        }
      },
      onFocus: () => {
        this.focused = true
        this.stopAnimation()
        this.setState({
          selectedIndex: index
        })
      },
      onBlur: () => {
        this.focused = false
        this.startAnimation()
      }
    }
  }

  startAnimation () {
    if (this.interval) {
      return
    }

    this.interval = window.setInterval(() => {
      this.setState({
        selectedIndex: (this.state.selectedIndex + 1) % this.props.components.length
      })
    }, this.state.intervalDuration)
  }

  stopAnimation () {
    if (this.interval) {
      window.clearInterval(this.interval)
      this.interval = undefined
    }
  }

  render () {
    return (

        <Images style={{
          backgroundImage: `url(${this.props.image})`
        }}>
          <svg role='presentation' version='1.1' x='0px' y='0px' viewBox={[...this.props.svgOffset, ...this.props.dimensions].join(' ')}>
            {this.props.components.map((component, index) => (
              <rect tabIndex={0} key={index} className={this.state.selectedIndex === index ? 'selected' : ''}
                {...this.addEventListeners(index)}
                x={component.dimensions.x} y={component.dimensions.y} rx={5}
                width={component.dimensions.width} height={component.dimensions.height} />
            ))}
          </svg>
        </Images>
        <ComponentTable>

            {this.props.components.map((component, index) => {
              const componentTableCells = Array.isArray(component.tableCells) ? component.tableCells : [component.tableCells]

              const cells = componentTableCells.map((contents, index) => (

                  {contents}

              ))

              return (

                  {cells}

              )
            })}

        </ComponentTable>

    )
  }
}

export default ExplainScreen


text:
package org.gwtbootstrap3.client.ui;

import org.gwtbootstrap3.client.ui.base.button.CloseButton;
import org.gwtbootstrap3.client.ui.constants.ButtonDismiss;
import org.gwtbootstrap3.client.ui.constants.HeadingSize;
import org.gwtbootstrap3.client.ui.constants.Styles;
import org.gwtbootstrap3.client.ui.gwt.FlowPanel;

import com.google.gwt.dom.client.Element;

/**
 * @author Sven Jacobs
 * @author Joshua Godi
 * @see Modal
 */
public class ModalHeader extends FlowPanel implements ModalComponent, IsClosable {

    private final Heading heading = new Heading(HeadingSize.H4);
    private final CloseButton closeButton = new CloseButton();

    public ModalHeader() {
        setStyleName(Styles.MODAL_HEADER);

        heading.setStyleName(Styles.MODAL_TITLE);
        closeButton.setDataDismiss(ButtonDismiss.MODAL);

        add(closeButton);
    }

    @Override
    public void setTitle(final String title) {
        heading.setText(title);

        if (heading.getParent() == null) {
            add(heading);
        }
    }

    @Override
    public void setClosable(final boolean closable) {
        if (closable) {
            insert(closeButton, (Element) getElement(), 0, true);
        } else {
            closeButton.removeFromParent();
        }
    }

    @Override
    public boolean isClosable() {
        return closeButton.getParent() != null;
    }
}


text:
class AddRoleToUsers < ActiveRecord::Migration[5.1]
  def change
    add_column :users, :role, :integer
  end
end


text:
"""Runs FedAvg + Finetuning experiment."""

import collections
import functools
from typing import Any, Callable, List, OrderedDict, Tuple

from absl import app
from absl import flags
import tensorflow as tf
import tensorflow_federated as tff

from personalization_benchmark.cross_device import constants
from personalization_benchmark.cross_device.algorithms import finetuning_utils
from personalization_benchmark.cross_device.datasets import emnist
from personalization_benchmark.cross_device.datasets import landmark
from personalization_benchmark.cross_device.datasets import stackoverflow
from personalization_benchmark.cross_device.datasets import ted_multi
from utils import training_utils
from utils import utils_impl
from utils.optimizers import optimizer_utils

with utils_impl.record_hparam_flags() as training_flags:
  # Training loop configuration
  _EXPERIMENT_NAME = flags.DEFINE_string(
      'experiment_name', None, 'The name of this experiment. Will be append to '
      '--root_output_dir to separate experiment results.')
  _DATASET_NAME = flags.DEFINE_enum('dataset_name', None,
                                    constants.DATASET_NAMES,
                                    'Which dataset to use for experiments.')
  _ROOT_OUTPUT_DIR = flags.DEFINE_string(
      'root_output_dir', '/tmp/personalization_benchmark/',
      'Root directory for writing experiment output.')
  _TOTAL_ROUNDS = flags.DEFINE_integer('total_rounds', 100,
                                       'Number of total training rounds.')
  _ROUNDS_PER_EVALUATION = flags.DEFINE_integer(
      'rounds_per_evaluation', 10, 'Frequency of performing evaluation.')
  _ROUNDS_PER_CHECKPOINT = flags.DEFINE_integer(
      'rounds_per_checkpoint', 50, 'Frequency of saving a checkpoint.')
  _PATH_TO_INITIAL_MODEL_WEIGHTS = flags.DEFINE_string(
      'path_to_initial_model_weights', None,
      'Path to load the initial Keras model used for warmstarting FedAvg. If '
      'None, use random initialization. See `algorithms/checkpoint_utils.py` '
      'for extracting model weights from a checkpoint given by our trainer.')

  # Train client configuration
  _CLIENTS_PER_TRAIN_ROUND = flags.DEFINE_integer(
      'clients_per_train_round', 10,
      'How many clients to sample at each training round.')
  _TRAIN_EPOCHS = flags.DEFINE_integer(
      'train_epochs', 1,
      'Number of epochs performed by a client during a round of training.')
  _TRAIN_BATCH_SIZE = flags.DEFINE_integer('train_batch_size', 10,
                                           'Batch size on train clients.')

  # Finetuning evaluation configuration
  _FINETUNE_MAX_EPOCHS = flags.DEFINE_integer(
      'finetune_max_epochs', 20, 'Maximum number of finetuning epochs. The '
      'best epoch will be in [0, finetune_max_epochs], which is identified '
      'by post-processing the evaluation metrics.')
  _FINETUNE_LAST_LAYER = flags.DEFINE_bool(
      'finetune_last_layer', False, 'If True, only finetune the last layer. '
      'Default is False, which means all layers will be finetuned.')
  _VALID_CLIENTS_PER_EVALUATION = flags.DEFINE_integer(
      'valid_clients_per_evaluation', 100, 'Number of validation clients '
      'sampled to perform finetuning evaluation.')
  _TEST_CLIENTS_PER_EVALUATION = flags.DEFINE_integer(
      'test_clients_per_evaluation', 100, 'Number of test clients sampled to '
      'perform finetuning evaluation.')

  # Random seeds for reproducibility
  _BASE_RANDOM_SEED = flags.DEFINE_integer(
      'base_random_seed', 0, 'An integer random seed governing'
      ' the randomness in the simulation.')

  # Debugging flags
  _USE_SYNTHETIC_DATA = flags.DEFINE_bool(
      'use_synthetic_data', False, 'Whether to use synthetic data. This should '
      'only be set to True for debugging purposes.')

with utils_impl.record_hparam_flags() as optimizer_flags:
  optimizer_utils.define_optimizer_flags('client')
  optimizer_utils.define_optimizer_flags('server')
  optimizer_utils.define_optimizer_flags('finetune')

def _write_hparams():
  """Creates an ordered dictionary of hyperparameter flags and writes to CSV."""
  hparam_dict = utils_impl.lookup_flag_values(training_flags)
  # Update with optimizer flags corresponding to the chosen optimizers.
  opt_flag_dict = utils_impl.lookup_flag_values(optimizer_flags)
  opt_flag_dict = optimizer_utils.remove_unused_flags('client', opt_flag_dict)
  opt_flag_dict = optimizer_utils.remove_unused_flags('server', opt_flag_dict)
  opt_flag_dict = optimizer_utils.remove_unused_flags('finetune', opt_flag_dict)
  hparam_dict.update(opt_flag_dict)
  # Write the updated hyperparameters to a file.
  training_utils.write_hparams_to_csv(hparam_dict, _ROOT_OUTPUT_DIR.value,
                                      _EXPERIMENT_NAME.value)

def _create_train_algorithm(
    model_fn: Callable[[], tff.learning.Model]
) -> tff.learning.templates.LearningProcess:
  """Creates a learning process for client training."""
  server_optimizer_fn = optimizer_utils.create_optimizer_fn_from_flags('server')
  client_optimizer_fn = optimizer_utils.create_optimizer_fn_from_flags('client')
  learning_process = tff.learning.algorithms.build_weighted_fed_avg(
      model_fn=model_fn,
      client_optimizer_fn=client_optimizer_fn,
      server_optimizer_fn=server_optimizer_fn,
      model_aggregator=tff.learning.robust_aggregator())
  if _PATH_TO_INITIAL_MODEL_WEIGHTS.value is not None:
    saved_keras_model = tf.keras.models.load_model(
        _PATH_TO_INITIAL_MODEL_WEIGHTS.value)
    initial_model_weights = tff.learning.ModelWeights.from_model(
        saved_keras_model)
    original_state = learning_process.initialize()
    initial_state = learning_process.set_model_weights(original_state,
                                                       initial_model_weights)

    @tff.federated_computation
    def new_initialize_fn():
      return tff.federated_value(initial_state, tff.SERVER)

    learning_process = tff.learning.templates.LearningProcess(
        new_initialize_fn, learning_process.next,
        learning_process.get_model_weights, learning_process.set_model_weights)
  return learning_process

def _create_model_and_data(
    dataset_name: str, use_synthetic_data: bool
) -> Tuple[constants.ModelFnType, constants.FederatedDatasetsType,
           constants.ProcessFnType, constants.SplitDataFnType, str]:
  """Creates model, datasets, and processing functions for the given dataset."""
  if dataset_name == 'emnist':
    return emnist.create_model_and_data(
        num_local_epochs=_TRAIN_EPOCHS.value,
        train_batch_size=_TRAIN_BATCH_SIZE.value,
        use_synthetic_data=use_synthetic_data)
  elif dataset_name == 'stackoverflow':
    return stackoverflow.create_model_and_data(
        num_local_epochs=_TRAIN_EPOCHS.value,
        train_batch_size=_TRAIN_BATCH_SIZE.value,
        use_synthetic_data=use_synthetic_data)
  elif dataset_name == 'landmark':
    return landmark.create_model_and_data(
        num_local_epochs=_TRAIN_EPOCHS.value,
        train_batch_size=_TRAIN_BATCH_SIZE.value,
        use_synthetic_data=use_synthetic_data)
  elif dataset_name == 'ted_multi':
    return ted_multi.create_model_and_data(
        num_local_epochs=_TRAIN_EPOCHS.value,
        train_batch_size=_TRAIN_BATCH_SIZE.value,
        use_synthetic_data=use_synthetic_data)
  raise ValueError(f'Accepted dataset names: {constants.DATASET_NAMES}, but '
                   f'found {dataset_name}. Please provide a valid name.')

def _finetune_eval_fn_builder():
  return finetuning_utils.build_finetune_eval_fn(
      optimizer_fn=optimizer_utils.create_optimizer_fn_from_flags('finetune'),
      batch_size=_TRAIN_BATCH_SIZE.value,
      num_finetuning_epochs=_FINETUNE_MAX_EPOCHS.value,
      finetune_last_layer=_FINETUNE_LAST_LAYER.value)

def _split_data_and_run_finetuning_eval_computation(
    client_data, model_fn, split_data_fn) -> tff.Computation:
  """Creates a TFF computation to split client data and run finetuning eval."""

  @tff.tf_computation(tf.string)
  def split_data_for_client(client_id):
    return split_data_fn(client_data.dataset_computation(client_id))

  # Create the evaluation computation (and wiring in a dataset computation).
  finetuning_eval_computation = tff.learning.build_personalization_eval(
      model_fn=model_fn,
      personalize_fn_dict=collections.OrderedDict([
          (constants.FINETUNING_FN_NAME, _finetune_eval_fn_builder)
      ]),
      baseline_evaluate_fn=finetuning_utils.evaluate_fn,
      max_num_clients=max(_VALID_CLIENTS_PER_EVALUATION.value,
                          _TEST_CLIENTS_PER_EVALUATION.value))

  model_weights_type = finetuning_eval_computation.type_signature.parameter[0]
  # Note that `tff.simulation.compose_dataset_computation_with_computation` does
  # not work here, because the dataset computation returns a dict of datasets.
  @tff.federated_computation(model_weights_type,
                             tff.types.at_clients(tf.string))
  def split_data_and_run_finetuning_eval(model_weights, client_ids):
    processed_datasets = tff.federated_map(split_data_for_client, client_ids)
    return finetuning_eval_computation(model_weights, processed_datasets)

  return split_data_and_run_finetuning_eval

def main(argv):
  if len(argv) > 1:
    raise app.UsageError('Expected no command-line arguments, '
                         'got: {}'.format(argv))
  if not _EXPERIMENT_NAME.value:
    raise ValueError('FLAGS.experiment_name must be set.')

  model_fn, federated_datasets, train_preprocess_fn, split_data_fn, accuracy_name = (
      _create_model_and_data(_DATASET_NAME.value, _USE_SYNTHETIC_DATA.value))
  train_client_data = federated_datasets[constants.TRAIN_CLIENTS_KEY]
  valid_client_data = federated_datasets[constants.VALID_CLIENTS_KEY]
  test_client_data = federated_datasets[constants.TEST_CLIENTS_KEY]

  # Create the training client selection function, which takes in an integer
  # round number, and outputs a list of client IDs for training in that round.
  training_selection_fn = functools.partial(
      tff.simulation.build_uniform_sampling_fn(
          train_client_data.client_ids, random_seed=_BASE_RANDOM_SEED.value),
      size=_CLIENTS_PER_TRAIN_ROUND.value)

  # Create the training process (and wiring in a dataset computation)
  @tff.tf_computation(tf.string)
  def build_train_dataset_from_client_id(client_id):
    raw_client_data = train_client_data.dataset_computation(client_id)
    return train_preprocess_fn(raw_client_data)

  learning_process = _create_train_algorithm(model_fn)
  training_process = tff.simulation.compose_dataset_computation_with_iterative_process(
      build_train_dataset_from_client_id, learning_process)
  training_process.get_model_weights = learning_process.get_model_weights

  # Create the evaluation client selection function, which takes in an integer
  # round number, and outputs two lists of client IDs: validation clients and
  # test clients. The output of `evaluation_selection_fn` will be used as the
  # second parameter of the `evaluation_fn` below.
  valid_clients_sampling_fn = tff.simulation.build_uniform_sampling_fn(
      valid_client_data.client_ids, random_seed=_BASE_RANDOM_SEED.value)
  test_clients_sampling_fn = tff.simulation.build_uniform_sampling_fn(
      test_client_data.client_ids, random_seed=_BASE_RANDOM_SEED.value)
  evaluation_selection_fn = lambda round_num: (  # pylint: disable=g-long-lambda
      valid_clients_sampling_fn(round_num, _VALID_CLIENTS_PER_EVALUATION.value),
      test_clients_sampling_fn(round_num, _TEST_CLIENTS_PER_EVALUATION.value))

  valid_clients_eval_computation = _split_data_and_run_finetuning_eval_computation(
      valid_client_data, model_fn, split_data_fn)
  test_clients_eval_computation = _split_data_and_run_finetuning_eval_computation(
      test_client_data, model_fn, split_data_fn)

  def evaluation_fn(
      state: tff.learning.templates.LearningAlgorithmState,
      sampled_client_ids: Tuple[List[str], List[str]]) -> OrderedDict[str, Any]:
    """Evaluates the current model on the sampled validation and test clients.

    Args:
      state: The current round's state returned by `training_process`.
      sampled_client_ids: A tuple of sample validation and test client ids
        returned by `evaluation_selection_fn`.

    Returns:
      The evaluation metrics after postprocessing. Specifically, we use the
      validation metrics to select the best finetuning epoch (between 0 and
      `finetune_max_epochs`), and returns the test metrics at that epoch.
    """
    valid_client_ids, test_client_ids = sampled_client_ids
    raw_valid_metrics = valid_clients_eval_computation(
        training_process.get_model_weights(state), valid_client_ids)
    raw_test_metrics = test_clients_eval_computation(
        training_process.get_model_weights(state), test_client_ids)
    return finetuning_utils.postprocess_finetuning_metrics(
        valid_metrics_dict=raw_valid_metrics,
        test_metrics_dict=raw_test_metrics,
        accuracy_name=accuracy_name,
        finetuning_fn_name=constants.FINETUNING_FN_NAME)

  # Configuring release managers and performing training/eval
  program_state_manager, metrics_managers = training_utils.create_managers(
      _ROOT_OUTPUT_DIR.value, _EXPERIMENT_NAME.value)
  _write_hparams()
  tff.simulation.run_training_process(
      training_process=training_process,
      training_selection_fn=training_selection_fn,
      total_rounds=_TOTAL_ROUNDS.value,
      evaluation_fn=evaluation_fn,
      evaluation_selection_fn=evaluation_selection_fn,
      rounds_per_evaluation=_ROUNDS_PER_EVALUATION.value,
      program_state_manager=program_state_manager,
      rounds_per_saving_program_state=_ROUNDS_PER_CHECKPOINT.value,
      metrics_managers=metrics_managers)

if __name__ == '__main__':
  app.run(main)


text:
<!DOCTYPE html>

    <meta charset="utf-8">
    <title>CSS Masking: mask-size: mask layer size</title>
    <link rel="author" title="Astley Chen" href="mailto:aschen@mozilla.com">
    <link rel="author" title="Mozilla" href="https://www.mozilla.org">
    <link rel="help" href="https://www.w3.org/TR/css-masking-1/#the-mask-size">
    <link rel="match" href="mask-size-auto-ref.html">
    <meta name="assert" content="Test checks whether sizing mask layer works correctly or not.">


text:
from datetime import datetime, timedelta
import os
import sys
import threading
import time

from cement.utils.misc import minimal_logger
from six import iteritems

from ..core import fileoperations, io
from ..lib import elasticbeanstalk, utils, cloudwatch
from ..lib.aws import MaxRetriesError
from ..resources.strings import strings, prompts
from ..objects.exceptions import ServiceError
from . import commonops

LOG = minimal_logger(__name__)

def logs(env_name, info_type, do_zip=False, instance_id=None):
    # Request info
    result = elasticbeanstalk.request_environment_info(env_name, info_type)

    # Wait for logs to finish
    request_id = result['ResponseMetadata']['RequestId']
    io.echo(prompts['logs.retrieving'])
    commonops.wait_for_success_events(request_id, timeout_in_minutes=2,
                                      sleep_time=1, stream_events=False)

    get_logs(env_name, info_type, do_zip=do_zip,
             instance_id=instance_id)

def get_logs(env_name, info_type, do_zip=False, instance_id=None):
    # Get logs
    result = elasticbeanstalk.retrieve_environment_info(env_name, info_type)

    """
    Results are ordered with latest last, we just want the latest
    """
    log_list = {}
    for log in result['EnvironmentInfo']:
        i_id = log['Ec2InstanceId']
        url = log['Message']
        log_list[i_id] = url

    if instance_id:
        log_list = {instance_id: log_list[instance_id]}

    if info_type == 'bundle':
        # save file, unzip, place in logs directory
        logs_folder_name = datetime.now().strftime("%y%m%d_%H%M%S")
        logs_location = fileoperations.get_logs_location(logs_folder_name)
        #get logs for each instance
        for i_id, url in iteritems(log_list):
            zip_location = utils.save_file_from_url(url, logs_location,
                                                    i_id + '.zip')
            instance_folder = os.path.join(logs_location, i_id)
            fileoperations.unzip_folder(zip_location, instance_folder)
            fileoperations.delete_file(zip_location)

        fileoperations.set_user_only_permissions(logs_location)
        if do_zip:
            fileoperations.zip_up_folder(logs_location, logs_location + '.zip')
            fileoperations.delete_directory(logs_location)

            logs_location += '.zip'
            fileoperations.set_user_only_permissions(logs_location)
            io.echo(strings['logs.location'].replace('{location}',
                                                     logs_location))
        else:
            io.echo(strings['logs.location'].replace('{location}',
                                                     logs_location))
            # create symlink to logs/latest
            latest_location = fileoperations.get_logs_location('latest')
            try:
                os.unlink(latest_location)
            except OSError:
                # doesn't exist. Ignore
                pass
            try:
                os.symlink(logs_location, latest_location)
                io.echo('Updated symlink at', latest_location)
            except OSError:
                #Oh well.. we tried.
                ## Probably on windows, or logs/latest is not a symlink
                pass

    else:
        # print logs
        data = []
        for i_id, url in iteritems(log_list):
            data.append('============= ' + str(i_id) + ' ==============')
            log_result = utils.get_data_from_url(url)
            if sys.version_info[0] >= 3:
                log_result = log_result.decode()
            data.append(log_result)
        io.echo_with_pager(os.linesep.join(data))

def stream_logs(env_name, sleep_time=2):
    log_group_name = 'awseb-' + env_name + '-activity'
    stream_names = []

    streamer = io.get_event_streamer()
    streamer.prompt = ' -- eb-activity log -- (Ctrl+C to exit)'
    jobs = []
    while True:
        new_names = cloudwatch.get_all_stream_names(log_group_name)
        for name in new_names:
            if name not in stream_names:
                stream_names.append(name)

                p = threading.Thread(
                    target=_stream_single_stream,
                    args=(log_group_name, name, streamer, sleep_time))
                p.daemon = True
                jobs.append(p)
                p.start()
            time.sleep(0.2)  # offset threads

        time.sleep(10)

def _stream_single_stream(log_group_name, stream_name, streamer, sleep_time=4):
    next_token = None
    while True:
        try:
            response = cloudwatch.get_log_events(log_group_name, stream_name,
                                                 next_token=next_token)

            if response and response.get('events'):
                for event in response.get('events'):
                    message = event.get('message')

                    streamer.stream_event('[{}] {}'.format(stream_name, message))

                # Set the next token
                next_token = response.get('nextForwardToken')

            else:
                time.sleep(sleep_time)
        except MaxRetriesError:
            LOG.debug('Received max retries')
            io.echo('Received Max retries. You are being heavily throttled.')
        except ServiceError as e:
            # Something went wrong getting the stream
            # It probably doesnt exist anymore.
            LOG.debug('Received service error {}'.format(e))
            return
        except Exception as e:
            # We want to swallow all exceptions or else they will be
            # printed as a stack trace to the Console
            # Exceptions are typically connections reset and
            # Various things
            LOG.debug('Exception raised: ' + str(e))
            # Loop will cause a retry