text:
package com.dci.intellij.dbn.language.psql;

import com.dci.intellij.dbn.language.common.element.ElementType;
import com.dci.intellij.dbn.language.common.element.util.ElementTypeAttribute;
import com.dci.intellij.dbn.language.common.psi.BasePsiElement;
import com.dci.intellij.dbn.language.common.psi.ChameleonPsiElement;
import com.dci.intellij.dbn.language.common.psi.TokenPsiElement;
import com.intellij.lang.ASTNode;
import com.intellij.lang.folding.FoldingBuilder;
import com.intellij.lang.folding.FoldingDescriptor;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;

import java.util.ArrayList;
import java.util.List;

public class PSQLFoldingBuilder implements FoldingBuilder {

    public FoldingDescriptor[] buildFoldRegions(ASTNode node, Document document) {
        List<FoldingDescriptor> foldingDescriptors = new ArrayList<FoldingDescriptor>();
        createFoldingDescriptors(node.getPsi(), document, foldingDescriptors, 0);
        return foldingDescriptors.toArray(new FoldingDescriptor[foldingDescriptors.size()]);
    }

    private void createFoldingDescriptors(PsiElement psiElement, Document document, List<FoldingDescriptor> foldingDescriptors, int nestingIndex) {
        PsiElement child = psiElement.getFirstChild();
        while (child != null) {
            if (child instanceof PsiComment) {
                String text = child.getText();
                if (text.startsWith("/*") && text.indexOf('\n') > -1) {
                    FoldingDescriptor foldingDescriptor = new FoldingDescriptor(
                            child.getNode(),
                            child.getTextRange());

                    foldingDescriptors.add(foldingDescriptor);
                }
            }
            if (child instanceof BasePsiElement) {
                BasePsiElement basePsiElement = (BasePsiElement) child;
                ElementType elementType = basePsiElement.getElementType();
                int blockEndOffset = basePsiElement.getTextOffset() + basePsiElement.getTextLength();

                boolean folded = false;

                if (elementType.is(ElementTypeAttribute.FOLDABLE_BLOCK)) {
                    BasePsiElement subjectPsiElement = basePsiElement.lookupFirstPsiElement(ElementTypeAttribute.SUBJECT);
                    if (subjectPsiElement == null) {
                        PsiElement firstChild = basePsiElement.getFirstChild();
                        if (firstChild instanceof TokenPsiElement) {
                            subjectPsiElement = (BasePsiElement) firstChild;
                        }
                    }
                    if (subjectPsiElement != null && subjectPsiElement.getParent() == basePsiElement) {

                        int subjectEndOffset = subjectPsiElement.getTextOffset() + subjectPsiElement.getTextLength();

                        int subjectLineNumber = document.getLineNumber(subjectEndOffset);
                        int blockEndOffsetLineNumber = document.getLineNumber(blockEndOffset);

                        if (subjectLineNumber < blockEndOffsetLineNumber) {
                            TextRange textRange = new TextRange(subjectEndOffset, blockEndOffset);

                            FoldingDescriptor foldingDescriptor = new FoldingDescriptor(basePsiElement.getNode(), textRange);

                            foldingDescriptors.add(foldingDescriptor);
                            nestingIndex++;
                            folded = true;
                        }
                    }
                }

                if (!folded && elementType.is(ElementTypeAttribute.STATEMENT)) {
                    if (basePsiElement.containsLineBreaks()) {
                        TextRange textRange = null;

                        BasePsiElement firstPsiElement = basePsiElement.lookupFirstLeafPsiElement();
                        int firstElementEndOffset = firstPsiElement.getTextOffset() + firstPsiElement.getTextLength();
                        int firstElementLineNumber = document.getLineNumber(firstElementEndOffset);

                        BasePsiElement subjectPsiElement = basePsiElement.lookupFirstPsiElement(ElementTypeAttribute.SUBJECT);
                        if (subjectPsiElement != null && subjectPsiElement.getParent() == basePsiElement) {
                            int subjectEndOffset = subjectPsiElement.getTextOffset() + subjectPsiElement.getTextLength();
                            int subjectLineNumber = document.getLineNumber(subjectEndOffset);

                            if (subjectLineNumber == firstElementLineNumber) {
                                textRange = new TextRange(subjectEndOffset, blockEndOffset);
                            }
                        }

                        if (textRange == null) {
                            textRange = new TextRange(firstElementEndOffset, blockEndOffset);
                        }

                        FoldingDescriptor foldingDescriptor = new FoldingDescriptor(basePsiElement.getNode(), textRange);
                        foldingDescriptors.add(foldingDescriptor);
                        nestingIndex++;
                    }
                }

                if (nestingIndex < 9) {
                    createFoldingDescriptors(child, document, foldingDescriptors, nestingIndex);
                }
            }
            child = child.getNextSibling();
        }
    }

    public String getPlaceholderText(ASTNode node) {
        PsiElement psiElement = node.getPsi();
        if (psiElement instanceof BasePsiElement) {
            /*BasePsiElement basePsiElement = (BasePsiElement) psiElement;
            PsiElement subject = basePsiElement.lookupFirstSubjectPsiElement();
            StringBuilder buffer = new StringBuilder(basePsiElement.getElementType().getDescription());
            if (subject != null) {
                buffer.append(" (");
                buffer.append(subject.getText());
                buffer.append(")");
            }
            return buffer.toString();*/
            return "(...)";
        }

        if (psiElement instanceof PsiComment) {
            return "/*...*/";
        }

        if (psiElement instanceof ChameleonPsiElement) {
            ChameleonPsiElement chameleonPsiElement = (ChameleonPsiElement) psiElement;
            return chameleonPsiElement.getLanguage().getDisplayName() + " block";
        }
        return "";
    }

    public boolean isCollapsedByDefault(ASTNode node) {
        return false;
    }

}


text:
/*

---------------------------------------------------------------------------------

                                               oooo
                                               `888
                oooo d8b  .ooooo.  oooo    ooo  888  oooo  oooo
                `888""8P d88' `88b  `88b..8P'   888  `888  `888
                 888     888   888    Y888'     888   888   888
                 888     888   888  .o8"'88b    888   888   888
                d888b    `Y8bod8P' o88'   888o o888o  `V88V"V8P'

                                                  www.roxlu.com
                                             www.apollomedia.nl
                                          www.twitter.com/roxlu

---------------------------------------------------------------------------------

  # MultiVideoStreamer

  The multi video streamer will create multiple audio and video encoder instances
  and streams these to the specified servers. Some media server providers have a
  feature to stream multiple quality video to their end users; this is when you
  would use the MultiVideoStreamer.

  If you only need to stream one quality stream, use the VideoStreamer class.

  ---

  After you've successfully loaded the settings, setup() and start()ed the MultiVideoStreamer
  you can start adding video and audio packets. It's important that you use a MemoryPool to
  handle packets. The memory pool uses a manual reference counting system to allow multiple
  encoder to work with the same data. YOU DO NOT HAVE TO THINK ABOUT THE REFERENCE COUNTS WHEN
  USING THIS CLASS. WE WILL ADD THE APPROPRIATE REFERENCE COUNTS FOR YOU!

  ---

  The feature of using multiple quality video/audio streams has had a big influence on the
  design of the whole Video Streamer library. This is the reason why we have a memory pool,
  where raw (input) video and audio data are shared between multiple video and audio
  encoders.

  IMPORTANT NOTE ABOUT AVPACKET
  -----------------------------

  It's important to know that I'm not entirely happy with the design of AVPacket and the way
  multiple video streams are handled. Though the current solution seems to be the most elegant
  I could think off to mix multi and basic streams into one AVPacket.  The thing is, that
  the YUV420PGrabber creates one big texture that will hold all the texture/video data for
  multiple streams (see this image where we have all the video data for 3 different sized
  video streams: http://www.flickr.com/photos/diederick/10027076563/ ). All this video data
  is assigned to the AVPacket::data member. We use strides and plane pointers to tell
  the video encoder where to fetch it's data from.

  Therefore, when using a multi packet. you need to assing all the strides + planes all the
  different streams are using. And this is where the "stream_id" member of VideoEncoder
  comes to work.

*/

#ifndef ROXLU_MULTI_VIDEOSTREAMER_H
#define ROXLU_MULTI_VIDEOSTREAMER_H

#include <streamer/core/MemoryPool.h>
#include <streamer/core/AudioEncoderMP3.h>
#include <streamer/core/AudioEncoderFAAC.h>
#include <streamer/videostreamer/VideoStreamer.h>
#include <streamer/videostreamer/VideoStreamerConfig.h>
#include <vector>

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

struct MultiStreamerInfo { /* contains all the necessary information for one video streamer to "run", everythin is owned and managed by the MultiVideoStreamer instance  */
  MultiStreamerInfo();
  AudioEncoder* audio_encoder;
  VideoStreamer* streamer;
  int32_t id;
};

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

class MultiVideoStreamer {
 public:
  MultiVideoStreamer();
  ~MultiVideoStreamer();
  bool loadSettings(std::string filepath); /* call before setup() - load the settings where you define all of the stream properties */
  bool setup(); /* after you've called loadSettings(), call setup() to setup all the streams and allocate the instances we need*/
  bool start(); /* this will start all the streams */
  void update(); /* call this regurlarly - it will make sure that we reconnect to the streaming server when we get disconncted */
  bool stop(); /* stop all the streams! */
  void shutdown(); /* deallocates all allocated memory from start(). */
  void print(); /* print debug information about the different qualities we've got */
  void addVideo(AVPacket* pkt); /* add video - make sure to set the timestamp! - DO NOT ADD ANY EXTRA REFERENCE COUNTERS! WE ADD THE REFCOUNTS FOR YOU! - BY DEFAULT AN AVPACKET HAS A REFCOUNT OF 1 WHEN RETURNED FROM THE FREE PACKETS IN A MEMORY POOL, WE WILL INCREASE THIS NUMBER BY THE NECESSARY AMOUNT! */
  void addAudio(AVPacket* pkt); /* add video - make sure to set the timestamp! - DO NOT ADD ANY EXTRA REFERENCE COUNTERS! WE ADD THE REFCOUNTS FOR YOU! - BY DEFAULT AN AVPACKET HAS A REFCOUNT OF 1 WHEN RETURNED FROM THE FREE PACKETS IN A MEMORY POOL, WE WILL INCREASE THIS NUMBER BY THE NECESSARY AMOUNT! */
  MultiStreamerInfo* operator[](unsigned int dx); /* returns the stream info for the given index */
  size_t size(); /* returns the number of MultiStreamerInfo objects - which represent a stream */
 private:
  VideoStreamerConfig config;
  std::vector<MultiStreamerInfo*> streamers; /* the memory pools and streamers that we use for the multi streaming; MultiVideoStreamer owns and manages these objects */
};

inline size_t MultiVideoStreamer::size() {
  return streamers.size();
}

inline MultiStreamerInfo* MultiVideoStreamer::operator[](unsigned int dx) {
#if !defined(NDEBUG)
  return streamers.at(dx);
#else
  return streamers[dx];
#endif
}

inline void MultiVideoStreamer::update() {

  for(std::vector<MultiStreamerInfo*>::iterator it = streamers.begin(); it != streamers.end(); ++it) {
    MultiStreamerInfo* msi = *it;
    msi->streamer->update();
  }
}

#endif

redpajama_set_name: RedPajamaGithub


text:
/*! UIkit 3.13.9 | https://www.getuikit.com | (c) 2014 - 2022 YOOtheme | MIT License */

(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('uikit-util')) :
    typeof define === 'function' && define.amd ? define('uikitslideshow_parallax', ['uikit-util'], factory) :
    (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.UIkitSlideshow_parallax = factory(global.UIkit.util));
})(this, (function (uikitUtil) { 'use strict';

    var Media = {
      props: {
        media: Boolean },

      data: {
        media: false },

      connected() {
        const media = toMedia(this.media);
        this.mediaObj = window.matchMedia(media);
        const handler = () => {
          this.matchMedia = this.mediaObj.matches;
          uikitUtil.trigger(this.$el, uikitUtil.createEvent('mediachange', false, true, [this.mediaObj]));
        };
        this.offMediaObj = uikitUtil.on(this.mediaObj, 'change', () => {
          handler();
          this.$emit('resize');
        });
        handler();
      },

      disconnected() {var _this$offMediaObj;
        (_this$offMediaObj = this.offMediaObj) == null ? void 0 : _this$offMediaObj.call(this);
      } };

    function toMedia(value) {
      if (uikitUtil.isString(value)) {
        if (uikitUtil.startsWith(value, '@')) {
          const name = "breakpoint-" + value.substr(1);
          value = uikitUtil.toFloat(uikitUtil.getCssVar(name));
        } else if (isNaN(value)) {
          return value;
        }
      }

      return value && uikitUtil.isNumeric(value) ? "(min-width: " + value + "px)" : '';
    }

    uikitUtil.memoize(async (src) => {
      if (src) {
        if (uikitUtil.startsWith(src, 'data:')) {
          return decodeURIComponent(src.split(',')[1]);
        } else {
          return (await fetch(src)).text();
        }
      } else {
        return Promise.reject();
      }
    });

    function getMaxPathLength(el) {
      return Math.ceil(
      Math.max(
      0,
      ...uikitUtil.$$('[stroke]', el).map((stroke) => {
        try {
          return stroke.getTotalLength();
        } catch (e) {
          return 0;
        }
      })));

    }

    const props = {
      x: transformFn,
      y: transformFn,
      rotate: transformFn,
      scale: transformFn,
      color: colorFn,
      backgroundColor: colorFn,
      borderColor: colorFn,
      blur: filterFn,
      hue: filterFn,
      fopacity: filterFn,
      grayscale: filterFn,
      invert: filterFn,
      saturate: filterFn,
      sepia: filterFn,
      opacity: cssPropFn,
      stroke: strokeFn,
      bgx: backgroundFn,
      bgy: backgroundFn };

    const { keys } = Object;

    var Parallax = {
      mixins: [Media],

      props: fillObject(keys(props), 'list'),

      data: fillObject(keys(props), undefined),

      computed: {
        props(properties, $el) {
          return keys(props).reduce((result, prop) => {
            if (!uikitUtil.isUndefined(properties[prop])) {
              result[prop] = props[prop](prop, $el, properties[prop].slice());
            }
            return result;
          }, {});
        } },

      events: {
        load() {
          this.$emit();
        } },

      methods: {
        reset() {
          uikitUtil.each(this.getCss(0), (_, prop) => uikitUtil.css(this.$el, prop, ''));
        },

        getCss(percent) {
          const css = { transform: '', filter: '' };
          for (const prop in this.props) {
            this.props[prop](css, percent);
          }
          return css;
        } } };

    function transformFn(prop, el, stops) {
      const unit = getUnit(stops) || { x: 'px', y: 'px', rotate: 'deg' }[prop] || '';
      let transformFn;

      if (prop === 'x' || prop === 'y') {
        prop = "translate" + uikitUtil.ucfirst(prop);
        transformFn = (stop) => uikitUtil.toFloat(uikitUtil.toFloat(stop).toFixed(unit === 'px' ? 0 : 6));
      }

      if (stops.length === 1) {
        stops.unshift(prop === 'scale' ? 1 : 0);
      }

      stops = parseStops(stops, transformFn);

      return (css, percent) => {
        css.transform += " " + prop + "(" + getValue(stops, percent) + unit + ")";
      };
    }

    function colorFn(prop, el, stops) {
      if (stops.length === 1) {
        stops.unshift(getCssValue(el, prop, ''));
      }

      stops = parseStops(stops, (stop) => parseColor(el, stop));

      return (css, percent) => {
        const [start, end, p] = getStop(stops, percent);
        const value = start.
        map((value, i) => {
          value += p * (end[i] - value);
          return i === 3 ? uikitUtil.toFloat(value) : parseInt(value, 10);
        }).
        join(',');
        css[prop] = "rgba(" + value + ")";
      };
    }

    function parseColor(el, color) {
      return getCssValue(el, 'color', color).
      split(/[(),]/g).
      slice(1, -1).
      concat(1).
      slice(0, 4).
      map(uikitUtil.toFloat);
    }

    function filterFn(prop, el, stops) {
      if (stops.length === 1) {
        stops.unshift(0);
      }

      const unit = getUnit(stops) || { blur: 'px', hue: 'deg' }[prop] || '%';
      prop = { fopacity: 'opacity', hue: 'hue-rotate' }[prop] || prop;
      stops = parseStops(stops);

      return (css, percent) => {
        const value = getValue(stops, percent);
        css.filter += " " + prop + "(" + (value + unit) + ")";
      };
    }

    function cssPropFn(prop, el, stops) {
      if (stops.length === 1) {
        stops.unshift(getCssValue(el, prop, ''));
      }

      stops = parseStops(stops);

      return (css, percent) => {
        css[prop] = getValue(stops, percent);
      };
    }

    function strokeFn(prop, el, stops) {
      if (stops.length === 1) {
        stops.unshift(0);
      }

      const unit = getUnit(stops);
      const length = getMaxPathLength(el);
      stops = parseStops(stops.reverse(), (stop) => {
        stop = uikitUtil.toFloat(stop);
        return unit === '%' ? stop * length / 100 : stop;
      });

      if (!stops.some((_ref) => {let [value] = _ref;return value;})) {
        return uikitUtil.noop;
      }

      uikitUtil.css(el, 'strokeDasharray', length);

      return (css, percent) => {
        css.strokeDashoffset = getValue(stops, percent);
      };
    }

    function backgroundFn(prop, el, stops) {
      if (stops.length === 1) {
        stops.unshift(0);
      }

      prop = prop.substr(-1);
      const attr = prop === 'y' ? 'height' : 'width';
      stops = parseStops(stops, (stop) => uikitUtil.toPx(stop, attr, el));

      const bgPos = getCssValue(el, "background-position-" + prop, '');

      return getCssValue(el, 'backgroundSize', '') === 'cover' ?
      backgroundCoverFn(prop, el, stops, bgPos, attr) :
      setBackgroundPosFn(prop, stops, bgPos);
    }

    function backgroundCoverFn(prop, el, stops, bgPos, attr) {
      const dimImage = getBackgroundImageDimensions(el);

      if (!dimImage.width) {
        return uikitUtil.noop;
      }

      const values = stops.map((_ref2) => {let [value] = _ref2;return value;});
      const min = Math.min(...values);
      const max = Math.max(...values);
      const down = values.indexOf(min) < values.indexOf(max);

      const diff = max - min;
      let pos = (down ? -diff : 0) - (down ? min : max);

      const dimEl = {
        width: el.offsetWidth,
        height: el.offsetHeight };

      const baseDim = uikitUtil.Dimensions.cover(dimImage, dimEl);
      const span = baseDim[attr] - dimEl[attr];

      if (span < diff) {
        dimEl[attr] = baseDim[attr] + diff - span;
      } else if (span > diff) {
        const posPercentage = dimEl[attr] / uikitUtil.toPx(bgPos, attr, el, true);

        if (posPercentage) {
          pos -= (span - diff) / posPercentage;
        }
      }

      const dim = uikitUtil.Dimensions.cover(dimImage, dimEl);

      const fn = setBackgroundPosFn(prop, stops, pos + "px");
      return (css, percent) => {
        fn(css, percent);
        css.backgroundSize = dim.width + "px " + dim.height + "px";
        css.backgroundRepeat = 'no-repeat';
      };
    }

    function setBackgroundPosFn(prop, stops, pos) {
      return function (css, percent) {
        css["background-position-" + prop] = "calc(" + pos + " + " + getValue(stops, percent) + "px)";
      };
    }

    const dimensions = {};
    function getBackgroundImageDimensions(el) {
      const src = uikitUtil.css(el, 'backgroundImage').replace(/^none|url\(["']?(.+?)["']?\)$/, '$1');

      if (dimensions[src]) {
        return dimensions[src];
      }

      const image = new Image();
      if (src) {
        image.src = src;

        if (!image.naturalWidth) {
          image.onload = () => {
            dimensions[src] = toDimensions(image);
            uikitUtil.trigger(el, uikitUtil.createEvent('load', false));
          };
          return toDimensions(image);
        }
      }

      return dimensions[src] = toDimensions(image);
    }

    function toDimensions(image) {
      return {
        width: image.naturalWidth,
        height: image.naturalHeight };

    }

    function parseStops(stops, fn) {if (fn === void 0) {fn = uikitUtil.toFloat;}
      const result = [];
      const { length } = stops;
      let nullIndex = 0;
      for (let i = 0; i < length; i++) {
        let [value, percent] = uikitUtil.isString(stops[i]) ? stops[i].trim().split(' ') : [stops[i]];
        value = fn(value);
        percent = percent ? uikitUtil.toFloat(percent) / 100 : null;

        if (i === 0) {
          if (percent === null) {
            percent = 0;
          } else if (percent) {
            result.push([value, 0]);
          }
        } else if (i === length - 1) {
          if (percent === null) {
            percent = 1;
          } else if (percent !== 1) {
            result.push([value, percent]);
            percent = 1;
          }
        }

        result.push([value, percent]);

        if (percent === null) {
          nullIndex++;
        } else if (nullIndex) {
          const leftPercent = result[i - nullIndex - 1][1];
          const p = (percent - leftPercent) / (nullIndex + 1);
          for (let j = nullIndex; j > 0; j--) {
            result[i - j][1] = leftPercent + p * (nullIndex - j + 1);
          }

          nullIndex = 0;
        }
      }

      return result;
    }

    function getStop(stops, percent) {
      const index = uikitUtil.findIndex(stops.slice(1), (_ref3) => {let [, targetPercent] = _ref3;return percent <= targetPercent;}) + 1;
      return [
      stops[index - 1][0],
      stops[index][0],
      (percent - stops[index - 1][1]) / (stops[index][1] - stops[index - 1][1])];

    }

    function getValue(stops, percent) {
      const [start, end, p] = getStop(stops, percent);
      return uikitUtil.isNumber(start) ? start + Math.abs(start - end) * p * (start < end ? 1 : -1) : +end;
    }

    const unitRe = /^-?\d+([^\s]*)/;
    function getUnit(stops, defaultUnit) {
      for (const stop of stops) {
        const match = stop.match == null ? void 0 : stop.match(unitRe);
        if (match) {
          return match[1];
        }
      }
      return defaultUnit;
    }

    function getCssValue(el, prop, value) {
      const prev = el.style[prop];
      const val = uikitUtil.css(uikitUtil.css(el, prop, value), prop);
      el.style[prop] = prev;
      return val;
    }

    function fillObject(keys, value) {
      return keys.reduce((data, prop) => {
        data[prop] = value;
        return data;
      }, {});
    }

    var Component = {
      mixins: [Parallax],

      data: {
        selItem: '!li' },

      beforeConnect() {
        this.item = uikitUtil.query(this.selItem, this.$el);
      },

      disconnected() {
        this.item = null;
      },

      events: [
      {
        name: 'itemin itemout',

        self: true,

        el() {
          return this.item;
        },

        handler(_ref) {let { type, detail: { percent, duration, timing, dir } } = _ref;
          uikitUtil.fastdom.read(() => {
            const propsFrom = this.getCss(getCurrentPercent(type, dir, percent));
            const propsTo = this.getCss(isIn(type) ? 0.5 : dir > 0 ? 1 : 0);
            uikitUtil.fastdom.write(() => {
              uikitUtil.css(this.$el, propsFrom);
              uikitUtil.Transition.start(this.$el, propsTo, duration, timing).catch(uikitUtil.noop);
            });
          });
        } },

      {
        name: 'transitioncanceled transitionend',

        self: true,

        el() {
          return this.item;
        },

        handler() {
          uikitUtil.Transition.cancel(this.$el);
        } },

      {
        name: 'itemtranslatein itemtranslateout',

        self: true,

        el() {
          return this.item;
        },

        handler(_ref2) {let { type, detail: { percent, dir } } = _ref2;
          uikitUtil.fastdom.read(() => {
            const props = this.getCss(getCurrentPercent(type, dir, percent));
            uikitUtil.fastdom.write(() => uikitUtil.css(this.$el, props));
          });
        } }] };

    function isIn(type) {
      return uikitUtil.endsWith(type, 'in');
    }

    function getCurrentPercent(type, dir, percent) {
      percent /= 2;

      return isIn(type) ^ dir < 0 ? percent : 1 - percent;
    }

    if (typeof window !== 'undefined' && window.UIkit) {
      window.UIkit.component('slideshowParallax', Component);
    }

    return Component;

}));


text:
package br.com.ingenieux.mojo.beanstalk;

import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.junit.Ignore;

import java.io.File;

import br.com.ingenieux.mojo.beanstalk.version.RollbackVersionMojo;

@Ignore
public class RollbackVersionMojoTest extends BeanstalkTestBase {

  private RollbackVersionMojo mojo;

  @Override
  protected void setUp() throws Exception {
    super.setUp();

    File testPom = super.getBasePom("pom.xml");

    PlexusConfiguration pluginConfiguration = extractPluginConfiguration("beanstalk-maven-plugin", testPom);

    RollbackVersionMojo mojo = (RollbackVersionMojo) configureMojo(new RollbackVersionMojo(), pluginConfiguration);

    this.mojo = mojo;
  }

  public void ignoretestPreviousVersion() throws Exception {
    setVariableValueToObject(mojo, "applicationName", "belemtransito");
    setVariableValueToObject(mojo, "environmentName", "production");

    mojo.execute();
  }

  public void ignoretestLatestInstead() throws Exception {
    setVariableValueToObject(mojo, "applicationName", "belemtransito");
    setVariableValueToObject(mojo, "environmentName", "production");
    setVariableValueToObject(mojo, "latestVersionInstead", true);

    mojo.execute();
  }
}


text:
import flask

from stackalytics.openstack.common import log as logging
from stackalytics.openstack.common.py3kcompat import urlutils

LOG = logging.getLogger(__name__)

DEFAULTS = {
    'metric': 'commits',
    'release': 'icehouse',
    'project_type': 'openstack',
    'review_nth': 5,
}

METRIC_LABELS = {
    'loc': 'Lines of code',
    'commits': 'Commits',
    'marks': 'Reviews',
    'tm_marks': 'Top Mentors',
    'emails': 'Emails',
    'bpd': 'Drafted Blueprints',
    'bpc': 'Completed Blueprints',
}

METRIC_TO_RECORD_TYPE = {
    'loc': 'commit',
    'commits': 'commit',
    'marks': 'mark',
    'tm_marks': 'mark',
    'emails': 'email',
    'bpd': 'bpd',
    'bpc': 'bpc',
}

DEFAULT_RECORDS_LIMIT = 10
DEFAULT_STATIC_ACTIVITY_SIZE = 100

def get_default(param_name):
    if param_name in DEFAULTS:
        return DEFAULTS[param_name]
    else:
        return None

def get_parameter(kwargs, singular_name, plural_name=None, use_default=True):
    if singular_name in kwargs:
        p = kwargs[singular_name]
    else:
        p = flask.request.args.get(singular_name)
        if (not p) and plural_name:
            p = flask.request.args.get(plural_name)
    if p:
        return urlutils.unquote_plus(p).split(',')
    elif use_default:
        default = get_default(singular_name)
        return [default] if default else []
    else:
        return []

def get_single_parameter(kwargs, singular_name, use_default=True):
    param = get_parameter(kwargs, singular_name, use_default)
    if param:
        return param[0]
    else:
        return ''


text:
.. index:: mathematical modeling and simulation

COMP 421: Mathematical Modeling and Simulation
=======================================================

Credit Hours
-----------------------------------

3

Prerequisites
----------------------------

|math215| or :doc:`comp215` or :doc:`comp170`

|math132| or |math162|

|stat203|

Description
----------------------------

The tool of simulation is probably the most useful tool for analyzing problems
which arise in real applications which are much too complicated to be analyzed
mathematically. One example is the study of inventory control policies. When
should the inventory be checked? How many units should be ordered? What are
the anticipated consequences of these decisions? We simulate the random
character of the demand for the product and the delay in receiving orders of
the product. The objective is to provide management with relevant information
for constructing an acceptable inventory policy.

Syllabi
----------------------

No recent syllabi available.


text:
import logging
import os
import string
from types import NoneType
from haystack.backends import SQ
from haystack.inputs import Raw
from haystack.query import SearchQuerySet
import re
from nltk import corpus

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "crowdsource.settings")
import pprint
from crowdsource.settings import db_regex_char
from lost_visions.utils import db_tools
from django.core.exceptions import ObjectDoesNotExist
import operator
from django.db.models import Q
from random import randint
from django.core import serializers
from lost_visions import models

logger = logging.getLogger('lost_visions')

__author__ = 'ubuntu'

class ImagePicker():
    def __init__(self):
        # saves continually calculating this
        self.image_db_size = models.Image.objects.count()
        # print('db size {}'.format(self.image_db_size))

    # can cause multiple db accesses on django models
    def pprint_object(self, obj):
        print pprint.pformat(obj, indent=1, width=80, depth=None)

    def get_untagged_image(self, retries=0, allowed_retries=100):
        rand_image_pk = randint(1, self.image_db_size)

        # number_queries = len(connection.queries)
        try:
            an_image = models.Image.objects.get(pk=rand_image_pk)
        except ObjectDoesNotExist:
            retries += 1
            return self.get_untagged_image(retries)

        image_tag_count = models.Tag.objects.filter(image=an_image).count()

        if image_tag_count == 0 and an_image.flickr_id != '':
            # print ('before {} after {} diff {}'.format(number_queries,
            #                                            len(connection.queries),
            #                                            str(len(connection.queries) - number_queries)))
            return str(an_image.flickr_id)

        else:
            print('moving on...')

            if an_image.flickr_id == '':
                print serializers.serialize('json', [an_image, ]) + '\n'

            retries += 1
            print('retries {}'.format(retries))
            if retries > allowed_retries:
                return None

            return self.get_untagged_image(retries=retries)

    def get_untagged_images(self, number):

        image_ids = []
        for i in range(0, int(number)):
            image_ids.append(self.get_untagged_image())
        return image_ids

    def get_tagged_images(self):
        tagged_images = models.Tag.objects.order_by().values_list('image__flickr_id', flat=True).distinct()
        return tagged_images

    def get_tagged_images_for_tag(self, tag, number=None):
        tagged_images = models.Tag.objects.filter(tag__iexact=tag).order_by().values_list('image__flickr_id', flat=True).distinct()
        return tagged_images

    def get_tags_for_image(self, flickr_id):
        all_results = SearchQuerySet()
        all_results = all_results.filter(SQ(flickr_id=flickr_id))

        tags = []
        tag_with_order = []
        for res in all_results:
            # print flickr_id
            # print pprint.pformat(res.__dict__)
            # print type(res)
            # print res.tag
            if res.tag is not None:
                tags.append(res.tag)
                tag_with_order.append([res.tag, res.tag_order])
        return tags, tag_with_order

    def get_tagged_images_for_tags_again(self, tag_array, and_or='or', number=None):

        regex_string = r"{0}"

        # Using haystack for speed
        all_results = SearchQuerySet()
        ors = []

        for tag in tag_array:
            try:
                # tag = tag.encode('utf-8')
                # print tag
                if len(tag.strip()):
                    regex_format = regex_string.format(tag)

                    ors.append(SQ(tag=Raw(regex_format + '*')))
            except Exception as e6272:
                print 'e6272 : ', tag, str(e6272), type(e6272)
                # raise e6272

        if len(ors) > 0:
            try:
                all_results = all_results.filter(reduce(operator.or_, ors))
                qu = u'%s' % all_results.query
                # print qu.encode('utf-8')

                # print all_results.count()

            except Exception as e5352729:
                print 'e5352729 : ' + str(e5352729)
                # raise e5352729
        else:
            raise Exception('No tags to filter images for')

        # print all_results.query
        return list(set(all_results))

    def get_tagged_images_for_tags(self, tag_array, and_or='or', number=None):

        # only interested in images where all tags are present
        if and_or is 'and':

            # For each tag find the Tag DB objects with that tag
            tagged_images = dict()
            for tag in tag_array:
                tagged_images[tag] = (self.get_tagged_images_for_tag(tag))

            # pprint_object(tagged_images)

            # find largest list.
            # If the tag isn't in this, there's no reason looking in the others
            largest_list = []
            most_used_tag = ''
            for key_tag in tagged_images:
                if len(tagged_images[key_tag]) > len(largest_list):
                    largest_list = tagged_images[key_tag]
                    most_used_tag = key_tag

            # traverse the longest list, compare with others
            images_with_all_tags = []
            for image_in_largest_list in largest_list:
                tags_lists_image_appears_in = 0
                for key_tag in tagged_images:
                    # If the inspected list B is not the longest list A (pointless comparing A and A)
                    # and the image we're looking at from A is also in list B
                    if key_tag is not most_used_tag and image_in_largest_list in tagged_images[key_tag]:
                        # Increment the number of times we've seen it
                        tags_lists_image_appears_in += 1

                # If we say the image N-1 times, it was in every list
                # N-1 as we didn't search the longest list
                if tags_lists_image_appears_in == len(tag_array) - 1:
                    # Add image to return list
                    images_with_all_tags.append(image_in_largest_list)

            if number is not None:
                images_with_all_tags = images_with_all_tags[:number]

            return images_with_all_tags

        # assume 'or', so any one of the given tags gives a hit
        else:
            # 'reduce' the input list to a long list of "OR"s and filter for all
            tagged_images = models.Tag.objects.filter(reduce(operator.or_, (Q(tag__iexact=x) for x in tag_array))).order_by().values_list('image__flickr_id', flat=True).distinct()

        return list(set(tagged_images))

    def search_keyword(self, keyword):
        keywords = keyword.split(' ')

        ors = []
        for word in keywords:
            ors.append(Q(first_author__iregex=r"\b{0}\b".format(word)))
            ors.append(Q(date__iregex=r"\b{0}\b".format(word)))
            ors.append(Q(title__iregex=r"\b{0}\b".format(word)))
            ors.append(Q(publisher__iregex=r"\b{0}\b".format(word)))
            ors.append(Q(pubplace__iregex=r"\b{0}\b".format(word)))

        found = models.Image.objects.filter(reduce(operator.or_, ors)).values_list('book_identifier', flat=True).distinct()

        found = list(found)

        ors_tag = []
        for word in keywords:
            ors_tag.append(Q(tag__iregex=r"\b{0}\b".format(word)))

        tag_ids = models.Tag.objects.filter(reduce(operator.or_, ors_tag)) \
            .values_list('image__flickr_id', flat=True).distinct()

        found.extend(tag_ids)

        ors_usr_txt = []
        for word in keywords:

            ors_usr_txt.append(Q(caption__iregex=r"\b{0}\b".format(word)))
            ors_usr_txt.append(Q(description__iregex=r"\b{0}\b".format(word)))

        usr_txt_ids = models.ImageText.objects.filter(reduce(operator.or_, ors_usr_txt)) \
            .values_list('image__flickr_id', flat=True).distinct()

        found.extend(usr_txt_ids)

        return found

    def get_tagged_images_for_similar_tag(self, tag):
        # formatted_word = db_tools.wordnet_formatted(tag)
        #
        # # pprint_object(formatted_word)
        #
        # alternative_words = db_tools.list_wordnet_links(formatted_word[1]['synset'])
        #
        # # pprint_object(alternative_words)
        #
        # word_list = []
        # for word in alternative_words:
        #     word_list.append(word[0])
        #
        # pprint_object(word_list)

        word_list = self.get_similar_word_array(tag)

        find_tags = self.get_tagged_images_for_tags(word_list, and_or='or')
        return find_tags

    def get_similar_word_array(self, tag):
        formatted_word = db_tools.wordnet_formatted(tag)

        # pprint_object(formatted_word)

        alternative_words = db_tools.list_wordnet_links(formatted_word[1]['synset'])

        # pprint_object(alternative_words)

        word_list = []
        for word in alternative_words:
            word_list.append(word[0])

        # self.pprint_object(word_list)

        return word_list

    def get_image_from_untagged_cluster(self):
        pass

    def predict_tags_for_image_by_cluster(self):
        pass

    def advanced_search(self, request, similar_tags=False):

        # regex_string = r"\b{0}\b"

        # db_regex_char = "\y"
        regex_string = r"\b{0}\b".replace("\\b", db_regex_char)

        # print request.GET

        keywords = request.GET.get('keyword', '').strip()
        keywords = [x.strip() for x in keywords.split(' ')]

        year = request.GET.get('year', '').strip()
        author = request.GET.get('author', '').strip()
        illustrator = request.GET.get('illustrator', '').strip()
        number_of_results = request.GET.get('num_results', '').strip()
        book_id = request.GET.get('book_id', '').strip()
        publisher = request.GET.get('publisher', '').strip()
        publishing_place = request.GET.get('publishing_place', '').strip()
        title = request.GET.get('title', '').strip()

        tag_keywords_only = request.GET.get('tag_keywords_only', False)

        all_results = models.Image.objects.all()

        if len(year):
            decade = year[0:3]
            all_results = all_results.filter((Q(date__startswith=decade)))
            # filtered = True
            # readable_query += ' for the ' + year + "'s"

        if len(author):
            all_results = all_results.filter(Q(first_author__iregex=regex_string.format(author)))
            # filtered = True
            # readable_query += ' with author ' + author

        if len(title):
            all_results = all_results.filter(Q(title__iregex=regex_string.format(title)))
            # filtered = True
            # readable_query += ' with title ' + title

        if len(illustrator.strip()):
            q_or_objects = []
            for illustrator_book_id in models.BookIllustrator.objects \
                    .filter(name__iregex=regex_string.format(illustrator)).values_list('book_id', flat=True).distinct():
                if illustrator_book_id:
                    q_or_objects.append(Q(book_identifier=str(illustrator_book_id)))

            if len(q_or_objects) > 0:
                all_results = all_results.filter(reduce(operator.or_, q_or_objects))

                # filtered = True
                # readable_query += ' with illustrator ' + illustrator

        if len(book_id):
            all_results = all_results.filter(book_identifier=book_id)
            # filtered = True
            # title = models.Image.objects.values_list('title', flat=True).filter(book_identifier=book_id)[:1].get()
            # readable_query += ' for book title ' + title

        if len(publisher):
            all_results = all_results.filter(Q(publisher__iregex=regex_string.format(publisher)))
            filtered = True
            # readable_query += ' from publisher ' + publisher

        if len(publishing_place):
            all_results = all_results.filter(Q(pubplace__iregex=regex_string.format(publishing_place)))
            filtered = True
            # readable_query += ' published in ' + publishing_place

        if similar_tags:
            similar_words = []
            for word in keywords:
                if len(word):
                    similar_words.extend(self.get_similar_word_array(word))
            keywords = similar_words

        # print '*' + str(keywords) + '*'
        for word in keywords:
            if len(word):
                # all_results = self.filter_all_on_tag(all_results, word)

                regex_format = regex_string.format(word)
                ors = [
                    Q(tag__tag__iregex=regex_format),
                    Q(imagetext__caption__iregex=regex_format),
                    Q(imagetext__description__iregex=regex_format),
                    ]

                if not tag_keywords_only:
                    ors += [
                        Q(first_author__iregex=regex_format),
                        Q(date__iregex=regex_format),
                        Q(title__iregex=regex_format),
                        Q(publisher__iregex=regex_format),
                        Q(pubplace__iregex=regex_format),

                        ]

                all_results = all_results.filter(reduce(operator.or_, ors))

        all_results = all_results.values_list('flickr_id', flat=True).distinct()[:500]
        logger.debug(all_results.query)
        return all_results

    def advanced_haystack_search(self, query_items, similar_tags=False, book_ids=None):

        regex_string = r"{0}"

        print query_items
        logger.debug(query_items)

        keywords = query_items.get('keyword', '').strip()
        keywords = [x.strip() for x in keywords.split(' ')]

        print keywords
        clean_keywords = []

        stop = corpus.stopwords.words('english')
        for word in keywords:
            if word not in stop:
                clean_keywords.append(word)

        keywords = clean_keywords

        print 'keywords', keywords

        year = query_items.get('year', '').strip()
        author = query_items.get('author', '').strip()
        illustrator = query_items.get('illustrator', '').strip()
        number_of_results = query_items.get('num_results', '').strip()
        book_id = query_items.get('book_id', '').strip()
        publisher = query_items.get('publisher', '').strip()
        publishing_place = query_items.get('publishing_place', '').strip()
        title = query_items.get('title', '').strip()

        tag_keywords_only = query_items.get('tag_keywords_only', False)

        all_results = SearchQuerySet()
        print all_results.count()

        # # This should work, but it makes solr throw "too many boolean clauses"
        # # if the user selects too many years/ books
        # if book_ids is not None and len(book_ids) > 0:
        #     print len(book_ids), type(book_ids)
        #     # image_ids_from_books = []
        #     # if x.book_identifier in book_ids:
        #     #     image_ids_from_books.append(x.flickr_id)
        #     all_results = all_results.filter(book_identifier__in=book_ids)

        if len(year):
            decade = year[0:3]
            all_results = all_results.filter(SQ(date__startswith=decade))

        if len(author):
            # all_results = all_results.filter_or(first_author__icontains='*' + author + '*')
            author_words = [x.strip() for x in author.split(' ')]
            for author_word in author_words:
                re.sub(r'\W+', '', author_word)
                all_results = all_results.filter(first_author__icontains='*' + author_word + '*')

        if len(title):
            # all_results = all_results.filter_or(title__icontains='*"' + title + '"*')
            title_words = [x.strip() for x in title.split(' ')]
            for title_word in title_words:
                re.sub(r'\W+', '', title_word)
                all_results = all_results.filter(title__icontains='*' + title_word + '*')
            # all_results = all_results.filter(SQ(title=Raw('*' + title + '*')))

        if len(illustrator):
            # all_results = all_results.filter_or(first_author__icontains='*' + author + '*')
            illustrator_words = [x.strip() for x in illustrator.split(' ')]
            for illustrator_word in illustrator_words:
                re.sub(r'\W+', '', illustrator_word)
                all_results = all_results.filter(title__icontains='*' + illustrator_word + '*')

            # q_or_objects = []
            # for illustrator_book_id in models.BookIllustrator.objects \
            #         .filter(name__contains=regex_string.format(illustrator)).values_list('book_id', flat=True).distinct():
            #     if illustrator_book_id:
            #         q_or_objects.append(SQ(book_identifier=str(illustrator_book_id)))

            # if len(q_or_objects) > 0:
            #     all_results = all_results.filter(reduce(operator.or_, q_or_objects))

        if len(book_id):
            all_results = all_results.filter(book_identifier=book_id)

        if len(publisher):
            publisher_words = [x.strip() for x in publisher.split(' ')]
            for publisher_word in publisher_words:
                re.sub(r'\W+', '', publisher_word)
                all_results = all_results.filter(publisher__icontains='*' + publisher_word + '*')
            # all_results = all_results.filter(SQ(publisher=Raw('*' + publisher + '*')))

        if len(publishing_place):
            publishing_place_words = [x.strip() for x in publishing_place.split(' ')]
            for publishing_place_word in publishing_place_words:
                re.sub(r'\W+', '', publishing_place_word)
                all_results = all_results.filter(pubplace__icontains='*' + publishing_place_word + '*')
            # all_results = all_results.filter(SQ(pubplace=Raw('*' + publishing_place + '*')))

        if similar_tags:
            similar_words = []
            for word in keywords:
                if len(word):
                    similar_words.extend(self.get_similar_word_array(word))
            keywords = similar_words

        for word in keywords:
            if len(word):
                # Create seperate querysets for each keyword with all the possible ORs
                # then use | to get the union of the queysets with the main set from above

                all_results_for_ANDs = SearchQuerySet()

                all_results_for_ANDs = all_results_for_ANDs.filter_or(tag__icontains=word)
                all_results_for_ANDs = all_results_for_ANDs.filter_or(caption__icontains=word)
                all_results_for_ANDs = all_results_for_ANDs.filter_or(description__icontains=word)

                if not tag_keywords_only:
                    all_results_for_ANDs = all_results_for_ANDs.filter_or(first_author__icontains=word)
                    all_results_for_ANDs = all_results_for_ANDs.filter_or(date__icontains=word)
                    all_results_for_ANDs = all_results_for_ANDs.filter_or(title__icontains=word)
                    all_results_for_ANDs = all_results_for_ANDs.filter_or(publisher__icontains=word)
                    all_results_for_ANDs = all_results_for_ANDs.filter_or(pubplace__icontains=word)

                all_results = all_results & all_results_for_ANDs

                # all_results = all_results.filter_or(tag__icontains=word)
                # all_results = all_results.filter_or(caption__icontains=word)
                # all_results = all_results.filter_or(description__icontains=word)
                #
                # if not tag_keywords_only:
                #     all_results = all_results.filter_or(first_author__icontains=word)
                #     all_results = all_results.filter_or(date__icontains=word)
                #     all_results = all_results.filter_or(title__icontains=word)
                #     all_results = all_results.filter_or(publisher__icontains=word)
                #     all_results = all_results.filter_or(pubplace__icontains=word)

        sort_by = query_items.get('sort_results', '').strip()
        # print 'sorting by', sort_by
        if sort_by == 'none' or len(sort_by) < 1:
            pass
        elif sort_by == 'title':
            # print 'title found'
            all_results = all_results.order_by('title')
        elif sort_by == 'date':
            # print 'date found'
            all_results = all_results.order_by('date')
        elif sort_by == 'author':
            # print 'author found'
            all_results = all_results.order_by('first_author')

        logger.debug(all_results.query)
        # print 'full query', all_results.query
        logger.debug(all_results.count())
        # print 'full result count', all_results.count()
        return all_results

    # A method to find the overall tag weight for a tag
    # taking into account the tag order, for multiple taggers
    def get_tag_order_weight(self, tag_orders, tag):
        tag_weight_total = 0

        # For each tag order in the list, add up the total weight for this tag
        for tag_order in tag_orders:
            if len(tag_order) < 9:
                # print tag_order + ' becomes 100100100'
                # Some tags have the old system of 0, 1, 2, 3...
                # assume they are equally important "first order" tags
                tag_order = '100100100'

            user_order = ''.join(tag_order[0:3])
            hypernym_order = ''.join(tag_order[3:6])
            synonym_order = ''.join(tag_order[6:9])

            user_order = int(user_order.lstrip('0').rstrip('0'))
            hypernym_order = int(hypernym_order.lstrip('0').rstrip('0'))
            # For future reference, synonyms are currently considered equal
            synonym_order = int(synonym_order.lstrip('0').rstrip('0'))

            # multiply 1/ user order by 1/ hypernym order
            # eg 100100100, 200200200, 300300300 =
            # ( 1/1 * 1/1 ) + ( 1/2 * 1/2 ) + ( 1/3 * 1/3 )
            # 1 + 1/4 + 1/9 = 49/36 = 1.361
            tag_weight_total += (1/float(user_order)) * (1/float(hypernym_order))

        # print tag, tag_order, user_order, hypernym_order, synonym_order, tag_weight
        return tag_weight_total

    # A giant method to find all the images similar to "Image Alpha" based on it's tags
    def get_similar_images_with_tags(self, id_alpha):

        # Get alpha images tags and their tag order
        # Multiple users may use same tag with different tagging order
        tags_for_image = models.Tag.objects.all().filter(image__flickr_id=id_alpha).values('tag', 'tag_order')
        tags_for_image = list(tags_for_image)

        # Sort them by tagging order, for visuals only
        # ie 100100100 < 200300400 < 300400200
        tags_for_image = sorted(tags_for_image, key=lambda image: image['tag_order'])

        # print 'alpha tags : ' + pprint.pformat(tags_for_image)

        weighted_tags_for_alpha_image = {}

        # For each tag in the alpha set, look through the set and grab all tag orders
        for alpha_tag in tags_for_image:
            # Don't search for blank tags
            if len(alpha_tag['tag']):
                # Only record if we haven't already seen this tag
                if weighted_tags_for_alpha_image.get(alpha_tag['tag'], None) is None:
                    weighted_tag = {}
                    tag_orders = []

                    # We have an unseen tag from the alpha image
                    # check through all tags and see if it appears again
                    # append all tag orders to a list
                    for alpha_tag2 in tags_for_image:
                        if alpha_tag2['tag'] == alpha_tag['tag']:
                            tag_orders.append(str(alpha_tag2['tag_order']))

                    # For alpha tag we record a list of tag orders
                    # and the "weight" of this tag overall
                    weighted_tag['tag_orders'] = tag_orders
                    weighted_tag['weight'] = self.get_tag_order_weight(tag_orders, alpha_tag['tag'])
                    weighted_tags_for_alpha_image[alpha_tag['tag']] = weighted_tag

        # print 'weighted alpha tags : ' + pprint.pformat(weighted_tags_for_alpha_image, indent=4)

        to_search = []

        # removing tags with spaces, as it breaks search for some reason
        # TODO figure this out
        for tag in weighted_tags_for_alpha_image.keys():
            if ' ' not in tag:
                to_search.append(tag)

        # print pprint.pformat(to_search)

        # im = ImagePicker()
        images_with_tag = []
        try:
            # Get images which have any of the tags found in the alpha image
            images_with_tag = self.get_tagged_images_for_tags_again(to_search)
        except Exception as e76275623:
            # print 'getting similar tagged images ' + str(e76275623)
            pass

        to_join = []

        discovered = []

        count = 0

        for x in images_with_tag:
            count += 1
            count2 = 0
            matched2 = []
            all_tags_for_image = []
            matched2_with_order = []

            # Haystack can be weird,
            # so we need to check this item has a flickr_id, isn't the alpha image, and isn't a repeat
            if type(x.flickr_id) is not NoneType and x.flickr_id != id_alpha and x.flickr_id not in to_join:
                to_join.append(x.flickr_id)

                # Get all the tags for this image
                tags_for_image2, tags_with_order = self.get_tags_for_image(x.flickr_id)

                # print 'tags_with_order : ' + str(tags_with_order)
                # Count the number of tags which are matched between this image and the alpha image
                for tag2 in tags_for_image2:
                    all_tags_for_image.append(tag2)
                    if tag2 in to_search and tag2 not in matched2:
                        count2 += 1
                        matched2.append(tag2)

                #         Try and store the tag_orders at the same time
                for tag2 in tags_with_order:
                    if tag2[0] in to_search and tag2[0] not in matched2_with_order:
                        count2 += 1
                        matched2_with_order.append({
                            'tag': tag2[0],
                            'tag_order': tag2[1]
                        })

                #         This could be tweaked to ensure at least 2-3 matching tags
                if count2 > 0:

                    # Record the number of matches and the list of shared tags
                    discovered.append({
                        'flickr_id': x.flickr_id,
                        'matches': count2,
                        'tags': matched2,
                        'all_tags': all_tags_for_image,
                        'all_tags_with_order': matched2_with_order
                    })

        # Sort by the number of matches
        # Assuming that number of matching tags is most relevent here, to cut down on processing
        sorted_tag_matches = sorted(discovered, key=lambda image: image['matches'], reverse=True)[:15]

        # print '\nFOUND MATCHES\n'
        # print pprint.pformat(sorted_tag_matches)

        # Now we need the weighted sums for all these matching-images tag-orders
        for tag_set in sorted_tag_matches:
            # print '\n'
            # print pprint.pformat(tag_set)

            weighted_tag_sum = 0

            # tags_for_found_image = []
            #
            # for image_tag in tag_set['tags']:
            #     # Get all the tags from the image, with tag orders
            #     # TODO do this in the DB hit above?
            #     tag = models.Tag.objects.filter(image__flickr_id=tag_set['flickr_id']).filter(tag=image_tag)
            #     for tag_orders in tag:
            #         # print image_tag
            #         # print tag_orders.tag_order
            #         tags_for_found_image.append({
            #             'tag': image_tag,
            #             'tag_order': tag_orders.tag_order
            #         })

            # Organise the weights of the tags
            weighted_tags_for_found_image = {}

            # TODO does this make things better?
            # for image_tag in tags_for_found_image:
            for image_tag in tag_set['all_tags_with_order']:

                if weighted_tags_for_found_image.get(image_tag['tag'], None) is None:
                    weighted_tag = {}
                    tag_orders = []

                    # As above with the Alpha image, get the weight for the tag
                    for image_tag2 in tags_for_image:
                        if image_tag2['tag'] == image_tag['tag']:
                            tag_orders.append(str(image_tag2['tag_order']))

                    weighted_tag['tag_orders'] = tag_orders
                    weighted_tag['weight'] = self.get_tag_order_weight(tag_orders, image_tag)
                    weighted_tags_for_found_image[image_tag['tag']] = weighted_tag

                    # This is special
                    # Multiply the weighted sum of the tag for this image
                    # by the weight for the tag in the alpha image
                    # Add the result of this to the weights for all tags for this image
                    weighted_tag_sum += weighted_tag['weight'] * weighted_tags_for_alpha_image[image_tag['tag']]['weight']

            # Add these tags and weights to the discovered image dict
            for discovered_image in sorted_tag_matches:
                if discovered_image['flickr_id'] == tag_set['flickr_id']:
                    discovered_image['weighted_importance'] = weighted_tag_sum
                    discovered_image['weighted_tags'] = weighted_tags_for_found_image

                    # print pprint.pformat(weighted_tags_for_found_image)

        # print '\nFOUND MATCHES\n'
        # print pprint.pformat(sorted_tag_matches)

        # Sort by overall calculated weight
        # print '\nWEIGHTED DISCOVERIES\n'
        sorted_discovered = sorted(sorted_tag_matches, key=lambda image: image['weighted_importance'], reverse=True)
        # print pprint.pformat(sorted_discovered)

        # Bin all that info and just return a list of flickr_IDs
        # Shame, this is probably decent stuff.
        finals = []
        for final_found in sorted_discovered:
            finals.append(final_found['flickr_id'])
            # print final_found['flickr_id'], final_found['weighted_importance']

        return finals

    def find_collections(self, request_get):
        keywords = request_get.get('keyword', '').strip()
        keywords = [x.strip() for x in keywords.split(' ') if len(x)]

        # print keywords
        clean_keywords = []
        stop = corpus.stopwords.words('english')
        for word in keywords:
            if word not in stop:
                clean_keywords.append(word)
        keywords = clean_keywords

        if len(keywords) == 0:
            return models.ImageCollection.objects.none()

        all_results = models.ImageCollection.objects.all()

        ors = []
        regex_string = r"\b{0}\b".replace("\\b", db_regex_char)

        for word in keywords:
            if len(word):
                regex_format = regex_string.format(word)
                ors.append(Q(name__icontains=word))

        filtered_collections = all_results.filter(reduce(operator.or_, ors))
        # print filtered_collections.query

        collection_tags = models.CollectionTag.objects.all()
        tag_ors = []

        for word in keywords:
            if len(word):
                tag_ors.append(Q(tag__icontains=word))

        collection_tags = collection_tags.filter(reduce(operator.or_, tag_ors)).values_list('collection', flat=True).distinct()
        collection_tag_ors = []
        for collection_id in collection_tags:
            collection_tag_ors.append(Q(id=collection_id))

        if len(collection_tag_ors):
            collections_with_tags = models.ImageCollection.objects.filter(reduce(operator.or_, collection_tag_ors))
            # print collections_with_tags.query
        else:
            collections_with_tags = models.ImageCollection.objects.none()

        return filtered_collections | collections_with_tags

class Request():
    def __init__(self):
        self.GET = {
            # 'author': 'BETTANY',
            'keyword': 'tree water'
        }


text:
// process.env.DEBUG = 'minecraft-protocol raknet'
const { Client } = require('./client')
const { Server } = require('./server')
const { Player } = require('./serverPlayer')
const debug = require('debug')('minecraft-protocol relay')
const { serialize } = require('./datatypes/util')

/** @typedef {{ hostname: string, port: number, auth: 'client' | 'server' | null, destination?: { hostname: string, port: number } }} Options  */

const debugging = true // Do re-encoding tests

class RelayPlayer extends Player {
  constructor (server, conn) {
    super(server, conn)
    this.server = server
    this.conn = conn

    this.startRelaying = false
    this.once('join', () => { // The client has joined our proxy
      this.flushDownQueue() // Send queued packets from the upstream backend
      this.startRelaying = true
    })
    this.downQ = []
    this.upQ = []
    this.upInLog = (...msg) => console.info('** Backend -> Proxy', ...msg)
    this.upOutLog = (...msg) => console.info('** Proxy -> Backend', ...msg)
    this.downInLog = (...msg) => console.info('** Client -> Proxy', ...msg)
    this.downOutLog = (...msg) => console.info('** Proxy -> Client', ...msg)

    if (!server.options.logging) {
      this.upInLog = () => { }
      this.upOutLog = () => { }
      this.downInLog = () => { }
      this.downOutLog = () => { }
    }

    // this.upOutLog = (...msg) => {
    //   if (msg.includes('player_auth_input')) {
    //     // stream.write(msg)
    //     console.info('INPUT', msg)
    //   }
    // }

    this.outLog = this.downOutLog
    this.inLog = this.downInLog
  }

  // Called when we get a packet from backend server (Backend -> PROXY -> Client)
  readUpstream (packet) {
    if (!this.startRelaying) {
      console.warn('The downstream client is not ready yet !!')
      this.downQ.push(packet)
      return
    }
    this.upInLog('Recv packet', packet)
    const des = this.server.deserializer.parsePacketBuffer(packet)
    const name = des.data.name
    const params = des.data.params
    this.upInLog('~ Bounce B->C', name, serialize(params).slice(0, 100))
    // this.upInLog('~ ', des.buffer)
    if (name === 'play_status' && params.status === 'login_success') return // We already sent this, this needs to be sent ASAP or client will disconnect

    if (debugging) { // some packet encode/decode testing stuff
      const rpacket = this.server.serializer.createPacketBuffer({ name, params })
      if (rpacket.toString('hex') !== packet.toString('hex')) {
        console.warn('New', rpacket.toString('hex'))
        console.warn('Old', packet.toString('hex'))
        console.log('Failed to re-encode', name, params)
        process.exit(1)
      }
    }

    this.queue(name, params)
    // this.sendBuffer(packet)
  }

  // Send queued packets to the connected client
  flushDownQueue () {
    for (const packet of this.downQ) {
      const des = this.server.deserializer.parsePacketBuffer(packet)
      this.write(des.data.name, des.data.params)
    }
    this.downQ = []
  }

  // Send queued packets to the backend upstream server from the client
  flushUpQueue () {
    for (const e of this.upQ) { // Send the queue
      const des = this.server.deserializer.parsePacketBuffer(e)
      if (des.data.name === 'client_cache_status') { // Currently broken, force off the chunk cache
        this.upstream.write('client_cache_status', { enabled: false })
      } else {
        this.upstream.write(des.data.name, des.data.params)
      }
    }
    this.upQ = []
  }

  // Called when the server gets a packet from the downstream player (Client -> PROXY -> Backend)
  readPacket (packet) {
    if (this.startRelaying) { // The downstream client conn is established & we got a packet to send to upstream server
      if (!this.upstream) { // Upstream is still connecting/handshaking
        this.downInLog('Got downstream connected packet but upstream is not connected yet, added to q', this.upQ.length)
        this.upQ.push(packet) // Put into a queue
        return
      }
      this.flushUpQueue() // Send queued packets
      this.downInLog('Recv packet', packet)
      // TODO: If we fail to parse a packet, proxy it raw and log an error
      const des = this.server.deserializer.parsePacketBuffer(packet)

      if (debugging) { // some packet encode/decode testing stuff
        const rpacket = this.server.serializer.createPacketBuffer(des.data)
        if (rpacket.toString('hex') !== packet.toString('hex')) {
          console.warn('New', rpacket.toString('hex'))
          console.warn('Old', packet.toString('hex'))
          console.log('Failed to re-encode', des.data)
          process.exit(1)
        }
      }

      switch (des.data.name) {
        case 'client_cache_status':
          this.upstream.queue('client_cache_status', { enabled: false })
          break
        default:
          // Emit the packet as-is back to the upstream server
          // this.upstream.queue(des.data.name, des.data.params)
          this.downInLog('Relaying', des.data)
          this.upstream.sendBuffer(packet)
      }
    } else {
      super.readPacket(packet)
    }
  }
}

class Relay extends Server {
  /**
   * Creates a new non-transparent proxy connection to a destination server
   * @param {Options} options
   */
  constructor (options) {
    super(options)
    this.RelayPlayer = options.relayPlayer || RelayPlayer
    this.forceSingle = true
    this.upstreams = new Map()
  }

  openUpstreamConnection (ds, clientAddr) {
    const client = new Client({
      hostname: this.options.destination.hostname,
      port: this.options.destination.port,
      encrypt: this.options.encrypt,
      autoInitPlayer: false
    })
    client.outLog = ds.upOutLog
    client.inLog = ds.upInLog
    // console.log('Set upstream logs', client.outLog, client.inLog)
    client.once('join', () => { // Intercept once handshaking done
      ds.upstream = client
      ds.flushUpQueue()
      console.log('Connected to upstream server')
      client.readPacket = (packet) => ds.readUpstream(packet)
    })
    this.upstreams.set(clientAddr.hash, client)
  }

  closeUpstreamConnection (clientAddr) {
    const up = this.upstreams.get(clientAddr.hash)
    if (!up) throw Error(`unable to close non-open connection ${clientAddr.hash}`)
    up.close()
    this.upstreams.delete(clientAddr.hash)
    debug('relay closed connection', clientAddr)
  }

  onOpenConnection = (conn) => {
    debug('new connection', conn)
    if (this.forceSingle && this.clientCount > 0) {
      debug('dropping connection as single client relay', conn)
      conn.close()
    } else {
      const player = new this.RelayPlayer(this, conn)
      console.debug('New connection from', conn.address)
      this.clients[conn.address] = player
      this.emit('connect', { client: player })
      this.openUpstreamConnection(player, conn.address)
    }
  }
}

// Too many things called 'Proxy' ;)
module.exports = { Relay }


text:
# Copyright 2008 The Tor Project, Inc.  See LICENSE for licensing information.
# Copyright 2010 The Update Framework.  See LICENSE for licensing information.

import gzip
import os
import re
import shutil
import sys
import tempfile

import tuf.log
import tuf.conf

logger = tuf.log.get_logger()

_json_module = None

def import_json():
    global _json_module
    if _json_module is not None:
        return _json_module

    for name in [ "json", "simplejson" ]:
        try:
            mod = __import__(name)
        except ImportError:
            continue
        if not hasattr(mod, "dumps"):
            # Some versions of Ubuntu have a module called 'json' that is
            # not a recognizable simplejson module.  Naughty.
            if name == 'json':
                logger.warn("Your operating system has a nonfunctional json "
                            "module.  That's going to break any programs that "
                            "use the real json module in Python 2.6.  Trying "
                            "simplejson instead.")
            continue

        # Some old versions of simplejson escape / as \/ in a misguided and
        # inadequate attempt to fix XSS attacks.  Make them not do that.  This
        # code is not guaranteed to work on all broken versions of simplejson:
        # it replaces an entry in the internal character-replacement
        # dictionary so that "/" is translated to itself rather than to \/.
        # We also need to make sure that ensure_ascii is False, so that we
        # do not call the C-optimized string encoder in these broken versions,
        # which we can't fix easily.  Both parts are a kludge.
        try:
            escape_dct = mod.encoder.ESCAPE_DCT
        except NameError:
            pass
        else:
            if escape_dct.has_key("/"):
                escape_dct["/"] = "/"
                save_dumps = mod.dumps
                save_dump = mod.dump
                def dumps(*k, **v):
                    v['ensure_ascii'] = False
                    return save_dumps(*k, **v)
                def dump(*k, **v):
                    v['ensure_ascii'] = False
                    return save_dump(*k, **v)
                mod.dump = dump
                mod.dumps = dumps
                logger.warn("Your operating system has an old broken "
                            "simplejson module.  I tried to fix it for you.")

        _json_module = mod
        return mod

    raise ImportError("Couldn't import a working json module")

json = import_json()

def move_file(fromLocation, toLocation):
    """Move the file from fromLocation to toLocation, removing any file
       in toLocation.
    """
    if sys.platform in ('cygwin', 'win32'):
        # Win32 doesn't let rename replace an existing file.
        try:
            os.unlink(toLocation)
        except OSError:
            pass

    os.rename(fromLocation, toLocation)

def replace_file(fname, contents, textMode=False):
    """overwrite the file in 'fname' atomically with the content of 'contents'
    """
    dir, prefix = os.path.split(fname)
    fd, fname_tmp = tempfile.mkstemp(prefix=prefix, dir=dir, text=textMode)

    try:
        os.write(fd, contents)
    finally:
        os.close(fd)

    move_file(fname_tmp, fname)

def user_filename(name):
    """Return a path relative to $TUF_HOME or ~/.tuf whose final path
       component is 'name', creating parent directories as needed."""
    try:
        base = os.environ["TUF_HOME"]
    except KeyError:
        base = "~/.tuf"

    base = os.path.expanduser(base)
    result = os.path.normpath(os.path.join(base, name))
    ensure_parent_dir(result)
    return result

def get_file_details(filename):
    """Get length and hash information about a file.

    Args:
        filename: The path to a file.

    Returns:
        A tuple of (length, hashes) describing filename.
    """
    length = os.path.getsize(filename)

    d_obj = tuf.hash.Digest('sha256')
    d_obj.update_filename(filename)

    hashes = {'sha256' : d_obj.format()}
    return length, hashes

class TempFile(object):
    """A high-level temporary file that cleans itself up or can be manually
       cleaned up. This isn't a complete file-like object. The file functions
       that are supported make additional common-case safe assumptions. There
       are additional functions that aren't part of file-like objects."""

    def __init__(self, prefix='tmp'):
        self._compression = None
        self._orig_file = None
        temp_dir = tuf.conf.settings.temp_dir
        if temp_dir is not None:
            # We use TemporaryFile for the auto-delete aspects of it to ensure
            # we don't leave behind temp files.
            try:
                self._file = tempfile.TemporaryFile(prefix=prefix, dir=temp_dir)
            except OSError, err:
                logger.error("Temp file in %s failed: %s " % (temp_dir, err))
                logger.error("Will attempt to use system default temp dir.")

        try:
            self._file = tempfile.TemporaryFile(prefix=prefix)
        except OSError, err:
            logger.critical("Temp file in %s failed: %s " % (temp_dir, err))
            # TODO: raise an exception that indicates an unrecoverable error.

    def delete(self):
        self._file.close()
        # If compression has been set, we need to explicitly close the original
        # file object.
        if self._orig_file is not None:
            self._orig_file.close()

    def flush(self):
        self._file.flush()

    def move(self, dest_path):
        """Copies a temp file at src to a non-temp file at dst and then closes
           the temp file so that it is removed."""
        self.flush()
        self.seek(0)
        f_dst = open(dest_path, 'wb')
        shutil.copyfileobj(self._file, f_dst)
        f_dst.close()
        self.delete()

    def read(self, size=None):
        """If no size specified, read the whole file and leave the file pointer
           at the beginning of the file."""
        if size is None:
            self._file.seek(0)
            data = self._file.read()
            self._file.seek(0)
            return data
        else:
            return self._file.read(size)

    def seek(self, *args):
        self._file.seek(*args)

    def write(self, str, auto_flush=True):
        self._file.write(str)
        if auto_flush:
            self.flush()

    def set_compression(self, compression):
        """Basic functionality for dealing with a TempFile that is compressed.
           After calling this, write() can no longer be called."""
        if self._orig_file is not None:
            raise tuf.Error("Can only set compression on a TempFile once.")
        if compression != 'gzip':
            raise tuf.Error("Only gzip compression is supported.")
        self.seek(0)
        self._compression = compression
        self._orig_file = self._file
        self._file = gzip.GzipFile(fileobj=self._file, mode='rb')

def load_json_string(data):
    return json.loads(data)

def load_json_file(filename):
    fp = open(filename)
    try:
        return json.load(fp)
    finally:
        fp.close()

def ensure_parent_dir(name):
    """If the parent directory of 'name' does not exist, create it."""
    directory = os.path.split(name)[0]
    if directory and not os.path.exists(directory):
        os.makedirs(directory, 0700)

_path_regex_cache = {}

def path_in_patterns(path, pattern_list):
    """Return true iff the relative path in the filesystem 'path' conforms
       to the pattern 'rolePath': a path that a given key is
       authorized to sign.  Patterns are allowed to contain * to
       represent one or more characters in a filename, and ** to
       represent any level of directory structure.

       Part of the reason for making it a pattern list instead of a pattern
       is that we always end up dealing with pattern lists. The other part
       of the reason is that it's nice to force the arguments to be of
       different types to prevent callers mixing the two up and somehow not
       noticing.

    >>> path_in_patterns("a/b/c/", ["a/b/c/"])
    True
    >>> path_in_patterns("**/c.*", ["a/b/c.txt"])
    True
    >>> path_in_patterns("**/c.*", ["a/b/ctxt"])
    False
    >>> path_in_patterns("**/c.*", ["a/b/c.txt/foo"])
    False
    >>> path_in_patterns("a/*/c", ["a/b/c"])
    True
    >>> path_in_patterns("a/*/c", ["a/b/c.txt"])
    False
    >>> path_in_patterns("a/*/c", ["a/b/c.txt"]) #Check cache
    False
    """
    if not isinstance(path, basestring):
        raise TypeError("The path must be a string.")
    if not isinstance(pattern_list, (list, tuple)):
        raise TypeError("The pattern_list must be a list or a tuple.")

    for pattern in pattern_list:
        # Ignore slashes at the beginning.
        pattern = pattern.lstrip('/')
        try:
            regex = _path_regex_cache[pattern]
        except KeyError:
            orig = pattern
            # Remove duplicate slashes.
            pattern = re.sub(r'/+', '/', pattern)
            # Escape, then ** becomes .*
            pattern = re.escape(pattern).replace(r'\*\*', r'.*')
            # * becomes [^/]*
            pattern = pattern.replace(r'\*', r'[^/]*')
            # No extra text is allowed.
            pattern = '^' + pattern + '$'
            regex = _path_regex_cache[orig] = re.compile(pattern)

        if regex.match(path) != None:
            return True

    return False


text:
package loon.jni;

import com.google.gwt.core.client.JavaScriptObject;

public final class XDomainRequest extends JavaScriptObject {

	public static interface Handler {
		void onError(XDomainRequest xdr);

		void onLoad(XDomainRequest xdr);

		void onProgress(XDomainRequest xdr);

		void onTimeout(XDomainRequest xdr);
	}

	public static native XDomainRequest create() /*-{
													return new $wnd.XDomainRequest();
													}-*/;

	protected XDomainRequest() {
	}

	public native void abort() /*-{
								this.abort();
								}-*/;

	public native String getResponseText() /*-{
											return this.responseText;
											}-*/;

	public native String getStatus() /*-{
										return this.contentType;
										}-*/;

	public native int getTimeout() /*-{
									return this.timeout;
									}-*/;

	public native void open(String httpMethod, String url) /*-{
															this.open(httpMethod, url, true);
															}-*/;

	public native void send() /*-{
								this.send();
								}-*/;

	public native void send(String requestData) /*-{
												this.send(requestData);
												}-*/;

	public native void setHandler(Handler handler) /*-{

													var _this = this;

													this.onerror = $entry(function() {
													handler.@loon.jni.XDomainRequest.Handler::onError(Lloon/jni/XDomainRequest;)(_this);
													});

													this.onload = $entry(function() {
													handler.@loon.jni.XDomainRequest.Handler::onLoad(Lloon/jni/XDomainRequest;)(_this);
													});

													this.onprogress = $entry(function() {
													handler.@loon.jni.XDomainRequest.Handler::onProgress(Lloon/jni/XDomainRequest;)(_this);
													});

													this.ontimeout = $entry(function() {
													handler.@loon.jni.XDomainRequest.Handler::onTimeout(Lloon/jni/XDomainRequest;)(_this);
													});
													}-*/;
}