text:
/* SPDX-License-Identifier: BSD-2-Clause */
/*******************************************************************************
 * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
 * All rights reserved.
 *******************************************************************************/

#ifndef ESYS_TYPES_H
#define ESYS_TYPES_H

#define ESYS_MAX_SIZE_METADATA 3072

typedef UINT32 TSS2_ESYS_RC;

#ifndef TSS2_RC_SUCCESS
#define TSS2_RC_SUCCESS 0
#endif

/** Type of resource
 */
typedef UINT32 IESYSC_RESOURCE_TYPE_CONSTANT;
#define IESYSC_KEY_RSRC                1    /**< Tag for key resource */
#define IESYSC_NV_RSRC                 2    /**< Tag for NV Ram resource */
#define IESYSC_SESSION_RSRC            3    /**< Tag for session resources */
#define IESYSC_DEGRADED_SESSION_RSRC   4    /**< Tag for degraded session resources */
#define IESYSC_WITHOUT_MISC_RSRC       0    /**< Tag for other resources, e.g. PCR register, hierarchies */

/** Type to indicate parameter encryption (by TPM)
 */
typedef UINT32 IESYSC_PARAM_ENCRYPT;
#define ENCRYPT                        1    /**< Parameter encryption by TPM */
#define NO_ENCRYPT                     0    /**< No parameter encryption by TPM */

/** Type to indicate parameter decryption (by TPM)
 */
typedef UINT32 IESYSC_PARAM_DECRYPT;
#define DECRYPT                        1    /**< Parameter decryption by TPM */
#define NO_DECRYPT                     0    /**< No parameter decryption by TPM */

/** Type of policy authorization
 */
typedef UINT32 IESYSC_TYPE_POLICY_AUTH;
#define POLICY_PASSWORD                2    /**< Marker to include auth value of the authorized object */
#define POLICY_AUTH                    1    /**< Marker to include the auth value in the HMAC key */
#define NO_POLICY_AUTH                 0    /**< no special handling */

/** Type for representing TPM-Session
 */
typedef struct {
    TPM2B_NAME                             bound_entity;    /**< Entity to which the session is bound */
    TPM2B_ENCRYPTED_SECRET                encryptedSalt;    /**< Encrypted salt which can be provided by application */
    TPM2B_DATA                                     salt;    /**< Salt computed if no encrypted salt is provided */
    TPMT_SYM_DEF                              symmetric;    /**< Algorithm selection for parameter encryption */
    TPMI_ALG_HASH                              authHash;    /**< Hashalg used for authorization */
    TPM2B_DIGEST                             sessionKey;    /**< sessionKey used for KDFa to compute symKey */
    TPM2_SE                                 sessionType;    /**< Type of the session (HMAC, Policy) */
    TPMA_SESSION                      sessionAttributes;    /**< Flags which define the session behaviour */
    TPMA_SESSION                  origSessionAttributes;    /**< Copy of flags which define the session behaviour */
    TPM2B_NONCE                             nonceCaller;    /**< Nonce computed by the ESAPI for every session call */
    TPM2B_NONCE                                nonceTPM;    /**< Nonce which is returned by the TPM for every session call */
    IESYSC_PARAM_ENCRYPT                        encrypt;    /**< Indicate parameter encryption by the TPM */
    IESYSC_PARAM_DECRYPT                        decrypt;    /**< Indicate parameter decryption by the TPM */
    IESYSC_TYPE_POLICY_AUTH         type_policy_session;    /**< Field to store markers for policy sessions */
    UINT16                             sizeSessionValue;    /**< Size of sessionKey plus optionally authValue */
    BYTE                 sessionValue [2*sizeof(TPMU_HA)];    /**< sessionKey || AuthValue */
    UINT16                                sizeHmacValue;    /**< Size of sessionKey plus optionally authValue */
} IESYS_SESSION;

/** Selector type for esys resources
 */
typedef UINT32                  IESYSC_RESOURCE_TYPE;

/** Type for representing public info of a TPM-Resource
 */
typedef union {
    TPM2B_PUBLIC                           rsrc_key_pub;    /**< Public info for key objects */
    TPM2B_NV_PUBLIC                         rsrc_nv_pub;    /**< Public info for NV ram objects */
    IESYS_SESSION                          rsrc_session;    /**< Internal esapi session information */
    TPMS_EMPTY                               rsrc_empty;    /**< no specialized date for resource */
} IESYS_RSRC_UNION;

/** Type for representing TPM-Resource
 */
typedef struct {
    TPM2_HANDLE                                  handle;    /**< Handle used by TPM */
    TPM2B_NAME                                     name;    /**< TPM name of the object */
    IESYSC_RESOURCE_TYPE                       rsrcType;    /**< Selector for resource type */
    IESYS_RSRC_UNION                               misc;    /**< Resource specific information */
} IESYS_RESOURCE;

/**  Esys resource with size field
 */
typedef struct {
    UINT16                                         size;    /**< size of the operand buffer */
    IESYS_RESOURCE                                 data;    /**< Esys resource data */

} IESYS_METADATA;

/** Type for representing ESYS metadata
 */
typedef struct {
    UINT32                                     reserved;    /**< Must allways be zero */
    TPM2B_CONTEXT_DATA                       tpmContext;    /**< Context information computed by tpm */
    IESYS_METADATA                         esysMetadata;    /**< Meta data of the ESY_TR object */
} IESYS_CONTEXT_DATA;

#endif /* ESYS_TYPES_H */


text:
using Refit;
using System.Threading.Tasks;

namespace Newbe.Mahua.CQP.ApiExtensions
{
    public interface IQqGroupApi
    {
        [Post("/cgi-bin/announce/add_qun_notice")]
        Task<string> SetNotice([Body(BodySerializationMethod.UrlEncoded)]SetNoticeInput input);
    }

    public class SetNoticeInput
    {
        [AliasAs("bkn")]
        public string Bkn { get; set; }

        [AliasAs("qid")]
        public string Qid { get; set; }

        [AliasAs("title")]
        public string Title { get; set; }

        [AliasAs("text")]
        public string Text { get; set; }
    }
}

redpajama_set_name: RedPajamaGithub


text:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <litepal>
        <dbname value="cool_weather" />
        <version value="1"/>
        <list>
            <mapping class="com.example.accfcx.coolweather.db.Province" />
            <mapping class="com.example.accfcx.coolweather.db.City" />
            <mapping class="com.example.accfcx.coolweather.db.Country" />
        </list>
    </litepal>
</resources>


text:
package warriors;

/**
 * Created by dovbysh on 10.02.2015.
 */
public interface Warrior {

    String getWarriorClass();
}


text:
use std::panic::{UnwindSafe, catch_unwind};

use neon_runtime;
use neon_runtime::raw;

use context::Context;
use result::{NeonResult, Throw};
use types::{Value, Object, Handle, Managed, build};
use types::internal::ValueInternal;
use types::utf8::Utf8;

/// A JS `Error` object.
#[repr(C)]
#[derive(Clone, Copy)]
pub struct JsError(raw::Local);

impl Managed for JsError {
    fn to_raw(self) -> raw::Local { self.0 }

    fn from_raw(h: raw::Local) -> Self { JsError(h) }
}

impl ValueInternal for JsError {
    fn name() -> String { "Error".to_string() }

    fn is_typeof<Other: Value>(other: Other) -> bool {
        unsafe { neon_runtime::tag::is_error(other.to_raw()) }
    }
}

impl Value for JsError { }

impl Object for JsError { }

impl JsError {
    /// Creates a direct instance of the [`Error`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error) class.
    pub fn error<'a, C: Context<'a>, S: AsRef<str>>(cx: &mut C, msg: S) -> NeonResult<Handle<'a, JsError>> {
        let msg = cx.string(msg.as_ref());
        build(|out| unsafe {
            neon_runtime::error::new_error(out, msg.to_raw());
            true
        })
    }

    /// Creates an instance of the [`TypeError`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/TypeError) class.
    pub fn type_error<'a, C: Context<'a>, S: AsRef<str>>(cx: &mut C, msg: S) -> NeonResult<Handle<'a, JsError>> {
        let msg = cx.string(msg.as_ref());
        build(|out| unsafe {
            neon_runtime::error::new_type_error(out, msg.to_raw());
            true
        })
    }

    /// Creates an instance of the [`RangeError`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RangeError) class.
    pub fn range_error<'a, C: Context<'a>, S: AsRef<str>>(cx: &mut C, msg: S) -> NeonResult<Handle<'a, JsError>> {
        let msg = cx.string(msg.as_ref());
        build(|out| unsafe {
            neon_runtime::error::new_range_error(out, msg.to_raw());
            true
        })
    }
}

pub(crate) fn convert_panics<T, F: UnwindSafe + FnOnce() -> NeonResult<T>>(f: F) -> NeonResult<T> {
    match catch_unwind(|| { f() }) {
        Ok(result) => result,
        Err(panic) => {
            let msg = if let Some(string) = panic.downcast_ref::<String>() {
                format!("internal error in Neon module: {}", string)
            } else if let Some(str) = panic.downcast_ref::<&str>() {
                format!("internal error in Neon module: {}", str)
            } else {
                "internal error in Neon module".to_string()
            };
            let (data, len) = Utf8::from(&msg[..]).truncate().lower();
            unsafe {
                neon_runtime::error::throw_error_from_utf8(data, len);
                Err(Throw)
            }
        }
    }
}


text:
/* stylelint-disable no-descending-specificity */
.UnitDoc {
  margin-bottom: 2rem;
  word-break: break-word;
}
.UnitDoc h2 a.UnitDoc-idLink,
.UnitDoc summary a {
  opacity: 0;
}
.UnitDoc h2:hover a,
.UnitDoc summary:focus a {
  opacity: 1;
}
.UnitDoc-title {
  border-bottom: var(--border);
  padding-bottom: 1rem;
}
.UnitDoc-title img {
  margin: auto 1rem auto 0;
}
.UnitDoc-emptySection {
  background-color: var(--color-background-accented);
  color: var(--color-text-subtle);
  height: 12.25rem;
  margin-top: 1.5rem;
  text-align: center;
}
.UnitDoc-emptySection img {
  height: 7.8125rem;
  width: auto;
}
.Documentation .UnitDoc-emptySection p {
  margin: 1rem auto;
}
.UnitDoc .Documentation h4 {
  margin-top: 1.5rem;
}
.Documentation {
  display: block;
}
.Documentation p {
  margin: 1rem 0;
}
.Documentation h2,
.Documentation h3 {
  margin-top: 1.5rem;
}
.Documentation a {
  text-decoration: none;
}
.Documentation a:hover {
  text-decoration: underline;
}
.Documentation h2 a,
.Documentation h3 a,
.Documentation h4 a.Documentation-idLink,
.Documentation summary a {
  opacity: 0;
}
.Documentation a:focus {
  opacity: 1;
}
.Documentation h3 a.Documentation-source {
  opacity: 1;
}
.Documentation h2:hover a,
.Documentation h3:hover a,
.Documentation h4:hover a,
.Documentation summary:hover a,
.Documentation summary:focus a {
  opacity: 1;
}
.Documentation ul {
  line-height: 1.5rem;
  list-style: none;
  padding-left: 0;
}
.Documentation ul ul {
  padding-left: 2em;
}

.Documentation .Documentation-bulletList {
  list-style: disc;
  padding-left: 2rem;
  margin-bottom: 1rem;
}

.Documentation .Documentation-numberList {
  list-style: decimal;
  padding-left: 2rem;
  margin-bottom: 1rem;
}
.Documentation pre + pre {
  margin-top: 0.625rem;
}

.Documentation .Documentation-declarationLink + pre {
  border-radius: 0 0 0.3em 0.3em;
  border-top: var(--border);
  margin-top: 0;
}
.Documentation pre .comment {
  color: var(--color-code-comment);
}

.Documentation-toc,
.Documentation-overview,
.Documentation-index,
.Documentation-examples {
  padding-bottom: 0;
}
.Documentation-empty {
  color: var(--color-text-subtle);
  margin-top: -0.5rem;
}
@media only screen and (min-width: 64rem) {
  .Documentation-toc {
    margin-left: 2rem;
    white-space: nowrap;
  }
  .Documentation-toc-columns {
    columns: 2;
  }
}
.Documentation-toc:empty {
  display: none;
}
.Documentation-tocItem {
  overflow: hidden;
  text-overflow: ellipsis;
}

.Documentation-tocItem--constants,
.Documentation-tocItem--funcsAndTypes,
.Documentation-tocItem--functions,
.Documentation-tocItem--types,
.Documentation-tocItem--variables,
.Documentation-tocItem--notes {
  display: none;
}

.Documentation-overviewHeader,
.Documentation-indexHeader,
.Documentation-constantsHeader,
.Documentation-variablesHeader,
.Documentation-examplesHeader,
.Documentation-filesHeader,
.Documentation-functionHeader,
.Documentation-typeHeader,
.Documentation-typeMethodHeader,
.Documentation-typeFuncHeader {
  margin-bottom: 0.5rem;
}

.Documentation-function h4,
.Documentation-type h4,
.Documentation-typeFunc h4,
.Documentation-typeMethod h4 {
  align-items: baseline;
  display: flex;
  justify-content: space-between;
}
.Documentation-sinceVersion {
  color: var(--color-text-subtle);
  font-size: 0.9375rem;
  font-weight: 400;
}

.Documentation-constants br:last-of-type,
.Documentation-variables br:last-of-type {
  display: none;
}

.Documentation-build {
  color: var(--color-text-subtle);
  padding-top: 1.5rem;
  text-align: right;
}
.Documentation-declaration pre {
  scroll-padding-top: calc(var(--js-sticky-header-height, 3.5rem) + 3.75rem);
}
@media only screen and (min-width: 64rem) {
  .Documentation-declaration pre {
    scroll-padding-top: calc(var(--js-sticky-header-height, 3.5rem) + 0.75rem);
  }
}
.Documentation-declaration + .Documentation-declaration {
  margin-top: 0.625rem;
}
.Documentation-declarationLink {
  background-color: var(--color-background-accented);
  border: var(--border);
  border-bottom: none;
  border-radius: 0.3em 0.3em 0 0;
  display: block;
  font-size: 0.75rem;
  line-height: 0.5rem;
  padding: 0.375rem;
  text-align: right;
}
.Documentation-exampleButtonsContainer {
  align-items: center;
  display: flex;
  justify-content: flex-end;
  margin-top: 0.5rem;
}
.Documentation-examplePlayButton {
  background-color: var(--white);
  border: 0.15rem solid var(--turq-med);
  color: var(--turq-med);
  cursor: pointer;
  flex-shrink: 0;
  height: 2.5rem;
  width: 4.125rem;
}
.Documentation-exampleRunButton,
.Documentation-exampleShareButton,
.Documentation-exampleFormatButton {
  border: 0.0625rem solid var(--turq-dark);
  border-radius: 0.25rem;
  cursor: pointer;
  height: 2rem;
  margin-left: 0.5rem;
  padding: 0 1rem;
}
.Documentation-exampleRunButton {
  background-color: var(--turq-dark);
  color: var(--white);
}
.Documentation-exampleShareButton,
.Documentation-exampleFormatButton {
  background-color: var(--white);
  color: var(--turq-dark);
}
.Documentation-exampleDetails {
  margin-top: 1rem;
}
.Documentation-exampleDetailsBody pre {
  border-radius: 0 0 0.3rem 0.3rem;
  margin-bottom: 1rem;
  margin-top: -0.25rem;
}
.Documentation-exampleDetailsBody textarea {
  height: 100%;
  outline: none;
  overflow-x: auto;
  resize: none;
  white-space: pre;
  width: 100%;
}

/**
 * We add another selector here to these two classes to increase CSS specificity,
 * the selector .Documentation pre + pre overrides .Documentation-exampleCode
 * and .Documentation-exampleOutput by itself and would replace the styles.
 */
.Documentation-exampleDetailsBody .Documentation-exampleCode {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  margin: 0;
}
.Documentation-exampleDetailsBody .Documentation-exampleOutput {
  border-top-left-radius: 0;
  border-top-right-radius: 0;
  margin: 0 0 0.5rem;
}
.Documentation-exampleDetailsHeader {
  color: var(--color-brand-primary);
  cursor: pointer;
  margin-bottom: 2rem;
  outline: none;
  text-decoration: none;
}
.Documentation-exampleOutputLabel {
  color: var(--color-text-subtle);
}
.Documentation-exampleError {
  color: var(--pink);
  margin-right: 0.4rem;
  padding-right: 0.5rem;
}

/* See https://golang.org/issue/43368 for context. */
.Documentation-function pre,
.Documentation-typeFunc pre,
.Documentation-typeMethod pre {
  white-space: pre-wrap;
  word-break: break-all;
  word-wrap: break-word;
}

.Documentation-indexDeprecated {
  margin-left: 0.5rem;
}
.Documentation-deprecatedBody {
  color: var(--color-text-subtle);
  font-size: 0.87rem;
  font-weight: 400;
  margin-left: 0.25rem;
  margin-right: 0.5rem;
}
.Documentation-deprecatedTag {
  background-color: var(--color-border);
  border-radius: 0.125rem;
  color: var(--color-text-inverted);
  font-size: 0.75rem;
  font-weight: normal;
  line-height: 1.375;
  padding: 0.125rem 0.25rem;
  text-transform: uppercase;
  vertical-align: middle;
}
.Documentation-deprecatedTitle {
  align-items: center;
  display: flex;
  gap: 0.5rem;
}
.Documentation-deprecatedDetails {
  color: var(--color-text-subtle);
}
.Documentation-deprecatedDetails a {
  color: var(--color-text-subtle);
}
.Documentation-deprecatedDetails[open] {
  color: var(--color-text);
}
.Documentation-deprecatedDetails[open] a {
  color: var(--color-brand-primary);
}
.Documentation-deprecatedDetails .Documentation-deprecatedBody::after {
  color: var(--color-brand-primary);
  content: 'Show';
}
.Documentation-deprecatedDetails[open] .Documentation-deprecatedBody::after {
  color: var(--color-brand-primary);
  content: 'Hide';
}
.Documentation-deprecatedDetails > summary {
  list-style: none;
  opacity: 1;
}
.Documentation-deprecatedDetails .Documentation-source {
  opacity: 1;
}
.Documentation-deprecatedItemBody {
  padding: 1rem 1rem 0.5rem 1rem;
}
.Documentation-deprecatedMessage {
  align-items: center;
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1rem;
}


text:
package org.apache.lens.server.user;

import java.util.HashMap;
import java.util.Map;

import org.apache.lens.server.api.LensConfConstants;
import org.apache.lens.server.api.user.UserConfigLoader;

import org.apache.hadoop.hive.conf.HiveConf;

import com.google.common.collect.Maps;

/**
 * The Class FixedUserConfigLoader.
 */
public class FixedUserConfigLoader implements UserConfigLoader {

  private final String fixedValue;

  public FixedUserConfigLoader(HiveConf conf) {
    fixedValue = conf.get(LensConfConstants.USER_RESOLVER_FIXED_VALUE);
  }

  /*
     * (non-Javadoc)
     *
     * @see org.apache.lens.server.user.UserConfigLoader#getUserConfig(java.lang.String)
     */
  @Override
  public Map<String, String> getUserConfig(String loggedInUser) {
    HashMap<String, String> userConfig = Maps.newHashMap();
    userConfig.put(LensConfConstants.SESSION_CLUSTER_USER, fixedValue);
    return userConfig;
  }
}


text:
"""Image V2 Action Implementations"""

import argparse
from base64 import b64encode
import logging
import os
import sys

from cinderclient import api_versions
from openstack.image import image_signer
from osc_lib.api import utils as api_utils
from osc_lib.cli import format_columns
from osc_lib.cli import parseractions
from osc_lib.command import command
from osc_lib import exceptions
from osc_lib import utils
from oslo_utils import uuidutils

from openstackclient.common import progressbar
from openstackclient.i18n import _
from openstackclient.identity import common

if os.name == "nt":
    import msvcrt
else:
    msvcrt = None

CONTAINER_CHOICES = ["ami", "ari", "aki", "bare", "docker", "ova", "ovf"]
DEFAULT_CONTAINER_FORMAT = 'bare'
DEFAULT_DISK_FORMAT = 'raw'
DISK_CHOICES = [
    "ami",
    "ari",
    "aki",
    "vhd",
    "vmdk",
    "raw",
    "qcow2",
    "vhdx",
    "vdi",
    "iso",
    "ploop",
]
MEMBER_STATUS_CHOICES = ["accepted", "pending", "rejected", "all"]

LOG = logging.getLogger(__name__)

def _format_image(image, human_readable=False):
    """Format an image to make it more consistent with OSC operations."""

    info = {}
    properties = {}

    # the only fields we're not including is "links", "tags" and the properties
    fields_to_show = [
        'status',
        'name',
        'container_format',
        'created_at',
        'size',
        'disk_format',
        'updated_at',
        'visibility',
        'min_disk',
        'protected',
        'id',
        'file',
        'checksum',
        'owner',
        'virtual_size',
        'min_ram',
        'schema',
    ]

    # TODO(gtema/anybody): actually it should be possible to drop this method,
    # since SDK already delivers a proper object
    image = image.to_dict(ignore_none=True, original_names=True)

    # split out the usual key and the properties which are top-level
    for key in image:
        if key in fields_to_show:
            info[key] = image.get(key)
        elif key == 'tags':
            continue  # handle this later
        elif key == 'properties':
            # NOTE(gtema): flatten content of properties
            properties.update(image.get(key))
        elif key != 'location':
            properties[key] = image.get(key)

    if human_readable:
        info['size'] = utils.format_size(image['size'])

    # format the tags if they are there
    info['tags'] = format_columns.ListColumn(image.get('tags'))

    # add properties back into the dictionary as a top-level key
    if properties:
        info['properties'] = format_columns.DictColumn(properties)

    return info

_formatters = {
    'tags': format_columns.ListColumn,
}

def _get_member_columns(item):
    column_map = {'image_id': 'image_id'}
    hidden_columns = ['id', 'location', 'name']
    return utils.get_osc_show_columns_for_sdk_resource(
        item.to_dict(),
        column_map,
        hidden_columns,
    )

def get_data_from_stdin():
    # distinguish cases where:
    # (1) stdin is not valid (as in cron jobs):
    #    openstack ... <&-
    # (2) image data is provided through stdin:
    #    openstack ... < /tmp/file
    # (3) no image data provided
    #    openstack ...
    try:
        os.fstat(0)
    except OSError:
        # (1) stdin is not valid
        return None

    if not sys.stdin.isatty():
        # (2) image data is provided through stdin
        image = sys.stdin
        if hasattr(sys.stdin, 'buffer'):
            image = sys.stdin.buffer
        if msvcrt:
            msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)

        return image
    else:
        # (3)
        return None

class AddProjectToImage(command.ShowOne):
    _description = _("Associate project with image")

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        parser.add_argument(
            "image",
            metavar="<image>",
            help=_("Image to share (name or ID)"),
        )
        parser.add_argument(
            "project",
            metavar="<project>",
            help=_("Project to associate with image (ID)"),
        )
        common.add_project_domain_option_to_parser(parser)
        return parser

    def take_action(self, parsed_args):
        image_client = self.app.client_manager.image
        identity_client = self.app.client_manager.identity

        if uuidutils.is_uuid_like(parsed_args.project):
            project_id = parsed_args.project
        else:
            project_id = common.find_project(
                identity_client,
                parsed_args.project,
                parsed_args.project_domain,
            ).id

        image = image_client.find_image(
            parsed_args.image, ignore_missing=False
        )

        obj = image_client.add_member(
            image=image.id,
            member_id=project_id,
        )

        display_columns, columns = _get_member_columns(obj)
        data = utils.get_item_properties(obj, columns, formatters={})

        return (display_columns, data)

class CreateImage(command.ShowOne):
    _description = _("Create/upload an image")

    deadopts = ('size', 'location', 'copy-from', 'checksum', 'store')

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        # TODO(bunting): There are additional arguments that v1 supported
        # that v2 either doesn't support or supports weirdly.
        # --checksum - could be faked clientside perhaps?
        # --location - maybe location add?
        # --size - passing image size is actually broken in python-glanceclient
        # --copy-from - does not exist in v2
        # --store - does not exits in v2
        parser.add_argument(
            "name",
            metavar="<image-name>",
            help=_("New image name"),
        )
        parser.add_argument(
            "--id",
            metavar="<id>",
            help=_("Image ID to reserve"),
        )
        parser.add_argument(
            "--container-format",
            default=DEFAULT_CONTAINER_FORMAT,
            choices=CONTAINER_CHOICES,
            metavar="<container-format>",
            help=(
                _(
                    "Image container format. "
                    "The supported options are: %(option_list)s. "
                    "The default format is: %(default_opt)s"
                )
                % {
                    'option_list': ', '.join(CONTAINER_CHOICES),
                    'default_opt': DEFAULT_CONTAINER_FORMAT,
                }
            ),
        )
        parser.add_argument(
            "--disk-format",
            default=DEFAULT_DISK_FORMAT,
            choices=DISK_CHOICES,
            metavar="<disk-format>",
            help=_(
                "Image disk format. The supported options are: %s. "
                "The default format is: raw"
            )
            % ', '.join(DISK_CHOICES),
        )
        parser.add_argument(
            "--min-disk",
            metavar="<disk-gb>",
            type=int,
            help=_("Minimum disk size needed to boot image, in gigabytes"),
        )
        parser.add_argument(
            "--min-ram",
            metavar="<ram-mb>",
            type=int,
            help=_("Minimum RAM size needed to boot image, in megabytes"),
        )
        source_group = parser.add_mutually_exclusive_group()
        source_group.add_argument(
            "--file",
            dest="filename",
            metavar="<file>",
            help=_("Upload image from local file"),
        )
        source_group.add_argument(
            "--volume",
            metavar="<volume>",
            help=_("Create image from a volume"),
        )
        parser.add_argument(
            "--force",
            dest='force',
            action='store_true',
            default=False,
            help=_(
                "Force image creation if volume is in use "
                "(only meaningful with --volume)"
            ),
        )
        parser.add_argument(
            "--progress",
            action="store_true",
            default=False,
            help=_(
                "Show upload progress bar "
                "(ignored if passing data via stdin)"
            ),
        )
        parser.add_argument(
            '--sign-key-path',
            metavar="<sign-key-path>",
            default=[],
            help=_(
                "Sign the image using the specified private key. "
                "Only use in combination with --sign-cert-id"
            ),
        )
        parser.add_argument(
            '--sign-cert-id',
            metavar="<sign-cert-id>",
            default=[],
            help=_(
                "The specified certificate UUID is a reference to "
                "the certificate in the key manager that corresponds "
                "to the public key and is used for signature validation. "
                "Only use in combination with --sign-key-path"
            ),
        )
        protected_group = parser.add_mutually_exclusive_group()
        protected_group.add_argument(
            "--protected",
            action="store_true",
            dest="is_protected",
            default=None,
            help=_("Prevent image from being deleted"),
        )
        protected_group.add_argument(
            "--unprotected",
            action="store_false",
            dest="is_protected",
            default=None,
            help=_("Allow image to be deleted (default)"),
        )
        public_group = parser.add_mutually_exclusive_group()
        public_group.add_argument(
            "--public",
            action="store_const",
            const="public",
            dest="visibility",
            help=_("Image is accessible to the public"),
        )
        public_group.add_argument(
            "--private",
            action="store_const",
            const="private",
            dest="visibility",
            help=_("Image is inaccessible to the public (default)"),
        )
        public_group.add_argument(
            "--community",
            action="store_const",
            const="community",
            dest="visibility",
            help=_("Image is accessible to the community"),
        )
        public_group.add_argument(
            "--shared",
            action="store_const",
            const="shared",
            dest="visibility",
            help=_("Image can be shared"),
        )
        parser.add_argument(
            "--property",
            dest="properties",
            metavar="<key=value>",
            action=parseractions.KeyValueAction,
            help=_(
                "Set a property on this image "
                "(repeat option to set multiple properties)"
            ),
        )
        parser.add_argument(
            "--tag",
            dest="tags",
            metavar="<tag>",
            action='append',
            help=_(
                "Set a tag on this image "
                "(repeat option to set multiple tags)"
            ),
        )
        parser.add_argument(
            "--project",
            metavar="<project>",
            help=_("Set an alternate project on this image (name or ID)"),
        )
        parser.add_argument(
            "--import",
            dest="use_import",
            action="store_true",
            help=_(
                "Force the use of glance image import instead of direct upload"
            ),
        )
        common.add_project_domain_option_to_parser(parser)
        for deadopt in self.deadopts:
            parser.add_argument(
                "--%s" % deadopt,
                metavar="<%s>" % deadopt,
                dest=deadopt.replace('-', '_'),
                help=argparse.SUPPRESS,
            )
        return parser

    def _take_action_image(self, parsed_args):
        identity_client = self.app.client_manager.identity
        image_client = self.app.client_manager.image

        # Build an attribute dict from the parsed args, only include
        # attributes that were actually set on the command line
        kwargs = {'allow_duplicates': True}
        copy_attrs = (
            'name',
            'id',
            'container_format',
            'disk_format',
            'min_disk',
            'min_ram',
            'tags',
            'visibility',
        )
        for attr in copy_attrs:
            if attr in parsed_args:
                val = getattr(parsed_args, attr, None)
                if val:
                    # Only include a value in kwargs for attributes that
                    # are actually present on the command line
                    kwargs[attr] = val

        # properties should get flattened into the general kwargs
        if getattr(parsed_args, 'properties', None):
            for k, v in parsed_args.properties.items():
                kwargs[k] = str(v)

        # Handle exclusive booleans with care
        # Avoid including attributes in kwargs if an option is not
        # present on the command line.  These exclusive booleans are not
        # a single value for the pair of options because the default must be
        # to do nothing when no options are present as opposed to always
        # setting a default.
        if parsed_args.is_protected is not None:
            kwargs['is_protected'] = parsed_args.is_protected

        if parsed_args.visibility is not None:
            kwargs['visibility'] = parsed_args.visibility

        if parsed_args.project:
            kwargs['owner_id'] = common.find_project(
                identity_client,
                parsed_args.project,
                parsed_args.project_domain,
            ).id

        if parsed_args.use_import:
            kwargs['use_import'] = True

        # open the file first to ensure any failures are handled before the
        # image is created. Get the file name (if it is file, and not stdin)
        # for easier further handling.
        if parsed_args.filename:
            try:
                fp = open(parsed_args.filename, 'rb')
            except FileNotFoundError:
                raise exceptions.CommandError(
                    '%r is not a valid file' % parsed_args.filename,
                )
        else:
            fp = get_data_from_stdin()

        if fp is not None and parsed_args.volume:
            msg = _(
                "Uploading data and using container are not allowed at "
                "the same time"
            )
            raise exceptions.CommandError(msg)

        if parsed_args.progress and parsed_args.filename:
            # NOTE(stephenfin): we only show a progress bar if the user
            # requested it *and* we're reading from a file (not stdin)
            filesize = os.path.getsize(parsed_args.filename)
            if filesize is not None:
                kwargs['validate_checksum'] = False
                kwargs['data'] = progressbar.VerboseFileWrapper(fp, filesize)
            else:
                kwargs['data'] = fp
        elif parsed_args.filename:
            kwargs['filename'] = parsed_args.filename
        elif fp:
            kwargs['validate_checksum'] = False
            kwargs['data'] = fp

        # sign an image using a given local private key file
        if parsed_args.sign_key_path or parsed_args.sign_cert_id:
            if not parsed_args.filename:
                msg = _(
                    "signing an image requires the --file option, "
                    "passing files via stdin when signing is not "
                    "supported."
                )
                raise exceptions.CommandError(msg)

            if (
                len(parsed_args.sign_key_path) < 1 or
                len(parsed_args.sign_cert_id) < 1
            ):
                msg = _(
                    "'sign-key-path' and 'sign-cert-id' must both be "
                    "specified when attempting to sign an image."
                )
                raise exceptions.CommandError(msg)

            sign_key_path = parsed_args.sign_key_path
            sign_cert_id = parsed_args.sign_cert_id
            signer = image_signer.ImageSigner()
            try:
                pw = utils.get_password(
                    self.app.stdin,
                    prompt=(
                        "Please enter private key password, leave "
                        "empty if none: "
                    ),
                    confirm=False,
                )

                if not pw or len(pw) < 1:
                    pw = None
                else:
                    # load_private_key() requires the password to be
                    # passed as bytes
                    pw = pw.encode()

                signer.load_private_key(sign_key_path, password=pw)
            except Exception:
                msg = _(
                    "Error during sign operation: private key "
                    "could not be loaded."
                )
                raise exceptions.CommandError(msg)

            signature = signer.generate_signature(fp)
            signature_b64 = b64encode(signature)
            kwargs['img_signature'] = signature_b64
            kwargs['img_signature_certificate_uuid'] = sign_cert_id
            kwargs['img_signature_hash_method'] = signer.hash_method
            if signer.padding_method:
                kwargs['img_signature_key_type'] = signer.padding_method

        image = image_client.create_image(**kwargs)

        if parsed_args.filename:
            fp.close()

        return _format_image(image)

    def _take_action_volume(self, parsed_args):
        volume_client = self.app.client_manager.volume

        unsupported_opts = {
            # 'name',  # 'name' is a positional argument and will always exist
            'id',
            'min_disk',
            'min_ram',
            'file',
            'force',
            'progress',
            'sign_key_path',
            'sign_cert_id',
            'properties',
            'tags',
            'project',
            'use_import',
        }
        for unsupported_opt in unsupported_opts:
            if getattr(parsed_args, unsupported_opt, None):
                opt_name = unsupported_opt.replace('-', '_')
                if unsupported_opt == 'use_import':
                    opt_name = 'import'
                msg = _(
                    "'--%s' was given, which is not supported when "
                    "creating an image from a volume. "
                    "This will be an error in a future version."
                )
                # TODO(stephenfin): These should be an error in a future
                # version
                LOG.warning(msg % opt_name)

        source_volume = utils.find_resource(
            volume_client.volumes,
            parsed_args.volume,
        )
        kwargs = {}
        if volume_client.api_version < api_versions.APIVersion('3.1'):
            if (
                parsed_args.visibility or
                parsed_args.is_protected is not None
            ):
                msg = _(
                    '--os-volume-api-version 3.1 or greater is required '
                    'to support the --public, --private, --community, '
                    '--shared or --protected option.'
                )
                raise exceptions.CommandError(msg)
        else:
            kwargs.update(
                visibility=parsed_args.visibility or 'private',
                protected=parsed_args.is_protected or False,
            )

        response, body = volume_client.volumes.upload_to_image(
            source_volume.id,
            parsed_args.force,
            parsed_args.name,
            parsed_args.container_format,
            parsed_args.disk_format,
            **kwargs
        )
        info = body['os-volume_upload_image']
        try:
            info['volume_type'] = info['volume_type']['name']
        except TypeError:
            info['volume_type'] = None

        return info

    def take_action(self, parsed_args):
        for deadopt in self.deadopts:
            if getattr(parsed_args, deadopt.replace('-', '_'), None):
                msg = _(
                    "ERROR: --%s was given, which is an Image v1 option "
                    "that is no longer supported in Image v2"
                )
                raise exceptions.CommandError(msg % deadopt)

        if parsed_args.volume:
            info = self._take_action_volume(parsed_args)
        else:
            info = self._take_action_image(parsed_args)

        return zip(*sorted(info.items()))

class DeleteImage(command.Command):
    _description = _("Delete image(s)")

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        parser.add_argument(
            "images",
            metavar="<image>",
            nargs="+",
            help=_("Image(s) to delete (name or ID)"),
        )
        return parser

    def take_action(self, parsed_args):

        del_result = 0
        image_client = self.app.client_manager.image
        for image in parsed_args.images:
            try:
                image_obj = image_client.find_image(
                    image, ignore_missing=False
                )
                image_client.delete_image(image_obj.id)
            except Exception as e:
                del_result += 1
                msg = _(
                    "Failed to delete image with name or "
                    "ID '%(image)s': %(e)s"
                )
                LOG.error(msg, {'image': image, 'e': e})

        total = len(parsed_args.images)
        if del_result > 0:
            msg = _("Failed to delete %(dresult)s of %(total)s images.") % {
                'dresult': del_result,
                'total': total,
            }
            raise exceptions.CommandError(msg)

class ListImage(command.Lister):
    _description = _("List available images")

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        public_group = parser.add_mutually_exclusive_group()
        public_group.add_argument(
            "--public",
            action="store_const",
            const="public",
            dest="visibility",
            help=_("List only public images"),
        )
        public_group.add_argument(
            "--private",
            action="store_const",
            const="private",
            dest="visibility",
            help=_("List only private images"),
        )
        public_group.add_argument(
            "--community",
            action="store_const",
            const="community",
            dest="visibility",
            help=_("List only community images"),
        )
        public_group.add_argument(
            "--shared",
            action="store_const",
            const="shared",
            dest="visibility",
            help=_("List only shared images"),
        )
        public_group.add_argument(
            "--all",
            action="store_const",
            const="all",
            dest="visibility",
            help=_("List all images"),
        )
        parser.add_argument(
            '--property',
            metavar='<key=value>',
            action=parseractions.KeyValueAction,
            help=_(
                'Filter output based on property '
                '(repeat option to filter on multiple properties)'
            ),
        )
        parser.add_argument(
            '--name',
            metavar='<name>',
            default=None,
            help=_("Filter images based on name."),
        )
        parser.add_argument(
            '--status',
            metavar='<status>',
            default=None,
            help=_("Filter images based on status."),
        )
        parser.add_argument(
            '--member-status',
            metavar='<member-status>',
            default=None,
            type=lambda s: s.lower(),
            choices=MEMBER_STATUS_CHOICES,
            help=(
                _(
                    "Filter images based on member status. "
                    "The supported options are: %s. "
                )
                % ', '.join(MEMBER_STATUS_CHOICES)
            ),
        )
        parser.add_argument(
            '--project',
            metavar='<project>',
            help=_("Search by project (admin only) (name or ID)"),
        )
        common.add_project_domain_option_to_parser(parser)
        parser.add_argument(
            '--tag',
            metavar='<tag>',
            action='append',
            default=[],
            help=_(
                'Filter images based on tag. '
                '(repeat option to filter on multiple tags)'
            ),
        )
        parser.add_argument(
            '--hidden',
            action='store_true',
            dest='is_hidden',
            default=False,
            help=_('List hidden images'),
        )
        parser.add_argument(
            '--long',
            action='store_true',
            default=False,
            help=_('List additional fields in output'),
        )

        # --page-size has never worked, leave here for silent compatibility
        # We'll implement limit/marker differently later
        parser.add_argument(
            "--page-size",
            metavar="<size>",
            help=argparse.SUPPRESS,
        )
        parser.add_argument(
            '--sort',
            metavar="<key>[:<direction>]",
            default='name:asc',
            help=_(
                "Sort output by selected keys and directions (asc or desc) "
                "(default: name:asc), multiple keys and directions can be "
                "specified separated by comma"
            ),
        )
        parser.add_argument(
            "--limit",
            metavar="<num-images>",
            type=int,
            help=_("Maximum number of images to display."),
        )
        parser.add_argument(
            '--marker',
            metavar='<image>',
            default=None,
            help=_(
                "The last image of the previous page. Display "
                "list of images after marker. Display all images if not "
                "specified. (name or ID)"
            ),
        )
        return parser

    def take_action(self, parsed_args):
        identity_client = self.app.client_manager.identity
        image_client = self.app.client_manager.image

        kwargs = {}
        if parsed_args.visibility is not None:
            kwargs['visibility'] = parsed_args.visibility
        if parsed_args.limit:
            kwargs['limit'] = parsed_args.limit
        if parsed_args.marker:
            kwargs['marker'] = image_client.find_image(parsed_args.marker).id
        if parsed_args.name:
            kwargs['name'] = parsed_args.name
        if parsed_args.status:
            kwargs['status'] = parsed_args.status
        if parsed_args.member_status:
            kwargs['member_status'] = parsed_args.member_status
        if parsed_args.tag:
            kwargs['tag'] = parsed_args.tag
        project_id = None
        if parsed_args.project:
            project_id = common.find_project(
                identity_client,
                parsed_args.project,
                parsed_args.project_domain,
            ).id
            kwargs['owner'] = project_id
        if parsed_args.is_hidden:
            kwargs['is_hidden'] = parsed_args.is_hidden
        if parsed_args.long:
            columns = (
                'ID',
                'Name',
                'Disk Format',
                'Container Format',
                'Size',
                'Checksum',
                'Status',
                'visibility',
                'is_protected',
                'owner_id',
                'tags',
            )
            column_headers = (
                'ID',
                'Name',
                'Disk Format',
                'Container Format',
                'Size',
                'Checksum',
                'Status',
                'Visibility',
                'Protected',
                'Project',
                'Tags',
            )
        else:
            columns = ("ID", "Name", "Status")
            column_headers = columns

        # List of image data received
        if 'limit' in kwargs:
            # Disable automatic pagination in SDK
            kwargs['paginated'] = False
        data = list(image_client.images(**kwargs))

        if parsed_args.property:
            for attr, value in parsed_args.property.items():
                api_utils.simple_filter(
                    data,
                    attr=attr,
                    value=value,
                    property_field='properties',
                )

        data = utils.sort_items(data, parsed_args.sort, str)

        return (
            column_headers,
            (
                utils.get_item_properties(
                    s,
                    columns,
                    formatters=_formatters,
                )
                for s in data
            ),
        )

class ListImageProjects(command.Lister):
    _description = _("List projects associated with image")

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        parser.add_argument(
            "image",
            metavar="<image>",
            help=_("Image (name or ID)"),
        )
        common.add_project_domain_option_to_parser(parser)
        return parser

    def take_action(self, parsed_args):
        image_client = self.app.client_manager.image
        columns = ("Image ID", "Member ID", "Status")

        image_id = image_client.find_image(parsed_args.image).id

        data = image_client.members(image=image_id)

        return (
            columns,
            (
                utils.get_item_properties(
                    s,
                    columns,
                )
                for s in data
            ),
        )

class RemoveProjectImage(command.Command):
    _description = _("Disassociate project with image")

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        parser.add_argument(
            "image",
            metavar="<image>",
            help=_("Image to unshare (name or ID)"),
        )
        parser.add_argument(
            "project",
            metavar="<project>",
            help=_("Project to disassociate with image (name or ID)"),
        )
        common.add_project_domain_option_to_parser(parser)
        return parser

    def take_action(self, parsed_args):
        image_client = self.app.client_manager.image
        identity_client = self.app.client_manager.identity

        project_id = common.find_project(
            identity_client, parsed_args.project, parsed_args.project_domain
        ).id

        image = image_client.find_image(
            parsed_args.image, ignore_missing=False
        )

        image_client.remove_member(member=project_id, image=image.id)

class SaveImage(command.Command):
    _description = _("Save an image locally")

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        parser.add_argument(
            "--file",
            metavar="<filename>",
            dest="filename",
            help=_("Downloaded image save filename (default: stdout)"),
        )
        parser.add_argument(
            "image",
            metavar="<image>",
            help=_("Image to save (name or ID)"),
        )
        return parser

    def take_action(self, parsed_args):
        image_client = self.app.client_manager.image
        image = image_client.find_image(parsed_args.image)

        output_file = parsed_args.filename
        if output_file is None:
            output_file = getattr(sys.stdout, "buffer", sys.stdout)

        image_client.download_image(image.id, stream=True, output=output_file)

class SetImage(command.Command):
    _description = _("Set image properties")

    deadopts = ('visibility',)

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        # TODO(bunting): There are additional arguments that v1 supported
        # --size - does not exist in v2
        # --store - does not exist in v2
        # --location - maybe location add?
        # --copy-from - does not exist in v2
        # --file - should be able to upload file
        # --volume - not possible with v2 as can't change id
        # --force - see `--volume`
        # --checksum - maybe could be done client side
        # --stdin - could be implemented
        parser.add_argument(
            "image", metavar="<image>", help=_("Image to modify (name or ID)")
        )
        parser.add_argument(
            "--name", metavar="<name>", help=_("New image name")
        )
        parser.add_argument(
            "--min-disk",
            type=int,
            metavar="<disk-gb>",
            help=_("Minimum disk size needed to boot image, in gigabytes"),
        )
        parser.add_argument(
            "--min-ram",
            type=int,
            metavar="<ram-mb>",
            help=_("Minimum RAM size needed to boot image, in megabytes"),
        )
        parser.add_argument(
            "--container-format",
            metavar="<container-format>",
            choices=CONTAINER_CHOICES,
            help=_("Image container format. The supported options are: %s")
            % ', '.join(CONTAINER_CHOICES),
        )
        parser.add_argument(
            "--disk-format",
            metavar="<disk-format>",
            choices=DISK_CHOICES,
            help=_("Image disk format. The supported options are: %s")
            % ', '.join(DISK_CHOICES),
        )
        protected_group = parser.add_mutually_exclusive_group()
        protected_group.add_argument(
            "--protected",
            action="store_true",
            dest="is_protected",
            default=None,
            help=_("Prevent image from being deleted"),
        )
        protected_group.add_argument(
            "--unprotected",
            action="store_false",
            dest="is_protected",
            default=None,
            help=_("Allow image to be deleted (default)"),
        )
        public_group = parser.add_mutually_exclusive_group()
        public_group.add_argument(
            "--public",
            action="store_const",
            const="public",
            dest="visibility",
            help=_("Image is accessible to the public"),
        )
        public_group.add_argument(
            "--private",
            action="store_const",
            const="private",
            dest="visibility",
            help=_("Image is inaccessible to the public (default)"),
        )
        public_group.add_argument(
            "--community",
            action="store_const",
            const="community",
            dest="visibility",
            help=_("Image is accessible to the community"),
        )
        public_group.add_argument(
            "--shared",
            action="store_const",
            const="shared",
            dest="visibility",
            help=_("Image can be shared"),
        )
        parser.add_argument(
            "--property",
            dest="properties",
            metavar="<key=value>",
            action=parseractions.KeyValueAction,
            help=_(
                "Set a property on this image "
                "(repeat option to set multiple properties)"
            ),
        )
        parser.add_argument(
            "--tag",
            dest="tags",
            metavar="<tag>",
            default=None,
            action='append',
            help=_(
                "Set a tag on this image "
                "(repeat option to set multiple tags)"
            ),
        )
        parser.add_argument(
            "--architecture",
            metavar="<architecture>",
            help=_("Operating system architecture"),
        )
        parser.add_argument(
            "--instance-id",
            metavar="<instance-id>",
            help=_("ID of server instance used to create this image"),
        )
        parser.add_argument(
            "--instance-uuid",
            metavar="<instance-id>",
            dest="instance_id",
            help=argparse.SUPPRESS,
        )
        parser.add_argument(
            "--kernel-id",
            metavar="<kernel-id>",
            help=_("ID of kernel image used to boot this disk image"),
        )
        parser.add_argument(
            "--os-distro",
            metavar="<os-distro>",
            help=_("Operating system distribution name"),
        )
        parser.add_argument(
            "--os-version",
            metavar="<os-version>",
            help=_("Operating system distribution version"),
        )
        parser.add_argument(
            "--ramdisk-id",
            metavar="<ramdisk-id>",
            help=_("ID of ramdisk image used to boot this disk image"),
        )
        deactivate_group = parser.add_mutually_exclusive_group()
        deactivate_group.add_argument(
            "--deactivate",
            action="store_true",
            help=_("Deactivate the image"),
        )
        deactivate_group.add_argument(
            "--activate",
            action="store_true",
            help=_("Activate the image"),
        )
        parser.add_argument(
            "--project",
            metavar="<project>",
            help=_("Set an alternate project on this image (name or ID)"),
        )
        common.add_project_domain_option_to_parser(parser)
        for deadopt in self.deadopts:
            parser.add_argument(
                "--%s" % deadopt,
                metavar="<%s>" % deadopt,
                dest=f"dead_{deadopt.replace('-', '_')}",
                help=argparse.SUPPRESS,
            )

        membership_group = parser.add_mutually_exclusive_group()
        membership_group.add_argument(
            "--accept",
            action="store_const",
            const="accepted",
            dest="membership",
            default=None,
            help=_("Accept the image membership"),
        )
        membership_group.add_argument(
            "--reject",
            action="store_const",
            const="rejected",
            dest="membership",
            default=None,
            help=_("Reject the image membership"),
        )
        membership_group.add_argument(
            "--pending",
            action="store_const",
            const="pending",
            dest="membership",
            default=None,
            help=_("Reset the image membership to 'pending'"),
        )

        hidden_group = parser.add_mutually_exclusive_group()
        hidden_group.add_argument(
            "--hidden",
            dest="is_hidden",
            default=None,
            action="store_true",
            help=_("Hide the image"),
        )
        hidden_group.add_argument(
            "--unhidden",
            dest="is_hidden",
            default=None,
            action="store_false",
            help=_("Unhide the image"),
        )
        return parser

    def take_action(self, parsed_args):
        identity_client = self.app.client_manager.identity
        image_client = self.app.client_manager.image

        for deadopt in self.deadopts:
            if getattr(parsed_args, f"dead_{deadopt.replace('-', '_')}", None):
                raise exceptions.CommandError(
                    _(
                        "ERROR: --%s was given, which is an Image v1 option"
                        " that is no longer supported in Image v2"
                    )
                    % deadopt
                )

        image = image_client.find_image(
            parsed_args.image,
            ignore_missing=False,
        )
        project_id = None
        if parsed_args.project:
            project_id = common.find_project(
                identity_client,
                parsed_args.project,
                parsed_args.project_domain,
            ).id

        # handle activation status changes

        activation_status = None
        if parsed_args.deactivate or parsed_args.activate:
            if parsed_args.deactivate:
                image_client.deactivate_image(image.id)
                activation_status = "deactivated"
            if parsed_args.activate:
                image_client.reactivate_image(image.id)
                activation_status = "activated"

        # handle membership changes

        if parsed_args.membership:
            # If a specific project is not passed, assume we want to update
            # our own membership
            if not project_id:
                project_id = self.app.client_manager.auth_ref.project_id
            image_client.update_member(
                image=image.id,
                member=project_id,
                status=parsed_args.membership,
            )

        # handle everything else

        kwargs = {}
        copy_attrs = (
            'architecture',
            'container_format',
            'disk_format',
            'file',
            'instance_id',
            'kernel_id',
            'locations',
            'min_disk',
            'min_ram',
            'name',
            'os_distro',
            'os_version',
            'prefix',
            'progress',
            'ramdisk_id',
            'tags',
            'visibility',
        )
        for attr in copy_attrs:
            if attr in parsed_args:
                val = getattr(parsed_args, attr, None)
                if val is not None:
                    # Only include a value in kwargs for attributes that are
                    # actually present on the command line
                    kwargs[attr] = val

        # Properties should get flattened into the general kwargs
        if getattr(parsed_args, 'properties', None):
            for k, v in parsed_args.properties.items():
                kwargs[k] = str(v)

        # Handle exclusive booleans with care
        # Avoid including attributes in kwargs if an option is not
        # present on the command line.  These exclusive booleans are not
        # a single value for the pair of options because the default must be
        # to do nothing when no options are present as opposed to always
        # setting a default.
        if parsed_args.is_protected is not None:
            kwargs['is_protected'] = parsed_args.is_protected

        if parsed_args.visibility is not None:
            kwargs['visibility'] = parsed_args.visibility

        if parsed_args.project:
            # We already did the project lookup above
            kwargs['owner_id'] = project_id

        if parsed_args.tags:
            # Tags should be extended, but duplicates removed
            kwargs['tags'] = list(set(image.tags).union(set(parsed_args.tags)))

        if parsed_args.is_hidden is not None:
            kwargs['is_hidden'] = parsed_args.is_hidden

        try:
            image = image_client.update_image(image.id, **kwargs)
        except Exception:
            if activation_status is not None:
                LOG.info(
                    _("Image %(id)s was %(status)s."),
                    {'id': image.id, 'status': activation_status},
                )
            raise

class ShowImage(command.ShowOne):
    _description = _("Display image details")

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        parser.add_argument(
            "--human-readable",
            default=False,
            action='store_true',
            help=_("Print image size in a human-friendly format."),
        )
        parser.add_argument(
            "image",
            metavar="<image>",
            help=_("Image to display (name or ID)"),
        )
        return parser

    def take_action(self, parsed_args):
        image_client = self.app.client_manager.image

        image = image_client.find_image(
            parsed_args.image, ignore_missing=False
        )

        info = _format_image(image, parsed_args.human_readable)
        return zip(*sorted(info.items()))

class UnsetImage(command.Command):
    _description = _("Unset image tags and properties")

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        parser.add_argument(
            "image",
            metavar="<image>",
            help=_("Image to modify (name or ID)"),
        )
        parser.add_argument(
            "--tag",
            dest="tags",
            metavar="<tag>",
            default=[],
            action='append',
            help=_(
                "Unset a tag on this image "
                "(repeat option to unset multiple tags)"
            ),
        )
        parser.add_argument(
            "--property",
            dest="properties",
            metavar="<property-key>",
            default=[],
            action='append',
            help=_(
                "Unset a property on this image "
                "(repeat option to unset multiple properties)"
            ),
        )
        return parser

    def take_action(self, parsed_args):
        image_client = self.app.client_manager.image
        image = image_client.find_image(
            parsed_args.image, ignore_missing=False
        )

        kwargs = {}
        tagret = 0
        propret = 0
        if parsed_args.tags:
            for k in parsed_args.tags:
                try:
                    image_client.remove_tag(image.id, k)
                except Exception:
                    LOG.error(
                        _("tag unset failed, '%s' is a " "nonexistent tag "), k
                    )
                    tagret += 1

        if parsed_args.properties:
            for k in parsed_args.properties:
                if k in image:
                    delattr(image, k)
                elif k in image.properties:
                    # Since image is an "evil" object from SDK POV we need to
                    # pass modified properties object, so that SDK can figure
                    # out, what was changed inside
                    # NOTE: ping gtema to improve that in SDK
                    new_props = kwargs.get(
                        'properties', image.get('properties').copy()
                    )
                    new_props.pop(k, None)
                    kwargs['properties'] = new_props
                else:
                    LOG.error(
                        _(
                            "property unset failed, '%s' is a "
                            "nonexistent property "
                        ),
                        k,
                    )
                    propret += 1

            # We must give to update a current image for the reference on what
            # has changed
            image_client.update_image(image, **kwargs)

        tagtotal = len(parsed_args.tags)
        proptotal = len(parsed_args.properties)
        if tagret > 0 and propret > 0:
            msg = _(
                "Failed to unset %(tagret)s of %(tagtotal)s tags,"
                "Failed to unset %(propret)s of %(proptotal)s properties."
            ) % {
                'tagret': tagret,
                'tagtotal': tagtotal,
                'propret': propret,
                'proptotal': proptotal,
            }
            raise exceptions.CommandError(msg)
        elif tagret > 0:
            msg = _("Failed to unset %(tagret)s of %(tagtotal)s tags.") % {
                'tagret': tagret,
                'tagtotal': tagtotal,
            }
            raise exceptions.CommandError(msg)
        elif propret > 0:
            msg = _(
                "Failed to unset %(propret)s of %(proptotal)s" " properties."
            ) % {'propret': propret, 'proptotal': proptotal}
            raise exceptions.CommandError(msg)

class StageImage(command.Command):
    _description = _(
        "Upload data for a specific image to staging.\n"
        "This requires support for the interoperable image import process, "
        "which was first introduced in Image API version 2.6 "
        "(Glance 16.0.0 (Queens))"
    )

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)

        parser.add_argument(
            '--file',
            metavar='<file>',
            dest='filename',
            help=_(
                'Local file that contains disk image to be uploaded. '
                'Alternatively, images can be passed via stdin.'
            ),
        )
        # NOTE(stephenfin): glanceclient had a --size argument but it didn't do
        # anything so we have chosen not to port this
        parser.add_argument(
            '--progress',
            action='store_true',
            default=False,
            help=_(
                'Show upload progress bar '
                '(ignored if passing data via stdin)'
            ),
        )
        parser.add_argument(
            'image',
            metavar='<image>',
            help=_('Image to upload data for (name or ID)'),
        )

        return parser

    def take_action(self, parsed_args):
        image_client = self.app.client_manager.image

        image = image_client.find_image(
            parsed_args.image,
            ignore_missing=False,
        )
        # open the file first to ensure any failures are handled before the
        # image is created. Get the file name (if it is file, and not stdin)
        # for easier further handling.
        if parsed_args.filename:
            try:
                fp = open(parsed_args.filename, 'rb')
            except FileNotFoundError:
                raise exceptions.CommandError(
                    '%r is not a valid file' % parsed_args.filename,
                )
        else:
            fp = get_data_from_stdin()

        kwargs = {}

        if parsed_args.progress and parsed_args.filename:
            # NOTE(stephenfin): we only show a progress bar if the user
            # requested it *and* we're reading from a file (not stdin)
            filesize = os.path.getsize(parsed_args.filename)
            if filesize is not None:
                kwargs['data'] = progressbar.VerboseFileWrapper(fp, filesize)
            else:
                kwargs['data'] = fp
        elif parsed_args.filename:
            kwargs['filename'] = parsed_args.filename
        elif fp:
            kwargs['data'] = fp

        image_client.stage_image(image, **kwargs)


text:
from django.views.generic import View, TemplateView

class IndexView(TemplateView):
    template_name = 'index.html'


text:
var roleBuilder = require('role.builder');

module.exports = {
    // a function to run the logic for this role
    run: function(creep) {
        // if creep is trying to repair something but has no energy left
        if (creep.memory.working == true && creep.carry.energy == 0) {
            // switch state
            creep.memory.working = false;
        }
        // if creep is harvesting energy but is full
        else if (creep.memory.working == false && creep.carry.energy == creep.carryCapacity) {
            // switch state
            creep.memory.working = true;
        }

        // if creep is supposed to repair something
        if (creep.memory.working == true) {
            // find closest structure with less than max hits
            // Exclude walls because they have way too many max hits and would keep
            // our repairers busy forever. We have to find a solution for that later.
            var structure = creep.pos.findClosestByPath(FIND_STRUCTURES, {
                // the second argument for findClosestByPath is an object which takes
                // a property called filter which can be a function
                // we use the arrow operator to define it
                filter: (s) => s.hits < s.hitsMax && s.structureType != STRUCTURE_WALL
            });

            // if we find one
            if (structure != undefined) {
                // try to repair it, if it is out of range
                if (creep.repair(structure) == ERR_NOT_IN_RANGE) {
                    // move towards it
                    creep.moveTo(structure);
                }
            }
            // if we can't fine one
            else {
                // look for construction sites
                roleBuilder.run(creep);
            }
        }
        // if creep is supposed to harvest energy from source
        else {
            // find closest source
            var source = creep.pos.findClosestByPath(FIND_SOURCES);
            // try to harvest energy, if the source is not in range
            if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
                // move towards the source
                creep.moveTo(source);
            }
        }
    }
};


text:
ACCEPTED

#### According to
The Catalogue of Life, 3rd January 2011

#### Published in
Ill. Handb. Laubholzk. 1:201. 1904

#### Original name
null

### Remarks
null


text:
require 'active_job/base'

module ActiveJob
  module Unique
    module Compatible
      extend ActiveSupport::Concern

      included do
        # compatible with rails 4.x
        attr_writer   :priority
        attr_accessor :provider_job_id
        attr_accessor :executions
      end

      module ClassMethods
        def deserialize(job_data)
          begin
            job = super
          rescue LoadError
            sleep(1)

            job = super
          end

          job_uniqueness_mode = job_data['uniqueness_mode'] || uniqueness_mode

          if uniqueness_api.valid_uniqueness_mode?(job_uniqueness_mode)
            job.uniqueness_mode       = job_uniqueness_mode
            job.uniqueness_id         = job_data['uniqueness_id']
            job.uniqueness_skipped    = job_data['uniqueness_skipped']
            job.uniqueness_expires    = job_data['uniqueness_expires']
            job.uniqueness_expiration = job_data['uniqueness_expiration']
            job.uniqueness_debug      = job_data['uniqueness_debug'] || uniqueness_debug
          end

          job
        end
      end

      def serialize
        data = super

        if uniqueness_api.valid_uniqueness_mode?(uniqueness_mode.to_s.to_sym)
          data['uniqueness_id']         = uniqueness_id
          data['uniqueness_skipped']    = uniqueness_skipped
          data['uniqueness_expires']    = uniqueness_expires
          data['uniqueness_expiration'] = uniqueness_expiration
          data['uniqueness_mode']       = uniqueness_mode
          data['uniqueness_debug']      = uniqueness_debug
        end

        data
      end
    end
  end
end


text:
(function($, document)
{

    // variables
    var cacheKey = 'data-jckwt-galleries';

	/* on doc ready */
	$(document).ready(function()
	{
		setupVariationImageManagement();
		setupBulkSaveBtns();
		hide_plans();
	});

	// local cache

	var localCache = {

        /**
        * timeout for cache in millis
        * @type {number}
        */
        timeout: 1800000, // 30 minutes
        /**
        * @type {{_: number, data: {}}}
        **/
        data: {},
        remove: function (key) {
            delete localCache.data[key];
        },
        exist: function (key) {
            return !!localCache.data[key] && ((new Date().getTime() - localCache.data[key]._) < localCache.timeout);
        },
        get: function (key) {
            return localCache.data[key].data;
        },
        set: function ( key, cachedData, callback) {
            localCache.remove(key);
            localCache.data[key] = {
                _: new Date().getTime(),
                data: cachedData
            };
            if ($.isFunction(callback)) callback(cachedData);
        }

    };

   	// ! Bulk Save Buttons

   	function setupBulkSaveBtns()
   	{
	   	$('.saveVariationImages').on('click', function(){

	   		var btn = $(this),
	   			$messageContainer = btn.next('.updateMsg'),
	   			updateText = btn.attr('data-update'),
	   			updatedText = btn.attr('data-updated'),
	   			updatingText = btn.attr('data-updating'),
	   			errorText = btn.attr('data-error'),
	   			varid = btn.attr('data-varid'),
	   			images = $(btn.attr('data-input')).val(),
	   			ajaxargs = {
					action: iconic_woothumbs_vars.slug+'_bulk_save',
					nonce: iconic_woothumbs_vars.nonce,
					varid: varid,
					images: images
				}

			btn.val(updatingText);

			$.post( iconic_woothumbs_vars.ajaxurl, ajaxargs, function( data )
			{
				if(data.result == 'success')
				{
					btn.val(updatedText);
				}
				else
				{
					btn.val(errorText);
				}

				$messageContainer.text(data.message);

				setTimeout(function(){
					btn.val(updateText);
					$messageContainer.text('');
				}, 3000);
			});

		   	return false;
	   	});
   	}

   	// Update Selected Images

   	function selectedImgs($tableCol) {
		// Get all selected images
		var $selectedImgs = [],
		    $gallery_field = $tableCol.find('.variation_image_gallery');

		$tableCol.find('.wooThumbs .image').each(function(){
			$selectedImgs.push($(this).attr('data-attachment_id'));
		});

		// Update hidden input with chosen images
    	$gallery_field.val($selectedImgs.join(','));
    	input_changed( $gallery_field );
	}

	function triggerGalleryData() {

    	var $ImgUploadBtns = $('.woocommerce_variations .upload_image_button');

        // set an empty object to store our variation galleries by id
        localCache.set( cacheKey, {} );

        // loop through each upload image btn
		$ImgUploadBtns.each(function(){

			var $uploadBtn = $(this),
			    varId = $uploadBtn.attr('rel'),
			    galleries = {};

            // if the cache is already set, get the current data
			if (localCache.exist( cacheKey )) {
        		var galleries = localCache.get( cacheKey );
    		}

    		if( typeof(galleries[varId]) != "undefined" && galleries[varId] !== null ) {

                // this gallery has been loaded before, so
                // trigger this button as ready
                $('body').trigger( 'gallery_ready', [ $uploadBtn, varId ] );

    		} else {

        		// Set up content to inset after variation Image
    			var ajaxargs = {
    				'action': 		'admin_load_thumbnails',
    				'nonce':   		iconic_woothumbs_vars.nonce,
    				'varID': 		varId
    			}

    			$.ajax({
    				url: iconic_woothumbs_vars.ajaxurl,
    				data: ajaxargs,
    				context: this
    			}).success(function(data) {

        			var gallery = data;

            		// add our gallery to the galleries data
            		// and add it to the cache
            		galleries[varId] = gallery;
        			localCache.set( cacheKey, galleries );

        			// this gallery is now loaded, so,
        			// trigger this button as ready
        			$('body').trigger( 'gallery_ready', [ $uploadBtn, varId ] );

                });

    		}

		});

		refreshGalleryHtml();

	}

	// insert gallery html

	function refreshGalleryHtml() {

    	$('body').on('gallery_ready', function( event, $btn, varId ){

            var galleries = {};

    		if (localCache.exist( cacheKey )) {
        		var galleries = localCache.get( cacheKey );
    		}

    		if( typeof(galleries[varId]) != "undefined" && galleries[varId] !== null ) {

        		var galleryWrapperClass = 'wooThumbs-wrapper--'+varId;

        		$('.'+galleryWrapperClass).remove();

                var $wooThumbs = '

Additional Images
'+galleries[varId]+'Add Additional Images
';
                $btn.after($wooThumbs);

            }

            // Sort Images
			$( ".wooThumbs" ).sortable({
			    deactivate: function(en, ui) {
			        var $tableCol = $(ui.item).closest('.upload_image');
					selectedImgs($tableCol);
			    },
			    placeholder: 'ui-state-highlight'
            });

		});

	}

	function input_changed( $input ) {
    	$input
            .closest( '.woocommerce_variation' )
            .addClass( 'variation-needs-update' );

        $( 'button.cancel-variation-changes, button.save-variation-changes' ).removeAttr( 'disabled' );

        $( '#variable_product_options' ).trigger( 'woocommerce_variations_input_changed' );
	}

	// Setup Variation Image Manager

	function setupVariationImageManagement()
	{
		triggerGalleryData();

		var product_gallery_frame;

		$('.manage_wooThumbs').live( 'click', 'a', function( event ) {

			var $wooThumbs = $(this).siblings('.wooThumbs');
			var $image_gallery_ids = $(this).siblings('.variation_image_gallery');

			var $el = $(this);
			var attachment_ids = $image_gallery_ids.val();

			event.preventDefault();

			// Create the media frame.
			product_gallery_frame = wp.media.frames.downloadable_file = wp.media({
				// Set the title of the modal.
				title: 'Manage Variation Images',
				button: {
					text: 'Add to variation',
				},
				multiple: true
			});

			// When an image is selected, run a callback.
			product_gallery_frame.on( 'select', function() {

				var selection = product_gallery_frame.state().get('selection');

				selection.map( function( attachment ) {

					attachment = attachment.toJSON();

					if ( attachment.id ) {
						attachment_ids = attachment_ids ? attachment_ids + "," + attachment.id : attachment.id;

						$wooThumbs.append('\

\
								\

');
					}

				} );

				$image_gallery_ids.val( attachment_ids );
				input_changed( $image_gallery_ids );
			});

			// Finally, open the modal.
			product_gallery_frame.open();

			return false;
		});

		// Delete Image

		$('.wooThumbs .delete').live("mouseenter mouseleave click", function(event){

			if (event.type == 'click') {
				var $tableCol = $(this).closest('.upload_image');
				// Remove clicked image
				$(this).closest('li').remove();

				selectedImgs($tableCol);
		        return false;
		    }

			if (event.type == 'mouseenter') {
		        $(this).find('img').animate({"opacity": 0.3}, 150);
		    }
		    if (event.type == 'mouseleave') {
		        $(this).find('img').animate({"opacity": 1}, 150);
		    }

		});

		// after variations load

		$( '#woocommerce-product-data' ).on( 'woocommerce_variations_loaded', function(){

    		triggerGalleryData();

		});

		// Once a new variation is added

		$('#variable_product_options').on('woocommerce_variations_added', function(){

    		triggerGalleryData();

		});

	}

    /**
     * Hide "Change Plan" links in Freemius.
     */
	function hide_plans() {

    	//remove_action();
    	//remove_upgrade_button();
    	//remove_activate_free_version();
    	//remove_upgrade_tab();

	}

	/**
	 * Remove change plan action link.
	 */
    function remove_action() {

        var $fs_header_actions = $('.fs-header-actions li');

    	if( $fs_header_actions.length <= 0 ) { return; }

    	$fs_header_actions.each(function( index, action ){

        	var $action = $( action ),
        	    action_text = trim_string( $action.text() );

        	if( action_text !== "Change Plan" && action_text !== "Downgrade" ) { return; }

        	$action.remove();

    	});

    }

    /**
	 * Remove change plan button.
	 */
    function remove_upgrade_button() {

        var $button_upgrade = $('.button-upgrade');

    	if( $button_upgrade.length <= 0 ) { return; }

    	var button_upgrade_text = trim_string( $button_upgrade.text() );

    	if( button_upgrade_text === "Upgrade" ) {

            $button_upgrade.text('Buy License').attr('href', 'https://iconicwp.com').attr('target', '_blank');
        	return;

        }

    	var $buttons = $button_upgrade.siblings('.button');

    	if( $buttons.length <= 1 ) {

        	$button_upgrade.closest('form').removeClass('button-group');

    	}

    	$button_upgrade.remove();

    }

    /**
	 * Remove activate free version link.
	 */
    function remove_activate_free_version() {

        var $activate_free_version = $('.fs-freemium-licensing');

    	if( $activate_free_version.length <= 0 ) { return; }

    	$activate_free_version.remove();

    }

    /**
	 * Remove upgrade tab.
	 */
    function remove_upgrade_tab() {

        var $upgrade_tab = $('.nav-tab');

    	if( $upgrade_tab.length <= 0 ) { return; }

    	$upgrade_tab.each(function( index, tab ){

        	var $tab = $( tab ),
        	    tab_text = trim_string( $tab.text() );

        	if( tab_text !== "Upgrade" ) { return; }

        	$tab.remove();

    	});

    }

	/**
	 * Trim string and remove special characters.
	 */
	function trim_string( str ) {

    	return $.trim( str.replace(/[^a-z0-9\s]/gi, '') );

	}

}(jQuery, document));


text:
'use strict';
const common = require('../common');
const assert = require('assert');
const v8 = require('v8');
const { Worker, resourceLimits, isMainThread } = require('worker_threads');

if (isMainThread) {
  assert.deepStrictEqual(resourceLimits, {});
}

const testResourceLimits = {
  maxOldGenerationSizeMb: 16,
  maxYoungGenerationSizeMb: 4,
  codeRangeSizeMb: 16,
};

// Do not use isMainThread so that this test itself can be run inside a Worker.
if (!process.env.HAS_STARTED_WORKER) {
  process.env.HAS_STARTED_WORKER = 1;
  const w = new Worker(__filename, { resourceLimits: testResourceLimits });
  assert.deepStrictEqual(w.resourceLimits, testResourceLimits);
  w.on('exit', common.mustCall((code) => {
    assert.strictEqual(code, 1);
    assert.deepStrictEqual(w.resourceLimits, {});
  }));
  w.on('error', common.expectsError({
    code: 'ERR_WORKER_OUT_OF_MEMORY',
    message: 'Worker terminated due to reaching memory limit: ' +
    'JS heap out of memory'
  }));
  return;
}

assert.deepStrictEqual(resourceLimits, testResourceLimits);
const array = [];
while (true) {
  // Leave 10 % wiggle room here.
  const usedMB = v8.getHeapStatistics().used_heap_size / 1024 / 1024;
  assert(usedMB < resourceLimits.maxOldGenerationSizeMb * 1.1);

  let seenSpaces = 0;
  for (const { space_name, space_size } of v8.getHeapSpaceStatistics()) {
    if (space_name === 'new_space') {
      seenSpaces++;
      assert(
        space_size / 1024 / 1024 < resourceLimits.maxYoungGenerationSizeMb * 2);
    } else if (space_name === 'old_space') {
      seenSpaces++;
      assert(space_size / 1024 / 1024 < resourceLimits.maxOldGenerationSizeMb);
    } else if (space_name === 'code_space') {
      seenSpaces++;
      assert(space_size / 1024 / 1024 < resourceLimits.codeRangeSizeMb);
    }
  }
  assert.strictEqual(seenSpaces, 3);

  for (let i = 0; i < 100; i++)
    array.push([array]);
}


text:
I needed a tool to calculate time-window event rollups, and I wanted to learn functional programming with various languages.

Now aspiring to provide libs that other people could actually use!

## Example

If you have a list of event timestamps, say something like this:

    127.0.0.1 - - [01/Dec/2011:00:00:11 -0500] "GET / HTTP/1.0" 304
    127.0.0.1 - - [01/Dec/2011:00:00:24 -0500] "GET /feed/rss2/ HTTP/1.0" 301
    127.0.0.1 - - [01/Dec/2011:00:00:25 -0500] "GET /feed/ HTTP/1.0" 304
    127.0.0.1 - - [01/Dec/2011:00:00:30 -0500] "GET /robots.txt HTTP/1.0" 200
    127.0.0.1 - - [01/Dec/2011:00:00:30 -0500] "GET / HTTP/1.0" 200
    127.0.0.1 - - [01/Dec/2011:00:01:35 -0500] "POST /wp-cron.php?doing_wp_cron HTTP/1.0" 200

then you could run one of these tools like so:

    gzcat event-data | rollup.coffee -w 1d

and you'd get something like this:

    Start	End	Count
    2011-12-01 00:00	2011-12-01 23:59	2823
    2011-12-02 00:00	2011-12-02 23:59	2572
    2011-12-03 00:00	2011-12-03 23:59	2621
    2011-12-04 00:00	2011-12-04 23:59	3027

## Aspirations
I plan to:

* support various input and output formats (but not too many)
* support usage as either a command-line tool or as a programmatic library
* make it fast

## Language Variants

A big part of my goal was to learn functional programming by implementing this functionality in various languages.

The goal is for each variant to be functionally equivalent, but I'm not quite there yet.

So here's some notes on the status of each one:

### Clojure
* Fast — possibly the fastest with small windows
* Should already be usable as a library

### CoffeeScript / Node
* Fast
* Probably not safe to use as a library yet

### Scala
* Slow
* Window spec is currently hard-coded to 1 day

## To Dos

### Definite
* Support other event timestamp formats
* Support other output formats (starting with JSON)
* Support cmd-line arg for csv separator
* Refactor to be usable as a general-purpose library
  * CoffeeScript: either in Node or in a browser

### Possible
* Decide whether data must be passed in sorted or not (would allow for some optimizations)
* Add behaviour tests!!
* Support parallellization (off by default)
* Add an option to specify whether weeks should start on Sunday or Monday
* Support rollup windows of N months
* Support the input already being a rollup, of which we'd do a bigger rollup
  * so you might store a per-minute rollup in a file, and generate a per-hour rollup from that
  * kinda like re-reduce
  * This'd work really well with a companion tool, something which would support incremental processing by keeping track of where you were in a file or stream, and grabbing only the new part of the data, then updating the pointer


text:
Multiplies slices of two tensors in batches.

Multiplies all slices of `Tensor` `x` and `y` (each slice can be
viewed as an element of a batch), and arranges the individual results
in a single output tensor of the same batch size. Each of the
individual slices can optionally be adjointed (to adjoint a matrix
means to transpose and conjugate it) before multiplication by setting
the `adj_x` or `adj_y` flag to `True`, which are by default `False`.

The input tensors `x` and `y` are 3-D or higher with shape `[..., r_x, c_x]`
and `[..., r_y, c_y]`.

The output tensor is 3-D or higher with shape `[..., r_o, c_o]`, where:

    r_o = c_x if adj_x else r_x
    c_o = r_y if adj_y else c_y

It is computed as:

    output[..., :, :] = matrix(x[..., :, :]) * matrix(y[..., :, :])

##### Args:

*  <b>`x`</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `complex64`.
    3-D or higher with shape `[..., r_x, c_x]`.
*  <b>`y`</b>: A `Tensor`. Must have the same type as `x`.
    3-D or higher with shape `[..., r_y, c_y]`.
*  <b>`adj_x`</b>: An optional `bool`. Defaults to `False`.
    If `True`, adjoint the slices of `x`. Defaults to `False`.
*  <b>`adj_y`</b>: An optional `bool`. Defaults to `False`.
    If `True`, adjoint the slices of `y`. Defaults to `False`.
*  <b>`name`</b>: A name for the operation (optional).

##### Returns:

  A `Tensor`. Has the same type as `x`.
  3-D or higher with shape `[..., r_o, c_o]`


text:
package models

// Rule ..
type Rule struct {
	Num         string `json:"num"`
	Pkts        string `json:"pkts"`
	Bytes       string `json:"bytes"`
	Target      string `json:"target"`
	Prot        string `json:"prot"`
	Opt         string `json:"opt"`
	In          string `json:"in"`
	Out         string `json:"out"`
	Source      string `json:"source"`
	Destination string `json:"destination"`
	Details     string `json:"details"`
	Comment     string `json:"comment"`
}


text:
@ECHO OFF
SET EXTNAME="extension-facebook"

rmdir "project\obj" /s /q
lime rebuild . ios
rmdir "project\obj" /s /q
REM Build extension
zip -r %EXTNAME%.zip extension haxelib.json include.xml project ndll dependencies frameworks template


text:
module Spree
  module Kelkoo
    class Config
      include Singleton
      include PreferenceAccess

      class << self
        def instance
          return nil unless ActiveRecord::Base.connection.tables.include?('configurations')
          KelkooConfiguration.find_or_create_by_name("Kelkoo configuration")
        end
      end
    end
  end
end


text:
<!DOCTYPE html>

		<title>What2Watch</title>
		<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato">
		<link rel="stylesheet" type="text/css" href="style/style.css" />

What2Watch

How are you feeling today?

			<input type="button" class="button happy" onclick = "clicked(this,'Happy')" value="Happy">
			<input type="button" class="button sad" onclick = "clicked(this,'Sad')" value="Sad">
			<input type="button" class="button calm" onclick = "clicked(this,'Calm')" value="Calm">
			<input type="button" class="button excited" onclick = "clicked(this,'Excited')" value="Excited">
			<input type="button" class="button adventurous" onclick = "clicked(this,'Adventurous')" value="Adventurous">

Have you tried this movie before?

Click here to go to RottenTomatoes website.

copyright &copy YoungChul Chun


text:
r"""TF Estimator-based ResNet compatible with TPU Distribution Strategy.

This is an implementation of TensorFlow Estimator-based ResNet and is
additionally compatible with TPU Distribution Strategy.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import json
import os
from absl import app
import tensorflow as tf

import imagenet_input
import resnet_model
from tensorflow.contrib.distribute.python import tpu_strategy as tpu_lib

tf.flags.DEFINE_string('tpu', None, 'Name of TPU to run this against')
tf.flags.DEFINE_string('gcp_project', None, 'GCP project containing the TPU')
tf.flags.DEFINE_string('tpu_zone', None, 'GCP zone of the TPU')

tf.flags.DEFINE_integer('num_cores', 8, 'Number of cores in TPU')
tf.flags.DEFINE_string('data_dir', None, 'Directory containing ImageNet data')
tf.flags.DEFINE_string('model_dir', '',
                       'Directory containing model data and checkpoints')
tf.flags.DEFINE_integer('train_batch_size', 128, 'Per core batch size')
tf.flags.DEFINE_integer('eval_batch_size', 125, 'Per core batch size')
tf.flags.DEFINE_string('precision', 'bfloat16',
                       'Precision to use; one of: {bfloat16, float32}')
tf.flags.DEFINE_bool('use_keras_model', False,
                     'Whether to use Keras implementation of ResNet model')
tf.flags.DEFINE_bool('transpose_input', True,
                     'Whether to transpose input for better performance')
tf.flags.DEFINE_string('optimizer', 'momentum',
                       'Optimizer to use; one of: {momentum, sgd}')

FLAGS = tf.flags.FLAGS

_NUM_TRAIN_IMAGES = 1281167
_NUM_EVAL_IMAGES = 50000
_NUM_CLASSES = 1000
_RESNET_DEPTH = 50
_LEARNING_RATE = 0.1
_MOMENTUM = 0.9
_WEIGHT_DECAY = 1e-4

_TRAIN_STEPS = 112590

MEAN_RGB = [0.485, 0.456, 0.406]
STDDEV_RGB = [0.229, 0.224, 0.225]

# Learning rate schedule (much more aggressive for testing)
LR_SCHEDULE = [    # (multiplier, epoch to start) tuples
    (1.0, 5), (0.1, 30), (0.01, 60), (0.001, 80)
]

def learning_rate_schedule(current_epoch):
  scaled_lr = (_LEARNING_RATE *
               (FLAGS.train_batch_size * FLAGS.num_cores / 256.0))
  decay_rate = (scaled_lr * LR_SCHEDULE[0][0] *
                tf.to_float(current_epoch) / LR_SCHEDULE[0][1])
  for mult, start_epoch in LR_SCHEDULE:
    decay_rate = tf.where(current_epoch < start_epoch,
                          decay_rate, scaled_lr * mult)
  return decay_rate

def model_fn(features, labels, mode):
  """Definition for ResNet model."""
  is_training = mode == tf.estimator.ModeKeys.TRAIN

  if FLAGS.transpose_input:
    features = tf.transpose(features, [3, 0, 1, 2])  # Double-transpose trick

  # Normalize the image to zero mean and unit variance.
  features -= tf.constant(MEAN_RGB, shape=[1, 1, 3], dtype=features.dtype)
  features /= tf.constant(STDDEV_RGB, shape=[1, 1, 3], dtype=features.dtype)

  def create_model():
    """Create the model and compute the logits."""
    if FLAGS.use_keras_model:
      model = tf.keras.applications.resnet50.ResNet50(
          include_top=True,
          weights=None,
          input_tensor=None,
          input_shape=None,
          pooling=None,
          classes=_NUM_CLASSES)
      return model(features, training=is_training)
    else:
      model = resnet_model.resnet_v1(
          resnet_depth=_RESNET_DEPTH,
          num_classes=_NUM_CLASSES,
          data_format='channels_last')
      return model(inputs=features, is_training=is_training)

  if FLAGS.precision == 'bfloat16':
    with tf.contrib.tpu.bfloat16_scope():
      logits = create_model()
  else:
    logits = create_model()

  logits = tf.cast(logits, tf.float32)

  if mode == tf.estimator.ModeKeys.PREDICT:
    assert False, 'Not implemented correctly right now!'
    predictions = {'logits': logits}
    return tf.estimator.EstimatorSpec(mode, predictions=predictions)

  cross_entropy = tf.losses.sparse_softmax_cross_entropy(
      labels=labels, logits=logits)

  loss = cross_entropy + _WEIGHT_DECAY * tf.add_n(
      [tf.nn.l2_loss(v) for v in tf.trainable_variables()
       if 'batch_normalization' not in v.name])

  if mode == tf.estimator.ModeKeys.EVAL:
    predictions = tf.argmax(logits, axis=1)
    top_1_accuracy = tf.metrics.accuracy(labels, predictions)
    # TODO(priyag): Add this back when in_top_k is supported on TPU.
    # in_top_5 = tf.cast(tf.nn.in_top_k(logits, labels, 5), tf.float32)
    # top_5_accuracy = tf.metrics.mean(in_top_5)

    eval_metric_ops = {
        'top_1_accuracy': top_1_accuracy,
        # 'top_5_accuracy': top_5_accuracy,
    }

    return tf.estimator.EstimatorSpec(
        mode, loss=loss, eval_metric_ops=eval_metric_ops)

  assert mode == tf.estimator.ModeKeys.TRAIN

  global_step = tf.train.get_or_create_global_step()
  batches_per_epoch = (_NUM_TRAIN_IMAGES /
                       (FLAGS.train_batch_size * FLAGS.num_cores))
  current_epoch = (tf.cast(global_step, tf.float32) / batches_per_epoch)
  learning_rate = learning_rate_schedule(current_epoch)

  if FLAGS.optimizer == 'sgd':
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
  else:
    optimizer = tf.train.MomentumOptimizer(
        learning_rate=learning_rate,
        momentum=_MOMENTUM,
        use_nesterov=True)

  update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
  with tf.control_dependencies(update_ops):
    train_op = optimizer.minimize(loss, global_step=global_step)
  return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)

def main(unused_argv):
  """Starts a ResNet training session."""
  tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
      FLAGS.tpu,
      zone=FLAGS.tpu_zone,
      project=FLAGS.gcp_project)

  # Estimator looks at the master it connects to for MonitoredTrainingSession
  # by reading the `TF_CONFIG` environment variable.
  tf_config_env = {
      'session_master': tpu_cluster_resolver.get_master(),
      'eval_session_master': tpu_cluster_resolver.get_master()
  }
  os.environ['TF_CONFIG'] = json.dumps(tf_config_env)

  steps_per_run_train = _NUM_TRAIN_IMAGES // (
      FLAGS.train_batch_size * FLAGS.num_cores)
  steps_per_run_eval = _NUM_EVAL_IMAGES // (
      FLAGS.eval_batch_size * FLAGS.num_cores)
  steps_per_eval = steps_per_run_train

  train_distribution = tpu_lib.TPUStrategy(tpu_cluster_resolver,
                                           steps_per_run=steps_per_run_train)
  eval_distribution = tpu_lib.TPUStrategy(tpu_cluster_resolver,
                                          steps_per_run=steps_per_run_eval)
  config = tf.estimator.RunConfig(
      model_dir=FLAGS.model_dir,
      train_distribute=train_distribution,
      eval_distribute=eval_distribution,
      save_checkpoints_steps=steps_per_eval,
      save_checkpoints_secs=None,
      keep_checkpoint_max=1000)

  resnet_estimator = tf.estimator.Estimator(
      model_fn=model_fn, config=config)

  train_input, eval_input = [
      imagenet_input.ImageNetInput(
          is_training=is_training,
          data_dir=FLAGS.data_dir,
          transpose_input=FLAGS.transpose_input,
          use_bfloat16=(FLAGS.precision == 'bfloat16'))
      for is_training in [True, False]
  ]

  try:
    current_step = resnet_estimator.get_variable_value(tf.GraphKeys.GLOBAL_STEP)
  except ValueError:
    current_step = 0

  while current_step < _TRAIN_STEPS:
    next_checkpoint = min(current_step + steps_per_eval, _TRAIN_STEPS)

    resnet_estimator.train(
        input_fn=lambda: train_input.input_fn(  # pylint: disable=g-long-lambda
            {'batch_size': FLAGS.train_batch_size}),
        max_steps=next_checkpoint)
    current_step = next_checkpoint

    eval_results = resnet_estimator.evaluate(
        input_fn=lambda: eval_input.input_fn(  # pylint: disable=g-long-lambda
            {'batch_size': FLAGS.eval_batch_size}),
        steps=_NUM_EVAL_IMAGES // (FLAGS.eval_batch_size * FLAGS.num_cores))

    tf.logging.info('Eval results: %s' % eval_results)

if __name__ == '__main__':
  tf.logging.set_verbosity(tf.logging.INFO)
  app.run(main)