text:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

/// <reference path='services.ts' />

/// <reference path='compiler\pathUtils.ts' />
/// <reference path='compiler\precompile.ts' />

var debugObjectHost = (<any>this);

module ts {
    export interface ScriptSnapshotShim {
        // Get's a portion of the script snapshot specified by [start, end).
        getText(start: number, end: number): string;

        // Get's the length of this script snapshot.
        getLength(): number;

        // This call returns the JSON encoded array of the type:
        //  number[]
        getLineStartPositions(): string;

        // Returns a JSON encoded value of the type:
        //  { span: { start: number; length: number }; newLength: number }
        //
        // Or null value if there was no change.
        getTextChangeRangeSinceVersion(scriptVersion: number): string;
    }

    //
    // Public interface of the host of a language service shim instance.
    //
    export interface LanguageServiceShimHost extends Logger {
        getCompilationSettings(): string;

        // Returns a JSON encoded value of the type:
        // string[]
        getScriptFileNames(): string;
        getScriptVersion(fileName: string): number;
        getScriptIsOpen(fileName: string): boolean;
        getScriptSnapshot(fileName: string): ScriptSnapshotShim;
        getLocalizedDiagnosticMessages(): string;
        getCancellationToken(): CancellationToken;
    }

    //
    // Public interface of of a language service instance shim.
    //
    export interface ShimFactory {
        registerShim(shim: Shim): void;
        unregisterShim(shim: Shim): void;
    }

    export interface Shim {
        dispose(dummy: any): void;
    }

    export interface LanguageServiceShim extends Shim {
        languageService: LanguageService;

        dispose(dummy: any): void;

        refresh(throwOnError: boolean): void;

        cleanupSemanticCache(): void;

        getSyntacticDiagnostics(fileName: string): string;
        getSemanticDiagnostics(fileName: string): string;
        getCompilerOptionsDiagnostics(): string;

        getCompletionsAtPosition(fileName: string, position: number, isMemberCompletion: boolean): string;
        getCompletionEntryDetails(fileName: string, position: number, entryName: string): string;

        getTypeAtPosition(fileName: string, position: number): string;
        getNameOrDottedNameSpan(fileName: string, startPos: number, endPos: number): string;
        getBreakpointStatementAtPosition(fileName: string, position: number): string;
        getSignatureAtPosition(fileName: string, position: number): string;

        // Returns a JSON encoded value of the type:
        // { fileName: string; minChar: number; limChar: number; kind: string; name: string; containerKind: string; containerName: string }
        //
        // Or null value if no definition can be found.
        getDefinitionAtPosition(fileName: string, position: number): string;

        // Returns a JSON encoded value of the type:
        // { fileName: string; minChar: number; limChar: number; isWriteAccess: boolean }[]
        getReferencesAtPosition(fileName: string, position: number): string;

        // Returns a JSON encoded value of the type:
        // { fileName: string; minChar: number; limChar: number; isWriteAccess: boolean }[]
        getOccurrencesAtPosition(fileName: string, position: number): string;

        // Returns a JSON encoded value of the type:
        // { fileName: string; minChar: number; limChar: number; isWriteAccess: boolean }[]
        getImplementorsAtPosition(fileName: string, position: number): string;

        // Returns a JSON encoded value of the type:
        // { name: string; kind: string; kindModifiers: string; containerName: string; containerKind: string; matchKind: string; fileName: string; minChar: number; limChar: number; } [] = [];
        getNavigateToItems(searchValue: string): string;

        // Returns a JSON encoded value of the type:
        // { name: string; kind: string; kindModifiers: string; containerName: string; containerKind: string; matchKind: string; fileName: string; minChar: number; limChar: number; } [] = [];
        getScriptLexicalStructure(fileName: string): string;

        // Returns a JSON encoded value of the type:
        // { name: string; kind: string; kindModifiers: string; containerName: string; containerKind: string; matchKind: string; fileName: string; minChar: number; limChar: number; } [] = [];
        getOutliningRegions(fileName: string): string;

        getBraceMatchingAtPosition(fileName: string, position: number): string;
        getIndentationAtPosition(fileName: string, position: number, options: string/*Services.EditorOptions*/): string;

        getFormattingEditsForRange(fileName: string, minChar: number, limChar: number, options: string/*Services.FormatCodeOptions*/): string;
        getFormattingEditsForDocument(fileName: string, minChar: number, limChar: number, options: string/*Services.FormatCodeOptions*/): string;
        getFormattingEditsOnPaste(fileName: string, minChar: number, limChar: number, options: string/*Services.FormatCodeOptions*/): string;
        getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: string/*Services.FormatCodeOptions*/): string;

        getEmitOutput(fileName: string): string;
    }

    export interface ClassifierShim extends Shim {
        getClassificationsForLine(text: string, lexState: EndOfLineState): string;
    }

    export interface CoreServicesShim extends Shim {
        getPreProcessedFileInfo(fileName: string, sourceText: TypeScript.IScriptSnapshot): string;
        getDefaultCompilationSettings(): string;
    }

    /// TODO: delete this, it is only needed untill the VS interface is updated
    enum LanguageVersion {
        EcmaScript3 = 0,
        EcmaScript5 = 1,
    }

    enum ModuleGenTarget {
        Unspecified = 0,
        Synchronous = 1,
        Asynchronous = 2,
    }

    interface CompilationSettings {
        propagateEnumConstants?: boolean;
        removeComments?: boolean;
        watch?: boolean;
        noResolve?: boolean;
        allowAutomaticSemicolonInsertion?: boolean;
        noImplicitAny?: boolean;
        noLib?: boolean;
        codeGenTarget?: LanguageVersion;
        moduleGenTarget?: ModuleGenTarget;
        outFileOption?: string;
        outDirOption?: string;
        mapSourceFiles?: boolean;
        mapRoot?: string;
        sourceRoot?: string;
        generateDeclarationFiles?: boolean;
        useCaseSensitiveFileResolution?: boolean;
        gatherDiagnostics?: boolean;
        codepage?: number;
        emitBOM?: boolean;
    }

    function languageVersionToScriptTarget(languageVersion: LanguageVersion): ScriptTarget {
        if (typeof languageVersion === "undefined") return undefined;

        switch (languageVersion) {
            case LanguageVersion.EcmaScript3: return ScriptTarget.ES3;
            case LanguageVersion.EcmaScript5: return ScriptTarget.ES5;
            default: throw Error("unsuported LanguageVersion value: " + languageVersion);
        }
    }

    function moduleGenTargetToModuleKind(moduleGenTarget: ModuleGenTarget): ModuleKind {
        if (typeof moduleGenTarget === "undefined") return undefined;

        switch (moduleGenTarget) {
            case ModuleGenTarget.Asynchronous: return ModuleKind.AMD;
            case ModuleGenTarget.Synchronous: return ModuleKind.CommonJS;
            case ModuleGenTarget.Unspecified: return ModuleKind.None;
            default: throw Error("unsuported ModuleGenTarget value: " + moduleGenTarget);
        }
    }

    function scriptTargetTolanguageVersion(scriptTarget: ScriptTarget): LanguageVersion {
        if (typeof scriptTarget === "undefined") return undefined;

        switch (scriptTarget) {
            case ScriptTarget.ES3: return LanguageVersion.EcmaScript3;
            case ScriptTarget.ES5: return LanguageVersion.EcmaScript5;
            default: throw Error("unsuported ScriptTarget value: " + scriptTarget);
        }
    }

    function moduleKindToModuleGenTarget(moduleKind: ModuleKind): ModuleGenTarget {
        if (typeof moduleKind === "undefined") return undefined;

        switch (moduleKind) {
            case ModuleKind.AMD: return ModuleGenTarget.Asynchronous;
            case ModuleKind.CommonJS: return ModuleGenTarget.Synchronous;
            case ModuleKind.None: return ModuleGenTarget.Unspecified;
            default: throw Error("unsuported ModuleKind value: " + moduleKind);
        }
    }

    function compilationSettingsToCompilerOptions(settings: CompilationSettings): CompilerOptions {
        // TODO: we should not be converting, but use options all the way
        var options: CompilerOptions = {};
        //options.propagateEnumConstants = settings.propagateEnumConstants;
        options.removeComments = settings.removeComments;
        options.noResolve = settings.noResolve;
        options.noImplicitAny = settings.noImplicitAny;
        options.noLib = settings.noLib;
        options.target = languageVersionToScriptTarget(settings.codeGenTarget);
        options.module = moduleGenTargetToModuleKind(settings.moduleGenTarget);
        options.out = settings.outFileOption;
        options.outDir = settings.outDirOption;
        options.sourceMap = settings.mapSourceFiles;
        options.mapRoot = settings.mapRoot;
        options.sourceRoot = settings.sourceRoot;
        options.declaration = settings.generateDeclarationFiles;
        //options.useCaseSensitiveFileResolution = settings.useCaseSensitiveFileResolution;
        options.codepage = settings.codepage;
        options.emitBOM = settings.emitBOM;
        return options;
    }

    function compilerOptionsToCompilationSettings(options: CompilerOptions): CompilationSettings {
        var settings: CompilationSettings = {};
        //options.propagateEnumConstants = settings.propagateEnumConstants;
        settings.removeComments = options.removeComments;
        settings.noResolve = options.noResolve;
        settings.noImplicitAny = options.noImplicitAny;
        settings.noLib = options.noLib;
        settings.codeGenTarget = scriptTargetTolanguageVersion(options.target);
        settings.moduleGenTarget = moduleKindToModuleGenTarget(options.module);
        settings.outFileOption = options.out;
        settings.outDirOption = options.outDir;
        settings.mapSourceFiles = options.sourceMap;
        settings.mapRoot = options.mapRoot;
        settings.sourceRoot = options.sourceRoot;
        settings.generateDeclarationFiles = options.declaration;
        // settings.useCaseSensitiveFileResolution = options.useCaseSensitiveFileResolution;
        settings.codepage = options.codepage;
        settings.emitBOM = options.emitBOM;
        return settings;
    }

    function logInternalError(logger: Logger, err: Error) {
        logger.log("*INTERNAL ERROR* - Exception in typescript services: " + err.message);
    }

    class ScriptSnapshotShimAdapter implements TypeScript.IScriptSnapshot {
        private lineStartPositions: number[] = null;

        constructor(private scriptSnapshotShim: ScriptSnapshotShim) {
        }

        public getText(start: number, end: number): string {
            return this.scriptSnapshotShim.getText(start, end);
        }

        public getLength(): number {
            return this.scriptSnapshotShim.getLength();
        }

        public getLineStartPositions(): number[] {
            if (this.lineStartPositions == null) {
                this.lineStartPositions = JSON.parse(this.scriptSnapshotShim.getLineStartPositions());
            }

            return this.lineStartPositions;
        }

        public getTextChangeRangeSinceVersion(scriptVersion: number): TypeScript.TextChangeRange {
            var encoded = this.scriptSnapshotShim.getTextChangeRangeSinceVersion(scriptVersion);
            if (encoded == null) {
                return null;
            }

            var decoded: { span: { start: number; length: number; }; newLength: number; } = JSON.parse(encoded);
            return new TypeScript.TextChangeRange(
                new TypeScript.TextSpan(decoded.span.start, decoded.span.length), decoded.newLength);
        }
    }

    class LanguageServiceShimHostAdapter implements LanguageServiceHost {
        constructor(private shimHost: LanguageServiceShimHost) {
        }

        public information(): boolean {
            return this.shimHost.information();
        }

        public debug(): boolean {
            return this.shimHost.debug();
        }

        public warning(): boolean {
            return this.shimHost.warning();
        }

        public error(): boolean {
            return this.shimHost.error();
        }

        public fatal(): boolean {
            return this.shimHost.fatal();
        }

        public log(s: string): void {
            this.shimHost.log(s);
        }

        public getCompilationSettings(): CompilerOptions {
            var settingsJson = this.shimHost.getCompilationSettings();
            if (settingsJson == null || settingsJson == "") {
                throw Error("LanguageServiceShimHostAdapter.getCompilationSettings: empty compilationSettings");
                return null;
            }
            var options = compilationSettingsToCompilerOptions(<CompilerOptions>JSON.parse(<any>settingsJson));

            /// TODO: this should be pushed into VS.
            /// We can not ask the LS instance to resolve, as this will lead to asking the host about files it does not know about,
            /// something it is not desinged to handle. for now make sure we never get a "noresolve == false".
            /// This value should not matter, as the host runs resolution logic independentlly
            options.noResolve = true;

            return options;
        }

        public getScriptFileNames(): string[] {
            var encoded = this.shimHost.getScriptFileNames();
            return JSON.parse(encoded);
        }

        public getScriptSnapshot(fileName: string): TypeScript.IScriptSnapshot {
            return new ScriptSnapshotShimAdapter(this.shimHost.getScriptSnapshot(fileName));
        }

        public getScriptVersion(fileName: string): number {
            return this.shimHost.getScriptVersion(fileName);
        }

        public getScriptIsOpen(fileName: string): boolean {
            return this.shimHost.getScriptIsOpen(fileName);
        }

        public getLocalizedDiagnosticMessages(): any {
            var diagnosticMessagesJson = this.shimHost.getLocalizedDiagnosticMessages();
            if (diagnosticMessagesJson == null || diagnosticMessagesJson == "") {
                return null;
            }
            try {
                return JSON.parse(diagnosticMessagesJson);
            }
            catch (e) {
                this.log(e.description || "diagnosticMessages.generated.json has invalid JSON format");
                return null;
            }
        }

        public getCancellationToken(): CancellationToken {
            return this.shimHost.getCancellationToken();
        }
    }

    function simpleForwardCall(logger: Logger, actionDescription: string, action: () => any): any {
        logger.log(actionDescription);
        var start = Date.now();
        var result = action();
        var end = Date.now();
        logger.log(actionDescription + " completed in " + (end - start) + " msec");
        if (typeof (result) === "string") {
            var str = <string>result;
            if (str.length > 128) {
                str = str.substring(0, 128) + "...";
            }
            logger.log("  result.length=" + str.length + ", result='" + JSON.stringify(str) + "'");
        }
        return result;
    }

    function forwardJSONCall(logger: Logger, actionDescription: string, action: () => any): string {
        try {
            var result = simpleForwardCall(logger, actionDescription, action);
            return JSON.stringify({ result: result });
        }
        catch (err) {
            if (err instanceof OperationCanceledException) {
                return JSON.stringify({ canceled: true });
            }
            logInternalError(logger, err);
            err.description = actionDescription;
            return JSON.stringify({ error: err });
        }
    }

    class ShimBase implements Shim {
        constructor(private factory: ShimFactory) {
            factory.registerShim(this);
        }
        public dispose(dummy: any): void {
            this.factory.unregisterShim(this);
        }
    }

    class LanguageServiceShimObject extends ShimBase implements LanguageServiceShim {
        private logger: Logger;

        constructor(factory: ShimFactory,
            private host: LanguageServiceShimHost,
            public languageService: LanguageService) {
            super(factory);
            this.logger = this.host;
        }

        public forwardJSONCall(actionDescription: string, action: () => any): string {
            return forwardJSONCall(this.logger, actionDescription, action);
        }

        // DISPOSE
        // Ensure (almost) determinstic release of internal Javascript resources when
        // some external native objects holds onto us (e.g. Com/Interop).
        public dispose(dummy: any): void {
            this.logger.log("dispose()");
            this.languageService.dispose();
            this.languageService = null;

            // force a GC
            if (debugObjectHost && debugObjectHost.CollectGarbage) {
                debugObjectHost.CollectGarbage();
                this.logger.log("CollectGarbage()");
            }

            this.logger = null;

            super.dispose(dummy);
        }

        // REFRESH
        // Update the list of scripts known to the compiler
        public refresh(throwOnError: boolean): void {
            this.forwardJSONCall(
                "refresh(" + throwOnError + ")",
                () => {
                    this.languageService.refresh();
                    return <any>null;
                });
        }

        public cleanupSemanticCache(): void {
            this.forwardJSONCall(
                "cleanupSemanticCache()",
                () => {
                    this.languageService.cleanupSemanticCache();
                    return <any>null;
                });
        }
        /// SQUIGGLES
        ///

        private static realizeDiagnostic(diagnostic: Diagnostic): { message: string; start: number; length: number; category: string; } {
            return {
                message: diagnostic.messageText,
                start: diagnostic.start,
                length: diagnostic.length,
                /// TODO: no need for the tolowerCase call
                category: DiagnosticCategory[diagnostic.category].toLowerCase()
            };
        }

        private realizeDiagnosticWithFileName(diagnostic: Diagnostic): { fileName: string; message: string; start: number; length: number; category: string; } {
            return {
                fileName: diagnostic.file.filename,
                message: diagnostic.messageText,
                start: diagnostic.start,
                length: diagnostic.length,
                /// TODO: no need for the tolowerCase call
                category: DiagnosticCategory[diagnostic.category].toLowerCase()
            };
        }

        public getSyntacticDiagnostics(fileName: string): string {
            return this.forwardJSONCall(
                "getSyntacticDiagnostics(\"" + fileName + "\")",
                () => {
                    var errors = this.languageService.getSyntacticDiagnostics(fileName);
                    return errors.map(LanguageServiceShimObject.realizeDiagnostic);
                });
        }

        public getSemanticDiagnostics(fileName: string): string {
            return this.forwardJSONCall(
                "getSemanticDiagnostics(\"" + fileName + "\")",
                () => {
                    var errors = this.languageService.getSemanticDiagnostics(fileName);
                    return errors.map(LanguageServiceShimObject.realizeDiagnostic);
                });
        }

        public getCompilerOptionsDiagnostics(): string {
            return this.forwardJSONCall(
                "getCompilerOptionsDiagnostics()",
                () => {
                    var errors = this.languageService.getCompilerOptionsDiagnostics();
                    return errors.map(d => this.realizeDiagnosticWithFileName(d))
                });
        }

        /// QUICKINFO
        /// Computes a string representation of the type at the requested position
        /// in the active file.
        public getTypeAtPosition(fileName: string, position: number): string {
            return this.forwardJSONCall(
                "getTypeAtPosition(\"" + fileName + "\", " + position + ")",
                () => {
                    var typeInfo = this.languageService.getTypeAtPosition(fileName, position);
                    return typeInfo;
                });
        }

        /// NAMEORDOTTEDNAMESPAN
        /// Computes span information of the name or dotted name at the requested position
        // in the active file.
        public getNameOrDottedNameSpan(fileName: string, startPos: number, endPos: number): string {
            return this.forwardJSONCall(
                "getNameOrDottedNameSpan(\"" + fileName + "\", " + startPos + ", " + endPos + ")",
                () => {
                    var spanInfo = this.languageService.getNameOrDottedNameSpan(fileName, startPos, endPos);
                    return spanInfo;
                });
        }

        /// STATEMENTSPAN
        /// Computes span information of statement at the requested position in the active file.
        public getBreakpointStatementAtPosition(fileName: string, position: number): string {
            return this.forwardJSONCall(
                "getBreakpointStatementAtPosition(\"" + fileName + "\", " + position + ")",
                () => {
                    var spanInfo = this.languageService.getBreakpointStatementAtPosition(fileName, position);
                    return spanInfo;
                });
        }

        /// SIGNATUREHELP
        /// Computes a string representation of the signatures at the requested position
        /// in the active file.
        public getSignatureAtPosition(fileName: string, position: number): string {
            return this.forwardJSONCall(
                "getSignatureAtPosition(\"" + fileName + "\", " + position + ")",
                () => {
                    var signatureInfo = this.languageService.getSignatureAtPosition(fileName, position);
                    return signatureInfo;
                });
        }

        /// GOTO DEFINITION
        /// Computes the definition location and file for the symbol
        /// at the requested position.
        public getDefinitionAtPosition(fileName: string, position: number): string {
            return this.forwardJSONCall(
                "getDefinitionAtPosition(\"" + fileName + "\", " + position + ")",
                () => {
                    return this.languageService.getDefinitionAtPosition(fileName, position);
                });
        }

        /// GET BRACE MATCHING
        public getBraceMatchingAtPosition(fileName: string, position: number): string {
            return this.forwardJSONCall(
                "getBraceMatchingAtPosition(\"" + fileName + "\", " + position + ")",
                () => {
                    var textRanges = this.languageService.getBraceMatchingAtPosition(fileName, position);
                    return textRanges;
                });
        }

        /// GET SMART INDENT
        public getIndentationAtPosition(fileName: string, position: number, options: string /*Services.EditorOptions*/): string {
            return this.forwardJSONCall(
                "getIndentationAtPosition(\"" + fileName + "\", " + position + ")",
                () => {
                    var localOptions: EditorOptions = JSON.parse(options);
                    var columnOffset = this.languageService.getIndentationAtPosition(fileName, position, localOptions);
                    return { value: columnOffset };
                });
        }

        /// GET REFERENCES
        ///  Return references to a symbol at the requested position.
        ///  References are separated by "\n".
        ///  Each reference is a "fileindex min lim" sub-string.
        public getReferencesAtPosition(fileName: string, position: number): string {
            return this.forwardJSONCall(
                "getReferencesAtPosition(\"" + fileName + "\", " + position + ")",
                () => {
                    return this.languageService.getReferencesAtPosition(fileName, position);
                });
        }

        public getOccurrencesAtPosition(fileName: string, position: number): string {
            return this.forwardJSONCall(
                "getOccurrencesAtPosition(\"" + fileName + "\", " + position + ")",
                () => {
                    return this.languageService.getOccurrencesAtPosition(fileName, position);
                });
        }

        /// GET IMPLEMENTORS
        public getImplementorsAtPosition(fileName: string, position: number): string {
            return this.forwardJSONCall(
                "getImplementorsAtPosition(\"" + fileName + "\", " + position + ")",
                () => {
                    return this.languageService.getImplementorsAtPosition(fileName, position);
                });
        }

        /// COMPLETION LISTS
        /// Get a string based representation of the completions
        /// to provide at the given source position and providing a member completion
        /// list if requested.
        public getCompletionsAtPosition(fileName: string, position: number, isMemberCompletion: boolean) {
            return this.forwardJSONCall(
                "getCompletionsAtPosition(\"" + fileName + "\", " + position + ", " + isMemberCompletion + ")",
                () => {
                    var completion = this.languageService.getCompletionsAtPosition(fileName, position, isMemberCompletion);
                    return completion;
                });
        }

        /// Get a string based representation of a completion list entry details
        public getCompletionEntryDetails(fileName: string, position: number, entryName: string) {
            return this.forwardJSONCall(
                "getCompletionEntryDetails(\"" + fileName + "\", " + position + ", " + entryName + ")",
                () => {
                    var details = this.languageService.getCompletionEntryDetails(fileName, position, entryName);
                    return details;
                });
        }

        /// FORMAT SELECTION
        public getFormattingEditsForRange(fileName: string, minChar: number, limChar: number, options: string/*Services.FormatCodeOptions*/): string {
            return this.forwardJSONCall(
                "getFormattingEditsForRange(\"" + fileName + "\", " + minChar + ", " + limChar + ")",
                () => {
                    var localOptions: FormatCodeOptions = JSON.parse(options);
                    var edits = this.languageService.getFormattingEditsForRange(fileName, minChar, limChar, localOptions);
                    return edits;
                });
        }

        /// FORMAT DOCUMENT
        public getFormattingEditsForDocument(fileName: string, minChar: number, limChar: number, options: string/*Services.FormatCodeOptions*/): string {
            return this.forwardJSONCall(
                "getFormattingEditsForDocument(\"" + fileName + "\", " + minChar + ", " + limChar + ")",
                () => {
                    var localOptions: FormatCodeOptions = JSON.parse(options);
                    var edits = this.languageService.getFormattingEditsForDocument(fileName, minChar, limChar, localOptions);
                    return edits;
                });
        }

        /// FORMAT ON PASTE
        public getFormattingEditsOnPaste(fileName: string, minChar: number, limChar: number, options: string/*Services.FormatCodeOptions*/): string {
            return this.forwardJSONCall(
                "getFormattingEditsOnPaste(\"" + fileName + "\", " + minChar + ", " + limChar + ")",
                () => {
                    var localOptions: FormatCodeOptions = JSON.parse(options);
                    var edits = this.languageService.getFormattingEditsOnPaste(fileName, minChar, limChar, localOptions);
                    return edits;
                });
        }

        /// FORMAT
        public getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: string/*Services.FormatCodeOptions*/): string {
            return this.forwardJSONCall(
                "getFormattingEditsAfterKeystroke(\"" + fileName + "\", " + position + ", \"" + key + "\")",
                () => {
                    var localOptions: FormatCodeOptions = JSON.parse(options);
                    var edits = this.languageService.getFormattingEditsAfterKeystroke(fileName, position, key, localOptions);
                    return edits;
                });
        }

        /// NAVIGATE TO
        ///  Return a list of symbols that are interesting to navigate to
        public getNavigateToItems(searchValue: string): string {
            return this.forwardJSONCall(
                "getNavigateToItems(\"" + searchValue + "\")",
                () => {
                    var items = this.languageService.getNavigateToItems(searchValue);
                    var result = this._navigateToItemsToString(items);
                    return result;
                });
        }

        // GET SCRIPT LEXICAL STRUCTURE
        //
        public getScriptLexicalStructure(fileName: string): string {
            return this.forwardJSONCall(
                "getScriptLexicalStructure(\"" + fileName + "\")",
                () => {
                    var items = this.languageService.getScriptLexicalStructure(fileName);
                    var result = this._navigateToItemsToString(items);
                    return result;
                });
        }

        // GET OUTLINING REGIONS
        //
        public getOutliningRegions(fileName: string): string {
            return this.forwardJSONCall(
                "getOutliningRegions(\"" + fileName + "\")",
                () => {
                    var items = this.languageService.getOutliningRegions(fileName);
                    // return just the part of data that language service v2 can understand
                    // language service v2 will use the entire OutliningSpan
                    var spans = map(items, i => i.textSpan);
                    return spans;
                });
        }

        /// Emit
        public getEmitOutput(fileName: string): string {
            return this.forwardJSONCall(
                "getEmitOutput(\"" + fileName + "\")",
                () => {
                    var output = this.languageService.getEmitOutput(fileName);
                    return output;
                });
        }

        private _navigateToItemsToString(items: NavigateToItem[]): any {
            var result: {
                name: string;
                kind: string;
                kindModifiers: string;
                containerName: string;
                containerKind: string;
                matchKind: string;
                fileName: string;
                minChar: number;
                limChar: number;
                additionalSpans?: { start: number; end: number; }[];
            }[] = [];

            for (var i = 0; i < items.length; i++) {
                var item = items[i];

                result.push({
                    name: item.name,
                    kind: item.kind,
                    kindModifiers: item.kindModifiers,
                    containerName: item.containerName,
                    containerKind: item.containerKind,
                    matchKind: item.matchKind,
                    fileName: item.fileName,
                    minChar: item.minChar,
                    limChar: item.limChar,
                    additionalSpans: item.additionalSpans ? item.additionalSpans.map(i => { return { start: i.minChar, end: i.limChar }; }) : undefined
                });
            }

            return result;
        }
    }

    class ClassifierShimObject extends ShimBase implements ClassifierShim {
        public classifier: Classifier;

        constructor(factory: ShimFactory, public host: Logger) {
            super(factory);
            this.classifier = createClassifier(this.host);
        }

        /// COLORIZATION
        public getClassificationsForLine(text: string, lexState: EndOfLineState): string {
            var classification = this.classifier.getClassificationsForLine(text, lexState);
            var items = classification.entries;
            var result = "";
            for (var i = 0; i < items.length; i++) {
                result += items[i].length + "\n";
                result += items[i].classification + "\n";
            }
            result += classification.finalLexState;
            return result;
        }
    }

    class CoreServicesShimObject extends ShimBase implements CoreServicesShim {
        constructor(factory: ShimFactory, public host: Logger) {
            super(factory);
        }

        private forwardJSONCall(actionDescription: string, action: () => any): any {
            return forwardJSONCall(this.host, actionDescription, action);
        }

        ///
        /// getPreProcessedFileInfo
        ///
        public getPreProcessedFileInfo(fileName: string, sourceText: TypeScript.IScriptSnapshot): string {
            return this.forwardJSONCall(
                "getPreProcessedFileInfo(\"" + fileName + "\")",
                () => {
                    var result = TypeScript.preProcessFile(fileName, sourceText);
                    return result;
                });
        }

        ///
        /// getDefaultCompilationSettings
        ///
        public getDefaultCompilationSettings(): string {
            return this.forwardJSONCall(
                "getDefaultCompilationSettings()",
                () => {
                    return compilerOptionsToCompilationSettings(getDefaultCompilerOptions());
                });
        }
    }

    export class TypeScriptServicesFactory implements ShimFactory {
        private _shims: Shim[] = [];
        private documentRegistry: DocumentRegistry = createDocumentRegistry();

        public createLanguageServiceShim(host: LanguageServiceShimHost): LanguageServiceShim {
            try {
                var hostAdapter = new LanguageServiceShimHostAdapter(host);
                var pullLanguageService = createLanguageService(hostAdapter, this.documentRegistry);
                return new LanguageServiceShimObject(this, host, pullLanguageService);
            }
            catch (err) {
                logInternalError(host, err);
                throw err;
            }
        }

        public createClassifierShim(host: Logger): ClassifierShim {
            try {
                return new ClassifierShimObject(this, host);
            }
            catch (err) {
                logInternalError(host, err);
                throw err;
            }
        }

        public createCoreServicesShim(host: Logger): CoreServicesShim {
            try {
                return new CoreServicesShimObject(this, host);
            }
            catch (err) {
                logInternalError(host, err);
                throw err;
            }
        }

        public close(): void {
            // Forget all the registered shims
            this._shims = [];
            this.documentRegistry = createDocumentRegistry();
        }

        public registerShim(shim: Shim): void {
            this._shims.push(shim);
        }

        public unregisterShim(shim: Shim): void {
            for (var i = 0, n = this._shims.length; i < n; i++) {
                if (this._shims[i] === shim) {
                    delete this._shims[i];
                    return;
                }
            }

            throw TypeScript.Errors.invalidOperation();
        }
    }
}

/// TODO: this is used by VS, clean this up on both sides of the interfrace
module TypeScript.Services {
    export var TypeScriptServicesFactory = ts.TypeScriptServicesFactory;
}


text:
using Google.Api.Ads.Common.Util;
using Google.Api.Ads.AdManager.v202208;

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Reflection;

namespace Google.Api.Ads.AdManager.Util.v202208
{
    ///
    /// A utility class for handling PQL objects.
    ///
    public class PqlUtilities
    {
        ///
        /// Gets the underlying value of the Value object. For SetValues, returns
        /// a List of underlying values.
        ///
        /// <value>The Value object to get the value from.</value>
        /// <returns>The underlying value, or List of underlying values from a SetValue.</returns>
        public static object GetValue(Value value)
        {
            if (value is SetValue)
            {
                PropertyInfo propInfo = value.GetType().GetProperty("values");
                if (propInfo != null)
                {
                    Value[] setValues = propInfo.GetValue(value, null) as Value[];
                    List<object> extractedValues = new List<object>();
                    foreach (Value setValue in setValues)
                    {
                        validateSetValueForSet(GetValue(setValue), extractedValues);
                        extractedValues.Add(GetValue(setValue));
                    }

                    return extractedValues;
                }
            }
            else
            {
                PropertyInfo propInfo = value.GetType().GetProperty("value");
                if (propInfo != null)
                {
                    return propInfo.GetValue(value, null);
                }
            }

            return null;
        }

        private static void validateSetValueForSet(object entry, IList<object> set)
        {
            if (entry is IList<object> || entry is SetValue)
            {
                throw new ArgumentException("Unsupported Value type [nested sets]");
            }

            if (set.Count > 0)
            {
                IEnumerator<Object> enumerator = set.GetEnumerator();
                enumerator.MoveNext();
                Object existingEntry = enumerator.Current;
                if (!existingEntry.GetType().IsAssignableFrom(entry.GetType()))
                {
                    throw new ArgumentException(String.Format(
                        "Unsupported Value type [SetValue with " + "mixed types {0} and {1}]",
                        existingEntry.GetType(), entry.GetType()));
                }
            }
        }

        ///
        /// Gets the result set as list of string arrays.
        ///
        /// <param name="resultSet">The result set to convert to a string array list.</param>
        /// <returns>A list of string arrays representing the result set.</returns>
        public static List<String[]> ResultSetToStringArrayList(ResultSet resultSet)
        {
            List<string[]> stringArrayList = new List<string[]>();
            stringArrayList.Add(GetColumnLabels(resultSet));
            if (resultSet.rows != null)
            {
                foreach (Row row in resultSet.rows)
                {
                    stringArrayList.Add(GetRowStringValues(row));
                }
            }

            return stringArrayList;
        }

        ///
        /// Gets the result set as a table represenation in the form of:
        ///
        ///

        /// +-------+-------+-------+
        /// |column1|column2|column3|
        /// +-------+-------+-------+
        /// |value1 |value2 |value3 |
        /// +-------+-------+-------+
        /// |value1 |value2 |value3 |
        /// +-------+-------+-------+
        ///

        ///
        /// <param name="resultSet">The result set to display as a string</param>
        /// <returns>The string represenation of result set as a table.</returns>
        public static String ResultSetToString(ResultSet resultSet)
        {
            StringBuilder resultSetStringBuilder = new StringBuilder();
            List<String[]> resultSetStringArrayList = ResultSetToStringArrayList(resultSet);
            List<int> maxColumnSizes = GetMaxColumnSizes(resultSetStringArrayList);
            string rowTemplate = CreateRowTemplate(maxColumnSizes);
            string rowSeparator = CreateRowSeperator(maxColumnSizes);

            resultSetStringBuilder.Append(rowSeparator);
            for (int i = 0; i < resultSetStringArrayList.Count; i++)
            {
                resultSetStringBuilder
                    .AppendFormat(rowTemplate, (object[]) resultSetStringArrayList[i])
                    .Append(rowSeparator);
            }

            return resultSetStringBuilder.ToString();
        }

        ///
        /// Creates the row template given the maximum size for each column.
        ///
        /// <param name="maxColumnSizes">The maximum size for each column</param>
        /// <returns>The row template to format row data into.</returns>
        private static string CreateRowTemplate(List<int> maxColumnSizes)
        {
            List<String> columnFormatSpecifiers = new List<string>();
            int i = 0;
            foreach (int maxColumnSize in maxColumnSizes)
            {
                columnFormatSpecifiers.Add(string.Format("{{{0},{1}}}", i, maxColumnSize));
                i++;
            }

            return new StringBuilder("| ")
                .Append(string.Join(" | ", columnFormatSpecifiers.ToArray())).Append(" |\n")
                .ToString();
        }

        ///
        /// Creates the row seperator given the maximum size for each column.
        ///
        /// <param name="maxColumnSizes">The maximum size for each column.</param>
        /// <returns>The row seperator.</returns>
        private static String CreateRowSeperator(List<int> maxColumnSizes)
        {
            StringBuilder rowSeperator = new StringBuilder("+");
            foreach (int maxColumnSize in maxColumnSizes)
            {
                for (int i = 0; i < maxColumnSize + 2; i++)
                {
                    rowSeperator.Append("-");
                }

                rowSeperator.Append("+");
            }

            return rowSeperator.Append("\n").ToString();
        }

        ///
        /// Gets a list of the maximum size for each column.
        ///
        /// <param name="resultSet">The result set to process.</param>
        /// <returns>A list of the maximum size for each column.</returns>
        private static List<int> GetMaxColumnSizes(List<string[]> resultSet)
        {
            List<int> maxColumnSizes = new List<int>();
            for (int i = 0; i < resultSet[i].Length; i++)
            {
                int maxColumnSize = -1;
                for (int j = 0; j < resultSet.Count; j++)
                {
                    if (resultSet[j][i].Length > maxColumnSize)
                    {
                        maxColumnSize = resultSet[j][i].Length;
                    }
                }

                maxColumnSizes.Add(maxColumnSize);
            }

            return maxColumnSizes;
        }

        ///
        /// Gets the column labels for the result set.
        ///
        /// <param name="resultSet">The result set to get the column labels for.
        /// </param>
        /// <returns>The string array of column labels.</returns>
        public static String[] GetColumnLabels(ResultSet resultSet)
        {
            List<string> columnLabels = new List<string>();
            foreach (ColumnType column in resultSet.columnTypes)
            {
                columnLabels.Add(column.labelName);
            }

            return columnLabels.ToArray();
        }

        ///
        /// Gets the row values for a row of the result set in the form of an object
        /// array.
        ///
        /// <param name="row">The row to get the values for.</param>
        /// <returns>The object array of the row values.</returns>
        public static object[] GetRowValues(Row row)
        {
            List<object> rowValues = new List<object>();
            foreach (Value value in row.values)
            {
                rowValues.Add(GetValue(value));
            }

            return rowValues.ToArray();
        }

        ///
        /// Gets the row values for a row of the result set in a the form of a string
        /// array. null values are interperted as empty strings.
        ///
        /// <param name="row">The row to get the values for.</param>
        /// <returns>The string array of the row values.</returns>
        public static String[] GetRowStringValues(Row row)
        {
            object[] rowValues = GetRowValues(row);
            List<string> rowStringValues = new List<string>();
            foreach (object obj in rowValues)
            {
                rowStringValues.Add(GetTextValue(obj));
            }

            return rowStringValues.ToArray();
        }

        ///
        /// Gets the text value of an unwrapped Value object.
        ///
        /// <param name="value">The unwrapped Value.</param>
        /// <returns>A formatted text representation of the value.</returns>
        /// <remarks>DateValue is formatted in yyyy-mm-dd format. DateTimeValue is
        /// formatted in yyyy-mm-dd HH:mm:ss Z format.</remarks>
        private static string GetTextValue(Object value)
        {
            if (value == null)
            {
                return "";
            }

            if (value is Google.Api.Ads.AdManager.v202208.Date)
            {
                Google.Api.Ads.AdManager.v202208.Date date =
                    (Google.Api.Ads.AdManager.v202208.Date) value;
                return string.Format("{0:0000}-{1:00}-{2:00}", date.year, date.month, date.day);
            }
            else if (value is Google.Api.Ads.AdManager.v202208.DateTime)
            {
                Google.Api.Ads.AdManager.v202208.DateTime dateTime =
                    (Google.Api.Ads.AdManager.v202208.DateTime) value;
                return string.Format("{0:0000}-{1:00}-{2:00}T{3:00}:{4:00}:{5:00} {6}",
                    dateTime.date.year, dateTime.date.month, dateTime.date.day, dateTime.hour,
                    dateTime.minute, dateTime.second, dateTime.timeZoneId);
            }
            else if (value is List<object>)
            {
                List<string> textValues = (value as List<object>)
                    .ConvertAll(new Converter<object, string>(GetTextValue))
                    .ConvertAll(new Converter<string, string>(EscapeCsv));
                return String.Join<string>(",", textValues);
            }
            else
            {
                // NumberValue, BooleanValue, TextValue
                return value.ToString();
            }
        }

        private static string EscapeCsv(string value)
        {
            value = value.Replace("\"", "\"\"");
            if (value.Contains(",") || value.Contains("\""))
            {
                value = string.Format(CultureInfo.InvariantCulture, "\"{0}\"", value);
            }

            return value;
        }
    }
}

redpajama_set_name: RedPajamaGithub


text:
package org.apache.kerby.kerberos.kerb.integration.test;

import org.apache.kerby.util.NetworkUtil;
import org.junit.Assert;
import org.junit.Before;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.security.auth.Subject;
import java.security.PrivilegedAction;

public abstract class TokenAppTest extends TokenLoginTestBase {
    private static final Logger LOG = LoggerFactory.getLogger(TokenAppTest.class);
    private int serverPort;
    protected AppServer appServer;

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

        serverPort = NetworkUtil.getServerPort();

        setupAppServer();
    }

    protected int getServerPort() {
        return serverPort;
    }

    protected void setupAppServer() throws Exception {
        Subject subject = loginServiceUsingKeytab();
        Subject.doAs(subject, new PrivilegedAction<Object>() {
            @Override
            public Object run() {
                try {
                    appServer = createAppServer();
                    appServer.start();
                } catch (Exception ex) {
                    LOG.error(ex.toString());
                }

                return null;
            }
        });
    }

    protected abstract AppServer createAppServer() throws Exception;

    protected void runAppClient(final AppClient appClient) throws Exception {
        Subject subject = loginClientUsingTicketCache();
        Subject.doAs(subject, new PrivilegedAction<Object>() {
            @Override
            public Object run() {
                try {
                    appClient.run();
                } catch (Exception ex) {
                    LOG.error(ex.toString());
                }
                return null;
            }
        });

        Assert.assertTrue("Client successfully connected and authenticated to server",
                appClient.isTestOK());
    }

}


text:
from datetime import timedelta, datetime

from gtfspy.import_loaders.table_loader import TableLoader

class DayLoader(TableLoader):
    # Note: calendar and calendar_dates should have been imported before
    # importing with DayLoader
    fname = None
    table = 'days'
    tabledef = '(date TEXT, day_start_ut INT, trip_I INT)'
    copy_where = "WHERE  {start_ut} <= day_start_ut  AND  day_start_ut < {end_ut}"

    def post_import(self, cur):
        insert_data_to_days(cur, self._conn)

    def index(self, cur):
        create_day_table_indices(cur)

def create_day_table_indices(cursor):
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_days_day ON days (date)')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_days_dsut_tid ON days (day_start_ut, trip_I)')

def drop_day_table_indices(cursor):
    cursor.execute("DROP INDEX IF EXISTS idx_days_day")
    cursor.execute("DROP INDEX IF EXISTS idx_days_dsut_tid")

def insert_data_to_days(cur, conn):
    # clear if something existed before
    cur.execute("DELETE FROM days")
    days = []
    # This index is important here, but no where else, and not for
    # future processing.  So, create it here, delete it at the end
    # of the function.  If this index was important, it could be
    # moved to CalendarDatesLoader.
    cur.execute('CREATE INDEX IF NOT EXISTS idx_calendar_dates_sid ON calendar_dates (service_I)')

    cur.execute('SELECT * FROM calendar')
    colnames = cur.description
    cur2 = conn.cursor()

    def make_dict(row):
        """Quick function to make dictionary out of row"""
        return dict((key[0], value) for key, value in zip(colnames, row))

    def iter_dates(start, end):
        """Iter date objects for start--end, INCLUSIVE of end date"""
        one_day = timedelta(days=1)
        date = start
        while date <= end:
            yield date
            date += one_day

    weekdays = ['m', 't', 'w', 'th', 'f', 's', 'su']
    # For every row in the calendar...
    for row in cur:
        row = make_dict(row)
        service_I = int(row['service_I'])
        # EXCEPTIONS (calendar_dates): Get a set of all
        # exceptional days.  exception_type=2 means that service
        # removed on that day.  Below, we will exclude all dates
        # that are in this set.
        cur2.execute('SELECT date FROM calendar_dates '
                     'WHERE service_I=? and exception_type=?',
                     (service_I, 2))
        exception_dates = set(x[0] for x in cur2.fetchall())
        #
        start_date = datetime.strptime(row['start_date'], '%Y-%m-%d').date()
        end_date = datetime.strptime(row['end_date'], '%Y-%m-%d').date()
        # For every date in that row's date range...
        for date in iter_dates(start_date, end_date):
            weekday = date.isoweekday() - 1  # -1 to match weekdays list above
            # Exclude dates with service exceptions
            date_str = date.strftime('%Y-%m-%d')
            if date_str in exception_dates:
                # print "calendar_dates.txt exception: removing %s from %s"%(service_I, date)
                continue
            # If this weekday is marked as true in the calendar...
            if row[weekdays[weekday]]:
                # Make a list of this service ID being active then.
                days.append((date, service_I))

    # Store in database, day_start_ut is "noon minus 12 hours".
    cur.executemany("""INSERT INTO days
                   (date, day_start_ut, trip_I)
                   SELECT ?, strftime('%s', ?, '12:00', 'utc')-43200, trip_I
                   FROM trips WHERE service_I=?
                   """, ((date, date, service_I)
                         for date, service_I in days))

    # EXCEPTIONS: Add in dates with exceptions.  Find them and
    # store them directly in the database.
    cur2.execute("INSERT INTO days "
                 "(date, day_start_ut, trip_I) "
                 "SELECT date, strftime('%s',date,'12:00','utc')-43200, trip_I "
                 "FROM trips "
                 "JOIN calendar_dates USING(service_I) "
                 "WHERE exception_type=?",
                 (1,))
    conn.commit()
    cur.execute('DROP INDEX IF EXISTS main.idx_calendar_dates_sid')

def recreate_days_table(conn):
    cursor = conn.cursor()
    drop_day_table_indices(cursor)
    insert_data_to_days(cursor, conn)
    create_day_table_indices(cursor)


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

    <title>MavenArtifact (Gradle API 2.3)</title>
    <meta name="date" content="2015-02-16">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link href="../../../../../groovy.ico" type="image/x-icon" rel="shortcut icon">
    <link href="../../../../../groovy.ico" type="image/x-icon" rel="icon">
    <link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">

JavaScript is disabled on your browser.

Overview

Package

Class

Deprecated

Index

Help

Frames

No Frames

Summary:

            Nested   Field
Method

 | Detail:

            Field
Method

Package: org.gradle.api.publish.maven

[Java] Interface MavenArtifact

org.gradle.api.publish.maven.MavenArtifact

All Superinterfaces:

Buildable

 An artifact published as part of a MavenPublication.

Methods Summary

Methods

                            Type
                            Name and description

                            void
                            builtBy(Object... tasks)
Registers some tasks which build this artifact.

                            String
                            getClassifier()
The classifier used to publish the artifact file.

                            String
                            getExtension()
The extension used to publish the artifact file, never null.

                            File
                            getFile()
The actual file contents to publish.

                            void
                            setClassifier(String classifier)
Sets the classifier used to publish the artifact file.

                            void
                            setExtension(String extension)
Sets the extension used to publish the artifact file.

Inherited Methods Summary

Inherited Methods

                            Methods inherited from class
                            Name

                            interface Buildable
                            getBuildDependencies

Method Detail

public void builtBy(Object... tasks)

 Registers some tasks which build this artifact.

Parameters:

tasks -  The tasks. These are evaluated as per Task.dependsOn.

@Nullable
public String getClassifier()

 The classifier used to publish the artifact file.
 A null value (the default) indicates that this artifact will be published without a classifier.

public String getExtension()

 The extension used to publish the artifact file, never null.
 For an artifact without an extension, this value will be an empty String.

public File getFile()

 The actual file contents to publish.

public void setClassifier(@Nullable String classifier)

 Sets the classifier used to publish the artifact file.

Parameters:

classifier -  The classifier.

public void setExtension(String extension)

 Sets the extension used to publish the artifact file.

Parameters:

extension -  The extension.

Overview

Package

Class

Deprecated

Index

Help

Frames

No Frames

Summary:

            Nested   Field
Method

 | Detail:

            Field
Method

Gradle API 2.3


text:
<?php

/**
 * Configurable product associated products in stock filter
 *
 * @category   Mage
 * @package    Mage_Adminhtml
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Super_Config_Grid_Filter_Inventory extends Mage_Adminhtml_Block_Widget_Grid_Column_Filter_Select
{

    protected function _getOptions()
    {
        return array(
            array(
                'value' =>  '',
                'label' =>  ''
            ),
            array(
                'value' =>  1,
                'label' =>  Mage::helper('catalog')->__('In Stock')
            ),
            array(
                'value' =>  0,
                'label' =>  Mage::helper('catalog')->__('Out of Stock')
            )
        );
    }

}


text:
package org.kuali.rice.krad.web.bind;

import org.apache.commons.lang.StringUtils;
import org.kuali.rice.core.api.CoreConstants;
import org.kuali.rice.core.api.datetime.DateTimeService;
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
import org.kuali.rice.core.api.util.RiceKeyConstants;
import org.kuali.rice.core.web.format.FormatException;

import java.beans.PropertyEditorSupport;
import java.io.Serializable;
import java.sql.Time;
import java.text.ParseException;

/**
 * PropertyEditor converts between time display strings and {@code java.sql.Time} objects
 *
 * @author Kuali Rice Team (rice.collab@kuali.org)
 */
public class UifTimeEditor extends PropertyEditorSupport implements Serializable {

    private static final long serialVersionUID = 3087028215717940843L;

    private transient DateTimeService dateTimeService;

    /**
     * Converts the time object to the display string format.
     *
     * @return the time object in the display string format or null if it is empty
     */
    @Override
    public String getAsText() {
        if (getValue() == null) {
            return null;
        }

        if (getValue() instanceof String && StringUtils.isBlank((String) getValue())) {
            return null;
        }

        return getDateTimeService().toTimeString((Time) getValue());
    }

    /**
     * Converts the display string to a time object.
     */
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        setValue(convertToObject(text));
    }

    /**
     * Converts the display text to a time object.
     *
     * @param text the display text
     *
     * @return the time object
     */
    protected Object convertToObject(String text) {
        if (StringUtils.isBlank(text)) {
            return null;
        }

        try {
            return getDateTimeService().convertToSqlTime(text);
        } catch (ParseException e) {
            throw new FormatException("parsing", RiceKeyConstants.ERROR_TIME, text, e);
        }
    }

    /**
     * Gets the date time service.
     *
     * @return the date time service
     */
    protected DateTimeService getDateTimeService() {
        if (dateTimeService == null) {
            dateTimeService = GlobalResourceLoader.getService(CoreConstants.Services.DATETIME_SERVICE);
        }
        return dateTimeService;
    }

}


text:
using CharacterMap.Core;
using CharacterMap.Models;
using System;
using System.Collections.Generic;
using Windows.UI.Xaml.Controls;

namespace CharacterMap.Provider
{
    public class CppWinrtDevProvider : DevProviderBase
    {
        public CppWinrtDevProvider(CharacterRenderingOptions r, Character c) : base(r, c) {
            DisplayName = "C++/WinRT";
        }

        protected override DevProviderType GetDevProviderType() => DevProviderType.CppWinRT;
        protected override IReadOnlyList<DevOption> OnGetContextOptions() => Inflate();
        protected override IReadOnlyList<DevOption> OnGetOptions() => Inflate();

        IReadOnlyList<DevOption> Inflate()
        {
            var v = Options.Variant;
            var c = Character;

            bool hasSymbol = FontFinder.IsSystemSymbolFamily(v) && Enum.IsDefined(typeof(Symbol), (int)c.UnicodeIndex);
            string hex = c.UnicodeIndex.ToString("x4").ToUpper();
            string pathIconData = GetOutlineGeometry(c, Options);

            string fontIcon =
                "// Add \"#include <winrt/Windows.UI.Xaml.Media.h>\" to pch.h\n" +
                "auto f = FontIcon();\n" +
                $"f.Glyph(L\"\\u{hex}\");\n" +
                $"f.FontFamily(Media::FontFamily(L\"{v?.XamlFontSource}\"));";

            var ops = new List<DevOption>()
            {
                new ("TxtXamlCode/Header", c.UnicodeIndex > 0xFFFF ? $"\\U{c.UnicodeIndex:x8}".ToUpper() : $"\\u{hex}"),
                new ("TxtFontIcon/Header", fontIcon, true),
            };

            if (!string.IsNullOrWhiteSpace(pathIconData))
            {
                var data =
                    $"// Add \"#include <winrt/Windows.UI.Xaml.Media.h>\" to pch.h\n" +
                    $"auto p = PathIcon();\n" +
                    $"p.VerticalAlignment(VerticalAlignment::Center);\n" +
                    $"p.HorizontalAlignment(HorizontalAlignment::Center);\n" +
                    $"p.Data(Markup::XamlBindingHelper::ConvertValue(winrt::xaml_typename<Geometry>(), box_value(L\"{pathIconData}\")).try_as<Geometry>());";

                ops.Add(new DevOption("TxtPathIcon/Text", data, true, true));
            }

            if (hasSymbol)
                ops.Add(new DevOption("TxtSymbolIcon/Header", $"SymbolIcon(Symbol::{(Symbol)c.UnicodeIndex});"));

            return ops;
        }
    }

}


text:
namespace query_tiles {
namespace {

TEST(QueryTilesStatsTest, RecordImageLoading) {
  base::HistogramTester tester;
  stats::RecordImageLoading(stats::ImagePreloadingEvent::kStart);
  tester.ExpectBucketCount(stats::kImagePreloadingHistogram, 0, 1);
}

TEST(QueryTilesStatsTest, RecordTileFetcherResponseCode) {
  base::HistogramTester tester;
  stats::RecordTileFetcherResponseCode(200);
  tester.ExpectBucketCount(stats::kHttpResponseCodeHistogram, 200, 1);
}

TEST(QueryTilesStatsTest, RecordTileFetcherNetErrorCode) {
  base::HistogramTester tester;
  stats::RecordTileFetcherNetErrorCode(105);
  tester.ExpectBucketCount(stats::kNetErrorCodeHistogram, -105, 1);
}

TEST(QueryTilesStatsTest, RecordTileRequestStatus) {
  base::HistogramTester tester;
  stats::RecordTileRequestStatus(TileInfoRequestStatus::kSuccess);
  tester.ExpectBucketCount(stats::kRequestStatusHistogram, 1, 1);
}

TEST(QueryTilesStatsTest, RecordTileGroupStatus) {
  base::HistogramTester tester;
  stats::RecordTileGroupStatus(TileGroupStatus::kNoTiles);
  tester.ExpectBucketCount(stats::kGroupStatusHistogram, 3, 1);
}

TEST(QueryTilesStatsTest, RecordFirstFetchFlowDuration) {
  base::HistogramTester tester;
  stats::RecordFirstFetchFlowDuration(18);
  tester.ExpectBucketCount(stats::kFirstFlowDurationHistogram, 18, 1);
}

TEST(QueryTilesStatsTest, RecordExplodeOnFetchStarted) {
  base::HistogramTester tester;
  stats::RecordExplodeOnFetchStarted(12);
  tester.ExpectBucketCount(stats::kFetcherStartHourHistogram, 12, 1);
}

TEST(QueryTilesStatsTest, RecordGroupPruned) {
  base::HistogramTester tester;
  stats::RecordGroupPruned(stats::PrunedGroupReason::kExpired);
  stats::RecordGroupPruned(stats::PrunedGroupReason::kInvalidLocale);
  tester.ExpectBucketCount(stats::kPrunedGroupReasonHistogram, 0, 1);
  tester.ExpectBucketCount(stats::kPrunedGroupReasonHistogram, 1, 1);
}

TEST(QueryTilesStatsTest, RecordTrendingTileEvent) {
  base::HistogramTester tester;
  stats::RecordTrendingTileEvent(stats::TrendingTileEvent::kRemoved);
  stats::RecordTrendingTileEvent(stats::TrendingTileEvent::kClicked);
  tester.ExpectBucketCount(stats::kTrendingTileEventHistogram, 0, 0);
  tester.ExpectBucketCount(stats::kTrendingTileEventHistogram, 1, 1);
  tester.ExpectBucketCount(stats::kTrendingTileEventHistogram, 2, 1);
}

}  // namespace
}  // namespace query_tiles


text:
import csv
from sys import exit
from Bio import SeqIO
import subprocess
import multiprocessing
import functools
import logging
import pprint
import mglobals

log = logging.getLogger('pipeline')

def sub_call(command, stdout=None, shell=False):
    '''
    Subprocess manager: will log commands and raise an error
    if return code does not equal 0.
    '''
    pretty_command = pprint.pformat(command, indent=4)
    log.debug('running command:\n{}'.format(pretty_command))
    if stdout:
        log.debug('writing to: ' + stdout.name)
    subprocess.check_call(command, stdout=stdout, shell=shell)

def log_func(func):
    '''
    Decorator which will wrap functions so that the beginning
    and end is logged.
    '''
    @functools.wraps(func)
    def log_wrapper(*args, **kwargs):
        if mglobals.original:
            log.info('{:-^50}'.format(' Original: beginning ' + func.__name__ + ' '))
        else:
            log.info('{:-^50}'.format(' Alternate: beginning ' + func.__name__ + ' '))
        output = func(*args, **kwargs)
        if mglobals.original:
            log.info(' Original: finished ' + func.__name__ + ' ')
        else:
            log.info(' Alternate: finished ' + func.__name__ + ' ')
        return output
    return log_wrapper

def multiprocess(iterator):
    '''
    Returns the multiprocess decorator. This function's only purpose
    is to pass its iterator argument along.
    '''

    def multiprocess_decorator(func):
        '''
        Returns the multiprocess wrapper
        '''

        @functools.wraps(func)
        def multiprocess_wrapper():
            '''
            Multiprocesses a given function with arguments specified
            by the iterator.  This will create a separate process for each
            item in the iterator.
            '''
            log.info('#' * 45 + ' -> start')
            log.info('Calling multi_processor with target: ' + func.__name__)
            jobs = []
            for it in iterator:
                if isinstance(it, tuple):
                    j = multiprocessing.Process(target=func,
                                                name=(func.__name__ + ' ' + str(it)),
                                                args=it)
                else:
                    j = multiprocessing.Process(target=func,
                                                name=(func.__name__ + ' ' + it),
                                                args=(it,))
                jobs.append(j)
                j.start()

            for j in jobs:
                j.join()
            for j in jobs:
                if j.exitcode != 0:
                    raise AssertionError('multi process returned with non-0 '
                                         'exit code')
            log.info('#' * 45 + ' end <-')
        return multiprocess_wrapper

    return multiprocess_decorator

def remove_dups(input_f, output_f):
    with open(input_f, 'rb') as f, open(output_f, 'wb') as fout:
        reader = csv.reader(f, delimiter='\t')

        # sort by chromosome and then position
        possortedreader = sorted(reader, key=lambda row: int(row[1]))
        sortedreader = sorted(possortedreader, key=lambda row: row[0])

        writer = csv.writer(fout, delimiter='\t', lineterminator='\n')
        posBuffer = None
        for row in sortedreader:
            if row[:2] == posBuffer:
                continue
            posBuffer = row[:2]
            writer.writerow(row)

def vcf_fix(template_f, input_f, output_f):
    '''
    Removes duplicate alleles from a varScan generated vcf. Also transfers the header
    from a specified template file.
    '''
    with open(template_f, 'rb') as t, open(input_f, 'rb') as f, open(output_f, 'wb') as fout:
        for line in t:
            if line.startswith('##'):
                fout.write(line)
            elif line.startswith('#') and not line.startswith('##'):
                fout.write(line)
                break
        reader = csv.reader(f, delimiter='\t')
        # Sort the vcfs here as well
        sortedreader = sorted(reader, key=lambda row: int(row[1]))
        sortedreader.sort(key=lambda row: row[0])
        writer = csv.writer(fout, delimiter='\t', lineterminator='\n')
        for row in sortedreader:
            if row[3] != row[4]:
                newrow = row
                alt_alleles = row[4]
                newalt_alleles = ','.join(list(set(row[4].split(','))))
                newrow[4] = newalt_alleles
                writer.writerow(newrow)

def fasta_fix(input_f, output_f):

    def replaceheaders():
        chromList = ['2L', '2R', '3L', '3R', '4', 'M', 'X']
        i = 0
        for rec in SeqIO.parse(input_f, "fasta"):
            rec.id = chromList[i]
            rec.description = ''
            yield rec
            i += 1

    newrecs = (rec for rec in replaceheaders())
    SeqIO.write(newrecs, output_f, "fasta")

def quick_mean_propR(input_f):
    with open(input_f, 'rb') as f:
        reader = csv.DictReader(f, delimiter='\t')

        propRs = []
        for i, row in enumerate(reader, 1):
            ref_depth = float(row['R_Depth'])
            alt_depth = float(row['A_Depth'])
            if ref_depth != 0 and alt_depth != 0:
                propR = ref_depth/(ref_depth + alt_depth)
                propRs.append(propR)
        mean_propR = sum(propRs)/len(propRs)
        return (mean_propR, i)

def filter_vcf_by_coverage_cutoffs(vcf, cutoff_table):
    with open(cutoff_table, 'rb') as cf:
        cutoff_reader = csv.reader(cf, delimiter=',')
        next(cutoff_reader)  # remove header
        cutoffs = {int(row[0]): int(row[1]) for row in cutoff_reader}

    assert vcf[-4:] == '.vcf'
    with open(vcf, 'rb') as vf, open((vcf[:-4] + '_covfil.vcf'), 'wb') as fout:
        # Remove commented lines
        for line in vf:
            if line.startswith('##'):
                continue
            else:
                break
        fields = ('CHROM', 'POS', 'ID', 'REF', 'ALT', 'QUAL', 'FILTER',
                  'INFO', 'FORMAT', 'Sample1')
        vcf_reader = csv.DictReader(vf, delimiter='\t', fieldnames=fields)
        writer = csv.DictWriter(fout, delimiter='\t', fieldnames=fields, lineterminator='\n')
        for row in vcf_reader:
            format_list = row['FORMAT'].split(':')
            sample_list = row['Sample1'].split(':')
            sample_dict = dict(zip(format_list, sample_list))
            num_ref = int(sample_dict['RD'])
            num_alt = int(sample_dict['AD'])
            coverage = num_ref + num_alt
            cutoff = cutoffs.get(coverage, 24)  # Will return 24 if key not in dict
            if coverage >= 10 and num_ref >= cutoff and num_alt >= cutoff:
                writer.writerow(row)


text:
import copy
import json
import os
from unittest.mock import MagicMock, call

from bravado.client import SwaggerClient
import bravado.exception
from bravado_falcon import FalconHttpClient
import falcon
import pytest
import pytest_falcon.plugin
import responses
import yaml

from data_acquisition.acquisition_request import AcquisitionRequest, RequestNotFoundError
from data_acquisition.consts import (ACQUISITION_PATH, DOWNLOAD_CALLBACK_PATH,
                                     METADATA_PARSER_CALLBACK_PATH, GET_REQUEST_PATH)
from data_acquisition.resources import (get_download_callback_url, get_metadata_callback_url,
                                        AcquisitionResource)
import tests
from tests.consts import (TEST_DOWNLOAD_REQUEST, TEST_DOWNLOAD_CALLBACK, TEST_ACQUISITION_REQ,
                          TEST_ACQUISITION_REQ_JSON)

FAKE_TIME = 234.25
FAKE_TIMESTAMP = 234

@pytest.fixture(scope='function')
def client(falcon_api):
    client = pytest_falcon.plugin.Client(falcon_api)
    client.post = (lambda path, data, post=client.post:
                   post(path, json.dumps(data), headers={'Content-Type': 'application/json'}))
    return client

@pytest.fixture(scope='session')
def swagger_spec():
    spec_file_path = os.path.join(tests.__path__[0], '../api_doc.yaml')
    with open(spec_file_path) as spec_file:
        return yaml.load(spec_file)

@pytest.fixture(scope='function')
def client_no_req_validation(falcon_api, swagger_spec):
    return SwaggerClient.from_spec(swagger_spec,
                                   http_client=FalconHttpClient(falcon_api),
                                   config={'validate_requests': False})

@pytest.fixture(scope='function')
def client_swagger(falcon_api, swagger_spec):
    return SwaggerClient.from_spec(swagger_spec,
                                   http_client=FalconHttpClient(falcon_api))

@pytest.fixture(scope='function')
def acquisition_requests_resource(das_config, mock_executor, mock_req_store, fake_time):
    return AcquisitionResource(mock_req_store, mock_executor, das_config)

@pytest.fixture(scope='function')
def req_store_get(mock_req_store):
    mock_req_store.get.return_value = copy.deepcopy(TEST_ACQUISITION_REQ)
    return mock_req_store.get

@pytest.fixture(scope='function')
def fake_time(monkeypatch):
    monkeypatch.setattr('time.time', lambda: FAKE_TIME)

def test_get_download_callback_url():
    callback_url = get_download_callback_url('https://some-test-das-url', 'some-test-id')
    assert callback_url == 'https://some-test-das-url/v1/das/callback/downloader/some-test-id'

def test_get_metadata_callback_url():
    callback_url = get_metadata_callback_url('https://some-test-das-url', 'some-test-id')
    assert callback_url == 'https://some-test-das-url/v1/das/callback/metadata/some-test-id'

@responses.activate
def test_external_service_call_not_ok(acquisition_requests_resource):
    test_url = 'https://some-fake-url/'
    responses.add(responses.POST, test_url, status=404)

    assert not acquisition_requests_resource._external_service_call(
        url=test_url, data={'a': 'b'}, token='bearer fake-token', request_id='some-fake-id')

def test_processing_acquisition_request_for_hdfs(acquisition_requests_resource, mock_req_store):
    # arrange
    mock_enqueue_metadata_req = MagicMock()
    acquisition_requests_resource._enqueue_metadata_request = mock_enqueue_metadata_req

    hdfs_acquisition_req = copy.deepcopy(TEST_ACQUISITION_REQ)
    hdfs_acquisition_req.source = TEST_ACQUISITION_REQ.source.replace('http://', 'hdfs://')
    proper_saved_request = copy.deepcopy(hdfs_acquisition_req)
    proper_saved_request.set_downloaded()
    fake_token = 'bearer asdasdasdasd'

    # act
    acquisition_requests_resource._process_acquisition_request(hdfs_acquisition_req, fake_token)

    # assert
    mock_enqueue_metadata_req.assert_called_with(proper_saved_request, None, fake_token)
    mock_req_store.put.assert_called_with(proper_saved_request)

def test_acquisition_bad_request(client_no_req_validation):
    broken_request = dict(TEST_DOWNLOAD_REQUEST)
    del broken_request['category']

    with pytest.raises(bravado.exception.HTTPError):
        client_no_req_validation.rest.submitAcquisitionRequest(body=broken_request).result()

def test_downloader_callback_failed(client, fake_time, mock_req_store, req_store_get):
    failed_callback_req = dict(TEST_DOWNLOAD_CALLBACK)
    failed_callback_req['state'] = 'ERROR'

    response = client.post(
        path=DOWNLOAD_CALLBACK_PATH.format(req_id=TEST_ACQUISITION_REQ.id),
        data=failed_callback_req)

    assert response.status == falcon.HTTP_200

    updated_request = AcquisitionRequest(**TEST_ACQUISITION_REQ_JSON)
    updated_request.state = 'ERROR'
    updated_request.timestamps['ERROR'] = FAKE_TIMESTAMP
    mock_req_store.put.assert_called_with(updated_request)

def test_metadata_callback_failed(client, fake_time, mock_req_store, req_store_get):
    response = client.post(
        path=METADATA_PARSER_CALLBACK_PATH.format(req_id=TEST_ACQUISITION_REQ.id),
        data={'state': 'FAILED'})

    assert response.status == falcon.HTTP_200
    updated_request = AcquisitionRequest(**TEST_ACQUISITION_REQ_JSON)
    updated_request.state = 'ERROR'
    updated_request.timestamps['ERROR'] = FAKE_TIMESTAMP
    mock_req_store.put.assert_called_with(updated_request)

def test_get_request(das_api, client_swagger, req_store_get):
    das_api.request_management_res._org_checker = MagicMock()
    acquisition_request = client_swagger.rest.getRequest(req_id=TEST_ACQUISITION_REQ.id).result()
    assert AcquisitionRequest(**acquisition_request.__dict__) == TEST_ACQUISITION_REQ

def test_get_request_not_found(client, mock_req_store):
    mock_req_store.get.side_effect = RequestNotFoundError()
    response = client.get(GET_REQUEST_PATH.format(req_id='some-fake-id'))
    assert response.status == falcon.HTTP_404

def test_delete_request(das_api, client, mock_req_store, req_store_get):
    das_api.request_management_res._org_checker = MagicMock()

    response = client.delete(GET_REQUEST_PATH.format(req_id=TEST_ACQUISITION_REQ.id))

    assert response.status == falcon.HTTP_200
    mock_req_store.delete.assert_called_with(TEST_ACQUISITION_REQ)

def test_delete_request_not_found(client, mock_req_store):
    mock_req_store.get.side_effect = RequestNotFoundError()
    response = client.delete(GET_REQUEST_PATH.format(req_id='fake-id'))
    assert response.status == falcon.HTTP_404

@pytest.mark.parametrize('org_ids', [
    ['id-1'],
    ['id-1', 'id-2'],
    ['id-1', 'id-2', 'id-3'],
])
@pytest.mark.parametrize('acquisition_requests', [
    [TEST_ACQUISITION_REQ],
    [TEST_ACQUISITION_REQ, TEST_ACQUISITION_REQ]
])
def test_get_requests_for_org(org_ids, acquisition_requests,
                              das_api, client, mock_req_store):
    das_api.acquisition_res._org_checker = MagicMock()
    mock_req_store.get_for_org.return_value = acquisition_requests

    response = client.get(path=ACQUISITION_PATH,
                          query_string='orgs=' + ','.join(org_ids))

    returned_requests = [AcquisitionRequest(**req_json) for req_json in response.json]
    assert response.status == falcon.HTTP_200
    assert returned_requests == acquisition_requests * len(org_ids)
    assert mock_req_store.get_for_org.call_args_list == [call(id) for id in org_ids]


text:
from contextlib import contextmanager
import importlib
import numpy as np
import os
from scipy.misc import toimage
import shutil
import tempfile
import types
import unittest

__all__ = [
    'unittest',
    'TempDir',
    'TempFile',
    'MonkeyPatch',
    'RecordedFunctionCall',
    'MakeCorpus',
    'MakeCorpusWithImages',
    ]

@contextmanager
def TempDir():
  """Context manager that creates a temporary directory on disk.

  The created directory is deleted from disk when the interpreter leaves the
  context.

  Example:
  >>> with TempDir() as dname:
  >>>   print dname

  """
  path = tempfile.mkdtemp()
  yield path
  shutil.rmtree(path)

@contextmanager
def TempFile(suffix=''):
  """Context manager that creates a temporary file on disk.

  The created file is deleted from disk when the interpreter leaves the
  context.

  Example:
  >>> with TempFile() as fname:
  >>>   print fname

  """
  fd, path = tempfile.mkstemp(suffix=suffix)
  yield path
  os.remove(path)

@contextmanager
def MonkeyPatch(*records):
  """Context manager that temporarily replace function definitions.

  :param records: Functions to replace, and the callable with which to replace
     them.
  :type records: 3-tuple or list of 3-tuple

  A record is a 3-tuple of (MODULE, SYMBOL, FUNC), where
  :param MODULE: a module type or a string giving the module name
  :param str SYMBOL: symbol name to replace
  :param callable FUNC: function to call when SYMBOL is accessed

  All patched symbols are reset when the interpreter leaves the context.

  Example:
  >>> import glimpse.experiment
  >>> with MonkeyPatch(glimpse.experiment, 'Verbose', (lambda x=None: None)):
  >>>   glimpse.experiment.Verbose()
  >>> with MonkeyPatch(('glimpse.experiment', 'Verbose',
          (lambda x=None: None)),):
  >>>   glimpse.Experiment.Verbse()

  """
  assert len(records) > 0
  if (isinstance(records[0], basestring) or
      isinstance(records[0], types.ModuleType) or
      not hasattr(records[0], '__len__')):
    records = [ records ]  # assume single record was given
  unwind_records = list()
  for module_name, symbol, patch_value in records:
    if isinstance(module_name, types.ModuleType):
      mod = module_name
    else:
      mod = importlib.import_module(module_name)
    orig_value = getattr(mod, symbol)
    setattr(mod, symbol, patch_value)
    unwind_records.append((mod, symbol, orig_value))
  yield
  for mod, symbol, orig_value in unwind_records:
    setattr(mod, symbol, orig_value)

class RecordedFunctionCall(object):
  """A constant-valued function that records information about it's call."""

  def __init__(self, return_value=None):
    self.called = False  # whether function has been called
    self.args = None  # positional arguments passed to function
    self.kw = None  # keyword arguments passed to function
    self.return_value = return_value  # value that function will return

  def __call__(self, *args, **kw):
    self.called = True
    self.args = args
    self.kw = kw
    return self.return_value

def MakeCorpus(root, **subdirs):
  """Create a corpus on disk with empty files.

  :param subdirs: file names in sub-directory
  :type subdirs: list of str

  Example:
  >>> MakeCorpus('/tmp/my_test_root', cats=('cat1.png', 'cat2.png'),
          dogs=('dog1.png', 'dog2.png'))

  """
  for cls, files in subdirs.items():
    os.mkdir(os.path.join(root, cls))
    for f in files:
      with open(os.path.join(root, cls, f), 'w') as fh:
        fh.write("1")

def MakeCorpusWithImages(root, **subdirs):
  """Create a corpus on disk with random image data.

  :param subdirs: file names in sub-directory
  :type subdirs: list of str

  Example:
  >>> MakeCorpusWithImages('/tmp/my_test_root', cats=('cat1.png', 'cat2.png'),
          dogs=('dog1.png', 'dog2.png'))

  """
  for cls, files in subdirs.items():
    os.mkdir(os.path.join(root, cls))
    for f in files:
      img = toimage(np.random.random((100,101)) * 255)
      img.save(os.path.join(root, cls, f))


text:
/* global bard */
describe('History Routes:', function () {

    beforeEach(function () {
        //Create abstract parent state first
        //TODO: create funcion for this, so we can just call the function and not need to inject the entire block everywhere
        module('ui.router', function ($stateProvider) {
            $stateProvider.state('flexget', { abstract: true });
        });
        module('plugins.history');

        /* global $state, $rootScope, $location */
        bard.inject('$state', '$rootScope', '$location');
    });

    it('should map state \'flexget.history\' to url #/history', function () {
        expect($state.href('flexget.history', {})).to.equal('#/history');
    });

    it.skip('should map state to the \'history\' component', function () {
        expect($state.get('flexget.history').component).to.equal('historyView');
    });

    describe('Transitions', function () {
        it('should work with $state.go', function () {
            $state.go('flexget.history');
            $rootScope.$digest();
            expect($state.is('flexget.history')).to.be.true;
        });

        it('should work with \'history\' path', function () {
            $location.path('history');
            $rootScope.$digest();
            expect($state.is('flexget.history')).to.be.true;
        });
    });
});


text:
CompoundDefinitions/loose-shallow.md

Item 1
Item 1 lazy continuation

Item 2
Item 2 continuation

Loose Item 3
 Item 3 deep indent continuation

Item 4
Item 4 lazy continuation
Item 4 continuation
 Item 4 deep indent continuation

Loose Item 5
Item 5 lazy continuation

Item 5 paragraph 1
Item 5 paragraph 1 lazy continuation
Item 5 paragraph 1 continuation
 Item 5 paragraph 1 deep indent continuation

Item 5 code block 1
    Item 5 code block 1 indented 1
        Item 5 code block indented x2
    Item 5 code block 1 indented 2
Item 5 code block 1.2

Item 5 paragraph 2
Item 5 paragraph 2 lazy continuation
Item 5 paragraph 2 continuation
 Item 5 paragraph 2 deep indent continuation

    Item 5 code block 2
        Item 5 code block 2 indented 1
            Item 5 code block indented x2
        Item 5 code block 2 indented 2
    Item 5 code block 2.2

Loose Item 6


text:
[Orbax](https://github.com/google/orbax/blob/main/docs/index.md) is a
library providing common utilities for JAX users.

## Checkpointing

Orbax includes a checkpointing library oriented towards JAX users, supporting a
variety of different features required by different frameworks, including
asynchronous checkpointing, various types, and various storage formats.
We aim to provide a highly customizable and composable API which maximizes
flexibility for diverse use cases.

To get started, check out our [documentation](https://github.com/google/orbax/blob/main/docs/checkpoint.md).

Check out our [colab](http://colab.research.google.com/github/google/orbax/blob/main/orbax//checkpoint/orbax_checkpoint.ipynb) for a hands-on introduction.