text:
import {BackendTimingInfo, DataMover, KernelBackend} from './backends/backend';
import {Environment, setEnvironmentGlobal} from './environment';
import {Profiler} from './profiler';
import {backpropagateGradients, getFilteredNodesXToY, NamedGradientMap, TapeNode} from './tape';
import {DataId, setTensorTracker, Tensor, Tensor3D, TensorTracker, Variable} from './tensor';
import {GradSaveFunc, NamedTensorMap, NamedVariableMap, TensorContainer} from './tensor_types';
import {getTensorsInContainer} from './tensor_util';
import {BackendValues, DataType, PixelData} from './types';
import * as util from './util';
import {bytesFromStringArray, makeOnesTypedArray, now, sizeFromShape} from './util';

/**
 * A function that computes an output. The save function is for saving tensors
 * computed in the forward pass, that we need in the backward pass.
 */
export type ForwardFunc<T> = (backend: KernelBackend, save?: GradSaveFunc) => T;

/**
 * @docalias (a: Tensor, b: Tensor,..., save?: Function) => {
 *   value: Tensor,
 *   gradFunc: (dy: Tensor, saved?: NamedTensorMap) => Tensor | Tensor[]
 * }
 */
export type CustomGradientFunc<T extends Tensor> =
    (...inputs: Array<Tensor|GradSaveFunc>) => {
      value: T;
      gradFunc: (dy: T, saved: Tensor[]) => Tensor | Tensor[];
    };

export type MemoryInfo = {
  numTensors: number; numDataBuffers: number; numBytes: number;
  unreliable?: boolean; reasons: string[];
};

type KernelProfile = {
  name: string; bytesAdded: number; totalBytesSnapshot: number;
  tensorsAdded: number;
  totalTensorsSnapshot: number;
  inputShapes: number[][];
  outputShape: number[] | number[][];
};

export type ProfileInfo = {
  newBytes: number; newTensors: number; peakBytes: number;
  kernels: KernelProfile[];
  result: TensorContainer;
};

export interface TimingInfo extends BackendTimingInfo {
  wallMs: number;
}

/** @docalias Function */
export type ScopeFn<T extends TensorContainer> = () => T;

export interface TensorManager {
  registerTensor(a: Tensor, backend?: KernelBackend): void;
  registerVariable(v: Variable): void;
  disposeTensor(a: Tensor): void;
  memory(): {numDataBuffers: number; numBytes: number;};
}

interface ScopeState {
  track: Tensor[];
  name: string;
  id: number;
}

class EngineState {
  // Public since optimizers will use it.
  registeredVariables: NamedVariableMap = {};

  nextTapeNodeId = 0;
  numBytes = 0;
  numTensors = 0;
  numStringTensors = 0;
  numDataBuffers = 0;

  activeTape: TapeNode[];
  // Number of nested tf.grad() statements when computing higher-order
  // gradients. E.g. `1` for first-order gradients and `2` for second-order
  // gradients. Used to track if the tape should be removed after a backprop.
  gradientDepth = 0;
  // Number of nested kernel calls. When kernel depth is greater than 1, we turn
  // off the tape.
  kernelDepth = 0;

  // Keep Tensors that parallel the tapes.
  activeScope: ScopeState;
  scopeStack: ScopeState[] = [];
  nextScopeId = 0;

  tensorInfo = new WeakMap<DataId, {
    backend: KernelBackend,
    bytes: number,
    dtype: DataType,
    shape: number[],
    refCount: number
  }>();

  profiling = false;
  activeProfile: ProfileInfo =
      {newBytes: 0, newTensors: 0, peakBytes: 0, kernels: [], result: null};

  dispose() {
    for (const variableName in this.registeredVariables) {
      this.registeredVariables[variableName].dispose();
    }
  }
}

export class Engine implements TensorManager, TensorTracker, DataMover {
  state: EngineState;
  backendName: string;
  registry: {[id: string]: KernelBackend} = {};
  registryFactory: {
    [id: string]: {
      factory: () => KernelBackend | Promise<KernelBackend>,
      priority: number
    }
  } = {};

  private profiler: Profiler;
  private backendInstance: KernelBackend;
  private pendingBackendInit: Promise<boolean>;
  private pendingBackendInitId = 0;

  constructor(public ENV: Environment) {
    this.state = new EngineState();
  }

  async ready(): Promise<void> {
    if (this.pendingBackendInit != null) {
      return this.pendingBackendInit.then(() => {});
    }
    if (this.backendInstance != null) {
      return;
    }
    const sortedBackends = this.getSortedBackends();

    for (let i = 0; i < sortedBackends.length; i++) {
      const backendName = sortedBackends[i];
      const success = await this.initializeBackend(backendName).success;
      if (success) {
        await this.setBackend(backendName);
        return;
      }
    }

    throw new Error(
        `Could not initialize any backends, all backend initializations ` +
        `failed.`);
  }

  get backend(): KernelBackend {
    if (this.pendingBackendInit != null) {
      throw new Error(
          `Backend '${this.backendName}' has not yet been initialized. Make ` +
          `sure to await tf.ready() before calling other methods`);
    }
    if (this.backendInstance == null) {
      const {name, asyncInit} = this.initializeBackendsAndReturnBest();
      if (asyncInit) {
        throw new Error(
            `The highest priority backend '${name}' has not yet been ` +
            `initialized. Make sure to await tf.ready() before calling ` +
            `other methods`);
      }
      this.setBackend(name);
    }
    return this.backendInstance;
  }

  backendNames(): string[] {
    return Object.keys(this.registryFactory);
  }

  findBackend(backendName: string): KernelBackend {
    if (!(backendName in this.registry)) {
      // If the backend hasn't been initialized but we have a registry entry for
      // it, initialize it and return it.
      if (backendName in this.registryFactory) {
        const {asyncInit} = this.initializeBackend(backendName);
        if (asyncInit) {
          // Backend is not ready yet.
          return null;
        }
      } else {
        return null;
      }
    }
    return this.registry[backendName];
  }

  findBackendFactory(backendName: string):
      () => KernelBackend | Promise<KernelBackend> {
    if (!(backendName in this.registryFactory)) {
      return null;
    }
    return this.registryFactory[backendName].factory;
  }

  registerBackend(
      backendName: string,
      factory: () => KernelBackend | Promise<KernelBackend>,
      priority = 1): boolean {
    if (backendName in this.registryFactory) {
      console.warn(
          `${backendName} backend was already registered. ` +
          `Reusing existing backend factory.`);
      return false;
    }
    this.registryFactory[backendName] = {factory, priority};
    return true;
  }

  async setBackend(backendName: string): Promise<boolean> {
    if (this.registryFactory[backendName] == null) {
      throw new Error(`Backend name '${backendName}' not found in registry`);
    }
    this.backendName = backendName;
    if (this.registry[backendName] == null) {
      this.backendInstance = null;
      const {success, asyncInit} = this.initializeBackend(backendName);
      const result = asyncInit ? await success : success;
      if (!result) {
        return false;
      }
    }
    this.backendInstance = this.registry[backendName];

    // Reset the profiler.
    this.profiler = new Profiler(this.backendInstance);

    return true;
  }

  /**
   * Initializes a backend by looking up the backend name in the factory
   * registry and calling the factory method. Returns a boolean representing
   * whether the initialization of the backend suceeded. Throws an error if
   * there is no backend in the factory registry.
   */
  private initializeBackend(backendName: string):
      {success: boolean|Promise<boolean>, asyncInit: boolean} {
    const registryFactoryEntry = this.registryFactory[backendName];
    if (registryFactoryEntry == null) {
      throw new Error(
          `Cannot initialize backend ${backendName}, no registration found.`);
    }

    try {
      const backend = registryFactoryEntry.factory();
      // Test if the factory returns a promise.
      if (Promise.resolve(backend) === backend) {
        const promiseId = ++this.pendingBackendInitId;
        const success =
            backend
                .then(backendInstance => {
                  // Outdated promise. Another backend was set in the meantime.
                  if (promiseId < this.pendingBackendInitId) {
                    return false;
                  }
                  this.registry[backendName] = backendInstance;
                  this.pendingBackendInit = null;
                  return true;
                })
                .catch(err => {
                  // Outdated promise. Another backend was set in the meantime.
                  if (promiseId < this.pendingBackendInitId) {
                    return false;
                  }
                  this.pendingBackendInit = null;
                  console.warn(
                      `Initialization of backend ${backendName} failed`);
                  console.warn(err.stack || err.message);
                  return false;
                });
        this.pendingBackendInit = success;
        return {success, asyncInit: true};
      } else {
        this.registry[backendName] = backend as KernelBackend;
        return {success: true, asyncInit: false};
      }
    } catch (err) {
      console.warn(`Initialization of backend ${backendName} failed`);
      console.warn(err.stack || err.message);
      return {success: false, asyncInit: false};
    }
  }

  removeBackend(backendName: string): void {
    if (!(backendName in this.registryFactory)) {
      throw new Error(`${backendName} backend not found in registry`);
    }
    if (this.backendName === backendName && this.pendingBackendInit != null) {
      // There is a pending promise of the backend we want to remove. Make it
      // obsolete.
      this.pendingBackendInitId++;
    }

    if (backendName in this.registry) {
      this.registry[backendName].dispose();
      delete this.registry[backendName];
    }

    delete this.registryFactory[backendName];

    // Unset the backend if it is active.
    if (this.backendName === backendName) {
      this.pendingBackendInit = null;
      this.backendName = null;
      this.backendInstance = null;
    }
  }

  private getSortedBackends(): string[] {
    if (Object.keys(this.registryFactory).length === 0) {
      throw new Error('No backend found in registry.');
    }
    return Object.keys(this.registryFactory).sort((a: string, b: string) => {
      // Highest priority comes first.
      return this.registryFactory[b].priority -
          this.registryFactory[a].priority;
    });
  }

  private initializeBackendsAndReturnBest():
      {name: string, asyncInit: boolean} {
    const sortedBackends = this.getSortedBackends();

    for (let i = 0; i < sortedBackends.length; i++) {
      const backendName = sortedBackends[i];
      const {success, asyncInit} = this.initializeBackend(backendName);
      if (asyncInit || success) {
        return {name: backendName, asyncInit};
      }
    }
    throw new Error(
        `Could not initialize any backends, all backend initializations ` +
        `failed.`);
  }

  moveData(destBackend: KernelBackend, dataId: DataId) {
    this.write(destBackend, dataId, this.readSync(dataId));
  }

  tidy<T extends TensorContainer>(nameOrFn: string|ScopeFn<T>, fn?: ScopeFn<T>):
      T {
    let name: string = null;
    if (fn == null) {
      // Called with only 1 argument.
      if (typeof nameOrFn !== 'function') {
        throw new Error('Please provide a function to tidy()');
      }
      fn = nameOrFn;
    } else {
      // Called with 2 arguments.
      if (typeof nameOrFn !== 'string' && !(nameOrFn instanceof String)) {
        throw new Error(
            'When calling with two arguments, the first argument ' +
            'to tidy() must be a string');
      }
      if (typeof fn !== 'function') {
        throw new Error(
            'When calling with two arguments, the 2nd argument ' +
            'to tidy() must be a function');
      }
      name = nameOrFn as string;
      // TODO(nsthorat,smilkov): Do operation logging and performance
      // profiling.
    }
    let result: T;
    return this.scopedRun(
        () => this.startScope(name), () => this.endScope(result), () => {
          result = fn();
          if (result instanceof Promise) {
            console.error('Cannot return a Promise inside of tidy.');
          }
          return result;
        });
  }

  private scopedRun<T>(start: () => void, end: () => void, f: () => T): T {
    start();
    try {
      const res = f();
      end();
      return res;
    } catch (ex) {
      end();
      throw ex;
    }
  }

  private static nextTensorId = 0;
  nextTensorId(): number {
    return Engine.nextTensorId++;
  }

  private static nextVariableId = 0;
  nextVariableId(): number {
    return Engine.nextVariableId++;
  }

  /**
   * This method is called instead of the public-facing tensor.clone() when
   * saving a tensor for backwards pass. It makes sure to add the clone
   * operation to the tape regardless of being called inside a kernel
   * execution.
   */
  private clone(x: Tensor): Tensor {
    const y = Tensor.make(x.shape, {dataId: x.dataId}, x.dtype);
    this.addTapeNode([x], y, dy => [dy.toFloat()]);
    return y;
  }

  runKernel<T extends Tensor|Tensor[], I extends NamedTensorMap>(
      forwardFunc: ForwardFunc<T>,
      inputs: I,
      backwardsFunc?: (dy: T, saved: Tensor[]) => {[P in keyof I]: () => I[P]},
      ): T {
    let result: T;
    let saved: Tensor[] = [];
    const isTapeOn = this.isTapeOn();
    const scopeName =
        this.state.activeScope != null ? this.state.activeScope.name : '';
    const saveFunc: GradSaveFunc = (tensors) => {
      // Do not save unless we are recording to the tape. Otherwise it would
      // cause a mem leak since we would never run backprop, which disposes
      // the kept tensors.
      if (!isTapeOn) {
        return;
      }
      saved = tensors.map(tensor => this.keep(this.clone(tensor)));
    };

    const startingBytecount = this.state.numBytes;
    const startingNumTensors = this.state.numTensors;

    // Stop recording to a tape when running a kernel.
    this.scopedRun(
        () => this.state.kernelDepth++, () => this.state.kernelDepth--, () => {
          if (!this.ENV.getBool('DEBUG')) {
            result = forwardFunc(this.backend, saveFunc);
          } else {
            result = this.profiler.profileKernel(
                scopeName, () => forwardFunc(this.backend, saveFunc));
          }
        });

    if (isTapeOn) {
      const tapeNode: TapeNode = {
        id: this.state.nextTapeNodeId++,
        name: scopeName,
        inputs,
        outputs: Array.isArray(result) ? result : [result] as Tensor[],
        saved
      };
      if (backwardsFunc != null) {
        tapeNode.gradient = (dy: T) => backwardsFunc(dy, saved);
      }
      this.state.activeTape.push(tapeNode);
    }

    if (this.state.profiling) {
      this.state.activeProfile.kernels.push({
        name: scopeName,
        bytesAdded: this.state.numBytes - startingBytecount,
        totalBytesSnapshot: this.state.numBytes,
        tensorsAdded: this.state.numTensors - startingNumTensors,
        totalTensorsSnapshot: this.state.numTensors,
        inputShapes: Object.keys(inputs).map(key => inputs[key].shape),
        outputShape: Array.isArray(result) ?
            (result as Tensor[]).map(item => (item as Tensor).shape) :
            (result as Tensor).shape
      });
    }

    return result;
  }

  // TensorManager implementation.

  registerTensor(a: Tensor|Variable, backend?: KernelBackend): void {
    const refCount = this.state.tensorInfo.has(a.dataId) ?
        this.state.tensorInfo.get(a.dataId).refCount :
        0;
    this.state.numTensors++;
    if (a.dtype === 'string') {
      this.state.numStringTensors++;
    }
    if (refCount === 0) {
      this.state.numDataBuffers++;

      // Bytes for complex numbers are counted by their components. Bytes for
      // string tensors are counted when writing values.
      let bytes = 0;
      if (a.dtype !== 'complex64' && a.dtype !== 'string') {
        bytes = a.size * util.bytesPerElement(a.dtype);
      }
      this.state.tensorInfo.set(a.dataId, {
        backend: backend != null ? backend : this.backend,
        dtype: a.dtype,
        shape: a.shape,
        bytes,
        refCount: 0
      });
      this.state.numBytes += bytes;
      if (backend != null) {
        backend.register(a.dataId, a.shape, a.dtype);
      } else {
        this.backend.register(a.dataId, a.shape, a.dtype);
      }
    }
    this.state.tensorInfo.get(a.dataId).refCount++;
    if (!(a instanceof Variable)) {
      this.track(a);
    }
  }

  registerVariable(v: Variable) {
    if (this.state.registeredVariables[v.name] != null) {
      throw new Error(`Variable with name ${v.name} was already registered`);
    }
    this.state.registeredVariables[v.name] = v;
  }

  disposeTensor(a: Tensor): void {
    if (!this.state.tensorInfo.has(a.dataId)) {
      return;
    }

    this.state.numTensors--;
    if (a.dtype === 'string') {
      this.state.numStringTensors--;
    }
    const info = this.state.tensorInfo.get(a.dataId);
    const refCount = info.refCount;
    if (refCount <= 1) {
      // Don't count bytes for complex numbers as they are counted by their
      // components.
      if (a.dtype !== 'complex64') {
        this.state.numBytes -= info.bytes;
      }
      this.state.numDataBuffers--;
      info.backend.disposeData(a.dataId);
      this.state.tensorInfo.delete(a.dataId);
    } else {
      this.state.tensorInfo.get(a.dataId).refCount--;
    }
    // TODO(nsthorat): Construct an error and save the stack trace for
    // debugging when in debug mode. Creating a stack trace is too expensive
    // to do unconditionally.
  }

  disposeVariables(): void {
    for (const varName in this.state.registeredVariables) {
      const v = this.state.registeredVariables[varName];
      this.disposeVariable(v);
    }
  }

  disposeVariable(v: Variable): void {
    this.disposeTensor(v);
    if (this.state.registeredVariables[v.name] != null) {
      delete this.state.registeredVariables[v.name];
    }
  }

  memory(): MemoryInfo {
    const info = this.backend.memory() as MemoryInfo;
    info.numTensors = this.state.numTensors;
    info.numDataBuffers = this.state.numDataBuffers;
    info.numBytes = this.state.numBytes;
    if (this.state.numStringTensors > 0) {
      info.unreliable = true;
      if (info.reasons == null) {
        info.reasons = [];
      }
      info.reasons.push(
          'Memory usage by string tensors is approximate ' +
          '(2 bytes per character)');
    }
    return info;
  }

  async profile(query: () => TensorContainer): Promise<ProfileInfo> {
    this.state.profiling = true;

    const startBytes = this.state.numBytes;
    const startNumTensors = this.state.numTensors;

    this.state.activeProfile.kernels = [];
    this.state.activeProfile.result = query();

    this.state.profiling = false;

    this.state.activeProfile.peakBytes = Math.max(
        ...this.state.activeProfile.kernels.map(d => d.totalBytesSnapshot));
    this.state.activeProfile.newBytes = this.state.numBytes - startBytes;
    this.state.activeProfile.newTensors =
        this.state.numTensors - startNumTensors;
    return this.state.activeProfile;
  }

  isTapeOn(): boolean {
    return this.state.gradientDepth > 0 && this.state.kernelDepth === 0;
  }

  private addTapeNode(
      inputs: Tensor[], result: Tensor,
      gradientsFunc: (dy: Tensor) => Tensor[]): void {
    const inputsMap: NamedTensorMap = {};
    inputs.forEach((input, idx) => {
      inputsMap[idx] = input;
    });

    const gradient = (dy: Tensor) => {
      const res = gradientsFunc(dy);
      const resMap: NamedGradientMap = {};
      res.forEach((r, idx) => {
        resMap[idx] = () => r;
      });
      return resMap;
    };

    const tapeNode: TapeNode = {
      id: this.state.nextTapeNodeId++,
      name: this.state.activeScope.name,
      inputs: inputsMap,
      outputs: [result],
      gradient
    };
    this.state.activeTape.push(tapeNode);
  }

  keep<T extends Tensor>(result: T): T {
    result.kept = true;
    return result;
  }

  private startTape() {
    if (this.state.gradientDepth === 0) {
      this.state.activeTape = [];
    }
    this.state.gradientDepth++;
  }

  private endTape() {
    this.state.gradientDepth--;
  }

  /**
   * Start a scope. Use this with endScope() to achieve the same functionality
   * as scope() without the need for a function closure.
   */
  startScope(name?: string) {
    const scopeInfo: ScopeState = {
      track: [],
      name: 'unnamed scope',
      id: this.state.nextScopeId++
    };
    if (name) {
      scopeInfo.name = name;
    }
    this.state.scopeStack.push(scopeInfo);
    this.state.activeScope = scopeInfo;
  }

  /**
   * End a scope. Use this with startScope() to achieve the same functionality
   * as scope() without the need for a function closure.
   */
  endScope(result?: TensorContainer) {
    const tensorsToTrackInParent = getTensorsInContainer(result);
    const tensorsToTrackInParentSet =
        new Set(tensorsToTrackInParent.map(t => t.id));

    // Dispose the arrays tracked in this scope.
    for (let i = 0; i < this.state.activeScope.track.length; i++) {
      const tensor = this.state.activeScope.track[i];
      if (!tensor.kept && !tensorsToTrackInParentSet.has(tensor.id)) {
        tensor.dispose();
      }
    }

    const oldScope = this.state.scopeStack.pop();
    this.state.activeScope = this.state.scopeStack.length === 0 ?
        null :
        this.state.scopeStack[this.state.scopeStack.length - 1];

    // Track the current result in the parent scope.
    tensorsToTrackInParent.forEach(tensor => {
      // Only track the tensor if was allocated in the inner scope and is not
      // globally kept.
      if (!tensor.kept && tensor.scopeId === oldScope.id) {
        this.track(tensor);
      }
    });
  }

  /**
   * Returns gradients of `f` with respect to each of the `xs`. The gradients
   * returned are of the same length as `xs`, but some might be null if `f`
   * was not a function of that `x`. It also takes optional dy to multiply the
   * gradient, which defaults to `1`.
   */
  gradients<T extends Tensor>(
      f: () => T, xs: Tensor[], dy?: T,
      allowNoGradients = false): {value: T, grads: Tensor[]} {
    util.assert(
        xs.length > 0, () => 'gradients() received an empty list of xs.');
    if (dy != null && dy.dtype !== 'float32') {
      throw new Error(`dy must have 'float32' dtype, but has '${dy.dtype}'`);
    }

    const y = this.scopedRun(
        () => this.startTape(), () => this.endTape(),
        () => this.tidy('forward', f));

    util.assert(
        y instanceof Tensor,
        () => 'The result y returned by f() must be a tensor.');
    // Filter out the nodes that don't connect x => y.
    const filteredTape = getFilteredNodesXToY(this.state.activeTape, xs, y);
    if (!allowNoGradients && filteredTape.length === 0 && xs.length > 0) {
      throw new Error(
          'Cannot compute gradient of y=f(x) with respect to x. Make sure ' +
          'that the f you passed encloses all operations that lead from x ' +
          'to y.');
    }

    return this.tidy('backward', () => {
      const accumulatedGradientMap: {[tensorId: number]: Tensor} = {};
      accumulatedGradientMap[y.id] = (dy == null) ? ones(y.shape) : dy;

      // Backprop gradients through the filtered nodes.
      backpropagateGradients(
          accumulatedGradientMap, filteredTape,
          // Pass the tidy function to avoid circular dep with `tape.ts`.
          f => this.tidy(f as ScopeFn<Tensor>));
      const grads = xs.map(x => accumulatedGradientMap[x.id]);

      if (this.state.gradientDepth === 0) {
        // This means that we are not computing higher-order gradients
        // and can clean up the tape.
        this.state.activeTape.forEach(node => {
          for (const key in node.saved) {
            node.saved[key].dispose();
          }
        });
        this.state.activeTape = null;
      }
      return {value: y, grads};
    });
  }

  customGrad<T extends Tensor>(f: CustomGradientFunc<T>):
      (...args: Array<Tensor|GradSaveFunc>) => T {
    util.assert(
        util.isFunction(f),
        () => 'The f passed in customGrad(f) must be a function.');
    return (...inputs: Tensor[]): T => {
      util.assert(
          inputs.every(t => t instanceof Tensor),
          () => 'The args passed in customGrad(f)(x1, x2,...) must all be ' +
              'tensors');

      let res: {
        value: T,
        gradFunc: (dy: T, saved: Tensor[]) => Tensor | Tensor[],
      };
      const inputMap: NamedTensorMap = {};
      inputs.forEach((input, i) => {
        inputMap[i] = input;
      });
      return this.runKernel(
          (_, save) => {
            res = f(...[...inputs, save]);
            util.assert(
                res.value instanceof Tensor,
                () => 'The function f passed in customGrad(f) must return an ' +
                    'object where `obj.value` is a tensor');
            util.assert(
                util.isFunction(res.gradFunc),
                () => 'The function f passed in customGrad(f) must return an ' +
                    'object where `obj.gradFunc` is a function.');
            return res.value;
          },
          inputMap,
          (dy: T, saved: Tensor[]) => {
            const gradRes = res.gradFunc(dy, saved);
            const grads: Tensor[] =
                Array.isArray(gradRes) ? gradRes : [gradRes];
            util.assert(
                grads.length === inputs.length,
                () => 'The function f passed in customGrad(f) must return an ' +
                    'object where `obj.gradFunc` is a function that returns ' +
                    'the same number of tensors as inputs passed to f(...).');
            util.assert(
                grads.every(t => t instanceof Tensor),
                () => 'The function f passed in customGrad(f) must return an ' +
                    'object where `obj.gradFunc` is a function that returns ' +
                    'a list of only tensors.');
            const gradMap: {[key: string]: () => Tensor} = {};
            grads.forEach((grad, i) => {
              gradMap[i] = () => grad;
            });
            return gradMap;
          });
    };
  }

  // Forwarding to backend.
  write(destBackend: KernelBackend, dataId: DataId, values: BackendValues):
      void {
    const info = this.state.tensorInfo.get(dataId);

    const srcBackend = info.backend;
    destBackend = destBackend || this.backend;

    // Bytes for string tensors are counted when writing.
    if (info.dtype === 'string') {
      const newBytes = bytesFromStringArray(values as Uint8Array[]);
      this.state.numBytes += newBytes - info.bytes;
      info.bytes = newBytes;
    }

    if (destBackend !== srcBackend) {
      // Delete the tensor from the old backend and move it to the new
      // backend.
      srcBackend.disposeData(dataId);
      info.backend = destBackend;
      destBackend.register(dataId, info.shape, info.dtype);
    }
    destBackend.write(dataId, values);
  }
  readSync(dataId: DataId): BackendValues {
    // Route the read to the correct backend.
    const info = this.state.tensorInfo.get(dataId);
    return info.backend.readSync(dataId);
  }
  read(dataId: DataId): Promise<BackendValues> {
    // Route the read to the correct backend.
    const info = this.state.tensorInfo.get(dataId);
    return info.backend.read(dataId);
  }
  fromPixels(
      pixels: PixelData|ImageData|HTMLImageElement|HTMLCanvasElement|
      HTMLVideoElement,
      numChannels: number): Tensor3D {
    return this.backend.fromPixels(pixels, numChannels);
  }
  async time(query: () => void): Promise<TimingInfo> {
    const start = now();
    const timingInfo = await this.backend.time(query) as TimingInfo;
    timingInfo.wallMs = now() - start;
    return timingInfo;
  }

  /**
   * Tracks a Tensor in the current scope to be automatically cleaned up
   * when the current scope ends, and returns the value.
   *
   * @param result The Tensor to track in the current scope.
   */
  private track<T extends Tensor>(result: T): T {
    if (this.state.activeScope != null) {
      result.scopeId = this.state.activeScope.id;
      this.state.activeScope.track.push(result);
    }

    return result;
  }

  get registeredVariables(): NamedVariableMap {
    return this.state.registeredVariables;
  }

  /**
   * Resets the engine state. Removes all backends but does not remove
   * registered backend factories.
   */
  reset(): void {
    // Make any pending promise obsolete.
    this.pendingBackendInitId++;

    this.state.dispose();
    this.ENV.reset();
    this.state = new EngineState();

    for (const backendName in this.registry) {
      this.registry[backendName].dispose();
      delete this.registry[backendName];
    }
    this.backendName = null;
    this.backendInstance = null;
    this.pendingBackendInit = null;
  }
}

function ones(shape: number[]): Tensor {
  const values = makeOnesTypedArray(sizeFromShape(shape), 'float32');
  return Tensor.make(shape, {values});
}

let GLOBAL: {_tfengine: Engine};
function getGlobalNamespace(): {_tfengine: Engine} {
  if (GLOBAL == null) {
    // tslint:disable-next-line:no-any
    let ns: any;
    if (typeof (window) !== 'undefined') {
      ns = window;
    } else if (typeof (global) !== 'undefined') {
      ns = global;
    } else if (typeof (process) !== 'undefined') {
      ns = process;
    } else if (typeof (self) !== 'undefined') {
      ns = self;
    } else {
      throw new Error('Could not find a global object');
    }
    GLOBAL = ns;
  }
  return GLOBAL;
}

function getOrMakeEngine(): Engine {
  const ns = getGlobalNamespace();
  if (ns._tfengine == null) {
    const environment = new Environment(ns);
    ns._tfengine = new Engine(environment);
  }
  setEnvironmentGlobal(ns._tfengine.ENV);

  // Tell the current tensor interface that the global engine is responsible
  // for tracking.
  setTensorTracker(() => ns._tfengine);
  return ns._tfengine;
}

export let ENGINE = getOrMakeEngine();


text:
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="springContextUtil" class="com.docker.utils.SpringContextUtil"/>

	<bean id="dockerStatusHelper" class="com.docker.storage.mongodb.MongoHelper"
		init-method="init">
		<property name="host" value="${mongo.dbhost}" />
		<property name="connectionsPerHost" value="${connectionsPerHost}" />
		<property name="dbName" value="${dockerstatus.dbname}" />
		<property name="username" value="${mongo.username}" />
        <property name="password" value="${mongo.password}"/>
	</bean>
	<bean id="configHelper" class="com.docker.storage.mongodb.MongoHelper"
		  init-method="init">
		<property name="host" value="${mongo.dbhost}" />
		<property name="connectionsPerHost" value="${connectionsPerHost}" />
		<property name="dbName" value="${config.dbname}" />
		<property name="username" value="${mongo.username}" />
		<property name="password" value="${mongo.password}"/>
	</bean>
	<bean id="dockerStatusDAO" class="com.docker.storage.mongodb.daos.DockerStatusDAO"
		init-method="init">
		<property name="mongoHelper" ref="dockerStatusHelper" />
	</bean>
	<bean id="serversDAO" class="com.docker.storage.mongodb.daos.ServersDAO"
		  init-method="init">
		<property name="mongoHelper" ref="configHelper" />
	</bean>
	<bean id="lansDAO" class="com.docker.storage.mongodb.daos.LansDAO"
		  init-method="init">
		<property name="mongoHelper" ref="configHelper" />
	</bean>
	<bean id="sdockerDAO" class="com.docker.storage.mongodb.daos.SDockerDAO"
		  init-method="init">
		<property name="mongoHelper" ref="configHelper" />
	</bean>
</beans>

redpajama_set_name: RedPajamaGithub


text:
import styled from 'styled-components';

const PageHeader = styled.h1`
  border-bottom: 1px solid;
`;

export default PageHeader;


text:
@interface SHCWindowController : NSWindowController

- (IBAction)performFindAction:(id)sender;

@end


text:
{% extends "mod/server_detail_base.html" %}

{% load get_option %}

{% comment %}
context variables:
    server
    form
    moderator
{% endcomment %}

{% block title %}upTee - {{ server|get_option:'sv_name' }} - Upload map{% endblock title %}

{% block tab_content %}

    <form action="{{ request.path }}" method="post" enctype="multipart/form-data">
        <input type="hidden" name="csrfmiddlewaretoken" value="{% with csrf_token as csrf_token_clean %}{{ csrf_token_clean }}{% endwith %}">
        {{ form.as_p }}

<input class="button" type="submit" value="Upload">

    </form>

{% endblock tab_content %}


text:
#ifndef ScriptExecutionContext_h
#define ScriptExecutionContext_h

#include "ActiveDOMObject.h"
#include "ConsoleTypes.h"
#include "KURL.h"
#include "SecurityContext.h"
#include "Supplementable.h"
#include <wtf/HashSet.h>

namespace JSC {
class ExecState;
class VM;
}

namespace WebCore {

class CachedScript;
class DatabaseContext;
class DOMTimer;
class EventListener;
class EventQueue;
class EventTarget;
class MessagePort;
class ScriptCallStack;

#if ENABLE(BLOB)
class PublicURLManager;
#endif

class ScriptExecutionContext : public SecurityContext, public Supplementable<ScriptExecutionContext> {
public:
    ScriptExecutionContext();
    virtual ~ScriptExecutionContext();

    virtual bool isDocument() const { return false; }
    virtual bool isWorkerGlobalScope() const { return false; }

    virtual bool isContextThread() const { return true; }
    virtual bool isJSExecutionForbidden() const = 0;

    virtual const KURL& url() const = 0;
    virtual KURL completeURL(const String& url) const = 0;

    virtual String userAgent(const KURL&) const = 0;

    virtual void disableEval(const String& errorMessage) = 0;

    bool sanitizeScriptError(String& errorMessage, int& lineNumber, int& columnNumber, String& sourceURL, CachedScript* = 0);
    // FIXME: <http://webkit.org/b/114315> ScriptExecutionContext log exception should include a column number
    void reportException(const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL, PassRefPtr<ScriptCallStack>, CachedScript* = 0);

    void addConsoleMessage(MessageSource, MessageLevel, const String& message, const String& sourceURL, unsigned lineNumber, unsigned columnNumber, JSC::ExecState* = 0, unsigned long requestIdentifier = 0);
    virtual void addConsoleMessage(MessageSource, MessageLevel, const String& message, unsigned long requestIdentifier = 0) = 0;

    virtual SecurityOrigin* topOrigin() const = 0;

#if ENABLE(BLOB)
    PublicURLManager& publicURLManager();
#endif
    // Active objects are not garbage collected even if inaccessible, e.g. because their activity may result in callbacks being invoked.
    bool canSuspendActiveDOMObjects();
    // Active objects can be asked to suspend even if canSuspendActiveDOMObjects() returns 'false' -
    // step-by-step JS debugging is one example.
    virtual void suspendActiveDOMObjects(ActiveDOMObject::ReasonForSuspension);
    virtual void resumeActiveDOMObjects(ActiveDOMObject::ReasonForSuspension);
    virtual void stopActiveDOMObjects();

    bool activeDOMObjectsAreSuspended() const { return m_activeDOMObjectsAreSuspended; }
    bool activeDOMObjectsAreStopped() const { return m_activeDOMObjectsAreStopped; }

    // Called from the constructor and destructors of ActiveDOMObject.
    void didCreateActiveDOMObject(ActiveDOMObject*);
    void willDestroyActiveDOMObject(ActiveDOMObject*);

    // Called after the construction of an ActiveDOMObject to synchronize suspend state.
    void suspendActiveDOMObjectIfNeeded(ActiveDOMObject*);

    typedef HashSet<ActiveDOMObject*> ActiveDOMObjectsSet;
    const ActiveDOMObjectsSet& activeDOMObjects() const { return m_activeDOMObjects; }

    void didCreateDestructionObserver(ContextDestructionObserver*);
    void willDestroyDestructionObserver(ContextDestructionObserver*);

    // MessagePort is conceptually a kind of ActiveDOMObject, but it needs to be tracked separately for message dispatch.
    void processMessagePortMessagesSoon();
    void dispatchMessagePortEvents();
    void createdMessagePort(MessagePort*);
    void destroyedMessagePort(MessagePort*);
    const HashSet<MessagePort*>& messagePorts() const { return m_messagePorts; }

    void ref() { refScriptExecutionContext(); }
    void deref() { derefScriptExecutionContext(); }

    class Task {
        WTF_MAKE_NONCOPYABLE(Task);
        WTF_MAKE_FAST_ALLOCATED;
    public:
        Task() { }
        virtual ~Task();
        virtual void performTask(ScriptExecutionContext*) = 0;
        // Certain tasks get marked specially so that they aren't discarded, and are executed, when the context is shutting down its message queue.
        virtual bool isCleanupTask() const { return false; }
    };

    virtual void postTask(PassOwnPtr<Task>) = 0; // Executes the task on context's thread asynchronously.

    // Gets the next id in a circular sequence from 1 to 2^31-1.
    int circularSequentialID();

    bool addTimeout(int timeoutId, DOMTimer* timer) { return m_timeouts.add(timeoutId, timer).isNewEntry; }
    void removeTimeout(int timeoutId) { m_timeouts.remove(timeoutId); }
    DOMTimer* findTimeout(int timeoutId) { return m_timeouts.get(timeoutId); }

    JSC::VM* vm();

    // Interval is in seconds.
    void adjustMinimumTimerInterval(double oldMinimumTimerInterval);
    virtual double minimumTimerInterval() const;

    void didChangeTimerAlignmentInterval();
    virtual double timerAlignmentInterval() const;

    virtual EventQueue& eventQueue() const = 0;

#if ENABLE(SQL_DATABASE)
    void setDatabaseContext(DatabaseContext*);
#endif

protected:
    class AddConsoleMessageTask : public Task {
    public:
        static PassOwnPtr<AddConsoleMessageTask> create(MessageSource source, MessageLevel level, const String& message)
        {
            return adoptPtr(new AddConsoleMessageTask(source, level, message));
        }
        virtual void performTask(ScriptExecutionContext*);
    private:
        AddConsoleMessageTask(MessageSource source, MessageLevel level, const String& message)
            : m_source(source)
            , m_level(level)
            , m_message(message.isolatedCopy())
        {
        }
        MessageSource m_source;
        MessageLevel m_level;
        String m_message;
    };

    ActiveDOMObject::ReasonForSuspension reasonForSuspendingActiveDOMObjects() const { return m_reasonForSuspendingActiveDOMObjects; }

private:
    virtual void addMessage(MessageSource, MessageLevel, const String& message, const String& sourceURL, unsigned lineNumber, unsigned columnNumber, PassRefPtr<ScriptCallStack>, JSC::ExecState* = 0, unsigned long requestIdentifier = 0) = 0;
    virtual EventTarget* errorEventTarget() = 0;
    virtual void logExceptionToConsole(const String& errorMessage, const String& sourceURL, int lineNumber, int columnNumber, PassRefPtr<ScriptCallStack>) = 0;
    bool dispatchErrorEvent(const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL, CachedScript*);

    void closeMessagePorts();

    virtual void refScriptExecutionContext() = 0;
    virtual void derefScriptExecutionContext() = 0;

    HashSet<MessagePort*> m_messagePorts;
    HashSet<ContextDestructionObserver*> m_destructionObservers;
    ActiveDOMObjectsSet m_activeDOMObjects;
    bool m_iteratingActiveDOMObjects;
    bool m_inDestructor;

    int m_circularSequentialID;
    typedef HashMap<int, DOMTimer*> TimeoutMap;
    TimeoutMap m_timeouts;

    bool m_inDispatchErrorEvent;
    class PendingException;
    OwnPtr<Vector<OwnPtr<PendingException> > > m_pendingExceptions;

    bool m_activeDOMObjectsAreSuspended;
    ActiveDOMObject::ReasonForSuspension m_reasonForSuspendingActiveDOMObjects;
    bool m_activeDOMObjectsAreStopped;

#if ENABLE(BLOB)
    OwnPtr<PublicURLManager> m_publicURLManager;
#endif

#if ENABLE(SQL_DATABASE)
    RefPtr<DatabaseContext> m_databaseContext;
#endif
};

} // namespace WebCore

#endif // ScriptExecutionContext_h


text:
namespace arc {

class ArcClipboardBridge
    : public ArcService,
      public InstanceHolder<mojom::ClipboardInstance>::Observer,
      public mojom::ClipboardHost {
 public:
  explicit ArcClipboardBridge(ArcBridgeService* bridge_service);
  ~ArcClipboardBridge() override;

  // InstanceHolder<mojom::ClipboardInstance>::Observer overrides.
  void OnInstanceReady() override;

  // mojom::ClipboardHost overrides.
  void SetTextContent(const mojo::String& text) override;
  void GetTextContent() override;

 private:
  bool CalledOnValidThread();

  mojo::Binding<mojom::ClipboardHost> binding_;

  base::ThreadChecker thread_checker_;

  DISALLOW_COPY_AND_ASSIGN(ArcClipboardBridge);
};

}  // namespace arc

#endif  // COMPONENTS_ARC_CLIPBOARD_ARC_CLIPBOARD_BRIDGE_H_


text:
package org.springframework.web.reactive.result.view;

import java.util.Locale;
import java.util.function.Function;

import reactor.core.publisher.Mono;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.PatternMatchUtils;

/**
 * A {@link ViewResolver} that allows direct resolution of symbolic view names
 * to URLs without explicit mapping definitions. This is useful if symbolic names
 * match the names of view resources in a straightforward manner (i.e. the
 * symbolic name is the unique part of the resource's filename), without the need
 * for a dedicated mapping to be defined for each view.
 *
 *
Supports {@link AbstractUrlBasedView} subclasses like
 * {@link org.springframework.web.reactive.result.view.freemarker.FreeMarkerView}.
 * The view class for all views generated by this resolver can be specified
 * via the "viewClass" property.
 *
 *
View names can either be resource URLs themselves, or get augmented by a
 * specified prefix and/or suffix. Exporting an attribute that holds the
 * RequestContext to all views is explicitly supported.
 *
 *
Example: prefix="templates/", suffix=".ftl", viewname="test" →
 * "templates/test.ftl"
 *
 *
As a special feature, redirect URLs can be specified via the "redirect:"
 * prefix. E.g.: "redirect:myAction" will trigger a redirect to the given
 * URL, rather than resolution as standard view name. This is typically used
 * for redirecting to a controller URL after finishing a form workflow.
 *
 *
Note: This class does not support localized resolution, i.e. resolving
 * a symbolic view name to different resources depending on the current locale.
 *
 * @author Rossen Stoyanchev
 * @author Sebastien Deleuze
 * @author Juergen Hoeller
 * @author Sam Brannen
 * @since 5.0
 */
public class UrlBasedViewResolver extends ViewResolverSupport
		implements ViewResolver, ApplicationContextAware, InitializingBean {

	/**
	 * Prefix for special view names that specify a redirect URL (usually
	 * to a controller after a form has been submitted and processed).
	 * Such view names will not be resolved in the configured default
	 * way but rather be treated as special shortcut.
	 */
	public static final String REDIRECT_URL_PREFIX = "redirect:";

	@Nullable
	private Class<?> viewClass;

	private String prefix = "";

	private String suffix = "";

	@Nullable
	private String[] viewNames;

	private Function<String, RedirectView> redirectViewProvider = RedirectView::new;

	@Nullable
	private String requestContextAttribute;

	@Nullable
	private ApplicationContext applicationContext;

	/**
	 * Set the view class that should be used to create views.
	 * @param viewClass a class that is assignable to the required view class
	 * (by default: AbstractUrlBasedView)
	 * @see #requiredViewClass()
	 * @see #instantiateView()
	 * @see AbstractUrlBasedView
	 */
	public void setViewClass(@Nullable Class<?> viewClass) {
		if (viewClass != null && !requiredViewClass().isAssignableFrom(viewClass)) {
			String name = viewClass.getName();
			throw new IllegalArgumentException("Given view class [" + name + "] " +
					"is not of type [" + requiredViewClass().getName() + "]");
		}
		this.viewClass = viewClass;
	}

	/**
	 * Return the view class to be used to create views.
	 * @see #setViewClass
	 */
	@Nullable
	protected Class<?> getViewClass() {
		return this.viewClass;
	}

	/**
	 * Set the prefix that gets prepended to view names when building a URL.
	 */
	public void setPrefix(@Nullable String prefix) {
		this.prefix = (prefix != null ? prefix : "");
	}

	/**
	 * Return the prefix that gets prepended to view names when building a URL.
	 */
	protected String getPrefix() {
		return this.prefix;
	}

	/**
	 * Set the suffix that gets appended to view names when building a URL.
	 */
	public void setSuffix(@Nullable String suffix) {
		this.suffix = (suffix != null ? suffix : "");
	}

	/**
	 * Return the suffix that gets appended to view names when building a URL.
	 */
	protected String getSuffix() {
		return this.suffix;
	}

	/**
	 * Set the view names (or name patterns) that can be handled by this
	 * {@link ViewResolver}. View names can contain simple wildcards such that
	 * 'my*', '*Report' and '*Repo*' will all match the view name 'myReport'.
	 * @see #canHandle
	 */
	public void setViewNames(@Nullable String... viewNames) {
		this.viewNames = viewNames;
	}

	/**
	 * Return the view names (or name patterns) that can be handled by this
	 * {@link ViewResolver}.
	 */
	@Nullable
	protected String[] getViewNames() {
		return this.viewNames;
	}

	/**
	 * URL based {@link RedirectView} provider which can be used to provide, for example,
	 * redirect views with a custom default status code.
	 */
	public void setRedirectViewProvider(Function<String, RedirectView> redirectViewProvider) {
		this.redirectViewProvider = redirectViewProvider;
	}

	/**
	 * Set the name of the {@link RequestContext} attribute for all views.
	 * @param requestContextAttribute name of the RequestContext attribute
	 * @see AbstractView#setRequestContextAttribute
	 */
	public void setRequestContextAttribute(@Nullable String requestContextAttribute) {
		this.requestContextAttribute = requestContextAttribute;
	}

	/**
	 * Return the name of the {@link RequestContext} attribute for all views, if any.
	 */
	@Nullable
	protected String getRequestContextAttribute() {
		return this.requestContextAttribute;
	}

	/**
	 * Accept the containing {@code ApplicationContext}, if any.
	 *
To be used for the initialization of newly created {@link View} instances,
	 * applying lifecycle callbacks and providing access to the containing environment.
	 * @see #setViewClass
	 * @see #createView
	 * @see #applyLifecycleMethods
	 */
	@Override
	public void setApplicationContext(@Nullable ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}

	/**
	 * Return the containing {@code ApplicationContext}, if any.
	 * @see #setApplicationContext
	 */
	@Nullable
	public ApplicationContext getApplicationContext() {
		return this.applicationContext;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		if (getViewClass() == null) {
			throw new IllegalArgumentException("Property 'viewClass' is required");
		}
	}

	@Override
	public Mono<View> resolveViewName(String viewName, Locale locale) {
		if (!canHandle(viewName, locale)) {
			return Mono.empty();
		}

		AbstractUrlBasedView urlBasedView;
		if (viewName.startsWith(REDIRECT_URL_PREFIX)) {
			String redirectUrl = viewName.substring(REDIRECT_URL_PREFIX.length());
			urlBasedView = this.redirectViewProvider.apply(redirectUrl);
		}
		else {
			urlBasedView = createView(viewName);
		}

		View view = applyLifecycleMethods(viewName, urlBasedView);
		try {
			return (urlBasedView.checkResourceExists(locale) ? Mono.just(view) : Mono.empty());
		}
		catch (Exception ex) {
			return Mono.error(ex);
		}
	}

	/**
	 * Indicates whether this {@link ViewResolver} can handle the supplied
	 * view name. If not, an empty result is returned. The default implementation
	 * checks against the configured {@link #setViewNames view names}.
	 * @param viewName the name of the view to retrieve
	 * @param locale the Locale to retrieve the view for
	 * @return whether this resolver applies to the specified view
	 * @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String)
	 */
	protected boolean canHandle(String viewName, Locale locale) {
		String[] viewNames = getViewNames();
		return (viewNames == null || PatternMatchUtils.simpleMatch(viewNames, viewName));
	}

	/**
	 * Return the required type of view for this resolver.
	 * This implementation returns {@link AbstractUrlBasedView}.
	 * @see #instantiateView()
	 * @see AbstractUrlBasedView
	 */
	protected Class<?> requiredViewClass() {
		return AbstractUrlBasedView.class;
	}

	/**
	 * Instantiate the specified view class.
	 *
The default implementation uses reflection to instantiate the class.
	 * @return a new instance of the view class
	 * @since 5.3
	 * @see #setViewClass
	 */
	protected AbstractUrlBasedView instantiateView() {
		Class<?> viewClass = getViewClass();
		Assert.state(viewClass != null, "No view class");
		return (AbstractUrlBasedView) BeanUtils.instantiateClass(viewClass);
	}

	/**
	 * Creates a new View instance of the specified view class and configures it.
	 *
Does not perform any lookup for pre-defined View instances.
	 *
Spring lifecycle methods as defined by the bean container do not have to
	 * be called here: They will be automatically applied afterwards, provided
	 * that an {@link #setApplicationContext ApplicationContext} is available.
	 * @param viewName the name of the view to build
	 * @return the View instance
	 * @see #getViewClass()
	 * @see #applyLifecycleMethods
	 */
	protected AbstractUrlBasedView createView(String viewName) {
		AbstractUrlBasedView view = instantiateView();
		view.setSupportedMediaTypes(getSupportedMediaTypes());
		view.setDefaultCharset(getDefaultCharset());
		view.setUrl(getPrefix() + viewName + getSuffix());

		String requestContextAttribute = getRequestContextAttribute();
		if (requestContextAttribute != null) {
			view.setRequestContextAttribute(requestContextAttribute);
		}

		return view;
	}

	/**
	 * Apply the containing {@link ApplicationContext}'s lifecycle methods
	 * to the given {@link View} instance, if such a context is available.
	 * @param viewName the name of the view
	 * @param view the freshly created View instance, pre-configured with
	 * {@link AbstractUrlBasedView}'s properties
	 * @return the {@link View} instance to use (either the original one
	 * or a decorated variant)
	 * @see #getApplicationContext()
	 * @see ApplicationContext#getAutowireCapableBeanFactory()
	 * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#initializeBean
	 */
	protected View applyLifecycleMethods(String viewName, AbstractUrlBasedView view) {
		ApplicationContext context = getApplicationContext();
		if (context != null) {
			Object initialized = context.getAutowireCapableBeanFactory().initializeBean(view, viewName);
			if (initialized instanceof View) {
				return (View) initialized;
			}
		}
		return view;
	}

}


text:
package net.ichigotake.playground.screenstructure.flow;

import android.content.Context;

public interface ScreenContextFactory {

	Context setUpContext(Screen screen, Context parentContext);

	/**
	 * Tear down any services previously started by {@link #setUpContext(Screen, Context)}. Note that
	 * the Context instance given here may be a wrapper around an instance that this factory
	 * created.
	 */
	void tearDownContext(Context context);

}


text:
@implementation AABaseSet

-(instancetype)initWithHashMap:(AABaseHashMap *)hashMap {
    self = [self init];
    if (self) {
        self.
        self.hashMap = hashMap;
        _mutateFlag = 0;
    }
    return self;
}

-(NSUInteger)count {
    return self.hashMap.count;
}

-(instancetype)addObject:(id)anObject {
    return [[[self class] alloc] initWithHashMap:[self.hashMap setObject:anObject forKey:anObject]];
}

-(instancetype)removeObject:(id)anObject {
    return [[[self class] alloc] initWithHashMap:[self.hashMap removeObjectForKey:anObject]];
}

-(BOOL)containsObject:(id)anObject {
    return !![self.hashMap objectForKey:anObject];
}

-(void)each:(void(^)(id))block {
    [self.hashMap each:^(id key, id _) {
        block(key);
    }];
}

-(NSSet *)toSet {
    NSMutableSet *set = [[NSMutableSet alloc] init];
    [self each:^(id object) {
        [set addObject:object];
    }];
    return [NSSet setWithSet:set];
}

-(NSString *)description {
    return [[self toSet] description];
}

-(NSUInteger)hash {
    return self.hashMap.hash;
}

-(BOOL)isEqual:(id)object {
    return [super isEqual:object] ||
    ([self class] == [object class] && [self isEqualToSet:(AABaseSet *)object]);
}

-(BOOL)isEqualToSet:(AABaseSet *)set {
    return [self.hashMap isEqualToHashMap:set.hashMap];
}

-(id<AAIIterator>)iterator {
    id<AAIIterator> i = [self.hashMap iterator];
    if (i) {
        return [[AASetIterator alloc] initWithIterator:i];
    } else {
        return nil;
    }
}

-(NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
                                 objects:(id __unsafe_unretained [])buffer
                                   count:(NSUInteger)len {
    if (state->state == 0) {
        state->state = 1;
        state->mutationsPtr = (unsigned long *)&_mutateFlag;
        state->extra[0] = (unsigned long)(__bridge_retained void *)[self iterator];
    }
    id<AAIIterator> iterator = (__bridge_transfer id<AAIIterator>)(void *)state->extra[0];
    NSUInteger i;
    for (i = 0; i < len && iterator != nil; i += 1) {
        id __unsafe_unretained value = [iterator first];
        buffer[i] = value;
        iterator = [iterator next];
    }
    if (i > 0) {
        state->extra[0] = (unsigned long)(__bridge_retained void *)iterator;
    }
    state->itemsPtr = buffer;
    return i;
}

@end


text:
package org.saiku.service.util.security.authentication;

import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import static org.junit.Assert.assertEquals;

public class FileBackedPasswordProviderTest
{
    private static final String PASSWORD_KEY = "password";

    @Test
    public void shouldReturnPasswordFromValidPasswordFile() throws Exception
    {
        File passwordFile = createPasswordFile(PASSWORD_KEY, "thePassword");
        PasswordProvider provider = new FileBackedPasswordProvider(passwordFile, PASSWORD_KEY);

        assertEquals(provider.getPassword(), "thePassword");
    }

    @Test
    public void shouldReturnEmptyPasswordFromValidPasswordFile() throws Exception
    {
        File passwordFile = createPasswordFile(PASSWORD_KEY, "");
        PasswordProvider provider = new FileBackedPasswordProvider(passwordFile, PASSWORD_KEY);

        assertEquals(provider.getPassword(), "");
    }

    @Test(expected = RuntimeException.class)
    public void shouldThrowExceptionIfFileCannotBeFound() throws Exception
    {
        File passwordFile = new File("unknownFile");

        new FileBackedPasswordProvider(passwordFile, PASSWORD_KEY);
    }

    @Test(expected = RuntimeException.class)
    public void shouldThrowExceptionIfPasswordKeyCannotBeFoundInFile() throws Exception
    {
        File passwordFile = createPasswordFile("notTheKey", "thePassword");
        PasswordProvider provider = new FileBackedPasswordProvider(passwordFile, PASSWORD_KEY);

        provider.getPassword();
    }

    private File createPasswordFile(String passwordKey, String password) throws IOException
    {
        File passwordFile = File.createTempFile("FileBackedPasswordProviderTest", ".properties");
        passwordFile.deleteOnExit();
        FileOutputStream fileOutputStream = new FileOutputStream(passwordFile);

        try
        {
            Properties properties = new Properties();
            properties.setProperty(passwordKey, password);
            properties.store(fileOutputStream, "test properties");
            return passwordFile;
        }
        finally
        {
            fileOutputStream.close();
        }
    }
}


text:
import vim
from sys import version_info
import re
from os.path import abspath, basename, dirname, expanduser, splitext, isfile, relpath, exists, join
from os import remove, mkdir
from shutil import move
from glob import glob

from .timestamps import timestamp
from .vim_interface import *
from .modelines import format_modeline
from .utils import U

def update():
    if not bool(int(vim.eval('exists("b:pad_modified")'))):
        return

    modified = bool(int(vim.eval("b:pad_modified")))
    can_rename = get_setting("rename_files", bool)
    if modified and can_rename:
        _id = PadInfo(vim.current.buffer).id
        old_path = expanduser(vim.current.buffer.name)

        # if the file already has an extension
        ext = splitext(old_path)[1]
        if ext != '' and ext != get_setting("default_file_extension"):
            return

        fs = filter(isfile, \
                glob(expanduser(join(dirname(vim.current.buffer.name), _id)) + "*"))
        if old_path not in fs:
            if fs == []:
                new_path = expanduser(join(get_save_dir(), _id))
            else:
                exts = map(lambda i: '0' if i == '' else i[1:], \
                        map(lambda i: splitext(i)[1], fs))
                new_path = ".".join([
                                    expanduser(join(get_save_dir(), _id)),
                                    str(int(max(exts)) + 1)])
            new_path = new_path + vim.eval("g:pad#default_file_extension")
            V + "bwipeout"
            move(old_path, new_path)

def delete():
    path = vim.current.buffer.name
    if exists(path):
        confirm = vim.eval('input("really delete? (y/n): ")')
        if confirm.lower() == "y":
            remove(path)
            V + "bdelete!"
            V + "redraw!"

def add_modeline():
    mode = vim.eval('input("filetype: ", "", "filetype")')
    if mode:
        args = [format_modeline(mode)]
        if get_setting('modeline_position') == 'top':
            args.append(0)
        vim.current.buffer.append(*args)
        V + ("set filetype=" + mode)
        V + "set nomodified"

def move_to_folder(path=None):
    if path is None:
        path = vim.eval("input('move to: ')")
    new_path = join(get_save_dir(), path, basename(vim.current.buffer.name))
    if not exists(join(get_save_dir(), path)):
        mkdir(join(get_save_dir(), path))
    move(vim.current.buffer.name, new_path)
    V + "bdelete"

def move_to_savedir():
    move_to_folder("")

def archive():
    move_to_folder("archive")

def unarchive():
    move_to_savedir()

def isfileobject(o):
    if version_info.major == 2:
        return isinstance(o, file)
    elif version_info.major == 3:
        import io
        return isinstance(o, io.IOBase)

class PadInfo(object):
    __slots__ = "id", "summary", "body", "isEmpty", "folder"

    def __init__(self, source):
        """

        source can be:

        * a vim buffer
        * a file object
        * a list of strings, one per line
        """

        nchars = int(vim.eval("g:pad#read_nchars_from_files"))
        self.summary = ""
        self.body = ""
        self.isEmpty = True
        self.folder = ""
        self.id = timestamp()

        if source is vim.current.buffer:
            source = source[:10]
        elif isfileobject(source):
            save_dir = get_save_dir()
            if abspath(source.name).startswith(save_dir):
                pos = len(get_save_dir()), len(basename(source.name))
                self.folder = abspath(source.name)[pos[0]:-pos[1]]
            else:
                self.folder = dirname(relpath(source.name, vim.eval('getcwd()')))
            if vim.eval("g:pad#title_first_line") == '1':
                source = source.readline().split("\n")
            else:
                source = source.read(nchars).split('\n')

        data = [line.strip() for line in source if line != ""]

        if data != []:
            # we discard modelines
            if re.match("^.* vim: set .*:.*$", data[0]):
                data = data[1:]

            self.summary = data[0].strip()
            org_tags_data = None
            if len(self.summary) > 0:
                # vim-orgmode adds tags after whitespace
                org_tags_data = re.search("\s+(?P<tags>:.*$)", self.summary)
                if org_tags_data:
                    self.summary = re.sub("\s+:.*$", "", self.summary)
                if self.summary[0] in ("%", "#"):  # pandoc and markdown titles
                    self.summary = str(self.summary[1:]).strip()

            self.body = U(u'\u21b2').join(data[1:]).strip()
            # if we have orgmode tag data, add it to the body
            if org_tags_data:
                self.body = ' '.join(\
                    [" ".join(\
                              map(lambda a: "@" + a, \
                                  filter(lambda a: a != "", \
                                         org_tags_data.group("tags").split(":")))), \
                     self.body])
            # remove extra spaces in bodies
            self.body = re.sub("\s{2,}", "", str(self.body))

        if self.summary != "":
            self.isEmpty = False
            self.id = self.summary.lower().replace(" ", "_")
            # remove ilegal characters from names (using rules for windows
            # systems to err on the side of precaution)
            self.id = re.sub("[*:<>/\|^]", "", self.id)

        if self.id.startswith("."):
            self.id = re.sub("^\.*", "", self.id)


text:
<?php

class configureProjectDNSTask extends sfBaseTask
{
  protected function configure()
  {
    $this->addArguments(array(
      new sfCommandArgument('domain', sfCommandArgument::REQUIRED, 'The domain name to configure'),
      new sfCommandArgument('username', sfCommandArgument::OPTIONAL, 'The username to configure'),
      new sfCommandArgument('password', sfCommandArgument::OPTIONAL, 'and the password'),
    ));

    $this->addOptions(array(
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'all'),
    ));

    $this->namespace        = 'configure';
    $this->name             = 'dns-settings';
    $this->briefDescription = 'Configures project DNS settings';
    $this->detailedDescription = <<<EOF
The [dns|INFO] task configures the username & password for the DNS provider.

Call this task with:

  [php symfony configure:dns-settings domain-name|INFO]

or including the username and password:

  [php symfony configure:dns-settings domain-name username password|INFO]

and you can additionally provide the environment it should apply to:

  [php symfony configure:dns-settings --env=dev domain-name username password|INFO]
EOF;
  }

  protected function saveConfig($config = array()) {
    $configFilename = sfConfig::get('sf_config_dir').'/app.yml';

    file_put_contents($configFilename, sfYaml::dump($config, 4));
  }

  protected function loadConfig() {

    $configFilename = sfConfig::get('sf_config_dir').'/app.yml';
    $config = file_exists($configFilename) ? sfYaml::load($configFilename) : array();

    return $config;
  }

  protected function execute($arguments = array(), $options = array())
  {
    $config = $this->loadConfig();
    $config[$options['env']]['dns']['domain'] = $arguments['domain'];

    if (isset($arguments['username'])) {
      if (!isset($arguments['password'])) {
        throw new sfCommandException('Password must also be provided');
      }
      $config[$options['env']]['dns']['username'] = $arguments['username'];
      $config[$options['env']]['dns']['password'] = $arguments['password'];
    }

    $this->saveConfig($config);
  }
}


text:
     NAME
	  glXCreateGLXPixmap - create an off-screen GLX	rendering area

     C SPECIFICATION
	  GLXPixmap glXCreateGLXPixmap(	Display	*dpy,
					XVisualInfo *vis,
					Pixmap pixmap )

     PARAMETERS
	  dpy	  Specifies the	connection to the X server.

	  vis	  Specifies the	visual that defines the	structure of
		  the rendering	area.  It is a pointer to an
		  XVisualInfo structure, not a visual ID or a pointer
		  to a Visual.

	  pixmap  Specifies the	X pixmap that will be used as the
		  front	left color buffer of the off-screen rendering
		  area.

     DESCRIPTION
	  glXCreateGLXPixmap creates an	off-screen rendering area and
	  returns its XID.  Any	GLX rendering context that was created
	  with respect to vis can be used to render into this off-
	  screen area.	Use glXMakeCurrent to associate	the rendering
	  area with a GLX rendering context.

	  The X	pixmap identified by pixmap is used as the front left
	  buffer of the	resulting off-screen rendering area.  All
	  other	buffers	specified by vis, including color buffers
	  other	than the front left buffer, are	created	without
	  externally visible names.  GLX pixmaps with double-buffering
	  are supported.  However, glXSwapBuffers is ignored by	these
	  pixmaps.

	  Some implementations may not support GLX pixmaps with	direct
	  rendering contexts.

     NOTES
	  XVisualInfo is defined in Xutil.h.  It is a structure	that
	  includes visual, visualID, screen, and depth elements.

     ERRORS
	  BadMatch is generated	if the depth of	pixmap does not	match
	  the depth value reported by core X11 for vis,	or if pixmap
	  was not created with respect to the same screen as vis.

	  BadValue is generated	if vis is not a	valid XVisualInfo
	  pointer (for example,	if a particular	GLX implementation
	  does not support this	visual).
	  BadPixmap is generated if pixmap is not a valid pixmap.

	  BadAlloc is generated	if the server cannot allocate the GLX
	  pixmap.

     SEE ALSO
	  glXCreateContext, glXIsDirect, glXMakeCurrent


text:
package com.yy.cloud.common.data.assess;

import java.io.Serializable;
import java.sql.Timestamp;

import lombok.Data;

/**
 * ClassName:AssessTemplateMapItem

 * Function: TODO ADD FUNCTION.

 * Reason: TODO ADD REASON.

 * Date: Sep 27, 2017 8:51:19 PM

 *
 * @author chenxj
 * @version
 * @since JDK 1.8
 * @see
 */
@Data
public class AssessTemplateMapItem implements Serializable {
	/**
	 * serialVersionUID:TODO Description.
	 */
	private static final long serialVersionUID = -1669747057102031658L;
	private String id;
	private Timestamp createDate;
	private Byte status;
	private String templateId;
	private Timestamp updateDate;
}


text:
package excel;

import beans.Incidencia;
import chart.ChartView;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author marc.gareta
 */
@ManagedBean(name = "paginatorView", eager = true)
@ViewScoped
public class PaginatorView implements Serializable {

    private List<Incidencia> incidencias;

    @ManagedProperty(value = "#{incidenciaService}")
    private IncidenciaService incidenciaService;

    @PostConstruct
    public void init() {
        incidencias = incidenciaService.loadIncidencias();
        ChartView.maxAxisX = incidencias.size();
    }

    public List<Incidencia> getIncidencias() {
        return incidencias;
    }

    public IncidenciaService getIncidenciaService() {
        return incidenciaService;
    }

    public void setIncidenciaService(IncidenciaService service) {
        this.incidenciaService = service;
    }
}


text:
var _ = require('substance/util/helpers');
var pluck = require('substance/util/pluck');
var oo = require('substance/util/oo');

function Collection(doc, containerId, itemType, labelPrefix) {
  this.doc = doc;
  this.containerId = containerId;
  this.itemType = itemType;
  this.labelPrefix = labelPrefix;
  this.doc.connect(this, {
    'document:changed': this.onDocumentChanged
  });
}

Collection.Prototype = function() {

  this.dispose = function() {
    this.doc.disconnect(this);
  };

  this.getDocument = function() {
    return this.doc;
  };

  // Determines item order by checking their occurence in the container
  this.determineItems = function() {
    var doc = this.doc;
    var container = doc.get(this.containerId);
    var items = doc.getIndex('type').get(this.itemType);
    // Map itemIds (figures/tables) to container positions
    var _items = [];
    _.each(items, function(item) {
      var pos = container.getPosition(item.id);
      var isShown;
      if (pos >= 0) {
        isShown = true;
      } else {
        isShown = false;
        pos = Number.MAX_VALUE;
      }
      _items.push({
        pos: pos,
        item: item,
        isShown: isShown
      });
    });
    _items = _.sortBy(_items, 'pos');
    var counter = 0;
    _.each(_items, function(_item) {
      if (_item.isShown) {
        _item.index = counter++;
      } else {
        _item.index = -1;
      }
    });
    // console.log('Sorted Items', sortedItems);
    return _items;
  };

  this.createItemLabel = function(_item) {
    if (_item.index < 0) {
      return this.labelPrefix + " ?";
    } else {
      return [this.labelPrefix, _item.index + 1].join(' ');
    }
  };

  this.createCitationLabel = function(_citation) {
    var citation = _citation.citation;
    var targets = citation.targets;
    var targetPositions = [];
    var doc = this.doc;
    var err;
    // find the positions of the cited items
    _.each(targets, function(targetId) {
      var target = doc.get(targetId);
      var itemIndex = this.items.indexOf(target);
      if (itemIndex < 0) {
        console.error("citation target not found: ", targetId);
        err = targetId;
        return;
      }
      var _item = this._items[itemIndex];
      if(_item.index<0) {
        console.error("citation target does not have a label: ", targetId);
        err = targetId;
        return;
      }
      var targetPos = _item.index + 1;
      targetPositions.push(targetPos);
    }.bind(this));
    targetPositions.sort();
    // generate a label by concatenating numbers
    // and provide a special label for empty citations.
    var label;
    if (targetPositions.length === 0) {
      label = this.labelPrefix+ " ???";
    } else {
      label = [this.labelPrefix, targetPositions.join(", ")].join(' ');
    }
    // console.log('generated citation label', label);
    return label;
  };

  this.updateItemLabels = function() {
    _.each(this._items, function(_item) {
      var label = this.createItemLabel(_item);
      // console.log('generated item label', label);
      _item.item.setLabel(label);
    }.bind(this));
  };

  this.updateCitationLabels = function() {
    // console.log('citations', this.citations);
    _.each(this._citations, function(_citation) {
      var label = this.createCitationLabel(_citation);
      _citation.citation.setLabel(label);
    }.bind(this));
  };

  // get citation nodes sorted by occurence in container.
  this.determineCitations = function() {
    var doc = this.doc;
    var citations = doc.getIndex('type').get(this.itemType+'-citation');
    var container = doc.get(this.containerId);
    // generate information for sorting
    var _citations = _.map(citations, function(citation) {
      var address = container.getAddress(citation.path);
      return {
        citation: citation,
        address: address
      };
    });
    // sort citation by occurrence in the container
    _citations.sort(function(a, b) {
      if (a < b) {
        return -1;
      } else if (a > b) {
        return 1;
      } else {
        return a.citation.startOffset - b.citation.startOffset;
      }
    });
    return _citations;
  };

  this.update = function() {
    // items are augmented with information necessary for compilation
    // and are sorted to reflect the order they should appear in the collection
    // ATTENTION: there is a convention here to use `_item` for
    // an augmented item and `item` for the document node.
    this._items = this.determineItems();
    // Note: doing this right away allows us to easily find the
    // position of an item
    this.items = pluck(this._items, 'item');

    this._citations = this.determineCitations();
    this.citations = pluck(this._citations, 'citation');

    // compile labels
    this.updateItemLabels();
    this.updateCitationLabels();
  };

  this.getItems = function() {
    return this.items;
  };

  // HACK: Lots of hard coded things here, we need to improve this along with
  // removing the redudancy with Bibliography.js
  this.onDocumentChanged = function(change) {
    var doc = this.doc;
    var needsUpdate = false;
    var node, deletedNode;

    _.each(change.ops, function(op) {

      // Figure citation has been created/changed/delete
      // -----------------
      //

      if (op.isCreate() || op.isSet() || op.isUpdate()) {
        var nodeId = op.path[0];
        node = doc.get(nodeId);
        if (!node) return;

        if (op.isCreate()) {
          // Create
          if (node.type === 'image-figure-citation' || node.type === 'table-figure-citation') {
            needsUpdate = true;
          }
        } else {
          // Update/Set
          if (node.type === 'image-figure-citation' || node.type === 'table-figure-citation') {
            if (op.path[1] === 'targets') {
              needsUpdate = true;
            }
          }
        }
      } else if (op.isDelete()) {
        // Delete
        deletedNode = op.val;
        if (deletedNode.type === 'image-figure-citation' || deletedNode.type === 'table-figure-citation') {
          needsUpdate = true;
        }
      }

      // New Figure has been inserted/moved or deleted
      // ----------------
      //
      // Figure insert or move case (when container is updated)
      if (!needsUpdate && op.path[0] === this.containerId) {
        if (op.type === "set") {
          needsUpdate = true;
        }
        // Note: updates on the container nodes are always ArrayOperations
        // which have the inserted or removed value as `val`.
        if (op.type === "update") {
          var id = op.diff.val;
          node = doc.get(id);
          // ATTENTION: as these are intermediate ops
          // it may happen that the node itself has been
          // deleted by a later op in this change
          // So this guard can be considered ok
          if (!node) return;
          // look for item type or an include pointing to an item type
          if (node.type === this.itemType) {
            needsUpdate = true;
          }
        }
      }

      // When node of this.itemType has been deleted
      if (op.isDelete()) {
        deletedNode = op.val;
        if (deletedNode.type === this.itemType) {
          needsUpdate = true;
        }
      }
    }.bind(this));

    if (needsUpdate) {
      // console.log('Collection', this.itemType, 'is being updated');
      this.update();
    }
  };

};

oo.initClass(Collection);
module.exports = Collection;


text:
package org.elasticsearch.search.aggregations.bucket.nested;

import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.aggregations.bucket.ParsedSingleBucketAggregation;

import java.io.IOException;

public class ParsedReverseNested extends ParsedSingleBucketAggregation implements ReverseNested {

    @Override
    public String getType() {
        return ReverseNestedAggregationBuilder.NAME;
    }

    public static ParsedReverseNested fromXContent(XContentParser parser, final String name) throws IOException {
        return parseXContent(parser, new ParsedReverseNested(), name);
    }
}


text:
!!! tip ""

    The organization description supports [full Markdown syntax][markdown-syntax],
    including **bold**/*italic*, links, lists, and more.

{start_tabs}

{settings_tab|organization-profile}

1. Edit your organization **name**, **type**, **description**, and
**profile picture**.

1. *(optional)* Click **Preview organization profile** to see a preview of your
   organization's login page in a new browser tab.

{!save-changes.md!}

{end_tabs}

[markdown-syntax]: /help/format-your-message-using-markdown


text:
package com.thoughtworks.go.apiv1.configrepooperations;

import com.thoughtworks.go.api.ApiController;
import com.thoughtworks.go.api.ApiVersion;
import com.thoughtworks.go.api.spring.ApiAuthenticationHelper;
import com.thoughtworks.go.api.util.HaltApiResponses;
import com.thoughtworks.go.apiv1.configrepooperations.representers.PreflightResultRepresenter;
import com.thoughtworks.go.config.*;
import com.thoughtworks.go.config.exceptions.EntityType;
import com.thoughtworks.go.config.exceptions.GoConfigInvalidException;
import com.thoughtworks.go.config.exceptions.RecordNotFoundException;
import com.thoughtworks.go.config.materials.git.GitMaterialConfig;
import com.thoughtworks.go.config.remote.ConfigOrigin;
import com.thoughtworks.go.config.remote.ConfigRepoConfig;
import com.thoughtworks.go.config.remote.EphemeralConfigOrigin;
import com.thoughtworks.go.config.remote.PartialConfig;
import com.thoughtworks.go.domain.config.Configuration;
import com.thoughtworks.go.domain.materials.MaterialConfig;
import com.thoughtworks.go.plugin.access.configrepo.InvalidPartialConfigException;
import com.thoughtworks.go.server.service.ConfigRepoService;
import com.thoughtworks.go.server.service.GoConfigService;
import com.thoughtworks.go.server.util.UuidGenerator;
import com.thoughtworks.go.spark.spring.SparkSpringController;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import spark.Request;
import spark.Response;
import spark.utils.StringUtils;

import javax.servlet.http.Part;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import static com.thoughtworks.go.spark.Routes.ConfigRepos.OPERATIONS_BASE;
import static com.thoughtworks.go.spark.Routes.ConfigRepos.PREFLIGHT_PATH;
import static java.lang.String.format;
import static spark.Spark.*;

@Component
public class ConfigRepoOperationsControllerV1 extends ApiController implements SparkSpringController {
    private static final UuidGenerator UUID = new UuidGenerator();

    private final ApiAuthenticationHelper authenticationHelper;
    private final GoConfigPluginService pluginService;
    private final ConfigRepoService service;
    private final GoConfigService gcs;
    private final PartialConfigService partialConfigService;

    @Autowired
    public ConfigRepoOperationsControllerV1(ApiAuthenticationHelper authenticationHelper, GoConfigPluginService pluginService, ConfigRepoService service, GoConfigService gcs, PartialConfigService partialConfigService) {
        super(ApiVersion.v1);
        this.authenticationHelper = authenticationHelper;
        this.pluginService = pluginService;
        this.service = service;
        this.gcs = gcs;
        this.partialConfigService = partialConfigService;
    }

    @Override
    public String controllerBasePath() {
        return OPERATIONS_BASE;
    }

    @Override
    public void setupRoutes() {
        path(controllerBasePath(), () -> {
            before("", mimeType, this::setContentType);
            before("", mimeType, authenticationHelper::checkAdminUserAnd403);
            before("", mimeType, this::verifyContentType);

            before("/*", mimeType, this::setContentType);
            before("/*", mimeType, authenticationHelper::checkAdminUserAnd403);
            before(PREFLIGHT_PATH, mimeType, this::setMultipartUpload);

            post(PREFLIGHT_PATH, mimeType, this::preflight);
        });
    }

    String preflight(Request req, Response res) throws IOException {
        ConfigRepoConfig repo = repoFromRequest(req);
        ConfigRepoPlugin plugin = pluginFromRequest(req);
        PartialConfigLoadContext context = configContext(repo);

        final PreflightResult result = new PreflightResult();

        try {
            Collection<Part> uploads = req.raw().getParts();
            Map<String, String> contents = new LinkedHashMap<>();

            for (Part ul : uploads) {
                if (!"files[]".equals(ul.getName())) {
                    continue;
                }

                StringWriter w = new StringWriter();
                IOUtils.copy(ul.getInputStream(), w, StandardCharsets.UTF_8);
                contents.put(ul.getSubmittedFileName(), w.toString());
            }

            if (contents.isEmpty()) {
                result.update(Collections.singletonList("No file content provided; check to make sure you POST the form data as `files[]=`"), false);
            } else {
                PartialConfig partialConfig = plugin.parseContent(contents, context);
                partialConfig.setOrigins(adHocConfigOrigin(repo));
                CruiseConfig config = partialConfigService.merge(partialConfig, context.configMaterial().getFingerprint(), gcs.clonedConfigForEdit());

                gcs.validateCruiseConfig(config);
                result.update(Collections.emptyList(), true);
            }
        } catch (RecordNotFoundException e) {
            throw e;
        } catch (InvalidPartialConfigException e) {
            result.update(Collections.singletonList(e.getErrors()), false);
        } catch (GoConfigInvalidException e) {
            result.update(e.getAllErrors(), false);
        } catch (Exception e) {
            throw HaltApiResponses.haltBecauseOfReason(e.getMessage());
        }

        return writerForTopLevelObject(req, res, w -> PreflightResultRepresenter.toJSON(w, result));
    }

    private ConfigOrigin adHocConfigOrigin(ConfigRepoConfig repo) {
        return new EphemeralConfigOrigin(format("preflighted <%s>", null == repo ? "NEW REPO" : repo.getId()));
    }

    private PartialConfigLoadContext configContext(ConfigRepoConfig repo) {
        return new PartialConfigLoadContext() {
            @Override
            public Configuration configuration() {
                return null; // don't allow parse-content to view configurations as they may expose secrets
            }

            /**
             * This is primarily used for two things:
             *
             * 1) When a pipeline definition defines its material type as `configrepo`, this
             *    provides the material configuration used by the {@link ConfigRepoConfig}
             *    itself; this means that the pipeline definition is on the same repo as the
             *    pipeline build material. For preflight, if no `repoId` is provided, we return
             *    a dummy material; this would be the case when someone is preflighting a definition
             *    in a new config repo that has not been created yet.
             *
             * 2) We use the fingerprint of the material here to match against an existing partial
             *    when merging the candidate {@link PartialConfig} into a {@link CruiseConfig}. For
             *    an existing {@link ConfigRepoConfig}, this will allow us to replace the current
             *    partial with the preflight partial so we can perform validations.
             *
             * @return a {@link MaterialConfig}
             */
            @Override
            public MaterialConfig configMaterial() {
                return null == repo ? dummyMaterial() : repo.getRepo();
            }

            private MaterialConfig dummyMaterial() {
                GitMaterialConfig gitMaterialConfig = new GitMaterialConfig();
                gitMaterialConfig.setUrl(UUID.randomUuid());
                return gitMaterialConfig;
            }
        };
    }

    private ConfigRepoPlugin pluginFromRequest(Request req) {
        String pluginId = req.queryParams("pluginId");
        if (StringUtils.isBlank(pluginId)) {
            throw HaltApiResponses.haltBecauseRequiredParamMissing("pluginId");
        }
        return (ConfigRepoPlugin) pluginService.partialConfigProviderFor(pluginId);
    }

    private ConfigRepoConfig repoFromRequest(Request req) {
        String repoId = req.queryParams("repoId");
        if (null == repoId) {
            return null;
        }

        ConfigRepoConfig repo = service.getConfigRepo(repoId);

        if (null == repo) {
            throw new RecordNotFoundException(EntityType.ConfigRepo, repoId);
        }

        return repo;
    }
}


text:
<!DOCTYPE html>

	<meta charset="utf-8">
	<title>Bountify | John Murch</title>
	<meta name="description" content="I have built a ton of various scrapers, parsers and scripts in the past to get at all kinds of data on the web. I have mostly used PHP, Ruby and more recentl...">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="X-Frame-Options" content="sameorigin">

	<link rel="stylesheet" href="/css/main.css">

	<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">

	<link rel="canonical" href="http://example.com/2015/08/04/bountify.html">

	<link rel="alternate" type="application/atom+xml" title="John Murch" href="http://example.com/feed.xml" />

	<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet">

	<link href="//fonts.googleapis.com/css?family=Source+Sans+Pro:400,700,700italic,400italic" rel="stylesheet" type="text/css">

			John Murch

					About

Bountify

    August 4, 2015

I have built a ton of various scrapers, parsers and scripts in the past to get at all kinds of data on the web. I have mostly used PHP, Ruby and more recently Node.js (JavaScript).

When I saw a post on Bountify for a Scraper for Seeking Alpha I was curious. I wasn't familiar with Seeking Alpha, but had a free night where my wife was at an event and I was at home watching the dog, so I figured why not give it a try.

After taking Seeking Alpha for a spin I knew I could parse the profiles, but scraping using their search was going to be more work than the bounty offered. Figuring that there was only going to be a couple of developers posting the answer if any, I figured why not do what I can in as little of time as possible.

I simplified the code below but you can download the gist

var fs = require('fs'),
    request = require('request'),
    async = require('async'),
    _ = require('underscore'),
    cheerio = require('cheerio');

var outputCSV = [];
//******************************************************
// EACH CATGORY URL (Top 100)
// http://seekingalpha.com/leader-board/opinion-leaders
//******************************************************
var urls = [
"http://seekingalpha.com/opinion-leaders/long-ideas",
"http://seekingalpha.com/opinion-leaders/long-ideas/2",
"http://seekingalpha.com/opinion-leaders/long-ideas/3",
...
"http://seekingalpha.com/opinion-leaders/mutual-funds/3",
"http://seekingalpha.com/opinion-leaders/mutual-funds/4"
]

var authors = [];
async.forEach(urls,function(url,urlcb){
    request(url, function(error, response, body) {
      if (!error && response.statusCode == 200) {
        var $ = cheerio.load(body);
        ////*[@id="author_articles_wrapper"]/div/div[1]/ul
        $('.ld_top_list li a').each(function(i, elm) {
          authors.push($(this).attr('href'));
        });
        $('.ld_more_list li a').each(function(i, elm) {
          authors.push($(this).attr('href'));
        });
        console.log("FETCH: "+ url);
        urlcb();
      }else{
        console.log("Check Connection: Issue viewing: "+ url);
        console.log(error);
        process.exit(0);
      }
    });
},function(){
    var uniqAuthors = _.uniq(authors);
    async.forEach(uniqAuthors,function(name,pcb){
      var url = 'http://seekingalpha.com' + name;
      request(url, function(error, response, body) {
          if (!error && response.statusCode == 200) {
            console.log("Fetching: "+url);
            var $ = cheerio.load(body);
            var author = $("#author_full_name").text();
            var profile = $('.profile_item_mini_text').text().toLowerCase();
            // console.log(name+"|"+profile);
            //Check if keyword is in profile - save as boolean true or false
            var boolHedgeFund = contains(profile, 'hedge fund');
            var boolFormer = contains(profile, 'former');
            var boolEx = contains(profile, 'ex');
            var boolPortfolioManager = contains(profile, 'portfolio manager');

            var count=0;
            var keywords =[];
            if(boolHedgeFund){count++;keywords.push('hedge fund')}
            if(boolFormer){count++;keywords.push('former')}
            if(boolEx){count++;keywords.push('ex')}
            if(boolPortfolioManager){count++;keywords.push('portfolio manager')}

            var capture = {
              author:author,
              url:url,
              keywords:keywords,
              // profile: profile,
              count:count
            }
            if(count>0){
              outputCSV.push(capture);
              pcb();
            }else{
              pcb();
            }
          }else{
            pcb();
          }
      });
    },function(){
      console.log("DONE");
      var wstream = fs.createWriteStream('results.csv',{'flags': 'w'});
      wstream.write("Author,URL,Count,Keywords\n");
      async.forEach(outputCSV,function(a,acb){
        wstream.write(
            JSON.stringify(a.author)
            +","+a.url+","
            +a.count+","
            +JSON.stringify(a.keywords.join())
            +"\n"
        );
      },function(){
        wstream.end();
        console.log("DONE");
        process.exit(0);
      });
    })
})

function contains(r,s){
  return r.indexOf(s) !== -1;
}


text:
module Type where

import qualified Syntax


text:
<?php namespace Anomaly\FilesModule\File;

use Anomaly\FilesModule\File\Contract\FileInterface;
use Anomaly\FilesModule\File\Contract\FileRepositoryInterface;
use Anomaly\FilesModule\Folder\Contract\FolderInterface;
use Anomaly\Streams\Platform\Model\EloquentModel;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\Factory;
use League\Flysystem\MountManager;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Class FileUploader
 *
 * @link          http://pyrocms.com/
 * @author        PyroCMS, Inc. <support@pyrocms.com>
 * @author        Ryan Thompson <ryan@pyrocms.com>
 */
class FileUploader
{

    /**
     * The file repository.
     *
     * @var FileRepositoryInterface
     */
    protected $files;

    /**
     * The config repository.
     *
     * @var Repository
     */
    protected $config;

    /**
     * The mount manager.
     *
     * @var MountManager
     */
    protected $manager;

    /**
     * The file rotator.
     *
     * @var FileRotator
     */
    protected $rotator;

    /**
     * The validation factory.
     *
     * @var Factory
     */
    protected $validator;

    /**
     * Create a new FileUploader instance.
     *
     * @param Factory                 $validator
     * @param MountManager            $manager
     * @param FileRotator             $rotator
     * @param Repository              $config
     * @param FileRepositoryInterface $files
     */
    public function __construct(
        Factory $validator,
        MountManager $manager,
        FileRotator $rotator,
        Repository $config,
        FileRepositoryInterface $files
    ) {
        $this->files     = $files;
        $this->config    = $config;
        $this->manager   = $manager;
        $this->rotator   = $rotator;
        $this->validator = $validator;
    }

    /**
     * Upload a file.
     *
     * @param  UploadedFile       $file
     * @param  FolderInterface    $folder
     * @return bool|FileInterface
     */
    public function upload(UploadedFile $file, FolderInterface $folder)
    {
        $rules = 'required';

        if ($allowed = $folder->getAllowedTypes()) {
            $rules .= '|mimes:' . implode(',', $allowed);
        }

        $validation = $this->validator->make(['file' => $file], ['file' => $rules]);

        if (!$validation->passes()) {
            throw new \Exception($validation->messages()->first(), 1);
        }

        $disk = $folder->getDisk();

        $file = $this->rotator->rotate($file);

        /* @var FileInterface|EloquentModel $entry */
        $entry = $this->manager->put(
            $disk->getSlug() . '://' . $folder->getSlug() . '/' . $file->getClientOriginalName(),
            file_get_contents($file->getRealPath())
        );

        if (in_array($entry->getExtension(), $this->config->get('anomaly.module.files::mimes.types.image'))) {

            $size       = filesize($file->getRealPath());
            $dimensions = getimagesize($file->getRealPath());

            $this->files->save(
                $entry
                    ->setAttribute('size', $size)
                    ->setAttribute('width', $dimensions[0])
                    ->setAttribute('height', $dimensions[1])
            );
        }

        return $entry;
    }
}


text:
#ifndef BOOST_COMPUTE_ALGORITHM_STABLE_PARTITION_HPP
#define BOOST_COMPUTE_ALGORITHM_STABLE_PARTITION_HPP

#include <boost/compute/system.hpp>
#include <boost/compute/context.hpp>
#include <boost/compute/functional.hpp>
#include <boost/compute/command_queue.hpp>
#include <boost/compute/algorithm/copy_if.hpp>
#include <boost/compute/container/vector.hpp>

namespace boost {
namespace compute {

///
/// \brief Partitioning algorithm
///
/// Partitions the elements in the range [\p first, \p last) according to
/// \p predicate. The order of the elements is preserved.
/// \return Iterator pointing to end of true values
///
/// \param first Iterator pointing to start of range
/// \param last Iterator pointing to end of range
/// \param predicate Unary predicate to be applied on each element
/// \param queue Queue on which to execute
///
/// \see is_partitioned() and partition()
///
template<class Iterator, class UnaryPredicate>
inline Iterator stable_partition(Iterator first,
                                 Iterator last,
                                 UnaryPredicate predicate,
                                 command_queue &queue = system::default_queue())
{
    typedef typename std::iterator_traits<Iterator>::value_type value_type;

    // make temporary copy of the input
    ::boost::compute::vector<value_type> tmp(first, last, queue);

    // copy true values
    Iterator last_true =
        ::boost::compute::copy_if(tmp.begin(),
                                  tmp.end(),
                                  first,
                                  predicate,
                                  queue);

    // copy false values
    Iterator last_false =
        ::boost::compute::copy_if(tmp.begin(),
                                  tmp.end(),
                                  last_true,
                                  not1(predicate),
                                  queue);

    // return iterator pointing to the last true value
    return last_true;
}

} // end compute namespace
} // end boost namespace

#endif // BOOST_COMPUTE_ALGORITHM_STABLE_PARTITION_HPP


text:
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
    <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
    <open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
        <group>
            <file>file:/C:/Users/Asfo/Documents/Quiubas/quiubas-java/src/com/quiubas/api/Sms.java</file>
            <file>file:/C:/Users/Asfo/Documents/Quiubas/quiubas-java/src/com/quiubas/api/Balance.java</file>
            <file>file:/C:/Users/Asfo/Documents/Quiubas/quiubas-java/src/com/quiubas/api/Quiubas.java</file>
            <file>file:/C:/Users/Asfo/Documents/Quiubas/quiubas-java/src/com/quiubas/api/Base.java</file>
            <file>file:/C:/Users/Asfo/Documents/Quiubas/quiubas-java/src/com/quiubas/api/TwoFactor.java</file>
            <file>file:/C:/Users/Asfo/Documents/Quiubas/quiubas-java/src/com/quiubas/api/Network.java</file>
        </group>
    </open-files>
</project-private>


text:
<?php

declare(strict_types=1);

namespace GeoJson\Exception;

use RuntimeException;

use function get_class;
use function get_debug_type;
use function gettype;
use function is_object;
use function sprintf;

class UnserializationException extends RuntimeException implements Exception
{
    /**
     * Creates an UnserializationException for a value with an invalid type.
     *
     * @param mixed $value
     */
    public static function invalidValue(string $context, $value, string $expectedType): self
    {
        return new self(sprintf(
            '%s expected value of type %s, %s given',
            $context,
            $expectedType,
            get_debug_type($value)
        ));
    }

    /**
     * Creates an UnserializationException for a property with an invalid type.
     *
     * @param mixed $value
     */
    public static function invalidProperty(string $context, string $property, $value, string $expectedType): self
    {
        return new self(sprintf(
            '%s expected "%s" property of type %s, %s given',
            $context,
            $property,
            $expectedType,
            is_object($value) ? get_class($value) : gettype($value)
        ));
    }

    /**
     * Creates an UnserializationException for a missing property.
     */
    public static function missingProperty(string $context, string $property, string $expectedType): self
    {
        return new self(sprintf(
            '%s expected "%s" property of type %s, none given',
            $context,
            $property,
            $expectedType
        ));
    }

    /**
     * Creates an UnserializationException for an unsupported "type" property.
     */
    public static function unsupportedType(string $context, string $value): self
    {
        return new self(sprintf('Invalid %s type "%s"', $context, $value));
    }
}