text:
require 'base64'
require 'eventmachine'
require 'digest/md5'
require 'thin'

module Skinny
  module Callbacks
    def self.included base
      base.class_eval do
        extend ClassMethods
        include InstanceMethods
      end
    end

    module ClassMethods
      def define_callback *names
        names.each do |name|
          define_method name do |&block|
            add_callback name, &block
          end
        end
      end
    end

    module InstanceMethods
      def add_callback name, &block
        @callbacks ||= {}
        @callbacks[name] ||= []
        @callbacks[name] << block
      end

      def callback name, *args, &block
        return [] if @callbacks.nil? || @callbacks[name].nil?
        @callbacks[name].collect { |callback| callback.call *args, &block }
      end
    end
  end

  class WebSocketError < RuntimeError; end
  class WebSocketProtocolError < WebSocketError; end

  # We need to be really careful not to throw an exception too high
  # or we'll kill the server.
  class Websocket < EventMachine::Connection
    include Callbacks
    include Thin::Logging

    define_callback :on_open, :on_start, :on_handshake, :on_message, :on_error, :on_finish, :on_close

    # 4mb is almost too generous, imho.
    MAX_BUFFER_LENGTH = 2 ** 32

    GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

    OPCODE_CONTINUATION = 0x00
    OPCODE_TEXT = 0x01
    OPCODE_BINARY = 0x02
    OPCODE_CLOSE = 0x08
    OPCODE_PING = 0x09
    OPCODE_PONG = 0x0a

    # Create a new WebSocket from a Thin::Request environment
    def self.from_env env, options={}
      # Pull the connection out of the env
      thin_connection = env[Thin::Request::ASYNC_CALLBACK].receiver
      # Steal the IO
      fd = thin_connection.detach
      # EventMachine 1.0.0 needs this to be closable
      io = IO.for_fd(fd) unless fd.respond_to? :close
      # We have all the events now, muahaha
      EM.attach(io, self, env, options)
    end

    def initialize env, options={}
      @env = env.dup
      @buffer = ''

      @protocol = options.delete :protocol if options.has_key? :protocol
      [:on_open, :on_start, :on_handshake, :on_message, :on_error, :on_finish, :on_close].each do |name|
        send name, &options.delete(name) if options.has_key?(name)
      end
      raise ArgumentError, "Unknown options: #{options.inspect}" unless options.empty?
    end

    # Connection is now open
    def post_init
      EM.next_tick { callback :on_open, self rescue error! "Error in open callback" }
      @state = :open
    rescue
      error! "Error opening connection"
    end

    # Return an async response -- stops Thin doing anything with connection.
    def response
      Thin::Connection::AsyncResponse
    end

    # Arrayify self into a response tuple
    alias :to_a :response

    # Start the websocket connection
    def start!
      # Steal any remaining data from rack.input
      @buffer = @env[Thin::Request::RACK_INPUT].read + @buffer

      # Remove references to Thin connection objects, freeing memory
      @env.delete Thin::Request::RACK_INPUT
      @env.delete Thin::Request::ASYNC_CALLBACK
      @env.delete Thin::Request::ASYNC_CLOSE

      # Figure out which version we're using
      @version = @env['HTTP_SEC_WEBSOCKET_VERSION']
      @version ||= "hixie-76" if @env.has_key?('HTTP_SEC_WEBSOCKET_KEY1') and @env.has_key?('HTTP_SEC_WEBSOCKET_KEY2')
      @version ||= "hixie-75"

      # Pull out the details we care about
      @origin ||= @env['HTTP_SEC_WEBSOCKET_ORIGIN'] || @env['HTTP_ORIGIN']
      @location ||= "ws#{secure? ? 's' : ''}://#{@env['HTTP_HOST']}#{@env['REQUEST_PATH']}"
      @protocol ||= @env['HTTP_SEC_WEBSOCKET_PROTOCOL'] || @env['HTTP_WEBSOCKET_PROTOCOL']

      EM.next_tick { callback :on_start, self rescue error! "Error in start callback" }

      # Queue up the actual handshake
      EM.next_tick method :handshake!

      @state = :started

      # Return self so we can be used as a response
      self
    rescue
      error! "Error starting connection"
    end

    attr_reader :env, :version, :origin, :location, :protocol

    def hixie_75?
      @version == "hixie-75"
    end

    def hixie_76?
      @version == "hixie-76"
    end

    def secure?
      @env['HTTPS'] == 'on' or
      # XXX: This could be faked... do we care?
      @env['HTTP_X_FORWARDED_PROTO'] == 'https' or
      @env['rack.url_scheme'] == 'https'
    end

    def key
      @env['HTTP_SEC_WEBSOCKET_KEY']
    end

    [1, 2].each do |i|
      define_method :"key#{i}" do
        key = env["HTTP_SEC_WEBSOCKET_KEY#{i}"]
        key.scan(/[0-9]/).join.to_i / key.count(' ')
      end
    end

    def key3
      @key3 ||= @buffer.slice!(0...8)
    end

    def challenge?
      env.has_key? 'HTTP_SEC_WEBSOCKET_KEY1'
    end

    def challenge
      if hixie_75?
        nil
      elsif hixie_76?
        [key1, key2].pack("N*") + key3
      else
        key + GUID
      end
    end

    def challenge_response
      if hixie_75?
        nil
      elsif hixie_76?
        Digest::MD5.digest(challenge)
      else
        Base64.encode64(Digest::SHA1.digest(challenge)).strip
      end
    end

    # Generate the handshake
    def handshake
      "HTTP/1.1 101 Switching Protocols\r\n" <<
      "Connection: Upgrade\r\n" <<
      "Upgrade: WebSocket\r\n" <<
      if hixie_75?
        "WebSocket-Location: #{location}\r\n" <<
        "WebSocket-Origin: #{origin}\r\n"
      elsif hixie_76?
        "Sec-WebSocket-Location: #{location}\r\n" <<
        "Sec-WebSocket-Origin: #{origin}\r\n"
      else
        "Sec-WebSocket-Accept: #{challenge_response}\r\n"
      end <<
      (protocol ? "Sec-WebSocket-Protocol: #{protocol}\r\n" : "") <<
      "\r\n" <<
      (if hixie_76? then challenge_response else "" end)
    end

    def handshake!
      if hixie_76?
        [key1, key2].each { |key| raise WebSocketProtocolError, "Invalid key: #{key}" if key >= 2**32 }
        raise WebSocketProtocolError, "Invalid challenge: #{key3}" if key3.length < 8
      end

      send_data handshake

      @state = :handshook

      EM.next_tick { callback :on_handshake, self rescue error! "Error in handshake callback" }
    rescue
      error! "Error during WebSocket connection handshake"
    end

    def receive_data data
      @buffer << data

      EM.next_tick { process_frame } if @state == :handshook
    rescue
      error! "Error while receiving WebSocket data"
    end

    def mask payload, mask_key
      payload.unpack("C*").map.with_index do |byte, index|
        byte ^ mask_key[index % 4]
      end.pack("C*")
    end

    def process_frame
      if hixie_75? or hixie_76?
        if @buffer.length >= 1
          if @buffer[0].ord < 0x7f
            if ending = @buffer.index("\xff")
              frame = @buffer.slice! 0..ending
              message = frame[1..-2]

              EM.next_tick { receive_message message }

              # There might be more frames to process
              EM.next_tick { process_frame }
            elsif @buffer.length > MAX_BUFFER_LENGTH
              raise WebSocketProtocolError, "Maximum buffer length (#{MAX_BUFFER_LENGTH}) exceeded: #{@buffer.length}"
            end
          elsif @buffer[0] == "\xff"
            if @buffer.length > 1
              if @buffer[1] == "\x00"
                @buffer.slice! 0..1

                EM.next_tick { finish! }
              else
                raise WebSocketProtocolError, "Incorrect finish frame length: #{@buffer[1].inspect}"
              end
            end
          else
            raise WebSocketProtocolError, "Unknown frame type: #{@buffer[0].inspect}"
          end
        end
      else
        @frame_state ||= :opcode

        if @frame_state == :opcode
          return unless @buffer.length >= 2

          bytes = @buffer.slice!(0...2).unpack("C*")

          @opcode = bytes[0] & 0x0f
          @fin = (bytes[0] & 0x80) != 0
          @payload_length = bytes[1] & 0x7f
          @masked = (bytes[1] & 0x80) != 0

          return error! "Received unmasked data" unless @masked

          if @payload_length == 126
            @frame_state = :payload_2
          elsif @payload_length == 127
            @frame_state = :payload_8
          else
            @frame_state = :payload
          end

        elsif @frame_state == :payload_2
          return unless @buffer.length >= 2

          @payload_length = @buffer.slice!(0...2).unpack("n")[0]

          @frame_state = :mask

        elsif @frame_state == :payload_8
          return unless @buffer.length >= 8

          (high, low) = @buffer.slice!(0...8).unpack("NN")
          @payload_length = high * (2 ** 32) + low

          @frame_state = :mask

        elsif @frame_state == :mask
          return unless @buffer.length >= 4

          bytes = @buffer[(offset)...(offset += 4)]
          @mask_key = bytes.unpack("C*")

          @frame_state = :payload

        elsif @frame_state == :payload
          return unless @buffer.length >= @payload_length

          payload = @buffer.slice!(0...@payload_length)
          payload = mask(payload, @mask_key)

          if @opcode == OPCODE_TEXT
            message = payload.force_encoding("UTF-8") if payload.respond_to? :force_encoding
            EM.next_tick { receive_message payload }
          elsif @opcode == OPCODE_CLOSE
            EM.next_tick { finish! }
          else
            error! "Unsupported opcode: %d" % @opcode
          end

          @frame_state = nil
          @opcode = @fin = @payload_length = @masked = nil
        end
      end
    rescue
      error! "Error while processing WebSocket frames"
    end

    def receive_message message
      EM.next_tick { callback :on_message, self, message rescue error! "Error in message callback" }
    end

    # This is for post-hixie-76 versions only
    def send_frame opcode, payload="", masked=false
      payload = payload.dup.force_encoding("ASCII-8BIT") if payload.respond_to? :force_encoding
      payload_length = payload.bytesize

      # We don't support continuations (yet), so always send fin
      fin_byte = 0x80
      send_data [fin_byte | opcode].pack("C")

      # We shouldn't be sending mask, we're a server only
      masked_byte = masked ? 0x80 : 0x00

      if payload_length <= 125
        send_data [masked_byte | payload_length].pack("C")

      elsif payload_length < 2 ** 16
        send_data [masked_byte | 126].pack("C")
        send_data [payload_length].pack("n")

      else
        send_data [masked_byte | 127].pack("C")
        send_data [payload_length / (2 ** 32), payload_length % (2 ** 32)].pack("NN")
      end

      if payload_length
        if masked
          mask_key = Array.new(4) { rand(256) }.pack("C*")
          send_data mask_key
          payload = mask payload, mask_key
        end

        send_data payload
      end
    end

    def send_message message
      if hixie_75? or hixie_76?
        send_data "\x00#{message}\xff"
      else
        send_frame OPCODE_TEXT, message
      end
    end

    # Finish the connection read for closing
    def finish!
      if hixie_75? or hixie_76?
        send_data "\xff\x00"
      else
        send_frame OPCODE_CLOSE
      end

      EM.next_tick { callback(:on_finish, self) rescue error! "Error in finish callback" }
      EM.next_tick { close_connection_after_writing }

      @state = :finished
    rescue
      error! "Error finishing WebSocket connection"
    end

    # Make sure we call the on_close callbacks when the connection
    # disappears
    def unbind
      EM.next_tick { callback(:on_close, self) rescue error! "Error in close callback" }
      @state = :closed
    rescue
      error! "Error closing WebSocket connection"
    end

    def error! message=nil, callback=true
      log message unless message.nil?
      log_error # Logs the exception itself

      # Allow error messages to be handled, maybe
      # but only if this error was not caused by the error callback
      if callback
        EM.next_tick { callback(:on_error, self) rescue error! "Error in error callback", true }
      end

      # Try to finish and close nicely.
      EM.next_tick { finish! } unless [:finished, :closed, :error].include? @state

      # We're closed!
      @state = :error
    end
  end

  CONNECTION = 'HTTP_CONNECTION'.freeze
  UPGRADE = 'HTTP_UPGRADE'.freeze
  SKINNY_WEBSOCKET = 'skinny.websocket'.freeze

  UPGRADE_REGEXP = /\bupgrade\b/i.freeze
  WEBSOCKET_REGEXP = /\bwebsocket\b/i.freeze

  module Helpers
    def websocket?
      env[CONNECTION] =~ UPGRADE_REGEXP && env[UPGRADE] =~ WEBSOCKET_REGEXP
    end

    def websocket options={}, &block
      env[SKINNY_WEBSOCKET] ||= begin
        raise RuntimerError, "Not a WebSocket request" unless websocket?
        options[:on_message] = block if block_given?
        Websocket.from_env(env, options)
      end
    end

    def websocket! options={}, &block
      websocket(options, &block).start!
    end
  end
end


text:
package tk.zielony.randomdata.common;

import android.content.Context;
import android.graphics.drawable.Drawable;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import tk.zielony.randomdata.DataContext;
import tk.zielony.randomdata.Generator;
import tk.zielony.randomdata.R;

public class DrawableImageGenerator extends Generator<Drawable> {
    private Context context;
    private Random random = new Random();
    private boolean preventDuplicates;

    private Set<Integer> used = new HashSet<>();

    private int[] images = {
            R.drawable.randomdata_background0, R.drawable.randomdata_background1, R.drawable.randomdata_background2, R.drawable.randomdata_background3, R.drawable.randomdata_background4
    };

    public DrawableImageGenerator(Context context) {
        this.context = context;
        preventDuplicates = true;
    }

    public DrawableImageGenerator(Context context, boolean preventDuplicates) {
        this.context = context;
        this.preventDuplicates = preventDuplicates;
    }

    @Override
    public Drawable next(DataContext dataContext) {
        return context.getResources().getDrawable(next2());
    }

    private int next2() {
        int resId;
        if (preventDuplicates) {
            if (used.size() == images.length)
                used.clear();
            do {
                resId = images[random.nextInt(images.length)];
            }
            while (used.contains(resId));
            used.add(resId);
        } else {
            resId = images[random.nextInt(images.length)];
        }
        return resId;
    }

    @Override
    public void reset() {
        used.clear();
    }
}

redpajama_set_name: RedPajamaGithub


text:
using System;
using System.IO;
using LibGD;
using NUnit.Framework;

[TestFixture]
public class GlobalMembersBug00002_1
{
    private const string TMP_FN = "bug00002_1.png";

    [Test]
    public void TestBug00002_1()
	{
		gdImageStruct im;
		IntPtr fp;
		string path = new string(new char[1024]);

		im = gd.gdImageCreateTrueColor(100, 100);

		if (im == null)
		{
            GlobalMembersGdtest.gdTestErrorMsg(GlobalMembersGdtest.__FILE__, GlobalMembersGdtest.__LINE__, "Cannot create image.\n");
			Assert.Fail();
		}

		gd.gdImageFill(im, 0, 0, 0xffffff);
		gd.gdImageFill(im, 0, 0, 0xffffff);

		fp = C.fopen(TMP_FN, "wb");
		if (fp == IntPtr.Zero)
		{
            GlobalMembersGdtest.gdTestErrorMsg(GlobalMembersGdtest.__FILE__, GlobalMembersGdtest.__LINE__, "Cannot create image from <%s>\n", TMP_FN);
			gd.gdImageDestroy(im);
            Assert.Fail();
		}

		gd.gdImagePng(im,new _iobuf(fp));
		C.fclose(fp);

		path = string.Format("{0}/gdimagefill/bug00002_1_exp.png", GlobalMembersGdtest.DefineConstants.GDTEST_TOP_DIR);
		if (GlobalMembersGdtest.gdTestImageCompareToFile(GlobalMembersGdtest.__FILE__, GlobalMembersGdtest.__LINE__, null, (path), (im)) == 0)
		{
			gd.gdImageDestroy(im);
            Assert.Fail();
		}

		gd.gdImageDestroy(im);

        File.Delete(TMP_FN);
	}
}


text:
;(function(window, undefined){

    window.RNDM = {
        v: '0.0.1',
        msg: 'rndm',
        gradientOffset: 50
    };

    if(console.info){
        console.info('hi there,');
        console.info("You don't need to crawl through the source using your beloved browser console.");
        console.info("Everything you see here is available on github: https://github.com/makepanic/rndm-web");
    }

})(window)


text:
"""Helps support reading and parsing metadata elements from a TDS client catalog."""

from __future__ import print_function

import logging

log = logging.getLogger(__name__)
log.setLevel(logging.ERROR)
log.addHandler(logging.StreamHandler())

class _SimpleTypes(object):
    def __init__(self):
        self._valid = {'dataFormat': self._load_valid_data_format_types(),
                       'upOrDown': self._load_valid_up_or_down(),
                       'dataType': self._load_valid_data_types()}

    @staticmethod
    def _load_valid_data_types():
        valid = ['grid',
                 'image',
                 'point',
                 'radial',
                 'station',
                 'swath',
                 'trajectory']
        return valid

    @staticmethod
    def _load_valid_data_format_types():
        import mimetypes
        valid = ['BUFR',
                 'ESML',
                 'GEMPAK',
                 'GINI',
                 'GRIB-1',
                 'GRIB-2',
                 'HDF4',
                 'HDF5',
                 'McIDAS-AREA',
                 'NcML',
                 'NetCDF',
                 'NetCDF-4',
                 'NEXRAD2',
                 'NIDS',
                 'image/gif',
                 'image/jpeg',
                 'image/tiff',
                 'text/csv',
                 'text/html',
                 'text/plain',
                 'text/tab-separated-values',
                 'text/xml',
                 'video/mpeg',
                 'video/quicktime',
                 'video/realtime']

        valid_mime_types = list(mimetypes.types_map.values())
        valid.extend(valid_mime_types)
        return valid

    @staticmethod
    def _load_valid_up_or_down():
        return ['up', 'down']

    def handle_upOrDown(self, element):  # noqa
        # name="upOrDown"
        #   <xsd:restriction base="xsd:token">
        #    <xsd:enumeration value="up"/>
        #    <xsd:enumeration value="down"/>
        #   </xsd:restriction>
        #
        type_name = 'upOrDown'
        valid = self._valid[type_name]
        for attrib in element.attrib:
            attr = attrib
            val = element.attrib[attr]
            if val not in valid:
                log.warning('Value %s not valid for type %s: must be %s',
                            val, type_name, valid)
        return {attr: val}

    def handle_dataFormat(self, element):  # noqa
        # name="dataFormatTypes"
        #   <xsd:union memberTypes="xsd:token mimeType">
        #     <xsd:simpleType>
        #       <xsd:restriction base="xsd:token">
        #         <xsd:enumeration value="BUFR"/>
        #         <xsd:enumeration value="ESML"/>
        #         <xsd:enumeration value="GEMPAK"/>
        #         <xsd:enumeration value="GINI"/>
        #         <xsd:enumeration value="GRIB-1"/>
        #         <xsd:enumeration value="GRIB-2"/>
        #         <xsd:enumeration value="HDF4"/>
        #         <xsd:enumeration value="HDF5"/>
        #         <xsd:enumeration value="McIDAS-AREA"/>
        #         <xsd:enumeration value="NcML"/>
        # 		  <xsd:enumeration value="NetCDF"/>
        # 		  <xsd:enumeration value="NetCDF-4"/>
        #         <xsd:enumeration value="NEXRAD2"/>
        #         <xsd:enumeration value="NIDS"/>
        #
        #         <xsd:enumeration value="image/gif"/>
        #         <xsd:enumeration value="image/jpeg"/>
        #         <xsd:enumeration value="image/tiff"/>
        #         <xsd:enumeration value="text/csv"/>
        #         <xsd:enumeration value="text/html"/>
        # 		  <xsd:enumeration value="text/plain"/>
        # 		  <xsd:enumeration value="text/tab-separated-values"/>
        #         <xsd:enumeration value="text/xml"/>
        #         <xsd:enumeration value="video/mpeg"/>
        #         <xsd:enumeration value="video/quicktime"/>
        #         <xsd:enumeration value="video/realtime"/>
        #       </xsd:restriction>
        #     </xsd:simpleType>
        #   </xsd:union>
        #
        # name="mimeType"
        #   <xsd:restriction base="xsd:token">
        #     <xsd:annotation>
        #       <xsd:documentation>any valid mime type
        #         (see http://www.iana.org/assignments/media-types/)
        #       </xsd:documentation>
        #     </xsd:annotation>
        #   </xsd:restriction>
        #   NOTE: to see if mimetype is valude, check against
        #         mimetypes.types_map.values
        #
        type_name = 'dataFormat'
        valid = self._valid[type_name]
        val = element.text
        if val not in valid:
            log.warning('Value %s not valid for type %s: must be %s',
                        val, type_name, valid)
        return {type_name: val}

    def handle_dataType(self, element):  # noqa
        # name="dataTypes"
        #   <xsd:union memberTypes="xsd:token">
        #     <xsd:simpleType>
        #       <xsd:restriction base="xsd:token">
        #         <xsd:enumeration value="Grid"/>
        #         <xsd:enumeration value="Image"/>
        #         <xsd:enumeration value="Point"/>
        #         <xsd:enumeration value="Radial"/>
        #         <xsd:enumeration value="Station"/>
        #         <xsd:enumeration value="Swath"/>
        #         <xsd:enumeration value="Trajectory"/>
        #       </xsd:restriction>
        #     </xsd:simpleType>
        #   </xsd:union>
        type_name = 'dataType'
        valid = self._valid[type_name]
        # case insensitive

        val = element.text
        if val.lower() not in valid:
            log.warning('Value %s not valid for type %s: must be %s',
                        val, type_name, valid)
        return {type_name: val}

class _ComplexTypes(object):
    @staticmethod
    def _get_tag_name(element):
        if '}' in element.tag:
            element_name = element.tag.split('}')[-1]
        else:
            element_name = element.tag
        return element_name

    @staticmethod
    def _spatial_range_req_children():
        return ['start', 'size']

    @staticmethod
    def _spatial_range_opt_children():
        return ['resolution', 'units']

    @staticmethod
    def _date_type_formatted_valid_attrs():
        return ['format', 'type']

    @staticmethod
    def _controlled_vocatulary_opt_attrs():
        return ['vocabulary']

    @staticmethod
    def _variable_opt_attrs():
        return ['vocabulary_name', 'units']

    @staticmethod
    def _variable_req_attrs():
        return ['name']

    @staticmethod
    def _variables_opt_attrs():
        return ['vocabulary']

    @staticmethod
    def _data_size_req_attrs():
        return ['units']

    #
    # complex types:
    # ==============
    def handle_spatialRange(self, element):  # noqa
        # name="spatialRange">
        #   <xsd:sequence>
        #    <xsd:element name="start" type="xsd:double"  />
        #    <xsd:element name="size" type="xsd:double" />
        #    <xsd:element name="resolution" type="xsd:double" minOccurs="0" />
        #    <xsd:element name="units" type="xsd:string" minOccurs="0" />
        #   </xsd:sequence>
        type_name = 'spatialRange'
        req_children = self._spatial_range_req_children()
        opt_children = self._spatial_range_opt_children()
        valid = req_children + opt_children

        spatial_range = {}
        for child in element:
            child_name = child.tag
            if child_name in valid:
                if child_name != 'units':
                    spatial_range[child.tag] = float(child.text)
                else:
                    spatial_range[child.tag] = child.text
            else:
                # child not valid
                log.warning('%s is not valid for type %s',
                            child_name, type_name)
        return spatial_range

    def handle_controlledVocabulary(self, element):  # noqa
        #
        # type="controlledVocabulary"
        #   <xsd:simpleContent>
        #    <xsd:extension base="xsd:string">
        #     <xsd:attribute name="vocabulary" type="xsd:string" />
        #    </xsd:extension>
        #   </xsd:simpleContent>
        #
        type_name = 'controlledVocabulary'

        opt_attrs = self._controlled_vocatulary_opt_attrs()
        val = {}
        for attr in element.attrib:
            if attr not in opt_attrs:
                log.warning('%s not a valid attribute for %s', type_name,
                            attr)
            else:
                val[attr] = element.attrib[attr]

        name = element.text
        tmp = {'name': name}
        if val:
            tmp.update(val)
        return tmp

    def handle_dateTypeFormatted(self, element):  # noqa
        # name="dateTypeFormatted"
        #   <xsd:simpleContent>
        #     <xsd:extension base="dateType">
        #       <xsd:attribute name="format" type="xsd:string" /> // from
        #                                        java.text.SimpleDateFormat
        #       <xsd:attribute name="type" type="dateEnumTypes" />
        #     </xsd:extension>
        #
        type_name = 'dateTypeFormatted'
        valid_attrs = self._date_type_formatted_valid_attrs()
        val = {}
        for attr in element.attrib:
            if attr not in valid_attrs:
                log.warning('%s is not a valid attribute for %s', attr,
                            type_name)
            else:
                val[attr] = element.attrib[attr]

        val['value'] = element.text

        return val

    def handle_sourceType(self, element):  # noqa
        # name="sourceType"
        #   <xsd:sequence>
        #     <xsd:element name="name" type="controlledVocabulary"/>
        #     <xsd:element name="contact">
        #       <xsd:complexType>
        #         <xsd:attribute name="email" type="xsd:string"
        #                                     use="required"/>
        #         <xsd:attribute name="url" type="xsd:anyURI"/>
        #       </xsd:complexType>
        #     </xsd:element>
        #   </xsd:sequence>
        parsed = {}
        for child in element:
            value = {}
            if child.tag == 'name':
                value = self.handle_controlledVocabulary(child)
            elif child.tag == 'contact':
                if 'url' in child.attrib:
                    value['url'] = child.attrib['url']
                if 'email' in child.attrib:
                    value['email'] = child.attrib['email']
                else:
                    log.warning("'contact' must have an attribute: 'email'")
                    value['email'] = 'missing'
            if value:
                parsed.update(value)
        return parsed

    def handle_timeCoverageType(self, element):  # noqa
        # name="timeCoverageType">
        #   <xsd:sequence>
        #     <xsd:choice minOccurs="2" maxOccurs="3" >
        #       <xsd:element name="start" type="dateTypeFormatted"/>
        #       <xsd:element name="end" type="dateTypeFormatted"/>
        #       <xsd:element name="duration" type="duration"/>
        #     </xsd:choice>
        #     <xsd:element name="resolution" type="duration" minOccurs="0"/>
        #   </xsd:sequence>
        parsed = {}
        tags = []
        for child in element:
            tags.append(child.tag)
        valid_num_elements = len(tags) >= 2 & len(tags) <= 3
        if valid_num_elements:
            for child in element:
                value = {}
                if child.tag in ['start', 'end']:
                    processed = self.handle_dateTypeFormatted(child)
                    value[child.tag] = processed['value']
                elif child.tag in ['duration', 'resolution']:
                    value[child.tag] = child.text
                parsed.update(value)
        else:
            log.warning('Not enough elements to make a valid timeCoverage')

        return parsed

    def handle_variable(self, element):
        # element_name="variable"
        #   <xsd:complexType mixed="true">
        #     <xsd:attribute name="name" type="xsd:string" use="required"/>
        #     <xsd:attribute name="vocabulary_name" type="xsd:string"
        #                    use="optional"/>
        #     <xsd:attribute name="units" type="xsd:string"/>
        #   </xsd:complexType>
        type_name = 'variable'
        opt_attrs = self._variable_opt_attrs()
        req_attrs = self._variable_req_attrs()
        valid_attrs = opt_attrs + req_attrs
        valid = True
        variable = {}
        for req_attr in req_attrs:
            if req_attr not in element.attrib:
                valid = False
                log.warning('%s must have an attribute %s', type_name,
                            req_attr)
        if valid:
            if element.text:
                variable['description'] = element.text
            for attr in element.attrib:
                if attr in valid_attrs:
                    variable[attr] = element.attrib[attr]

        return variable

    @staticmethod
    def handle_variableMap(element):  # noqa
        # element_name="variableMap"
        #   <xsd:complexType>
        #     <xsd:attributeGroup ref="XLink"/>
        #   </xsd:complexType>
        type_name = 'variableMap'  # noqa
        var_map = {}
        for attr in element.attrib:
            var_map[attr] = element.attrib[attr]

        return var_map

    def handle_variables(self, element):
        # element_name="variables"
        #   <xsd:complexType>
        #     <xsd:choice>
        #       <xsd:element ref="variable" minOccurs="0"
        #                    maxOccurs="unbounded"/>
        #       <xsd:element ref="variableMap" minOccurs="0"/>
        #     </xsd:choice>
        #     <xsd:attribute name="vocabulary" type="variableNameVocabulary"
        #                    use="optional"/>
        #     <xsd:attributeGroup ref="XLink"/>
        #   </xsd:complexType>
        type_name = 'variables'  # noqa
        variables = {}
        variable_list = []
        variable_map_list = []
        for child in element:
            child_type = self._get_tag_name(child)

            if child_type == 'variable':
                var = self.handle_variable(child)
                variable_list.append(var)
            elif child_type == 'variableMap':
                var_map = self.handle_variableMap(element)
                variable_map_list.append(var_map)

        opt_attrs = self._variables_opt_attrs()
        for attr in element.attrib:
            if attr in opt_attrs:
                variables[attr] = element.attrib[attr]

        if variable_list:
            variables['variables'] = variable_list

        if variable_map_list:
            variables['variableMaps'] = variable_map_list
        return variables

    def handle_dataSize(self, element):  # noqa
        #   <xsd:complexType>
        #     <xsd:simpleContent>
        #     <xsd:extension base="xsd:string">
        #       <xsd:attribute name="units" type="xsd:string" use="required"/>
        #     </xsd:extension>
        #     </xsd:simpleContent>
        #   </xsd:complexType>
        #
        req_attrs = self._data_size_req_attrs()
        data_size = {'size': float(element.text)}

        for attr in element.attrib:
            if attr in req_attrs:
                data_size[attr] = element.attrib[attr]

        return data_size

class TDSCatalogMetadata(object):
    """Hold information contained in the catalog Metadata tag.

    Attributes
    ----------
    metadata : dict[str, object]
        The dictionary containing the metadata entries

    """

    def __init__(self, element, metadata_in=None):
        """Initialize a :class:`TDSCatalogMetadata` object.

        Parameters
        ----------
        element : :class:`~xml.etree.ElementTree.Element`
            An :class:`~xml.etree.ElementTree.Element` representing a metadata node
        metadata_in : dict[str, object], optional
            Parent metadata to inherit, if appropriate. Defaults to None.

        """
        self._ct = _ComplexTypes()
        self._st = _SimpleTypes()
        self._sts = _SimpleTypes.__dict__
        self._cts = _ComplexTypes.__dict__

        inherited = False
        if 'inherited' in element.attrib:
            inherited = element.attrib['inherited']
            if inherited == 'true':
                inherited = True
            else:
                inherited = False

        if metadata_in and inherited:
            # only inherit metadata passed in if the new metadata
            # element has inherit set to True
            self.metadata = metadata_in
        else:
            self.metadata = {'inherited': inherited}

        element_name = self._get_tag_name(element)
        if element_name == 'metadata':
            for child in element:
                self._parse_element(child)
        else:
            self._parse_element(element)

    @staticmethod
    def _get_tag_name(element):
        if '}' in element.tag:
            element_name = element.tag.split('}')[-1]
        else:
            element_name = element.tag
        return element_name

    def _get_handler(self, handler_name):
        handler_name = 'handle_' + handler_name
        if handler_name in self._cts:
            return getattr(self._ct, handler_name)
        elif handler_name in self._sts:
            return getattr(self._st, handler_name)
        else:
            msg = 'cannot find handler for element {}'.format(handler_name)
            log.warning(msg)

    def _parse_element(self, element):

        element_name = self._get_tag_name(element)

        parser = {'documentation': self._parse_documentation,
                  'property': self._parse_property,
                  'contributor': self._parse_contributor,
                  'geospatialCoverage': self._parse_geospatial_coverage,
                  'serviceName': self._parse_service_name,
                  'authority': self._parse_authority,
                  'publisher': self._parse_publisher,
                  'creator': self._parse_creator,
                  'keyword': self._parse_keyword,
                  'project': self._parse_project,
                  'dataFormat': self._parse_data_format,
                  'dataType': self._parse_data_type,
                  'date': self._parse_date,
                  'timeCoverage': self._parse_timeCoverage,
                  'variableMap': self._parse_variableMap,
                  'variables': self._parse_variables}

        try:
            parser[element_name](element)
        except KeyError:
            log.warning('No parser found for element %s', element_name)

    def _parse_documentation(self, element):
        # <xsd:simpleType name="documentationEnumTypes">
        #  <xsd:union memberTypes="xsd:token">
        #   <xsd:simpleType>
        #    <xsd:restriction base="xsd:token">
        #     <xsd:enumeration value="funding"/>
        #     <xsd:enumeration value="history"/>
        #     <xsd:enumeration value="processing_level"/>
        #     <xsd:enumeration value="rights"/>
        #     <xsd:enumeration value="summary"/>
        #    </xsd:restriction>
        #   </xsd:simpleType>
        #  </xsd:union>
        # </xsd:simpleType>
        #
        # <xsd:complexType name="documentationType" mixed="true">
        #  <xsd:sequence>
        #   <xsd:any namespace="http://www.w3.org/1999/xhtml" minOccurs="0"
        #         maxOccurs="unbounded" processContents="strict"/>
        #  </xsd:sequence>
        #  <xsd:attribute name="type" type="documentationEnumTypes"/>
        #  <xsd:attributeGroup ref="XLink" />
        # </xsd:complexType>
        xlink_href_attr = '{http://www.w3.org/1999/xlink}href'
        xlink_title_attr = '{http://www.w3.org/1999/xlink}title'

        # doc_enum_types = ("funding", "history", "processing_level", "rights",
        #                  "summary")
        known = 'type' in element.attrib
        # document element has no attributes
        plain_doc = not element.attrib
        md = self.metadata
        md.setdefault('documentation', {})
        if known or plain_doc:
            if known:
                doc_type = element.attrib['type']
            else:
                doc_type = 'generic'
            md['documentation'].setdefault(doc_type, []).append(element.text)
        elif xlink_href_attr in element.attrib:
            title = element.attrib[xlink_title_attr]
            href = element.attrib[xlink_href_attr]
            xlink = {'title': title, 'href': href}
            md['documentation'].setdefault('xlink', []).append(xlink)
        self.metadata = md

    def _parse_property(self, element):
        # <xsd:element name="property">
        #  <xsd:complexType>
        #   <xsd:attribute name="name" type="xsd:string"/>
        #   <xsd:attribute name="value" type="xsd:string"/>
        #  </xsd:complexType>
        # </xsd:element>
        name = element.attrib['name']
        value = element.attrib['value']
        self.metadata.setdefault('property', {})[name] = value

    def _parse_contributor(self, element):
        # <xsd:element name="contributor">
        #   <xsd:complexType>
        #     <xsd:simpleContent>
        #       <xsd:extension base="xsd:string">
        #         <xsd:attribute name="role" type="xsd:string"
        #                        use="required"/>
        #       </xsd:extension>
        #     </xsd:simpleContent>
        #   </xsd:complexType>
        # </xsd:element>
        element_type = 'contributor'
        role = element.attrib['role']
        name = element.text
        self.metadata.setdefault(element_type, {}).setdefault(role, []).append(name)

    def _parse_geospatial_coverage(self, element):
        element_type = 'geospatialCoverage'
        md = {}
        # <xsd:element name="geospatialCoverage">
        #  <xsd:complexType>
        #   <xsd:sequence>
        #    <xsd:element name="northsouth" type="spatialRange"
        #                 minOccurs="0" />
        #    <xsd:element name="eastwest" type="spatialRange"
        #                 minOccurs="0" />
        #    <xsd:element name="updown" type="spatialRange"
        #                 minOccurs="0" />
        #    <xsd:element name="name" type="controlledVocabulary"
        #                 minOccurs="0" maxOccurs="unbounded"/>
        #   </xsd:sequence>
        #
        #   <xsd:attribute name="zpositive" type="upOrDown" default="up"/>
        #  </xsd:complexType>
        # </xsd:element>
        elements = {'northsouth': 'spatialRange',
                    'eastwest': 'spatialRange',
                    'updown': 'spatialRange',
                    'name': 'controlledVocabulary'
                    }
        attrs = {'zpositive': 'upOrDown'}

        if element.attrib:
            for attr in element.attrib:
                if attr in attrs:
                    handler_name = attrs[attr]
                    handler = self._get_handler(handler_name)
                    value = handler(element)
                    md.update({attr: value})
                else:
                    log.warning('Attr on %s : %s not captured', attr,
                                element_type)

        for child in element:
            child_name = child.tag
            if child_name in elements:
                handler_name = elements[child_name]
                handler = self._get_handler(handler_name)
                value = handler(child)
                md.update(value)
        self.metadata.setdefault(element_type, []).append(md)

    def _parse_service_name(self, element):
        # can only have one serviceName
        element_type = 'serviceName'
        self.metadata[element_type] = element.text

    def _parse_authority(self, element):
        element_type = 'authority'
        self.metadata.setdefault(element_type, []).append(element.text)

    def _parse_publisher(self, element):
        element_type = 'publisher'
        parsed = self._ct.handle_sourceType(element)
        self.metadata.setdefault(element_type, []).append(parsed)

    def _parse_creator(self, element):
        element_type = 'creator'
        parsed = self._ct.handle_sourceType(element)
        self.metadata.setdefault(element_type, []).append(parsed)

    def _parse_keyword(self, element):
        element_type = 'keyword'
        parsed = self._ct.handle_controlledVocabulary(element)
        self.metadata.setdefault(element_type, []).append(parsed)

    def _parse_project(self, element):
        element_type = 'project'
        parsed = self._ct.handle_controlledVocabulary(element)
        self.metadata.setdefault(element_type, []).append(parsed)

    def _parse_data_format(self, element):
        element_type = 'dataFormat'  # noqa
        parsed = self._st.handle_dataFormat(element)
        self.metadata.update(parsed)

    def _parse_data_type(self, element):
        element_type = 'dataType'  # noqa
        parsed = self._st.handle_dataType(element)
        self.metadata.update(parsed)

    def _parse_date(self, element):
        element_type = 'date'
        parsed = self._ct.handle_dateTypeFormatted(element)
        self.metadata.setdefault(element_type, []).append(parsed)

    def _parse_timeCoverage(self, element):  # noqa
        element_type = 'timeCoverage'
        parsed = self._ct.handle_timeCoverageType(element)
        self.metadata.setdefault(element_type, []).append(parsed)

    def _parse_variableMap(self, element):  # noqa
        element_type = 'variableMap'
        parsed = self._ct.handle_variableMap(element)
        self.metadata.setdefault(element_type, []).append(parsed)

    def _parse_variables(self, element):
        element_type = 'variables'
        parsed = self._ct.handle_variables(element)
        for variable in parsed['variables']:
            var_name = variable['name']
            variable.pop('name', None)
            self.metadata.setdefault(element_type, {})[var_name] = variable


text: Atode WebExtension client


text:
from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
                        unicode_literals, with_statement)

import os
import re

from pants.build_graph.address import Address
from pants.engine.exp import parsers
from pants.engine.exp.objects import Serializable
from pants.util.memo import memoized_method, memoized_property

class MappingError(Exception):
  """Indicates an error mapping addressable objects."""

class UnaddressableObjectError(MappingError):
  """Indicates an un-addressable object was found at the top level."""

class DuplicateNameError(MappingError):
  """Indicates more than one top-level object was found with the same name."""

class AddressMap(object):
  """Maps addressable Serializable objects from a byte source."""

  @classmethod
  def parse(cls, path, parser=None):
    """Parses a source for addressable Serializable objects.

    By default an enhanced JSON parser is used.  The parser admits extra blank lines, comment lines
    and more than one top-level JSON object.  See :`pants.engine.exp.parsers.parse_json` for more
    details on the modified JSON format and the schema for Serializable json objects.

    No matter the parser used, the parsed and mapped addressable objects are all 'thin'; ie: any
    objects they point to in other namespaces or even in the same namespace but from a seperate
    source are left as unresolved pointers.

    :param string path: The path to the byte source containing serialized objects.
    :param parser: The parser to use; by default a json parser.
    :type parser: :class:`collection.Callable` that accepts a file path and returns a list of all
                  addressable Serializable objects parsed from it.
    """
    parse = parser or parsers.parse_json
    objects = parse(path)
    objects_by_name = {}
    for obj in objects:
      if not Serializable.is_serializable(obj):
        raise UnaddressableObjectError('Parsed a non-serializable object: {!r}'.format(obj))
      attributes = obj._asdict()

      name = attributes.get('name')
      if not name:
        raise UnaddressableObjectError('Parsed a non-addressable object: {!r}'.format(obj))

      if name in objects_by_name:
        raise DuplicateNameError('An object already exists at {!r} with name {!r}: {!r}.  Cannot '
                                 'map {!r}'.format(path, name, objects_by_name[name], obj))

      objects_by_name[name] = obj
    return cls(path, objects_by_name)

  def __init__(self, path, objects_by_name):
    """Not intended for direct use, instead see `parse`."""
    self._path = path
    self._objects_by_name = objects_by_name

  @property
  def path(self):
    """Return the path to the byte source this address map's objects were pased from.

    :rtype: string
    """
    return self._path

  @property
  def objects_by_name(self):
    """Return a mapping from object name to the parsed 'thin' addressable object.

    :rtype: dict from string to thin addressable objects.
    """
    return self._objects_by_name

  def __repr__(self):
    return 'AddressMap(path={!r}, objects_by_name={!r})'.format(self._path, self._objects_by_name)

class DifferingFamiliesError(MappingError):
  """Indicates an attempt was made to merge address maps from different families together."""

class AddressFamily(object):
  """Represents the family of addressed objects in a namespace.

  An address family can be composed of the addressed objects from one or more underlying address
  sources.
  """

  @classmethod
  def create(cls, build_root, address_maps):
    """Creates an address family from the given set of address maps.

    :param string build_root: The absolute path of the root of the namespace. All address maps must
                              be hydrated from child paths of the build root.
    :param address_maps: The family of maps that form this namespace.
    :type address_maps: :class:`collections.Iterable` of :class:`AddressMap`
    :returns: a new address family.
    :rtype: :class:`AddressFamily`
    :raises: :class:`MappingError` if the given address maps do not form a family.
    """
    if not address_maps:
      raise TypeError('Handed an empty set of AddressMaps - must be given at least one.')

    spec_paths = {os.path.dirname(address_map.path) for address_map in address_maps}
    if len(spec_paths) > 1:
      raise DifferingFamiliesError('Expected all AddressMaps to share the same parent directory '
                                   'but given a mix of parent directories:\n\t{}'
                                   .format('\n\t'.join(sorted(spec_paths))))
    spec_path = os.path.relpath(spec_paths.pop(), build_root)
    if spec_path == '.':
      spec_path = ''

    objects_by_name = {}
    for address_map in address_maps:
      current_path = address_map.path
      for name, obj in address_map.objects_by_name.items():
        previous = objects_by_name.get(name)
        if previous:
          previous_path, _ = previous
          raise DuplicateNameError('An object with name {name!r} is already defined in '
                                   '{previous_path!r}, will not overwrite with {obj!r} from '
                                   '{current_path!r}.'
                                   .format(name=name,
                                           previous_path=previous_path,
                                           obj=obj,
                                           current_path=current_path))
        objects_by_name[name] = (current_path, obj)
    return AddressFamily(namespace=spec_path,
                         objects_by_name={name: obj for name, (_, obj) in objects_by_name.items()})

  def __init__(self, namespace, objects_by_name):
    """Not intended for direct use, instead see `create`."""
    self._namespace = namespace
    self._objects_by_name = objects_by_name

  @property
  def namespace(self):
    """Return the namespace path of this address family.

    :rtype: string
    """
    return self._namespace

  # TODO(John Sirois): Support in-memory injection of fully-hydrated (synthetic) addressables in
  # this family's namespace (spec_path).  It would be appealing to support this by
  # expanding/re-defining the family and growing it to include an in-memory AddressMap sibling that
  # would carry the injection(s).
  @memoized_property
  def addressables(self):
    """Return a mapping from address to thin addressable objects in this namespace.

    :rtype: dict from :class:`pants.build_graph.address.Address` to thin addressable objects.
    """
    return {Address(spec_path=self._namespace, target_name=name): obj
            for name, obj in self._objects_by_name.items()}

  def __repr__(self):
    return 'AddressFamily(namespace={!r}, objects_by_name={!r})'.format(self._namespace,
                                                                        self._objects_by_name)

class ResolveError(MappingError):
  """Indicates an error resolving targets."""

# TODO(John Sirois): Support in-memory injection of (synthetic) addressables to support conversion
# of the legacy system.
class AddressMapper(object):
  """Maps addresses to the objects they point to.

  An address mapper serves as its own cache of the BUILD files it has parsed.  Although it has no
  knowledge of BUILD file contents, it does expose an `invalidate_build_file` for external agents
  aware of file changes to mark the corresponding address namespaces as being in-need of re-parsing.
  """

  def __init__(self, build_root, build_pattern=None, parser=None):
    """Creates an address mapper rooted at the given `build_root`.

    Both the set of files that define a mappable BUILD files and the parser used to parse those
    files can be customized.  See the `pants.engine.exp.parsers` module for example parsers.

    :param string build_root: The root of the BUILD files; typically the code repository root
                              directory.
    :param string build_pattern: A regular expression for identifying BUILD files used to resolve
                                 addresses; by default looks for `BUILD*` files.
    :param parser: The BUILD file parser to use; by default a JSON BUILD file format parser.
    :type parser: A :class:`collections.Callable` that takes a byte string and produces a list of
                  parsed addressable Serializable objects found in the byte string.
    """
    self._build_root = os.path.realpath(build_root)
    self._build_pattern = re.compile(build_pattern or r'^BUILD(\.[a-zA-Z0-9_-]+)?$')
    self._parser = parser

  def _find_build_files(self, dir_path):
    abs_dir_path = os.path.join(self._build_root, dir_path)
    if not os.path.isdir(abs_dir_path):
      raise ResolveError('Expected {} to be a directory containing build files.'.format(dir_path))
    for f in os.listdir(abs_dir_path):
      if self._build_pattern.match(f):
        abs_build_file = os.path.join(abs_dir_path, f)
        if os.path.isfile(abs_build_file):
          yield abs_build_file

  @staticmethod
  def _normalize_parse_path(path):
    return os.path.realpath(path)

  @memoized_method
  def _parse(self, path):
    return AddressMap.parse(path, parser=self._parser)

  @memoized_method
  def family(self, namespace):
    """Load the address family in the given namespace.

    :param string namespace: The namespace of the address family to load.
    :returns: The address family at the given namespace.
    :rtype: :class:`AddressFamily`
    :raises: :class:`ResolveError` if the address family could not be found.
    """
    family = self._maybe_family(namespace)
    if not family:
      raise ResolveError('No addresses registered in namespace {}'.format(namespace))
    return family

  def _maybe_family(self, namespace):
    build_files = list(self._find_build_files(namespace))
    return self._family(build_files) if build_files else None

  def _family(self, build_files):
    return AddressFamily.create(self._build_root, [self._parse(bf) for bf in build_files])

  def resolve(self, address):
    """Resolve the given address to a named Serializable object.

    :param address: The address to resolve to an named Serializable object.
    :type address: :class:`pants.build_graph.address.Address`
    :returns: The resolved object.
    :raises: :class:`ResolveError` if the object could not be resolved.
    """
    family = self.family(address.spec_path)
    obj = family.addressables.get(address)
    if not obj:
      raise ResolveError('Object with address {} was not found'.format(address))
    return obj

  def invalidate_build_file(self, path):
    """Force the given build file path to be re-parsed on next access of its namespace.

    The namespace containing BUILD file is also invalidated such that the enclosing family is
    completely recalculated.  This allows for adding new paths to a BUILD file family, modifying
    existing paths or marking paths as having been deleted.

    :param string path: The path of the build file; either absolute or relative to the build root.
    """
    # TODO(John Sirois): replace @memoized caches with hand-build local caches if needed when
    # considering concurrency implications of a seperate thread calling invalidate while other
    # threads access the cache.
    path = path if os.path.isabs(path) else os.path.join(self._build_root, path)
    normalized_path = self._normalize_parse_path(path)

    self._parse.forget(self, normalized_path)
    namespace = os.path.relpath(os.path.dirname(normalized_path), self._build_root)
    self.family.forget(self, namespace)

  def walk_addressables(self, rel_path=None, path_excludes=None):
    """Return an iterator over all addressable objects found under `rel_path`.

    :param string rel_path: The path relative to the build root to scan beneath; '' by default,
                            meaning the whole build root will be scanned.
    :param path_excludes: Directory paths relative to the build root to exclude from the scan.
    :type path_excludes: list of string
    :returns: An iterator of (address, addressable object).
    :rtype: tuple of (:class:`pants.base.address.Address`, object)
    """
    path_excludes = [os.path.join(self._build_root, p) for p in (path_excludes or ())]
    map_root = os.path.join(self._build_root, rel_path or '')

    for root, dirs, files in os.walk(map_root):
      if path_excludes:
        for index, directory in enumerate(dirs):
          dir_path = os.path.join(root, directory)
          if dir_path in path_excludes:
            del dirs[index]
            break
      build_files = [os.path.join(root, f) for f in files if self._build_pattern.match(f)]
      if build_files:
        for item in self._family(build_files).addressables.items():
          yield item


text:
@implementation NSString (Random)

+ (NSString *)randomNameGenerator:(NSInteger)characterLength
{
    NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    NSMutableString *randomString = [NSMutableString stringWithCapacity:characterLength];

    for (int i = 0; i < characterLength; i++) {
        [randomString appendFormat: @"%C", [letters characterAtIndex: arc4random() % [letters length]]];
    }

    return randomString;
}

@end


text:
module Receiver
( trapReceiver
) where

import Config
import TrapTypes

import Control.Exception
import Control.Monad.IO.Class
import Data.ASN1.BinaryEncoding
import Data.ASN1.Encoding
import Data.ASN1.Types
import Data.Conduit
import Data.Conduit.Network.UDP
import Data.Time.LocalTime
import Network.Socket
import System.IO

trapReceiver :: SockAddr -> IO ()
trapReceiver sockAddr =
  withFile "./log/snmptrap.log" AppendMode $ \hdl -> do
    hSetBuffering hdl NoBuffering
    withSocketsDo $ bracket (socket AF_INET Datagram 0) close $ \sock -> do
      bind sock sockAddr
      loop hdl sock
  where loop h s = (sourceSocket s maxUdpFrameBytes
                   $= parsingMsg
                   $$ loggingMsgTo h
                   ) >> loop h s

parsingMsg :: Conduit Message IO TrapLog
parsingMsg = do
  mbMsg <- await
  case mbMsg of
    Nothing -> return ()
    Just (Message msg sender) -> do
      time <- liftIO getZonedTime
      asn1 <- liftIO $ either throwIO return $ decodeASN1' BER msg
      trap <- liftIO $ either (throwIO . ErrorCall) return $ fst <$> fromASN1 asn1
      (Just hostname,_) <- liftIO $ getNameInfo [] True False sender
      yield (time, hostname, trapoid trap, varbind trap) >> parsingMsg

loggingMsgTo :: Handle -> Sink TrapLog IO ()
loggingMsgTo h = do
  mbTrapLog <- await
  case mbTrapLog of
    Nothing -> return ()
    Just traplog -> liftIO $ hPrint h traplog


text:
import {Component, ViewChild} from '@angular/core';
import {NavController, NavParams, Content} from 'ionic-angular';

@Component({
  selector: 'page-event-rankings',
  templateUrl: 'event-rankings.html'
})
export class EventRankingsPage {

  @ViewChild(Content) content: Content;

  event: any;
  rankings: any;

  constructor(private navCtrl: NavController, private navParams: NavParams) {
    this.event = navParams.get("event");
    this.rankings = [];
    this.repopulateArray();
  }

  repopulateArray() {
    for (let i = 1; i < this.event.ranks.length; i++) {
      let ranking = this.event.ranks[i];
      let rank = ranking[0];
      let team = ranking[1];

      this.rankings.push({
        "team": team,
        "rank": rank
      });
    }
  }

  checkScroll(e) {
    let scroll = document.getElementById("scroll");
    if (e.scrollTop >= 150) {
      if (scroll.classList.contains("hidden")) {
        scroll.classList.remove("hidden");
        scroll.classList.add("visible");
      }
    } else {
      if (scroll.classList.contains("visible")) {
        scroll.classList.remove("visible");
        scroll.classList.add("hidden");
      }
    }
  }

  scrollToTop() {
    this.content.scrollToTop(1200);
  }

}


text:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Caching;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using SampleMvcAzAuth.Data;
using SampleMvcAzAuth.Models;

namespace SampleMvcAzAuth.Utils {
  public class EfAdalTokenCache  : TokenCache {
    private TokenCacheDataContext db = new TokenCacheDataContext();
    string User;
    private PerUserWebCache Cache;

    // constructor
    public EfAdalTokenCache(string user) {
      // associate the cache to the current user of the web app
      User = user;

      this.AfterAccess = AfterAccessNotification;
      this.BeforeAccess = BeforeAccessNotification;
      this.BeforeWrite = BeforeWriteNotification;

      // look up the entry in the DB
      Cache = db.PerUserCacheList.FirstOrDefault(c => c.WebUserUniqueId== User);
      // place the entry in memory
      this.Deserialize((Cache == null) ? null : Cache.CacheBits);
    }

    // clean up the DB
    public override void Clear() {
      base.Clear();
      foreach (var cacheEntry in db.PerUserCacheList)
        db.PerUserCacheList.Remove(cacheEntry);
      db.SaveChanges();
    }

    // Notification raised before ADAL accesses the cache.
    // This is your chance to update the in-memory copy from the DB, if the in-memory version is stale
    void BeforeAccessNotification(TokenCacheNotificationArgs args) {
      if (Cache == null) {
        // first time access
        Cache = db.PerUserCacheList.FirstOrDefault(c => c.WebUserUniqueId == User);
      } else {   // retrieve last write from the DB
        var status = from e in db.PerUserCacheList
                     where (e.WebUserUniqueId == User)
                     select new {
                       LastWrite = e.LastWrite
                     };
        // if the in-memory copy is older than the persistent copy
        if (status.First().LastWrite > Cache.LastWrite)
        //// read from from storage, update in-memory copy
        {
          Cache = db.PerUserCacheList.FirstOrDefault(c => c.WebUserUniqueId == User);
        }
      }

      this.Deserialize((Cache == null) ? null : Cache.CacheBits);
    }
    // Notification raised after ADAL accessed the cache.
    // If the HasStateChanged flag is set, ADAL changed the content of the cache
    void AfterAccessNotification(TokenCacheNotificationArgs args) {
      // if state changed
        if (this.HasStateChanged)
        {
            //for a first time person, cache is null.
            if (Cache == null)
            {
                //created a new object
                Cache = new PerUserWebCache
                {
                    WebUserUniqueId = User,
                    CacheBits = this.Serialize(),
                    LastWrite = DateTime.Now
                };

                //add it to the DbContext
                db.PerUserCacheList.Add(Cache);
            }
            else
            {
                //update the CacheBits and LastWrite on the existing cache object.
                Cache.CacheBits = this.Serialize();
                Cache.LastWrite = DateTime.Now;
                db.Entry(Cache).State = EntityState.Modified;
            }

            //update the database
            db.SaveChanges();

            //reset the flag
            this.HasStateChanged = false;
        }
    }
    void BeforeWriteNotification(TokenCacheNotificationArgs args) {
      // if you want to ensure that no concurrent write take place, use this notification to place a lock on the entry
    }
  }

}


text:
import re

import pytest

import notifiers

mock_name = "mock_provider"

@pytest.mark.usefixtures("mock_provider")
class TestCLI:
    """CLI tests"""

    def test_bad_notify(self, cli_runner):
        """Test invalid notification usage"""
        cmd = f"{mock_name} notify".split()
        result = cli_runner(cmd)
        assert result.exit_code
        assert getattr(result, "exception")

    def test_notify_sanity(self, cli_runner):
        """Test valid notification usage"""
        cmd = f"{mock_name} notify --required bar foo".split()
        result = cli_runner(cmd)
        assert (
            not result.exit_code
        ), f"Exit code is {result.exit_code}. Output: {result.output}"
        assert "Succesfully sent a notification" in result.output

    def test_providers(self, cli_runner):
        """Test providers command"""
        result = cli_runner(["providers"])
        assert not result.exit_code
        assert "mock" in result.output

    def test_metadata(self, cli_runner):
        """Test metadata command"""
        cmd = f"{mock_name} metadata".split()
        result = cli_runner(cmd)
        assert not result.exit_code
        assert '"base_url": "https://api.mock.com"' in result.output
        assert '"site_url": "https://www.mock.com"' in result.output
        assert '"name": "mock_provider"' in result.output

    def test_required(self, cli_runner):
        """Test required command"""
        cmd = f"{mock_name} required".split()
        result = cli_runner(cmd)
        assert not result.exit_code
        assert "required" in result.output

    def test_help(self, cli_runner):
        """Test help command"""
        cmd = f"{mock_name} notify --help".split()
        result = cli_runner(cmd)
        assert not result.exit_code
        assert "--required" in result.output
        assert "--not-required" in result.output
        assert "MESSAGE" in result.output

    def test_no_defaults(self, cli_runner):
        """Test defaults command"""
        cmd = "pushover defaults".split()
        result = cli_runner(cmd)
        assert not result.exit_code
        assert "{}" in result.output

    def test_defaults(self, cli_runner):
        """Test defaults command"""
        cmd = f"{mock_name} defaults".split()
        result = cli_runner(cmd)
        assert not result.exit_code
        assert '"option_with_default": "foo"' in result.output

    def test_resources(self, cli_runner):
        cmd = f"{mock_name} resources".split()
        result = cli_runner(cmd)
        assert not result.exit_code
        assert "mock_rsrc" in result.output

    def test_piping_input(self, cli_runner):
        """Test piping in message"""
        cmd = f"{mock_name} notify --required foo".split()
        result = cli_runner(cmd, input="bar")
        assert not result.exit_code
        assert "Succesfully sent a notification" in result.output

    @pytest.mark.parametrize(
        "prefix, command",
        [
            (None, f"{mock_name} notify".split()),
            ("FOO_", f"--env-prefix FOO_ {mock_name} notify".split()),
        ],
    )
    def test_environ(self, prefix, command, monkeypatch, cli_runner):
        """Test provider environ usage"""
        prefix = prefix if prefix else "NOTIFIERS_"
        monkeypatch.setenv(f"{prefix}MOCK_PROVIDER_REQUIRED", "foo")
        monkeypatch.setenv(f"{prefix}MOCK_PROVIDER_MESSAGE", "foo")
        result = cli_runner(command)
        assert not result.exit_code
        assert "Succesfully sent a notification" in result.output

    def test_version_command(self, cli_runner):
        result = cli_runner(["--version"])
        assert not result.exit_code
        version_re = re.search(r"(\d+\.\d+\.\d+)", result.output)
        assert version_re
        assert version_re.group(1) == notifiers.__version__

    def test_multiple_option(self, cli_runner):
        cmd = f"{mock_name} notify --required foo --not-required baz --not-required piz bar"
        result = cli_runner(cmd.split())
        assert not result.exit_code
        assert "Succesfully sent a notification" in result.output


text:
import React       from 'react'
import { shallow } from 'enzyme'

import SvgLogo from '../index'

//-----------  Tests  -----------//

describe('<SvgLogo />', () => {
  it('Expect to have unit tests specified', () => {
    expect(true).toEqual(false);
  });
});


text:
layout: blog
---

    {{ content }}

  {% include adsence_leader.html %}
  {% include analytics.html %}
  {% include disqus.html %}


text:
set -euo pipefail

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
LH_ROOT="$SCRIPT_DIR/../../.."

roll_devtools() {
  # Roll devtools. Besides giving DevTools the latest lighthouse source files,
  # this also copies over the e2e tests.
  cd "$LH_ROOT"
  yarn devtools "$DEVTOOLS_PATH"
  cd -
}

cd "$DEVTOOLS_PATH"
git --no-pager log -1
roll_devtools

# Needed to re-generate ninja rules, because there may possibly be e2e test files
# referenced in the rules initially generated by `download-devtools.sh` that
# `yarn devtools` deleted.
gclient sync --delete_unversioned_trees --reset

# Build devtools. This creates `out/Default/gen/front_end`.
autoninja -C out/Default
cd -


text:
package p02_ExtendedDatabase;

import javax.naming.OperationNotSupportedException;

import p02_ExtendedDatabase.interfaces.Database;
import p02_ExtendedDatabase.interfaces.People;

public class DatabaseImpl implements Database {

	private final static Integer DATABASE_CAPACITY = 16;
	private static Integer NEXT_INDEX = 0;

	private People[] database;

	public DatabaseImpl(People... initialNumbers) {
		this.setDatabase(initialNumbers);
	}

	private void setDatabase(People[] initPeople) {
		this.database = new People[DATABASE_CAPACITY];
		for (int i = 0; i < initPeople.length; i++) {
			this.database[i] = initPeople[i];
			NEXT_INDEX = i + 1;
		}
	}

	public void add(People people) throws OperationNotSupportedException {
		if (people == null) {
			throw new OperationNotSupportedException("People can't be null!");
		}
		if (NEXT_INDEX >= DATABASE_CAPACITY) {
			throw new OperationNotSupportedException("Databse is full!");
		}
		if (this.containsId(people.getId())) {
			throw new OperationNotSupportedException("There are user with this id");
		}
		if (this.containsUsername(people.getUsername())) {
			throw new OperationNotSupportedException("There are user with this id");
		}
		this.database[NEXT_INDEX] = people;
		NEXT_INDEX++;
	}

	private boolean containsUsername(String username) {
		for (int i = 0; i < this.database.length; i++) {
			if (this.database[i] != null && this.database[i].getUsername().equals(username)) {
				return true;
			}
		}
		return false;
	}

	private boolean containsId(Long id) {
		for (int i = 0; i < this.database.length; i++) {
			if (this.database[i] != null && this.database[i].getId() == id) {
				return true;
			}
		}
		return false;
	}

	public void remove() throws OperationNotSupportedException {
		if (NEXT_INDEX <= 0) {
			throw new OperationNotSupportedException("Databse is empty!");
		}
		NEXT_INDEX--;
		this.database[NEXT_INDEX] = null;
	}

	public People[] fetch() {
		return this.database;
	}

	@Override
	public People findById(Long id) throws OperationNotSupportedException {
		if (NEXT_INDEX <= 0) {
			throw new OperationNotSupportedException("Databse is empty!");
		}
		for (int i = 0; i < this.database.length; i++) {
			if (this.database[i] != null && this.database[i].getId() == id) {
				return this.database[i];
			}
		}
		throw new OperationNotSupportedException("There are no such user!");
	}

	@Override
	public People findByUsername(String username) throws OperationNotSupportedException {
		if (NEXT_INDEX <= 0) {
			throw new OperationNotSupportedException("Databse is empty!");
		}
		for (int i = 0; i < this.database.length; i++) {
			if (this.database[i] != null && this.database[i].getUsername().equals(username)) {
				return this.database[i];
			}
		}
		throw new OperationNotSupportedException("There are no such user!");
	}

}


text:
using namespace llvm;

#define DEBUG_TYPE "mips16-instrinfo"

Mips16InstrInfo::Mips16InstrInfo(const MipsSubtarget &STI)
    : MipsInstrInfo(STI, Mips::Bimm16) {}

const MipsRegisterInfo &Mips16InstrInfo::getRegisterInfo() const {
  return RI;
}

/// isLoadFromStackSlot - If the specified machine instruction is a direct
/// load from a stack slot, return the virtual or physical register number of
/// the destination along with the FrameIndex of the loaded stack slot.  If
/// not, return 0.  This predicate must return 0 if the instruction has
/// any side effects other than loading from the stack slot.
unsigned Mips16InstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
                                              int &FrameIndex) const {
  return 0;
}

/// isStoreToStackSlot - If the specified machine instruction is a direct
/// store to a stack slot, return the virtual or physical register number of
/// the source reg along with the FrameIndex of the loaded stack slot.  If
/// not, return 0.  This predicate must return 0 if the instruction has
/// any side effects other than storing to the stack slot.
unsigned Mips16InstrInfo::isStoreToStackSlot(const MachineInstr &MI,
                                             int &FrameIndex) const {
  return 0;
}

void Mips16InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
                                  MachineBasicBlock::iterator I,
                                  const DebugLoc &DL, unsigned DestReg,
                                  unsigned SrcReg, bool KillSrc) const {
  unsigned Opc = 0;

  if (Mips::CPU16RegsRegClass.contains(DestReg) &&
      Mips::GPR32RegClass.contains(SrcReg))
    Opc = Mips::MoveR3216;
  else if (Mips::GPR32RegClass.contains(DestReg) &&
           Mips::CPU16RegsRegClass.contains(SrcReg))
    Opc = Mips::Move32R16;
  else if ((SrcReg == Mips::HI0) &&
           (Mips::CPU16RegsRegClass.contains(DestReg)))
    Opc = Mips::Mfhi16, SrcReg = 0;
  else if ((SrcReg == Mips::LO0) &&
           (Mips::CPU16RegsRegClass.contains(DestReg)))
    Opc = Mips::Mflo16, SrcReg = 0;

  assert(Opc && "Cannot copy registers");

  MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc));

  if (DestReg)
    MIB.addReg(DestReg, RegState::Define);

  if (SrcReg)
    MIB.addReg(SrcReg, getKillRegState(KillSrc));
}

bool Mips16InstrInfo::isCopyInstrImpl(const MachineInstr &MI,
                                      const MachineOperand *&Src,
                                      const MachineOperand *&Dest) const {
  if (MI.isMoveReg()) {
    Dest = &MI.getOperand(0);
    Src = &MI.getOperand(1);
    return true;
  }
  return false;
}

void Mips16InstrInfo::storeRegToStack(MachineBasicBlock &MBB,
                                      MachineBasicBlock::iterator I,
                                      unsigned SrcReg, bool isKill, int FI,
                                      const TargetRegisterClass *RC,
                                      const TargetRegisterInfo *TRI,
                                      int64_t Offset) const {
  DebugLoc DL;
  if (I != MBB.end()) DL = I->getDebugLoc();
  MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOStore);
  unsigned Opc = 0;
  if (Mips::CPU16RegsRegClass.hasSubClassEq(RC))
    Opc = Mips::SwRxSpImmX16;
  assert(Opc && "Register class not handled!");
  BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill)).
      addFrameIndex(FI).addImm(Offset)
      .addMemOperand(MMO);
}

void Mips16InstrInfo::loadRegFromStack(MachineBasicBlock &MBB,
                                       MachineBasicBlock::iterator I,
                                       unsigned DestReg, int FI,
                                       const TargetRegisterClass *RC,
                                       const TargetRegisterInfo *TRI,
                                       int64_t Offset) const {
  DebugLoc DL;
  if (I != MBB.end()) DL = I->getDebugLoc();
  MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOLoad);
  unsigned Opc = 0;

  if (Mips::CPU16RegsRegClass.hasSubClassEq(RC))
    Opc = Mips::LwRxSpImmX16;
  assert(Opc && "Register class not handled!");
  BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(Offset)
    .addMemOperand(MMO);
}

bool Mips16InstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
  MachineBasicBlock &MBB = *MI.getParent();
  switch (MI.getDesc().getOpcode()) {
  default:
    return false;
  case Mips::RetRA16:
    ExpandRetRA16(MBB, MI, Mips::JrcRa16);
    break;
  }

  MBB.erase(MI.getIterator());
  return true;
}

/// GetOppositeBranchOpc - Return the inverse of the specified
/// opcode, e.g. turning BEQ to BNE.
unsigned Mips16InstrInfo::getOppositeBranchOpc(unsigned Opc) const {
  switch (Opc) {
  case Mips::BeqzRxImmX16: return Mips::BnezRxImmX16;
  case Mips::BnezRxImmX16: return Mips::BeqzRxImmX16;
  case Mips::BeqzRxImm16: return Mips::BnezRxImm16;
  case Mips::BnezRxImm16: return Mips::BeqzRxImm16;
  case Mips::BteqzT8CmpX16: return Mips::BtnezT8CmpX16;
  case Mips::BteqzT8SltX16: return Mips::BtnezT8SltX16;
  case Mips::BteqzT8SltiX16: return Mips::BtnezT8SltiX16;
  case Mips::Btnez16: return Mips::Bteqz16;
  case Mips::BtnezX16: return Mips::BteqzX16;
  case Mips::BtnezT8CmpiX16: return Mips::BteqzT8CmpiX16;
  case Mips::BtnezT8SltuX16: return Mips::BteqzT8SltuX16;
  case Mips::BtnezT8SltiuX16: return Mips::BteqzT8SltiuX16;
  case Mips::Bteqz16: return Mips::Btnez16;
  case Mips::BteqzX16: return Mips::BtnezX16;
  case Mips::BteqzT8CmpiX16: return Mips::BtnezT8CmpiX16;
  case Mips::BteqzT8SltuX16: return Mips::BtnezT8SltuX16;
  case Mips::BteqzT8SltiuX16: return Mips::BtnezT8SltiuX16;
  case Mips::BtnezT8CmpX16: return Mips::BteqzT8CmpX16;
  case Mips::BtnezT8SltX16: return Mips::BteqzT8SltX16;
  case Mips::BtnezT8SltiX16: return Mips::BteqzT8SltiX16;
  }
  llvm_unreachable("Illegal opcode!");
}

static void addSaveRestoreRegs(MachineInstrBuilder &MIB,
                               const std::vector<CalleeSavedInfo> &CSI,
                               unsigned Flags = 0) {
  for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
    // Add the callee-saved register as live-in. Do not add if the register is
    // RA and return address is taken, because it has already been added in
    // method MipsTargetLowering::lowerRETURNADDR.
    // It's killed at the spill, unless the register is RA and return address
    // is taken.
    unsigned Reg = CSI[e-i-1].getReg();
    switch (Reg) {
    case Mips::RA:
    case Mips::S0:
    case Mips::S1:
      MIB.addReg(Reg, Flags);
      break;
    case Mips::S2:
      break;
    default:
      llvm_unreachable("unexpected mips16 callee saved register");

    }
  }
}

// Adjust SP by FrameSize bytes. Save RA, S0, S1
void Mips16InstrInfo::makeFrame(unsigned SP, int64_t FrameSize,
                                MachineBasicBlock &MBB,
                                MachineBasicBlock::iterator I) const {
  DebugLoc DL;
  MachineFunction &MF = *MBB.getParent();
  MachineFrameInfo &MFI    = MF.getFrameInfo();
  const BitVector Reserved = RI.getReservedRegs(MF);
  bool SaveS2 = Reserved[Mips::S2];
  MachineInstrBuilder MIB;
  unsigned Opc = ((FrameSize <= 128) && !SaveS2)? Mips::Save16:Mips::SaveX16;
  MIB = BuildMI(MBB, I, DL, get(Opc));
  const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
  addSaveRestoreRegs(MIB, CSI);
  if (SaveS2)
    MIB.addReg(Mips::S2);
  if (isUInt<11>(FrameSize))
    MIB.addImm(FrameSize);
  else {
    int Base = 2040; // should create template function like isUInt that
                     // returns largest possible n bit unsigned integer
    int64_t Remainder = FrameSize - Base;
    MIB.addImm(Base);
    if (isInt<16>(-Remainder))
      BuildAddiuSpImm(MBB, I, -Remainder);
    else
      adjustStackPtrBig(SP, -Remainder, MBB, I, Mips::V0, Mips::V1);
  }
}

// Adjust SP by FrameSize bytes. Restore RA, S0, S1
void Mips16InstrInfo::restoreFrame(unsigned SP, int64_t FrameSize,
                                   MachineBasicBlock &MBB,
                                   MachineBasicBlock::iterator I) const {
  DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
  MachineFunction *MF = MBB.getParent();
  MachineFrameInfo &MFI    = MF->getFrameInfo();
  const BitVector Reserved = RI.getReservedRegs(*MF);
  bool SaveS2 = Reserved[Mips::S2];
  MachineInstrBuilder MIB;
  unsigned Opc = ((FrameSize <= 128) && !SaveS2)?
    Mips::Restore16:Mips::RestoreX16;

  if (!isUInt<11>(FrameSize)) {
    unsigned Base = 2040;
    int64_t Remainder = FrameSize - Base;
    FrameSize = Base; // should create template function like isUInt that
                     // returns largest possible n bit unsigned integer

    if (isInt<16>(Remainder))
      BuildAddiuSpImm(MBB, I, Remainder);
    else
      adjustStackPtrBig(SP, Remainder, MBB, I, Mips::A0, Mips::A1);
  }
  MIB = BuildMI(MBB, I, DL, get(Opc));
  const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
  addSaveRestoreRegs(MIB, CSI, RegState::Define);
  if (SaveS2)
    MIB.addReg(Mips::S2, RegState::Define);
  MIB.addImm(FrameSize);
}

// Adjust SP by Amount bytes where bytes can be up to 32bit number.
// This can only be called at times that we know that there is at least one free
// register.
// This is clearly safe at prologue and epilogue.
void Mips16InstrInfo::adjustStackPtrBig(unsigned SP, int64_t Amount,
                                        MachineBasicBlock &MBB,
                                        MachineBasicBlock::iterator I,
                                        unsigned Reg1, unsigned Reg2) const {
  DebugLoc DL;
  //
  // li reg1, constant
  // move reg2, sp
  // add reg1, reg1, reg2
  // move sp, reg1
  //
  //
  MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::LwConstant32), Reg1);
  MIB1.addImm(Amount).addImm(-1);
  MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::MoveR3216), Reg2);
  MIB2.addReg(Mips::SP, RegState::Kill);
  MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::AdduRxRyRz16), Reg1);
  MIB3.addReg(Reg1);
  MIB3.addReg(Reg2, RegState::Kill);
  MachineInstrBuilder MIB4 = BuildMI(MBB, I, DL, get(Mips::Move32R16),
                                                     Mips::SP);
  MIB4.addReg(Reg1, RegState::Kill);
}

void Mips16InstrInfo::adjustStackPtrBigUnrestricted(
    unsigned SP, int64_t Amount, MachineBasicBlock &MBB,
    MachineBasicBlock::iterator I) const {
   llvm_unreachable("adjust stack pointer amount exceeded");
}

/// Adjust SP by Amount bytes.
void Mips16InstrInfo::adjustStackPtr(unsigned SP, int64_t Amount,
                                     MachineBasicBlock &MBB,
                                     MachineBasicBlock::iterator I) const {
  if (Amount == 0)
    return;

  if (isInt<16>(Amount))  // need to change to addiu sp, ....and isInt<16>
    BuildAddiuSpImm(MBB, I, Amount);
  else
    adjustStackPtrBigUnrestricted(SP, Amount, MBB, I);
}

/// This function generates the sequence of instructions needed to get the
/// result of adding register REG and immediate IMM.
unsigned Mips16InstrInfo::loadImmediate(unsigned FrameReg, int64_t Imm,
                                        MachineBasicBlock &MBB,
                                        MachineBasicBlock::iterator II,
                                        const DebugLoc &DL,
                                        unsigned &NewImm) const {
  //
  // given original instruction is:
  // Instr rx, T[offset] where offset is too big.
  //
  // lo = offset & 0xFFFF
  // hi = ((offset >> 16) + (lo >> 15)) & 0xFFFF;
  //
  // let T = temporary register
  // li T, hi
  // shl T, 16
  // add T, Rx, T
  //
  RegScavenger rs;
  int32_t lo = Imm & 0xFFFF;
  NewImm = lo;
  int Reg =0;
  int SpReg = 0;

  rs.enterBasicBlock(MBB);
  rs.forward(II);
  //
  // We need to know which registers can be used, in the case where there
  // are not enough free registers. We exclude all registers that
  // are used in the instruction that we are helping.
  //  // Consider all allocatable registers in the register class initially
  BitVector Candidates =
      RI.getAllocatableSet
      (*II->getParent()->getParent(), &Mips::CPU16RegsRegClass);
  // Exclude all the registers being used by the instruction.
  for (unsigned i = 0, e = II->getNumOperands(); i != e; ++i) {
    MachineOperand &MO = II->getOperand(i);
    if (MO.isReg() && MO.getReg() != 0 && !MO.isDef() &&
        !Register::isVirtualRegister(MO.getReg()))
      Candidates.reset(MO.getReg());
  }

  // If the same register was used and defined in an instruction, then
  // it will not be in the list of candidates.
  //
  // we need to analyze the instruction that we are helping.
  // we need to know if it defines register x but register x is not
  // present as an operand of the instruction. this tells
  // whether the register is live before the instruction. if it's not
  // then we don't need to save it in case there are no free registers.
  int DefReg = 0;
  for (unsigned i = 0, e = II->getNumOperands(); i != e; ++i) {
    MachineOperand &MO = II->getOperand(i);
    if (MO.isReg() && MO.isDef()) {
      DefReg = MO.getReg();
      break;
    }
  }

  BitVector Available = rs.getRegsAvailable(&Mips::CPU16RegsRegClass);
  Available &= Candidates;
  //
  // we use T0 for the first register, if we need to save something away.
  // we use T1 for the second register, if we need to save something away.
  //
  unsigned FirstRegSaved =0, SecondRegSaved=0;
  unsigned FirstRegSavedTo = 0, SecondRegSavedTo = 0;

  Reg = Available.find_first();

  if (Reg == -1) {
    Reg = Candidates.find_first();
    Candidates.reset(Reg);
    if (DefReg != Reg) {
      FirstRegSaved = Reg;
      FirstRegSavedTo = Mips::T0;
      copyPhysReg(MBB, II, DL, FirstRegSavedTo, FirstRegSaved, true);
    }
  }
  else
    Available.reset(Reg);
  BuildMI(MBB, II, DL, get(Mips::LwConstant32), Reg).addImm(Imm).addImm(-1);
  NewImm = 0;
  if (FrameReg == Mips::SP) {
    SpReg = Available.find_first();
    if (SpReg == -1) {
      SpReg = Candidates.find_first();
      // Candidates.reset(SpReg); // not really needed
      if (DefReg!= SpReg) {
        SecondRegSaved = SpReg;
        SecondRegSavedTo = Mips::T1;
      }
      if (SecondRegSaved)
        copyPhysReg(MBB, II, DL, SecondRegSavedTo, SecondRegSaved, true);
    }
   else
     Available.reset(SpReg);
    copyPhysReg(MBB, II, DL, SpReg, Mips::SP, false);
    BuildMI(MBB, II, DL, get(Mips::  AdduRxRyRz16), Reg).addReg(SpReg, RegState::Kill)
      .addReg(Reg);
  }
  else
    BuildMI(MBB, II, DL, get(Mips::  AdduRxRyRz16), Reg).addReg(FrameReg)
      .addReg(Reg, RegState::Kill);
  if (FirstRegSaved || SecondRegSaved) {
    II = std::next(II);
    if (FirstRegSaved)
      copyPhysReg(MBB, II, DL, FirstRegSaved, FirstRegSavedTo, true);
    if (SecondRegSaved)
      copyPhysReg(MBB, II, DL, SecondRegSaved, SecondRegSavedTo, true);
  }
  return Reg;
}

unsigned Mips16InstrInfo::getAnalyzableBrOpc(unsigned Opc) const {
  return (Opc == Mips::BeqzRxImmX16   || Opc == Mips::BimmX16  ||
          Opc == Mips::Bimm16  ||
          Opc == Mips::Bteqz16        || Opc == Mips::Btnez16 ||
          Opc == Mips::BeqzRxImm16    || Opc == Mips::BnezRxImm16   ||
          Opc == Mips::BnezRxImmX16   || Opc == Mips::BteqzX16 ||
          Opc == Mips::BteqzT8CmpX16  || Opc == Mips::BteqzT8CmpiX16 ||
          Opc == Mips::BteqzT8SltX16  || Opc == Mips::BteqzT8SltuX16  ||
          Opc == Mips::BteqzT8SltiX16 || Opc == Mips::BteqzT8SltiuX16 ||
          Opc == Mips::BtnezX16       || Opc == Mips::BtnezT8CmpX16 ||
          Opc == Mips::BtnezT8CmpiX16 || Opc == Mips::BtnezT8SltX16 ||
          Opc == Mips::BtnezT8SltuX16 || Opc == Mips::BtnezT8SltiX16 ||
          Opc == Mips::BtnezT8SltiuX16 ) ? Opc : 0;
}

void Mips16InstrInfo::ExpandRetRA16(MachineBasicBlock &MBB,
                                  MachineBasicBlock::iterator I,
                                  unsigned Opc) const {
  BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
}

const MCInstrDesc &Mips16InstrInfo::AddiuSpImm(int64_t Imm) const {
  if (validSpImm8(Imm))
    return get(Mips::AddiuSpImm16);
  else
    return get(Mips::AddiuSpImmX16);
}

void Mips16InstrInfo::BuildAddiuSpImm
  (MachineBasicBlock &MBB, MachineBasicBlock::iterator I, int64_t Imm) const {
  DebugLoc DL;
  BuildMI(MBB, I, DL, AddiuSpImm(Imm)).addImm(Imm);
}

const MipsInstrInfo *llvm::createMips16InstrInfo(const MipsSubtarget &STI) {
  return new Mips16InstrInfo(STI);
}

bool Mips16InstrInfo::validImmediate(unsigned Opcode, unsigned Reg,
                                     int64_t Amount) {
  switch (Opcode) {
  case Mips::LbRxRyOffMemX16:
  case Mips::LbuRxRyOffMemX16:
  case Mips::LhRxRyOffMemX16:
  case Mips::LhuRxRyOffMemX16:
  case Mips::SbRxRyOffMemX16:
  case Mips::ShRxRyOffMemX16:
  case Mips::LwRxRyOffMemX16:
  case Mips::SwRxRyOffMemX16:
  case Mips::SwRxSpImmX16:
  case Mips::LwRxSpImmX16:
    return isInt<16>(Amount);
  case Mips::AddiuRxRyOffMemX16:
    if ((Reg == Mips::PC) || (Reg == Mips::SP))
      return isInt<16>(Amount);
    return isInt<15>(Amount);
  }
  llvm_unreachable("unexpected Opcode in validImmediate");
}


text:
DEFINE_LOG_COMPONENT(LOG_PRIORITY_DEBUG, "Property");
IMPLEMENT_SINGLETON(Editor::PropertyTreeMgr);

namespace Editor
{

    //////////////////////////////////////////////////////////////////
    ///
    //////////////////////////////////////////////////////////////////
    PropertyTreeNode::PropertyTreeNode()
        : valueRoot_(NULL)
        , parent_(NULL)
        , propertyUI_(NULL)
    {
    }

    PropertyTreeNode::~PropertyTreeNode()
    {
    }

    bool PropertyTreeNode::load(rapidjson::Value & config)
    {
        rapidjson::Value *value = &config["name"];
        if(!value->IsString())
        {
            return false;
        }
        name_ = value->GetString();

        value = &config["parent"];
        if(value->IsString())
        {
            parent_ = PropertyTreeMgr::instance()->findProperty(value->GetString());
            if(NULL == parent_)
            {
                LOG_ERROR("Failed to find parent '%s' for type '%s'",
                           value->GetString(), name_.c_str());
                return false;
            }
        }

        valueRoot_ = new PropertyItemType();
        valueRoot_->type_ = "class";
        valueRoot_->key_ = name_;
        valueRoot_->name_ = name_;

        value = &config["property"];
        if(value->IsObject() && !valueRoot_->loadChildren(*value))
        {
            return false;
        }

        propertyUI_ = PropertyItemFactory::instance()->createPropertyByDef(valueRoot_);
        if(NULL == propertyUI_)
        {
            LOG_ERROR("Failed to create ui for property '%s'", name_.c_str());
            return false;
        }

        return true;
    }

    IPropertyItem* PropertyTreeNode::findPropertyItem(const std::string &name)
    {
        if(propertyUI_ != nullptr)
        {
            QString qname(name.c_str());

            QList<QtProperty *> subProperties = propertyUI_->subProperties();
            for(QtProperty *item : subProperties)
            {
                if(qname == item->propertyName())
                {
                    return dynamic_cast<IPropertyItem*>(item);
                }
            }
        }

        return nullptr;
    }

    //////////////////////////////////////////////////////////////////
    ///
    //////////////////////////////////////////////////////////////////

    PropertyTreeMgr::PropertyTreeMgr()
    {
        loadDefaultClassNameMap(this);
    }

    PropertyTreeMgr::~PropertyTreeMgr()
    {

    }

    bool PropertyTreeMgr::loadPropertyFile(const std::string & filename)
    {
        rapidjson::Document document;
        if(!openJsonFile(filename, document) || !document.IsArray())
        {
            return false;
        }

        std::string path = filename;
        size_t split = path.rfind('/');
        if(split != path.npos)
        {
            path.erase(split + 1);
        }

        for(rapidjson::Value::ValueIterator it = document.Begin();
            it != document.End(); ++it)
        {
            if(it->IsString())
            {
                if(!loadPropertyFile(path + it->GetString()))
                {
                    return false;
                }
            }
            else if(it->IsObject())
            {
                if(!registerProperty(*it))
                {
                    return false;
                }
            }
        }
        return true;
    }

    PropertyTreeNode* PropertyTreeMgr::findProperty(const std::string & name)
    {
        PropertyMap::iterator it = properties_.find(name);
        if(it != properties_.end())
        {
            return it->second;
        }
        return  NULL;
    }

    bool PropertyTreeMgr::registerProperty(rapidjson::Value & value)
    {
        PropertyTreeNode *pNode = new PropertyTreeNode();
        if(!pNode->load(value))
        {
            delete pNode;
            return false;
        }

        properties_[pNode->getName()] = pNode;
        return true;
    }

    const std::string & PropertyTreeMgr::cppNameToUIName(const std::string & className)
    {
        StringMap::iterator it = class2uiName_.find(className);
        if(it != class2uiName_.end())
        {
            return it->second;
        }

        return empty_string;
    }

    void PropertyTreeMgr::registerUIName(const std::string & className, const std::string & uiName)
    {
        class2uiName_[className] = uiName;
    }

} // end namespace Editor


text:
"use strict";

// Extends method
// (taken from http://code.jquery.com/jquery-1.9.0.js)
// Populate the class2type map
var class2type = {};

var types = ["Boolean", "Number", "String", "Function", "Array", "Date", "RegExp", "Object", "Error"];
for (var i = 0; i < types.length; i++) {
  var typename = types[i];
  class2type["[object " + typename + "]"] = typename.toLowerCase();
}

var core_toString = class2type.toString;
var core_hasOwn = class2type.hasOwnProperty;

function type(obj) {
  if (obj === null) {
    return String(obj);
  }
  return typeof obj === "object" || typeof obj === "function" ?
    class2type[core_toString.call(obj)] || "object" :
    typeof obj;
}

function isWindow(obj) {
  return obj !== null && obj === obj.window;
}

function isPlainObject(obj) {
  // Must be an Object.
  // Because of IE, we also have to check the presence of the constructor property.
  // Make sure that DOM nodes and window objects don't pass through, as well
  if (!obj || type(obj) !== "object" || obj.nodeType || isWindow(obj)) {
    return false;
  }

  try {
    // Not own constructor property must be Object
    if (obj.constructor &&
      !core_hasOwn.call(obj, "constructor") &&
      !core_hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) {
      return false;
    }
  } catch ( e ) {
    // IE8,9 Will throw exceptions on certain host objects #9897
    return false;
  }

  // Own properties are enumerated firstly, so to speed up,
  // if last one is own, then all properties are own.
  var key;
  for (key in obj) {}

  return key === undefined || core_hasOwn.call(obj, key);
}

function isFunction(obj) {
  return type(obj) === "function";
}

var isArray = Array.isArray || function (obj) {
  return type(obj) === "array";
};

function extend() {
  var options, name, src, copy, copyIsArray, clone,
    target = arguments[0] || {},
    i = 1,
    length = arguments.length,
    deep = false;

  // Handle a deep copy situation
  if (typeof target === "boolean") {
    deep = target;
    target = arguments[1] || {};
    // skip the boolean and the target
    i = 2;
  }

  // Handle case when target is a string or something (possible in deep copy)
  if (typeof target !== "object" && !isFunction(target)) {
    target = {};
  }

  // extend jQuery itself if only one argument is passed
  if (length === i) {
    /* jshint validthis: true */
    target = this;
    --i;
  }

  for (; i < length; i++) {
    // Only deal with non-null/undefined values
    if ((options = arguments[i]) != null) {
      // Extend the base object
      for (name in options) {
        src = target[name];
        copy = options[name];

        // Prevent never-ending loop
        if (target === copy) {
          continue;
        }

        // Recurse if we're merging plain objects or arrays
        if (deep && copy && (isPlainObject(copy) || (copyIsArray = isArray(copy)))) {
          if (copyIsArray) {
            copyIsArray = false;
            clone = src && isArray(src) ? src : [];

          } else {
            clone = src && isPlainObject(src) ? src : {};
          }

          // Never move original objects, clone them
          target[name] = extend(deep, clone, copy);

        // Don't bring in undefined values
        } else if (copy !== undefined) {
          if (!(isArray(options) && isFunction(copy))) {
            target[name] = copy;
          }
        }
      }
    }
  }

  // Return the modified object
  return target;
}

module.exports = extend;


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  <meta name="generator" content="PSPad editor, www.pspad.com" />
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  <link rel="stylesheet" type="text/css" href="styles/lame.css" />
	<title>LAME MP3 Encoder :: Contact Information</title>

Index page

About LAME

Intro to encoding

Usage of LAME

Version history

Coders of LAME

Contact LAME

LAME links

Contact Information

MP3 encoder mailing list

	For general discussions and comments about LAME and MP3 encoding, you can
	check the MP3 encoder mailing list. Keep in mind this
	is not the preferred list if you want to get in touch with the
	developers.

	Archives
	are also available (older archives: before October
	2000)

LAME development mailing list

	The lame-dev
	list is right place for everything related to LAME development: bug
	reports, questions and suggestions concerning the encoder, the documentation
	and the web pages. It's also the best way to get in touch with the active
	developers.
	Archives are provided by SourceForge.

CVS mailing list

	If you want to be automatically notified of commits to CVS, you can subscribe
	to the lame-cvs
	list.

IRC

	Some developers and other interested parties can eventually be found on the
	#mp3encoder channel on the FreeNode
	Network.

Bug reports/tracker

	To submit bug reports, you can use the bug
	tracker besides the lame-dev mailing
	list. Please be descriptive in your bug report. You should clearly mention
	your problem, LAME version used and how to reproduce the bug.

Contact developers

	Here is the list of developers. If you encounter
	a problem, please use any of the ways mentioned above instead of directly
	contact a developer.

Contact the Webmaster

	The current Webmaster for the LAME project is Roberto Amorim. You can contact him through
	e-mail.

HTML markup and design by Roberto Amorim and Sebastian Mares. Logo by Sam Fisher.