text:
package com.amazonaws.services.securityhub.model;

import java.io.Serializable;
import javax.annotation.Generated;
import com.amazonaws.protocol.StructuredPojo;
import com.amazonaws.protocol.ProtocolMarshaller;

/**
 *

 * Provides information about the VPC peering connection options for the accepter or requester VPC.
 *

 *
 * @see AWS API Documentation
 */
@Generated("com.amazonaws:aws-java-sdk-code-generator")
public class VpcInfoPeeringOptionsDetails implements Serializable, Cloneable, StructuredPojo {

    /**
     *

     * Indicates whether a local VPC can resolve public DNS hostnames to private IP addresses when queried from
     * instances in a peer VPC.
     *

     */
    private Boolean allowDnsResolutionFromRemoteVpc;
    /**
     *

     * Indicates whether a local ClassicLink connection can communicate with the peer VPC over the VPC peering
     * connection.
     *

     */
    private Boolean allowEgressFromLocalClassicLinkToRemoteVpc;
    /**
     *

     * Indicates whether a local VPC can communicate with a ClassicLink connection in the peer VPC over the VPC peering
     * connection.
     *

     */
    private Boolean allowEgressFromLocalVpcToRemoteClassicLink;

    /**
     *

     * Indicates whether a local VPC can resolve public DNS hostnames to private IP addresses when queried from
     * instances in a peer VPC.
     *

     *
     * @param allowDnsResolutionFromRemoteVpc
     *        Indicates whether a local VPC can resolve public DNS hostnames to private IP addresses when queried from
     *        instances in a peer VPC.
     */

    public void setAllowDnsResolutionFromRemoteVpc(Boolean allowDnsResolutionFromRemoteVpc) {
        this.allowDnsResolutionFromRemoteVpc = allowDnsResolutionFromRemoteVpc;
    }

    /**
     *

     * Indicates whether a local VPC can resolve public DNS hostnames to private IP addresses when queried from
     * instances in a peer VPC.
     *

     *
     * @return Indicates whether a local VPC can resolve public DNS hostnames to private IP addresses when queried from
     *         instances in a peer VPC.
     */

    public Boolean getAllowDnsResolutionFromRemoteVpc() {
        return this.allowDnsResolutionFromRemoteVpc;
    }

    /**
     *

     * Indicates whether a local VPC can resolve public DNS hostnames to private IP addresses when queried from
     * instances in a peer VPC.
     *

     *
     * @param allowDnsResolutionFromRemoteVpc
     *        Indicates whether a local VPC can resolve public DNS hostnames to private IP addresses when queried from
     *        instances in a peer VPC.
     * @return Returns a reference to this object so that method calls can be chained together.
     */

    public VpcInfoPeeringOptionsDetails withAllowDnsResolutionFromRemoteVpc(Boolean allowDnsResolutionFromRemoteVpc) {
        setAllowDnsResolutionFromRemoteVpc(allowDnsResolutionFromRemoteVpc);
        return this;
    }

    /**
     *

     * Indicates whether a local VPC can resolve public DNS hostnames to private IP addresses when queried from
     * instances in a peer VPC.
     *

     *
     * @return Indicates whether a local VPC can resolve public DNS hostnames to private IP addresses when queried from
     *         instances in a peer VPC.
     */

    public Boolean isAllowDnsResolutionFromRemoteVpc() {
        return this.allowDnsResolutionFromRemoteVpc;
    }

    /**
     *

     * Indicates whether a local ClassicLink connection can communicate with the peer VPC over the VPC peering
     * connection.
     *

     *
     * @param allowEgressFromLocalClassicLinkToRemoteVpc
     *        Indicates whether a local ClassicLink connection can communicate with the peer VPC over the VPC peering
     *        connection.
     */

    public void setAllowEgressFromLocalClassicLinkToRemoteVpc(Boolean allowEgressFromLocalClassicLinkToRemoteVpc) {
        this.allowEgressFromLocalClassicLinkToRemoteVpc = allowEgressFromLocalClassicLinkToRemoteVpc;
    }

    /**
     *

     * Indicates whether a local ClassicLink connection can communicate with the peer VPC over the VPC peering
     * connection.
     *

     *
     * @return Indicates whether a local ClassicLink connection can communicate with the peer VPC over the VPC peering
     *         connection.
     */

    public Boolean getAllowEgressFromLocalClassicLinkToRemoteVpc() {
        return this.allowEgressFromLocalClassicLinkToRemoteVpc;
    }

    /**
     *

     * Indicates whether a local ClassicLink connection can communicate with the peer VPC over the VPC peering
     * connection.
     *

     *
     * @param allowEgressFromLocalClassicLinkToRemoteVpc
     *        Indicates whether a local ClassicLink connection can communicate with the peer VPC over the VPC peering
     *        connection.
     * @return Returns a reference to this object so that method calls can be chained together.
     */

    public VpcInfoPeeringOptionsDetails withAllowEgressFromLocalClassicLinkToRemoteVpc(Boolean allowEgressFromLocalClassicLinkToRemoteVpc) {
        setAllowEgressFromLocalClassicLinkToRemoteVpc(allowEgressFromLocalClassicLinkToRemoteVpc);
        return this;
    }

    /**
     *

     * Indicates whether a local ClassicLink connection can communicate with the peer VPC over the VPC peering
     * connection.
     *

     *
     * @return Indicates whether a local ClassicLink connection can communicate with the peer VPC over the VPC peering
     *         connection.
     */

    public Boolean isAllowEgressFromLocalClassicLinkToRemoteVpc() {
        return this.allowEgressFromLocalClassicLinkToRemoteVpc;
    }

    /**
     *

     * Indicates whether a local VPC can communicate with a ClassicLink connection in the peer VPC over the VPC peering
     * connection.
     *

     *
     * @param allowEgressFromLocalVpcToRemoteClassicLink
     *        Indicates whether a local VPC can communicate with a ClassicLink connection in the peer VPC over the VPC
     *        peering connection.
     */

    public void setAllowEgressFromLocalVpcToRemoteClassicLink(Boolean allowEgressFromLocalVpcToRemoteClassicLink) {
        this.allowEgressFromLocalVpcToRemoteClassicLink = allowEgressFromLocalVpcToRemoteClassicLink;
    }

    /**
     *

     * Indicates whether a local VPC can communicate with a ClassicLink connection in the peer VPC over the VPC peering
     * connection.
     *

     *
     * @return Indicates whether a local VPC can communicate with a ClassicLink connection in the peer VPC over the VPC
     *         peering connection.
     */

    public Boolean getAllowEgressFromLocalVpcToRemoteClassicLink() {
        return this.allowEgressFromLocalVpcToRemoteClassicLink;
    }

    /**
     *

     * Indicates whether a local VPC can communicate with a ClassicLink connection in the peer VPC over the VPC peering
     * connection.
     *

     *
     * @param allowEgressFromLocalVpcToRemoteClassicLink
     *        Indicates whether a local VPC can communicate with a ClassicLink connection in the peer VPC over the VPC
     *        peering connection.
     * @return Returns a reference to this object so that method calls can be chained together.
     */

    public VpcInfoPeeringOptionsDetails withAllowEgressFromLocalVpcToRemoteClassicLink(Boolean allowEgressFromLocalVpcToRemoteClassicLink) {
        setAllowEgressFromLocalVpcToRemoteClassicLink(allowEgressFromLocalVpcToRemoteClassicLink);
        return this;
    }

    /**
     *

     * Indicates whether a local VPC can communicate with a ClassicLink connection in the peer VPC over the VPC peering
     * connection.
     *

     *
     * @return Indicates whether a local VPC can communicate with a ClassicLink connection in the peer VPC over the VPC
     *         peering connection.
     */

    public Boolean isAllowEgressFromLocalVpcToRemoteClassicLink() {
        return this.allowEgressFromLocalVpcToRemoteClassicLink;
    }

    /**
     * Returns a string representation of this object. This is useful for testing and debugging. Sensitive data will be
     * redacted from this string using a placeholder value.
     *
     * @return A string representation of this object.
     *
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        if (getAllowDnsResolutionFromRemoteVpc() != null)
            sb.append("AllowDnsResolutionFromRemoteVpc: ").append(getAllowDnsResolutionFromRemoteVpc()).append(",");
        if (getAllowEgressFromLocalClassicLinkToRemoteVpc() != null)
            sb.append("AllowEgressFromLocalClassicLinkToRemoteVpc: ").append(getAllowEgressFromLocalClassicLinkToRemoteVpc()).append(",");
        if (getAllowEgressFromLocalVpcToRemoteClassicLink() != null)
            sb.append("AllowEgressFromLocalVpcToRemoteClassicLink: ").append(getAllowEgressFromLocalVpcToRemoteClassicLink());
        sb.append("}");
        return sb.toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;

        if (obj instanceof VpcInfoPeeringOptionsDetails == false)
            return false;
        VpcInfoPeeringOptionsDetails other = (VpcInfoPeeringOptionsDetails) obj;
        if (other.getAllowDnsResolutionFromRemoteVpc() == null ^ this.getAllowDnsResolutionFromRemoteVpc() == null)
            return false;
        if (other.getAllowDnsResolutionFromRemoteVpc() != null
                && other.getAllowDnsResolutionFromRemoteVpc().equals(this.getAllowDnsResolutionFromRemoteVpc()) == false)
            return false;
        if (other.getAllowEgressFromLocalClassicLinkToRemoteVpc() == null ^ this.getAllowEgressFromLocalClassicLinkToRemoteVpc() == null)
            return false;
        if (other.getAllowEgressFromLocalClassicLinkToRemoteVpc() != null
                && other.getAllowEgressFromLocalClassicLinkToRemoteVpc().equals(this.getAllowEgressFromLocalClassicLinkToRemoteVpc()) == false)
            return false;
        if (other.getAllowEgressFromLocalVpcToRemoteClassicLink() == null ^ this.getAllowEgressFromLocalVpcToRemoteClassicLink() == null)
            return false;
        if (other.getAllowEgressFromLocalVpcToRemoteClassicLink() != null
                && other.getAllowEgressFromLocalVpcToRemoteClassicLink().equals(this.getAllowEgressFromLocalVpcToRemoteClassicLink()) == false)
            return false;
        return true;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int hashCode = 1;

        hashCode = prime * hashCode + ((getAllowDnsResolutionFromRemoteVpc() == null) ? 0 : getAllowDnsResolutionFromRemoteVpc().hashCode());
        hashCode = prime * hashCode
                + ((getAllowEgressFromLocalClassicLinkToRemoteVpc() == null) ? 0 : getAllowEgressFromLocalClassicLinkToRemoteVpc().hashCode());
        hashCode = prime * hashCode
                + ((getAllowEgressFromLocalVpcToRemoteClassicLink() == null) ? 0 : getAllowEgressFromLocalVpcToRemoteClassicLink().hashCode());
        return hashCode;
    }

    @Override
    public VpcInfoPeeringOptionsDetails clone() {
        try {
            return (VpcInfoPeeringOptionsDetails) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new IllegalStateException("Got a CloneNotSupportedException from Object.clone() " + "even though we're Cloneable!", e);
        }
    }

    @com.amazonaws.annotation.SdkInternalApi
    @Override
    public void marshall(ProtocolMarshaller protocolMarshaller) {
        com.amazonaws.services.securityhub.model.transform.VpcInfoPeeringOptionsDetailsMarshaller.getInstance().marshall(this, protocolMarshaller);
    }
}


text:
"""Implementation of a backup service using Datera Elastic Data Fabric

Architecture:
    Container/Bucket --> Datera EDF Volume (single volume AppInstance)
    Object --> Datera EDF Volume Snapshot
    ObjectID --> Datera EDF Volume Snapshot Timestamp

Essentially, we create a Volume as a "Bucket", then write data to that volume
and snapshot it.  The snapshots serves as our "Object" analogue.  We can
restore a backup by restoring snapshots in reverse order and reading the data
back.

Since our minimum volume size is 1 GB, we'll use that as our minimum chunk size

Multiplexing:
        This version of the driver also handles multiplexing between different
        drivers.  We determine the driver type by something!

"""
import contextlib
import hashlib
import os
import shlex
import six
import struct
import subprocess
import time
import uuid

import eventlet

from eventlet.green import threading

from oslo_concurrency import processutils as putils
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import excutils
from oslo_utils import importutils
from oslo_utils import units

from cinder.backup import chunkeddriver
from cinder import exception
from cinder.i18n import _
from cinder import interface
from cinder import utils

from os_brick import exception as brick_exception

import cinder.volume.drivers.datera.datera_common as datc

LOG = logging.getLogger(__name__)

bd_opts = [
    cfg.StrOpt('backup_datera_san_ip',
               default=None,
               help='(REQUIRED) IP address of Datera EDF backend'),
    cfg.StrOpt('backup_datera_san_login',
               default=None,
               help='(REQUIRED) Username for Datera EDF backend account'),
    cfg.StrOpt('backup_datera_san_password',
               default=None,
               help='(REQUIRED) Password for Datera EDF backend account'),
    cfg.StrOpt('backup_datera_tenant_id',
               default='/root',
               help='Datera tenant_id under which backup should be stored'),
    cfg.IntOpt('backup_datera_chunk_size',
               default=1,
               help='Total chunk size (in GB, min 1 GB) to use for backup'),
    cfg.BoolOpt('backup_datera_progress_timer',
                default=False,
                help='Enable progress timer for backup'),
    cfg.IntOpt('backup_datera_replica_count',
               default=3,
               help='Number of replicas for container'),
    cfg.StrOpt('backup_datera_placement_mode',
               default='hybrid',
               help='Options: hybrid, single_flash, all_flash'),
    cfg.StrOpt('backup_datera_api_port',
               default='7717',
               help='Datera API port.'),
    cfg.ListOpt('backup_datera_secondary_backup_drivers',
                default=[],
                help='Secondary drivers to manage with this driver.  This is '
                     'done as a way to simulate a scheduler for backups. '
                     'Takes the form:\n'
                     '["cinder.backup.drivers.driver1",\n'
                     ' "cinder.backup.drivers.driver2"]'),
    cfg.BoolOpt('backup_datera_debug',
                default=False,
                help="True to set function arg and return logging"),
    cfg.IntOpt('backup_datera_503_timeout',
               default='120',
               help='Timeout for HTTP 503 retry messages'),
    cfg.IntOpt('backup_datera_503_interval',
               default='5',
               help='Interval between 503 retries'),
    cfg.BoolOpt('backup_datera_disable_profiler',
                default=False,
                help="Set to True to disable profiling in the Datera driver"),
    cfg.BoolOpt('backup_driver_use_ssl', default=False,
                help="Set True to use SSL. Must also provide cert options"),
    cfg.StrOpt('backup_driver_client_cert',
               default=None,
               help="Path to client certificate file"),
    cfg.StrOpt('backup_driver_client_cert_key',
               default=None,
               help="Path to client certificate key file")
]

CONF = cfg.CONF
CONF.register_opts(bd_opts)

METADATA = "_metadata"
SHA256 = "_sha256file"
PREFIX = "DAT"
SI_NAME = 'storage-1'
VOL_NAME = 'volume-1'
PACK = "iQ32sxx"
TOTAL_OFFSET = 50

@interface.backupdriver
@six.add_metaclass(utils.TraceWrapperWithABCMetaclass)
class DateraBackupDriver(chunkeddriver.ChunkedBackupDriver):

    """Provides backup, restore and delete of backup objects within Datera EDF.

    Version history:
        1.0.0 - Initial driver
        1.0.1 - Added secondary backup driver dispatching/multiplexing
        2018.5.1.0 - Switched to date-based versioning scheme
    """

    VERSION = '2018.5.1.0'

    HEADER_DATA = {'Datera-Driver': 'OpenStack-Backup-{}'.format(VERSION)}

    def __init__(self, context, db_driver=None):
        # Ensure we have room for offset headers
        chunk_size = CONF.backup_datera_chunk_size * units.Gi - TOTAL_OFFSET
        # We don't care about chunks any smaller than our normal chunk size
        sha_size = chunk_size
        container_name = "replace-me"
        super(DateraBackupDriver, self).__init__(context, chunk_size,
                                                 sha_size, container_name,
                                                 db_driver)
        self.ctxt = context
        self.db_driver = db_driver
        self.support_force_delete = True
        self._backup = None
        self.san_ip = CONF.backup_datera_san_ip
        self.username = CONF.backup_datera_san_login
        self.password = CONF.backup_datera_san_password
        self.api_port = CONF.backup_datera_api_port
        self.driver_use_ssl = CONF.backup_driver_use_ssl
        self.driver_client_cert = CONF.backup_driver_client_cert
        self.driver_client_cert_key = CONF.backup_driver_client_cert_key
        self.replica_count = CONF.backup_datera_replica_count
        self.placement_mode = CONF.backup_datera_placement_mode
        self.driver_strs = CONF.backup_datera_secondary_backup_drivers
        self.driver = None
        self.drivers = {}
        self.type = 'datera'
        self.cluster_stats = {}
        self.datera_api_token = None
        self.interval = CONF.backup_datera_503_interval
        self.retry_attempts = (CONF.backup_datera_503_timeout /
                               self.interval)
        self.driver_prefix = str(uuid.uuid4())[:4]
        self.datera_debug = CONF.backup_datera_debug
        self.datera_api_versions = []

        if self.datera_debug:
            utils.setup_tracing(['method'])
        self.tenant_id = CONF.backup_datera_tenant_id
        if self.tenant_id and self.tenant_id.lower() == 'none':
            self.tenant_id = None
        self.api_check = time.time()
        self.api_cache = []
        self.api_timeout = 0
        self.do_profile = not CONF.backup_datera_disable_profiler
        self.thread_local = threading.local()
        self.thread_local.trace_id = ""

        self._populate_secondary_drivers()

        datc.register_driver(self)
        self._check_options()

    def _populate_secondary_drivers(self):
        for dstr in self.driver_strs:
            driver = importutils.import_module(dstr)
            self.drivers[dstr.split(".")[-1]] = driver

    @staticmethod
    def _execute(cmd):
        parts = shlex.split(cmd)
        putils.execute(*parts, root_helper=utils.get_root_helper(),
                       run_as_root=True)

    def login(self):
        """Use the san_login and san_password to set token."""
        body = {
            'name': self.username,
            'password': self.password
        }

        # Unset token now, otherwise potential expired token will be sent
        # along to be used for authorization when trying to login.
        self.datera_api_token = None

        try:
            LOG.debug('Getting Datera auth token.')
            results = self._issue_api_request(
                'login', 'put', body=body, sensitive=True, api_version='2.1',
                tenant=None)
            self.datera_api_token = results['key']
        except exception.NotAuthorized:
            with excutils.save_and_reraise_exception():
                LOG.error('Logging into the Datera cluster failed. Please '
                          'check your username and password set in the '
                          'cinder.conf and start the cinder-volume '
                          'service again.')

    def _check_options(self):
        req_opts = ('backup_datera_san_ip',
                    'backup_datera_san_login',
                    'backup_datera_san_password')
        no_opts = filter(lambda opt: not getattr(CONF, opt, None), req_opts)
        if no_opts:
            raise exception.InvalidInput(
                reason=_('Missing required opts %s') % no_opts)

    def _create_volume(self, name, size):
        tenant = self.tenant_id
        app_params = (
            {
                'create_mode': "openstack",
                'name': name,
                'access_control_mode': 'deny_all',
                'storage_instances': [
                    {
                        'name': SI_NAME,
                        'volumes': [
                            {
                                'name': VOL_NAME,
                                'size': size,
                                'placement_mode': self.placement_mode,
                                'replica_count': self.replica_count,
                                'snapshot_policies': [
                                ]
                            }
                        ]
                    }
                ]
            })
        self._issue_api_request(datc.URL_TEMPLATES['ai'](), 'post',
                                body=app_params, api_version='2.1',
                                tenant=tenant)

    def _detach_volume(self, name):
        url = datc.URL_TEMPLATES['ai_inst']().format(name)
        data = {
            'admin_state': 'offline',
            'force': True
        }
        try:
            self._issue_api_request(url, method='put', body=data,
                                    api_version='2.1', tenant=self.tenant_id)
        except exception.NotFound:
            msg = _("Tried to detach volume %s, but it was not found in the "
                    "Datera cluster. Continuing with detach.")
            LOG.info(msg, name)

    def _delete_volume(self, name):
        self._detach_volume(name)
        try:
            self._issue_api_request(
                datc.URL_TEMPLATES['ai_inst']().format(name), 'delete',
                api_version='2.1', tenant=self.tenant_id)
        except (exception.DateraAPIException, exception.NotFound):
            LOG.debug("Couldn't find volume: {}".format(name))

    def _volume_exists(self, bname):
        try:
            self._issue_api_request(datc.URL_TEMPLATES['ai_inst']().format(
                bname), 'get', api_version='2.1', tenant=self.tenant_id)
            return True
        except exception.NotFound:
            return False

    def _create_snapshot(self, bname):
        snap = self._issue_api_request(datc.URL_TEMPLATES['vol_inst'](
            SI_NAME, VOL_NAME).format(bname) + '/snapshots', 'post',
            body={}, api_version='2.1', tenant=self.tenant_id)
        # Polling the snapshot is absolutely necessary otherwise we hit race
        # conditions that can cause the snapshot to fail
        self._snap_poll_2_1(snap['path'].strip("/"))
        return snap['data']

    def _restore_snapshot(self, bname, timestamp):
        url = datc.URL_TEMPLATES['ai_inst']().format(bname)
        self._detach_volume(bname)
        self._issue_api_request(datc.URL_TEMPLATES['vol_inst'](
            SI_NAME, VOL_NAME).format(bname), 'put',
            body={'restore_point': timestamp}, api_version='2.1',
            tenant=self.tenant_id)
        data = {
            'admin_state': 'online'
        }
        self._issue_api_request(
            url, method='put', body=data, api_version='2.1',
            tenant=self.tenant_id)
        # Trying a sleep here to give the snapshot a moment to restore
        LOG.debug("Sleeping for 5s to give the snapshot a chance")
        eventlet.sleep(5)

    def _list_snapshots(self, bname):
        snaps = self._issue_api_request(datc.URL_TEMPLATES['vol_inst'](
            SI_NAME, VOL_NAME).format(bname) + '/snapshots', 'get',
            api_version='2.1', tenant=self.tenant_id)
        return snaps['data']

    def _get_snapshot(self, bname, timestamp):
        return self._issue_api_request(datc.URL_TEMPLATES['vol_inst'](
            SI_NAME, VOL_NAME).format(
                bname) + '/snapshots/{}'.format(timestamp), 'get',
            api_version='2.1', tenant=self.tenant_id)

    def _delete_snapshot(self, bname, timestamp):
        for snapshot in self._list_snapshots(bname):
            if snapshot['utc_ts'] == timestamp:
                self._issue_api_request(datc.URL_TEMPLATES['vol_inst'](
                    SI_NAME, VOL_NAME).format(bname) + '/snapshots/{'
                    '}'.format(timestamp), 'delete', api_version='2.1')
                return
        LOG.debug('Did not find snapshot {} to delete'.format(timestamp))

    def _get_sis_iqn_portal(self, bname):
        iqn = None
        portal = None
        url = datc.URL_TEMPLATES['ai_inst']().format(bname)
        data = {
            'admin_state': 'online'
        }
        app_inst = self._issue_api_request(
            url, method='put', body=data, api_version='2.1',
            tenant=self.tenant_id)['data']
        storage_instances = app_inst["storage_instances"]
        si = storage_instances[0]
        portal = si['access']['ips'][0] + ':3260'
        iqn = si['access']['iqn']
        return storage_instances, iqn, portal

    def _register_acl(self, bname, initiator, storage_instances):
        initiator_name = "OpenStack_{}_{}".format(
            self.driver_prefix, str(uuid.uuid4())[:4])
        found = False
        if not found:
            data = {'id': initiator, 'name': initiator_name}
            # Try and create the initiator
            # If we get a conflict, ignore it
            self._issue_api_request("initiators",
                                    method="post",
                                    body=data,
                                    conflict_ok=True,
                                    api_version='2.1',
                                    tenant=self.tenant_id)
        initiator_path = "/initiators/{}".format(initiator)
        # Create ACL with initiator for storage_instances
        for si in storage_instances:
            acl_url = (datc.URL_TEMPLATES['si']() +
                       "/{}/acl_policy").format(bname, si['name'])
            existing_acl = self._issue_api_request(acl_url,
                                                   method="get",
                                                   api_version='2.1',
                                                   tenant=self.tenant_id)[
                'data']
            data = {}
            data['initiators'] = existing_acl['initiators']
            data['initiators'].append({"path": initiator_path})
            data['initiator_groups'] = existing_acl['initiator_groups']
            self._issue_api_request(acl_url,
                                    method="put",
                                    body=data,
                                    api_version='2.1',
                                    tenant=self.tenant_id)
        self._si_poll(bname)

    def _si_poll(self, bname):
        TIMEOUT = 10
        retry = 0
        check_url = datc.URL_TEMPLATES['si_inst'](SI_NAME).format(bname)
        poll = True
        while poll and not retry >= TIMEOUT:
            retry += 1
            si = self._issue_api_request(check_url,
                                         api_version='2.1',
                                         tenant=self.tenant_id)['data']
            if si['op_state'] == 'available':
                poll = False
            else:
                eventlet.sleep(1)
        if retry >= TIMEOUT:
            raise exception.VolumeDriverException(
                message=_('Resource not ready.'))

    def _snap_poll_2_1(self, url):
        tenant = self.tenant_id
        eventlet.sleep(datc.DEFAULT_SNAP_SLEEP)
        TIMEOUT = 20
        retry = 0
        poll = True
        while poll and not retry >= TIMEOUT:
            retry += 1
            snap = self._issue_api_request(url,
                                           api_version='2.1',
                                           tenant=tenant)['data']
            if snap['op_state'] == 'available':
                poll = False
            else:
                eventlet.sleep(1)
        if retry >= TIMEOUT:
            raise exception.VolumeDriverException(
                message=_('Snapshot not ready.'))

    @contextlib.contextmanager
    def _connect_target(self, container):
        connector = None
        try:
            sis, iqn, portal = self._get_sis_iqn_portal(container)
            conn = {'driver_volume_type': 'iscsi',
                    'data': {
                        'target_discovered': False,
                        'target_iqn': iqn,
                        'target_portal': portal,
                        'target_lun': 0,
                        'volume_id': None,
                        'discard': False}}
            connector = utils.brick_get_connector(
                conn['driver_volume_type'],
                use_multipath=False,
                device_scan_attempts=10,
                conn=conn)

            # Setup ACL
            initiator = connector.get_initiator()
            self._register_acl(container, initiator, sis)

            # Attach Target
            attach_info = {}
            attach_info['target_portal'] = portal
            attach_info['target_iqn'] = iqn
            attach_info['target_lun'] = 0
            retries = 10
            while True:
                try:
                    attach_info.update(
                        connector.connect_volume(conn['data']))
                    break
                except brick_exception.FailedISCSITargetPortalLogin:
                    retries -= 1
                    if not retries:
                        LOG.error("Could not log into portal before end of "
                                  "polling period")
                        raise
                    LOG.debug("Failed to login to portal, retrying")
                    eventlet.sleep(2)
            device_path = attach_info['path']
            yield device_path
        finally:
            # Close target connection
            if connector:
                # Best effort disconnection
                try:
                    connector.disconnect_volume(attach_info, attach_info)
                except Exception:
                    pass

    def _parse_name(self, name):
        return int(name.split("-")[-1])

    def _get_driver(self):
        if not self.driver:
            supported_list = []
            for dstr in self.driver_strs:
                supported_list.append(dstr.split(".")[-1])
            name = (self._backup['display_name'].lower()
                    if self._backup['display_name'] else None)
            if not name or 'datera' in name:
                self.type = 'datera'
                return
            for supported in supported_list:
                if supported in name:
                    self.type = supported
                    self.driver = self.drivers[self.type].get_backup_driver(
                        self.ctxt)
            if not self.driver:
                raise EnvironmentError(
                    "Unsupported driver: {}, display name of backup must "
                    "contain name of driver to use.  Supported drivers: {}"
                    "".format(name, self.drivers.keys()))
        return self.driver

    def put_container(self, bucket):
        """Create the bucket if not exists."""
        driver = self._get_driver()
        if not driver:
            if self._volume_exists(bucket):
                return
            else:
                vol_size = CONF.backup_datera_chunk_size
                self._create_volume(bucket, vol_size)
                return
        return driver.put_container(bucket)

    def get_container_entries(self, bucket, prefix):
        """Get bucket entry names."""
        driver = self._get_driver()
        if not driver:
            return ["-".join((prefix, "{:05d}".format(i + 1)))
                    for i, _ in enumerate(self._list_snapshots(bucket))][:-2]
        return driver.get_container_entries(bucket, prefix)

    def get_object_writer(self, bucket, object_name, extra_metadata=None):
        """Return a writer object.

        Returns a writer object that stores a chunk of volume data in a
        Datera volume
        """
        driver = self._get_driver()
        if not driver:
            return DateraObjectWriter(bucket, object_name, self)
        return driver.get_object_reader(bucket, object_name, extra_metadata)

    def get_object_reader(self, bucket, object_name, extra_metadata=None):
        """Return reader object.

        Returns a reader object that retrieves a chunk of backed-up volume data
        from a Datera EDF object store.
        """
        driver = self._get_driver()
        if not driver:
            return DateraObjectReader(bucket, object_name, self)
        return driver.get_object_reader(bucket, object_name, extra_metadata)

    def delete_object(self, bucket, object_name):
        """Deletes a backup object from a Datera EDF object store."""
        driver = self._get_driver()
        if not driver:
            return self._delete_snapshot(bucket, object_name)
        return driver.delete_object(bucket, object_name)

    def backup(self, backup, volume_file, backup_metadata=False):
        self._backup = backup
        driver = self._get_driver()
        if not driver:
            # We should always backup metadata in the Datera driver
            # It costs practically nothing and Tempest expects metadata to
            # be backed up.
            return super(DateraBackupDriver, self).backup(
                backup, volume_file, backup_metadata=True)
        return driver.backup(backup, volume_file, backup_metadata)

    def restore(self, backup, volume_id, volume_file):
        self._backup = backup
        driver = self._get_driver()
        if not driver:
            return super(DateraBackupDriver, self).restore(
                backup, volume_id, volume_file)
        return driver.restore(backup, volume_id, volume_file)

    # def get_metadata(self, volume_id):
    #     driver = self._get_driver()
    #     if not driver:
    #         return super(DateraBackupDriver, self).get_metadata(volume_id)
    #     return driver.get_metadata(volume_id)

    # def put_metadata(self, volume_id, json_metadata):
    #     driver = self._get_driver()
    #     if not driver:
    #         return super(DateraBackupDriver, self).put_metadata(
    #             volume_id, json_metadata)
    #     return driver.put_metadata(volume_id, json_metadata)

    def delete(self, backup):
        self._backup = backup
        driver = self._get_driver()
        if not driver:
            container = backup['container']
            object_prefix = backup['service_metadata']
            LOG.debug('delete started, backup: %(id)s, container: %(cont)s, '
                      'prefix: %(pre)s.',
                      {'id': backup['id'],
                       'cont': container,
                       'pre': object_prefix})
            if container is not None:
                self._delete_volume(container)
            LOG.debug('delete %s finished.', backup['id'])
            return
        return driver.delete(backup)

    def export_record(self, backup):
        """Export driver specific backup record information.

        If backup backend needs additional driver specific information to
        import backup record back into the system it must overwrite this method
        and return it here as a dictionary so it can be serialized into a
        string.

        Default backup driver implementation has no extra information.

        :param backup: backup object to export
        :returns: driver_info - dictionary with extra information
        """
        self._backup = backup
        driver = self._get_driver()
        if not driver:
            return super(DateraBackupDriver, self).export_record(backup)
        return driver.export_record(backup)

    def import_record(self, backup, driver_info):
        """Import driver specific backup record information.

        If backup backend needs additional driver specific information to
        import backup record back into the system it must overwrite this method
        since it will be called with the extra information that was provided by
        export_record when exporting the backup.

        Default backup driver implementation does nothing since it didn't
        export any specific data in export_record.

        :param backup: backup object to export
        :param driver_info: dictionary with driver specific backup record
                            information
        :returns: nothing
        """
        self._backup = backup
        driver = self._get_driver()
        if not driver:
            return super(DateraBackupDriver, self).import_record(
                backup, driver_info)
        return driver.import_record(backup, driver_info)

    def _generate_object_name_prefix(self, backup):
        """Generates a Datera EDF backup object name prefix."""
        driver = self._get_driver()
        if not driver:
            return PREFIX
        return driver._generate_object_name_prefix(self, backup)

    def update_container_name(self, backup, bucket):
        """Use the bucket name as provided - don't update."""
        driver = self._get_driver()
        if not driver:
            if not backup['container']:
                return "-".join(("BACKUP", str(self._backup['id'])))
            else:
                return
        return driver.update_container_name(self, backup, bucket)

    def get_extra_metadata(self, backup, volume):
        """Datera EDF driver does not use any extra metadata."""
        driver = self._get_driver()
        if not driver:
            return
        return driver.get_extra_metadata(backup, volume)

class DateraObjectWriter(object):
    def __init__(self, container, object_name, driver):
        LOG.debug("Object writer. container: %(container)s, "
                  "object_name: %(object)s",
                  {'container': container,
                   'object': object_name})
        self.container = container
        self.object_name = object_name
        self.driver = driver
        self.data = None
        self.write_metadata = True if object_name.endswith(METADATA) else False
        self.write_sha256 = True if object_name.endswith(SHA256) else False

        if self.write_metadata and self.write_sha256:
            raise ValueError("We're misunderstanding the requirements...")

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.close()

    def write(self, data):
        # Assuming a single write
        self.data = data

    def close(self):
        LOG.debug("Writing backup.Container: %(container)s, "
                  "object_name: %(object)s",
                  {'container': self.container,
                   'object': self.object_name})
        with self.driver._connect_target(self.container) as device_path:
            # Write backup data
            self.driver._execute("chmod o+w {}".format(device_path))
            f = os.open(device_path, os.O_SYNC | os.O_WRONLY)
            # Write number, length and MD5 to initial offset
            if self.write_sha256:
                n = -2
            elif self.write_metadata:
                n = -1
            else:
                n = self.driver._parse_name(self.object_name)
            l = len(self.data)
            h = hashlib.md5(self.data).hexdigest()
            os.write(f, struct.pack(PACK, n, l, h))
            LOG.debug("Writing Headers.\n Number: %(number)s\n"
                      "Length: %(length)s\n"
                      "MD5: %(md5)s",
                      {'number': n,
                       'length': len(self.data),
                       'md5': h})
            # Write actual data
            # os.lseek(f, TOTAL_OFFSET, 0)
            os.write(f, self.data)
            # If we're writing a really small amount of data (< 1 KiB), then
            # we should write additional data to ensure the block device
            # recognizes that we wrote data.  We'll just write 5 KiB of random
            # data after the data we care about so as to not impact performance
            if l <= 1 * units.Ki:
                LOG.debug("Writing additional data to ensure write takes")
                # Pad 8 bytes for visual debugging
                os.write(f, "\x00" * 8)
                # Random data
                os.write(f, os.urandom(5 * units.Ki))
            os.close(f)
            # for short writes we need to let the cache flush
            subprocess.check_call("sync")
            # Then sleep so the flush occurs
            eventlet.sleep(3)
            self.driver._execute("chmod o-w {}".format(device_path))
            self.driver._create_snapshot(self.container)

class DateraObjectReader(object):
    def __init__(self, container, object_name, driver):
        LOG.debug("Object reader. Container: %(container)s, "
                  "object_name: %(object)s",
                  {'container': container,
                   'object': object_name})
        self.container = container
        self.object_name = object_name
        self.driver = driver
        self.read_metadata = True if object_name.endswith(METADATA) else False
        self.read_sha256 = True if object_name.endswith(SHA256) else False

        if self.read_metadata and self.read_sha256:
            raise ValueError("We're misunderstanding the requirements...")

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        return

    def read(self):
        LOG.debug("Reading backup. Container: %(container)s, "
                  "object_name: %(object)s",
                  {'container': self.container,
                   'object': self.object_name})
        data = self.driver._list_snapshots(self.container)
        if self.read_sha256:
            snap = data[-2]["utc_ts"]
        elif self.read_metadata:
            snap = data[-1]["utc_ts"]
        else:
            # Backups start at 00001, convert to zero index
            snap = data[self.driver._parse_name(self.object_name) - 1][
                "utc_ts"]
        LOG.debug("Restoring Snapshot: {}".format(snap))
        self.driver._restore_snapshot(self.container, snap)
        # self.driver._delete_snapshot(self.container, most_recent)
        with self.driver._connect_target(self.container) as device_path:
            # Read backup data
            self.driver._execute("chmod o+r {}".format(device_path))
            f = os.open(device_path, os.O_RDONLY)
            # Read headers
            rawh = os.read(f, TOTAL_OFFSET)
            n, l, h = struct.unpack(PACK, rawh)
            LOG.debug("Reading Headers.\n Number: %(number)s\n"
                      "Length: %(length)s\n"
                      "MD5: %(md5)s",
                      {'number': n,
                       'length': l,
                       'md5': h})
            # Read data
            data = os.read(f, l)
            os.close(f)
            # Compare hashes
            newh = hashlib.md5(data).hexdigest()
            if newh != h:
                raise ValueError("Data hash read off backup doesn't match "
                                 "calculated hash. new hash: %(new)s "
                                 "read hash: %(read)s",
                                 {'new': newh,
                                  'read': h})
            self.driver._execute("chmod o-r {}".format(device_path))
            return data

def get_backup_driver(context, db_driver=None):
    return DateraBackupDriver(context, db_driver=db_driver)

redpajama_set_name: RedPajamaGithub


text:
'''
Support
=======

Activate other frameworks/toolkits inside the kivy event loop.

'''

__all__ = ('install_gobject_iteration', 'install_twisted_reactor',
           'uninstall_twisted_reactor', 'install_android')

def install_gobject_iteration():
    '''Import and install gobject context iteration inside our event loop.
    This is used as soon as gobject is used (like gstreamer).
    '''

    from kivy.clock import Clock

    try:
        from gi.repository import GObject as gobject
    except ImportError:
        import gobject

    if hasattr(gobject, '_gobject_already_installed'):
        # already installed, don't do it twice.
        return

    gobject._gobject_already_installed = True

    # get gobject mainloop / context
    loop = gobject.MainLoop()
    gobject.threads_init()
    context = loop.get_context()

    # schedule the iteration each frame
    def _gobject_iteration(*largs):
        # XXX we need to loop over context here, otherwise, we might have a lag
        loop = 0
        while context.pending() and loop < 10:
            context.iteration(False)
            loop += 1
    Clock.schedule_interval(_gobject_iteration, 0)

# -----------------------------------------------------------------------------
# Android support
# -----------------------------------------------------------------------------

g_android_redraw_count = 0

def _android_ask_redraw(*largs):
    # after wakeup, we need to redraw more than once, otherwise we get a
    # black screen
    global g_android_redraw_count
    from kivy.core.window import Window
    Window.canvas.ask_update()
    g_android_redraw_count -= 1
    if g_android_redraw_count < 0:
        return False

def install_android():
    '''Install hooks for the android platform.

    * Automatically sleep when the device is paused.
    * Automatically kill the application when the return key is pressed.
    '''
    try:
        import android
    except ImportError:
        print('Android lib is missing, cannot install android hooks')
        return

    from kivy.clock import Clock
    from kivy.logger import Logger
    import pygame

    Logger.info('Support: Android install hooks')

    # Init the library
    android.init()
    android.map_key(android.KEYCODE_MENU, pygame.K_MENU)
    android.map_key(android.KEYCODE_BACK, pygame.K_ESCAPE)

    # Check if android should be paused or not.
    # If pause is requested, just leave the app.
    def android_check_pause(*largs):
        # do nothing until android asks for it.
        if not android.check_pause():
            return

        from kivy.app import App
        from kivy.base import stopTouchApp
        from kivy.logger import Logger
        from kivy.core.window import Window
        global g_android_redraw_count

        # try to get the current running application
        Logger.info('Android: Must go into sleep mode, check the app')
        app = App.get_running_app()

        # no running application, stop our loop.
        if app is None:
            Logger.info('Android: No app running, stop everything.')
            stopTouchApp()
            return

        # try to go to pause mode
        if app.dispatch('on_pause'):
            Logger.info('Android: App paused, now wait for resume.')

            # app goes in pause mode, wait.
            android.wait_for_resume()

            # is it a stop or resume ?
            if android.check_stop():
                # app must stop
                Logger.info('Android: Android wants to close our app.')
                stopTouchApp()
            else:
                # app resuming now !
                Logger.info('Android: Android has resumed, resume the app.')
                app.dispatch('on_resume')
                Window.canvas.ask_update()
                g_android_redraw_count = 25  # 5 frames/seconds for 5 seconds
                Clock.unschedule(_android_ask_redraw)
                Clock.schedule_interval(_android_ask_redraw, 1 / 5)
                Logger.info('Android: App resume completed.')

        # app doesn't support pause mode, just stop it.
        else:
            Logger.info('Android: App doesn\'t support pause mode, stop.')
            stopTouchApp()

    Clock.schedule_interval(android_check_pause, 0)

_twisted_reactor_stopper = None
_twisted_reactor_work = None

def install_twisted_reactor(**kwargs):
    '''Installs a threaded twisted reactor, which will schedule one
    reactor iteration before the next frame only when twisted needs
    to do some work.

    Any arguments or keyword arguments passed to this function will be
    passed on the the threadedselect reactors interleave function. These
    are the arguments one would usually pass to twisted's reactor.startRunning.

    Unlike the default twisted reactor, the installed reactor will not handle
    any signals unless you set the 'installSignalHandlers' keyword argument
    to 1 explicitly. This is done to allow kivy to handle the signals as
    usual unless you specifically want the twisted reactor to handle the
    signals (e.g. SIGINT).

    .. note::
        Twisted is not included in iOS build by default. To use it on iOS,
        put the twisted distribution (and zope.interface dependency) in your
        application directory.
    '''
    import twisted

    # prevent installing more than once
    if hasattr(twisted, '_kivy_twisted_reactor_installed'):
        return
    twisted._kivy_twisted_reactor_installed = True

    # don't let twisted handle signals, unless specifically requested
    kwargs.setdefault('installSignalHandlers', 0)

    # install threaded-select reactor, to use with own event loop
    from twisted.internet import _threadedselect
    _threadedselect.install()

    # now we can import twisted reactor as usual
    from twisted.internet import reactor
    from twisted.internet.error import ReactorNotRunning

    from collections import deque
    from kivy.base import EventLoop
    from kivy.logger import Logger
    from kivy.clock import Clock

    # will hold callbacks to twisted callbacks
    q = deque()

    # twisted will call the wake function when it needs to do work
    def reactor_wake(twisted_loop_next):
        '''Wakeup the twisted reactor to start processing the task queue
        '''

        Logger.trace("Support: twisted wakeup call to schedule task")
        q.append(twisted_loop_next)

    # called every frame, to process the reactors work in main thread
    def reactor_work(*args):
        '''Process the twisted reactor task queue
        '''
        Logger.trace("Support: processing twisted task queue")
        while len(q):
            q.popleft()()
    global _twisted_reactor_work
    _twisted_reactor_work = reactor_work

    # start the reactor, by telling twisted how to wake, and process
    def reactor_start(*args):
        '''Start the twisted reactor main loop
        '''
        Logger.info("Support: Starting twisted reactor")
        reactor.interleave(reactor_wake, **kwargs)
        Clock.schedule_interval(reactor_work, 0)

    # make sure twisted reactor is shutdown if eventloop exists
    def reactor_stop(*args):
        '''Shutdown the twisted reactor main loop
        '''
        if reactor.threadpool:
            Logger.info("Support: Stopping twisted threads")
            reactor.threadpool.stop()
        Logger.info("Support: Shutting down twisted reactor")
        reactor._mainLoopShutdown()
        try:
            reactor.stop()
        except ReactorNotRunning:
            pass

        import sys
        sys.modules.pop('twisted.internet.reactor', None)

    global _twisted_reactor_stopper
    _twisted_reactor_stopper = reactor_stop

    # start and stop the reactor along with kivy EventLoop
    Clock.schedule_once(reactor_start, 0)
    EventLoop.bind(on_stop=reactor_stop)

def uninstall_twisted_reactor():
    '''Uninstalls the Kivy's threaded Twisted Reactor. No more Twisted
    tasks will run after this got called. Use this to clean the
    `twisted.internet.reactor` .

    .. versionadded:: 1.9.0
    '''

    import twisted

    # prevent uninstalling more than once
    if not hasattr(twisted, '_kivy_twisted_reactor_installed'):
        return

    from kivy.base import EventLoop

    global _twisted_reactor_stopper
    _twisted_reactor_stopper()
    EventLoop.unbind(on_stop=_twisted_reactor_stopper)

    del twisted._kivy_twisted_reactor_installed


text:
"""Test suite for the HotResque library. To run this test suite, execute this
Python program (``python tests.py``). Redis must be running on localhost:6379,
and a list key named 'HotResque:testqueue' will be created and deleted in db 0
several times while the tests are running.
"""

from time import sleep
import threading
import unittest

from HotResque import HotResque

class HotResqueTestCase(unittest.TestCase):

    def setUp(self):
        """Create the queue instance before the test."""
        self.queue = HotResque('testqueue')

    def tearDown(self):
        """Clear the queue after the test."""
        self.queue.clear()

    def test_consume(self):
        """Test the consume generator method."""
        nums = [1, 2, 3, 4, 5, 6, 7, 8]
        # Test blocking with timeout:
        self.queue.put(*nums)
        msgs = []
        for msg in self.queue.consume(timeout=1):
            msgs.append(msg)
        self.assertEquals(msgs, nums)
        # Test non-blocking:
        self.queue.put(*nums)
        msgs = []
        for msg in self.queue.consume(block=False):
            msgs.append(msg)
        self.assertEquals(msgs, nums)

    def test_cleared(self):
        """Test for correct behaviour if the Redis list does not exist."""
        self.assertEquals(len(self.queue), 0)
        self.assertEquals(self.queue.get(), None)

    def test_get_order(self):
        """Test that messages are get in the same order they are put."""
        alphabet = ['abc', 'def', 'ghi', 'jkl', 'mno']
        self.queue.put(alphabet[0], alphabet[1], alphabet[2])
        self.queue.put(alphabet[3])
        self.queue.put(alphabet[4])
        msgs = []
        msgs.append(self.queue.get())
        msgs.append(self.queue.get())
        msgs.append(self.queue.get())
        msgs.append(self.queue.get())
        msgs.append(self.queue.get())
        self.assertEquals(msgs, alphabet)

    def test_length(self):
        """Test that the length of a queue is returned correctly."""
        self.queue.put('a message')
        self.queue.put('another message')
        self.assertEquals(len(self.queue), 2)

    def test_worker(self):
        """Test the worker decorator."""
        colors = ['blue', 'green', 'red', 'pink', 'black']
        # Test blocking with timeout:
        self.queue.put(*colors)
        msgs = []
        @self.queue.worker(timeout=1)
        def appender(msg):
            msgs.append(msg)
        appender()
        self.assertEqual(msgs, colors)
        # Test non-blocking:
        self.queue.put(*colors)
        msgs = []
        @self.queue.worker(block=False)
        def appender(msg):
            msgs.append(msg)
        appender()
        self.assertEqual(msgs, colors)

    def test_threaded(self):
        """Threaded test of put and consume methods."""
        msgs = []
        def put():
            for num in range(3):
                self.queue.put('message %d' % num)
                sleep(0.1)
        def consume():
            for msg in self.queue.consume(timeout=1):
                msgs.append(msg)
        putter = threading.Thread(target=put)
        consumer = threading.Thread(target=consume)
        putter.start()
        consumer.start()
        for thread in [putter, consumer]:
            thread.join()
        self.assertEqual(msgs, ['message 0', 'message 1', 'message 2'])

if __name__ == "__main__":
    unittest.main()


text:
package io.cdap.mmds.proto;

/**
 * Indicates an exception that can be handled in an Http endpoint in a standard fashion.
 */
public abstract class EndpointException extends RuntimeException {

  public EndpointException(String message) {
    super(message);
  }

  public EndpointException(String message, Throwable cause) {
    super(message, cause);
  }

  public abstract int getCode();
}


text:
package org.gradle.api.internal.artifacts.ivyservice.resolveengine.result;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableList;
import org.gradle.api.artifacts.result.ComponentSelectionCause;
import org.gradle.api.artifacts.result.ComponentSelectionDescriptor;
import org.gradle.api.artifacts.result.ComponentSelectionReason;
import org.gradle.internal.Describables;
import org.gradle.internal.serialize.Decoder;
import org.gradle.internal.serialize.Encoder;
import org.gradle.internal.serialize.Serializer;

import javax.annotation.concurrent.NotThreadSafe;
import java.io.IOException;
import java.util.List;

@NotThreadSafe
public class ComponentSelectionReasonSerializer implements Serializer<ComponentSelectionReason> {

    private final BiMap<String, Integer> descriptions = HashBiMap.create();

    public ComponentSelectionReason read(Decoder decoder) throws IOException {
        List<ComponentSelectionDescriptor> descriptions = readDescriptions(decoder);
        return VersionSelectionReasons.of(descriptions);
    }

    private List<ComponentSelectionDescriptor> readDescriptions(Decoder decoder) throws IOException {
        int size = decoder.readSmallInt();
        ImmutableList.Builder<ComponentSelectionDescriptor> builder = new ImmutableList.Builder<ComponentSelectionDescriptor>();
        for (int i = 0; i < size; i++) {
            ComponentSelectionCause cause = ComponentSelectionCause.values()[decoder.readByte()];
            String desc = readDescriptionText(decoder);
            String defaultReason = cause.getDefaultReason();
            if (desc.equals(defaultReason)) {
                builder.add(new DefaultComponentSelectionDescriptor(cause));
            } else {
                builder.add(new DefaultComponentSelectionDescriptor(cause, Describables.of(desc)));
            }

        }
        return builder.build();
    }

    private String readDescriptionText(Decoder decoder) throws IOException {
        boolean alreadyKnown = decoder.readBoolean();
        if (alreadyKnown) {
            return descriptions.inverse().get(decoder.readSmallInt());
        } else {
            String description = decoder.readString();
            descriptions.put(description, descriptions.size());
            return description;
        }
    }

    public void write(Encoder encoder, ComponentSelectionReason value) throws IOException {
        List<ComponentSelectionDescriptor> descriptions = value.getDescriptions();
        encoder.writeSmallInt(descriptions.size());
        for (ComponentSelectionDescriptor description : descriptions) {
            writeDescription(encoder, description);
        }
    }

    private void writeDescription(Encoder encoder, ComponentSelectionDescriptor description) throws IOException {
        encoder.writeByte((byte) description.getCause().ordinal());
        writeDescriptionText(encoder, description.getDescription());
    }

    private void writeDescriptionText(Encoder encoder, String description) throws IOException {
        Integer index = descriptions.get(description);
        encoder.writeBoolean(index != null); // already known custom reason
        if (index == null) {
            index = descriptions.size();
            descriptions.put(description, index);
            encoder.writeString(description);
        } else {
            encoder.writeSmallInt(index);
        }
    }

    public void reset() {
        descriptions.clear();
    }
}


text:
<!DOCTYPE html>

  <meta charset="utf-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>pymatgen.io.cif module — pymatgen 2020.7.3 documentation</title>

    <link rel="canonical" href="https://pymatgen.orgpymatgen.io.cif.html"/>

  <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="_static/css/custom.css" type="text/css" />
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="pymatgen.io.cssr module" href="pymatgen.io.cssr.html" />
    <link rel="prev" title="pymatgen.io.babel module" href="pymatgen.io.babel.html" />

             pymatgen

                2020.7.3

  <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>

Introduction

Installation

Change log

Usage

Development Team

References

API Docs

pymatgen package

Subpackages

pymatgen.alchemy package

pymatgen.analysis package

pymatgen.apps package

pymatgen.cli package

pymatgen.command_line package

pymatgen.core package

pymatgen.electronic_structure package

pymatgen.entries package

pymatgen.ext package

pymatgen.io package

pymatgen.optimization package

pymatgen.phonon package

pymatgen.plugins package

pymatgen.symmetry package

pymatgen.transformations package

pymatgen.util package

pymatgen.vis package

Submodules

Module contents

          pymatgen

Docs »

pymatgen »

pymatgen package »

pymatgen.io package »

pymatgen.io.cif module

               Edit on GitHub

pymatgen.io.cif module¶

Wrapper classes for Cif input and output from Structures.

class CifBlock(data, loops, header)[source]¶

Bases: object

Object for storing cif data. All data is stored in a single dictionary.
Data inside loops are stored in lists in the data dictionary, and
information on which keys are grouped together are stored in the loops
attribute.

Parameters

data – dict or OrderedDict of data to go into the cif. Values should
be convertible to string, or lists of these if the key is
in a loop

loops – list of lists of keys, grouped by which loop they should
appear in

header – name of the block (appears after the data_ on the first
line)

classmethod from_string(string)[source]¶

Reads CifBlock from string.

Parameters

string – String representation.

Returns

CifBlock

maxlen = 70[source]¶

class CifFile(data, orig_string=None, comment=None)[source]¶

Bases: object

Reads and parses CifBlocks from a .cif file or string

Parameters

data (OrderedDict) – Of CifBlock objects.å

orig_string (str) – The original cif string.

comment (str) – Comment string.

classmethod from_file(filename)[source]¶

Reads CifFile from a filename.

Parameters

filename – Filename

Returns

CifFile

classmethod from_string(string)[source]¶

Reads CifFile from a string.

Parameters

string – String representation.

Returns

CifFile

class CifParser(filename, occupancy_tolerance=1.0, site_tolerance=0.0001)[source]¶

Bases: object

Parses a CIF file. Attempts to fix CIFs that are out-of-spec, but will
issue warnings if corrections applied. These are also stored in the
CifParser's errors attribute.

Parameters

filename (str) – CIF filename, bzipped or gzipped CIF files are fine too.

occupancy_tolerance (float) – If total occupancy of a site is between 1
and occupancy_tolerance, the occupancies will be scaled down to 1.

site_tolerance (float) – This tolerance is used to determine if two
sites are sitting in the same position, in which case they will be
combined to a single disordered site. Defaults to 1e-4.

as_dict()[source]¶

Returns

MSONable dict

static from_string(cif_string, occupancy_tolerance=1.0)[source]¶

Creates a CifParser from a string.

Parameters

cif_string (str) – String representation of a CIF.

occupancy_tolerance (float) – If total occupancy of a site is
between 1 and occupancy_tolerance, the occupancies will be
scaled down to 1.

Returns

CifParser

get_bibtex_string()[source]¶

Get BibTeX reference from CIF file.
:param data:
:return: BibTeX string

get_lattice(data, length_strings='a', 'b', 'c', angle_strings='alpha', 'beta', 'gamma', lattice_type=None)[source]¶

Generate the lattice from the provided lattice parameters. In
the absence of all six lattice parameters, the crystal system
and necessary parameters are parsed

get_magsymops(data)[source]¶

Equivalent to get_symops except for magnetic symmetry groups.
Separate function since additional operation for time reversal symmetry
(which changes magnetic moments on sites) needs to be returned.

get_structures(primitive=True)[source]¶

Return list of structures in CIF file. primitive boolean sets whether a
conventional cell structure or primitive cell structure is returned.

Parameters

primitive (bool) – Set to False to return conventional unit cells.
Defaults to True. With magnetic CIF files, will return primitive
magnetic cell which may be larger than nuclear primitive cell.

Returns

List of Structures.

get_symops(data)[source]¶

In order to generate symmetry equivalent positions, the symmetry
operations are parsed. If the symops are not present, the space
group symbol is parsed, and symops are generated.

property has_errors[source]¶

Whether there are errors/warnings detected in CIF parsing.

Type

return

parse_magmoms(data, lattice=None)[source]¶

Parse atomic magnetic moments from data dictionary

parse_oxi_states(data)[source]¶

Parse oxidation states from data dictionary

class CifWriter(struct, symprec=None, write_magmoms=False, significant_figures=8, angle_tolerance=5.0)[source]¶

Bases: object

A wrapper around CifFile to write CIF files from pymatgen structures.

Parameters

struct (Structure) – structure to write

symprec (float) – If not none, finds the symmetry of the structure
and writes the cif with symmetry information. Passes symprec
to the SpacegroupAnalyzer.

write_magmoms (bool) – If True, will write magCIF file. Incompatible
with symprec

significant_figures (int) – Specifies precision for formatting of floats.
Defaults to 8.

angle_tolerance (float) – Angle tolerance for symmetry finding. Passes
angle_tolerance to the SpacegroupAnalyzer. Used only if symprec
is not None.

property ciffile[source]¶

CifFile associated with the CifWriter.

Type

Returns

write_file(filename)[source]¶

Write the cif file.

str2float(text)[source]¶

Remove uncertainty brackets from strings and return the float.

        © Copyright 2011, Pymatgen Development Team

  Built with Sphinx using a theme provided by Read the Docs.

This page uses
Google Analytics to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.


text:
module Refinery
  module Pages
    include ActiveSupport::Configurable

    config_accessor :pages_per_dialog, :pages_per_admin_index, :new_page_parts,
                    :marketable_urls, :approximate_ascii, :strip_non_ascii,
                    :default_parts, :use_custom_slugs, :scope_slug_by_parent,
                    :cache_pages_backend, :cache_pages_full, :layout_template_whitelist,
                    :use_layout_templates, :page_title, :absolute_page_links, :types,
                    :auto_expand_admin_tree, :show_title_in_body

    self.pages_per_dialog = 14
    self.pages_per_admin_index = 20
    self.new_page_parts = false
    self.marketable_urls = true
    self.approximate_ascii = false
    self.strip_non_ascii = false
    self.default_parts = ["Body", "Side Body"]
    self.use_custom_slugs = false
    self.scope_slug_by_parent = true
    self.cache_pages_backend = false
    self.cache_pages_full = false
    self.layout_template_whitelist = ["application"]
    class << self
      def layout_template_whitelist
        Array(config.layout_template_whitelist).map(&:to_s)
      end
    end
    self.use_layout_templates = false
    self.page_title = {
      :chain_page_title => false,
      :ancestors => {
        :separator => " | ",
        :class => 'ancestors',
        :tag => 'span'
      },
      :page_title => {
        :class => nil,
        :tag => nil,
        :wrap_if_not_chained => false
      }
    }
    self.show_title_in_body = true
    self.absolute_page_links = false
    self.types = Types.registered
    self.auto_expand_admin_tree = true
  end
end


text:
package org.jboss.weld.junit5.auto;

import jakarta.enterprise.context.NormalScope;
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.inject.spi.Extension;
import jakarta.enterprise.inject.spi.ProcessAnnotatedType;
import jakarta.enterprise.inject.spi.WithAnnotations;
import jakarta.inject.Scope;
import java.lang.reflect.Type;
import java.util.Set;

/**
 * Extension class that ensures selected classes are excluded as
 * beans.
 */
public class ExcludedBeansExtension implements Extension {

    private final Set<Type> excludedBeanTypes;
    private final Set<Class<?>> excludedBeanClasses;

    ExcludedBeansExtension(Set<Type> excludedBeanTypes, Set<Class<?>> excludedBeanClasses) {
        this.excludedBeanTypes = excludedBeanTypes;
        this.excludedBeanClasses = excludedBeanClasses;
    }

    <T> void excludeBeans(@Observes @WithAnnotations({Scope.class, NormalScope.class}) ProcessAnnotatedType<T> pat) {

        if (excludedBeanClasses.contains(pat.getAnnotatedType().getJavaClass())) {
            pat.veto();
            return;
        }

        Set<Type> typeClosure = pat.getAnnotatedType().getTypeClosure();
        for (Type excludedBeanType : excludedBeanTypes) {
            if (typeClosure.contains(excludedBeanType)) {
                pat.veto();
                return;
            }
        }
    }

}