text:
import factory.django, factory.fuzzy

class MembershipApplicationTagFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = 'members.MembershipApplicationTag'
        django_get_or_create = ('label',)

def generate_standard_set():
    for x in ('Welcome email sent', 'Special tag 1', 'Special tag 2'):
        MembershipApplicationTagFactory(label=x)


text:
"""
Copyright 2016 ElasticBox All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import time

from bson.objectid import ObjectId
from pymongo.errors import PyMongoError
from tornado.gen import coroutine, Return

class ObjectNotFoundError(PyMongoError):
    """Raised when object is not found into the collection.
    """

class Query(object):

    def __init__(self, database, collection, manipulate=False):
        self.database = database
        self.collection = collection
        self.manipulate = manipulate

        self.default_criteria = {"$and": [{"metadata.deletionTimestamp": None}]}

    def _generate_query(self, criteria):
        query = self.default_criteria
        if criteria:
            if len(criteria) > 1:
                for key, value in criteria.iteritems():
                    query['$and'].append({key: value})
            else:
                query['$and'].append(criteria)

        return query

    @coroutine
    def find_one(self, criteria=None, projection=None):
        document = yield self.database[self.collection].find_one(
            self._generate_query(criteria),
            projection,
            manipulate=self.manipulate)
        raise Return(document)

    @coroutine
    def find(self, criteria=None, projection=None, sort=None, limit=0):
        documents = []

        cursor = self.database[self.collection].find(
            self._generate_query(criteria),
            projection,
            sort=sort,
            limit=limit,
            manipulate=self.manipulate)
        while (yield cursor.fetch_next):
            documents.append(cursor.next_object())

        raise Return(documents)

    @coroutine
    def insert(self, document):
        if "metadata" in document:
            document["metadata"]["resourceVersion"] = time.time()
            document["metadata"]["creationTimestamp"] = time.time()
            document["metadata"]["deletionTimestamp"] = None
        else:
            document["metadata"] = dict(
                resourceVersion=time.time(),
                creationTimestamp=time.time(),
                deletionTimestamp=None
            )

        if '_id' not in document and not self.manipulate:
            document['_id'] = ObjectId()
        document_id = yield self.database[self.collection].insert(document, manipulate=self.manipulate)
        inserted_document = yield self.database[self.collection].find_one(
            {"_id": document_id},
            manipulate=self.manipulate)
        raise Return(inserted_document)

    @coroutine
    def update(self, document):
        document["metadata"]["resourceVersion"] = time.time()
        response = yield self.database[self.collection].update(
            {"_id": document["_id"]},
            document,
            upsert=True,
            manipulate=self.manipulate)
        if response['n'] == 0:
            raise ObjectNotFoundError()

        updated_document = yield self.database[self.collection].find_one(
            {"_id": document["_id"]},
            manipulate=self.manipulate)
        raise Return(updated_document)

    @coroutine
    def update_fields(self, criteria, fields):
        update = {"$set": fields}
        update["$set"]["metadata.resourceVersion"] = time.time()

        response = yield self.database[self.collection].update(criteria, update, manipulate=self.manipulate)
        raise Return(response)

    @coroutine
    def remove(self, document):
        response = yield self.database[self.collection].remove({"_id": document["_id"]})
        raise Return(response)

redpajama_set_name: RedPajamaGithub


text:
.footable {
	border-collapse: separate;
	border-spacing: 0;
	width: 100%;
	border: solid #ccc 1px;
	border-radius: 0;
	font-size: 14px;
}

.footable.breakpoint > tbody > tr > td.expand {
  background: url('img/plus.png') no-repeat 5px center;
  padding-left: 40px;
}

.footable.breakpoint > tbody > tr.footable-detail-show > td.expand {
  background: url('img/minus.png') no-repeat 5px center;
}

.footable.breakpoint > tbody > tr.footable-row-detail {
  background: #EBEDEF;
}

.footable > tbody > tr:hover {
  background: #fbf8e9;
}

.footable.breakpoint > tbody > tr:hover:not(.footable-row-detail) {
  cursor: pointer;
}

.footable > tbody > tr > td, .footable > thead > tr > th {
  border-left: 1px solid #ccc;
  border-top: 1px solid #ccc;
  padding: 10px;
  text-align: left;
}

.footable > tbody > tr > td.footable-cell-detail {
  border-left: none;
}

.footable > thead > tr > th, .footable > thead > tr > td {
  background-color:#EBEDED;

  -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
  -moz-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
  box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
  border-top: none;
  text-shadow: 0 1px 0 rgba(255,255,255,.5);
}

.footable > thead > tr:first-child > th.footable-first-column, .footable > thead > tr:first-child > td.footable-first-column {
  -moz-border-radius: 0 0;
  -webkit-border-radius: 0 0;
  border-radius: 0 0;
}

.footable > thead > tr:first-child > th.footable-last-column, .footable > thead > tr:first-child > td.footable-last-column {
  -moz-border-radius: 0 0;
  -webkit-border-radius: 0 0;
  border-radius: 0 0;
}

.footable > thead > tr:first-child > th.footable-first-column.footable-last-column, .footable > thead > tr:first-child > td.footable-first-column.footable-last-column {
  -moz-border-radius: 6px 0;
  -webkit-border-radius: 6px 0;
  border-radius: 6px 0;
}

.footable > tbody > tr:last-child > td.footable-first-column {
  -moz-border-radius: 0 0 0 6px;
  -webkit-border-radius: 0 0 0 6px;
  border-radius: 0 0 0 6px;
}

.footable > tbody > tr:last-child > td.footable-last-column {
  -moz-border-radius: 0 0 6px 0;
  -webkit-border-radius: 0 0 6px 0;
  border-radius: 0 0 6px 0;
}

.footable > tbody > tr:last-child > td.footable-first-column.footable-last-column {
  -moz-border-radius: 0 0 6px 6px;
  -webkit-border-radius: 0 0 6px 6px;
  border-radius: 0 0 6px 6px;
}

.footable > thead > tr > th.footable-first-column, .footable > thead > tr > td.footable-first-column,
.footable > tbody > tr > td.footable-first-column {
  border-left: none;
}

.footable > tbody img {
  vertical-align:middle;
}

.footable > tfoot > tr > th, .footable > tfoot > tr > td {
  background-color: #dce9f9;
  background-image: -webkit-gradient(linear, left top, left bottom, from(#ebf3fc), to(#dce9f9));
  background-image: -webkit-linear-gradient(top, #ebf3fc, #dce9f9);
  background-image: -moz-linear-gradient(top, #ebf3fc, #dce9f9);
  background-image: -ms-linear-gradient(top, #ebf3fc, #dce9f9);
  background-image: -o-linear-gradient(top, #ebf3fc, #dce9f9);
  background-image: linear-gradient(to bottom, #ebf3fc, #dce9f9);
  -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
  -moz-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
  box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
  border-top: 1px solid #ccc;
  text-shadow: 0 1px 0 rgba(255,255,255,.5);
  padding: 10px;
}


text:
const router = require('express').Router();
const User = require('../../db').models.user;
// const HttpError = require('../../utils/HttpError');

// This marries the original auth code we wrote to Passport.
// An alternative would be to use the "local strategy" option with Passport.

// check currently-authenticated user, i.e. "who am I?"
router.get('/', (req, res, next) => {
  // with Passport:
  res.json(req.user);
  // // before, without Passport:
  // User.findById(req.session.userId)
  // .then(user => res.json(user))
  // .catch(next);
});

// signup, i.e. "let `me` introduce myself"
router.post('/', function (req, res, next) {
  User.findOrCreate({
    where: {
      email: req.body.email
    },
    defaults: { // if the user doesn't exist, create including this info
      password: req.body.password
    }
  })
  .spread((user, created) => {
    if (created) {
      // with Passport:
      req.logIn(user, function (err) {
        if (err) return next(err);
        res.json(user);
      });
      // // before, without Passport:
      // req.session.userId = user.id;
      // res.json(user);
    } else {
      res.sendStatus(401); // this user already exists, you cannot sign up
    }
  })
  .catch(next);
});

// login, i.e. "you remember `me`, right?"
router.put('/', function (req, res, next) {
  User.findOne({
    where: req.body // email and password
  })
  .then(user => {
    if (!user) {
      res.sendStatus(401); // no message; good practice to omit why auth fails
    } else {
      // with Passport:
      req.logIn(user, (err) => {
        if (err) return next(err);
        res.json(user);
      });
      // // before, without Passport:
      // req.session.userId = user.id;
      // res.json(user);
    }
  })
  .catch(next);
});

// logout, i.e. "please just forget `me`"
router.delete('/', function (req, res, next) {
  // with Passport
  req.logOut();
  // // before, without Passport
  // req.session.destroy();
  res.sendStatus(204);

});

module.exports = router;


text:
module Linear
  ( module Linear.Algebra
  , module Linear.Binary
  , module Linear.Conjugate
  , module Linear.Covector
  , module Linear.Epsilon
  , module Linear.Matrix
  , module Linear.Metric
  , module Linear.Projection
  , module Linear.Quaternion
  , module Linear.Trace
  , module Linear.V0
  , module Linear.V1
  , module Linear.V2
  , module Linear.V3
  , module Linear.V4
  , module Linear.Vector
  )  where

import Linear.Algebra
import Linear.Binary
import Linear.Conjugate
import Linear.Covector
import Linear.Epsilon
import Linear.Instances ()
import Linear.Matrix
import Linear.Metric
import Linear.Projection
import Linear.Quaternion
import Linear.Trace
import Linear.V0
import Linear.V1
import Linear.V2
import Linear.V3
import Linear.V4
import Linear.Vector

{-# ANN module "Hlint: ignore Use import/export shortcut" #-}


text:
/*
 * changes to previous injection implementations
 * - no support for data-injection anymore, switch to new data-inject
 * - no support for data-href-next anymore, switch to data-inject: next-href
 * - XXX: add support for forms, see remnants in inject1 and ajaxify
 */
define([
    "jquery",
    "ajax",
    "parser",
    "logger",
    "registry",
    "utils",
    "htmlparser",
    "jquery-ext"  // for :scrollable for autoLoading-visible
], function($, ajax, Parser, logger, registry, utils, htmlparser) {
    var log = logger.getLogger("pat.inject"),
        parser = new Parser("inject");

    //parser.add_argument("selector", "body");
    parser.add_argument("selector");
    //XXX: (yet) unsupported: parser.add_argument("target", "$selector");
    parser.add_argument("target");
    parser.add_argument("data-type", "html");
    parser.add_argument("next-href");
    //XXX: (yet) unsupported: parser.add_argument("source", "$selector");
    parser.add_argument("source");
    parser.add_argument("trigger", "default", ["default", "autoload", "autoload-visible"]);
    // XXX: this should not be here but the parser would bail on
    // unknown parameters and expand/collapsible need to pass the url
    // to us
    parser.add_argument("url");
    parser.add_argument("class");
    parser.add_argument("history");

    var _ = {
        name: "inject",
        trigger: "a.pat-inject, form.pat-inject, .pat-subform.pat-inject",
        init: function($el, opts) {
            if ($el.length > 1)
                return $el.each(function() { _.init($(this), opts); });

            var cfgs = _.extractConfig($el, opts);

            // if the injection shall add a history entry and HTML5 pushState
            // is missing, then don't initialize the injection.
            if (cfgs.some(function(e){return e.history === "record";}) &&
                    !("pushState" in history))
                return $el;

            $el.data("patterns.inject", cfgs);

            // In case next-href is specified the anchor's href will
            // be set to it after the injection is triggered. In case
            // the next href already exists, we do not activate the
            // injection but instead just change the anchors href.
            //
            // XXX: This is used in only one project for linked
            // fullcalendars, it's sanity is wonky and we should
            // probably solve it differently. -- Maybe it's cool
            // after all.
            var $nexthref = $(cfgs[0].nextHref);
            if ($el.is("a") && $nexthref.length > 0) {
                log.debug("Skipping as next href already exists", $nexthref);
                // XXX: reconsider how the injection enters exhausted state
                return $el.attr({href: (window.location.href.split("#")[0] || "") +
                                 cfgs[0].nextHref});
            }

            switch (cfgs[0].trigger) {
            case "default":
                // setup event handlers
                if ($el.is("a")) {
                    $el.on("click.pat-inject", _.onClick);
                } else if ($el.is("form")) {
                    $el.on("submit.pat-inject", _.onSubmit)
                       .on("click.pat-inject", "[type=submit]", ajax.onClickSubmit);
                } else if ($el.is(".pat-subform")) {
                    log.debug("Initializing subform with injection");
                }
                break;
            case "autoload":
                _.onClick.apply($el[0], []);
                break;
            case "autoload-visible":
                _._initAutoloadVisible($el);
                break;
            }

            log.debug("initialised:", $el);

            return $el;
        },

        destroy: function($el) {
            $el.off(".pat-inject");
            $el.data("patterns.inject", null);
            return $el;
        },

        onClick: function(ev) {
            var cfgs = $(this).data("patterns.inject"),
                $el = $(this);
            if (ev)
                ev.preventDefault();
            $el.trigger("patterns-inject-triggered");
            _.execute(cfgs, $el);
        },

        onSubmit: function(ev) {
            var cfgs = $(this).data("patterns.inject"),
                $el = $(this);
            if (ev)
                ev.preventDefault();
            $el.trigger("patterns-inject-triggered");
            _.execute(cfgs, $el);
        },

        submitSubform: function($sub) {
            var $el = $sub.parents('form'),
                cfgs = $sub.data("patterns.inject");
            try {
                $el.trigger("patterns-inject-triggered");
            } catch (e) {
                log.error("patterns-inject-triggered", e);
            }
            _.execute(cfgs, $el);
        },

        extractConfig: function($el, opts) {
            opts = $.extend({}, opts);

            var cfgs = parser.parse($el, opts, true);
            cfgs.forEach(function(cfg) {
                var urlparts, defaultSelector;
                // opts and cfg have priority, fallback to href/action
                cfg.url = opts.url || cfg.url || $el.attr("href") ||
                    $el.attr("action") || $el.parents('form').attr('action') ||
                    "";

                // separate selector from url
                urlparts = cfg.url.split("#");
                cfg.url = urlparts[0];

                // if no selector, check for selector as part of original url
                defaultSelector = urlparts[1] && "#" + urlparts[1] || "body";

                if (urlparts.length > 2) {
                    log.warn("Ignoring additional source ids:", urlparts.slice(2));
                }

                cfg.selector = cfg.selector || defaultSelector;
            });
            return cfgs;
        },
        // verify and post-process config
        // XXX: this should return a command instead of messing around on the config
        verifyConfig: function(cfgs, $el) {
            var url = cfgs[0].url;

            // verification for each cfg in the array needs to succeed
            return cfgs.every(function(cfg) {
                // in case of multi-injection, all injections need to use
                // the same url
                if (cfg.url !== url) {
                    log.error("Unsupported different urls for multi-inject");
                    return false;
                }

                // defaults
                cfg.source = cfg.source || cfg.selector;
                cfg.target = cfg.target || cfg.selector;

                if (!_._extractModifiers(cfg))
                    return false;

                // make sure target exist
                cfg.$target = cfg.$target || (cfg.target==="self" ? $el : $(cfg.target));
                if (cfg.$target.length === 0) {
                    if (!cfg.target) {
                        log.error("Need target selector", cfg);
                        return false;
                    }
                    cfg.$target = _._createTarget(cfg.target);
                    cfg.$injected = cfg.$target;
                }
                return true;
            });
        },

        _extractModifiers: function(cfg) {
            var source_re = /^(.*?)(::element)?$/,
                target_re = /^(.*?)(::element)?(::after|::before)?$/,
                source_match = source_re.exec(cfg.source),
                target_match = target_re.exec(cfg.target),
                targetMod, targetPosition;

            // source content or element?
            cfg.source = source_match[1];
            // XXX: turn into source processor
            cfg.sourceMod = source_match[2] ? "element" : "content";

            // will be added while the ajax request is in progress
            cfg.targetLoadClasses = "injecting";

            // target content or element?
            targetMod = target_match[2] ? "element" : "content";
            cfg.target = target_match[1];
            cfg.targetLoadClasses += " injecting-" + targetMod;

            // position relative to target
            targetPosition = (target_match[3] || "::").slice(2);
            if (targetPosition)
                cfg.targetLoadClasses += " injecting-" + targetPosition;

            cfg.action = targetMod + targetPosition;

            // Once we start detecting illegal combinations, we'll
            // return false in case of error
            return true;
        },

        // create a target that matches the selector
        //
        // XXX: so far we only support #target and create a div with
        // that id appended to the body.
        _createTarget: function(selector) {
            var $target;
            if (selector.slice(0,1) !== "#") {
                log.error("only id supported for non-existing target");
                return null;
            }
            $target = $("
").attr({id: selector.slice(1)});
            $("body").append($target);
            return $target;
        },

        execute: function(cfgs, $el) {
            // get a kinda deep copy, we scribble on it
            cfgs = cfgs.map(function(cfg) {
                return $.extend({}, cfg);
            });

            // XXX: this need to get functional, returning a command
            // for internal use, instead of passing on cfgs
            if (!_.verifyConfig(cfgs, $el))
                return;

            // possibility for spinners on targets
            cfgs.forEach(function(cfg) {
                cfg.$target.addClass(cfg.targetLoadClasses);
            });

            var onSuccess = function(ev) {
                var data = ev && ev.jqxhr && ev.jqxhr.responseText;
                if (!data) {
                    log.warn("No response content, aborting", ev);
                    return;
                }

                var sources$ = _.callTypeHandler(cfgs[0].dataType, "sources", $el, [cfgs, data, ev]);

                cfgs.forEach(function(cfg, idx) {
                    var $source = sources$[idx];

                    if (cfg.sourceMod === "content")
                        $source = $source.contents();

                    // perform injection
                    cfg.$target.each(function() {
                        var $src;

                        // $source.clone() does not work with shived elements in IE8
                        if (document.all && document.querySelector &&
                            !document.addEventListener) {
                            $src = $source.map(function() {
                                return $(this.outerHTML)[0];
                            });
                        } else {
                            $src = $source.clone();
                        }

                        var $target = $(this),
                            $injected = cfg.$injected || $src;

                        $src.findInclusive('img').on('load', function() {
                            $(this).trigger('pat-inject-content-loaded');
                        });

                        if (_._inject($src, $target, cfg.action, cfg["class"])) {
                            $injected.filter(function() {
                                // setting data on textnode fails in IE8
                                return this.nodeType !== 3; //Node.TEXT_NODE
                            }).data("pat-injected", {origin: cfg.url});
                            $injected.addClass(cfg["class"])
                                .trigger("patterns-injected", cfg);
                        }
                        if ((cfg.history === "record") &&
                            ("pushState" in history))
                            history.pushState({url: cfg.url}, "", cfg.url);
                    });
                });

                if (cfgs[0].nextHref) {
                    $el.attr({href: (window.location.href.split("#")[0] || "") +
                              cfgs[0].nextHref});
                    _.destroy($el);

                    // XXX: this used to be the case, but I don't see
                    // why that would be a good idea.
                    //
                    // jump to new href target
                    //if (!$el.hasClass("autoLoading-visible"))
                    //    window.location.href = $el.attr('href');
                }
                $el.off("pat-ajax-success.pat-inject");
                $el.off("pat-ajax-error.pat-inject");
            };

            var onError = function() {
                cfgs.forEach(function(cfg) {
                    if ("$injected" in cfg)
                        cfg.$injected.remove();
                });
                $el.off("pat-ajax-success.pat-inject");
                $el.off("pat-ajax-error.pat-inject");
            };

            $el.on("pat-ajax-success.pat-inject", onSuccess);
            $el.on("pat-ajax-error.pat-inject", onError);

            ajax.request($el, {url: cfgs[0].url});
        },

        _inject: function($source, $target, action /* , classes */) {
            // action to jquery method mapping, except for "content"
            // and "element"
            var method = {
                contentbefore: "prepend",
                contentafter:  "append",
                elementbefore: "before",
                elementafter:  "after"
            }[action];

            if ($source.length === 0) {
                log.warn("Aborting injection, source not found:", $source);
                return false;
            }
            if ($target.length === 0) {
                log.warn("Aborting injection, target not found:", $target);
                return false;
            }

            if (action === "content")
                $target.empty().append($source);
            else if (action === "element")
                $target.replaceWith($source);
            else
                $target[method]($source);

            return true;
        },

        _sourcesFromHtml: function(html, url, sources) {
            var $html = _._parseRawHtml(html, url);
            return sources.map(function(source) {
                if (source === "body")
                    source = "#__original_body";

                var $source = $html.find(source);

                if ($source.length === 0)
                    log.warn("No source elements for selector:", source, $html);

                $source.find("a[href^=\"#\"]").each(function() {
                    var href = this.getAttribute("href");
                    // Skip in-document links pointing to an id that is inside
                    // this fragment.
                    if (href.length === 1)  // Special case for top-of-page links
                        this.href=url;
                    else if (!$source.find(href).length)
                        this.href=url+href;
                });

                return $source;
            });
        },

        _link_attributes: {
            A: "href",
            FORM: "action",
            IMG: "src",
            SOURCE: "src",
            VIDEO: "src"
        },

        _rebaseHTML_via_HTMLParser: function(base, html) {
            var output = [],
                i, link_attribute, value;

            htmlparser.HTMLParser(html, {
                start: function(tag, attrs, unary) {
                    output.push("<"+tag);
                    link_attribute = _._link_attributes[tag.toUpperCase()];
                    for (i=0; i<attrs.length; i++) {
                        if (attrs[i].name.toLowerCase() === link_attribute) {
                            value = attrs[i].value;
                            // Do not rewrite Zope views or in-document links.
                            // In-document links will be processed later after
                            // extracting the right fragment.
                            if (value.slice(0, 2) !== "@@" && value[0] !== "#") {
                                value = utils.rebaseURL(base, value);
                                value = value.replace(/(^|[^\\])"/g, '$1\\\"');
                            }
                        }  else
                            value = attrs[i].escaped;
                        output.push(" " + attrs[i].name + "=\"" + value + "\"");
                    }
                    output.push(unary ? "/>" : ">");
                },

                end: function(tag) {
                    output.push("</"+tag+">");
                },

                chars: function(text) {
                    output.push(text);
                },

                comment: function(text) {
                    output.push("");
                }
            });
            return output.join("");
        },

        _rebaseAttrs: {
            A: "href",
            FORM: "action",
            IMG: "data-pat-inject-rebase-src",
            SOURCE: "data-pat-inject-rebase-src",
            VIDEO: "data-pat-inject-rebase-src"
        },

        _rebaseHTML: function(base, html) {
            var $page = $(html.replace(
                /(\s)(src\s*)=/gi,
                '$1src="" data-pat-inject-rebase-$2='
            ).trim()).wrapAll('
').parent();

            $page.find(Object.keys(_._rebaseAttrs).join(',')).each(function() {
                var $this = $(this),
                    attrName = _._rebaseAttrs[this.tagName],
                    value = $this.attr(attrName);

                if (value && value.slice(0, 2) !== "@@" && value[0] !== "#") {
                    value = utils.rebaseURL(base, value);
                    $this.attr(attrName, value);
                }
            });
            // XXX: IE8 changes the order of attributes in html. The following
            // lines move data-pat-inject-rebase-src to src.
            $page.find('[data-pat-inject-rebase-src]').each(function() {
                var $el = $(this);
                $el.attr('src', $el.attr('data-pat-inject-rebase-src'))
                   .removeAttr('data-pat-inject-rebase-src');
            });

            return $page.html().replace(
                    /src="" data-pat-inject-rebase-/g, ''
                ).trim();
        },

        _parseRawHtml: function(html, url) {
            url = url || "";

            // remove script tags and head and replace body by a div
            var clean_html = html
                    .replace(/


text:
class AddGroupIdToUsersTable < ActiveRecord::Migration

  def up
    add_column :users, :group_id, :integer
    add_foreign_key :users, :groups, index: true
  end

  def down
    remove_foreign_key :users, :groups
    remove_column :users, :group_id
  end

end


text:
#ifndef HPL_MAC_ASYNC_H_INCLUDED
#define HPL_MAC_ASYNC_H_INCLUDED

#ifdef __cplusplus
extern "C" {
#endif

#include <compiler.h>
#include <utils.h>
#include <hpl_irq.h>
/**
 * \addtogroup hpl__mac__async__group MAC HPL APIs
 *
 *@{
 */

/**
 * \brief MAC device descriptor Forward declaration
 */
struct _mac_async_device;

/**
 * \brief MAC callback type
 *
 * \param[in] dev An MAC device descriptor
 */
typedef void (*_mac_async_cb_t)(struct _mac_async_device *const dev);

/**
 * \brief MAC callbacks
 */
struct _mac_async_callbacks {
	_mac_async_cb_t transmited; /*!< Frame received */
	_mac_async_cb_t received;   /*!< Frame transmited */
};

/**
 * \brief MAC device descriptor
 */
struct _mac_async_device {
	void *                      hw;  /*!< Hardware module instance handler */
	struct _mac_async_callbacks cb;  /*!< Callback handler */
	struct _irq_descriptor      irq; /*!< IRQ handler */
};

/**
 * \brief MAC callback types
 */
enum mac_async_cb_type {
	MAC_ASYNC_RECEIVE_CB, /*!< One or more frame been received */
	MAC_ASYNC_TRANSMIT_CB /*!< One or more frame been transmited */
};

struct mac_async_filter {
	uint8_t mac[6];     /*!< Destination address */
	uint8_t tid[2];     /*!< Type ID, 0x0600 IP package */
	bool    tid_enable; /*!< Enable TID matching */
};
/**
 * \brief Initialize the MAC driver
 *
 * Initialize the MAC driver
 *
 * \param[in] dev A MAC device descriptor to deinit
 * \param[in] hw  Hardware module instance
 *
 * \return Operation status.
 * \retval ERR_NONE Success.

 */
int32_t _mac_async_init(struct _mac_async_device *const dev, void *const hw);

/**
 * \brief Deinitialize the MAC driver
 *
 * Deinitialize the MAC driver
 *
 * \param[in] descr A MAC device descriptor to deinit
 *
 * \return Operation status.
 * \retval ERR_NONE Success.
 */
int32_t _mac_async_deinit(struct _mac_async_device *const dev);

/** \brief Enable the MAC
 *
 * Enable the MAC
 *
 * \param[in] dev Pointer to the HPL MAC device descriptor
 *
 * \return Operation status.
 * \retval ERR_NONE Success.
 */
int32_t _mac_async_enable(struct _mac_async_device *const dev);

/**
 * \brief Disable the MAC
 *
 * Disable the MAC
 *
 * \param[in] dev Pointer to the HPL MAC device descriptor
 *
 * \return Operation status.
 * \retval ERR_NONE Success.
 */
int32_t _mac_async_disable(struct _mac_async_device *const dev);

/**
 * \brief Write raw data to MAC
 *
 * Write raw data to MAC that will be transmitted.
 *
 * \param[in] dev Pointer to the HPL MAC device descriptor
 * \param[in] buffer Pointer to the data buffer
 * \param[in] length Length of the data buffer
 *
 * \return Operation status.
 * \retval ERR_NONE Success.
 */
int32_t _mac_async_write(struct _mac_async_device *const dev, uint8_t *buf, uint32_t len);

/**
 * \brief Read received raw data from MAC
 *
 * Read received raw data from MAC
 *
 * \param[in] dev    Pointer to the HPL MAC device descriptor
 * \param[in] buffer Pointer to the data buffer. if pointer is null, then the
 *                   frame will be discard.
 * \param[in] length Max Length of the data buffer to be read. if len is zero,
 *                   then the frame will be discard
 *
 * \return Number of bytes that have been received
 */
uint32_t _mac_async_read(struct _mac_async_device *const dev, uint8_t *buf, uint32_t len);

/**
 * \brief Get next valid package length
 *
 * Get next valid package length from MAC. Application can use this function
 * to fetch the length of the next package, malloc the a buffer whit this
 * length, and then invoke mac_async_read to read out the package data.
 *
 * \param[in] dev Pointer to the HPL MAC device descriptor.
 *
 * \return Number of bytes that next package can be read.
 */
uint32_t _mac_async_read_len(struct _mac_async_device *const dev);

/**
 * \brief Enable the MAC IRQ
 *
 * Enable the MAC IRQ
 *
 * \param[in] dev Pointer to the HPL MAC device descriptor
 */
void _mac_async_enable_irq(struct _mac_async_device *const dev);

/**
 * \brief Disable the MAC IRQ
 *
 * Disable the MAC IRQ
 *
 * \param[in] dev Pointer to the HPL MAC device descriptor
 */
void _mac_async_disable_irq(struct _mac_async_device *const dev);

/**
 * \brief Register the MAC callback
 *
 * Register the MAC callback
 *
 * \param[in] dev  Pointer to the HPL MAC device descriptor
 * \param[in] type Callback function type.
 * \param[in] func A callback function, passing NULL will de-register
 *
 * \return Operation status.
 * \retval ERR_NONE Success.
 */
int32_t _mac_async_register_callback(struct _mac_async_device *const dev, const enum mac_async_cb_type type,
                                     const FUNC_PTR func);

/**
 * \brief Set MAC filter
 *
 * Set MAC filter, ethernet frames which match the filter will be received.
 *
 * \param[in] dev    Pointer to the HPL MAC device descriptor
 * \param[in] index  Mac filter index, start from 0, max value depends on
 *                   hardware specificaions.
 * \param[in] filter Pointer to the filter descriptor.
 *
 * \return Operation status.
 * \retval ERR_NONE Success.
 */
int32_t _mac_async_set_filter(struct _mac_async_device *const dev, uint8_t index, struct mac_async_filter *filter);

/**
 * \brief Set MAC filter (expaneded)
 *
 * Set MAC filter, ethernet frames which match the filter will be received.
 *
 * \param[in] dev Pointer to the HPL MAC device descriptor
 * \param[in] mac Mac address
 *
 * \return Operation status.
 * \retval ERR_NONE Success.
 */
int32_t _mac_async_set_filter_ex(struct _mac_async_device *const dev, uint8_t mac[6]);

/**
 * \brief Write PHY register
 *
 * Write PHY register
 *
 * \param[in] dev Pointer to the HPL MAC device descriptor
 * \param[in] addr  PHY address.
 * \param[in] reg   Register address.
 * \param[in] data  Register value.
 *
 * \return Operation status.
 * \retval ERR_NONE Success.
 */
int32_t _mac_async_write_phy_reg(struct _mac_async_device *const dev, uint16_t addr, uint16_t reg, uint16_t data);

/**
 * \brief Read PHY register
 *
 * Read PHY register
 *
 * \param[in] dev Pointer to the HPL MAC device descriptor
 * \param[in] addr  PHY address.
 * \param[in] reg   Register address.
 * \param[in] data  Register value.
 *
 * \return Operation status.
 * \retval ERR_NONE Success.
 */
int32_t _mac_async_read_phy_reg(struct _mac_async_device *const dev, uint16_t addr, uint16_t reg, uint16_t *val);

/** @} */

#ifdef __cplusplus
}
#endif

#endif /* HPL_MAC_ASYNC_H_INCLUDED */


text:
from moto.core.exceptions import RESTError

EXCEPTION_RESPONSE = """<?xml version="1.0"?>
<ErrorResponse xmlns="http://elasticache.amazonaws.com/doc/2015-02-02/">
  <Error>
    <Type>Sender</Type>
    <Code>{{ error_type }}</Code>
    <Message>{{ message }}</Message>
  </Error>
  <{{ request_id_tag }}>30c0dedb-92b1-4e2b-9be4-1188e3ed86ab</{{ request_id_tag }}>
</ErrorResponse>"""

class ElastiCacheException(RESTError):

    code = 400

    def __init__(self, *args, **kwargs):
        kwargs.setdefault("template", "ecerror")
        self.templates["ecerror"] = EXCEPTION_RESPONSE
        super().__init__(*args, **kwargs)

class PasswordTooShort(ElastiCacheException):

    code = 404

    def __init__(self, **kwargs):
        super().__init__(
            "InvalidParameterValue",
            message="Passwords length must be between 16-128 characters.",
            **kwargs,
        )

class PasswordRequired(ElastiCacheException):

    code = 404

    def __init__(self, **kwargs):
        super().__init__(
            "InvalidParameterValue",
            message="No password was provided. If you want to create/update the user without password, please use the NoPasswordRequired flag.",
            **kwargs,
        )

class UserAlreadyExists(ElastiCacheException):

    code = 404

    def __init__(self, **kwargs):
        super().__init__(
            "UserAlreadyExists", message="User user1 already exists.", **kwargs
        )

class UserNotFound(ElastiCacheException):

    code = 404

    def __init__(self, user_id, **kwargs):
        super().__init__("UserNotFound", message=f"User {user_id} not found.", **kwargs)


text:
package org.apache.hadoop.yarn.server.resourcemanager.scheduler.event;

import org.apache.hadoop.yarn.api.records.ApplicationId;
import org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMAppState;

public class AppRemovedSchedulerEvent extends SchedulerEvent {

  private final ApplicationId applicationId;
  private final RMAppState finalState;

  public AppRemovedSchedulerEvent(ApplicationId applicationId,
      RMAppState finalState) {
    super(SchedulerEventType.APP_REMOVED);
    this.applicationId = applicationId;
    this.finalState = finalState;
  }

  public ApplicationId getApplicationID() {
    return this.applicationId;
  }

  public RMAppState getFinalState() {
    return this.finalState;
  }
}


text:
module A {

    class Point {
        x: number;
        y: number;
    }

    export class points {

        [idx: number]: Point;
        [idx: string]: Point;
    }
}

//// [ExportClassWithInaccessibleTypeInIndexerTypeAnnotations.js]
var A;
(function (A) {
    var Point = (function () {
        function Point() {
        }
        return Point;
    })();
    var points = (function () {
        function points() {
        }
        return points;
    })();
    A.points = points;
})(A || (A = {}));


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

<title>Text2</title>
<meta name="date" content="2013-03-07">
<link rel="stylesheet" type="text/css" href="../../../../stylesheet.css" title="Style">

JavaScript is disabled on your browser.

Overview

Package

Class

Use

Tree

Deprecated

Index

Help

Prev Class

Next Class

Frames

No Frames

All Classes

Summary:

Nested |

Field |

Constr |

Method

Detail:

Field |

Constr |

Method

com.tyrlib2.graphics.renderables

Class Text2

java.lang.Object

com.tyrlib2.graphics.scene.SceneObject

com.tyrlib2.graphics.renderables.Text2

All Implemented Interfaces:

IRenderable

public class Text2
extends SceneObject
implements IRenderable

Constructor Summary

Constructors

Constructor and Description

Text2(java.lang.String text,
     int rotation,
     Color color,
     Font font)

Method Summary

Methods

Modifier and Type
Method and Description

Color
getColor()

int
getRotation()

java.lang.String
getText()

void
render(float[] vpMatrix)

Renders this object the matrix containing projection and view will be passed

void
setColor(Color color)

void
setRotation(int rotation)

void
setText(java.lang.String text)

Methods inherited from class com.tyrlib2.graphics.scene.SceneObject

attachTo, detach, getAbsolutePos, getModelMatrix, getParent, getRelativePos, onTransformed

Methods inherited from class java.lang.Object

equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail

Text2

public Text2(java.lang.String text,
     int rotation,
     Color color,
     Font font)

Method Detail

render

public void render(float[] vpMatrix)

Description copied from interface: IRenderable

Renders this object the matrix containing projection and view will be passed

Specified by:

render in interface IRenderable

getColor

public Color getColor()

setColor

public void setColor(Color color)

getText

public java.lang.String getText()

setText

public void setText(java.lang.String text)

getRotation

public int getRotation()

setRotation

public void setRotation(int rotation)

Overview

Package

Class

Use

Tree

Deprecated

Index

Help

Prev Class

Next Class

Frames

No Frames

All Classes

Summary:

Nested |

Field |

Constr |

Method

Detail:

Field |

Constr |

Method


text:
set -e

log() {
  echo "[$(date +%F_%H:%M:%S)] - " $@
}

SECRET_FILE=/root/secret-aws-ecr.yml

# AWS ECR Login
log "Executing AWS ECR Login"
$(aws ecr get-login --no-include-email)
log "New AWS ECR token generated."

# Replace secret value
dockerconfigjson_base64=`echo -n "$(cat /root/.docker/config.json)" | base64 | tr -d '\n'`
echo -n \
"---
kind: Secret
apiVersion: v1
metadata:
  name: aws-ecr
data:
  .dockerconfigjson: $dockerconfigjson_base64
type: kubernetes.io/dockerconfigjson
" \
> $SECRET_FILE

log "Login as system:admin ... " && oc login -u system:admin || log "Unable to connect to Openshift cluster"
oc version

log "Creating/Updating secrets and configure service account on Openshift"

PROJECTS=$(oc get projects --no-headers | awk '{print $1}')

SAs=(default deployer builder)

for project in $PROJECTS; do
  log "Project: $project"
  found=$(oc get secrets -n $project | grep aws-ecr | wc -l)
  if [[ $found -eq 0 ]]; then
    oc create -f /root/secret-aws-ecr.yml -n $project
    log "Created secret aws-ecr"
    for sa in ${SAs[@]};do
      oc secrets add serviceaccount/$sa secrets/aws-ecr --for=pull -n $project
      log "Added secret to serviceaccount $sa"
    done
  else
    oc replace -f /root/secret-aws-ecr.yml -n $project
    log "Updated secret aws-ecr"
  fi
done


text:
from django.core import cache

def expireTemplateCache(fragment_name, *variables):
    # At this point you should probably just call
    # django.core.cache.utils.make_template_fragment_key directly, but for
    # compatibility we include the following convenience method.
    key = cache.utils.make_template_fragment_key(fragment_name, variables)
    cache.cache.delete(key)

def authStatus(user):
    if 'is_authenticated' in dir(user):
        return user.is_authenticated()
    else:
        return str(user)


text:
package com.vvishnoi.classified.model;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;

@Entity
@Table(name = "sub_categories")
public class SubCategory extends NamedEntity {

	public SubCategory() {
	}

	@JsonBackReference(value="category-subcategory")
	@ManyToOne
	@JoinColumn(name = "category_id")
	private Category category;

	@JsonManagedReference(value="item-subcategory")
	@OneToMany(mappedBy = "subCategory", cascade=CascadeType.ALL,  fetch=FetchType.LAZY)
	private Set<Item> items;

	@Column(name = "adult")
	private boolean adult;

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public boolean isAdult() {
		return adult;
	}

	public void setAdult(boolean adult) {
		this.adult = adult;
	}

	public Set<Item> getItems() {
		return items;
	}

	public void setItems(Set<Item> items) {
		this.items = items;
	}

}


text:
<?php

class P3WidgetTranslationController extends Controller
{
    #public $layout='//layouts/column2';
    public $defaultAction = "admin";
    public $scenario = "crud";

public function filters() {
	return array(
			'accessControl',
			);
}

public function accessRules() {
	return array(
			array('allow',
				'actions'=>array('create','ajaxUpdate','update','delete','admin','view'),
				'roles'=>array('P3widgets.P3WidgetTranslation.*'),
				),
			array('deny',
				'users'=>array('*'),
				),
			);
}

    public function beforeAction($action){
        parent::beforeAction($action);
        // map identifcationColumn to id
        if (!isset($_GET['id']) && isset($_GET['id'])) {
            $model=P3WidgetTranslation::model()->find('id = :id', array(
            ':id' => $_GET['id']));
            if ($model !== null) {
                $_GET['id'] = $model->id;
            } else {
                throw new CHttpException(400);
            }
        }
        if ($this->module !== null) {
            $this->breadcrumbs[$this->module->Id] = array('/'.$this->module->Id);
        }
        return true;
    }

    public function actionView($id)
    {
        $model = $this->loadModel($id);
        $this->render('view',array('model' => $model,));
    }

    public function actionCreate()
    {
        $model = new P3WidgetTranslation;
        $model->scenario = $this->scenario;

                $this->performAjaxValidation($model, 'p3-widget-translation-form');

        if(isset($_POST['P3WidgetTranslation'])) {
            $model->attributes = $_POST['P3WidgetTranslation'];

            try {
                if($model->save()) {
                    if (isset($_GET['returnUrl'])) {
                        $this->redirect($_GET['returnUrl']);
                    } else {
                        $this->redirect(array('view','id'=>$model->id));
                    }
                }
            } catch (Exception $e) {
                $model->addError('id', $e->getMessage());
            }
        } elseif(isset($_GET['P3WidgetTranslation'])) {
            $model->attributes = $_GET['P3WidgetTranslation'];
        }

        $this->render('create',array( 'model'=>$model));
    }

    public function actionUpdate($id)
    {
        $model = $this->loadModel($id);
        $model->scenario = $this->scenario;

                $this->performAjaxValidation($model, 'p3-widget-translation-form');

        if(isset($_POST['P3WidgetTranslation']))
        {
            $model->attributes = $_POST['P3WidgetTranslation'];

            try {
                if($model->save()) {
                    if (isset($_GET['returnUrl'])) {
                        $this->redirect($_GET['returnUrl']);
                    } else {
                        $this->redirect(array('view','id'=>$model->id));
                    }
                }
            } catch (Exception $e) {
                $model->addError('id', $e->getMessage());
            }
        }

        $this->render('update',array('model'=>$model,));
    }

    public function actionEditableSaver()
    {
        Yii::import('EditableSaver'); //or you can add import 'ext.editable.*' to config
        $es = new EditableSaver('P3WidgetTranslation');  // classname of model to be updated
        $es->update();
    }

    public function actionDelete($id)
    {
        if(Yii::app()->request->isPostRequest)
        {
            try {
                $this->loadModel($id)->delete();
            } catch (Exception $e) {
                throw new CHttpException(500,$e->getMessage());
            }

            if(!isset($_GET['ajax']))
            {
                if (isset($_GET['returnUrl'])) {
                    $this->redirect($_GET['returnUrl']);
                } else {
                    $this->redirect(array('admin'));
                }
            }
        }
        else
            throw new CHttpException(400,Yii::t('P3WidgetsModule.crud', 'Invalid request. Please do not repeat this request again.'));
    }

    public function actionIndex()
    {
        $dataProvider=new CActiveDataProvider('P3WidgetTranslation');
        $this->render('index',array('dataProvider'=>$dataProvider,));
    }

    public function actionAdmin()
    {
        $model=new P3WidgetTranslation('search');
        $model->unsetAttributes();

        if(isset($_GET['P3WidgetTranslation'])) {
            $model->attributes = $_GET['P3WidgetTranslation'];
        }

        $this->render('admin',array('model'=>$model,));
    }

    public function loadModel($id)
    {
        $model=P3WidgetTranslation::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,Yii::t('P3WidgetsModule.crud', 'The requested page does not exist.'));
        return $model;
    }

    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='p3-widget-translation-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}


text:
#include <scenegraph/output/display/display.h>

using namespace motorcar;

Display::Display(OpenGLContext *glContext, glm::vec2 displayDimensions, PhysicalNode *parent, const glm::mat4 &transform)
    :PhysicalNode(parent, transform)
    ,m_glContext(glContext)
    ,m_dimensions(displayDimensions)

{
    m_glContext->makeCurrent();

    glm::ivec2 res = this->size();

    glGenFramebuffers(1, &m_scratchFrameBuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, m_scratchFrameBuffer);

    glGenTextures(1, &m_scratchColorBufferTexture);
    glBindTexture(GL_TEXTURE_2D, m_scratchColorBufferTexture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, res.x, res.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_scratchColorBufferTexture, 0);

    glGenTextures(1, &m_scratchDepthBufferTexture);
    glBindTexture(GL_TEXTURE_2D, m_scratchDepthBufferTexture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, res.x, res.y, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, 0);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, m_scratchDepthBufferTexture, 0);

    std::cout << "Checking Scratch Framebuffer:" << std::endl;
    switch(glCheckFramebufferStatus(GL_FRAMEBUFFER)){
            case(GL_FRAMEBUFFER_COMPLETE):
                std::cout << "Framebuffer Complete" << std::endl;
                break;
            case(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT):
                std::cout << "Framebuffer Attachment Incomplete" << std::endl;
                break;
            case(GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT):
                std::cout << "Framebuffer Attachment Incomplete/Missing" << std::endl;
                break;
            case(GL_FRAMEBUFFER_UNSUPPORTED):
                std::cout << "Framebuffer Unsupported" << std::endl;
                break;
            default:
                std::cout << "Framebuffer is Incomplete for Unknown Reasons" << std::endl;
                break;
    }
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

}

Display::~Display()

{
}

void Display::prepareForDraw()
{
   //std::cout << "display size: " << this->resolution().x << ", " << this->resolution().y <<std::endl;
//    Geometry::printMatrix(worldTransform());

//    for(ViewPoint *viewpoint : viewpoints()){
//        viewpoint->updateViewMatrix();
//    }
    glContext()->makeCurrent();
    //glClearColor(.7f, .85f, 1.f, 1.0f);
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClearStencil(0.0);
    glStencilMask(0xFF);
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    glEnable(GL_BLEND);
    glBlendFunc (GL_ONE,GL_ONE_MINUS_SRC_ALPHA);
}

void Display::addViewpoint(ViewPoint *v)
{
    m_viewpoints.push_back(v);
}

std::vector<ViewPoint *> Display::viewpoints() const
{
    return m_viewpoints;
}

glm::vec2 Display::dimensions() const
{
    return m_dimensions;
}

Geometry::Ray Display::worldRayAtDisplayPosition(glm::vec2 pixel)
{
    ViewPoint *cam = viewpoints().front();
//    glm::vec3 cameraCenterOfFocus(cam->worldTransform() * cam->centerOfFocus());
//    glm::vec3 pixelPos = worldPositionAtDisplayPosition(pixel);
//    Geometry::printVector(pixelPos);
//    std::cout << std::endl;

//    Geometry::Ray ray(cameraCenterOfFocus, glm::normalize(pixelPos - cameraCenterOfFocus));
//    //ray.print();
//    return ray;
    return cam->worldRayAtDisplayPosition(pixel.x, pixel.y);
}

glm::vec3 Display::worldPositionAtDisplayPosition(glm::vec2 pixel)
{
//    Geometry::printVector(glm::vec3(pixel, 0));
//    Geometry::printVector(glm::vec3(resolution(), 0));
//    Geometry::printVector(glm::vec3(size(), 0));

    return glm::vec3(worldTransform() * glm::vec4(((pixel / glm::vec2(size())) - glm::vec2(0.5)) * dimensions(), 0 , 1));
}

glm::ivec2 Display::size()
{
    return glContext()->defaultFramebufferSize();
}

OpenGLContext *Display::glContext() const
{
    return m_glContext;
}

void Display::setGlContext(OpenGLContext *glContext)
{
    m_glContext = glContext;
}

GLuint Display::scratchFrameBuffer() const
{
    return m_scratchFrameBuffer;
}
GLuint Display::scratchColorBufferTexture() const
{
    return m_scratchColorBufferTexture;
}
GLuint Display::scratchDepthBufferTexture() const
{
    return m_scratchDepthBufferTexture;
}


text:
#ifdef _MSC_VER
#pragma comment(linker, "/SUBSYSTEM:CONSOLE")
#ifdef IVW_ENABLE_MSVC_MEM_LEAK_TEST
#include <vld.h>
#endif
#endif

#include <inviwo/testutil/configurablegtesteventlistener.h>

#include <inviwo/core/datastructures/representationutil.h>
#include <inviwo/core/datastructures/representationfactorymanager.h>

#include <warn/push>
#include <warn/ignore/all>
#include <gtest/gtest.h>
#include <warn/pop>

using namespace inviwo;

int main(int argc, char** argv) {
    RepresentationFactoryManager rfm;
    util::registerCoreRepresentations(rfm);

    int ret = -1;
    {

#ifdef IVW_ENABLE_MSVC_MEM_LEAK_TEST
        VLDDisable();
        ::testing::InitGoogleTest(&argc, argv);
        VLDEnable();
#else
        ::testing::InitGoogleTest(&argc, argv);
#endif
        ConfigurableGTestEventListener::setup();
        ret = RUN_ALL_TESTS();
    }

    return ret;
}


text:
__version__ = "0.8.0"

import os, sys
import re
from fractions import gcd
import inspect
import traceback
import datetime
import importlib
from functools import reduce

from numpy import dot, array

from tilde.core.common import u, is_binary_string, generate_cif, html_formula, hrsize
from tilde.core.symmetry import SymmetryHandler
from tilde.core.settings import BASE_DIR, settings, virtualize_path, get_hierarchy
from tilde.core.electron_structure import ElectronStructureError
from tilde.parsers import Output
import tilde.core.model as model

from ase.data import chemical_symbols
from ase.geometry import cell_to_cellpar
from sqlalchemy import exists, func
from sqlalchemy.orm.exc import NoResultFound

import ujson as json
import six

class API:
    version = __version__
    __shared_state = {}
    formula_sequence = ['Fr','Cs','Rb','K','Na','Li',  'Be','Mg','Ca','Sr','Ba','Ra',  'Sc','Y','La','Ce','Pr','Nd','Pm','Sm','Eu','Gd','Tb','Dy','Ho','Er','Tm','Yb',  'Ac','Th','Pa','U','Np','Pu',  'Ti','Zr','Hf',  'V','Nb','Ta',  'Cr','Mo','W',  'Fe','Ru','Os',  'Co','Rh','Ir',  'Mn','Tc','Re',  'Ni','Pd','Pt',  'Cu','Ag','Au',  'Zn','Cd','Hg',  'B','Al','Ga','In','Tl',  'Pb','Sn','Ge','Si','C',   'N','P','As','Sb','Bi',   'H',   'Po','Te','Se','S','O',  'At','I','Br','Cl','F',  'He','Ne','Ar','Kr','Xe','Rn']

    def __init__(self, settings=settings):
        self.settings = settings

        # Default hierarchy is set in the file init-data.sql
        # Conventionally, the hierarchy values are set by hexadecimal numbers (with leading 0x)
        self.hierarchy, self.hierarchy_groups, self.hierarchy_values = get_hierarchy(settings)

        # *parser API*
        # Subfolder "parsers" contains directories with parsers.
        # Parser will be active if:
        # (1) its class defines a fingerprints method
        # (2) it is enabled by its manifest file
        # (3) its filename repeats the name of parser folder
        All_parsers, self.Parsers = {}, {}
        for parsername in os.listdir( os.path.realpath(BASE_DIR + '/../parsers') ):
            if self.settings.get('no_parse'): continue
            if not os.path.isfile( os.path.realpath(BASE_DIR + '/../parsers') + '/' + parsername + '/manifest.json' ): continue
            if not os.path.isfile( os.path.realpath(BASE_DIR + '/../parsers') + '/' + parsername + '/' + parsername + '.py' ):
                raise RuntimeError('Parser API Error: Parser code for ' + parsername + ' is missing!')
            try: parsermanifest = json.loads( open( os.path.realpath(BASE_DIR + '/../parsers') + '/' + parsername + '/manifest.json' ).read() )
            except: raise RuntimeError('Parser API Error: Parser manifest for ' + parsername + ' has corrupted format!')

            if (not 'enabled' in parsermanifest or not parsermanifest['enabled']) and not self.settings['debug_regime']: continue

            All_parsers[parsername] = importlib.import_module('tilde.parsers.' + parsername + '.' + parsername) # all imported modules will be stored here

        # replace modules by classes and check *fingerprints* method
        for parser, module in All_parsers.items():
            for name, cls in inspect.getmembers(module):
                if inspect.isclass(cls):
                    if inspect.isclass(cls) and hasattr(cls, 'fingerprints'):
                        self.Parsers[cls.__name__] = cls

        # *module API*
        # Tilde module (app) is a subfolder (%appfolder%) of apps folder
        # contains manifest.json and %appfolder%.py files
        # the following tags in manifest.json matter:
        # ((*onprocess* - invoking during processing: therefore %appfolder%.py must provide the class %Appfolder%))
        # *appcaption* - module caption (used as column caption in GUI data table & as atomic structure rendering pane overlay caption)
        # *appdata* - a new property defined by app
        # *apptarget* - conditions on whether an app should be executed, based on hierarchy values
        # *on3d* - app provides the data which may be shown in GUI on atomic structure rendering pane (used only by make3d of daemon.py)
        # *plottable* - column provided may be plotted in GUI
        # NB. GUI (has_column) is supported only if the class %Appfolder% defines cell_wrapper
        self.Apps = {}
        n = 1
        for appname in os.listdir( os.path.realpath(BASE_DIR + '/../apps') ):
            if self.settings.get('no_parse'): continue
            if os.path.isfile( os.path.realpath(BASE_DIR + '/../apps') + '/' + appname + '/manifest.json' ):
                try: appmanifest = json.loads( open( os.path.realpath(BASE_DIR + '/../apps') + '/' + appname + '/manifest.json' ).read() )
                except: raise RuntimeError('Module API Error: Module manifest for ' + appname + ' has corrupted format!')

                # tags processing
                if not 'appdata' in appmanifest: raise RuntimeError('Module API Error: no appdata tag for ' + appname + '!')
                if 'onprocess' in appmanifest:
                    try: app = __import__('tilde.apps.' + appname + '.' + appname, fromlist=[appname.capitalize()]) # this means: from foo import Foo
                    except ImportError:
                        raise RuntimeError('Module API Error: module ' + appname + ' is invalid or not found!')
                    self.Apps[appname] = {'appmodule': getattr(app, appname.capitalize()), 'appdata': appmanifest['appdata'], 'apptarget': appmanifest.get('apptarget', None), 'appcaption': appmanifest['appcaption'], 'on3d': appmanifest.get('on3d', 0)}

                    # compiling table columns:
                    if hasattr(self.Apps[appname]['appmodule'], 'cell_wrapper'):
                        self.hierarchy.append( {'cid': (2000+n), 'category': appmanifest['appcaption'], 'sort': (2000+n), 'has_column': True, 'cell_wrapper': getattr(self.Apps[appname]['appmodule'], 'cell_wrapper')}  )
                        if appmanifest.get('plottable', False): self.hierarchy[-1].update({'plottable': 1})
                        n += 1

        self.hierarchy = sorted( self.hierarchy, key=lambda x: x['sort'] )

        # *connector API*
        # Every connector implements reading methods:
        # *list* (if applicable) and *report* (obligatory)
        self.Conns = {}
        for connectname in os.listdir( os.path.realpath(BASE_DIR + '/../connectors') ):
            if connectname.endswith('.py') and connectname != '__init__.py':
                connectname = connectname[0:-3]
                conn = importlib.import_module('tilde.connectors.' + connectname) # this means: from foo import Foo
                self.Conns[connectname] = {'list': getattr(conn, 'list'), 'report': getattr(conn, 'report')}

        # *hierarchy API*
        # This is used for classification
        self.Classifiers = []
        for classifier in os.listdir( os.path.realpath(BASE_DIR + '/../classifiers') ):
            if self.settings.get('no_parse'): continue
            if classifier.endswith('.py') and classifier != '__init__.py':
                classifier = classifier[0:-3]
                obj = importlib.import_module('tilde.classifiers.' + classifier) # this means: from foo import Foo
                if getattr(obj, '__order__') is None: raise RuntimeError('Classifier %s has not defined an order to apply!' % classifier)

                self.Classifiers.append({
                    'classify': getattr(obj, 'classify'),\
                    'order': getattr(obj, '__order__'),\
                    'class': classifier})
                self.Classifiers = sorted(self.Classifiers, key = lambda x: x['order'])

    def assign_parser(self, name):
        '''
        Restricts parsing
        **name** is a name of the parser class
        NB: this is the PUBLIC method
        @procedure
        '''
        for n, p in list(self.Parsers.items()):
            if n != name:
                del self.Parsers[n]
        if len(self.Parsers) != 1: raise RuntimeError('Parser cannot be assigned!')

    def formula(self, atom_sequence):
        '''
        Constructs standardized chemical formula
        NB: this is the PUBLIC method
        @returns formula_str
        '''
        labels = {}
        types = []
        y = 0
        for k, atomi in enumerate(atom_sequence):
            lbl = re.sub("[0-9]+", "", atomi).capitalize()
            if lbl not in labels:
                labels[lbl] = y
                types.append([k+1])
                y += 1
            else:
                types[ labels[lbl] ].append(k+1)
        atoms = list(labels.keys())
        atoms = [x for x in self.formula_sequence if x in atoms] + [x for x in atoms if x not in self.formula_sequence] # accordingly
        formula = ''
        for atom in atoms:
            n = len(types[labels[atom]])
            if n==1: n = ''
            else: n = str(n)
            formula += atom + n
        return formula

    def count(self, session):
        return session.query(func.count(model.Calculation.checksum)).one()[0]

    def savvyize(self, input_string, recursive=False, stemma=False):
        '''
        Determines which files should be processed
        NB: this is the PUBLIC method
        @returns filenames_list
        '''
        input_string = os.path.abspath(input_string)
        tasks = []

        restricted = [ symbol for symbol in self.settings['skip_if_path'] ] if self.settings['skip_if_path'] else []

        # given folder
        if os.path.isdir(input_string):
            if recursive:
                for root, dirs, files in os.walk(input_string): # beware of broken links on unix! (NB find ~ -type l -exec rm -f {} \;)
                    # skip_if_path directive
                    to_filter = []
                    for dir in dirs:
                        dir = u(dir)
                        for rs in restricted:
                            if dir.startswith(rs) or dir.endswith(rs):
                                to_filter.append(dir)
                                break
                    dirs[:] = [x for x in dirs if x not in to_filter]
                    for filename in files:
                        # skip_if_path directive
                        filename = u(filename)
                        if restricted:
                            for rs in restricted:
                                if filename.startswith(rs) or filename.endswith(rs): break
                            else: tasks.append(root + os.sep + filename)
                        else: tasks.append(root + os.sep + filename)
            else:
                for filename in os.listdir(input_string):
                    filename = u(filename)
                    if os.path.isfile(input_string + os.sep + filename):
                        # skip_if_path directive
                        if restricted:
                            for rs in restricted:
                                if filename.startswith(rs) or filename.endswith(rs): break
                            else: tasks.append(input_string + os.sep + filename)
                        else: tasks.append(input_string + os.sep + filename)

        # given full filename
        elif os.path.isfile(input_string):
            tasks.append(input_string) # skip_if_path directive is not applicable here

        # given filename stemma
        else:
            if stemma:
                parent = os.path.dirname(input_string)
                for filename in os.listdir(parent):
                    filename = u(filename)
                    if input_string in parent + os.sep + filename and not os.path.isdir(parent + os.sep + filename):
                        # skip_if_path directive
                        if restricted:
                            for rs in restricted:
                                if filename.startswith(rs) or filename.endswith(rs): break
                            else: tasks.append(parent + os.sep + filename)
                        else: tasks.append(parent + os.sep + filename)
        return tasks

    def _parse(self, parsable, parser_name):
        '''
        Low-level parsing
        NB: this is the PRIVATE method
        @returns tilde_obj, error
        '''
        calc, error = None, None
        try:
            for calc in self.Parsers[parser_name].iparse(parsable):
                yield calc, None
            return
        except RuntimeError as e: error = "routine %s parser error in %s: %s" % ( parser_name, parsable, e )
        except:
            exc_type, exc_value, exc_tb = sys.exc_info()
            error = "unexpected %s parser error in %s:\n %s" % ( parser_name, parsable, "".join(traceback.format_exception( exc_type, exc_value, exc_tb )) )
        yield None, error

    def parse(self, parsable):
        '''
        High-level parsing:
        determines the data format
        and combines parent-children outputs
        NB: this is the PUBLIC method
        @returns tilde_obj, error
        '''
        calc, error = None, None
        try:
            f = open(parsable, 'rb')
            if is_binary_string(f.read(2048)):
                yield None, 'was read (binary data)...'
                return
            f.close()
        except IOError:
            yield None, 'read error!'
            return
        f = open(parsable, 'r', errors='surrogateescape') if six.PY3 else open(parsable, 'r') # open the file once again with right mode
        f.seek(0)
        i, detected = 0, False
        while not detected:
            if i>700: break # criterion: parser must detect its working format in first N lines of output
            fingerprint = f.readline()
            if not fingerprint: break
            for name, Parser in self.Parsers.items():
                if Parser.fingerprints(fingerprint):
                    for calc, error in self._parse(parsable, name):
                        detected = True
                        # check if we parsed something reasonable
                        if not error and calc:

                            if not len(calc.structures) or not len(calc.structures[-1]): error = 'Valid structure is not present!'

                            if calc.info['finished'] == 0x1: calc.warning( 'This calculation is not correctly finished!' )

                            if not calc.info['H']: error = 'XC potential is not present!'

                        yield calc, error

                    if detected: break
            i += 1
        f.close()

        # unsupported data occured
        if not detected: yield None, 'was read...'

    def classify(self, calc, symprec=None):
        '''
        Reasons on normalization, invokes hierarchy API and prepares calc for saving
        NB: this is the PUBLIC method
        @returns tilde_obj, error
        '''
        error = None
        symbols = calc.structures[-1].get_chemical_symbols()
        calc.info['formula'] = self.formula(symbols)
        calc.info['cellpar'] = cell_to_cellpar(calc.structures[-1].cell).tolist()
        if calc.info['input']:
            try: calc.info['input'] = str(calc.info['input'], errors='ignore')
            except: pass

        # applying filter: todo
        if (calc.info['finished'] == 0x1 and self.settings['skip_unfinished']) or \
           (not calc.info['energy'] and self.settings['skip_notenergy']):
            return None, 'data do not satisfy the active filter'

        # naive elements extraction
        fragments = re.findall(r'([A-Z][a-z]?)(\d*[?:.\d+]*)?', calc.info['formula'])
        for i in fragments:
            if i[0] == 'X': continue
            calc.info['elements'].append(i[0])
            calc.info['contents'].append(int(i[1])) if i[1] else calc.info['contents'].append(1)

        # extend hierarchy with modules
        for C_obj in self.Classifiers:
            try: calc = C_obj['classify'](calc)
            except:
                exc_type, exc_value, exc_tb = sys.exc_info()
                error = "Fatal error during classification:\n %s" % "".join(traceback.format_exception( exc_type, exc_value, exc_tb ))
                return None, error

        # chemical ratios
        if not len(calc.info['standard']):
            if len(calc.info['elements']) == 1: calc.info['expanded'] = 1
            if not calc.info['expanded']: calc.info['expanded'] = reduce(gcd, calc.info['contents'])
            for n, i in enumerate([x//calc.info['expanded'] for x in calc.info['contents']]):
                if i==1: calc.info['standard'] += calc.info['elements'][n]
                else: calc.info['standard'] += calc.info['elements'][n] + str(i)
        if not calc.info['expanded']: del calc.info['expanded']
        calc.info['nelem'] = len(calc.info['elements'])
        if calc.info['nelem'] > 13: calc.info['nelem'] = 13
        calc.info['natom'] = len(symbols)

        # periodicity
        if calc.info['periodicity'] == 0: calc.info['periodicity'] = 0x4
        elif calc.info['periodicity'] == -1: calc.info['periodicity'] = 0x5

        # general calculation type reasoning
        if (calc.structures[-1].get_initial_charges() != 0).sum(): calc.info['calctypes'].append(0x4) # numpy count_nonzero implementation
        if (calc.structures[-1].get_initial_magnetic_moments() != 0).sum(): calc.info['calctypes'].append(0x5)
        if calc.phonons['modes']: calc.info['calctypes'].append(0x6)
        if calc.phonons['ph_k_degeneracy']: calc.info['calctypes'].append(0x7)
        if calc.phonons['dielectric_tensor']: calc.info['calctypes'].append(0x8) # CRYSTAL-only!
        if len(calc.tresholds) > 1:
            calc.info['calctypes'].append(0x3)
            calc.info['optgeom'] = True
        if calc.electrons['dos'] or calc.electrons['bands']: calc.info['calctypes'].append(0x2)
        if calc.info['energy']: calc.info['calctypes'].append(0x1)
        calc.info['spin'] = 0x2 if calc.info['spin'] else 0x1

        # TODO: standardize
        if 'vac' in calc.info:
            if 'X' in symbols: calc.info['techs'].append('vacancy defect: ghost')
            else: calc.info['techs'].append('vacancy defect: void space')

        calc.info['lata'] = round(calc.info['cellpar'][0], 3)
        calc.info['latb'] = round(calc.info['cellpar'][1], 3)
        calc.info['latc'] = round(calc.info['cellpar'][2], 3)
        calc.info['latalpha'] = round(calc.info['cellpar'][3], 2)
        calc.info['latbeta'] = round(calc.info['cellpar'][4], 2)
        calc.info['latgamma'] = round(calc.info['cellpar'][5], 2)

        # invoke symmetry finder
        found = SymmetryHandler(calc, symprec)
        if found.error:
            return None, found.error
        calc.info['sg'] = found.i
        calc.info['ng'] = found.n
        calc.info['symmetry'] = found.symmetry
        calc.info['spg'] = "%s — %s" % (found.n, found.i)
        calc.info['pg'] = found.pg
        calc.info['dg'] = found.dg

        # phonons
        if calc.phonons['dfp_magnitude']: calc.info['dfp_magnitude'] = round(calc.phonons['dfp_magnitude'], 3)
        if calc.phonons['dfp_disps']: calc.info['dfp_disps'] = len(calc.phonons['dfp_disps'])
        if calc.phonons['modes']:
            calc.info['n_ph_k'] = len(calc.phonons['ph_k_degeneracy']) if calc.phonons['ph_k_degeneracy'] else 1

        #calc.info['rgkmax'] = calc.electrons['rgkmax'] # LAPW

        # electronic properties reasoning by bands
        if calc.electrons['bands']:
            if calc.electrons['bands'].is_conductor():
                calc.info['etype'] = 0x2
                calc.info['bandgap'] = 0.0
                calc.info['bandgaptype'] = 0x1
            else:
                try: gap, is_direct = calc.electrons['bands'].get_bandgap()
                except ElectronStructureError as e:
                    calc.electrons['bands'] = None
                    calc.warning(e.value)
                else:
                    calc.info['etype'] = 0x1
                    calc.info['bandgap'] = round(gap, 2)
                    calc.info['bandgaptype'] = 0x2 if is_direct else 0x3

        # electronic properties reasoning by DOS
        if calc.electrons['dos']:
            try: gap = round(calc.electrons['dos'].get_bandgap(), 2)
            except ElectronStructureError as e:
                calc.electrons['dos'] = None
                calc.warning(e.value)
            else:
                if calc.electrons['bands']: # check coincidence
                    if abs(calc.info['bandgap'] - gap) > 0.2: calc.warning('Bans gaps in DOS and bands data differ considerably! The latter will be considered.')
                else:
                    calc.info['bandgap'] = gap
                    if gap: calc.info['etype'] = 0x1
                    else:
                        calc.info['etype'] = 0x2
                        calc.info['bandgaptype'] = 0x1

        # TODO: beware to add something new to an existing item!
        # TODO2: unknown or absent?
        for entity in self.hierarchy:
            if entity['creates_topic'] and not entity['optional'] and not calc.info.get(entity['source']):
                if entity['enumerated']:
                    calc.info[ entity['source'] ] = [0x0] if entity['multiple'] else 0x0
                else:
                    calc.info[ entity['source'] ] = ['none'] if entity['multiple'] else 'none'

        calc.benchmark() # this call must be at the very end of parsing

        return calc, error

    def postprocess(self, calc, with_module=None, dry_run=None):
        '''
        Invokes module(s) API
        NB: this is the PUBLIC method
        @returns apps_dict
        '''
        for appname, appclass in self.Apps.items():
            if with_module and with_module != appname: continue

            run_permitted = False

            # scope-conditions
            if appclass['apptarget']:
                for key in appclass['apptarget']:
                    negative = False
                    if str(appclass['apptarget'][key]).startswith('!'):
                        negative = True
                        scope_prop = appclass['apptarget'][key][1:]
                    else:
                        scope_prop = appclass['apptarget'][key]

                    if key in calc.info:
                        # non-strict comparison ("CRYSTAL" matches "CRYSTAL09 v2.0")
                        if (str(scope_prop) in str(calc.info[key]) or scope_prop == calc.info[key]) != negative: # true if only one, but not both
                            run_permitted = True
                        else:
                            run_permitted = False
                            break

            else: run_permitted = True

            # module code running
            if run_permitted:
                calc.apps[appname] = {'error': None, 'data': None}
                if dry_run: continue
                try: AppInstance = appclass['appmodule'](calc)
                except:
                    exc_type, exc_value, exc_tb = sys.exc_info()
                    errmsg = "Fatal error in %s module:\n %s" % ( appname, " ".join(traceback.format_exception( exc_type, exc_value, exc_tb )) )
                    calc.apps[appname]['error'] = errmsg
                    calc.warning( errmsg )
                else:
                    try: calc.apps[appname]['data'] = getattr(AppInstance, appclass['appdata'])
                    except AttributeError:
                        errmsg = 'No appdata-defined property found for %s module!' % appname
                        calc.apps[appname]['error'] = errmsg
                        calc.warning( errmsg )
        return calc

    def save(self, calc, session):
        '''
        Saves tilde_obj into the database
        NB: this is the PUBLIC method
        @returns checksum, error
        '''
        checksum = calc.get_checksum()

        try: existing_calc = session.query(model.Calculation).filter(model.Calculation.checksum == checksum).one()
        except NoResultFound: pass
        else:
            del calc
            return None, "This calculation already exists!"

        if not calc.download_size:
            for f in calc.related_files:
                calc.download_size += os.stat(f).st_size

        ormcalc = model.Calculation(checksum = checksum)

        if calc._calcset:
            ormcalc.meta_data = model.Metadata(chemical_formula = calc.info['standard'], download_size = calc.download_size)

            for child in session.query(model.Calculation).filter(model.Calculation.checksum.in_(calc._calcset)).all():
                ormcalc.children.append(child)
            ormcalc.siblings_count = len(ormcalc.children)
            ormcalc.nested_depth = calc._nested_depth

        else:
            # prepare phonon data for saving
            # this is actually a dict to list conversion TODO re-structure this
            if calc.phonons['modes']:
                phonons_json = []

                for bzpoint, frqset in calc.phonons['modes'].items():
                    # re-orientate eigenvectors
                    for i in range(0, len(calc.phonons['ph_eigvecs'][bzpoint])):
                        for j in range(0, len(calc.phonons['ph_eigvecs'][bzpoint][i])//3):
                            eigv = array([calc.phonons['ph_eigvecs'][bzpoint][i][j*3], calc.phonons['ph_eigvecs'][bzpoint][i][j*3+1], calc.phonons['ph_eigvecs'][bzpoint][i][j*3+2]])
                            R = dot( eigv, calc.structures[-1].cell ).tolist()
                            calc.phonons['ph_eigvecs'][bzpoint][i][j*3], calc.phonons['ph_eigvecs'][bzpoint][i][j*3+1], calc.phonons['ph_eigvecs'][bzpoint][i][j*3+2] = [round(x, 3) for x in R]

                    try: irreps = calc.phonons['irreps'][bzpoint]
                    except KeyError:
                        empty = []
                        for i in range(len(frqset)): empty.append('')
                        irreps = empty
                    phonons_json.append({  'bzpoint':bzpoint, 'freqs':frqset, 'irreps':irreps, 'ph_eigvecs':calc.phonons['ph_eigvecs'][bzpoint]  })
                    if bzpoint == '0 0 0':
                        phonons_json[-1]['ir_active'] = calc.phonons['ir_active']
                        phonons_json[-1]['raman_active'] = calc.phonons['raman_active']
                    if calc.phonons['ph_k_degeneracy']:
                        phonons_json[-1]['ph_k_degeneracy'] = calc.phonons['ph_k_degeneracy'][bzpoint]

                ormcalc.phonons = model.Phonons()
                ormcalc.spectra.append( model.Spectra(kind = model.Spectra.PHONON, eigenvalues = json.dumps(phonons_json)) )

            # prepare electron data for saving TODO re-structure this
            for i in ['dos', 'bands']: # projected?
                if calc.electrons[i]: calc.electrons[i] = calc.electrons[i].todict()

            if calc.electrons['dos'] or calc.electrons['bands']:
                ormcalc.electrons = model.Electrons(gap = calc.info['bandgap'])
                if 'bandgaptype' in calc.info: ormcalc.electrons.is_direct = 1 if calc.info['bandgaptype'] == 'direct' else -1
                ormcalc.spectra.append(model.Spectra(
                    kind = model.Spectra.ELECTRON,
                    dos = json.dumps(calc.electrons['dos']),
                    bands = json.dumps(calc.electrons['bands']),
                    projected = json.dumps(calc.electrons['projected']),
                    eigenvalues = json.dumps(calc.electrons['eigvals']))
                )

            # construct ORM for other props
            calc.related_files = list(map(virtualize_path, calc.related_files))
            ormcalc.meta_data = model.Metadata(location = calc.info['location'], finished = calc.info['finished'], raw_input = calc.info['input'], modeling_time = calc.info['duration'], chemical_formula = html_formula(calc.info['standard']), download_size = calc.download_size, filenames = json.dumps(calc.related_files))

            codefamily = model.Codefamily.as_unique(session, content = calc.info['framework'])
            codeversion = model.Codeversion.as_unique(session, content = calc.info['prog'])

            codeversion.instances.append( ormcalc.meta_data )
            codefamily.versions.append( codeversion )

            pot = model.Pottype.as_unique(session, name = calc.info['H'])
            pot.instances.append(ormcalc)
            ormcalc.recipinteg = model.Recipinteg(kgrid = calc.info['k'], kshift = calc.info['kshift'], smearing = calc.info['smear'], smeartype = calc.info['smeartype'])
            ormcalc.basis = model.Basis(kind = calc.info['ansatz'], content = json.dumps(calc.electrons['basis_set']) if calc.electrons['basis_set'] else None)
            ormcalc.energy = model.Energy(convergence = json.dumps(calc.convergence), total = calc.info['energy'])

            ormcalc.spacegroup = model.Spacegroup(n=calc.info['ng'])
            ormcalc.struct_ratios = model.Struct_ratios(chemical_formula=calc.info['standard'], formula_units=calc.info['expanded'], nelem=calc.info['nelem'], dimensions=calc.info['dims'])
            if len(calc.tresholds) > 1: ormcalc.struct_optimisation = model.Struct_optimisation(tresholds=json.dumps(calc.tresholds), ncycles=json.dumps(calc.ncycles))

            for n, ase_repr in enumerate(calc.structures):
                is_final = True if n == len(calc.structures)-1 else False
                struct = model.Structure(step = n, final = is_final)

                s = cell_to_cellpar(ase_repr.cell)
                struct.lattice = model.Lattice(a=s[0], b=s[1], c=s[2], alpha=s[3], beta=s[4], gamma=s[5], a11=ase_repr.cell[0][0], a12=ase_repr.cell[0][1], a13=ase_repr.cell[0][2], a21=ase_repr.cell[1][0], a22=ase_repr.cell[1][1], a23=ase_repr.cell[1][2], a31=ase_repr.cell[2][0], a32=ase_repr.cell[2][1], a33=ase_repr.cell[2][2])

                #rmts =      ase_repr.get_array('rmts') if 'rmts' in ase_repr.arrays else [None for j in range(len(ase_repr))]
                charges =   ase_repr.get_array('charges') if 'charges' in ase_repr.arrays else [None for j in range(len(ase_repr))]
                magmoms =   ase_repr.get_array('magmoms') if 'magmoms' in ase_repr.arrays else [None for j in range(len(ase_repr))]
                for n, i in enumerate(ase_repr):
                    struct.atoms.append( model.Atom( number=chemical_symbols.index(i.symbol), x=i.x, y=i.y, z=i.z, charge=charges[n], magmom=magmoms[n] ) )

                ormcalc.structures.append(struct)
            # TODO Forces

        ormcalc.uigrid = model.Grid(info=json.dumps(calc.info))

        # tags ORM
        uitopics = []
        for entity in self.hierarchy:

            if not entity['creates_topic']: continue

            if entity['multiple'] or calc._calcset:
                for item in calc.info.get( entity['source'], [] ):
                    uitopics.append( model.topic(cid=entity['cid'], topic=item) )
            else:
                topic = calc.info.get(entity['source'])
                if topic or not entity['optional']: uitopics.append( model.topic(cid=entity['cid'], topic=topic) )

        uitopics = [model.Topic.as_unique(session, cid=x.cid, topic="%s" % x.topic) for x in uitopics]

        ormcalc.uitopics.extend(uitopics)

        if calc._calcset: session.add(ormcalc)
        else: session.add_all([codefamily, codeversion, pot, ormcalc])

        session.commit()
        del calc, ormcalc
        return checksum, None

    def purge(self, session, checksum):
        '''
        Deletes calc entry by checksum entirely from the database
        NB source files on disk are not deleted
        NB: this is the PUBLIC method
        @returns error
        '''
        C = session.query(model.Calculation).get(checksum)

        if not C: return 'Calculation does not exist!'

        # dataset deletion includes editing the whole dataset hierarchical tree (if any)
        if C.siblings_count:
            C_meta = session.query(model.Metadata).get(checksum)
            higher_lookup = {}
            more = C.parent
            distance = 0
            while True:
                distance += 1
                higher, more = more, []
                if not higher: break
                for i in higher:
                    try: higher_lookup[distance].add(i)
                    except KeyError: higher_lookup[distance] = set([i])
                    if i.parent:
                        more += i.parent
            for distance, members in higher_lookup.items():
                for i in members:
                    if distance == 1:
                        i.siblings_count -= 1
                    if not i.siblings_count:
                        return 'The parent dataset contains only one (current) item, please, delete parent dataset first!'

                    i.meta_data.download_size -= C_meta.download_size
                    session.add(i)
        # low-level entry deletion deals with additional tables
        else:
            session.execute( model.delete( model.Spectra ).where( model.Spectra.checksum == checksum) )
            session.execute( model.delete( model.Electrons ).where( model.Electrons.checksum == checksum ) )
            session.execute( model.delete( model.Phonons ).where( model.Phonons.checksum == checksum ) )
            session.execute( model.delete( model.Recipinteg ).where( model.Recipinteg.checksum == checksum ) )
            session.execute( model.delete( model.Basis ).where( model.Basis.checksum == checksum ) )
            session.execute( model.delete( model.Energy ).where( model.Energy.checksum == checksum ) )
            session.execute( model.delete( model.Spacegroup ).where( model.Spacegroup.checksum == checksum ) )
            session.execute( model.delete( model.Struct_ratios ).where( model.Struct_ratios.checksum == checksum ) )
            session.execute( model.delete( model.Struct_optimisation ).where( model.Struct_optimisation.checksum == checksum ) )

            struct_ids = [ int(i[0]) for i in session.query(model.Structure.struct_id).filter(model.Structure.checksum == checksum).all() ]
            for struct_id in struct_ids:
                session.execute( model.delete( model.Atom ).where( model.Atom.struct_id == struct_id ) )
                session.execute( model.delete( model.Lattice ).where( model.Lattice.struct_id == struct_id ) )
            session.execute( model.delete( model.Structure ).where( model.Structure.checksum == checksum ) )

        # for all types of entries
        if len(C.references):
            left_references = [ int(i[0]) for i in session.query(model.Reference.reference_id).join(model.metadata_references, model.Reference.reference_id == model.metadata_references.c.reference_id).filter(model.metadata_references.c.checksum == checksum).all() ]
            session.execute( model.delete( model.metadata_references ).where( model.metadata_references.c.checksum == checksum ) )

            # remove the whole citation?
            for lc in left_references:
                if not (session.query(model.metadata_references.c.checksum).filter(model.metadata_references.c.reference_id == lc).count()):
                    session.execute( model.delete( model.Reference ).where(model.Reference.reference_id == lc) )

        # TODO rewrite with cascading
        session.execute( model.delete( model.Metadata ).where( model.Metadata.checksum == checksum ) )

        session.execute( model.delete( model.Grid ).where( model.Grid.checksum == checksum ) )
        session.execute( model.delete( model.tags ).where( model.tags.c.checksum == checksum ) )

        session.execute( model.delete( model.calcsets ).where( model.calcsets.c.children_checksum == checksum ) )
        session.execute( model.delete( model.calcsets ).where( model.calcsets.c.parent_checksum == checksum ) )
        session.execute( model.delete( model.Calculation ).where( model.Calculation.checksum == checksum ) )
        session.commit()
        # NB tables topics, codefamily, codeversion, pottype are mostly irrelevant and, if needed, should be cleaned manually
        return False

    def merge(self, session, checksums, title):
        '''
        Merges calcs into a new calc called DATASET
        NB: this is the PUBLIC method
        @returns DATASET, error
        '''
        calc = Output(calcset=checksums)

        cur_depth = 0

        for nested_depth, grid_item, download_size in session.query(model.Calculation.nested_depth, model.Grid.info, model.Metadata.download_size).filter(model.Calculation.checksum == model.Grid.checksum, model.Grid.checksum == model.Metadata.checksum, model.Calculation.checksum.in_(checksums)).all():

            if nested_depth > cur_depth: cur_depth = nested_depth

            grid_item = json.loads(grid_item)

            for entity in self.hierarchy:

                topic = grid_item.get(entity['source'])
                if not topic: continue

                if not isinstance(topic, list): topic = [ topic ]
                calc.info[ entity['source'] ] = list(set( calc.info.get(entity['source'], []) + topic ))

            calc.download_size += download_size

        if not calc.download_size: return None, 'Wrong parameters provided!'

        calc._nested_depth = cur_depth + 1

        calc.info['standard'] = title

        # generate fake checksum
        calc._checksum = calc.get_collective_checksum()

        return calc, None

    def augment(self, session, parent, addendum):
        '''
        Augments a DATASET with some calcs
        NB: this is the PUBLIC method
        @returns error
        '''
        parent_calc = session.query(model.Calculation).get(parent)
        if not parent_calc or not parent_calc.siblings_count: return 'Dataset is erroneously selected!'

        existing_children, filtered_addendum = [child.checksum for child in parent_calc.children], []

        for child in addendum:
            if not child in existing_children: filtered_addendum.append(child)

        if not filtered_addendum: return 'All these data are already present in this dataset.'
        if parent_calc.checksum in filtered_addendum: return 'A dataset cannot be added into itself.'

        higher_lookup = {}
        more = parent_calc.parent
        distance = 0
        while True:
            distance += 1
            higher, more = more, []
            if not higher: break
            for i in higher:
                try: higher_lookup[distance].add(i)
                except KeyError: higher_lookup[distance] = set([i])
                if i.parent:
                    more += i.parent
        for members in list(higher_lookup.values()):
            for i in members:
                if i.checksum in filtered_addendum: return 'A parent dataset cannot be added to its children dataset.'

        parent_meta = session.query(model.Metadata).get(parent)
        parent_grid = session.query(model.Grid).get(parent)
        info_obj = json.loads(parent_grid.info)

        for nested_depth, grid_item, download_size in session.query(model.Calculation.nested_depth, model.Grid.info, model.Metadata.download_size).filter(model.Calculation.checksum == model.Grid.checksum, model.Grid.checksum == model.Metadata.checksum, model.Calculation.checksum.in_(filtered_addendum)).all():

            if nested_depth >= parent_calc.nested_depth: parent_calc.nested_depth = nested_depth + 1

            grid_item = json.loads(grid_item)

            for entity in self.hierarchy:

                topic = grid_item.get(entity['source'])
                if not topic: continue

                if entity['source'] == 'standard': topic = []

                if not isinstance(topic, list): topic = [ topic ]

                existing_term = info_obj.get(entity['source'], [])
                if not isinstance(existing_term, list): existing_term = [ existing_term ] # TODO

                info_obj[ entity['source'] ] = list(set( existing_term + topic ))

            parent_meta.download_size += download_size

        info_obj['standard'] = info_obj['standard'][0] # TODO
        parent_grid.info = json.dumps(info_obj)

        # tags ORM
        for entity in self.hierarchy:

            if not entity['creates_topic']: continue

            for item in info_obj.get( entity['source'], [] ):
                parent_calc.uitopics.append( model.Topic.as_unique(session, cid=entity['cid'], topic="%s" % item) )

        for child in session.query(model.Calculation).filter(model.Calculation.checksum.in_(filtered_addendum)).all():
            parent_calc.children.append(child)
        parent_calc.siblings_count = len(parent_calc.children)

        for distance, members in higher_lookup.items():
            for i in members:
                d = parent_calc.nested_depth - i.nested_depth + distance
                if d > 0:
                    i.nested_depth += d
                i.meta_data.download_size += parent_meta.download_size # fixme
                session.add(i)

        session.add_all([parent_calc, parent_meta, parent_grid])
        session.commit()
        return False


text:
. library.sh

old_index_name=${1?}
new_index_name=${2?}
es_port=${3?}
es_ip=${4:-""}
command_before_create=${5:-""}
shard_count=${6:-""}
replica_count=${7:-""}

################################################################################
# Set default values
log_file="/var/log/create_index_from_old_${BUILD_ID}.log"
# if $es_ip is not given, use ip of eth0 as default
if [ -z "$es_ip" ]; then
    es_ip=$(/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
fi

if [ -z "$shard_count" ]; then
    shard_count=$(get_index_shard_count "$es_ip" "$es_port" "$old_index_name")
fi

if [ -z "$replica_count" ]; then
    replica_count=$(get_index_replica_count "$es_ip" "$es_port" "$old_index_name")
fi

################################################################################
# Precheck
if [ "$index_alias_name" = "$old_index_name" ]; then
    echo "ERROR: wrong parameter. old_index_name and index_alias_name can't be the same"
    exit 1
fi
assert_jq_installed

# TODO: quit when alias and index doesn't match
assert_es_not_red "$es_ip" "$es_port"
assert_index_not_exists "$es_ip" "$es_port" "$old_index_name"
assert_index_exists "$es_ip" "$es_port" "$new_index_name"

################################################################################
log "=============== Create new index. old_index_name: $old_index_name, new_index_name: $new_index_name"

list_indices "$es_ip" "$es_port"

tmp_dir="/tmp/${old_index_name}"
[ -d "$tmp_dir" ] || mkdir -p "$tmp_dir"

create_json_file="${tmp_dir}/combined.json"

log "Get setting and mappings of old index to ${create_json_file}"
cd "${tmp_dir}"
curl "http://${es_ip}:${es_port}/${old_index_name}/_settings" | \
    jq ".[] | .settings.index.number_of_shards=\"${shard_count}\" | .settings.index.number_of_replicas=\"${replica_count}\"" \
       > "${tmp_dir}/settings.json"

curl "http://${es_ip}:${es_port}/${old_index_name}/_mapping" \
    | jq '.[]' > "${tmp_dir}/mapping.json"

cat mapping.json | jq --sort-keys '.' > mapping_sorted.json

cat "mapping_sorted.json" "settings.json" \
    | jq --slurp '.[0] * .[1]' > "${create_json_file}"

################################################################################
# run the hook
if [ -n "$command_before_create" ]; then
    export OLD_INDEX_NAME="$old_index_name"
    export MAPPING_JSON_FILE="${tmp_dir}/mapping_sorted.json"
    export SETTINGS_JSON_FILE="${tmp_dir}/settings.json"
    export COMBINED_JSON_FILE="$create_json_file"

    eval "$command_before_create" | tee -a "$log_file"
    # java -jar /root/fix-mappings-reindex-2.0.jar "$index_type" "${old_index_name}" ./mapping_sorted.json ./settings.json | tee -a "$log_file"

    if tail -n 10 "$log_file" | grep -i "ERROR"; then
        log "error is found when running command: $command_before_create"
        exit 1
    fi
fi

################################################################################
create_timeout="30m"
log "create new index with settings and mappings"
time curl -XPOST "http://${es_ip}:${es_port}/${new_index_name}?timeout=${create_timeout}&wait_for_active_shards=all" \
     -d @"${create_json_file}" | tee -a "$log_file"
echo >> "$log_file"

if tail -n 1 "$log_file" | grep "\"acknowledged\"*:*true"; then
    log "keep going with the following process"
else
    log "ERROR to run previous curl command"
    tail -n 5 "$log_file"
    exit 1
fi

log "Get the setting of the new index"
curl -XGET "http://${es_ip}:${es_port}/${new_index_name}/_settings?pretty"
## File : create_index_from_old.sh ends