text:
package com.zimbra.cs.service.authenticator;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.security.cert.CertificateFactory;
import java.security.cert.CertificateParsingException;
import java.security.cert.X509Certificate;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;
import javax.naming.ldap.Rdn;
import javax.security.auth.x500.X500Principal;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1TaggedObject;
import org.bouncycastle.asn1.DEREncodable;
import org.bouncycastle.asn1.DERIA5String;
import org.bouncycastle.asn1.DERObjectIdentifier;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.DERUTF8String;
import org.bouncycastle.asn1.x509.CRLDistPoint;
import org.bouncycastle.asn1.x509.DistributionPoint;
import org.bouncycastle.asn1.x509.DistributionPointName;
import org.bouncycastle.asn1.x509.GeneralName;
import org.bouncycastle.asn1.x509.GeneralNames;
import org.bouncycastle.asn1.x509.X509Extension;

import com.zimbra.common.util.ZimbraLog;
import com.zimbra.cs.service.authenticator.ClientCertPrincipalMap.CertField;
import com.zimbra.cs.service.authenticator.ClientCertPrincipalMap.KnownCertField;
import com.zimbra.cs.service.authenticator.ClientCertPrincipalMap.SubjectCertField;

public class CertUtil {
    static final String LOG_PREFIX = ClientCertAuthenticator.LOG_PREFIX;

    static final String ATTR_EMAILADDRESS = "EMAILADDRESS";

    /* ObjectID for UPN in SubjectaltName for windows smart card logon */
    private static final String OID_UPN          = "1.3.6.1.4.1.311.20.2.3";

    /*
     * RFC 2253 2.3
     *
     * If the AttributeType is in a published table of attribute types
     * associated with LDAP [4] (RFC 2252), then the type name string from that table
     * is used, otherwise it is encoded as the dotted-decimal encoding of
     * the AttributeType's OBJECT IDENTIFIER.
     *
     *
     */
    private static final Map<String, String> KNOWN_NON_RFC2252_ATTRS = new HashMap<String, String>();

    static {
        KNOWN_NON_RFC2252_ATTRS.put(ATTR_EMAILADDRESS, "1.2.840.113549.1.9.1");
    }

    X509Certificate cert;

    private CertUtil() {
    }

    CertUtil(X509Certificate cert) {
        this.cert = cert;
    }

    String getCertField(CertField certField) {
        if (certField instanceof KnownCertField) {
            return getKnownCertField((KnownCertField) certField);
        } else if (certField instanceof SubjectCertField) {
            return getSubjectCertField((SubjectCertField) certField);
        }
        return null;
    }

    private String getKnownCertField(KnownCertField certField) {
        String value = null;

        switch (certField.getField()) {
            case SUBJECT_DN:
                value = getSubjectDN();
                break;
            case SUBJECTALTNAME_OTHERNAME_UPN:
                value = getSubjectAltNameOtherNameUPN();
                break;
            case SUBJECTALTNAME_RFC822NAME:
                value = getSubjectAltNameRfc822Name();
                break;
        }
        return value;
    }

    private String getSubjectCertField(SubjectCertField certField) {
        String rdnAttrType = certField.getRDNAttrType();
        return getSubjectAttr(rdnAttrType, KNOWN_NON_RFC2252_ATTRS.get(rdnAttrType));
    }

    String getSubjectDN() {
        X500Principal subjectPrincipal = cert.getSubjectX500Principal();

        /*
        String CANONICAL = subjectPrincipal.getName(X500Principal.CANONICAL);
        // 1.2.840.113549.1.9.1=#161075736572314070686f6562652e6d6270,cn=user one,ou=engineering,o=example company,l=saratoga,st=california,c=us

        String RFC1779   = subjectPrincipal.getName(X500Principal.RFC1779);
        // OID.1.2.840.113549.1.9.1=user1@phoebe.mbp, CN=user one, OU=Engineering, O=Example Company, L=Saratoga, ST=California, C=US

        String RFC2253   = subjectPrincipal.getName(X500Principal.RFC2253);
        // 1.2.840.113549.1.9.1=#161075736572314070686f6562652e6d6270,CN=user one,OU=Engineering,O=Example Company,L=Saratoga,ST=California,C=US
        */

        return subjectPrincipal.getName();
    }

    String getSubjectAltNameOtherNameUPN() {
        Collection<List<?>> generalNames = null;
        try {
            generalNames = cert.getSubjectAlternativeNames();
        } catch (CertificateParsingException e) {
            ZimbraLog.account.warn(LOG_PREFIX + "unable to get subject alternative names", e);
        }

        if (generalNames == null) {
            return null;
        }

        try {
            // Check that the certificate includes the SubjectAltName extension
            for (List<?> generalName : generalNames) {
                Integer tag = (Integer) generalName.get(0);
                if (GeneralName.otherName == tag.intValue()) {
                    // Value is encoded using ASN.1
                    ASN1InputStream decoder = new ASN1InputStream((byte[]) generalName.toArray()[1]);
                    DEREncodable encoded = decoder.readObject();
                    DERSequence derSeq = (DERSequence) encoded;

                    DERObjectIdentifier typeId = DERObjectIdentifier.getInstance(derSeq.getObjectAt(0));
                    String oid = typeId.getId();

                    String value = null;
                    ASN1TaggedObject otherNameValue = ASN1TaggedObject.getInstance(derSeq.getObjectAt(1));
                    if (OID_UPN.equals(oid)) {
                        ASN1TaggedObject upnValue = ASN1TaggedObject.getInstance(otherNameValue.getObject());
                        DERUTF8String str = DERUTF8String.getInstance(upnValue.getObject());
                        value= str.getString();
                        return value;
                    }
                }
            }
        } catch (IOException e) {
            ZimbraLog.account.warn(LOG_PREFIX + "unable to process ASN.1 data", e);
        }

        return null;
    }

    String getSubjectAltNameRfc822Name() {
        Collection<List<?>> generalNames = null;
        try {
            generalNames = cert.getSubjectAlternativeNames();
        } catch (CertificateParsingException e) {
            ZimbraLog.account.warn(LOG_PREFIX + "unable to get subject alternative names", e);
        }

        if (generalNames == null) {
            return null;
        }

        for (List<?> generalName : generalNames) {
            Integer tag = (Integer) generalName.get(0);
            if (GeneralName.rfc822Name == tag.intValue()) {
                String value = (String) generalName.get(1);
                return value;
            }
        }

        return null;
    }

    private String getSubjectAttr(String needAttrName, String needAttrOid) {
        String subjectDN = getSubjectDN();

        try {
            LdapName dn = new LdapName(subjectDN);
            List<Rdn> rdns = dn.getRdns();

            for (Rdn rdn : rdns) {
                String type = rdn.getType();

                boolean isOid = type.contains(".");

                boolean matched = (isOid ? type.equals(needAttrOid) : type.equals(needAttrName));

                if (matched) {
                    Object value = rdn.getValue();
                    if (value == null) {
                        continue;
                    }

                    if (isOid) {
                        byte[] bytes = (byte[]) value;
                        try {
                            ASN1InputStream decoder = new ASN1InputStream(bytes);
                            DEREncodable encoded = decoder.readObject();
                            DERIA5String str = DERIA5String.getInstance(encoded);
                            return str.getString();
                        } catch (IOException e) {
                            ZimbraLog.account.warn(LOG_PREFIX + "unable to decode " + type, e);
                        }

                    } else {
                        return value.toString();
                    }
                }
            }
        } catch (InvalidNameException e) {
            ZimbraLog.account.warn(LOG_PREFIX + "Invalid subject dn value" + subjectDN, e);
        }

        return null;
    }

    /*
     * ======================================================
     * Printing methods below for CLI - Not production code
     * ======================================================
     */

    private void loadCert(String certFilePath) throws Exception {
        InputStream inStream = new FileInputStream(certFilePath);
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        cert = (X509Certificate)cf.generateCertificate(inStream);
        inStream.close();
    }

    private void dumpCert(String outputFlePath) throws Exception {
        outputCert(outputFlePath, true);
    }

    private void printCert(String outputFlePath) throws Exception {
        outputCert(outputFlePath, false);
    }

    private void outputCert(String outputFlePath, boolean dump) throws Exception {
        PrintStream outStream;

        if (outputFlePath != null) {
            outStream = new PrintStream(outputFlePath);
        } else {
            outStream = System.out;
        }

        try {
            if (dump) {
                outStream.println(cert.toString());
            } else {
                printCert(outStream);
            }
        } finally {
            outStream.flush();
            if (outputFlePath != null) {
                outStream.close();
            }
        }
    }

    private void printCert(PrintStream outStream) throws Exception {
        printVersion(outStream);
        printSerialNumber(outStream);
        printSigAlg(outStream);
        printIssuer(outStream);
        printValidity(outStream);
        printSubject(outStream);

        printX509V3Extension(outStream);
    }

    private void printX509V3Extension(PrintStream outStream) throws Exception {
        outStream.println();
        outStream.format("X509v3 extensions:\n");

        printSubjectAlternativeNames(outStream);
        outStream.println();

        printCRLDistributionPoints(outStream);
        outStream.println();
    }

    private void printVersion(PrintStream outStream) {
        int version = cert.getVersion();

        outStream.format("Version: %d (0x%x)\n", version, version);
    }

    private void printSerialNumber(PrintStream outStream) {
        BigInteger serialNumber = cert.getSerialNumber();

        outStream.format("Serial Number: %s (0x%x)\n", serialNumber.toString(), serialNumber);
    }

    private void printSigAlg(PrintStream outStream) throws Exception {
        String sigAlgName = cert.getSigAlgName();
        String sigAlgOID = cert.getSigAlgOID();

        outStream.format("Signature Algorithm: %s (%s)\n", sigAlgName, sigAlgOID);

        /*
        byte[] sigAlgParams = cert.getSigAlgParams();

        AlgorithmParameters algParams = AlgorithmParameters.getInstance(sigAlgName);
        algParams.init(sigAlgParams);
        outStream.format("Signature Algorithm Params: %s", algParams.toString());
        */
    }

    private void printIssuer(PrintStream outStream) {
        X500Principal issuerPrincipal = cert.getIssuerX500Principal();
        outStream.format("Issuer: %s\n", issuerPrincipal.getName());
    }

    private void printValidity(PrintStream outStream) {
        Date notBefore = cert.getNotBefore();
        Date notAfter = cert.getNotAfter();

        outStream.format("Validity\n");
        outStream.format("    Not Before: %s\n", notBefore.toGMTString());
        outStream.format("    Not After : %s\n", notAfter.toGMTString());
    }

    private void printSubject(PrintStream outStream) {
        X500Principal subjectPrincipal = cert.getSubjectX500Principal();
        outStream.format("Subject: %s\n", subjectPrincipal.getName());
    }

    private void printSubjectAlternativeNames(PrintStream outStream) throws Exception {

        final String UPN_DISPLAY = "Principal Name";
        final String RFC822NAME_DISPLAY = "RFC822 Name";
        final String DNSNAME_DISPLAY = "DNS Name";

        outStream.format("X509v3 Subject Alternative Name: \n");

        try {
            Collection<List<?>> generalNames = cert.getSubjectAlternativeNames();
            // Check that the certificate includes the SubjectAltName extension
            if (generalNames == null) {
                return;
            }

            /*
               OtherName ::= SEQUENCE {
                  type-id    OBJECT IDENTIFIER,
                  value      [0] EXPLICIT ANY DEFINED BY type-id }
             */

            for (List<?> generalName : generalNames) {
                Integer tag = (Integer) generalName.get(0);
                if (GeneralName.otherName == tag.intValue()) {
                    // Value is encoded using ASN.1
                    ASN1InputStream decoder = new ASN1InputStream((byte[]) generalName.toArray()[1]);
                    DEREncodable encoded = decoder.readObject();
                    DERSequence derSeq = (DERSequence) encoded;

                    DERObjectIdentifier typeId = DERObjectIdentifier.getInstance(derSeq.getObjectAt(0));
                    String oid = typeId.getId();

                    String value = null;
                    ASN1TaggedObject otherNameValue = ASN1TaggedObject.getInstance(derSeq.getObjectAt(1));
                    if (OID_UPN.equals(oid)) {
                        ASN1TaggedObject upnValue = ASN1TaggedObject.getInstance(otherNameValue.getObject());
                        DERUTF8String str = DERUTF8String.getInstance(upnValue.getObject());
                        value= str.getString();
                    }

                    outStream.format("    [%d] %s(%s) = %s\n", tag, oid, UPN_DISPLAY, value);
                } else if (GeneralName.rfc822Name == tag.intValue()) {
                    String value = (String) generalName.get(1);
                    outStream.format("    [%d] %s = %s\n", tag, RFC822NAME_DISPLAY, value);
                } else if (GeneralName.dNSName == tag.intValue()) {
                    String value = (String) generalName.get(1);
                    outStream.format("    [%d] %s = %s\n", tag, DNSNAME_DISPLAY, value);
                } else {
                    outStream.format("    [%d] - not yet supported\n", tag);
                }

            }
        }
        catch (CertificateParsingException e) {
            e.printStackTrace();
        }
    }

    private void printCRLDistributionPoints(PrintStream outStream) throws Exception {

        outStream.format("X509v3 CRL Distribution Points: \n");

        String extOid = X509Extension.cRLDistributionPoints.getId(); // 2.5.29.31
        byte[] extVal = cert.getExtensionValue(extOid);
        if (extVal == null) {
            return;
        }

        /* http://download.oracle.com/javase/6/docs/api/java/security/cert/X509Extension.html#getExtensionValue(java.lang.String)
         *
           The ASN.1 definition for this is:

             Extensions  ::=  SEQUENCE SIZE (1..MAX) OF Extension

             Extension  ::=  SEQUENCE  {
                 extnId        OBJECT IDENTIFIER,
                 critical      BOOLEAN DEFAULT FALSE,
                 extnValue     OCTET STRING
                               -- contains a DER encoding of a value
                               -- of the type registered for use with
                               -- the extnId object identifier value
             }
         */

        byte[] extnValue = DEROctetString.getInstance(ASN1Object.fromByteArray(extVal)).getOctets();

        CRLDistPoint crlDistPoint = CRLDistPoint.getInstance(ASN1Object.fromByteArray(extnValue));
        DistributionPoint[] distPoints = crlDistPoint.getDistributionPoints();

        for (DistributionPoint distPoint : distPoints) {
            DistributionPointName distPointName = distPoint.getDistributionPoint();
            int type = distPointName.getType();

            if (DistributionPointName.FULL_NAME == type) {
                outStream.format("Full Name: \n");
                GeneralNames generalNames = GeneralNames.getInstance(distPointName.getName());
                GeneralName[] names = generalNames.getNames();
                for (GeneralName generalname : names) {
                    int tag = generalname.getTagNo();
                    if (GeneralName.uniformResourceIdentifier == tag) {
                        DEREncodable name = generalname.getName();
                        DERIA5String str = DERIA5String.getInstance(name);
                        String value = str.getString();
                        outStream.format("    %s\n", value);
                    } else {
                        outStream.format("tag %d not yet implemented", tag);
                    }
                }
            } else {
                outStream.format("type %d not yet implemented", type);
            }
        }
    }

    private static int EXIT_CODE_GOOD = 0;
    private static int EXIT_CODE_BAD = 0;

    private static String O_CERT = "c";
    private static String O_DUMP = "d";
    private static String O_GET  = "g";
    private static String O_HELP = "h";
    private static String O_PRINT = "p";

    private static void usage(Options options, String msg) {
        System.out.println("\n");
        System.out.println(msg);
        usage(options);
    }

    private static void usage(Options options) {
        System.out.println("\n");
        PrintWriter pw = new PrintWriter(System.out, true);
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(pw, formatter.getWidth(),
                "zmjava " + CertUtil.class.getCanonicalName() + " [options]",
                null, options, formatter.getLeftPadding(), formatter.getDescPadding(), null);
        System.out.println("\n");
        pw.flush();
    }

    /*
     *  zmjava com.zimbra.cs.service.authenticator.CertUtil [options]
     */
    public static void main(String[] args) {

        Options options = new Options();
        options.addOption(O_CERT, true, "file path of the certificate");
        options.addOption(O_DUMP, false, "dump the certificate (print toString() value of the certificate)");
        options.addOption(O_GET, true, "get a field in the certificate, valid fields:" +
                KnownCertField.names() + "|" + SubjectCertField.names());
        options.addOption(O_HELP, false, "print usage");
        options.addOption(O_PRINT, false, "print the certificate(print each parsed certificate fields)");

        CommandLine cl = null;
        try {
            CommandLineParser parser = new GnuParser();
            cl = parser.parse(options, args);
            if (cl == null) {
                throw new ParseException("");
            }
        } catch (ParseException e) {
            usage(options);
            e.printStackTrace();
            System.exit(EXIT_CODE_BAD);
        }

        if (cl.hasOption(O_HELP)) {
            usage(options);
            System.exit(EXIT_CODE_GOOD);
        }

        String certFilePath = null;

        if (cl.hasOption(O_CERT)) {
            certFilePath = cl.getOptionValue(O_CERT);
        } else {
            usage(options, "missing cert path");
            System.exit(EXIT_CODE_BAD);
        }

        try {
            CertUtil certUtil = new CertUtil();
            certUtil.loadCert(certFilePath);

            if (cl.hasOption(O_DUMP)) {
                certUtil.dumpCert((String)null);
            } else if (cl.hasOption(O_PRINT)) {
                certUtil.printCert((String)null);
            } else if (cl.hasOption(O_GET)) {
                String field = cl.getOptionValue(O_GET);
                CertField certField = ClientCertPrincipalMap.parseCertField(field);
                String value = certUtil.getCertField(certField);
                System.out.println(field + ": " + value);
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.exit(EXIT_CODE_BAD);
        }
    }
}


text:
"""
Defines the 'request' class which encapsulates an HTTP request and verification.
"""

from hashlib import md5, sha1
from src.httpshandler import SmartHTTPConnection
from src.xmlUtils import getYesNoAttributeValue
import base64
import datetime
import os
import re
import src.xmlDefs
import time
import uuid

algorithms = {
    'md5': md5,
    'md5-sess': md5,
    'sha': sha1,
}

# DigestCalcHA1
def calcHA1(
    pszAlg,
    pszUserName,
    pszRealm,
    pszPassword,
    pszNonce,
    pszCNonce,
    preHA1=None
):
    """
    @param pszAlg: The name of the algorithm to use to calculate the digest.
        Currently supported are md5 md5-sess and sha.

    @param pszUserName: The username
    @param pszRealm: The realm
    @param pszPassword: The password
    @param pszNonce: The nonce
    @param pszCNonce: The cnonce

    @param preHA1: If available this is a str containing a previously
       calculated HA1 as a hex string. If this is given then the values for
       pszUserName, pszRealm, and pszPassword are ignored.
    """

    if (preHA1 and (pszUserName or pszRealm or pszPassword)):
        raise TypeError(("preHA1 is incompatible with the pszUserName, "
                         "pszRealm, and pszPassword arguments"))

    if preHA1 is None:
        # We need to calculate the HA1 from the username:realm:password
        m = algorithms[pszAlg]()
        m.update(pszUserName)
        m.update(":")
        m.update(pszRealm)
        m.update(":")
        m.update(pszPassword)
        HA1 = m.digest()
    else:
        # We were given a username:realm:password
        HA1 = preHA1.decode('hex')

    if pszAlg == "md5-sess":
        m = algorithms[pszAlg]()
        m.update(HA1)
        m.update(":")
        m.update(pszNonce)
        m.update(":")
        m.update(pszCNonce)
        HA1 = m.digest()

    return HA1.encode('hex')

# DigestCalcResponse
def calcResponse(
    HA1,
    algo,
    pszNonce,
    pszNonceCount,
    pszCNonce,
    pszQop,
    pszMethod,
    pszDigestUri,
    pszHEntity,
):
    m = algorithms[algo]()
    m.update(pszMethod)
    m.update(":")
    m.update(pszDigestUri)
    if pszQop == "auth-int":
        m.update(":")
        m.update(pszHEntity)
    HA2 = m.digest().encode('hex')

    m = algorithms[algo]()
    m.update(HA1)
    m.update(":")
    m.update(pszNonce)
    m.update(":")
    if pszNonceCount and pszCNonce and pszQop:
        m.update(pszNonceCount)
        m.update(":")
        m.update(pszCNonce)
        m.update(":")
        m.update(pszQop)
        m.update(":")
    m.update(HA2)
    respHash = m.digest().encode('hex')
    return respHash

class pause (object):
    pass

class request(object):
    """
    Represents the HTTP request to be executed, and verification information to
    be used to determine a satisfactory output or not.
    """

    nc = {} # Keep track of nonce count

    def __init__(self, manager):
        self.manager = manager
        self.host = self.manager.server_info.host
        self.port = self.manager.server_info.port
        self.auth = True
        self.user = ""
        self.pswd = ""
        self.end_delete = False
        self.print_request = False
        self.print_response = False
        self.wait_for_success = None
        self.require_features = set()
        self.exclude_features = set()
        self.method = ""
        self.headers = {}
        self.ruris = []
        self.ruri = ""
        self.ruri_quote = True
        self.data = None
        self.iterate_data = False
        self.count = 1
        self.verifiers = []
        self.graburi = None
        self.grabcount = None
        self.grabheader = []
        self.grabproperty = []
        self.grabelement = []
        self.grabjson = []
        self.grabcalprop = []
        self.grabcalparam = []

    def __str__(self):
        return "Method: %s; uris: %s" % (self.method, self.ruris if len(self.ruris) > 1 else self.ruri,)

    def missingFeatures(self):
        return self.require_features - self.manager.server_info.features

    def excludedFeatures(self):
        return self.exclude_features & self.manager.server_info.features

    def getURI(self, si):
        uri = si.extrasubs(self.ruri)
        if "**" in uri:
            if "?" not in uri or uri.find("?") > uri.find("**"):
                uri = uri.replace("**", str(uuid.uuid4()))
        elif "##" in uri:
            if "?" not in uri or uri.find("?") > uri.find("##"):
                uri = uri.replace("##", str(self.count))
        return uri

    def getHeaders(self, si):
        hdrs = self.headers
        for key, value in hdrs.items():
            hdrs[key] = si.extrasubs(value)

        # Content type
        if self.data != None:
            hdrs["Content-Type"] = self.data.content_type

        # Auth
        if self.auth:
            if si.authtype.lower() == "digest":
                hdrs["Authorization"] = self.gethttpdigestauth(si)
            else:
                hdrs["Authorization"] = self.gethttpbasicauth(si)

        return hdrs

    def gethttpbasicauth(self, si):
        basicauth = [self.user, si.user][self.user == ""]
        basicauth += ":"
        basicauth += [self.pswd, si.pswd][self.pswd == ""]
        basicauth = "Basic " + base64.encodestring(basicauth)
        basicauth = basicauth.replace("\n", "")
        return basicauth

    def gethttpdigestauth(self, si, wwwauthorize=None):

        # Check the nonce cache to see if we've used this user before
        user = [self.user, si.user][self.user == ""]
        pswd = [self.pswd, si.pswd][self.pswd == ""]
        details = None
        if user in self.manager.digestCache:
            details = self.manager.digestCache[user]
        else:
            http = SmartHTTPConnection(si.host, si.port, si.ssl)
            try:
                http.request("OPTIONS", self.getURI(si))

                response = http.getresponse()

            finally:
                http.close()

            if response.status == 401:

                wwwauthorize = response.msg.getheaders("WWW-Authenticate")
                for item in wwwauthorize:
                    if not item.lower().startswith("digest "):
                        continue
                    wwwauthorize = item[7:]
                    def unq(s):
                        if s[0] == s[-1] == '"':
                            return s[1:-1]
                        return s
                    parts = wwwauthorize.split(',')

                    details = {}

                    for (k, v) in [p.split('=', 1) for p in parts]:
                        details[k.strip()] = unq(v.strip())

                    self.manager.digestCache[user] = details
                    break

        if details:
            if details.get('qop'):
                if self.nc.get(details.get('nonce')) is None:
                    self.nc[details.get('nonce')] = 1
                else:
                    self.nc[details.get('nonce')] += 1
                details['nc'] = "%08x" % self.nc[details.get('nonce')]
                if details.get('cnonce') is None:
                    details['cnonce'] = "D4AAE4FF-ADA1-4149-BFE2-B506F9264318"

            digest = calcResponse(
                calcHA1(details.get('algorithm', 'md5'), user, details.get('realm'), pswd, details.get('nonce'), details.get('cnonce')),
                details.get('algorithm', 'md5'), details.get('nonce'), details.get('nc'), details.get('cnonce'), details.get('qop'), self.method, self.getURI(si), None
            )

            if details.get('qop'):
                response = (
                    'Digest username="%s", realm="%s", '
                    'nonce="%s", uri="%s", '
                    'response=%s, algorithm=%s, cnonce="%s", qop=%s, nc=%s' %
                    (user, details.get('realm'), details.get('nonce'), self.getURI(si), digest, details.get('algorithm', 'md5'), details.get('cnonce'), details.get('qop'), details.get('nc'),)
                )
            else:
                response = (
                    'Digest username="%s", realm="%s", '
                    'nonce="%s", uri="%s", '
                    'response=%s, algorithm=%s' %
                    (user, details.get('realm'), details.get('nonce'), self.getURI(si), digest, details.get('algorithm'),)
                )

            return response
        else:
            return ""

    def getFilePath(self):
        if self.data != None:
            return os.path.join(self.manager.data_dir, self.data.filepath) if self.manager.data_dir else self.data.filepath
        else:
            return ""

    def getData(self):
        data = ""
        if self.data != None:
            if len(self.data.value) != 0:
                data = self.data.value
            elif self.data.filepath:
                # read in the file data
                fd = open(self.data.nextpath if hasattr(self.data, "nextpath") else self.getFilePath(), "r")
                try:
                    data = fd.read()
                finally:
                    fd.close()
            data = str(self.manager.server_info.subs(data))
            self.manager.server_info.addextrasubs({"$request_count:": str(self.count)})
            data = self.manager.server_info.extrasubs(data)
            if self.data.substitutions:
                data = self.manager.server_info.subs(data, self.data.substitutions)
            if self.data.generate:
                if self.data.content_type.startswith("text/calendar"):
                    data = self.generateCalendarData(data)
            elif self.data.generator:
                data = self.data.generator.doGenerate()
        return data

    def getNextData(self):
        if not hasattr(self, "dataList"):
            self.dataList = sorted([path for path in os.listdir(self.getFilePath()) if not path.startswith(".")])
        if len(self.dataList):
            self.data.nextpath = os.path.join(self.getFilePath(), self.dataList.pop(0))
            return True
        else:
            if hasattr(self.data, "nextpath"):
                delattr(self.data, "nextpath")
            if hasattr(self, "dataList"):
                delattr(self, "dataList")
            return False

    def hasNextData(self):
        dataList = sorted([path for path in os.listdir(self.getFilePath()) if not path.startswith(".")])
        return len(dataList) != 0

    def generateCalendarData(self, data):
        """
        FIXME: does not work for events with recurrence overrides.
        """

        # Change the following iCalendar data values:
        # DTSTART, DTEND, RECURRENCE-ID, UID

        data = re.sub("UID:.*", "UID:%s" % (uuid.uuid4(),), data)
        data = re.sub("SUMMARY:(.*)", "SUMMARY:\\1 #%s" % (self.count,), data)

        now = datetime.date.today()
        data = re.sub("(DTSTART;[^:]*):[0-9]{8,8}", "\\1:%04d%02d%02d" % (now.year, now.month, now.day,), data)
        data = re.sub("(DTEND;[^:]*):[0-9]{8,8}", "\\1:%04d%02d%02d" % (now.year, now.month, now.day,), data)

        return data

    def parseXML(self, node):
        self.auth = node.get(src.xmlDefs.ATTR_AUTH, src.xmlDefs.ATTR_VALUE_YES) == src.xmlDefs.ATTR_VALUE_YES
        self.user = self.manager.server_info.subs(node.get(src.xmlDefs.ATTR_USER, "").encode("utf-8"))
        self.pswd = self.manager.server_info.subs(node.get(src.xmlDefs.ATTR_PSWD, "").encode("utf-8"))
        self.end_delete = getYesNoAttributeValue(node, src.xmlDefs.ATTR_END_DELETE)
        self.print_request = self.manager.print_request or getYesNoAttributeValue(node, src.xmlDefs.ATTR_PRINT_REQUEST)
        self.print_response = self.manager.print_response or getYesNoAttributeValue(node, src.xmlDefs.ATTR_PRINT_RESPONSE)
        self.iterate_data = getYesNoAttributeValue(node, src.xmlDefs.ATTR_ITERATE_DATA)
        self.wait_for_success = getYesNoAttributeValue(node, src.xmlDefs.ATTR_WAIT_FOR_SUCCESS)

        if node.get(src.xmlDefs.ATTR_HOST2, src.xmlDefs.ATTR_VALUE_NO) == src.xmlDefs.ATTR_VALUE_YES:
            self.host = self.manager.server_info.host2
            self.port = self.manager.server_info.port2

        for child in node.getchildren():
            if child.tag == src.xmlDefs.ELEMENT_REQUIRE_FEATURE:
                self.parseFeatures(child, require=True)
            elif child.tag == src.xmlDefs.ELEMENT_EXCLUDE_FEATURE:
                self.parseFeatures(child, require=False)
            elif child.tag == src.xmlDefs.ELEMENT_METHOD:
                self.method = child.text.encode("utf-8")
            elif child.tag == src.xmlDefs.ELEMENT_HEADER:
                self.parseHeader(child)
            elif child.tag == src.xmlDefs.ELEMENT_RURI:
                self.ruri_quote = child.get(src.xmlDefs.ATTR_QUOTE, src.xmlDefs.ATTR_VALUE_YES) == src.xmlDefs.ATTR_VALUE_YES
                self.ruris.append(self.manager.server_info.subs(child.text.encode("utf-8")))
                if len(self.ruris) == 1:
                    self.ruri = self.ruris[0]
            elif child.tag == src.xmlDefs.ELEMENT_DATA:
                self.data = data(self.manager)
                self.data.parseXML(child)
            elif child.tag == src.xmlDefs.ELEMENT_VERIFY:
                self.verifiers.append(verify(self.manager))
                self.verifiers[-1].parseXML(child)
            elif child.tag == src.xmlDefs.ELEMENT_GRABURI:
                self.graburi = child.text.encode("utf-8")
            elif child.tag == src.xmlDefs.ELEMENT_GRABCOUNT:
                self.grabcount = child.text.encode("utf-8")
            elif child.tag == src.xmlDefs.ELEMENT_GRABHEADER:
                self.parseGrab(child, self.grabheader)
            elif child.tag == src.xmlDefs.ELEMENT_GRABPROPERTY:
                self.parseGrab(child, self.grabproperty)
            elif child.tag == src.xmlDefs.ELEMENT_GRABELEMENT:
                self.parseMultiGrab(child, self.grabelement)
            elif child.tag == src.xmlDefs.ELEMENT_GRABJSON:
                self.parseMultiGrab(child, self.grabjson)
            elif child.tag == src.xmlDefs.ELEMENT_GRABCALPROP:
                self.parseGrab(child, self.grabcalprop)
            elif child.tag == src.xmlDefs.ELEMENT_GRABCALPARAM:
                self.parseGrab(child, self.grabcalparam)

    def parseFeatures(self, node, require=True):
        for child in node.getchildren():
            if child.tag == src.xmlDefs.ELEMENT_FEATURE:
                (self.require_features if require else self.exclude_features).add(child.text.encode("utf-8"))

    def parseHeader(self, node):

        name = None
        value = None
        for child in node.getchildren():
            if child.tag == src.xmlDefs.ELEMENT_NAME:
                name = child.text.encode("utf-8")
            elif child.tag == src.xmlDefs.ELEMENT_VALUE:
                value = self.manager.server_info.subs(child.text.encode("utf-8"))

        if (name is not None) and (value is not None):
            self.headers[name] = value

    def parseList(manager, node):
        requests = []
        for child in node.getchildren():
            if child.tag == src.xmlDefs.ELEMENT_REQUEST:
                req = request(manager)
                req.parseXML(child)
                requests.append(req)
            elif child.tag == src.xmlDefs.ELEMENT_PAUSE:
                requests.append(pause())
        return requests

    parseList = staticmethod(parseList)

    def parseGrab(self, node, appendto):

        name = None
        variable = None
        for child in node.getchildren():
            if child.tag in (src.xmlDefs.ELEMENT_NAME, src.xmlDefs.ELEMENT_PROPERTY):
                name = self.manager.server_info.subs(child.text.encode("utf-8"))
            elif child.tag == src.xmlDefs.ELEMENT_VARIABLE:
                variable = self.manager.server_info.subs(child.text.encode("utf-8"))

        if (name is not None) and (variable is not None):
            appendto.append((name, variable))

    def parseMultiGrab(self, node, appendto):

        name = None
        parent = None
        variable = None
        for child in node.getchildren():
            if child.tag in (src.xmlDefs.ELEMENT_NAME, src.xmlDefs.ELEMENT_PROPERTY, src.xmlDefs.ELEMENT_POINTER):
                name = self.manager.server_info.subs(child.text.encode("utf-8"))
            elif child.tag == src.xmlDefs.ELEMENT_PARENT:
                parent = self.manager.server_info.subs(child.text.encode("utf-8"))
            elif child.tag == src.xmlDefs.ELEMENT_VARIABLE:
                if variable is None:
                    variable = []
                variable.append(self.manager.server_info.subs(child.text.encode("utf-8")))

        if (name is not None) and (variable is not None):
            appendto.append((name, variable,) if parent is None else (name, parent, variable,))

class data(object):
    """
    Represents the data/body portion of an HTTP request.
    """

    def __init__(self, manager):
        self.manager = manager
        self.content_type = ""
        self.filepath = ""
        self.generator = None
        self.value = ""
        self.substitutions = {}
        self.substitute = False
        self.generate = False

    def parseXML(self, node):

        self.substitute = node.get(src.xmlDefs.ATTR_SUBSTITUTIONS, src.xmlDefs.ATTR_VALUE_YES) == src.xmlDefs.ATTR_VALUE_YES
        self.generate = node.get(src.xmlDefs.ATTR_GENERATE, src.xmlDefs.ATTR_VALUE_NO) == src.xmlDefs.ATTR_VALUE_YES

        for child in node.getchildren():
            if child.tag == src.xmlDefs.ELEMENT_CONTENTTYPE:
                self.content_type = child.text.encode("utf-8")
            elif child.tag == src.xmlDefs.ELEMENT_FILEPATH:
                self.filepath = child.text.encode("utf-8")
            elif child.tag == src.xmlDefs.ELEMENT_GENERATOR:
                self.generator = generator(self.manager)
                self.generator.parseXML(child)
            elif child.tag == src.xmlDefs.ELEMENT_SUBSTITUTE:
                self.parseSubstituteXML(child)

    def parseSubstituteXML(self, node):
        name = None
        value = None
        for child in node.getchildren():
            if child.tag == src.xmlDefs.ELEMENT_NAME:
                name = child.text.encode("utf-8")
            elif child.tag == src.xmlDefs.ELEMENT_VALUE:
                value = self.manager.server_info.subs(child.text.encode("utf-8"))
        if name and value:
            self.substitutions[name] = value

class generator(object):
    """
    Defines a dynamically generated request body.
    """

    def __init__(self, manager):
        self.manager = manager
        self.callback = None
        self.args = {}

    def doGenerate(self):

        # Re-do substitutions from values generated during the current test run
        if self.manager.server_info.hasextrasubs():
            for name, values in self.args.iteritems():
                newvalues = [self.manager.server_info.extrasubs(value) for value in values]
                self.args[name] = newvalues

        generatorClass = self._importName(self.callback, "Generator")
        gen = generatorClass()

        # Always clone the args as this verifier may be called multiple times
        args = dict((k, list(v)) for k, v in self.args.items())

        return gen.generate(self.manager, args)

    def _importName(self, modulename, name):
        """
        Import a named object from a module in the context of this function.
        """
        module = __import__(modulename, globals(), locals(), [name])
        return getattr(module, name)

    def parseXML(self, node):

        for child in node.getchildren():
            if child.tag == src.xmlDefs.ELEMENT_CALLBACK:
                self.callback = child.text.encode("utf-8")
            elif child.tag == src.xmlDefs.ELEMENT_ARG:
                self.parseArgXML(child)

    def parseArgXML(self, node):
        name = None
        values = []
        for child in node.getchildren():
            if child.tag == src.xmlDefs.ELEMENT_NAME:
                name = child.text.encode("utf-8")
            elif child.tag == src.xmlDefs.ELEMENT_VALUE:
                if child.text is not None:
                    values.append(self.manager.server_info.subs(child.text.encode("utf-8")))
                else:
                    values.append("")
        if name:
            self.args[name] = values

class verify(object):
    """
    Defines how the result of a request should be verified. This is done
    by passing the response and response data to a callback with a set of arguments
    specified in the test XML config file. The callback name is in the XML config
    file also and is dynamically loaded to do the verification.
    """

    def __init__(self, manager):
        self.manager = manager
        self.require_features = set()
        self.exclude_features = set()
        self.callback = None
        self.args = {}

    def missingFeatures(self):
        return self.require_features - self.manager.server_info.features

    def excludedFeatures(self):
        return self.exclude_features & self.manager.server_info.features

    def doVerify(self, uri, response, respdata):

        # Re-do substitutions from values generated during the current test run
        if self.manager.server_info.hasextrasubs():
            for name, values in self.args.iteritems():
                newvalues = [self.manager.server_info.extrasubs(value) for value in values]
                self.args[name] = newvalues

        verifierClass = self._importName("verifiers." + self.callback, "Verifier")
        verifier = verifierClass()

        # Always clone the args as this verifier may be called multiple times
        args = dict((k, list(v)) for k, v in self.args.items())

        return verifier.verify(self.manager, uri, response, respdata, args)

    def _importName(self, modulename, name):
        """
        Import a named object from a module in the context of this function.
        """
        module = __import__(modulename, globals(), locals(), [name])
        return getattr(module, name)

    def parseXML(self, node):

        for child in node.getchildren():
            if child.tag == src.xmlDefs.ELEMENT_REQUIRE_FEATURE:
                self.parseFeatures(child, require=True)
            elif child.tag == src.xmlDefs.ELEMENT_EXCLUDE_FEATURE:
                self.parseFeatures(child, require=False)
            elif child.tag == src.xmlDefs.ELEMENT_CALLBACK:
                self.callback = child.text.encode("utf-8")
            elif child.tag == src.xmlDefs.ELEMENT_ARG:
                self.parseArgXML(child)

    def parseFeatures(self, node, require=True):
        for child in node.getchildren():
            if child.tag == src.xmlDefs.ELEMENT_FEATURE:
                (self.require_features if require else self.exclude_features).add(child.text.encode("utf-8"))

    def parseArgXML(self, node):
        name = None
        values = []
        for child in node.getchildren():
            if child.tag == src.xmlDefs.ELEMENT_NAME:
                name = child.text.encode("utf-8")
            elif child.tag == src.xmlDefs.ELEMENT_VALUE:
                if child.text is not None:
                    values.append(self.manager.server_info.subs(child.text.encode("utf-8")))
                else:
                    values.append("")
        if name:
            self.args[name] = values

class stats(object):
    """
    Maintains stats about the current test.
    """

    def __init__(self):
        self.count = 0
        self.totaltime = 0.0
        self.currenttime = 0.0

    def startTimer(self):
        self.currenttime = time.time()

    def endTimer(self):
        self.count += 1
        self.totaltime += time.time() - self.currenttime

redpajama_set_name: RedPajamaGithub


text:
var Observable = require('rx/Observable'),
    inherits = require('util').inherits;

inherits(RangeObservable, Observable);

function RangeObservable(from, to, scheduler) {
    this.to   = to;
    this.from = from;
    this.scheduler = scheduler;
    return Observable.call(this, subscribe);
}

function subscribe(s, state) {
    var scheduler  = state && s || undefined,
        subscriber = state && state.subscriber || s,
        to   = state ? state.to   : this.to,
        from = state ? state.from : this.from - 1,
        active;

    if(scheduler) {
        if((state.from = ++from) < to) {
            return subscriber.onNext(from) && scheduler.schedule(state, subscribe);
        } else {
            return subscriber.onCompleted();
        }
    } else if(scheduler = this.scheduler) {
        return !subscriber.disposed && scheduler.schedule({
            subscriber: subscriber, to: to, from: from
        }, subscribe);
    } else {
        while((++from < to) && (active = subscriber.onNext(from))) {}
        return active && subscriber.onCompleted();
    }
}

module.exports = function range(from, to, scheduler) {
    return new RangeObservable(Math.min(from, to), Math.max(from, to), scheduler);
}


text:
#ifndef UNDOMODEL_H_HEADER_INCLUDED_C16ED098
#define UNDOMODEL_H_HEADER_INCLUDED_C16ED098

#include "mitkCommon.h"
#include "mitkOperation.h"
#include <itkObject.h>
#include <itkObjectFactory.h>

namespace mitk {

class UndoStackItem;
class OperationEvent;
class OperationActor;

//##Documentation
//## @brief superclass for all UndoModels
//##
//## all necessary operations, that all UndoModels share.
//## @ingroup Undo
class MITKCORE_EXPORT UndoModel : public itk::Object
{
  public:
  mitkClassMacroItkParent(UndoModel, itk::Object);

  // no New Macro because this is an abstract class!

  virtual bool SetOperationEvent(UndoStackItem* stackItem) = 0;

  virtual bool Undo() = 0;
  virtual bool Undo(bool fine) = 0;

  virtual bool Redo() = 0;
  virtual bool Redo(bool fine) = 0;

  //##Documentation
  //## @brief clears undo and Redolist
  virtual void Clear() = 0;

  //##Documentation
  //## @brief clears the RedoList
  virtual void ClearRedoList() = 0;

  //##Documentation
  //## @brief true if RedoList is empty
  virtual bool RedoListEmpty() = 0;

  //##Documentation
  //## @brief returns the ObjectEventId of the
  //## top Element in the OperationHistory of the selected
  //## UndoModel
  virtual int GetLastObjectEventIdInList() = 0;

  //##Documentation
  //## @brief returns the GroupEventId of the
  //## top Element in the OperationHistory of the selected
  //## UndoModel
  virtual int GetLastGroupEventIdInList() = 0;

  //##Documentation
  //## @brief returns the last specified OperationEvent in Undo-list
  //## corresponding to the given values; if nothing found, then returns NULL
  //##
  //## needed to get the old Position of an Element for declaring an UndoOperation
  virtual OperationEvent* GetLastOfType(OperationActor* destination, OperationType opType) = 0;

protected:
  UndoModel(){};
  virtual ~UndoModel(){};

};

}// namespace mitk
#endif /* UNDOMODEL_H_HEADER_INCLUDED_C16ED098 */


text:
"""Functions for creating objects with RandomVariable semantics."""
import tensorflow.compat.v2 as tf

from tensorflow_probability.python.distributions import distribution as tfd
from tensorflow_probability.python.util.deferred_tensor import DeferredTensor

__all__ = [
    'CallOnce',
    'RandomVariable',
]

class RandomVariable(DeferredTensor):
  """`RandomVariable` supports random variable semantics for TFP distributions.

  The `RandomVariable` class memoizes concretizations of TFP distribution-like
  objects so that random draws can be re-triggered on-demand, i.e., by calling
  `reset`. For more details type `help(tfp.util.DeferredTensor)`.

  #### Examples

  ```python
  # In this example we see the memoization semantics in action.
  tfd = tfp.distributions
  tfn = tfp.experimental.nn
  x = tfn.util.RandomVariable(tfd.Normal(0, 1))
  x_ = tf.convert_to_tensor(x)
  x _ + 1. == x + 1.
  # ==> True; `x` always has the same value until reset.
  x.reset()
  tf.convert_to_tensor(x) == x_
  # ==> False; `x` was reset which triggers a new sample.
  ```

  ```python
  # In this example we see how to concretize with different semantics.
  tfd = tfp.distributions
  tfn = tfp.experimental.nn
  x = tfn.util.RandomVariable(
      tfd.Bernoulli(probs=[[0.25], [0.5]]),
      convert_to_tensor_fn=tfd.Distribution.mean,
      dtype=tf.float32,
      shape=[2, 1],
      name='x')
  tf.convert_to_tensor(x)
  # ==> [[0.25], [0.5]]
  x.shape
  # ==> [2, 1]
  x.dtype
  # ==> tf.float32
  x.name
  # ==> 'x'
  ```

  ```python
  # In this example we see a common pitfall: accessing the memoized value from a
  # different graph context.
  tfd = tfp.distributions
  tfn = tfp.experimental.nn
  x = tfn.util.RandomVariable(tfd.Normal(0, 1))
  @tf.function(autograph=False, jit_compile=True)
  def run():
    return tf.convert_to_tensor(x)
  first = run()
  second = tf.convert_to_tensor(x)
  # raises ValueError:
  #   "You are attempting to access a memoized value from a different
  #   graph context. Please call `this.reset()` before accessing a
  #   memoized value from a different graph context."
  x.reset()
  third = tf.convert_to_tensor(x)
  # ==> No exception.
  first == third
  # ==> False
  ```

  """

  def __init__(self, distribution, convert_to_tensor_fn=tfd.Distribution.sample,
               dtype=None, shape=None, name=None):
    """Creates the `RandomVariable` object.

    Args:
      distribution: TFP distribution-like object which is passed into the
        `convert_to_tensor_fn` whenever this object is evaluated in
        `Tensor`-like contexts.
      convert_to_tensor_fn: Python `callable` which takes one argument, the
        `distribution` and returns a `Tensor` of type `dtype` and shape `shape`.
        Default value: `tfp.distributions.Distribution.sample`.
      dtype: TF `dtype` equivalent to what would otherwise be
        `convert_to_tensor_fn(distribution).dtype`.
        Default value: `None` (i.e., `distribution.dtype`).
      shape: `tf.TensorShape`-like object compatible with what would otherwise
        be `convert_to_tensor_fn(distribution).shape`.
        Default value: `'None'` (i.e., unspecified static shape).
      name: Python `str` representing this object's `name`; used only in graph
        mode.
        Default value: `None` (i.e., `distribution.name`)
    """

    self._distribution = distribution
    self._convert_to_tensor_fn = convert_to_tensor_fn
    super(RandomVariable, self).__init__(
        tf.constant([], tf.bool),  # Dummy.
        CallOnce(lambda _: convert_to_tensor_fn(distribution)),
        shape=shape,
        dtype=dtype or distribution.dtype,
        name=name or distribution.name)

  @property
  def distribution(self):
    return self._distribution

  @property
  def convert_to_tensor_fn(self):
    return self._convert_to_tensor_fn

  def reset(self):
    """Removes memoized value which triggers re-eval on subsequent reads."""
    self.transform_fn.reset()

  def is_unset(self):
    """Returns `True` if there is no memoized value and `False` otherwise."""
    return self.transform_fn.is_unset()

class CallOnce(tf.Module):
  """Function object which memoizes the result of `create_value_fn()`.

  This object is used to memoize the computation of some function. Upon first
  call, the user provided `create_value_fn` is called and with the args/kwargs
  provided to this object's `__call__`. On subsequent calls the previous result
  is returned and **regardless of the args/kwargs provided to this object's
  `__call__`**. To trigger a new evaluation, invoke `this.reset()` and to
  identify if a new evaluation will execute (on-demand) invoke
  `this.is_unset()`. For an example application of this object, see
  `help(tfp.experimental.nn.util.RandomVariable)` and/or
  `help(tfp.util.DeferredTensor)`.
  """

  def __init__(self, create_value_fn):
    """Creates the `CallOnce` object.

    Args:
      create_value_fn: Python `callable` which takes any input args/kwargs and
        returns a value to memoize. (The value is not presumed to be of any
        particular type.)
    """
    self._create_value_fn = create_value_fn
    self._value = _UNSET
    # TODO(b/156185251): We have to set `__name__` because of a
    # not-really-necessary requirement of DeferredTensor.
    self.__name__ = str(getattr(create_value_fn, 'name', None) or
                        getattr(type(create_value_fn), '__name__', 'unknown'))
    super(CallOnce, self).__init__(name=self.__name__)

  @property
  def create_value_fn(self):
    return self._create_value_fn

  @property
  def value(self):
    return self._value

  def is_unset(self):
    """Returns `True` if there is no memoized value and `False` otherwise."""
    return self._value is _UNSET

  def __call__(self, *args, **kwargs):
    """Return the memoized value."""
    if self.is_unset():
      self._value = self._create_value_fn(*args, **kwargs)
      return self._value  # No need to to go through checks on first call.
    my_graph = getattr(self._value, 'graph', None)
    my_graph_was_deleted = (
        my_graph is None and
        # Don't check subclass; check parent only.
        type(self._value) is tf.Tensor)  # pylint: disable=unidiomatic-typecheck
    # We write `your_graph` as a lambda to ensure it's only evaluated when
    # necessary.
    your_graph = lambda: getattr(tf.constant([], dtype=tf.bool), 'graph', None)
    if my_graph_was_deleted or (
        my_graph is not None and my_graph is not your_graph()):
      raise ValueError(
          'You are attempting to access a memoized value from a different '
          'graph context. Please call `this.reset()` before accessing a '
          'memoized value from a different graph context.')
    return self._value

  def reset(self):
    """Removes memoized value which triggers re-eval on subsequent reads."""
    self._value = _UNSET

class _Unset(object):
  """Dummy object which exists to be unique from any possible user value."""

  def __repr__(self):
    return 'unset'

_UNSET = _Unset()


text:
import logging
import re
import secrets
from email.headerregistry import AddressHeader
from email.message import EmailMessage
from typing import Dict, List, Optional, Tuple

from django.conf import settings
from django.utils.timezone import now as timezone_now
from django.utils.timezone import timedelta

from zerver.lib.actions import (
    check_send_message,
    internal_send_huddle_message,
    internal_send_private_message,
    internal_send_stream_message,
)
from zerver.lib.email_mirror_helpers import (
    ZulipEmailForwardError,
    decode_email_address,
    get_email_gateway_message_string_from_address,
)
from zerver.lib.email_notifications import convert_html_to_markdown
from zerver.lib.exceptions import JsonableError, RateLimited
from zerver.lib.message import normalize_body, truncate_topic
from zerver.lib.queue import queue_json_publish
from zerver.lib.rate_limiter import RateLimitedObject
from zerver.lib.send_email import FromAddress
from zerver.lib.upload import upload_message_file
from zerver.models import (
    Message,
    MissedMessageEmailAddress,
    Realm,
    Recipient,
    Stream,
    UserProfile,
    get_client,
    get_display_recipient,
    get_stream_by_id_in_realm,
    get_system_bot,
    get_user,
)
from zproject.backends import is_user_active

logger = logging.getLogger(__name__)

def redact_email_address(error_message: str) -> str:
    if not settings.EMAIL_GATEWAY_EXTRA_PATTERN_HACK:
        domain = settings.EMAIL_GATEWAY_PATTERN.rsplit("@")[-1]
    else:
        # EMAIL_GATEWAY_EXTRA_PATTERN_HACK is of the form '@example.com'
        domain = settings.EMAIL_GATEWAY_EXTRA_PATTERN_HACK[1:]

    address_match = re.search("\\b(\\S*?)@" + domain, error_message)
    if address_match:
        email_address = address_match.group(0)
        # Annotate basic info about the address before scrubbing:
        if is_missed_message_address(email_address):
            redacted_message = error_message.replace(
                email_address, f"{email_address} <Missed message address>"
            )
        else:
            try:
                target_stream_id = decode_stream_email_address(email_address)[0].id
                annotated_address = f"{email_address} <Address to stream id: {target_stream_id}>"
                redacted_message = error_message.replace(email_address, annotated_address)
            except ZulipEmailForwardError:
                redacted_message = error_message.replace(
                    email_address, f"{email_address} <Invalid address>"
                )

        # Scrub the address from the message, to the form XXXXX@example.com:
        string_to_scrub = address_match.groups()[0]
        redacted_message = redacted_message.replace(string_to_scrub, "X" * len(string_to_scrub))
        return redacted_message

    return error_message

def report_to_zulip(error_message: str) -> None:
    if settings.ERROR_BOT is None:
        return
    error_bot = get_system_bot(settings.ERROR_BOT)
    error_stream = Stream.objects.get(name="errors", realm=error_bot.realm)
    send_zulip(
        error_bot,
        error_stream,
        "email mirror error",
        f"""~~~\n{error_message}\n~~~""",
    )

def log_and_report(email_message: EmailMessage, error_message: str, to: Optional[str]) -> None:
    recipient = to or "No recipient found"
    error_message = "Sender: {}\nTo: {}\n{}".format(
        email_message.get("From"), recipient, error_message
    )

    error_message = redact_email_address(error_message)
    logger.error(error_message)
    report_to_zulip(error_message)

# Temporary missed message addresses

def generate_missed_message_token() -> str:
    return "mm" + secrets.token_hex(16)

def is_missed_message_address(address: str) -> bool:
    try:
        msg_string = get_email_gateway_message_string_from_address(address)
    except ZulipEmailForwardError:
        return False

    return is_mm_32_format(msg_string)

def is_mm_32_format(msg_string: Optional[str]) -> bool:
    """
    Missed message strings are formatted with a little "mm" prefix
    followed by a randomly generated 32-character string.
    """
    return msg_string is not None and msg_string.startswith("mm") and len(msg_string) == 34

def get_missed_message_token_from_address(address: str) -> str:
    msg_string = get_email_gateway_message_string_from_address(address)

    if not is_mm_32_format(msg_string):
        raise ZulipEmailForwardError("Could not parse missed message address")

    return msg_string

def get_usable_missed_message_address(address: str) -> MissedMessageEmailAddress:
    token = get_missed_message_token_from_address(address)
    try:
        mm_address = MissedMessageEmailAddress.objects.select_related().get(
            email_token=token,
            timestamp__gt=timezone_now()
            - timedelta(seconds=MissedMessageEmailAddress.EXPIRY_SECONDS),
        )
    except MissedMessageEmailAddress.DoesNotExist:
        raise ZulipEmailForwardError("Missed message address expired or doesn't exist.")

    if not mm_address.is_usable():
        # Technical, this also checks whether the event is expired,
        # but that case is excluded by the logic above.
        raise ZulipEmailForwardError("Missed message address out of uses.")

    return mm_address

def create_missed_message_address(user_profile: UserProfile, message: Message) -> str:
    # If the email gateway isn't configured, we specify a reply
    # address, since there's no useful way for the user to reply into
    # Zulip.
    if settings.EMAIL_GATEWAY_PATTERN == "":
        return FromAddress.NOREPLY

    mm_address = MissedMessageEmailAddress.objects.create(
        message=message, user_profile=user_profile, email_token=generate_missed_message_token()
    )
    return str(mm_address)

def construct_zulip_body(
    message: EmailMessage,
    realm: Realm,
    show_sender: bool = False,
    include_quotes: bool = False,
    include_footer: bool = False,
    prefer_text: bool = True,
) -> str:
    body = extract_body(message, include_quotes, prefer_text)
    # Remove null characters, since Zulip will reject
    body = body.replace("\x00", "")
    if not include_footer:
        body = filter_footer(body)

    if not body.endswith("\n"):
        body += "\n"
    body += extract_and_upload_attachments(message, realm)
    if not body.rstrip():
        body = "(No email body)"

    if show_sender:
        sender = str(message.get("From", ""))
        body = f"From: {sender}\n{body}"

    return body

## Sending the Zulip ##

class ZulipEmailForwardUserError(ZulipEmailForwardError):
    pass

def send_zulip(sender: UserProfile, stream: Stream, topic: str, content: str) -> None:
    internal_send_stream_message(
        sender,
        stream,
        truncate_topic(topic),
        normalize_body(content),
        email_gateway=True,
    )

def send_mm_reply_to_stream(
    user_profile: UserProfile, stream: Stream, topic: str, body: str
) -> None:
    try:
        check_send_message(
            sender=user_profile,
            client=get_client("Internal"),
            message_type_name="stream",
            message_to=[stream.id],
            topic_name=topic,
            message_content=body,
        )
    except JsonableError as error:
        error_message = "Error sending message to stream {stream} via message notification email reply:\n{error}".format(
            stream=stream.name, error=error.msg
        )
        internal_send_private_message(
            get_system_bot(settings.NOTIFICATION_BOT), user_profile, error_message
        )

def get_message_part_by_type(message: EmailMessage, content_type: str) -> Optional[str]:
    charsets = message.get_charsets()

    for idx, part in enumerate(message.walk()):
        if part.get_content_type() == content_type:
            content = part.get_payload(decode=True)
            assert isinstance(content, bytes)
            if charsets[idx]:
                return content.decode(charsets[idx], errors="ignore")
            # If no charset has been specified in the header, assume us-ascii,
            # by RFC6657: https://tools.ietf.org/html/rfc6657
            else:
                return content.decode("us-ascii", errors="ignore")

    return None

def extract_body(
    message: EmailMessage, include_quotes: bool = False, prefer_text: bool = True
) -> str:
    plaintext_content = extract_plaintext_body(message, include_quotes)
    html_content = extract_html_body(message, include_quotes)

    if plaintext_content is None and html_content is None:
        logger.warning("Content types: %s", [part.get_content_type() for part in message.walk()])
        raise ZulipEmailForwardUserError("Unable to find plaintext or HTML message body")
    if not plaintext_content and not html_content:
        raise ZulipEmailForwardUserError("Email has no nonempty body sections; ignoring.")

    if prefer_text:
        if plaintext_content:
            return plaintext_content
        else:
            assert html_content  # Needed for mypy. Ensured by the validating block above.
            return html_content
    else:
        if html_content:
            return html_content
        else:
            assert plaintext_content  # Needed for mypy. Ensured by the validating block above.
            return plaintext_content

talon_initialized = False

def extract_plaintext_body(message: EmailMessage, include_quotes: bool = False) -> Optional[str]:
    import talon_core

    global talon_initialized
    if not talon_initialized:
        talon_core.init()
        talon_initialized = True

    plaintext_content = get_message_part_by_type(message, "text/plain")
    if plaintext_content is not None:
        if include_quotes:
            return plaintext_content
        else:
            return talon_core.quotations.extract_from_plain(plaintext_content)
    else:
        return None

def extract_html_body(message: EmailMessage, include_quotes: bool = False) -> Optional[str]:
    import talon_core

    global talon_initialized
    if not talon_initialized:  # nocoverage
        talon_core.init()
        talon_initialized = True

    html_content = get_message_part_by_type(message, "text/html")
    if html_content is not None:
        if include_quotes:
            return convert_html_to_markdown(html_content)
        else:
            return convert_html_to_markdown(talon_core.quotations.extract_from_html(html_content))
    else:
        return None

def filter_footer(text: str) -> str:
    # Try to filter out obvious footers.
    possible_footers = [line for line in text.split("\n") if line.strip() == "--"]
    if len(possible_footers) != 1:
        # Be conservative and don't try to scrub content if there
        # isn't a trivial footer structure.
        return text

    return re.split(r"^\s*--\s*$", text, 1, flags=re.MULTILINE)[0].strip()

def extract_and_upload_attachments(message: EmailMessage, realm: Realm) -> str:
    user_profile = get_system_bot(settings.EMAIL_GATEWAY_BOT)

    attachment_links = []
    for part in message.walk():
        content_type = part.get_content_type()
        filename = part.get_filename()
        if filename:
            attachment = part.get_payload(decode=True)
            if isinstance(attachment, bytes):
                s3_url = upload_message_file(
                    filename,
                    len(attachment),
                    content_type,
                    attachment,
                    user_profile,
                    target_realm=realm,
                )
                formatted_link = f"[{filename}]({s3_url})"
                attachment_links.append(formatted_link)
            else:
                logger.warning(
                    "Payload is not bytes (invalid attachment %s in message from %s).",
                    filename,
                    message.get("From"),
                )

    return "\n".join(attachment_links)

def decode_stream_email_address(email: str) -> Tuple[Stream, Dict[str, bool]]:
    token, options = decode_email_address(email)

    try:
        stream = Stream.objects.get(email_token=token)
    except Stream.DoesNotExist:
        raise ZulipEmailForwardError("Bad stream token from email recipient " + email)

    return stream, options

def find_emailgateway_recipient(message: EmailMessage) -> str:
    # We can't use Delivered-To; if there is a X-Gm-Original-To
    # it is more accurate, so try to find the most-accurate
    # recipient list in descending priority order
    recipient_headers = [
        "X-Gm-Original-To",
        "Delivered-To",
        "Envelope-To",
        "Resent-To",
        "Resent-CC",
        "To",
        "CC",
    ]

    pattern_parts = [re.escape(part) for part in settings.EMAIL_GATEWAY_PATTERN.split("%s")]
    match_email_re = re.compile(".*?".join(pattern_parts))

    for header_name in recipient_headers:
        for header_value in message.get_all(header_name, []):
            if isinstance(header_value, AddressHeader):
                emails = [addr.addr_spec for addr in header_value.addresses]
            else:
                emails = [str(header_value)]

            for email in emails:
                if match_email_re.match(email):
                    return email

    raise ZulipEmailForwardError("Missing recipient in mirror email")

def strip_from_subject(subject: str) -> str:
    # strips RE and FWD from the subject
    # from: https://stackoverflow.com/questions/9153629/regex-code-for-removing-fwd-re-etc-from-email-subject
    reg = r"([\[\(] *)?\b(RE|FWD?) *([-:;)\]][ :;\])-]*|$)|\]+ *$"
    stripped = re.sub(reg, "", subject, flags=re.IGNORECASE | re.MULTILINE)
    return stripped.strip()

def is_forwarded(subject: str) -> bool:
    # regex taken from strip_from_subject, we use it to detect various forms
    # of FWD at the beginning of the subject.
    reg = r"([\[\(] *)?\b(FWD?) *([-:;)\]][ :;\])-]*|$)|\]+ *$"
    return bool(re.match(reg, subject, flags=re.IGNORECASE))

def process_stream_message(to: str, message: EmailMessage) -> None:
    subject_header = message.get("Subject", "")
    subject = strip_from_subject(subject_header) or "(no topic)"

    stream, options = decode_stream_email_address(to)
    # Don't remove quotations if message is forwarded, unless otherwise specified:
    if "include_quotes" not in options:
        options["include_quotes"] = is_forwarded(subject_header)

    body = construct_zulip_body(message, stream.realm, **options)
    send_zulip(get_system_bot(settings.EMAIL_GATEWAY_BOT), stream, subject, body)
    logger.info(
        "Successfully processed email to %s (%s)",
        stream.name,
        stream.realm.string_id,
    )

def process_missed_message(to: str, message: EmailMessage) -> None:
    mm_address = get_usable_missed_message_address(to)
    mm_address.increment_times_used()

    user_profile = mm_address.user_profile
    topic = mm_address.message.topic_name()

    if mm_address.message.recipient.type == Recipient.PERSONAL:
        # We need to reply to the sender so look up their personal recipient_id
        recipient = mm_address.message.sender.recipient
    else:
        recipient = mm_address.message.recipient

    if not is_user_active(user_profile):
        logger.warning("Sending user is not active. Ignoring this message notification email.")
        return

    body = construct_zulip_body(message, user_profile.realm)

    if recipient.type == Recipient.STREAM:
        stream = get_stream_by_id_in_realm(recipient.type_id, user_profile.realm)
        send_mm_reply_to_stream(user_profile, stream, topic, body)
        recipient_str = stream.name
    elif recipient.type == Recipient.PERSONAL:
        display_recipient = get_display_recipient(recipient)
        assert not isinstance(display_recipient, str)
        recipient_str = display_recipient[0]["email"]
        recipient_user = get_user(recipient_str, user_profile.realm)
        internal_send_private_message(user_profile, recipient_user, body)
    elif recipient.type == Recipient.HUDDLE:
        display_recipient = get_display_recipient(recipient)
        assert not isinstance(display_recipient, str)
        emails = [user_dict["email"] for user_dict in display_recipient]
        recipient_str = ", ".join(emails)
        internal_send_huddle_message(user_profile.realm, user_profile, emails, body)
    else:
        raise AssertionError("Invalid recipient type!")

    logger.info(
        "Successfully processed email from user %s to %s",
        user_profile.id,
        recipient_str,
    )

def process_message(message: EmailMessage, rcpt_to: Optional[str] = None) -> None:
    to: Optional[str] = None

    try:
        if rcpt_to is not None:
            to = rcpt_to
        else:
            to = find_emailgateway_recipient(message)

        if is_missed_message_address(to):
            process_missed_message(to, message)
        else:
            process_stream_message(to, message)
    except ZulipEmailForwardUserError as e:
        # TODO: notify sender of error, retry if appropriate.
        logger.warning(e.args[0])
    except ZulipEmailForwardError as e:
        log_and_report(message, e.args[0], to)

def validate_to_address(rcpt_to: str) -> None:
    if is_missed_message_address(rcpt_to):
        get_usable_missed_message_address(rcpt_to)
    else:
        decode_stream_email_address(rcpt_to)

def mirror_email_message(rcpt_to: str, msg_base64: str) -> Dict[str, str]:
    try:
        validate_to_address(rcpt_to)
    except ZulipEmailForwardError as e:
        return {
            "status": "error",
            "msg": f"5.1.1 Bad destination mailbox address: {e}",
        }

    queue_json_publish(
        "email_mirror",
        {
            "rcpt_to": rcpt_to,
            "msg_base64": msg_base64,
        },
    )
    return {"status": "success"}

# Email mirror rate limiter code:

class RateLimitedRealmMirror(RateLimitedObject):
    def __init__(self, realm: Realm) -> None:
        self.realm = realm
        super().__init__()

    def key(self) -> str:
        return f"{type(self).__name__}:{self.realm.string_id}"

    def rules(self) -> List[Tuple[int, int]]:
        return settings.RATE_LIMITING_MIRROR_REALM_RULES

def rate_limit_mirror_by_realm(recipient_realm: Realm) -> None:
    ratelimited = RateLimitedRealmMirror(recipient_realm).rate_limit()[0]

    if ratelimited:
        raise RateLimited()


text:
. /usr/lib64/nagios/plugins/functions

CURL="/usr/bin/curl"
FLAG="/tmp/nginx_is_restarting_now"

# Nginx is restarted every night.
# Do not warning this job.
if [ -f $FLAG ]; then
  echo $FLAG
  exit $RET_OK
fi

# nrpe timeout is 10s
$CURL --max-time 4 -F"foo=bar" http://www.xuanran001.com:88/userdata/user/update
xuanran001_ret=$?

$CURL --max-time 4 -F"foo=bar" http://www.feilujiaju.com:88/userdata/user/update
feilujiaju_ret=$?

if [ $xuanran001_ret -eq 52 ]; then
  echo "curl(52): Failed to post request to nginx(xuanran001.com), bug:14711"
  exit $RET_CR
fi

if [ $xuanran001_ret -eq 56 ]; then
  echo "curl(56): Failed to post request to nginx(xuanran001.com), bug:14711"
  exit $RET_CR
fi

if [ $feilujiaju_ret -eq 52 ]; then
  echo "curl(52): Failed to post request to nginx(feilujiaju.com), bug:14711"
  exit $RET_CR
fi

if [ $xuanran001_ret -eq 7 ]; then
  echo "curl(7): Couldn't connect to host(xuanran001.com), maybe nginx is stopped."
  exit $RET_CR
fi

if [ $xuanran001_ret -eq 28 ]; then
  echo "curl(28): Failed to post request to nginx(xuanran001.com), 9s timeout."
  exit $RET_CR
fi

if [ $xuanran001_ret -ne 0 ]; then
  echo "curl($xuanran001_ret): Failed to post request to nginx(xuanran001.com), unknown error."
  exit $RET_WR
fi

echo "OK"
exit $RET_OK


text:
package org.sagebionetworks.web.client.widget.entity.renderer;

import com.google.gwt.user.client.ui.IsWidget;

public interface WikiSubpagesOrderEditorView extends IsWidget {
  void configure(WikiSubpageOrderEditorTree subpageTree);

  void setSynAlert(IsWidget w);

  void initializeState();

  void setLoadingVisible(boolean visible);
}


text:
package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"strings"
)

func main() {
	err := filepath.Walk(".", visit)
	if err != nil {
		log.Fatal(err)
	}
}

func visit(path string, info os.FileInfo, err error) error {
	if info.IsDir() {
		if strings.HasPrefix(path, "Externals") {
			return filepath.SkipDir
		}

		return nil
	}

	if !(strings.HasSuffix(path, ".swift") || strings.HasSuffix(path, ".h") || strings.HasSuffix(path, ".md") || strings.HasSuffix(path, ".m")) {
		return nil
	}

	fileBytes, fileReadErr := ioutil.ReadFile(path)
	if fileReadErr != nil {
		return fileReadErr
	}

	fileReader := bytes.NewBuffer(fileBytes)

	fileScanner := bufio.NewScanner(fileReader)

	trimmedLines := []string{}
	for fileScanner.Scan() {
		line := fileScanner.Text()
		trimmedLine := strings.TrimRight(line, " \t\n")
		trimmedLines = append(trimmedLines, trimmedLine)
	}

	if fileScanner.Err() != nil {
		return fileScanner.Err()
	}

	tempFilePath := fmt.Sprintf("%s-tmp", path)
	trimmedTempFile, trimmedFileErr := os.Create(tempFilePath)
	if trimmedFileErr != nil {
		return trimmedFileErr
	}

	for _, trimmedLine := range trimmedLines {
		trimmedLineWithNewline := fmt.Sprintf("%s\n", trimmedLine)
		_, writeErr := trimmedTempFile.WriteString(trimmedLineWithNewline)
		if writeErr != nil {
			return writeErr
		}
	}

	swapContentErr := os.Rename(tempFilePath, path)
	if swapContentErr != nil {
		return swapContentErr
	}

	return nil
}


text:
 //===----------------------------------------------------------------------===//
//
//                         Peloton
//
// selectivity_test.cpp
//
// Identification: test/optimizer/selectivity_test.cpp
//
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#include "common/harness.h"

#include <vector>

#include "common/logger.h"
#include "optimizer/stats/selectivity.h"
#include "optimizer/stats/tuple_samples_storage.h"
#include "optimizer/stats/stats_storage.h"
#include "optimizer/stats/value_condition.h"
#include "optimizer/stats/table_stats.h"
#include "type/type.h"
#include "type/value.h"
#include "type/value_factory.h"
#include "sql/testing_sql_util.h"
#include "catalog/catalog.h"
#include "catalog/column_catalog.h"
#include "executor/testing_executor_util.h"

#define private public

namespace peloton {
namespace test {

using namespace optimizer;

class SelectivityTests : public PelotonTest {};

const std::string TEST_TABLE_NAME = "test";

// Equality checking accuracy offset
const double DEFAULT_SELECTIVITY_OFFSET = 0.01;

void CreateAndLoadTable() {
  TestingSQLUtil::ExecuteSQLQuery(
      "CREATE TABLE test(id INT PRIMARY KEY, b DECIMAL, c VARCHAR);");
}

// Utility function for compare approximate equality.
void ExpectSelectivityEqual(double actual, double expected,
                            double offset = DEFAULT_SELECTIVITY_OFFSET) {
  EXPECT_GE(actual, expected - offset);
  EXPECT_LE(actual, expected + offset);
}

// Test range selectivity including >, <, >= and <= using uniform dataset
TEST_F(SelectivityTests, RangeSelectivityTest) {
  auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
  auto txn = txn_manager.BeginTransaction();
  catalog::Catalog::GetInstance()->CreateDatabase(DEFAULT_DB_NAME, txn);
  txn_manager.CommitTransaction(txn);

  CreateAndLoadTable();

  // Create **uniform** table stats
  int nrow = 100;
  for (int i = 1; i <= nrow; i++) {
    std::stringstream ss;
    ss << "INSERT INTO test VALUES (" << i << ", 1.1, 'abcd');";
    TestingSQLUtil::ExecuteSQLQuery(ss.str());
  }

  auto catalog = catalog::Catalog::GetInstance();
  auto database = catalog->GetDatabaseWithName(DEFAULT_DB_NAME);
  auto table = catalog->GetTableWithName(DEFAULT_DB_NAME, TEST_TABLE_NAME);
  oid_t db_id = database->GetOid();
  oid_t table_id = table->GetOid();
  oid_t column_id = 0;  // first column
  auto stats_storage = StatsStorage::GetInstance();
  auto table_stats = stats_storage->GetTableStats(db_id, table_id);
  type::Value value1 = type::ValueFactory::GetIntegerValue(nrow / 4);
  ValueCondition condition{column_id, ExpressionType::COMPARE_LESSTHAN, value1};

  // Check for default selectivity when table stats does not exist.
  double default_sel = Selectivity::ComputeSelectivity(table_stats, condition);
  EXPECT_EQ(default_sel, DEFAULT_SELECTIVITY);

  // Run analyze
  TestingSQLUtil::ExecuteSQLQuery("ANALYZE test");

  // Get updated table stats and check new selectivity
  table_stats = stats_storage->GetTableStats(db_id, table_id);
  double less_than_sel =
      Selectivity::ComputeSelectivity(table_stats, condition);
  ExpectSelectivityEqual(less_than_sel, 0.25);

  condition = std::move(
      ValueCondition{column_id, ExpressionType::COMPARE_GREATERTHAN, value1});
  double greater_than_sel =
      Selectivity::ComputeSelectivity(table_stats, condition);
  ExpectSelectivityEqual(greater_than_sel, 0.75);

  // Free the database
  txn = txn_manager.BeginTransaction();
  catalog::Catalog::GetInstance()->DropDatabaseWithName(DEFAULT_DB_NAME, txn);
  txn_manager.CommitTransaction(txn);
}

// Test LIKE operator selectivity
// Note LIKE operator is not yet implemented so this test only
// checks if getting column stats and sampling works.
TEST_F(SelectivityTests, LikeSelectivityTest) {
  const int tuple_count = 1000;
  const int tuple_per_tilegroup = 100;

  // Create a table
  auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
  auto txn = txn_manager.BeginTransaction();
  std::unique_ptr<storage::DataTable> data_table(
      TestingExecutorUtil::CreateTable(tuple_per_tilegroup, false));
  TestingExecutorUtil::PopulateTable(data_table.get(), tuple_count, false,
                                     false, true, txn);
  txn_manager.CommitTransaction(txn);

  // Collect samples and add samples.
  txn = txn_manager.BeginTransaction();
  TupleSamplesStorage *tuple_samples_storage =
      TupleSamplesStorage::GetInstance();
  tuple_samples_storage->CollectSamplesForTable(data_table.get(), txn);
  txn_manager.CommitTransaction(txn);

  oid_t db_id = data_table->GetDatabaseOid();
  oid_t table_id = data_table->GetOid();
  type::Value value = type::ValueFactory::GetVarcharValue("a");
  auto stats_storage = StatsStorage::GetInstance();
  auto table_stats = stats_storage->GetTableStats(db_id, table_id);

  ValueCondition condition1{0, ExpressionType::COMPARE_LIKE, value};
  ValueCondition condition2{1, ExpressionType::COMPARE_LIKE, value};
  ValueCondition condition3{2, ExpressionType::COMPARE_LIKE, value};
  ValueCondition condition4{3, ExpressionType::COMPARE_LIKE, value};

  double like_than_sel_1 =
      Selectivity::ComputeSelectivity(table_stats, condition1);
  double like_than_sel_2 =
      Selectivity::ComputeSelectivity(table_stats, condition2);
  double like_than_sel_3 =
      Selectivity::ComputeSelectivity(table_stats, condition3);
  double like_than_sel_4 =
      Selectivity::ComputeSelectivity(table_stats, condition4);

  EXPECT_EQ(like_than_sel_1, DEFAULT_SELECTIVITY);
  EXPECT_EQ(like_than_sel_2, DEFAULT_SELECTIVITY);
  EXPECT_EQ(like_than_sel_3, DEFAULT_SELECTIVITY);
  EXPECT_EQ(like_than_sel_4, DEFAULT_SELECTIVITY);
}

TEST_F(SelectivityTests, EqualSelectivityTest) {
  auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
  auto txn = txn_manager.BeginTransaction();
  catalog::Catalog::GetInstance()->CreateDatabase(DEFAULT_DB_NAME, txn);
  txn_manager.CommitTransaction(txn);

  CreateAndLoadTable();

  int nrow = 100;
  for (int i = 1; i <= nrow; i++) {
    std::stringstream ss;
    ss << "INSERT INTO test VALUES (" << i << ", " << i % 3 + 1 << ", 'string');";
    TestingSQLUtil::ExecuteSQLQuery(ss.str());
  }

  auto catalog = catalog::Catalog::GetInstance();
  auto database = catalog->GetDatabaseWithName(DEFAULT_DB_NAME);
  auto table = catalog->GetTableWithName(DEFAULT_DB_NAME, TEST_TABLE_NAME);
  oid_t db_id = database->GetOid();
  oid_t table_id = table->GetOid();
  oid_t column_id1 = 1;
  auto stats_storage = StatsStorage::GetInstance();
  auto table_stats = stats_storage->GetTableStats(db_id, table_id);

  type::Value value1 = type::ValueFactory::GetDecimalValue(1.0);

  // Check for default selectivity when table stats does not exist.
  ValueCondition condition1{column_id1, ExpressionType::COMPARE_EQUAL, value1};
  double sel = Selectivity::ComputeSelectivity(table_stats, condition1);
  EXPECT_EQ(sel, DEFAULT_SELECTIVITY);

  // Run analyze
  TestingSQLUtil::ExecuteSQLQuery("ANALYZE test");
  table_stats = stats_storage->GetTableStats(db_id, table_id);

  // Check selectivity
  // equal, in mcv
  double eq_sel_in_mcv =
      Selectivity::ComputeSelectivity(table_stats, condition1);
  condition1 = std::move(
      ValueCondition{column_id1, ExpressionType::COMPARE_NOTEQUAL, value1});
  double neq_sel_in_mcv =
      Selectivity::ComputeSelectivity(table_stats, condition1);
  ExpectSelectivityEqual(eq_sel_in_mcv, 0.33);
  ExpectSelectivityEqual(neq_sel_in_mcv, 0.67);

  // Add other values into the table
  // default top_k == 10, so add another 10 - 3 = 7 elements (4-10)
  for (int i = 1; i <= nrow; i++) {
    std::stringstream ss;
    ss << "INSERT INTO test VALUES (" << i + 1000 << ", " << i % 7 + 4
       << ", 'string');";
    TestingSQLUtil::ExecuteSQLQuery(ss.str());
  }
  // these elements will not be in mcv
  for (int i = 1; i <= nrow; i++) {
    std::stringstream ss;
    ss << "INSERT INTO test VALUES (" << i + 2000 << ", " << i % 50 + 11
       << ", 'string');";
    TestingSQLUtil::ExecuteSQLQuery(ss.str());
  }

  // Run analyze
  TestingSQLUtil::ExecuteSQLQuery("ANALYZE test");
  table_stats = stats_storage->GetTableStats(db_id, table_id);

  // Check selectivity
  // equal, not in mcv
  oid_t column_id2 = 0;
  type::Value value2 = type::ValueFactory::GetDecimalValue(20.0);
  ValueCondition condition2{column_id2, ExpressionType::COMPARE_EQUAL, value2};

  double eq_sel_nin_mcv =
      Selectivity::ComputeSelectivity(table_stats, condition2);
  condition2 = std::move(
      ValueCondition{column_id2, ExpressionType::COMPARE_NOTEQUAL, value2});
  double neq_sel_nin_mcv =
      Selectivity::ComputeSelectivity(table_stats, condition2);
  // (1 - 2/3) / (3 + 7 + 50 - 10) = 1 / 150 = 0.01667
  ExpectSelectivityEqual(eq_sel_nin_mcv, 0.0066, 0.01);
  ExpectSelectivityEqual(neq_sel_nin_mcv, 0.9933, 0.01);

  // Free the database
  txn = txn_manager.BeginTransaction();
  catalog::Catalog::GetInstance()->DropDatabaseWithName(DEFAULT_DB_NAME, txn);
  txn_manager.CommitTransaction(txn);
}

} /* namespace test */
} /* namespace peloton */


text:
header='================='
subheader='-----------------'
cat <<- eof
	(Insert title here)
	$header

	Introduction
	$subheader

	Installation
	$subheader

	Usage
	$subheader

	More Program Options
	$subheader

	Software Used
	$subheader

	1.
	2.
	3.
	4.

	Support
	$subheader

	If you have any questions/concerns, please feel free to contact me.
	My email address is tanzhao@umich.edu
eof

# Add shell script message here that displays current progress of the program.


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

package com.boundary.plugin.sdk.bootstrap;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 * Utility class for downloading a plugin from a release in GitHub
 */
public class DownloadPluginJar {

	private final static String POM_PATH="pom.xml";
	private final static String JAR_DESTINATION_PATH="config/plugin.jar";
	private final static String VERSION_XPATH_EXPRESSION = "/project/version";
	private final static String BASE_URL_XPATH_EXPRESSION = "/project/properties/boundary-jar-base-url";
	private String version;
	private String baseUrl;

	public DownloadPluginJar() {

	}

	/**
	 * Reads the pom.xml file to extract information to find the jar plugin.
	 *
	 * @param pomFile Maven POM file path
	 * @throws Exception
	 */
	private void readPOM() throws Exception {
		DocumentBuilder parser = DocumentBuilderFactory.newInstance()
				.newDocumentBuilder();
		try {
			FileInputStream input = new FileInputStream(new File(POM_PATH));
			try {
				Document document = parser.parse(input);

				XPath xpath = XPathFactory.newInstance().newXPath();
				this.version = (String) xpath.evaluate(
						VERSION_XPATH_EXPRESSION, document,
						XPathConstants.STRING);
				this.baseUrl = (String) xpath.evaluate(
						BASE_URL_XPATH_EXPRESSION, document,
						XPathConstants.STRING);
				if (this.baseUrl.length() == 0) {
					throw new Exception(String.format("Base URL not found in %s%n",POM_PATH));
				}
				input.close();
			} catch (IOException e) {
				throw e;
			} finally {
				input.close();
			}
		} catch (FileNotFoundException e) {
			throw e;
		}
	}

	/**
	 * Downloads a jar file based on pom.xml information
	 * to the configuration directory config
	 *
	 * @throws IOException Any kind of IO error occurs
	 */
	private void downloadJAR() throws IOException {
		OutputStream out = null;

		HttpsURLConnection connection = null;
		String sUrl = String.format("%s/%s/plugin.jar", this.baseUrl,
				this.version);
		System.err.println(sUrl);
		URL url = new URL(sUrl);
		connection = (HttpsURLConnection) url.openConnection();
//		connection.addRequestProperty("Accept","application/zip");
		// Ensure that we follow redirects
		HttpsURLConnection.setFollowRedirects(true);
		connection.connect();

		InputStream in = connection.getInputStream();
		System.err.printf("HTTP Response %d%n", connection.getResponseCode());

		out = new FileOutputStream(new File(JAR_DESTINATION_PATH));
		byte[] b = new byte[1024];

		System.err.printf("Downloading %s of %d bytes from %s...",
				JAR_DESTINATION_PATH, connection.getContentLength(),
				connection.getURL());

		while (in.read(b) != -1) {
			out.write(b);
		}

		out.close();
	}

	/**
	 * Downloads the plugins jar file to the configuration directory config/plugin.jar
	 */
	public int execute(String[] args) {
		int result = 1;
		System.err.println("Running post extract...");
		try {
			readPOM();
			downloadJAR();
			System.err.println("Download successful");
			result = 0;
		} catch (Exception e) {
			System.err.printf("%s%n",e.getMessage());
		}
		// Ensure that the standard error is flushed before exiting.
		System.err.flush();
		return result;
	}

	/**
	 * Main execution
	 * @param args Arguments passed on the command line
	 */
	public static void main(String [] args) {
		DownloadPluginJar postExtract = new DownloadPluginJar();
		postExtract.execute(args);
	}
}


text:
(function(addon) {

    if (typeof define == "function" && define.amd) { // AMD
        define("uikit-form-select", ["uikit"], function(){
            return jQuery.UIkit.formSelect || addon(window, window.jQuery, window.jQuery.UIkit);
        });
    }

    if(window && window.jQuery && window.jQuery.UIkit) {
        addon(window, window.jQuery, window.jQuery.UIkit);
    }

})(function(global, $, UI){

    UI.component('formSelect', {
        defaults: {
            'target': '>span:first'
        },

        init: function() {
            var $this = this;

            this.target  = this.find(this.options.target);
            this.select  = this.find('select');

            // init + on change event
            this.select.on("change", (function(){

                var select = $this.select[0], fn = function(){

                    try {
                        $this.target.text(select.options[select.selectedIndex].text);
                    } catch(e) {}

                    return fn;
                };

                return fn();
            })());

            this.element.data("formSelect", this);
        }
    });

    // init code
    $(document).on("uk-domready", function(e) {

        $("[data-uk-form-select]").each(function(){
            var ele = $(this);

            if (!ele.data("formSelect")) {
                var obj = UI.formSelect(ele, UI.Utils.options(ele.attr("data-uk-form-select")));
            }
        });
    });

    return UI.formSelect;
});


text:
package org.tic.vertx.bp.serverless;

public class VertxHandler {
}


text: - Initial release of the chef-yourls cookbook


text:
import * as React from 'react';

export interface StatisticsWidgetProps {
  dataHook?: string;
  size?: 'large' | 'tiny';
  alignItems?: 'center' | 'start' | 'end';
  items?: StatisticsWidgetItem[];
}

export default class StatisticsWidget extends React.PureComponent<StatisticsWidgetProps> {}

export type StatisticsWidgetItem = {
  value: string;
  valueInShort?: string;
  description?: string;
  descriptionInfo?: string;
  percentage?: number;
  invertedPercentage?: boolean;
  onClick?: (
    event: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>,
  ) => void;
  children?: React.ReactNode;
};


text:
System.register("ionic/core/gestures/edge-drag-gesture", ["./drag-gesture"], function($__export) {
  "use strict";
  var __moduleName = "ionic/core/gestures/edge-drag-gesture";
  var DragGesture,
      EdgeDragGesture;
  return {
    setters: [function($__m) {
      DragGesture = $__m.DragGesture;
    }],
    execute: function() {
      EdgeDragGesture = (function($__super) {
        var EdgeDragGesture = function EdgeDragGesture() {
          $traceurRuntime.superConstructor(EdgeDragGesture).apply(this, arguments);
        };
        return ($traceurRuntime.createClass)(EdgeDragGesture, {}, {}, $__super);
      }(DragGesture));
    }
  };
});


text:
#include "extensions/tracers/dynamic_ot/config.h"

#include "envoy/config/trace/v3/trace.pb.h"
#include "envoy/config/trace/v3/trace.pb.validate.h"
#include "envoy/registry/registry.h"

#include "common/common/utility.h"
#include "common/tracing/http_tracer_impl.h"

#include "extensions/tracers/dynamic_ot/dynamic_opentracing_driver_impl.h"
#include "extensions/tracers/well_known_names.h"

namespace Envoy {
namespace Extensions {
namespace Tracers {
namespace DynamicOt {

DynamicOpenTracingTracerFactory::DynamicOpenTracingTracerFactory()
    : FactoryBase(TracerNames::get().DynamicOt) {}

Tracing::HttpTracerPtr DynamicOpenTracingTracerFactory::createHttpTracerTyped(
    const envoy::config::trace::v3::DynamicOtConfig& proto_config, Server::Instance& server) {
  const std::string& library = proto_config.library();
  const std::string config = MessageUtil::getJsonStringFromMessage(proto_config.config());
  Tracing::DriverPtr dynamic_driver =
      std::make_unique<DynamicOpenTracingDriver>(server.stats(), library, config);
  return std::make_unique<Tracing::HttpTracerImpl>(std::move(dynamic_driver), server.localInfo());
}

/**
 * Static registration for the dynamic opentracing tracer. @see RegisterFactory.
 */
REGISTER_FACTORY(DynamicOpenTracingTracerFactory,
                 Server::Configuration::TracerFactory){"envoy.dynamic.ot"};

} // namespace DynamicOt
} // namespace Tracers
} // namespace Extensions
} // namespace Envoy


text:
package com.brajagopal.rmend.utils;

import com.google.gson.JsonObject;

/**
 * @author <bxr4261>
 */
public class SerDeUtils {

    public static String getValue(JsonObject jsonObject, String key, String defaultValue) {

        if (jsonObject.has(key)) {
            return jsonObject.get(key).getAsString();
        }
        else {
            return defaultValue;
        }
    }

    public static Long getValue(JsonObject jsonObject, String key, Long defaultValue) {

        if (jsonObject.has(key)) {
            return jsonObject.get(key).getAsLong();
        }
        else {
            return defaultValue;
        }
    }

    public static Double getValue(JsonObject jsonObject, String key, Double defaultValue) {

        if (jsonObject.has(key)) {
            return jsonObject.get(key).getAsDouble();
        }
        else {
            return defaultValue;
        }
    }

    public static Integer getValue(JsonObject jsonObject, String key, Integer defaultValue) {

        if (jsonObject.has(key)) {
            return jsonObject.get(key).getAsInt();
        }
        else {
            return defaultValue;
        }
    }
}