text:
package com.intellij.codeInsight;

import com.intellij.openapi.util.Pair;
import com.intellij.psi.*;
import com.intellij.psi.controlFlow.*;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.infos.CandidateInfo;
import com.intellij.psi.scope.MethodProcessorSetupFailedException;
import com.intellij.psi.scope.processor.MethodResolverProcessor;
import com.intellij.psi.scope.util.PsiScopesUtil;
import com.intellij.psi.util.*;
import com.intellij.util.Function;
import com.intellij.util.NullableFunction;
import com.intellij.util.SmartList;
import com.intellij.util.containers.ContainerUtil;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

/**
 * @author mike
 */
public class ExceptionUtil {
  @NonNls private static final String CLONE_METHOD_NAME = "clone";

  private ExceptionUtil() {}

  @NotNull
  public static List<PsiClassType> getThrownExceptions(@NotNull PsiElement[] elements) {
    List<PsiClassType> array = ContainerUtil.newArrayList();
    for (PsiElement element : elements) {
      List<PsiClassType> exceptions = getThrownExceptions(element);
      addExceptions(array, exceptions);
    }

    return array;
  }

  @NotNull
  public static List<PsiClassType> getThrownCheckedExceptions(@NotNull PsiElement[] elements) {
    List<PsiClassType> exceptions = getThrownExceptions(elements);
    if (exceptions.isEmpty()) return exceptions;
    exceptions = filterOutUncheckedExceptions(exceptions);
    return exceptions;
  }

  @NotNull
  private static List<PsiClassType> filterOutUncheckedExceptions(@NotNull List<PsiClassType> exceptions) {
    List<PsiClassType> array = ContainerUtil.newArrayList();
    for (PsiClassType exception : exceptions) {
      if (!isUncheckedException(exception)) array.add(exception);
    }
    return array;
  }

  @NotNull
  public static List<PsiClassType> getThrownExceptions(@NotNull PsiElement element) {
    if (element instanceof PsiClass) {
      if (element instanceof PsiAnonymousClass) {
        final PsiExpressionList argumentList = ((PsiAnonymousClass)element).getArgumentList();
        if (argumentList != null){
          return getThrownExceptions(argumentList);
        }
      }
      // filter class declaration in code
      return Collections.emptyList();
    }
    else if (element instanceof PsiMethodCallExpression) {
      PsiReferenceExpression methodRef = ((PsiMethodCallExpression)element).getMethodExpression();
      JavaResolveResult result = methodRef.advancedResolve(false);
      return getExceptionsByMethodAndChildren(element, result);
    }
    else if (element instanceof PsiNewExpression) {
      JavaResolveResult result = ((PsiNewExpression)element).resolveMethodGenerics();
      return getExceptionsByMethodAndChildren(element, result);
    }
    else if (element instanceof PsiThrowStatement) {
      final PsiExpression expr = ((PsiThrowStatement)element).getException();
      if (expr == null) return Collections.emptyList();
      final List<PsiType> types = getPreciseThrowTypes(expr);
      List<PsiClassType> classTypes =
        new ArrayList<PsiClassType>(ContainerUtil.mapNotNull(types, new NullableFunction<PsiType, PsiClassType>() {
          @Override
          public PsiClassType fun(PsiType type) {
            return type instanceof PsiClassType ? (PsiClassType)type : null;
          }
        }));
      addExceptions(classTypes, getThrownExceptions(expr));
      return classTypes;
    }
    else if (element instanceof PsiTryStatement) {
      return getTryExceptions((PsiTryStatement)element);
    }
    else if (element instanceof PsiResourceVariable) {
      final PsiResourceVariable variable = (PsiResourceVariable)element;
      final List<PsiClassType> types = ContainerUtil.newArrayList();
      addExceptions(types, getCloserExceptions(variable));
      final PsiExpression initializer = variable.getInitializer();
      if (initializer != null) addExceptions(types, getThrownExceptions(initializer));
      return types;
    }
    return getThrownExceptions(element.getChildren());
  }

  @NotNull
  private static List<PsiClassType> getTryExceptions(@NotNull PsiTryStatement tryStatement) {
    List<PsiClassType> array = ContainerUtil.newArrayList();

    PsiResourceList resourceList = tryStatement.getResourceList();
    if (resourceList != null) {
      for (PsiResourceVariable variable : resourceList.getResourceVariables()) {
        addExceptions(array, getUnhandledCloserExceptions(variable, resourceList));
      }
    }

    PsiCodeBlock tryBlock = tryStatement.getTryBlock();
    if (tryBlock != null) {
      addExceptions(array, getThrownExceptions(tryBlock));
    }

    for (PsiParameter parameter : tryStatement.getCatchBlockParameters()) {
      PsiType exception = parameter.getType();
      for (int j = array.size() - 1; j >= 0; j--) {
        PsiClassType exception1 = array.get(j);
        if (exception.isAssignableFrom(exception1)) {
          array.remove(exception1);
        }
      }
    }

    for (PsiCodeBlock catchBlock : tryStatement.getCatchBlocks()) {
      addExceptions(array, getThrownExceptions(catchBlock));
    }

    PsiCodeBlock finallyBlock = tryStatement.getFinallyBlock();
    if (finallyBlock != null) {
      // if finally block completes normally, exception not caught
      // if finally block completes abruptly, exception gets lost
      try {
        ControlFlow flow = ControlFlowFactory
          .getInstance(finallyBlock.getProject()).getControlFlow(finallyBlock, LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance(), false);
        int completionReasons = ControlFlowUtil.getCompletionReasons(flow, 0, flow.getSize());
        List<PsiClassType> thrownExceptions = getThrownExceptions(finallyBlock);
        if ((completionReasons & ControlFlowUtil.NORMAL_COMPLETION_REASON) == 0) {
          array = ContainerUtil.newArrayList(thrownExceptions);
        }
        else {
          addExceptions(array, thrownExceptions);
        }
      }
      catch (AnalysisCanceledException e) {
        // incomplete code
      }
    }

    return array;
  }

  @NotNull
  private static List<PsiClassType> getExceptionsByMethodAndChildren(@NotNull PsiElement element, @NotNull JavaResolveResult resolveResult) {
    List<PsiClassType> result = ContainerUtil.newArrayList();

    PsiMethod method = (PsiMethod)resolveResult.getElement();
    if (method != null) {
      addExceptions(result, getExceptionsByMethod(method, resolveResult.getSubstitutor()));
    }

    addExceptions(result, getThrownExceptions(element.getChildren()));

    return result;
  }

  @NotNull
  private static List<PsiClassType> getExceptionsByMethod(@NotNull PsiMethod method, @NotNull PsiSubstitutor substitutor) {
    List<PsiClassType> result = ContainerUtil.newArrayList();

    PsiClassType[] referenceTypes = method.getThrowsList().getReferencedTypes();
    for (PsiType type : referenceTypes) {
      type = substitutor.substitute(type);
      if (type instanceof PsiClassType) {
        result.add((PsiClassType)type);
      }
    }

    return result;
  }

  private static void addExceptions(@NotNull List<PsiClassType> array, @NotNull Collection<PsiClassType> exceptions) {
    for (PsiClassType exception : exceptions) {
      addException(array, exception);
    }
  }

  private static void addException(@NotNull List<PsiClassType> array, @Nullable PsiClassType exception) {
    if (exception == null) return ;
    for (int i = array.size()-1; i>=0; i--) {
      PsiClassType exception1 = array.get(i);
      if (exception1.isAssignableFrom(exception)) return;
      if (exception.isAssignableFrom(exception1)) {
        array.remove(i);
      }
    }
    array.add(exception);
  }

  @NotNull
  public static Collection<PsiClassType> collectUnhandledExceptions(@NotNull PsiElement element, @Nullable PsiElement topElement) {
    return collectUnhandledExceptions(element, topElement, true);
  }

  @NotNull
  public static Collection<PsiClassType> collectUnhandledExceptions(@NotNull PsiElement element,
                                                                    @Nullable PsiElement topElement,
                                                                    boolean includeSelfCalls) {
    final Set<PsiClassType> set = collectUnhandledExceptions(element, topElement, null, includeSelfCalls);
    return set == null ? Collections.<PsiClassType>emptyList() : set;
  }

  @Nullable
  private static Set<PsiClassType> collectUnhandledExceptions(@NotNull PsiElement element,
                                                              @Nullable PsiElement topElement,
                                                              @Nullable Set<PsiClassType> foundExceptions,
                                                              boolean includeSelfCalls) {
    Collection<PsiClassType> unhandledExceptions = null;
    if (element instanceof PsiCallExpression) {
      PsiCallExpression expression = (PsiCallExpression)element;
      unhandledExceptions = getUnhandledExceptions(expression, topElement, includeSelfCalls);
    }
    else if (element instanceof PsiMethodReferenceExpression) {
      unhandledExceptions = getUnhandledExceptions((PsiMethodReferenceExpression)element, topElement);
    }
    else if (element instanceof PsiThrowStatement) {
      PsiThrowStatement statement = (PsiThrowStatement)element;
      unhandledExceptions = getUnhandledExceptions(statement, topElement);
    }
    else if (element instanceof PsiCodeBlock &&
             element.getParent() instanceof PsiMethod &&
             ((PsiMethod)element.getParent()).isConstructor() &&
             !firstStatementIsConstructorCall((PsiCodeBlock)element)) {
      // there is implicit parent constructor call
      final PsiMethod constructor = (PsiMethod)element.getParent();
      final PsiClass aClass = constructor.getContainingClass();
      final PsiClass superClass = aClass == null ? null : aClass.getSuperClass();
      final PsiMethod[] superConstructors = superClass == null ? PsiMethod.EMPTY_ARRAY : superClass.getConstructors();
      Set<PsiClassType> unhandled = new HashSet<PsiClassType>();
      for (PsiMethod superConstructor : superConstructors) {
        if (!superConstructor.hasModifierProperty(PsiModifier.PRIVATE) && superConstructor.getParameterList().getParametersCount() == 0) {
          final PsiClassType[] exceptionTypes = superConstructor.getThrowsList().getReferencedTypes();
          for (PsiClassType exceptionType : exceptionTypes) {
            if (!isUncheckedException(exceptionType) && !isHandled(element, exceptionType, topElement)) {
              unhandled.add(exceptionType);
            }
          }
          break;
        }
      }

      // plus all exceptions thrown in instance class initializers
      if (aClass != null) {
        final PsiClassInitializer[] initializers = aClass.getInitializers();
        final Set<PsiClassType> thrownByInitializer = new THashSet<PsiClassType>();
        for (PsiClassInitializer initializer : initializers) {
          if (initializer.hasModifierProperty(PsiModifier.STATIC)) continue;
          thrownByInitializer.clear();
          collectUnhandledExceptions(initializer.getBody(), initializer, thrownByInitializer, includeSelfCalls);
          for (PsiClassType thrown : thrownByInitializer) {
            if (!isHandled(constructor.getBody(), thrown, topElement)) {
              unhandled.add(thrown);
            }
          }
        }
      }
      unhandledExceptions = unhandled;
    }

    if (element instanceof PsiResourceVariable) {
      final List<PsiClassType> unhandled = getUnhandledCloserExceptions((PsiResourceVariable)element, topElement);
      if (!unhandled.isEmpty()) {
        if (unhandledExceptions == null) {
          unhandledExceptions = ContainerUtil.newArrayList(unhandled);
        }
        else {
          unhandledExceptions.addAll(unhandled);
        }
      }
    }

    if (unhandledExceptions != null) {
      if (foundExceptions == null) {
        foundExceptions = new THashSet<PsiClassType>();
      }
      foundExceptions.addAll(unhandledExceptions);
    }

    for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
      foundExceptions = collectUnhandledExceptions(child, topElement, foundExceptions, includeSelfCalls);
    }

    return foundExceptions;
  }

  @NotNull
  private static Collection<PsiClassType> getUnhandledExceptions(@NotNull PsiMethodReferenceExpression methodReferenceExpression,
                                                                 PsiElement topElement) {
    final JavaResolveResult resolveResult = methodReferenceExpression.advancedResolve(false);
    final PsiElement resolve = resolveResult.getElement();
    if (resolve instanceof PsiMethod) {
      return getUnhandledExceptions((PsiMethod)resolve, methodReferenceExpression, topElement, resolveResult.getSubstitutor());
    }
    return Collections.emptyList();
  }

  private static boolean firstStatementIsConstructorCall(@NotNull PsiCodeBlock constructorBody) {
    final PsiStatement[] statements = constructorBody.getStatements();
    if (statements.length == 0) return false;
    if (!(statements[0] instanceof PsiExpressionStatement)) return false;

    final PsiExpression expression = ((PsiExpressionStatement)statements[0]).getExpression();
    if (!(expression instanceof PsiMethodCallExpression)) return false;
    final PsiMethod method = (PsiMethod)((PsiMethodCallExpression)expression).getMethodExpression().resolve();
    return method != null && method.isConstructor();
  }

  @NotNull
  public static List<PsiClassType> getUnhandledExceptions(@NotNull PsiElement[] elements) {
    final List<PsiClassType> array = ContainerUtil.newArrayList();
    final PsiElementVisitor visitor = new JavaRecursiveElementWalkingVisitor() {
      @Override
      public void visitCallExpression(@NotNull PsiCallExpression expression) {
        addExceptions(array, getUnhandledExceptions(expression, null));
        visitElement(expression);
      }

      @Override
      public void visitThrowStatement(@NotNull PsiThrowStatement statement) {
        addExceptions(array, getUnhandledExceptions(statement, null));
        visitElement(statement);
      }

      @Override
      public void visitMethodReferenceExpression(@NotNull PsiMethodReferenceExpression expression) {
        addExceptions(array, getUnhandledExceptions(expression, null));
        visitElement(expression);
      }

      @Override
      public void visitResourceVariable(@NotNull PsiResourceVariable resourceVariable) {
        addExceptions(array, getUnhandledCloserExceptions(resourceVariable, null));
        visitElement(resourceVariable);
      }
    };

    for (PsiElement element : elements) {
      element.accept(visitor);
    }

    return array;
  }

  @NotNull
  public static List<PsiClassType> getUnhandledExceptions(PsiElement element) {
    if (element instanceof PsiCallExpression) {
      PsiCallExpression expression = (PsiCallExpression)element;
      return getUnhandledExceptions(expression, null);
    }
    else if (element instanceof PsiThrowStatement) {
      PsiThrowStatement throwStatement = (PsiThrowStatement)element;
      return getUnhandledExceptions(throwStatement, null);
    }
    else if (element instanceof PsiResourceVariable) {
      return getUnhandledCloserExceptions((PsiResourceVariable)element, null);
    }

    return getUnhandledExceptions(new PsiElement[]{element});
  }

  @NotNull
  public static List<PsiClassType> getUnhandledExceptions(@NotNull final PsiCallExpression methodCall, @Nullable final PsiElement topElement) {
    return getUnhandledExceptions(methodCall, topElement, true);
  }

  @NotNull
  public static List<PsiClassType> getUnhandledExceptions(@NotNull final PsiCallExpression methodCall,
                                                          @Nullable final PsiElement topElement,
                                                          final boolean includeSelfCalls) {
    final JavaResolveResult result = methodCall.resolveMethodGenerics();
    final PsiMethod method = (PsiMethod)result.getElement();
    final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(methodCall, PsiMethod.class);
    if (!includeSelfCalls && method == containingMethod) {
      return Collections.emptyList();
    }

    final PsiSubstitutor substitutor = result.getSubstitutor();
    if (method != null && !isArrayClone(method, methodCall) && methodCall instanceof PsiMethodCallExpression) {
      final PsiClassType[] thrownExceptions = method.getThrowsList().getReferencedTypes();
      if (thrownExceptions.length > 0) {
        final PsiFile containingFile = (containingMethod == null ? methodCall : containingMethod).getContainingFile();
        final MethodResolverProcessor processor = new MethodResolverProcessor((PsiMethodCallExpression)methodCall, containingFile);
        try {
          PsiScopesUtil.setupAndRunProcessor(processor, methodCall, false);
          final List<Pair<PsiMethod, PsiSubstitutor>> candidates = ContainerUtil.mapNotNull(
            processor.getResults(), new Function<CandidateInfo, Pair<PsiMethod, PsiSubstitutor>>() {
            @Override
            public Pair<PsiMethod, PsiSubstitutor> fun(CandidateInfo info) {
              PsiElement element = info.getElement();
              return element instanceof PsiMethod && MethodSignatureUtil.areSignaturesEqual(method, (PsiMethod)element)
                     ? Pair.create((PsiMethod)element, info.getSubstitutor()) : null;
            }
          });
          if (candidates.size() > 1) {
            final List<PsiClassType> ex = collectSubstituted(substitutor, thrownExceptions);
            for (Pair<PsiMethod, PsiSubstitutor> pair : candidates) {
              final PsiClassType[] exceptions = pair.first.getThrowsList().getReferencedTypes();
              if (exceptions.length == 0) {
                return getUnhandledExceptions(methodCall, topElement, PsiSubstitutor.EMPTY, PsiClassType.EMPTY_ARRAY);
              }
              retainExceptions(ex, collectSubstituted(pair.second, exceptions));
            }
            return getUnhandledExceptions(methodCall, topElement, PsiSubstitutor.EMPTY, ex.toArray(new PsiClassType[ex.size()]));
          }
        }
        catch (MethodProcessorSetupFailedException ignore) {
          return Collections.emptyList();
        }
      }
    }

    return getUnhandledExceptions(method, methodCall, topElement, substitutor);
  }

  public static void retainExceptions(List<PsiClassType> ex, List<PsiClassType> thrownEx) {
    final List<PsiClassType> replacement = new ArrayList<PsiClassType>();
    for (Iterator<PsiClassType> iterator = ex.iterator(); iterator.hasNext(); ) {
      PsiClassType classType = iterator.next();
      boolean found = false;
      for (PsiClassType psiClassType : thrownEx) {
        if (psiClassType.isAssignableFrom(classType)) {
          found = true;
          break;
        } else if (classType.isAssignableFrom(psiClassType)) {
          replacement.add(psiClassType);
          iterator.remove();
          found = true;
          break;
        }
      }
      if (!found) {
        iterator.remove();
      }
    }
    ex.addAll(replacement);
  }

  public static List<PsiClassType> collectSubstituted(PsiSubstitutor substitutor, PsiClassType[] thrownExceptions) {
    final List<PsiClassType> ex = new ArrayList<PsiClassType>();
    for (PsiClassType thrownException : thrownExceptions) {
      final PsiType psiType = substitutor.substitute(thrownException);
      if (psiType instanceof PsiClassType) {
        ex.add((PsiClassType)psiType);
      }
    }
    return ex;
  }

  @NotNull
  public static List<PsiClassType> getCloserExceptions(@NotNull final PsiResourceVariable resource) {
    final PsiMethod method = PsiUtil.getResourceCloserMethod(resource);
    return method != null ? getExceptionsByMethod(method, PsiSubstitutor.EMPTY) : Collections.<PsiClassType>emptyList();
  }

  @NotNull
  public static List<PsiClassType> getUnhandledCloserExceptions(@NotNull final PsiResourceVariable resource, @Nullable final PsiElement topElement) {
    final PsiMethod method = PsiUtil.getResourceCloserMethod(resource);
    return method != null ? getUnhandledExceptions(method, resource, topElement, PsiSubstitutor.EMPTY) : Collections.<PsiClassType>emptyList();
  }

  @NotNull
  public static List<PsiClassType> getUnhandledExceptions(@NotNull PsiThrowStatement throwStatement, @Nullable PsiElement topElement) {
    List<PsiClassType> unhandled = new SmartList<PsiClassType>();
    for (PsiType type : getPreciseThrowTypes(throwStatement.getException())) {
      List<PsiType> types = type instanceof PsiDisjunctionType ? ((PsiDisjunctionType)type).getDisjunctions() : Collections.singletonList(type);
      for (PsiType subType : types) {
        if (subType instanceof PsiClassType) {
          PsiClassType classType = (PsiClassType)subType;
          if (!isUncheckedException(classType) && !isHandled(throwStatement, classType, topElement)) {
            unhandled.add(classType);
          }
        }
      }
    }
    return unhandled;
  }

  @NotNull
  private static List<PsiType> getPreciseThrowTypes(@Nullable final PsiExpression expression) {
    if (expression instanceof PsiReferenceExpression) {
      final PsiElement target = ((PsiReferenceExpression)expression).resolve();
      if (target != null && PsiUtil.isCatchParameter(target)) {
        return ((PsiCatchSection)target.getParent()).getPreciseCatchTypes();
      }
    }

    if (expression != null) {
      final PsiType type = expression.getType();
      if (type != null) {
        return Arrays.asList(type);
      }
    }

    return Collections.emptyList();
  }

  @NotNull
  private static List<PsiClassType> getUnhandledExceptions(@Nullable PsiMethod method,
                                                           PsiElement element,
                                                           PsiElement topElement,
                                                           @NotNull PsiSubstitutor substitutor) {
    if (method == null || isArrayClone(method, element)) {
      return Collections.emptyList();
    }
    final PsiClassType[] referencedTypes = method.getThrowsList().getReferencedTypes();
    return getUnhandledExceptions(element, topElement, substitutor, referencedTypes);
  }

  private static List<PsiClassType> getUnhandledExceptions(PsiElement element,
                                                           PsiElement topElement,
                                                           PsiSubstitutor substitutor,
                                                           PsiClassType[] referencedTypes) {
    if (referencedTypes.length > 0) {
      List<PsiClassType> result = ContainerUtil.newArrayList();

      for (PsiClassType referencedType : referencedTypes) {
        final PsiType type = GenericsUtil.eliminateWildcards(substitutor.substitute(referencedType), false);
        if (!(type instanceof PsiClassType)) continue;
        PsiClassType classType = (PsiClassType)type;
        PsiClass exceptionClass = ((PsiClassType)type).resolve();
        if (exceptionClass == null) continue;

        if (isUncheckedException(classType)) continue;
        if (isHandled(element, classType, topElement)) continue;

        result.add((PsiClassType)type);
      }

      return result;
    }
    return Collections.emptyList();
  }

  private static boolean isArrayClone(@NotNull PsiMethod method, PsiElement element) {
    if (!method.getName().equals(CLONE_METHOD_NAME)) return false;
    PsiClass containingClass = method.getContainingClass();
    if (containingClass == null || !CommonClassNames.JAVA_LANG_OBJECT.equals(containingClass.getQualifiedName())) {
      return false;
    }
    if (element instanceof PsiMethodReferenceExpression) {
      final PsiMethodReferenceExpression methodCallExpression = (PsiMethodReferenceExpression)element;
      final PsiExpression qualifierExpression = methodCallExpression.getQualifierExpression();
      return qualifierExpression != null && qualifierExpression.getType() instanceof PsiArrayType;
    }
    if (!(element instanceof PsiMethodCallExpression)) return false;

    PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)element;
    final PsiExpression qualifierExpression = methodCallExpression.getMethodExpression().getQualifierExpression();
    return qualifierExpression != null && qualifierExpression.getType() instanceof PsiArrayType;
  }

  public static boolean isUncheckedException(@NotNull PsiClassType type) {
    return InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_LANG_RUNTIME_EXCEPTION) || InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_LANG_ERROR);
  }

  public static boolean isUncheckedExceptionOrSuperclass(@NotNull final PsiClassType type) {
    return isGeneralExceptionType(type) || isUncheckedException(type);
  }

  public static boolean isGeneralExceptionType(@NotNull final PsiType type) {
    final String canonicalText = type.getCanonicalText();
    return CommonClassNames.JAVA_LANG_THROWABLE.equals(canonicalText) ||
           CommonClassNames.JAVA_LANG_EXCEPTION.equals(canonicalText);
  }

  public static boolean isHandled(@NotNull PsiClassType exceptionType, @NotNull PsiElement throwPlace) {
    return isHandled(throwPlace, exceptionType, throwPlace.getContainingFile());
  }

  private static boolean isHandled(@Nullable PsiElement element, @NotNull PsiClassType exceptionType, PsiElement topElement) {
    if (element == null || element.getParent() == topElement || element.getParent() == null) return false;

    final PsiElement parent = element.getParent();

    if (parent instanceof PsiMethod) {
      PsiMethod method = (PsiMethod)parent;
      return isHandledByMethodThrowsClause(method, exceptionType);
    }
    else if (parent instanceof PsiClass) {
      // arguments to anon class constructor should be handled higher
      // like in void f() throws XXX { new AA(methodThrowingXXX()) { ... }; }
      return parent instanceof PsiAnonymousClass && isHandled(parent, exceptionType, topElement);
    }
    else if (parent instanceof PsiLambdaExpression) {
      final PsiType interfaceType = ((PsiLambdaExpression)parent).getFunctionalInterfaceType();
      return isDeclaredBySAMMethod(exceptionType, interfaceType);
    }
    else if (element instanceof PsiMethodReferenceExpression) {
      final PsiType interfaceType = ((PsiMethodReferenceExpression)element).getFunctionalInterfaceType();
      return isDeclaredBySAMMethod(exceptionType, interfaceType);
    }
    else if (parent instanceof PsiClassInitializer) {
      if (((PsiClassInitializer)parent).hasModifierProperty(PsiModifier.STATIC)) return false;
      // anonymous class initializers can throw any exceptions
      if (!(parent.getParent() instanceof PsiAnonymousClass)) {
        // exception thrown from within class instance initializer must be handled in every class constructor
        // check each constructor throws exception or superclass (there must be at least one)
        final PsiClass aClass = ((PsiClassInitializer)parent).getContainingClass();
        return areAllConstructorsThrow(aClass, exceptionType);
      }
    }
    else if (parent instanceof PsiTryStatement) {
      PsiTryStatement tryStatement = (PsiTryStatement)parent;
      if (tryStatement.getTryBlock() == element && isCaught(tryStatement, exceptionType)) {
        return true;
      }
      if (tryStatement.getResourceList() == element && isCaught(tryStatement, exceptionType)) {
        return true;
      }
      PsiCodeBlock finallyBlock = tryStatement.getFinallyBlock();
      if (element instanceof PsiCatchSection && finallyBlock != null && blockCompletesAbruptly(finallyBlock)) {
        // exception swallowed
        return true;
      }
    }
    else if (parent instanceof JavaCodeFragment) {
      JavaCodeFragment codeFragment = (JavaCodeFragment)parent;
      JavaCodeFragment.ExceptionHandler exceptionHandler = codeFragment.getExceptionHandler();
      return exceptionHandler != null && exceptionHandler.isHandledException(exceptionType);
    }
    else if (PsiImplUtil.isInServerPage(parent) && parent instanceof PsiFile) {
      return true;
    }
    else if (parent instanceof PsiFile) {
      return false;
    }
    else if (parent instanceof PsiField && ((PsiField)parent).getInitializer() == element) {
      final PsiClass aClass = ((PsiField)parent).getContainingClass();
      if (aClass != null && !(aClass instanceof PsiAnonymousClass) && !((PsiField)parent).hasModifierProperty(PsiModifier.STATIC)) {
        // exceptions thrown in field initializers should be thrown in all class constructors
        return areAllConstructorsThrow(aClass, exceptionType);
      }
    }
    return isHandled(parent, exceptionType, topElement);
  }

  private static boolean isDeclaredBySAMMethod(@NotNull PsiClassType exceptionType, @Nullable PsiType interfaceType) {
    if (interfaceType != null) {
      final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(interfaceType);
      if (interfaceMethod != null) {
        return isHandledByMethodThrowsClause(interfaceMethod, exceptionType);
      }
    }
    return true;
  }

  private static boolean areAllConstructorsThrow(@Nullable final PsiClass aClass, @NotNull PsiClassType exceptionType) {
    if (aClass == null) return false;
    final PsiMethod[] constructors = aClass.getConstructors();
    boolean thrown = constructors.length != 0;
    for (PsiMethod constructor : constructors) {
      if (!isHandledByMethodThrowsClause(constructor, exceptionType)) {
        thrown = false;
        break;
      }
    }
    return thrown;
  }

  private static boolean isCaught(@NotNull PsiTryStatement tryStatement, @NotNull PsiClassType exceptionType) {
    // if finally block completes abruptly, exception gets lost
    PsiCodeBlock finallyBlock = tryStatement.getFinallyBlock();
    if (finallyBlock != null && blockCompletesAbruptly(finallyBlock)) return true;

    final PsiParameter[] catchBlockParameters = tryStatement.getCatchBlockParameters();
    for (PsiParameter parameter : catchBlockParameters) {
      PsiType paramType = parameter.getType();
      if (paramType.isAssignableFrom(exceptionType)) return true;
    }

    return false;
  }

  private static boolean blockCompletesAbruptly(@NotNull final PsiCodeBlock finallyBlock) {
    try {
      ControlFlow flow = ControlFlowFactory.getInstance(finallyBlock.getProject()).getControlFlow(finallyBlock, LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance(), false);
      int completionReasons = ControlFlowUtil.getCompletionReasons(flow, 0, flow.getSize());
      if ((completionReasons & ControlFlowUtil.NORMAL_COMPLETION_REASON) == 0) return true;
    }
    catch (AnalysisCanceledException e) {
      return true;
    }
    return false;
  }

  private static boolean isHandledByMethodThrowsClause(@NotNull PsiMethod method, @NotNull PsiClassType exceptionType) {
    final PsiClassType[] referencedTypes = method.getThrowsList().getReferencedTypes();
    return isHandledBy(exceptionType, referencedTypes);
  }

  public static boolean isHandledBy(@NotNull PsiClassType exceptionType, @NotNull PsiClassType[] referencedTypes) {
    for (PsiClassType classType : referencedTypes) {
      if (classType.isAssignableFrom(exceptionType)) return true;
    }
    return false;
  }

  public static void sortExceptionsByHierarchy(@NotNull List<PsiClassType> exceptions) {
    if (exceptions.size() <= 1) return;
    sortExceptionsByHierarchy(exceptions.subList(1, exceptions.size()));
    for (int i=0; i<exceptions.size()-1;i++) {
      if (TypeConversionUtil.isAssignable(exceptions.get(i), exceptions.get(i+1))) {
        Collections.swap(exceptions, i,i+1);
      }
    }
  }
}


text:
package org.pcap4j.packet.factory;

import org.pcap4j.packet.CompressedPacket;
import org.pcap4j.packet.EncryptedPacket;
import org.pcap4j.packet.FragmentedPacket;
import org.pcap4j.packet.IllegalRawDataException;
import org.pcap4j.packet.IpV6ExtUnknownPacket;
import org.pcap4j.packet.Packet;
import org.pcap4j.packet.UnknownPacket;
import org.pcap4j.packet.namednumber.NotApplicable;

/**
 * @author Kaito Yamada
 * @since pcap4j 1.4.0
 */
public final class StaticNotApplicablePacketFactory
extends AbstractStaticPacketFactory<NotApplicable> {

  private static final StaticNotApplicablePacketFactory INSTANCE
    = new StaticNotApplicablePacketFactory();

  private StaticNotApplicablePacketFactory() {
    instantiaters.put(
      NotApplicable.UNKNOWN, new PacketInstantiater() {
        @Override
        public Packet newInstance(
          byte[] rawData, int offset, int length
        ) throws IllegalRawDataException {
          return UnknownPacket.newPacket(rawData, offset, length);
        }
        @Override
        public Class<UnknownPacket> getTargetClass() {
          return UnknownPacket.class;
        }
      }
    );
    instantiaters.put(
      NotApplicable.FRAGMENTED, new PacketInstantiater() {
        @Override
        public Packet newInstance(
          byte[] rawData, int offset, int length
        ) throws IllegalRawDataException {
          return FragmentedPacket.newPacket(rawData, offset, length);
        }
        @Override
        public Class<FragmentedPacket> getTargetClass() {
          return FragmentedPacket.class;
        }
      }
    );
    instantiaters.put(
      NotApplicable.UNKNOWN_IP_V6_EXTENSION, new PacketInstantiater() {
        @Override
        public Packet newInstance(
          byte[] rawData, int offset, int length
        ) throws IllegalRawDataException {
          return IpV6ExtUnknownPacket.newPacket(rawData, offset, length);
        }
        @Override
        public Class<IpV6ExtUnknownPacket> getTargetClass() {
          return IpV6ExtUnknownPacket.class;
        }
      }
    );
    instantiaters.put(
      NotApplicable.COMPRESSED, new PacketInstantiater() {
        @Override
        public Packet newInstance(
          byte[] rawData, int offset, int length
        ) throws IllegalRawDataException {
          return CompressedPacket.newPacket(rawData, offset, length);
        }
        @Override
        public Class<CompressedPacket> getTargetClass() {
          return CompressedPacket.class;
        }
      }
    );
    instantiaters.put(
      NotApplicable.ENCRYPTED, new PacketInstantiater() {
        @Override
        public Packet newInstance(
          byte[] rawData, int offset, int length
        ) throws IllegalRawDataException {
          return EncryptedPacket.newPacket(rawData, offset, length);
        }
        @Override
        public Class<EncryptedPacket> getTargetClass() {
          return EncryptedPacket.class;
        }
      }
    );
  };

  /**
   *
   * @return the singleton instance of StaticNaPacketFactory.
   */
  public static StaticNotApplicablePacketFactory getInstance() {
    return INSTANCE;
  }
}

redpajama_set_name: RedPajamaGithub


text:
import socketserver

class EchoTCPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        self.data = self.request.recv(1024).strip()
        print("{} wrote:".format(self.client_address[0]))
        print(self.data, flush=True)
        self.request.sendall(self.data)

if __name__ == "__main__":
    HOST, PORT = "0.0.0.0", 7
    server = socketserver.TCPServer((HOST, PORT), EchoTCPHandler)
    server.serve_forever()


text:
/**
 */

package org.apache.harmony.test.func.share;

import org.apache.harmony.share.DRLLogging;
import org.apache.harmony.share.LogFactory;

public class MyLog {

    static DRLLogging my_log = null ;

    public static void toMyLog(String str) {
        if ( my_log == null )
            my_log = new LogFactory().getLogger();

        my_log.add(str);
    }
}


text:
hello = "world"

def test_func():
    pass


text:
@interface IFStringTemplate()

- (void)parse:(NSString*)s;
- (EPStringTemplateBlock)textBlock:(NSString*)text;
- (EPStringTemplateBlock)refBlock:(NSString*)ref;

@end

#define URIEncode(s) ((NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(\
                        NULL, (__bridge CFStringRef)s, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8)))

@implementation IFStringTemplate

- (id)initWithString:(NSString *)s {
    self = [super init];
    if (self) {
        blocks = [[NSMutableArray alloc] init];
        [self parse:s];
    }
    return self;
}

- (void)parse:(NSString *)s {
    IFRegExp *regex = [[IFRegExp alloc] initWithPattern:@"^([^{]*)[{]([-a-zA-Z0-9_$.]+)[}](.*)$"];
    NSMutableArray *refs = [[NSMutableArray alloc] init];
    while (s) {
        NSArray* r = [regex match:s];
        if (r) {
            [blocks addObject:[self textBlock:[r objectAtIndex:1]]];
            NSString *ref = [r objectAtIndex:2];
            [blocks addObject:[self refBlock:ref]];
            [refs addObject:ref];
            s = [r objectAtIndex:3];
        }
        else {
            NSInteger i = [s rangeOfString:@"}"].location;
            if (i == NSNotFound) {
                [blocks addObject:[self textBlock:s]];
                s = nil;
            }
            else {
                [blocks addObject:[self textBlock:[s substringToIndex:i]]];
                s = [s substringFromIndex:i];
            }
        }
    }
    _refs = refs;
}

- (EPStringTemplateBlock)textBlock:(NSString *)text {
    return ^(id ctx, BOOL uriEncode) {
        return text;
    };
}

- (EPStringTemplateBlock)refBlock:(NSString *)ref {
    return ^(id ctx, BOOL uriEncode) {
        id val = nil;
        if ([ctx respondsToSelector:@selector(valueForKeyPath:)]) {
            @try {
                val = [[ctx valueForKeyPath:ref] description];
            }
            @catch (NSException *e) {
                val = e;
            }
            if (val && uriEncode) {
                val = URIEncode(val);
            }
        }
        return val ? val : @"";
    };
}

- (NSString *)render:(id)context {
    return [self render:context uriEncode:NO];
}

- (NSString*)render:(id)context uriEncode:(BOOL)uriEncode {
    NSMutableString* result = [[NSMutableString alloc] init];
    for (EPStringTemplateBlock block in blocks) {
        [result appendString:block( context, uriEncode )];
    }
    return result;
}

+ (IFStringTemplate*)templateWithString:(NSString *)s {
    return [[IFStringTemplate alloc] initWithString:s];
}

+ (NSString*)render:(NSString *)t context:(id)context {
    return [[IFStringTemplate templateWithString:t] render:context];
}

+ (NSString*)render:(NSString *)t context:(id)context uriEncode:(BOOL)uriEncode {
    return [[IFStringTemplate templateWithString:t] render:context uriEncode:uriEncode];
}

@end


text:
from rest_framework import authentication
from rest_framework.authtoken.serializers import AuthTokenSerializer

class AuthTokenSerializerAuthentication(authentication.BaseAuthentication):
    def authenticate(self, request):

        # if the user has made no attempt to add
        # credentials via the request body,
        # pass through so other authentication
        # can be tried
        username = request.data.get('username', None)
        password = request.data.get('password', None)
        if not username and not password:
            return None

        serializer = AuthTokenSerializer(data=request.data,
                                         context={'request': request})
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data['user']

        return(user, None)


text:
package tictactoe;

import tictactoe.agent.Agent;
import tictactoe.agent.HumanAgent;

public abstract class BoardRenderer {

    public void renderStateForAgent(Agent agent, int[] state) {
        if (agent instanceof HumanAgent) {
            print(state);
        }
    }

    public void renderWinner(Agent agent, int[] state) {
        print(state);
        if (agent instanceof HumanAgent) {
            System.out.println("--- Congrats you have won ---");
        } else {
            System.out.println("--- Computer has won ---");
        }
    }

    public void renderDraw(int[] state) {
        print(state);
        System.out.println("--- Draw ---");
    }

    static final String BOARD_FORMAT = "%n%s  %s  %s%n%s  %s  %s%n%s  %s  %s%n";

    private static void print(int[] state) {
        System.out.printf(BOARD_FORMAT,
                toChar(state, 0), toChar(state, 1), toChar(state, 2),
                toChar(state, 3), toChar(state, 4) ,toChar(state, 5),
                toChar(state, 6),toChar(state, 7),toChar(state, 8));
    }

    private static String toChar(int[] state, int p) {
        switch (state[p]) {
            case 0: return String.format(" %s ", Integer.toString(p + 1));
            case 1: return "(O)";
            case 2: return "(X)";
            default: throw new IllegalStateException(Integer.toString(state[p]));
        }
    }

    public static BoardRenderer Console = new BoardRenderer() {
    };

    public static BoardRenderer NoOp = new BoardRenderer() {
        @Override
        public void renderStateForAgent(Agent agent, int[] state) {
        }

        @Override
        public void renderWinner(Agent agent, int[] state) {
        }

        @Override
        public void renderDraw(int[] board) {
        }
    };
}


text:
// ==========================================================================
//  Squidex Headless CMS
// ==========================================================================
//  Copyright (c) Squidex UG (haftungsbeschraenkt)
//  All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System;
using Squidex.Domain.Apps.Core.Schemas;
using Squidex.Infrastructure;

namespace Squidex.Domain.Apps.Entities.Schemas
{
    public static class SchemaExtensions
    {
        public static NamedId<Guid> NamedId(this ISchemaEntity schema)
        {
            return new NamedId<Guid>(schema.Id, schema.Name);
        }

        public static string EscapePartition(this string value)
        {
            return value.Replace('-', '_');
        }

        public static string TypeName(this IField field)
        {
            return field.Name.ToPascalCase();
        }

        public static string TypeName(this ISchemaEntity schema)
        {
            return schema.SchemaDef.Name.ToPascalCase();
        }

        public static string DisplayName(this IField field)
        {
            return field.RawProperties.Label.WithFallback(field.TypeName());
        }

        public static string DisplayName(this ISchemaEntity schema)
        {
            return schema.SchemaDef.Properties.Label.WithFallback(schema.TypeName());
        }

        public static string TypeName(this Schema schema)
        {
            return schema.Name.ToPascalCase();
        }

        public static string DisplayName(this Schema schema)
        {
            return schema.Properties.Label.WithFallback(schema.TypeName());
        }
    }
}


text:
package net.anotheria.moskito.extensions.monitoring.parser;

import net.anotheria.moskito.core.stats.StatValue;
import net.anotheria.moskito.core.stats.StatValueTypes;
import net.anotheria.moskito.extensions.monitoring.metrics.IGenericMetrics;
import org.junit.Test;

import java.util.LinkedHashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * Test for StatusData bean.
 *
 * @author dzhmud
 */
public class StatusDataTest {

    final TestMetrics intMetric = new TestMetrics() {
        public Object parseValue(String value) {
            return Integer.parseInt(value);
        }
        public boolean isCorrectValue(Object value) {
            return value instanceof Integer;
        }
    };
    final TestMetrics stringMetric = new TestMetrics() {
        public Object parseValue(String value) {
            return value;
        }
        public boolean isCorrectValue(Object value) {
            return value instanceof String;
        }
    };

    @Test
    public void testCorrectScenario() {
        StatusData sd = new StatusData();
        assertEquals(null, sd.get(intMetric));

        int value0 = 12, value1 = 42;
        sd.put(intMetric, value0);
        assertEquals(value0, sd.get(intMetric));
        assertEquals(value0, sd.get(intMetric));

        sd.put(intMetric, value1);
        assertEquals(value1, sd.get(intMetric));
    }

    @Test
    public void testNullHandling() {
        StatusData sd = new StatusData();

        try {
            sd.put(null, 42);
            fail("Expected NullPointerException");
        } catch (IllegalArgumentException iae) {}
        try {
            sd.get(null);
            fail("Expected NullPointerException");
        } catch (IllegalArgumentException iae) {}

        assertEquals(null, sd.get(intMetric));

        sd.put(intMetric, 42);
        assertEquals(42, sd.get(intMetric));

        sd.put(intMetric, null);
        assertEquals(null, sd.get(intMetric));
    }

    @Test
    public void testWrongValueType() {
        try {
            new StatusData().put(intMetric, 42L);
            fail("Expected IllegalArgumentException");
        } catch (IllegalArgumentException e) {}
    }

    @Test
    public void testPutAll() {
        Map<TestMetrics,Object> map = new LinkedHashMap<>();
        //this will be put in status
        map.put(intMetric, 42);
        //this will be put in status
        map.put(stringMetric, 54);

        StatusData status = new StatusData();
        try {
            status.putAll(map);
            fail("Expected IllegalArgumentException");
        } catch (IllegalArgumentException expected) {}

        assertEquals(42, status.get(intMetric));

        map.put(intMetric, 11);
        map.put(stringMetric, "updated");
        status.putAll(map);
        assertEquals(11, status.get(intMetric));
        assertEquals("updated", status.get(stringMetric));

    }

    private static class TestMetrics implements IGenericMetrics {

        public String getValueName() {return null;}

        public String getCaption() {
            return null;
        }

        public String getShortExplanation() {
            return null;
        }

        public String getExplanation() {
            return null;
        }

        public StatValueTypes getType() {
            return null;
        }

        public StatValue createStatValue() {
            return null;
        }

        public boolean isRateMetric() {
            return false;
        }

        public boolean isDoubleValue() {
            return false;
        }

        public boolean isStringValue() {
            return false;
        }

        public boolean isIntegerValue() {
            return false;
        }

        public boolean isLongValue() {
            return false;
        }

        public Object parseValue(String value) {
            return null;
        }

        public boolean isCorrectValue(Object value) {
            return false;
        }

        public String getValueAsString(StatValue statValue, String intervalName) {
            return null;
        }

        public long getValueAsLong(StatValue statValue, String intervalName) {
            return 0;
        }

        public int getValueAsInt(StatValue statValue, String intervalName) {
            return 0;
        }

        public double getValueAsDouble(StatValue statValue, String intervalName) {
            return 0;
        }
    }
}


text:
using System;
using System.Text;
using System.IO;
using System.Collections.Generic;

namespace Portfoliospa.Utilities {

    public static class EasyCSV
    {

        public static IEnumerable<IList<string>> FromStream(Stream csv)
        {
            foreach (IList<string> item in FromStream(csv, ignoreFirstLineDefault)) yield return item;
        }

        public static IEnumerable<IList<string>> FromStream(Stream csv, bool ignoreFirstLine)
        {
            using (var rdr = new StreamReader(csv))
            {
                foreach (IList<string> item in FromReader(rdr, ignoreFirstLine)) yield return item;
            }
        }

        public static IEnumerable<IList<string>> FromReader(TextReader csv)
        {
            //Probably should have used TextReader instead of StreamReader
            foreach (IList<string> item in FromReader(csv, ignoreFirstLineDefault)) yield return item;
        }

        public static IEnumerable<IList<string>> FromReader(TextReader csv, bool ignoreFirstLine)
        {
            if (ignoreFirstLine) csv.ReadLine();

            IList<string> result = new List<string>();

            StringBuilder curValue = new StringBuilder();
            char c;
            c = (char)csv.Read();
            while (csv.Peek() != -1)
            {
                switch (c)
                {
                    case ',': //empty field
                        result.Add("");
                        c = (char)csv.Read();
                        break;
                    case '"': //qualified text
                    case '\'':
                        char q = c;
                        c = (char)csv.Read();
                        bool inQuotes = true;
                        while (inQuotes && csv.Peek() != -1)
                        {
                            if (c == q)
                            {
                                c = (char)csv.Read();
                                if (c != q)
                                    inQuotes = false;
                            }

                            if (inQuotes)
                            {
                                curValue.Append(c);
                                c = (char)csv.Read();
                            }
                        }
                        result.Add(curValue.ToString());
                        curValue = new StringBuilder();
                        if (c == ',') c = (char)csv.Read(); // either ',', newline, or endofstream
                        break;
                    case '\n': //end of the record
                    case '\r':
                        //potential bug here depending on what your line breaks look like
                        if (result.Count > 0) // don't return empty records
                        {
                            yield return result;
                            result = new List<string>();
                        }
                        c = (char)csv.Read();
                        break;
                    default: //normal unqualified text
                        while (c != ',' && c != '\r' && c != '\n' && csv.Peek() != -1)
                        {
                            curValue.Append(c);
                            c = (char)csv.Read();
                        }
                        result.Add(curValue.ToString());
                        curValue = new StringBuilder();
                        if (c == ',') c = (char)csv.Read(); //either ',', newline, or endofstream
                        break;
                }

            }
            if (curValue.Length > 0) //potential bug: I don't want to skip on a empty column in the last record if a caller really expects it to be there
                result.Add(curValue.ToString());
            if (result.Count > 0)
                yield return result;

        }
        private static bool ignoreFirstLineDefault = false;
    }
}


text:
require 'spec_helper'
module Domain
  describe Reuse, "reuse" do

    let(:object){ List.new([1, 2, 3]) }

    describe "size" do
      subject{ object.size }

      it{ should eq(3) }
    end

    describe "empty?" do
      subject{ object.empty? }

      it{ should be_false }
    end

  end
end


text:
'use strict';

const dbConfig = require('../database/config');

module.exports = appInfo => {
  const config = exports = {};

  config.siteFile = {
    '/favicon.ico': 'https://macacajs.github.io/assets/favicon.ico',
  };

  // use for cookie sign key, should change to your own and keep security
  config.keys = process.env.RELIABLE_SECRET_KEY || appInfo.name + '_1528180445670_8068';

  // add your config here
  config.middleware = [
    'forbidden',
    'hostRedirect',
    'openApiAuthorize',
    'authorize',
    'inject',
    'cors',
    'errorHandler',
  ];

  config.hostRedirect = {
    enable: !!process.env.RELIABLE_DEFAULT_HOST,
    defaultHost: process.env.RELIABLE_DEFAULT_HOST,
    ignore: [
      /^\/api\//,
    ],
  };

  config.authorize = {
    enable: process.env.RELIABLE_ENABLE_AUTHORIZE === 'Y',
    ignore: [
      '/snsAuthorize/callback/dingtalk',
      '/snsAuthorize/auth',
      '/snsAuthorize/signout',

      '/api/gw',
      '/api/latestBuild/:id',
      '/api/app/:id',
      '/api/build/:id',
    ],
    dingtalkAuth: {
      appid: process.env.RELIABLE_AUTH_DINGTALK_APPID,
      appsecret: process.env.RELIABLE_AUTH_DINGTALK_APPSECRET,
      callbackUrl: '/snsAuthorize/callback/dingtalk',
    },
  };

  config.openApiAuthorize = {
    enable: process.env.RELIABLE_ENABLE_OPENAPI_AUTHORIZE === 'Y',
    match: [
      '/api/gw',
      '/api/latestBuild/:id',
      '/api/app/:id',
      '/api/build/:id',
    ],
  };

  config.session = {
    maxAge: 48 * 3600 * 1000, // 48 hours
    renew: true, // keep session
  };

  config.modelQueryConfig = {
    pagination: {
      // default num
      num: 10,
    },
  };

  config.errorHandler = {
    match: '/api',
  };

  const reliableHost = process.env.RELIABLE_HOST || '127.0.0.1';

  config.reliableView = {
    serverUrl: '',
    reliableHost,
  };

  config.security = {
    csrf: {
      enable: false,
    },
    methodnoallow: {
      enable: false,
    },
  };

  config.sequelize = dbConfig.development;

  return config;
};


text:
package deployments

import (
	"fmt"
	"math/rand"
	"strings"
	"time"

	g "github.com/onsi/ginkgo"
	o "github.com/onsi/gomega"

	kapi "k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/labels"
	"k8s.io/kubernetes/pkg/util/wait"
	e2e "k8s.io/kubernetes/test/e2e/framework"

	deployutil "github.com/openshift/origin/pkg/deploy/util"
	exutil "github.com/openshift/origin/test/extended/util"
)

const deploymentRunTimeout = 5 * time.Minute

var _ = g.Describe("deploymentconfigs", func() {
	defer g.GinkgoRecover()
	var (
		oc                              = exutil.NewCLI("cli-deployment", exutil.KubeConfigPath())
		deploymentFixture               = exutil.FixturePath("testdata", "test-deployment-test.yaml")
		simpleDeploymentFixture         = exutil.FixturePath("testdata", "deployment-simple.yaml")
		customDeploymentFixture         = exutil.FixturePath("testdata", "custom-deployment.yaml")
		generationFixture               = exutil.FixturePath("testdata", "test-deployment.yaml")
		pausedDeploymentFixture         = exutil.FixturePath("testdata", "paused-deployment.yaml")
		failedHookFixture               = exutil.FixturePath("testdata", "failing-pre-hook.yaml")
		brokenDeploymentFixture         = exutil.FixturePath("testdata", "test-deployment-broken.yaml")
		historyLimitedDeploymentFixture = exutil.FixturePath("testdata", "deployment-history-limit.yaml")
		minReadySecondsFixture          = exutil.FixturePath("testdata", "deployment-min-ready-seconds.yaml")
	)

	g.Describe("when run iteratively", func() {
		g.AfterEach(func() {
			if !g.CurrentGinkgoTestDescription().Failed {
				return
			}

			if dc, rcs, pods, err := deploymentInfo(oc, "deployment-simple"); err == nil {
				e2e.Logf("DC: %#v", dc)
				e2e.Logf("  RCs: %#v", rcs)
				p, _ := deploymentPods(pods)
				for k, v := range p {
					for _, pod := range v {
						e2e.Logf("  Deployer: %s %#v", k, pod)
					}
				}
			}
		})

		g.It("should only deploy the last deployment [Conformance]", func() {
			_, err := oc.Run("create").Args("-f", simpleDeploymentFixture).Output()
			o.Expect(err).NotTo(o.HaveOccurred())

			iterations := 15
			for i := 0; i < iterations; i++ {
				if rand.Float32() < 0.2 {
					time.Sleep(time.Duration(rand.Float32() * rand.Float32() * float32(time.Second)))
				}
				switch n := rand.Float32(); {

				case n < 0.4:
					// trigger a new deployment
					e2e.Logf("%02d: triggering a new deployment with config change", i)
					out, err := oc.Run("set", "env").Args("dc/deployment-simple", fmt.Sprintf("A=%d", i)).Output()
					o.Expect(err).NotTo(o.HaveOccurred())
					o.Expect(out).To(o.ContainSubstring("updated"))

				case n < 0.7:
					// cancel any running deployment
					e2e.Logf("%02d: cancelling deployment", i)
					if out, err := oc.Run("deploy").Args("dc/deployment-simple", "--cancel").Output(); err != nil {
						// TODO: we should fix this
						if !strings.Contains(out, "the object has been modified") {
							o.Expect(err).NotTo(o.HaveOccurred())
						}
						e2e.Logf("--cancel deployment failed due to conflict: %v", err)
					}

				case n < 0.0:
					// delete the deployer pod - disabled because it forces the system to wait for the sync loop
					e2e.Logf("%02d: deleting one or more deployer pods", i)
					_, rcs, pods, err := deploymentInfo(oc, "deployment-simple")
					if err != nil {
						e2e.Logf("%02d: unable to get deployment info: %v", i, err)
						continue
					}
					all, err := deploymentPods(pods)
					if err != nil {
						e2e.Logf("%02d: unable to get deployment pods: %v", i, err)
						continue
					}
					if len(all) == 0 {
						e2e.Logf("%02d: no deployer pods", i)
						continue
					}
					top := len(rcs) - 1
					for j := top; i >= top-1 && j >= 0; j-- {
						pods, ok := all[rcs[j].Name]
						if !ok {
							e2e.Logf("%02d: no deployer pod for rc %q", i, rcs[j].Name)
							continue
						}
						for _, pod := range pods {
							e2e.Logf("%02d: deleting deployer pod %s", i, pod.Name)
							options := kapi.NewDeleteOptions(0)
							if rand.Float32() < 0.5 {
								options = nil
							}
							if err := oc.KubeREST().Pods(oc.Namespace()).Delete(pod.Name, options); err != nil {
								e2e.Logf("%02d: unable to delete deployer pod %q: %v", i, pod.Name, err)
							}
						}
					}
					e2e.Logf("%02d: triggering a new deployment with config change", i)
					out, err := oc.Run("set", "env").Args("dc/deployment-simple", fmt.Sprintf("A=%d", i)).Output()
					o.Expect(err).NotTo(o.HaveOccurred())
					o.Expect(out).To(o.ContainSubstring("updated"))

				default:
					// wait for the deployment to be running
					e2e.Logf("%02d: waiting for current deployment to start running", i)
					o.Expect(waitForLatestCondition(oc, "deployment-simple", deploymentRunTimeout, deploymentRunning)).NotTo(o.HaveOccurred())
				}
			}

			// trigger one more deployment, just in case we cancelled the latest output
			out, err := oc.Run("set", "env").Args("dc/deployment-simple", fmt.Sprintf("A=%d", iterations)).Output()
			o.Expect(err).NotTo(o.HaveOccurred())
			o.Expect(out).To(o.ContainSubstring("updated"))

			g.By("verifying all but terminal deployment is marked complete")
			o.Expect(waitForLatestCondition(oc, "deployment-simple", deploymentRunTimeout, deploymentReachedCompletion)).NotTo(o.HaveOccurred())
		})

		g.It("should immediately start a new deployment [Conformance]", func() {
			resource, name, err := createFixture(oc, simpleDeploymentFixture)
			o.Expect(err).NotTo(o.HaveOccurred())

			_, err = oc.Run("set", "env").Args(resource, "TRY=ONCE").Output()
			o.Expect(err).NotTo(o.HaveOccurred())

			g.By(fmt.Sprintf("by checking that the deployment config has the correct version"))
			err = wait.PollImmediate(500*time.Millisecond, 5*time.Second, func() (bool, error) {
				dc, _, _, err := deploymentInfo(oc, name)
				if err != nil {
					return false, nil
				}
				return dc.Status.LatestVersion == 2, nil
			})
			o.Expect(err).NotTo(o.HaveOccurred())

			g.By(fmt.Sprintf("by checking that the second deployment exists"))
			err = wait.PollImmediate(500*time.Millisecond, 30*time.Second, func() (bool, error) {
				_, rcs, _, err := deploymentInfo(oc, name)
				if err != nil {
					return false, nil
				}

				secondDeploymentExists := false
				for _, rc := range rcs {
					if rc.Name == deployutil.DeploymentNameForConfigVersion(name, 2) {
						secondDeploymentExists = true
						break
					}
				}

				return secondDeploymentExists, nil
			})
			o.Expect(err).NotTo(o.HaveOccurred())

			g.By(fmt.Sprintf("by checking that the first deployer was deleted and the second deployer exists"))
			err = wait.PollImmediate(500*time.Millisecond, 10*time.Second, func() (bool, error) {
				_, _, pods, err := deploymentInfo(oc, name)
				if err != nil {
					return false, nil
				}

				deploymentNamesToDeployers, err := deploymentPods(pods)
				if err != nil {
					return false, nil
				}

				firstDeploymentName := deployutil.DeploymentNameForConfigVersion(name, 1)
				firstDeployerRemoved := len(deploymentNamesToDeployers[firstDeploymentName]) == 0

				secondDeploymentName := deployutil.DeploymentNameForConfigVersion(name, 2)
				secondDeployerExists := len(deploymentNamesToDeployers[secondDeploymentName]) == 1

				return firstDeployerRemoved && secondDeployerExists, nil
			})
			o.Expect(err).NotTo(o.HaveOccurred())
		})
	})

	g.Describe("with test deployments", func() {
		g.It("should run a deployment to completion and then scale to zero [Conformance]", func() {
			out, err := oc.Run("create").Args("-f", deploymentFixture).Output()
			o.Expect(err).NotTo(o.HaveOccurred())

			o.Expect(waitForLatestCondition(oc, "deployment-test", deploymentRunTimeout, deploymentRunning)).NotTo(o.HaveOccurred())

			out, err = oc.Run("logs").Args("-f", "dc/deployment-test").Output()
			o.Expect(err).NotTo(o.HaveOccurred())
			g.By(fmt.Sprintf("checking the logs for substrings\n%s", out))
			o.Expect(out).To(o.ContainSubstring("deployment-test-1 to 2"))
			o.Expect(out).To(o.ContainSubstring("--> pre: Success"))
			o.Expect(out).To(o.ContainSubstring("--> Success"))

			g.By("verifying the deployment is marked complete and scaled to zero")
			o.Expect(waitForLatestCondition(oc, "deployment-test", deploymentRunTimeout, deploymentReachedCompletion)).NotTo(o.HaveOccurred())

			g.By("verifying that scaling does not result in new pods")
			out, err = oc.Run("scale").Args("dc/deployment-test", "--replicas=1").Output()
			o.Expect(err).NotTo(o.HaveOccurred())

			g.By("ensuring no scale up of the deployment happens")
			wait.Poll(100*time.Millisecond, 10*time.Second, func() (bool, error) {
				rc, err := oc.KubeREST().ReplicationControllers(oc.Namespace()).Get("deployment-test-1")
				o.Expect(err).NotTo(o.HaveOccurred())
				o.Expect(rc.Spec.Replicas).Should(o.BeEquivalentTo(0))
				o.Expect(rc.Status.Replicas).Should(o.BeEquivalentTo(0))
				return false, nil
			})

			g.By("verifying the scale is updated on the deployment config")
			config, err := oc.REST().DeploymentConfigs(oc.Namespace()).Get("deployment-test")
			o.Expect(err).NotTo(o.HaveOccurred())
			o.Expect(config.Spec.Replicas).Should(o.BeEquivalentTo(1))
			o.Expect(config.Spec.Test).Should(o.BeTrue())

			g.By("deploying a few more times")
			for i := 0; i < 3; i++ {
				out, err = oc.Run("deploy").Args("--latest", "--follow", "deployment-test").Output()
				o.Expect(err).NotTo(o.HaveOccurred())

				g.By("verifying the deployment is marked complete and scaled to zero")
				o.Expect(waitForLatestCondition(oc, "deployment-test", deploymentRunTimeout, deploymentReachedCompletion)).NotTo(o.HaveOccurred())

				g.By(fmt.Sprintf("checking the logs for substrings\n%s", out))
				o.Expect(out).To(o.ContainSubstring(fmt.Sprintf("deployment-test-%d up to 1", i+2)))
				o.Expect(out).To(o.ContainSubstring("--> pre: Success"))
				o.Expect(out).To(o.ContainSubstring("test pre hook executed"))
				o.Expect(out).To(o.ContainSubstring("--> Success"))
			}
		})
	})

	g.Describe("with enhanced status", func() {
		g.It("should include various info in status [Conformance]", func() {
			resource, name, err := createFixture(oc, simpleDeploymentFixture)
			o.Expect(err).NotTo(o.HaveOccurred())

			g.By("verifying the deployment is marked complete")
			o.Expect(waitForLatestCondition(oc, name, deploymentRunTimeout, deploymentReachedCompletion)).NotTo(o.HaveOccurred())

			g.By("verifying that status.replicas is set")
			replicas, err := oc.Run("get").Args(resource, "--output=jsonpath=\"{.status.replicas}\"").Output()
			o.Expect(err).NotTo(o.HaveOccurred())
			o.Expect(replicas).To(o.ContainSubstring("2"))
			g.By("verifying that status.updatedReplicas is set")
			updatedReplicas, err := oc.Run("get").Args(resource, "--output=jsonpath=\"{.status.updatedReplicas}\"").Output()
			o.Expect(err).NotTo(o.HaveOccurred())
			o.Expect(updatedReplicas).To(o.ContainSubstring("2"))
			g.By("verifying that status.availableReplicas is set")
			availableReplicas, err := oc.Run("get").Args(resource, "--output=jsonpath=\"{.status.availableReplicas}\"").Output()
			o.Expect(err).NotTo(o.HaveOccurred())
			o.Expect(availableReplicas).To(o.ContainSubstring("2"))
			g.By("verifying that status.unavailableReplicas is set")
			unavailableReplicas, err := oc.Run("get").Args(resource, "--output=jsonpath=\"{.status.unavailableReplicas}\"").Output()
			o.Expect(err).NotTo(o.HaveOccurred())
			o.Expect(unavailableReplicas).To(o.ContainSubstring("0"))
		})
	})

	g.Describe("with custom deployments", func() {
		g.It("should run the custom deployment steps [Conformance]", func() {
			out, err := oc.Run("create").Args("-f", customDeploymentFixture).Output()
			o.Expect(err).NotTo(o.HaveOccurred())

			o.Expect(waitForLatestCondition(oc, "custom-deployment", deploymentRunTimeout, deploymentRunning)).NotTo(o.HaveOccurred())

			out, err = oc.Run("deploy").Args("--follow", "dc/custom-deployment").Output()
			o.Expect(err).NotTo(o.HaveOccurred())

			g.By("verifying the deployment is marked complete")
			o.Expect(waitForLatestCondition(oc, "custom-deployment", deploymentRunTimeout, deploymentReachedCompletion)).NotTo(o.HaveOccurred())

			g.By(fmt.Sprintf("checking the logs for substrings\n%s", out))
			o.Expect(out).To(o.ContainSubstring("--> pre: Running hook pod ..."))
			o.Expect(out).To(o.ContainSubstring("test pre hook executed"))
			o.Expect(out).To(o.ContainSubstring("--> Scaling custom-deployment-1 to 2"))
			o.Expect(out).To(o.ContainSubstring("--> Reached 50%"))
			o.Expect(out).To(o.ContainSubstring("Halfway"))
			o.Expect(out).To(o.ContainSubstring("Finished"))
			o.Expect(out).To(o.ContainSubstring("--> Success"))
		})
	})

	g.Describe("viewing rollout history", func() {
		g.It("should print the rollout history [Conformance]", func() {
			resource, name, err := createFixture(oc, simpleDeploymentFixture)
			o.Expect(err).NotTo(o.HaveOccurred())
			o.Expect(waitForLatestCondition(oc, name, deploymentRunTimeout, deploymentReachedCompletion)).NotTo(o.HaveOccurred())

			config, err := oc.REST().DeploymentConfigs(oc.Namespace()).Get(name)
			o.Expect(err).NotTo(o.HaveOccurred())
			one := int64(1)
			config.Spec.Template.Spec.TerminationGracePeriodSeconds = &one
			_, err = oc.REST().DeploymentConfigs(oc.Namespace()).Update(config)
			// TODO: Retry on update conflicts
			o.Expect(err).NotTo(o.HaveOccurred())
			o.Expect(waitForLatestCondition(oc, name, deploymentRunTimeout, deploymentReachedCompletion)).NotTo(o.HaveOccurred())

			out, err := oc.Run("rollout").Args("history", resource).Output()
			o.Expect(err).NotTo(o.HaveOccurred())
			g.By(fmt.Sprintf("checking the history for substrings\n%s", out))
			o.Expect(out).To(o.ContainSubstring("deploymentconfigs \"deployment-simple\" history viewed"))
			o.Expect(out).To(o.ContainSubstring("REVISION	STATUS		CAUSE"))
			o.Expect(out).To(o.ContainSubstring("1		Complete	caused by a config change"))
			o.Expect(out).To(o.ContainSubstring("2		Complete	caused by a config change"))
		})
	})

	g.Describe("generation", func() {
		g.It("should deploy based on a status version bump [Conformance]", func() {
			resource, name, err := createFixture(oc, generationFixture)
			o.Expect(err).NotTo(o.HaveOccurred())

			g.By("verifying that both latestVersion and generation are updated")
			version, err := oc.Run("get").Args(resource, "--output=jsonpath=\"{.status.latestVersion}\"").Output()
			version = strings.Trim(version, "\"")
			o.Expect(err).NotTo(o.HaveOccurred())
			g.By(fmt.Sprintf("checking the latest version for %s: %s", resource, version))
			o.Expect(version).To(o.ContainSubstring("1"))
			generation, err := oc.Run("get").Args(resource, "--output=jsonpath=\"{.metadata.generation}\"").Output()
			o.Expect(err).NotTo(o.HaveOccurred())
			g.By(fmt.Sprintf("checking the generation for %s: %s", resource, generation))
			o.Expect(generation).To(o.ContainSubstring("1"))

			g.By("verifying the deployment is marked complete")
			err = wait.Poll(100*time.Millisecond, 1*time.Minute, func() (bool, error) {
				rc, err := oc.KubeREST().ReplicationControllers(oc.Namespace()).Get(name + "-" + version)
				o.Expect(err).NotTo(o.HaveOccurred())
				return deployutil.IsTerminatedDeployment(rc), nil
			})
			o.Expect(err).NotTo(o.HaveOccurred())

			g.By("verifying that scaling updates the generation")
			_, err = oc.Run("scale").Args(resource, "--replicas=2").Output()
			o.Expect(err).NotTo(o.HaveOccurred())
			generation, err = oc.Run("get").Args(resource, "--output=jsonpath=\"{.metadata.generation}\"").Output()
			o.Expect(err).NotTo(o.HaveOccurred())
			g.By(fmt.Sprintf("checking the generation for %s: %s", resource, generation))
			o.Expect(generation).To(o.ContainSubstring("2"))

			g.By("deploying a second time [new client]")
			_, err = oc.Run("deploy").Args("--latest", name).Output()
			o.Expect(err).NotTo(o.HaveOccurred())

			g.By("verifying that both latestVersion and generation are updated")
			version, err = oc.Run("get").Args(resource, "--output=jsonpath=\"{.status.latestVersion}\"").Output()
			version = strings.Trim(version, "\"")
			o.Expect(err).NotTo(o.HaveOccurred())
			g.By(fmt.Sprintf("checking the latest version for %s: %s", resource, version))
			o.Expect(version).To(o.ContainSubstring("2"))
			generation, err = oc.Run("get").Args(resource, "--output=jsonpath=\"{.metadata.generation}\"").Output()
			o.Expect(err).NotTo(o.HaveOccurred())
			g.By(fmt.Sprintf("checking the generation for %s: %s", resource, generation))
			o.Expect(generation).To(o.ContainSubstring("3"))

			g.By("verifying that observedGeneration equals generation")
			err = wait.Poll(1*time.Second, 1*time.Minute, func() (bool, error) {
				dc, _, _, err := deploymentInfo(oc, name)
				o.Expect(err).NotTo(o.HaveOccurred())
				return deployutil.HasSynced(dc), nil
			})
		})
	})

	g.Describe("paused", func() {
		g.It("should disable actions on deployments [Conformance]", func() {
			resource, name, err := createFixture(oc, pausedDeploymentFixture)
			o.Expect(err).NotTo(o.HaveOccurred())

			_, rcs, _, err := deploymentInfo(oc, name)
			o.Expect(err).NotTo(o.HaveOccurred())
			if len(rcs) != 0 {
				o.Expect(fmt.Errorf("expected no deployment, found %#v", rcs[0])).NotTo(o.HaveOccurred())
			}

			g.By("verifying that we cannot start a new deployment")
			out, err := oc.Run("deploy").Args(resource, "--latest").Output()
			o.Expect(err).To(o.HaveOccurred())
			o.Expect(out).To(o.ContainSubstring("cannot deploy a paused deployment config"))

			g.By("verifying that we cannot cancel a deployment")
			out, err = oc.Run("deploy").Args(resource, "--cancel").Output()
			o.Expect(err).To(o.HaveOccurred())
			o.Expect(out).To(o.ContainSubstring("cannot cancel a paused deployment config"))

			g.By("verifying that we cannot retry a deployment")
			out, err = oc.Run("deploy").Args(resource, "--retry").Output()
			o.Expect(err).To(o.HaveOccurred())
			o.Expect(out).To(o.ContainSubstring("cannot retry a paused deployment config"))

			g.By("verifying that we cannot rollback a deployment")
			out, err = oc.Run("rollback").Args(resource, "--to-version", "1").Output()
			o.Expect(err).To(o.HaveOccurred())
			o.Expect(out).To(o.ContainSubstring("cannot rollback a paused deployment config"))

			dc, rcs, _, err := deploymentInfo(oc, name)
			o.Expect(err).NotTo(o.HaveOccurred())
			if len(rcs) != 0 {
				o.Expect(fmt.Errorf("expected no deployment, found %#v", rcs[0])).NotTo(o.HaveOccurred())
			}

			dc.Spec.Paused = false
			_, err = oc.REST().DeploymentConfigs(dc.Namespace).Update(dc)
			// TODO: Retry on update conflicts
			o.Expect(err).NotTo(o.HaveOccurred())
			o.Expect(waitForLatestCondition(oc, name, deploymentRunTimeout, deploymentReachedCompletion)).NotTo(o.HaveOccurred())
		})
	})

	g.Describe("with failing hook", func() {
		g.It("should get all logs from retried hooks [Conformance]", func() {
			resource, name, err := createFixture(oc, failedHookFixture)
			o.Expect(err).NotTo(o.HaveOccurred())

			o.Expect(waitForLatestCondition(oc, name, deploymentRunTimeout, deploymentPreHookRetried)).NotTo(o.HaveOccurred())

			out, err := oc.Run("logs").Args(resource).Output()
			o.Expect(err).NotTo(o.HaveOccurred())

			g.By(fmt.Sprintf("checking the logs for substrings\n%s", out))
			o.Expect(out).To(o.ContainSubstring("--> pre: Running hook pod ..."))
			o.Expect(out).To(o.ContainSubstring("no such file or directory"))
			o.Expect(out).To(o.ContainSubstring("--> pre: Retrying hook pod (retry #1)"))
		})
	})

	g.Describe("rolled back", func() {
		g.It("should rollback to an older deployment [Conformance]", func() {
			resource, name, err := createFixture(oc, simpleDeploymentFixture)
			o.Expect(err).NotTo(o.HaveOccurred())

			o.Expect(waitForLatestCondition(oc, name, deploymentRunTimeout, deploymentReachedCompletion)).NotTo(o.HaveOccurred())

			_, err = oc.Run("deploy").Args(name, "--latest").Output()
			o.Expect(err).NotTo(o.HaveOccurred())

			o.Expect(waitForLatestCondition(oc, name, deploymentRunTimeout, deploymentReachedCompletion)).NotTo(o.HaveOccurred())

			g.By("verifying that we are on the second version")
			version, err := oc.Run("get").Args(resource, "--output=jsonpath=\"{.status.latestVersion}\"").Output()
			o.Expect(err).NotTo(o.HaveOccurred())
			version = strings.Trim(version, "\"")
			o.Expect(version).To(o.ContainSubstring("2"))

			g.By("verifying that we can rollback")
			_, err = oc.Run("rollout").Args("undo", resource).Output()
			o.Expect(err).NotTo(o.HaveOccurred())

			o.Expect(waitForLatestCondition(oc, name, deploymentRunTimeout, deploymentReachedCompletion)).NotTo(o.HaveOccurred())

			g.By("verifying that we are on the third version")
			version, err = oc.Run("get").Args(resource, "--output=jsonpath=\"{.status.latestVersion}\"").Output()
			o.Expect(err).NotTo(o.HaveOccurred())
			version = strings.Trim(version, "\"")
			o.Expect(version).To(o.ContainSubstring("3"))
		})
	})

	g.Describe("reaper", func() {
		g.It("should delete all failed deployer pods and hook pods [Conformance]", func() {
			resource, name, err := createFixture(oc, brokenDeploymentFixture)
			o.Expect(err).NotTo(o.HaveOccurred())

			g.By("waiting for the deployment to complete")
			err = waitForLatestCondition(oc, name, deploymentRunTimeout, deploymentReachedCompletion)
			o.Expect(err).To(o.HaveOccurred())

			g.By("fetching the deployer pod")
			out, err := oc.Run("get").Args("pod", fmt.Sprintf("%s-1-deploy", name)).Output()
			o.Expect(err).NotTo(o.HaveOccurred())
			o.Expect(out).To(o.ContainSubstring("Error"))

			g.By("fetching the pre-hook pod")
			out, err = oc.Run("get").Args("pod", fmt.Sprintf("%s-1-hook-pre", name)).Output()
			o.Expect(err).NotTo(o.HaveOccurred())
			o.Expect(out).To(o.ContainSubstring("Error"))

			g.By("deleting the deployment config")
			out, err = oc.Run("delete").Args(resource).Output()
			o.Expect(err).NotTo(o.HaveOccurred())

			g.By("fetching the deployer pod")
			out, err = oc.Run("get").Args("pod", fmt.Sprintf("%s-1-deploy", name)).Output()
			o.Expect(err).To(o.HaveOccurred())
			o.Expect(out).To(o.ContainSubstring("not found"))

			g.By("fetching the pre-hook pod")
			out, err = oc.Run("get").Args("pod", fmt.Sprintf("%s-1-hook-pre", name)).Output()
			o.Expect(err).To(o.HaveOccurred())
			o.Expect(out).To(o.ContainSubstring("not found"))
		})
	})

	g.Describe("with revision history limits", func() {
		g.It("should never persist more old deployments than acceptable after being observed by the controller [Conformance]", func() {
			revisionHistoryLimit := 3 // as specified in the fixture

			_, err := oc.Run("create").Args("-f", historyLimitedDeploymentFixture).Output()
			o.Expect(err).NotTo(o.HaveOccurred())

			iterations := 10
			for i := 0; i < iterations; i++ {
				o.Expect(waitForLatestCondition(oc, "history-limit", deploymentRunTimeout, deploymentReachedCompletion)).NotTo(o.HaveOccurred(),
					"the current deployment needs to have finished before attempting to trigger a new deployment through configuration change")
				e2e.Logf("%02d: triggering a new deployment with config change", i)
				out, err := oc.Run("set", "env").Args("dc/history-limit", fmt.Sprintf("A=%d", i)).Output()
				o.Expect(err).NotTo(o.HaveOccurred())
				o.Expect(out).To(o.ContainSubstring("updated"))
			}

			o.Expect(waitForLatestCondition(oc, "history-limit", deploymentRunTimeout, checkDeploymentConfigHasSynced)).NotTo(o.HaveOccurred(),
				"the controller needs to have synced with the updated deployment configuration before checking that the revision history limits are being adhered to")
			deploymentConfig, deployments, _, err := deploymentInfo(oc, "history-limit")
			o.Expect(err).NotTo(o.HaveOccurred())
			// sanity check to ensure that the following asertion on the amount of old deployments is valid
			o.Expect(*deploymentConfig.Spec.RevisionHistoryLimit).To(o.Equal(int32(revisionHistoryLimit)))

			// we need to filter out any deployments that we don't care about,
			// namely the active deployment and any newer deployments
			oldDeployments := deployutil.DeploymentsForCleanup(deploymentConfig, deployments)

			// we should not have more deployments than acceptable
			o.Expect(len(oldDeployments)).To(o.BeNumerically("<=", revisionHistoryLimit))

			// the deployments we continue to keep should be the latest ones
			for _, deployment := range oldDeployments {
				o.Expect(deployutil.DeploymentVersionFor(&deployment)).To(o.BeNumerically(">", iterations-revisionHistoryLimit))
			}
		})
	})

	g.Describe("with minimum ready seconds set", func() {
		g.It("should not transition the deployment to Complete before satisfied [Conformance]", func() {
			_, name, err := createFixture(oc, minReadySecondsFixture)
			o.Expect(err).NotTo(o.HaveOccurred())

			g.By("verifying the deployment is marked running")
			o.Expect(waitForLatestCondition(oc, name, deploymentRunTimeout, deploymentRunning)).NotTo(o.HaveOccurred())

			g.By("verifying that all pods are ready")
			config, err := oc.REST().DeploymentConfigs(oc.Namespace()).Get(name)
			o.Expect(err).NotTo(o.HaveOccurred())

			selector := labels.Set(config.Spec.Selector).AsSelector()
			opts := kapi.ListOptions{LabelSelector: selector}
			ready := 0
			if err := wait.PollImmediate(500*time.Millisecond, 1*time.Minute, func() (bool, error) {
				pods, err := oc.KubeREST().Pods(oc.Namespace()).List(opts)
				if err != nil {
					return false, nil
				}

				ready = 0
				for i := range pods.Items {
					pod := pods.Items[i]
					if kapi.IsPodReady(&pod) {
						ready++
					}
				}

				return len(pods.Items) == ready, nil
			}); err != nil {
				o.Expect(fmt.Errorf("deployment config %q never became ready (ready: %d, desired: %d)",
					config.Name, ready, config.Spec.Replicas)).NotTo(o.HaveOccurred())
			}

			g.By("verifying that the deployment is still running")
			latestName := deployutil.DeploymentNameForConfigVersion(name, config.Status.LatestVersion)
			latest, err := oc.KubeREST().ReplicationControllers(oc.Namespace()).Get(latestName)
			o.Expect(err).NotTo(o.HaveOccurred())
			if deployutil.IsTerminatedDeployment(latest) {
				o.Expect(fmt.Errorf("expected deployment %q not to have terminated", latest.Name)).NotTo(o.HaveOccurred())
			}
			o.Expect(waitForLatestCondition(oc, name, deploymentRunTimeout, deploymentRunning)).NotTo(o.HaveOccurred())
		})
	})
})


text:
[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
[![Build Status][travis-image]][travis-url]
[![Test Coverage][coveralls-image]][coveralls-url]
[![Gratipay][gratipay-image]][gratipay-url]

HTTP request logger middleware for node.js

> Named after [Dexter](http://en.wikipedia.org/wiki/Dexter_Morgan), a show you should not watch until completion.

## API

```js
var morgan = require('morgan')
```

### morgan(format, options)

Create a new morgan logger middleware function using the given `format` and `options`.
The `format` argument may be a string of a predefined name (see below for the names),
a string of a format string, or a function that will produce a log entry.

#### Options

Morgan accepts these properties in the options object.

#### buffer

Buffer duration before writing logs to the `stream`, defaults to `false`. When
set to `true`, defaults to `1000 ms`.

#### immediate

Write log line on request instead of response. This means that a requests will
be logged even if the server crashes, _but data from the response (like the
response code, content length, etc.) cannot be logged_.

##### skip

Function to determine if logging is skipped, defaults to `false`. This function
will be called as `skip(req, res)`.

```js
// EXAMPLE: only log error responses
morgan('combined', {
  skip: function (req, res) { return res.statusCode < 400 }
})
```

##### stream

Output stream for writing log lines, defaults to `process.stdout`.

#### Predefined Formats

There are various pre-defined formats provided:

##### combined

Standard Apache combined log output.

```
:remote-addr - :remote-user [:date] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"
```

##### common

Standard Apache common log output.

```
:remote-addr - :remote-user [:date] ":method :url HTTP/:http-version" :status :res[content-length]
```

##### dev

Concise output colored by response status for development use. The `:status`
token will be colored red for server error codes, yellow for client error
codes, cyan for redirection codes, and uncolored for all other codes.

```
:method :url :status :response-time ms - :res[content-length]
```

##### short

Shorter than default, also including response time.

```
:remote-addr :remote-user :method :url HTTP/:http-version :status :res[content-length] - :response-time ms
```

##### tiny

The minimal output.

```
:method :url :status :res[content-length] - :response-time ms
```

#### Tokens

- `:req[header]` ex: `:req[Accept]`
- `:res[header]` ex: `:res[Content-Length]`
- `:http-version`
- `:response-time`
- `:remote-addr`
- `:remote-user`
- `:date`
- `:method`
- `:url`
- `:referrer`
- `:user-agent`
- `:status`

To define a token, simply invoke `morgan.token()` with the name and a callback function. The value returned is then available as ":type" in this case:
```js
morgan.token('type', function(req, res){ return req.headers['content-type']; })
```

## Examples

### express/connect

Simple app that will log all request in the Apache combined format to STDOUT

```js
var express = require('express')
var morgan = require('morgan')

var app = express()

app.use(morgan('combined'))

app.get('/', function (req, res) {
  res.send('hello, world!')
})
```

### vanilla http server

Simple app that will log all request in the Apache combined format to STDOUT

```js
var finalhandler = require('finalhandler')
var http = require('http')
var morgan = require('morgan')

// create "middleware"
var logger = morgan('combined')

http.createServer(function (req, res) {
  var done = finalhandler(req, res)
  logger(req, res, function (err) {
    if (err) return done(err)

    // respond to request
    res.setHeader('content-type', 'text/plain')
    res.end('hello, world!')
  })
})
```

### write logs to a file

Simple app that will log all request in the Apache combined format to the file "access.log"

```js
var express = require('express')
var fs = require('fs')
var morgan = require('morgan')

var app = express()

// create a write stream (in append mode)
var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a'})

// setup the logger
app.use(morgan('combined', {stream: accessLogStream}))

app.get('/', function (req, res) {
  res.send('hello, world!')
})
```

## License

[MIT](LICENSE)

[npm-image]: https://img.shields.io/npm/v/morgan.svg?style=flat
[npm-url]: https://npmjs.org/package/morgan
[travis-image]: https://img.shields.io/travis/expressjs/morgan.svg?style=flat
[travis-url]: https://travis-ci.org/expressjs/morgan
[coveralls-image]: https://img.shields.io/coveralls/expressjs/morgan.svg?style=flat
[coveralls-url]: https://coveralls.io/r/expressjs/morgan?branch=master
[downloads-image]: http://img.shields.io/npm/dm/morgan.svg?style=flat
[downloads-url]: https://npmjs.org/package/morgan
[gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg?style=flat
[gratipay-url]: https://www.gratipay.com/dougwilson/


text:
"""CNN model with Mel spectrum."""
from kws_streaming.layers import modes
from kws_streaming.layers import quantize
from kws_streaming.layers import speech_features
from kws_streaming.layers import stream
from kws_streaming.layers.compat import tf
import kws_streaming.models.model_utils as utils
from tensorflow_model_optimization.python.core.quantization.keras import quantize_layer
from tensorflow_model_optimization.python.core.quantization.keras.quantizers import AllValuesQuantizer

def model_parameters(parser_nn):
  """Covolutional Neural Network(CNN) model parameters."""

  parser_nn.add_argument(
      '--cnn_filters',
      type=str,
      default='64,64,64,64,128,64,128',
      help='Number of output filters in the convolution layers',
  )
  parser_nn.add_argument(
      '--cnn_kernel_size',
      type=str,
      default='(3,3),(5,3),(5,3),(5,3),(5,2),(5,1),(10,1)',
      help='Heights and widths of the 2D convolution window',
  )
  parser_nn.add_argument(
      '--cnn_act',
      type=str,
      default="'relu','relu','relu','relu','relu','relu','relu'",
      help='Activation function in the convolution layers',
  )
  parser_nn.add_argument(
      '--cnn_dilation_rate',
      type=str,
      default='(1,1),(1,1),(2,1),(1,1),(2,1),(1,1),(2,1)',
      help='Dilation rate to use for dilated convolutions',
  )
  parser_nn.add_argument(
      '--cnn_strides',
      type=str,
      default='(1,1),(1,1),(1,1),(1,1),(1,1),(1,1),(1,1)',
      help='Strides of the convolution layers along the height and width',
  )
  parser_nn.add_argument(
      '--dropout1',
      type=float,
      default=0.5,
      help='Percentage of data dropped',
  )
  parser_nn.add_argument(
      '--units2',
      type=str,
      default='128,256',
      help='Number of units in the last set of hidden layers',
  )
  parser_nn.add_argument(
      '--act2',
      type=str,
      default="'linear','relu'",
      help='Activation function of the last set of hidden layers',
  )

def model(flags):
  """CNN model.

  It is based on paper:
  Convolutional Neural Networks for Small-footprint Keyword Spotting
  http://www.isca-speech.org/archive/interspeech_2015/papers/i15_1478.pdf
  Model topology is similar with "Hello Edge: Keyword Spotting on
  Microcontrollers" https://arxiv.org/pdf/1711.07128.pdf

  Supports quantization aware training with TF Model Optimization Toolkit
  including the experimental n-bit scheme.

  Args:
    flags: data/model parameters

  Returns:
    Keras model for training
  """

  input_audio = tf.keras.layers.Input(
      shape=modes.get_input_data_shape(flags, modes.Modes.TRAINING),
      batch_size=flags.batch_size)
  net = input_audio

  if flags.preprocess == 'raw':
    # it is a self contained model, user need to feed raw audio only
    net = speech_features.SpeechFeatures(
        speech_features.SpeechFeatures.get_params(flags))(
            net)

  if flags.quantize:
    net = quantize_layer.QuantizeLayer(
        AllValuesQuantizer(
            num_bits=8, per_axis=False, symmetric=False, narrow_range=False))(
                net)

  net = tf.keras.backend.expand_dims(net)
  for filters, kernel_size, activation, dilation_rate, strides in zip(
      utils.parse(flags.cnn_filters), utils.parse(flags.cnn_kernel_size),
      utils.parse(flags.cnn_act), utils.parse(flags.cnn_dilation_rate),
      utils.parse(flags.cnn_strides)):
    net = stream.Stream(
        cell=quantize.quantize_layer(
            tf.keras.layers.Conv2D(
                filters=filters,
                kernel_size=kernel_size,
                dilation_rate=dilation_rate,
                activation='linear',
                strides=strides),
            flags=flags,
            quantize_config=quantize.get_conv_bn_quantize_config(flags=flags)),
        pad_time_dim='causal',
        use_one_step=False)(
            net)
    net = quantize.quantize_layer(
        tf.keras.layers.BatchNormalization(),
        flags=flags,
        quantize_config=quantize.get_no_op_quantize_config(flags=flags)
        )(net)
    net = quantize.quantize_layer(
        tf.keras.layers.Activation(activation),
        flags=flags,
        )(net)

  net = stream.Stream(
      cell=quantize.quantize_layer(
          tf.keras.layers.Flatten(),
          flags=flags,
          ))(
              net)

  net = tf.keras.layers.Dropout(rate=flags.dropout1)(net)

  for units, activation in zip(
      utils.parse(flags.units2), utils.parse(flags.act2)):
    net = quantize.quantize_layer(
        tf.keras.layers.Dense(units=units, activation=activation),
        flags=flags,
        )(
            net)

  net = quantize.quantize_layer(
      tf.keras.layers.Dense(units=flags.label_count),
      flags=flags,
      )(
          net)
  if flags.return_softmax:
    net = quantize.quantize_layer(
        tf.keras.layers.Activation('softmax'),
        flags=flags,
        )(
            net)
  return tf.keras.Model(input_audio, net)


text:
namespace BlackFox.U2F.Gnubby
{
    public struct KeyRequest<TRequest>
    {
        public TRequest Request { get; }
        public InteractionFlags Flags { get; }

        public KeyRequest(TRequest request, InteractionFlags flags)
        {
            Request = request;
            Flags = flags;
        }
    }
}


text:
import Helmet from 'react-helmet'

const helmet = () => session => {
  session.on('server', render => {
    render()
    const data = Helmet.renderStatic()
    session.helmet = data
    Object.assign(session.document.htmlProps, data.htmlAttributes.toComponent())
    Object.assign(session.document.bodyProps, data.bodyAttributes.toComponent())
    session.document.head.push(
      ...data.title.toComponent(),
      ...data.meta.toComponent(),
      ...data.link.toComponent(),
      ...data.style.toComponent(),
      ...data.noscript.toComponent(),
      ...data.script.toComponent()
    )
  })
}

export default helmet


text:
<bill session="115" type="h" number="3766" updated="2017-10-11T07:16:34Z">
  <state datetime="2017-09-13">REFERRED</state>
  <status>
    <introduced datetime="2017-09-13"/>
  </status>
  <introduced datetime="2017-09-13"/>
  <titles>
    <title type="short" as="introduced">Credit Information Protection Act of 2017</title>
    <title type="short" as="introduced">Credit Information Protection Act of 2017</title>
    <title type="official" as="introduced">To amend the Fair Credit Reporting Act to require consumer reporting agencies to place a security freeze on a consumer report without a fee if the consumer reporting agency is subject to a breach of data security, and for other purposes.</title>
    <title type="display">Credit Information Protection Act of 2017</title>
  </titles>
  <sponsor bioguide_id="H001047"/>
  <cosponsors>
    <cosponsor bioguide_id="C001049" joined="2017-09-21"/>
    <cosponsor bioguide_id="C001069" joined="2017-10-10"/>
    <cosponsor bioguide_id="C001111" joined="2017-09-21"/>
    <cosponsor bioguide_id="D000620" joined="2017-09-18"/>
    <cosponsor bioguide_id="G000581" joined="2017-09-21"/>
    <cosponsor bioguide_id="K000389" joined="2017-09-21"/>
    <cosponsor bioguide_id="M000087" joined="2017-09-21"/>
    <cosponsor bioguide_id="M001137" joined="2017-09-21"/>
    <cosponsor bioguide_id="N000147" joined="2017-09-21"/>
    <cosponsor bioguide_id="P000593" joined="2017-10-10"/>
    <cosponsor bioguide_id="R000576" joined="2017-09-21"/>
    <cosponsor bioguide_id="S001157" joined="2017-09-21"/>
    <cosponsor bioguide_id="S000344" joined="2017-09-21"/>
    <cosponsor bioguide_id="S001191" joined="2017-09-21"/>
    <cosponsor bioguide_id="V000130" joined="2017-09-21"/>
  </cosponsors>
  <actions>
    <action datetime="2017-09-13">
      <text>Introduced in House</text>
    </action>
    <action datetime="2017-09-13" state="REFERRED">
      <text>Referred to the House Committee on Financial Services.</text>
    </action>
  </actions>
  <committees>
    <committee subcommittee="" code="HSBA" name="House Financial Services" activity="Referral"/>
  </committees>
  <relatedbills/>
  <subjects>
    <term name="Finance and financial sector"/>
  </subjects>
  <amendments/>
  <committee-reports/>
</bill>


text:
package org.sireum.$internal

import java.io.File
import java.nio.charset.StandardCharsets
import java.nio.file.Files

import scala.language.experimental.macros
import scala.collection.mutable.{Map => MMap}

object RC {

  def toTrie(m: scala.collection.Map[Vector[String], String]): Trie.Node[String, String] = {
    val root = Trie.InNode[String, String](MMap())

    def add(path: Vector[String], content: String, node: Trie.InNode[String, String]): Unit = path match {
      case Seq(s) => node.children += ((s, Trie.Leaf[String, String](content)))
      case _ =>
        node.children.get(path.head) match {
          case Some(n: Trie.InNode[String, String] @unchecked) => add(path.tail, content, n)
          case _ =>
            val n = Trie.InNode[String, String](MMap())
            node.children += ((path.head, n))
            add(path.tail, content, n)
        }
    }

    for ((path, content) <- m) add(path, content, root)

    root
  }

  def text(relPaths: Vector[String])(p: (Vector[String], File) => Boolean): Map[Vector[String], String] = macro RC.textImpl

  def base64(relPaths: Vector[String])(p: (Vector[String], File) => Boolean): Map[Vector[String], String] = macro RC.base64Impl
}

class RC(val c: scala.reflect.macros.blackbox.Context) {

  import c.universe._

  def commonImpl(
    isText: Boolean,
    relPaths: c.Expr[Vector[String]],
    p: c.Expr[(Vector[String], File) => Boolean]
  ): c.Expr[Map[Vector[String], String]] = {
    val pf = Macro.eval[(Vector[String], File) => Boolean](c)(p.tree)
    val rps = Macro.eval[Vector[String]](c)(relPaths.tree)
    var args = Vector[c.Tree]()

    def it(rp: String): Unit = {
      val anchorDir = new File(new File(p.tree.pos.source.file.canonicalPath).getParentFile, rp)
      val anchorPath = uriOf(anchorDir)

      def rec(file: File): Unit = {
        if (file.isFile) {
          val filePath = uriOf(file)
          if (filePath.startsWith(anchorPath)) {
            val path = filePath.substring(anchorPath.length).split('/').toVector
            if (pf(path, file)) {
              val pathSegments: Vector[c.Tree] = path.map(p => Literal(Constant(p)))
              val content = if (isText) readText(file) else readBase64(file)
              val s = content.grouped(20000).toSeq.map(c => Literal(Constant(c)))
              val fs = s.indices.toVector.map(n => TermName("f" + n))
              val ms = fs.zip(s).map(p => q"def ${p._1}: Predef.String = ${p._2}")
              val b = q"..${ms :+ fs.map(f => Ident(f): c.Tree).reduce((f1, f2) => q"$f1 + $f2")}"
              args :+= q"(Vector(..$pathSegments), $b)"
            }
          }
        } else if (file.isDirectory) {
          file.listFiles.foreach(rec)
        }
      }
      rec(anchorDir)
    }

    if (rps.nonEmpty) rps.foreach(it) else it(".")

    val r = q"scala.collection.immutable.ListMap[Vector[Predef.String], Predef.String](..$args)"
    //println(showCode(r))
    c.Expr(r)
  }

  def textImpl(relPaths: c.Expr[Vector[String]])(p: c.Expr[(Vector[String], File) => Boolean]): c.Expr[Map[Vector[String], String]] =
    commonImpl(isText = true, relPaths, p)

  def base64Impl(
    relPaths: c.Expr[Vector[String]]
  )(p: c.Expr[(Vector[String], File) => Boolean]): c.Expr[Map[Vector[String], String]] =
    commonImpl(isText = false, relPaths, p)

  def uriOf(f: File): String = f.toURI.toASCIIString

  def readText(f: File): String = new String(Files.readAllBytes(f.toPath), StandardCharsets.UTF_8)

  def readBase64(f: File): String = java.util.Base64.getEncoder.encodeToString(Files.readAllBytes(f.toPath))
}


text:
#ifndef MIDIInput_h
#define MIDIInput_h

#include "modules/EventTargetModules.h"
#include "modules/webmidi/MIDIPort.h"

namespace blink {

class MIDIAccess;

class MIDIInput FINAL : public MIDIPort {
public:
    static MIDIInput* create(MIDIAccess*, const String& id, const String& manufacturer, const String& name, const String& version);
    virtual ~MIDIInput() { }

    DEFINE_ATTRIBUTE_EVENT_LISTENER(midimessage);

    // EventTarget
    virtual const AtomicString& interfaceName() const OVERRIDE { return EventTargetNames::MIDIInput; }

    // |timeStamp| is a DOMHighResTimeStamp in the time coordinate system of performance.now().
    void didReceiveMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStamp);

    virtual void trace(Visitor*) OVERRIDE;

private:
    MIDIInput(MIDIAccess*, const String& id, const String& manufacturer, const String& name, const String& version);
};

typedef HeapVector<Member<MIDIInput> > MIDIInputVector;

} // namespace blink

#endif // MIDIInput_h


text:
class Procrastinate::Utils::OneTimeFlag
  def initialize
    @set        = false
  end

  # If the flag is set, does nothing. If it isn't, it blocks until the flag
  # is set.
  def wait
    return if set?

    sleep(0.01) until set?
  end

  # Sets the flag and releases all waiting threads.
  #
  def set
    @set = true
  end

  # Non blocking: Is the flag set?
  #
  def set?
    @set
  end
end


text:
<?php
/**
 * Shortcodes
 */

//allow shortcodes on text widgets
add_filter('widget_text', 'do_shortcode');

//shortcode for the install directory
function site_url_shortcode(){
    return site_url();
}
add_shortcode( 'site_url', 'site_url_shortcode' );

//shortcode for the install directory
function home_url_shortcode(){
    return home_url();
}
add_shortcode( 'home_url', 'home_url_shortcode' );

//shortcode for the upload directory
function upload_url_shortcode(){
    $upload_dir = wp_upload_dir();
    return $upload_dir['baseurl'];
}
add_shortcode( 'upload_url', 'upload_url_shortcode' );

//shortcode for the theme directory
function theme_url_shortcode(){
    return get_stylesheet_directory_url();
}
add_shortcode( 'theme_url', 'theme_url_shortcode' );

add_shortcode('pb_button','pb_button_shortcode');

function pb_button_shortcode($atts, $content){
    $atts = shortcode_atts(
        array(
            'class'  => 'btn',
            'link'  => '',
        ) , $atts
    );

    extract($atts);

    $output = ''.$content.'';

    return $output;

}