text:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.techempower</groupId>
        <artifactId>beyondj</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.techempower</groupId>
    <artifactId>beyondj-data-loader</artifactId>
    <name>BeyondJ Sample Project - Data Loader</name>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.techempower</groupId>
            <artifactId>beyondj-data</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.connector.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.entitymanager.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>${hibernate.validator.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons.io.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>

        <!-- Spring data jpa -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>${spring.data.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
            <version>${spring.data.commons.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>${commons.pool.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring-aop</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Start Spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring-aop</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons.lang.version}</version>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>${c3p0.version}</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>beyondj-data-loader</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
                <configuration>
                    <mainClass>com.techempower.beyondj.dataset.DataLoader</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <pluginRepositories>
        <pluginRepository>
            <id>beyondj.com-pluginRepository</id>
            <url>http://beyondj.com/maven2/</url>
        </pluginRepository>
    </pluginRepositories>
    <repositories>
        <repository>
            <id>beyondj.com</id>
            <url>http://beyondj.com/maven2/</url>
        </repository>
    </repositories>

</project>


text:
import semver from 'semver';
import { args, getVersion, run } from './util.js';

const { inc } = semver;

// default exec options
if (args.f || args.force || (await isDevCommit())) {
    // increase version patch number
    const version = inc(await getVersion(), 'patch');

    // get current git hash
    const hash = (await run('git rev-parse --short HEAD')).trim();

    // set version of package.json
    await run(`npm version ${version}-dev.${hash} --git-tag-version false`);

    // create dist files
    await run('yarn compile && yarn compile-rtl && yarn build-scss');

    // publish to dev tag
    await run('npm publish --tag dev');
}

async function isDevCommit() {
    // check for changes to publish (%B: raw body (unwrapped subject and body)
    const message = await run('git log -1 --pretty=%B');

    // https://www.conventionalcommits.org/en/v1.0.0/
    const type = message.match(
        /^(revert: )?(feat|fix|polish|docs|style|refactor|perf|test|workflow|ci|chore|types|build)(\(.+\))?: .{1,50}/
    );

    return type && ['feat', 'fix', 'refactor', 'perf'].includes(type[2]);
}

redpajama_set_name: RedPajamaGithub


text:
<?php

namespace NicklasW\PkmGoApi\Requests;

use Google\Protobuf\Internal\Message;
use POGOProtos\Networking\Envelopes\ResponseEnvelope;
use POGOProtos\Networking\Requests\Messages\RecycleInventoryItemMessage;
use POGOProtos\Networking\Requests\RequestType;
use POGOProtos\Networking\Responses\RecycleInventoryItemResponse;

class RecycleInventoryItemRequest extends Request
{

    /**
     * @var integer The request type
     */
    protected $type = RequestType::RECYCLE_INVENTORY_ITEM;

    /**
     * @var Message The request message
     */
    protected $message;

    /**
     * @var int
     */
    protected $itemId;

    /**
     * @var int
     */
    protected $itemCount;

    /**
     * RecycleInventoryItemRequest constructor.
     *
     * @param integer $itemId
     * @param integer $count
     */
    public function __construct($itemId, $count)
    {
        $this->itemId = $itemId;
        $this->itemCount = $count;
    }

    /**
     * @return int
     */
    public function getType()
    {
        return RequestType::RECYCLE_INVENTORY_ITEM;
    }

    /**
     * @return Message
     */
    public function getMessage()
    {
        $recycleInventoryItemMessage = new RecycleInventoryItemMessage();
        $recycleInventoryItemMessage->setItemId($this->itemId);
        $recycleInventoryItemMessage->setCount($this->itemCount);

        return $recycleInventoryItemMessage;
    }

    /**
     * Handles the request data.
     *
     * @param ResponseEnvelope $data
     * @return RecycleInventoryItemResponse
     */
    public function handleResponse($data)
    {
        // Retrieve the specific request data
        $requestData = $data->getReturns();

        // Initialize the rename pokemon response
        $recycleInventoryItemResponse = new RecycleInventoryItemResponse();

        // Unmarshall the response
        $recycleInventoryItemResponse->decode($requestData[0]);

        $this->setData($recycleInventoryItemResponse);
    }
}


text:
CKEDITOR.plugins.setLang( 'a11yhelp', 'af', {
	title: 'Toeganglikheid instruksies',
	contents: 'Hulp inhoud. Druk ESC om toe te maak.',
	legend: [
		{
		name: 'Algemeen',
		items: [
			{
			name: 'Bewerker balk',
			legend: 'Druk ${toolbarFocus} om op die werkbalk te land. Beweeg na die volgende en voorige wekrbalkgroep met TAB and SHIFT+TAB. Beweeg na die volgende en voorige werkbalkknop met die regter of linker pyl. Druk SPASIE of ENTER om die knop te bevestig.'
		},

			{
			name: 'Bewerker dialoog',
			legend:
				'Inside a dialog, press TAB to navigate to the next dialog element, press SHIFT+TAB to move to the previous dialog element, press ENTER to submit the dialog, press ESC to cancel the dialog. When a dialog has multiple tabs, the tab list can be reached either with ALT+F10 or with TAB as part of the dialog tabbing order. With tab list focused, move to the next and previous tab with RIGHT and LEFT ARROW, respectively.'  // MISSING
		},

			{
			name: 'Bewerkerinhoudmenu',
			legend: 'Press ${contextMenu} or APPLICATION KEY to open context-menu. Then move to next menu option with TAB or DOWN ARROW. Move to previous option with SHIFT+TAB or UP ARROW. Press SPACE or ENTER to select the menu option. Open sub-menu of current option with SPACE or ENTER or RIGHT ARROW. Go back to parent menu item with ESC or LEFT ARROW. Close context menu with ESC.' // MISSING
		},

			{
			name: 'Editor List Box', // MISSING
			legend: 'Inside a list-box, move to next list item with TAB OR DOWN ARROW. Move to previous list item with SHIFT+TAB or UP ARROW. Press SPACE or ENTER to select the list option. Press ESC to close the list-box.' // MISSING
		},

			{
			name: 'Editor Element Path Bar', // MISSING
			legend: 'Press ${elementsPathFocus} to navigate to the elements path bar. Move to next element button with TAB or RIGHT ARROW. Move to previous button with SHIFT+TAB or LEFT ARROW. Press SPACE or ENTER to select the element in editor.' // MISSING
		}
		]
	},
		{
		name: 'Commands', // MISSING
		items: [
			{
			name: ' Undo command', // MISSING
			legend: 'Press ${undo}' // MISSING
		},
			{
			name: ' Redo command', // MISSING
			legend: 'Press ${redo}' // MISSING
		},
			{
			name: ' Bold command', // MISSING
			legend: 'Press ${bold}' // MISSING
		},
			{
			name: ' Italic command', // MISSING
			legend: 'Press ${italic}' // MISSING
		},
			{
			name: ' Underline command', // MISSING
			legend: 'Press ${underline}' // MISSING
		},
			{
			name: ' Link command', // MISSING
			legend: 'Press ${link}' // MISSING
		},
			{
			name: ' Toolbar Collapse command', // MISSING
			legend: 'Press ${toolbarCollapse}' // MISSING
		},
			{
			name: ' Access previous focus space command', // MISSING
			legend: 'Press ${accessPreviousSpace} to access the closest unreachable focus space before the caret, for example: two adjacent HR elements. Repeat the key combination to reach distant focus spaces.' // MISSING
		},
			{
			name: ' Access next focus space command', // MISSING
			legend: 'Press ${accessNextSpace} to access the closest unreachable focus space after the caret, for example: two adjacent HR elements. Repeat the key combination to reach distant focus spaces.' // MISSING
		},
			{
			name: ' Accessibility Help', // MISSING
			legend: 'Press ${a11yHelp}' // MISSING
		}
		]
	}
	],
	backspace: 'Backspace', // MISSING
	tab: 'Tab', // MISSING
	enter: 'Enter', // MISSING
	shift: 'Shift', // MISSING
	ctrl: 'Ctrl',
	alt: 'Alt',
	pause: 'Pouse',
	capslock: 'Hoofletterslot',
	escape: 'Ontsnap',
	pageUp: 'Blaaiop',
	pageDown: 'Blaaiaf',
	end: 'Einde',
	home: 'Tuis',
	leftArrow: 'Linkspyl',
	upArrow: 'Oppyl',
	rightArrow: 'Regterpyl',
	downArrow: 'Afpyl',
	insert: 'Toevoeg',
	'delete': 'Verwyder',
	leftWindowKey: 'Left Windows key', // MISSING
	rightWindowKey: 'Right Windows key', // MISSING
	selectKey: 'Select key', // MISSING
	numpad0: 'Nommerblok 0',
	numpad1: 'Nommerblok 1',
	numpad2: 'Nommerblok 2',
	numpad3: 'Nommerblok 3',
	numpad4: 'Nommerblok 4',
	numpad5: 'Nommerblok 5',
	numpad6: 'Nommerblok 6',
	numpad7: 'Nommerblok 7',
	numpad8: 'Nommerblok 8',
	numpad9: 'Nommerblok 9',
	multiply: 'Maal',
	add: 'Plus',
	subtract: 'Minus',
	decimalPoint: 'Desimaalepunt',
	divide: 'Gedeeldeur',
	f1: 'F1',
	f2: 'F2',
	f3: 'F3',
	f4: 'F4',
	f5: 'F5',
	f6: 'F6',
	f7: 'F7',
	f8: 'F8',
	f9: 'F9',
	f10: 'F10',
	f11: 'F11',
	f12: 'F12',
	numLock: 'Nommervergrendel',
	scrollLock: 'Rolvergrendel',
	semiColon: 'Kommapunt',
	equalSign: 'Isgelykaan',
	comma: 'Komma',
	dash: 'Koppelteken',
	period: 'Punt',
	forwardSlash: 'Skuinsstreep',
	graveAccent: 'Aksentteken',
	openBracket: 'Oopblokhakkie',
	backSlash: 'Trustreep',
	closeBracket: 'Toeblokhakkie',
	singleQuote: 'Enkelaanhaalingsteken'
} );


text:
package com.cnblogs.yjmyzz.entity;

// Generated 2014-10-23 14:04:43 by Hibernate Tools 4.0.0
import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.*;

/**
 * TOrder generated by hbm2java
 */
@Entity(name = "T_ORDER")
public class TOrder implements java.io.Serializable {
	private static final long serialVersionUID = -779935606914199267L;
	private BigDecimal id;
	private String clientname;
	private BigDecimal amount;
	private Date createtime;

	public TOrder() {
	}

	public TOrder(BigDecimal id) {
		this.id = id;
	}

	public TOrder(BigDecimal id, String clientname, BigDecimal amount,
			Date createtime) {
		this.id = id;
		this.clientname = clientname;
		this.amount = amount;
		this.createtime = createtime;
	}

	@Id
//	@SequenceGenerator(name = "SEQ_ORDER", sequenceName = "SEQ_ORDER", allocationSize = 1)
//	@GeneratedValue(generator = "SEQ_ORDER", strategy = GenerationType.SEQUENCE)
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "ID", unique = true, nullable = false, precision = 22, scale = 0)
	public BigDecimal getId() {
		return this.id;
	}

	public void setId(BigDecimal id) {
		this.id = id;
	}

	@Column(name = "CLIENTNAME", length = 100)
	public String getClientname() {
		return this.clientname;
	}

	public void setClientname(String clientname) {
		this.clientname = clientname;
	}

	@Column(name = "AMOUNT", precision = 22, scale = 0)
	public BigDecimal getAmount() {
		return this.amount;
	}

	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "CREATETIME")
	public Date getCreatetime() {
		return this.createtime;
	}

	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}
}


text:
package br.com.caelum.vraptor.serialization;

import static br.com.caelum.vraptor.view.Results.json;
import static br.com.caelum.vraptor.view.Results.xml;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletResponse;

import org.junit.Before;
import org.junit.Test;

import com.google.gson.JsonDeserializer;
import com.google.gson.JsonSerializer;

import br.com.caelum.vraptor.core.DefaultReflectionProvider;
import br.com.caelum.vraptor.environment.Environment;
import br.com.caelum.vraptor.interceptor.DefaultTypeNameExtractor;
import br.com.caelum.vraptor.ioc.Container;
import br.com.caelum.vraptor.serialization.gson.CalendarGsonConverter;
import br.com.caelum.vraptor.serialization.gson.GsonBuilderWrapper;
import br.com.caelum.vraptor.serialization.gson.GsonJSONSerialization;
import br.com.caelum.vraptor.serialization.gson.GsonSerializerBuilder;
import br.com.caelum.vraptor.serialization.gson.MessageGsonConverter;
import br.com.caelum.vraptor.serialization.xstream.MessageConverter;
import br.com.caelum.vraptor.serialization.xstream.XStreamBuilder;
import br.com.caelum.vraptor.serialization.xstream.XStreamBuilderImpl;
import br.com.caelum.vraptor.serialization.xstream.XStreamXMLSerialization;
import br.com.caelum.vraptor.util.test.MockInstanceImpl;
import br.com.caelum.vraptor.validator.SingletonResourceBundle;

public class I18nMessageSerializationTest {

	private I18nMessageSerialization serialization;
	private ByteArrayOutputStream stream;
	private PrintWriter writer;

	@Before
	public void setup() throws Exception {
		stream = new ByteArrayOutputStream();

		Environment environment = mock(Environment.class);

		HttpServletResponse response = mock(HttpServletResponse.class);
		writer = new PrintWriter(stream);
		when(response.getWriter()).thenReturn(writer);
		DefaultTypeNameExtractor extractor = new DefaultTypeNameExtractor();
		XStreamBuilder builder = XStreamBuilderImpl.cleanInstance(new MessageConverter());
		XStreamXMLSerialization xmlSerialization = new XStreamXMLSerialization(response, builder, environment);

		List<JsonSerializer<?>> jsonSerializers = new ArrayList<>();
		List<JsonDeserializer<?>> jsonDeserializers = new ArrayList<>();
		jsonSerializers.add(new CalendarGsonConverter());
		jsonSerializers.add(new MessageGsonConverter());

		GsonSerializerBuilder gsonBuilder =  new GsonBuilderWrapper(new MockInstanceImpl<>(jsonSerializers),
				new MockInstanceImpl<>(jsonDeserializers), new Serializee(new DefaultReflectionProvider()), new DefaultReflectionProvider());
		GsonJSONSerialization jsonSerialization = new GsonJSONSerialization(response, extractor, gsonBuilder, environment,
				new DefaultReflectionProvider());

		Container container = mock(Container.class);
		when(container.instanceFor(JSONSerialization.class)).thenReturn(jsonSerialization);
		when(container.instanceFor(XMLSerialization.class)).thenReturn(xmlSerialization);

		ResourceBundle bundle = new SingletonResourceBundle("message.cat", "Just another {0} in {1}");
		serialization = new I18nMessageSerialization(container , bundle);
	}

	@Test
	public void shouldCallJsonSerialization() {
		String expectedResult = "{\"message\":{\"category\":\"success\",\"message\":\"Just another {0} in {1}\"}}";
		serialization.from("success", "message.cat").as(json());
		writer.flush();

		assertThat(result(), is(equalTo(expectedResult)));
	}

	@Test
	public void shouldCallXStreamXmlSerialization() {
		String expectedResult = "<message><message>Just another {0} in {1}</message><category>success</category></message>";
		serialization.from("success", "message.cat").as(xml());
		assertThat(result(), is(equalTo(expectedResult)));
	}

	@Test
	public void shouldCallXStreamSerializationWithParams() {
		String expectedResult = "<message><message>Just another object in memory</message><category>success</category></message>";
		Object[] params = {"object", "memory"};
		serialization.from("success", "message.cat", params).as(xml());
		assertThat(result(), is(equalTo(expectedResult)));
	}

	private String result() {
		return new String(stream.toByteArray());
	}

}


text:
package vspk

import "github.com/nuagenetworks/go-bambou/bambou"

// DomainIdentity represents the Identity of the object
var DomainIdentity = bambou.Identity{
	Name:     "domain",
	Category: "domains",
}

// DomainsList represents a list of Domains
type DomainsList []*Domain

// DomainsAncestor is the interface of an ancestor of a Domain must implement.
type DomainsAncestor interface {
	Domains(*bambou.FetchingInfo) (DomainsList, *bambou.Error)
	CreateDomains(*Domain) *bambou.Error
}

// Domain represents the model of a domain
type Domain struct {
	ID                              string        `json:"ID,omitempty"`
	ParentID                        string        `json:"parentID,omitempty"`
	ParentType                      string        `json:"parentType,omitempty"`
	Owner                           string        `json:"owner,omitempty"`
	PATEnabled                      string        `json:"PATEnabled,omitempty"`
	ECMPCount                       int           `json:"ECMPCount,omitempty"`
	BGPEnabled                      bool          `json:"BGPEnabled"`
	DHCPBehavior                    string        `json:"DHCPBehavior,omitempty"`
	DHCPServerAddress               string        `json:"DHCPServerAddress,omitempty"`
	LabelID                         int           `json:"labelID,omitempty"`
	BackHaulRouteDistinguisher      string        `json:"backHaulRouteDistinguisher,omitempty"`
	BackHaulRouteTarget             string        `json:"backHaulRouteTarget,omitempty"`
	BackHaulVNID                    int           `json:"backHaulVNID,omitempty"`
	MaintenanceMode                 string        `json:"maintenanceMode,omitempty"`
	Name                            string        `json:"name,omitempty"`
	LastUpdatedBy                   string        `json:"lastUpdatedBy,omitempty"`
	LeakingEnabled                  bool          `json:"leakingEnabled"`
	SecondaryDHCPServerAddress      string        `json:"secondaryDHCPServerAddress,omitempty"`
	TemplateID                      string        `json:"templateID,omitempty"`
	PermittedAction                 string        `json:"permittedAction,omitempty"`
	ServiceID                       int           `json:"serviceID,omitempty"`
	Description                     string        `json:"description,omitempty"`
	DhcpServerAddresses             []interface{} `json:"dhcpServerAddresses,omitempty"`
	GlobalRoutingEnabled            bool          `json:"globalRoutingEnabled"`
	ImportRouteTarget               string        `json:"importRouteTarget,omitempty"`
	Encryption                      string        `json:"encryption,omitempty"`
	UnderlayEnabled                 string        `json:"underlayEnabled,omitempty"`
	EntityScope                     string        `json:"entityScope,omitempty"`
	PolicyChangeStatus              string        `json:"policyChangeStatus,omitempty"`
	RouteDistinguisher              string        `json:"routeDistinguisher,omitempty"`
	RouteTarget                     string        `json:"routeTarget,omitempty"`
	UplinkPreference                string        `json:"uplinkPreference,omitempty"`
	ApplicationDeploymentPolicy     string        `json:"applicationDeploymentPolicy,omitempty"`
	AssociatedBGPProfileID          string        `json:"associatedBGPProfileID,omitempty"`
	AssociatedMulticastChannelMapID string        `json:"associatedMulticastChannelMapID,omitempty"`
	AssociatedPATMapperID           string        `json:"associatedPATMapperID,omitempty"`
	Stretched                       bool          `json:"stretched"`
	Multicast                       string        `json:"multicast,omitempty"`
	TunnelType                      string        `json:"tunnelType,omitempty"`
	CustomerID                      int           `json:"customerID,omitempty"`
	ExportRouteTarget               string        `json:"exportRouteTarget,omitempty"`
	ExternalID                      string        `json:"externalID,omitempty"`
}

// NewDomain returns a new *Domain
func NewDomain() *Domain {

	return &Domain{
		PATEnabled:                  "INHERITED",
		DHCPBehavior:                "CONSUME",
		TunnelType:                  "DC_DEFAULT",
		ApplicationDeploymentPolicy: "ZONE",
		MaintenanceMode:             "DISABLED",
	}
}

// Identity returns the Identity of the object.
func (o *Domain) Identity() bambou.Identity {

	return DomainIdentity
}

// Identifier returns the value of the object's unique identifier.
func (o *Domain) Identifier() string {

	return o.ID
}

// SetIdentifier sets the value of the object's unique identifier.
func (o *Domain) SetIdentifier(ID string) {

	o.ID = ID
}

// Fetch retrieves the Domain from the server
func (o *Domain) Fetch() *bambou.Error {

	return bambou.CurrentSession().FetchEntity(o)
}

// Save saves the Domain into the server
func (o *Domain) Save() *bambou.Error {

	return bambou.CurrentSession().SaveEntity(o)
}

// Delete deletes the Domain from the server
func (o *Domain) Delete() *bambou.Error {

	return bambou.CurrentSession().DeleteEntity(o)
}

// TCAs retrieves the list of child TCAs of the Domain
func (o *Domain) TCAs(info *bambou.FetchingInfo) (TCAsList, *bambou.Error) {

	var list TCAsList
	err := bambou.CurrentSession().FetchChildren(o, TCAIdentity, &list, info)
	return list, err
}

// CreateTCA creates a new child TCA under the Domain
func (o *Domain) CreateTCA(child *TCA) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// RedirectionTargets retrieves the list of child RedirectionTargets of the Domain
func (o *Domain) RedirectionTargets(info *bambou.FetchingInfo) (RedirectionTargetsList, *bambou.Error) {

	var list RedirectionTargetsList
	err := bambou.CurrentSession().FetchChildren(o, RedirectionTargetIdentity, &list, info)
	return list, err
}

// CreateRedirectionTarget creates a new child RedirectionTarget under the Domain
func (o *Domain) CreateRedirectionTarget(child *RedirectionTarget) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// Permissions retrieves the list of child Permissions of the Domain
func (o *Domain) Permissions(info *bambou.FetchingInfo) (PermissionsList, *bambou.Error) {

	var list PermissionsList
	err := bambou.CurrentSession().FetchChildren(o, PermissionIdentity, &list, info)
	return list, err
}

// CreatePermission creates a new child Permission under the Domain
func (o *Domain) CreatePermission(child *Permission) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// Metadatas retrieves the list of child Metadatas of the Domain
func (o *Domain) Metadatas(info *bambou.FetchingInfo) (MetadatasList, *bambou.Error) {

	var list MetadatasList
	err := bambou.CurrentSession().FetchChildren(o, MetadataIdentity, &list, info)
	return list, err
}

// CreateMetadata creates a new child Metadata under the Domain
func (o *Domain) CreateMetadata(child *Metadata) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// EgressACLEntryTemplates retrieves the list of child EgressACLEntryTemplates of the Domain
func (o *Domain) EgressACLEntryTemplates(info *bambou.FetchingInfo) (EgressACLEntryTemplatesList, *bambou.Error) {

	var list EgressACLEntryTemplatesList
	err := bambou.CurrentSession().FetchChildren(o, EgressACLEntryTemplateIdentity, &list, info)
	return list, err
}

// CreateEgressACLEntryTemplate creates a new child EgressACLEntryTemplate under the Domain
func (o *Domain) CreateEgressACLEntryTemplate(child *EgressACLEntryTemplate) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// EgressACLTemplates retrieves the list of child EgressACLTemplates of the Domain
func (o *Domain) EgressACLTemplates(info *bambou.FetchingInfo) (EgressACLTemplatesList, *bambou.Error) {

	var list EgressACLTemplatesList
	err := bambou.CurrentSession().FetchChildren(o, EgressACLTemplateIdentity, &list, info)
	return list, err
}

// CreateEgressACLTemplate creates a new child EgressACLTemplate under the Domain
func (o *Domain) CreateEgressACLTemplate(child *EgressACLTemplate) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// DomainFIPAclTemplates retrieves the list of child DomainFIPAclTemplates of the Domain
func (o *Domain) DomainFIPAclTemplates(info *bambou.FetchingInfo) (DomainFIPAclTemplatesList, *bambou.Error) {

	var list DomainFIPAclTemplatesList
	err := bambou.CurrentSession().FetchChildren(o, DomainFIPAclTemplateIdentity, &list, info)
	return list, err
}

// CreateDomainFIPAclTemplate creates a new child DomainFIPAclTemplate under the Domain
func (o *Domain) CreateDomainFIPAclTemplate(child *DomainFIPAclTemplate) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// FloatingIPACLTemplates retrieves the list of child FloatingIPACLTemplates of the Domain
func (o *Domain) FloatingIPACLTemplates(info *bambou.FetchingInfo) (FloatingIPACLTemplatesList, *bambou.Error) {

	var list FloatingIPACLTemplatesList
	err := bambou.CurrentSession().FetchChildren(o, FloatingIPACLTemplateIdentity, &list, info)
	return list, err
}

// CreateFloatingIPACLTemplate creates a new child FloatingIPACLTemplate under the Domain
func (o *Domain) CreateFloatingIPACLTemplate(child *FloatingIPACLTemplate) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// DHCPOptions retrieves the list of child DHCPOptions of the Domain
func (o *Domain) DHCPOptions(info *bambou.FetchingInfo) (DHCPOptionsList, *bambou.Error) {

	var list DHCPOptionsList
	err := bambou.CurrentSession().FetchChildren(o, DHCPOptionIdentity, &list, info)
	return list, err
}

// CreateDHCPOption creates a new child DHCPOption under the Domain
func (o *Domain) CreateDHCPOption(child *DHCPOption) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// Links retrieves the list of child Links of the Domain
func (o *Domain) Links(info *bambou.FetchingInfo) (LinksList, *bambou.Error) {

	var list LinksList
	err := bambou.CurrentSession().FetchChildren(o, LinkIdentity, &list, info)
	return list, err
}

// CreateLink creates a new child Link under the Domain
func (o *Domain) CreateLink(child *Link) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// FloatingIps retrieves the list of child FloatingIps of the Domain
func (o *Domain) FloatingIps(info *bambou.FetchingInfo) (FloatingIpsList, *bambou.Error) {

	var list FloatingIpsList
	err := bambou.CurrentSession().FetchChildren(o, FloatingIpIdentity, &list, info)
	return list, err
}

// CreateFloatingIp creates a new child FloatingIp under the Domain
func (o *Domain) CreateFloatingIp(child *FloatingIp) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// GlobalMetadatas retrieves the list of child GlobalMetadatas of the Domain
func (o *Domain) GlobalMetadatas(info *bambou.FetchingInfo) (GlobalMetadatasList, *bambou.Error) {

	var list GlobalMetadatasList
	err := bambou.CurrentSession().FetchChildren(o, GlobalMetadataIdentity, &list, info)
	return list, err
}

// CreateGlobalMetadata creates a new child GlobalMetadata under the Domain
func (o *Domain) CreateGlobalMetadata(child *GlobalMetadata) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// VMs retrieves the list of child VMs of the Domain
func (o *Domain) VMs(info *bambou.FetchingInfo) (VMsList, *bambou.Error) {

	var list VMsList
	err := bambou.CurrentSession().FetchChildren(o, VMIdentity, &list, info)
	return list, err
}

// CreateVM creates a new child VM under the Domain
func (o *Domain) CreateVM(child *VM) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// VMInterfaces retrieves the list of child VMInterfaces of the Domain
func (o *Domain) VMInterfaces(info *bambou.FetchingInfo) (VMInterfacesList, *bambou.Error) {

	var list VMInterfacesList
	err := bambou.CurrentSession().FetchChildren(o, VMInterfaceIdentity, &list, info)
	return list, err
}

// CreateVMInterface creates a new child VMInterface under the Domain
func (o *Domain) CreateVMInterface(child *VMInterface) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// IngressACLEntryTemplates retrieves the list of child IngressACLEntryTemplates of the Domain
func (o *Domain) IngressACLEntryTemplates(info *bambou.FetchingInfo) (IngressACLEntryTemplatesList, *bambou.Error) {

	var list IngressACLEntryTemplatesList
	err := bambou.CurrentSession().FetchChildren(o, IngressACLEntryTemplateIdentity, &list, info)
	return list, err
}

// CreateIngressACLEntryTemplate creates a new child IngressACLEntryTemplate under the Domain
func (o *Domain) CreateIngressACLEntryTemplate(child *IngressACLEntryTemplate) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// IngressACLTemplates retrieves the list of child IngressACLTemplates of the Domain
func (o *Domain) IngressACLTemplates(info *bambou.FetchingInfo) (IngressACLTemplatesList, *bambou.Error) {

	var list IngressACLTemplatesList
	err := bambou.CurrentSession().FetchChildren(o, IngressACLTemplateIdentity, &list, info)
	return list, err
}

// CreateIngressACLTemplate creates a new child IngressACLTemplate under the Domain
func (o *Domain) CreateIngressACLTemplate(child *IngressACLTemplate) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// IngressAdvFwdTemplates retrieves the list of child IngressAdvFwdTemplates of the Domain
func (o *Domain) IngressAdvFwdTemplates(info *bambou.FetchingInfo) (IngressAdvFwdTemplatesList, *bambou.Error) {

	var list IngressAdvFwdTemplatesList
	err := bambou.CurrentSession().FetchChildren(o, IngressAdvFwdTemplateIdentity, &list, info)
	return list, err
}

// CreateIngressAdvFwdTemplate creates a new child IngressAdvFwdTemplate under the Domain
func (o *Domain) CreateIngressAdvFwdTemplate(child *IngressAdvFwdTemplate) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// IngressExternalServiceTemplates retrieves the list of child IngressExternalServiceTemplates of the Domain
func (o *Domain) IngressExternalServiceTemplates(info *bambou.FetchingInfo) (IngressExternalServiceTemplatesList, *bambou.Error) {

	var list IngressExternalServiceTemplatesList
	err := bambou.CurrentSession().FetchChildren(o, IngressExternalServiceTemplateIdentity, &list, info)
	return list, err
}

// CreateIngressExternalServiceTemplate creates a new child IngressExternalServiceTemplate under the Domain
func (o *Domain) CreateIngressExternalServiceTemplate(child *IngressExternalServiceTemplate) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// Jobs retrieves the list of child Jobs of the Domain
func (o *Domain) Jobs(info *bambou.FetchingInfo) (JobsList, *bambou.Error) {

	var list JobsList
	err := bambou.CurrentSession().FetchChildren(o, JobIdentity, &list, info)
	return list, err
}

// CreateJob creates a new child Job under the Domain
func (o *Domain) CreateJob(child *Job) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// PolicyGroups retrieves the list of child PolicyGroups of the Domain
func (o *Domain) PolicyGroups(info *bambou.FetchingInfo) (PolicyGroupsList, *bambou.Error) {

	var list PolicyGroupsList
	err := bambou.CurrentSession().FetchChildren(o, PolicyGroupIdentity, &list, info)
	return list, err
}

// CreatePolicyGroup creates a new child PolicyGroup under the Domain
func (o *Domain) CreatePolicyGroup(child *PolicyGroup) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// Domains retrieves the list of child Domains of the Domain
func (o *Domain) Domains(info *bambou.FetchingInfo) (DomainsList, *bambou.Error) {

	var list DomainsList
	err := bambou.CurrentSession().FetchChildren(o, DomainIdentity, &list, info)
	return list, err
}

// AssignDomains assigns the list of Domains to the Domain
func (o *Domain) AssignDomains(children DomainsList) *bambou.Error {

	list := []bambou.Identifiable{}
	for _, c := range children {
		list = append(list, c)
	}

	return bambou.CurrentSession().AssignChildren(o, list, DomainIdentity)
}

// DomainTemplates retrieves the list of child DomainTemplates of the Domain
func (o *Domain) DomainTemplates(info *bambou.FetchingInfo) (DomainTemplatesList, *bambou.Error) {

	var list DomainTemplatesList
	err := bambou.CurrentSession().FetchChildren(o, DomainTemplateIdentity, &list, info)
	return list, err
}

// CreateDomainTemplate creates a new child DomainTemplate under the Domain
func (o *Domain) CreateDomainTemplate(child *DomainTemplate) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// Zones retrieves the list of child Zones of the Domain
func (o *Domain) Zones(info *bambou.FetchingInfo) (ZonesList, *bambou.Error) {

	var list ZonesList
	err := bambou.CurrentSession().FetchChildren(o, ZoneIdentity, &list, info)
	return list, err
}

// CreateZone creates a new child Zone under the Domain
func (o *Domain) CreateZone(child *Zone) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// Containers retrieves the list of child Containers of the Domain
func (o *Domain) Containers(info *bambou.FetchingInfo) (ContainersList, *bambou.Error) {

	var list ContainersList
	err := bambou.CurrentSession().FetchChildren(o, ContainerIdentity, &list, info)
	return list, err
}

// CreateContainer creates a new child Container under the Domain
func (o *Domain) CreateContainer(child *Container) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// ContainerInterfaces retrieves the list of child ContainerInterfaces of the Domain
func (o *Domain) ContainerInterfaces(info *bambou.FetchingInfo) (ContainerInterfacesList, *bambou.Error) {

	var list ContainerInterfacesList
	err := bambou.CurrentSession().FetchChildren(o, ContainerInterfaceIdentity, &list, info)
	return list, err
}

// CreateContainerInterface creates a new child ContainerInterface under the Domain
func (o *Domain) CreateContainerInterface(child *ContainerInterface) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// QOSs retrieves the list of child QOSs of the Domain
func (o *Domain) QOSs(info *bambou.FetchingInfo) (QOSsList, *bambou.Error) {

	var list QOSsList
	err := bambou.CurrentSession().FetchChildren(o, QOSIdentity, &list, info)
	return list, err
}

// CreateQOS creates a new child QOS under the Domain
func (o *Domain) CreateQOS(child *QOS) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// HostInterfaces retrieves the list of child HostInterfaces of the Domain
func (o *Domain) HostInterfaces(info *bambou.FetchingInfo) (HostInterfacesList, *bambou.Error) {

	var list HostInterfacesList
	err := bambou.CurrentSession().FetchChildren(o, HostInterfaceIdentity, &list, info)
	return list, err
}

// CreateHostInterface creates a new child HostInterface under the Domain
func (o *Domain) CreateHostInterface(child *HostInterface) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// RoutingPolicies retrieves the list of child RoutingPolicies of the Domain
func (o *Domain) RoutingPolicies(info *bambou.FetchingInfo) (RoutingPoliciesList, *bambou.Error) {

	var list RoutingPoliciesList
	err := bambou.CurrentSession().FetchChildren(o, RoutingPolicyIdentity, &list, info)
	return list, err
}

// CreateRoutingPolicy creates a new child RoutingPolicy under the Domain
func (o *Domain) CreateRoutingPolicy(child *RoutingPolicy) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// UplinkRDs retrieves the list of child UplinkRDs of the Domain
func (o *Domain) UplinkRDs(info *bambou.FetchingInfo) (UplinkRDsList, *bambou.Error) {

	var list UplinkRDsList
	err := bambou.CurrentSession().FetchChildren(o, UplinkRDIdentity, &list, info)
	return list, err
}

// CreateUplinkRD creates a new child UplinkRD under the Domain
func (o *Domain) CreateUplinkRD(child *UplinkRD) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// VPNConnections retrieves the list of child VPNConnections of the Domain
func (o *Domain) VPNConnections(info *bambou.FetchingInfo) (VPNConnectionsList, *bambou.Error) {

	var list VPNConnectionsList
	err := bambou.CurrentSession().FetchChildren(o, VPNConnectionIdentity, &list, info)
	return list, err
}

// CreateVPNConnection creates a new child VPNConnection under the Domain
func (o *Domain) CreateVPNConnection(child *VPNConnection) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// VPorts retrieves the list of child VPorts of the Domain
func (o *Domain) VPorts(info *bambou.FetchingInfo) (VPortsList, *bambou.Error) {

	var list VPortsList
	err := bambou.CurrentSession().FetchChildren(o, VPortIdentity, &list, info)
	return list, err
}

// CreateVPort creates a new child VPort under the Domain
func (o *Domain) CreateVPort(child *VPort) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// BridgeInterfaces retrieves the list of child BridgeInterfaces of the Domain
func (o *Domain) BridgeInterfaces(info *bambou.FetchingInfo) (BridgeInterfacesList, *bambou.Error) {

	var list BridgeInterfacesList
	err := bambou.CurrentSession().FetchChildren(o, BridgeInterfaceIdentity, &list, info)
	return list, err
}

// CreateBridgeInterface creates a new child BridgeInterface under the Domain
func (o *Domain) CreateBridgeInterface(child *BridgeInterface) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// Groups retrieves the list of child Groups of the Domain
func (o *Domain) Groups(info *bambou.FetchingInfo) (GroupsList, *bambou.Error) {

	var list GroupsList
	err := bambou.CurrentSession().FetchChildren(o, GroupIdentity, &list, info)
	return list, err
}

// CreateGroup creates a new child Group under the Domain
func (o *Domain) CreateGroup(child *Group) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// StaticRoutes retrieves the list of child StaticRoutes of the Domain
func (o *Domain) StaticRoutes(info *bambou.FetchingInfo) (StaticRoutesList, *bambou.Error) {

	var list StaticRoutesList
	err := bambou.CurrentSession().FetchChildren(o, StaticRouteIdentity, &list, info)
	return list, err
}

// CreateStaticRoute creates a new child StaticRoute under the Domain
func (o *Domain) CreateStaticRoute(child *StaticRoute) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// Statistics retrieves the list of child Statistics of the Domain
func (o *Domain) Statistics(info *bambou.FetchingInfo) (StatisticsList, *bambou.Error) {

	var list StatisticsList
	err := bambou.CurrentSession().FetchChildren(o, StatisticsIdentity, &list, info)
	return list, err
}

// CreateStatistics creates a new child Statistics under the Domain
func (o *Domain) CreateStatistics(child *Statistics) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// StatisticsPolicies retrieves the list of child StatisticsPolicies of the Domain
func (o *Domain) StatisticsPolicies(info *bambou.FetchingInfo) (StatisticsPoliciesList, *bambou.Error) {

	var list StatisticsPoliciesList
	err := bambou.CurrentSession().FetchChildren(o, StatisticsPolicyIdentity, &list, info)
	return list, err
}

// CreateStatisticsPolicy creates a new child StatisticsPolicy under the Domain
func (o *Domain) CreateStatisticsPolicy(child *StatisticsPolicy) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// Subnets retrieves the list of child Subnets of the Domain
func (o *Domain) Subnets(info *bambou.FetchingInfo) (SubnetsList, *bambou.Error) {

	var list SubnetsList
	err := bambou.CurrentSession().FetchChildren(o, SubnetIdentity, &list, info)
	return list, err
}

// CreateSubnet creates a new child Subnet under the Domain
func (o *Domain) CreateSubnet(child *Subnet) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// EventLogs retrieves the list of child EventLogs of the Domain
func (o *Domain) EventLogs(info *bambou.FetchingInfo) (EventLogsList, *bambou.Error) {

	var list EventLogsList
	err := bambou.CurrentSession().FetchChildren(o, EventLogIdentity, &list, info)
	return list, err
}

// CreateEventLog creates a new child EventLog under the Domain
func (o *Domain) CreateEventLog(child *EventLog) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}

// ExternalAppServices retrieves the list of child ExternalAppServices of the Domain
func (o *Domain) ExternalAppServices(info *bambou.FetchingInfo) (ExternalAppServicesList, *bambou.Error) {

	var list ExternalAppServicesList
	err := bambou.CurrentSession().FetchChildren(o, ExternalAppServiceIdentity, &list, info)
	return list, err
}

// CreateExternalAppService creates a new child ExternalAppService under the Domain
func (o *Domain) CreateExternalAppService(child *ExternalAppService) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}


text:
"""Miscellaneous utility functions for use with Gate."""

import errno
import fcntl
import os
import pwd
import sys
import time
import functools
import pkg_resources
from hashlib import md5
from random import random, shuffle
from urllib import quote
from contextlib import contextmanager, closing
import ctypes
import ctypes.util
from ConfigParser import ConfigParser, NoSectionError, NoOptionError, \
    RawConfigParser
from optparse import OptionParser
from tempfile import mkstemp, NamedTemporaryFile
try:
    import simplejson as json
except ImportError:
    import json
import cPickle as pickle
import glob
from urlparse import urlparse as stdlib_urlparse, ParseResult
import itertools

import eventlet
from eventlet import GreenPool, sleep, Timeout
from eventlet.green import socket, threading
import netifaces
import codecs
utf8_decoder = codecs.getdecoder('utf-8')
utf8_encoder = codecs.getencoder('utf-8')

from logging.handlers import SysLogHandler
import logging

# setup notice level logging

NOTICE = 25
logging._levelNames[NOTICE] = 'NOTICE'
SysLogHandler.priority_map['NOTICE'] = 'notice'

# These are lazily pulled from libc elsewhere

_sys_fsync = None
_sys_fallocate = None
_posix_fadvise = None

# If set to non-zero, fallocate routines will fail based on free space
# available being at or below this amount, in bytes.

FALLOCATE_RESERVE = 0

# Used when reading config values

TRUE_VALUES = set((
    'true',
    '1',
    'yes',
    'on',
    't',
    'y',
    ))

def config_true_value(value):
    """
    Returns True if the value is either True or a string in TRUE_VALUES.
    Returns False otherwise.
    """

    return value is True or isinstance(value, basestring) \
        and value.lower() in TRUE_VALUES

class EggLoader(object):

    def __init__(self, egg, entry_point):
        if egg.startswith('egg:'):
            egg = egg.replace('egg:', '')
        name = egg.split('#')[0]
        module = egg.split('#')[1]
        dist = pkg_resources.get_distribution(name)
        self.entry = dist.get_entry_info(entry_point, module)

    def __call__(self):
        return self.entry.load()

def load_egg(egg, entry_point):
    return EggLoader(egg, entry_point)()

def noop_libc_function(*args):
    return 0

def load_libc_function(func_name, log_error=True):
    """
    Attempt to find the function in libc, otherwise return a no-op func.

    :param func_name: name of the function to pull from libc.
    """

    try:
        libc = ctypes.CDLL(ctypes.util.find_library('c'),
                           use_errno=True)
        return getattr(libc, func_name)
    except AttributeError:
        if log_error:
            logging.warn(_('Unable to locate %s in libc.  Leaving as a no-op.'
                         ), func_name)
        return noop_libc_function

def get_param(req, name, default=None):
    """
    Get parameters from an HTTP request ensuring proper handling UTF-8
    encoding.

    :param req: request object
    :param name: parameter name
    :param default: result to return if the parameter is not found
    :returns: HTTP request parameter value
    """

    value = req.params.get(name, default)
    if value and not isinstance(value, unicode):
        value.decode('utf8')  # Ensure UTF8ness
    return value

class FallocateWrapper(object):

    def __init__(self, noop=False):
        if noop:
            self.func_name = 'posix_fallocate'
            self.fallocate = noop_libc_function
            return

        # # fallocate is preferred because we need the on-disk size to match
        # # the allocated size. Older versions of sqlite require that the
        # # two sizes match. However, fallocate is Linux only.

        for func in ('fallocate', 'posix_fallocate'):
            self.func_name = func
            self.fallocate = load_libc_function(func, log_error=False)
            if self.fallocate is not noop_libc_function:
                break
        if self.fallocate is noop_libc_function:
            logging.warn(_('Unable to locate fallocate, posix_fallocate in libc.  Leaving as a no-op.'
                         ))

    def __call__(
        self,
        fd,
        mode,
        offset,
        length,
        ):
        """ The length parameter must be a ctypes.c_uint64 """

        if FALLOCATE_RESERVE > 0:
            st = os.fstatvfs(fd)
            free = st.f_frsize * st.f_bavail - length.value
            if free <= FALLOCATE_RESERVE:
                raise OSError('FALLOCATE_RESERVE fail %s <= %s'
                              % (free, FALLOCATE_RESERVE))
        args = {'fallocate': (fd, mode, offset, length),
                'posix_fallocate': (fd, offset, length)}
        return self.fallocate(*args[self.func_name])

def disable_fallocate():
    global _sys_fallocate
    _sys_fallocate = FallocateWrapper(noop=True)

def fallocate(fd, size):
    """
    Pre-allocate disk space for a file.

    :param fd: file descriptor
    :param size: size to allocate (in bytes)
    """

    global _sys_fallocate
    if _sys_fallocate is None:
        _sys_fallocate = FallocateWrapper()
    if size < 0:
        size = 0

    # 1 means "FALLOC_FL_KEEP_SIZE", which means it pre-allocates invisibly

    ret = _sys_fallocate(fd, 1, 0, ctypes.c_uint64(size))
    err = ctypes.get_errno()
    if ret and err not in (0, errno.ENOSYS, errno.EOPNOTSUPP,
                           errno.EINVAL):
        raise OSError(err, 'Unable to fallocate(%s)' % size)

class FsyncWrapper(object):

    def __init__(self):
        if hasattr(os, 'fdatasync'):
            self.func_name = 'fdatasync'
            self.fsync = os.fdatasync
            self.fcntl_flag = None
        elif hasattr(fcntl, 'F_FULLFSYNC'):
            self.func_name = 'fcntl'
            self.fsync = fcntl.fcntl
            self.fcntl_flag = fcntl.F_FULLFSYNC
        else:
            self.func_name = 'fsync'
            self.fsync = os.fsync
            self.fcntl_flag = None

    def __call__(self, fd):
        args = {'fdatasync': (fd, ), 'fsync': (fd, ), 'fcntl': (fd,
                self.fcntl_flag)}
        return self.fsync(*args[self.func_name])

def fsync(fd):
    """
    Write buffered changes to disk.

    :param fd: file descriptor
    """

    global _sys_fsync
    if _sys_fsync is None:
        _sys_fsync = FsyncWrapper()

    ret = _sys_fsync(fd)
    err = ctypes.get_errno()
    if ret and err != 0:
        raise OSError(err, 'Unable to fsync(%s)' % fd)

def drop_buffer_cache(fd, offset, length):
    """
    Drop 'buffer' cache for the given range of the given file.

    :param fd: file descriptor
    :param offset: start offset
    :param length: length
    """

    global _posix_fadvise
    if _posix_fadvise is None:
        _posix_fadvise = load_libc_function('posix_fadvise64')

    # 4 means "POSIX_FADV_DONTNEED"

    ret = _posix_fadvise(fd, ctypes.c_uint64(offset),
                         ctypes.c_uint64(length), 4)
    if ret != 0:
        logging.warn('posix_fadvise64(%s, %s, %s, 4) -> %s' % (fd,
                     offset, length, ret))

def normalize_timestamp(timestamp):
    """
    Format a timestamp (string or numeric) into a standardized
    xxxxxxxxxx.xxxxx format.

    :param timestamp: unix timestamp
    :returns: normalized timestamp as a string
    """

    return '%016.05f' % float(timestamp)

def mkdirs(path):
    """
    Ensures the path is a directory or makes it if not. Errors if the path
    exists but is a file or on permissions failure.

    :param path: path to create
    """

    if not os.path.isdir(path):
        try:
            os.makedirs(path)
        except OSError, err:
            if err.errno != errno.EEXIST or not os.path.isdir(path):
                raise

def renamer(old, new):
    """
    Attempt to fix / hide race conditions like empty object directories
    being removed by backend processes during uploads, by retrying.

    :param old: old path to be renamed
    :param new: new path to be renamed to
    """

    try:
        mkdirs(os.path.dirname(new))
        os.rename(old, new)
    except OSError:
        mkdirs(os.path.dirname(new))
        os.rename(old, new)

def check_utf8(string):
    """
    Validate if a string is valid UTF-8 str or unicode and that it
    does not contain any null character.

    :param string: string to be validated
    :returns: True if the string is valid utf-8 str or unicode and
              contains no null characters, False otherwise
    """
    if not string:
        return False
    try:
        if isinstance(string, unicode):
            string.encode('utf-8')
        else:
            string.decode('UTF-8')
        return '\x00' not in string
    # If string is unicode, decode() will raise UnicodeEncodeError
    # So, we should catch both UnicodeDecodeError & UnicodeEncodeError
    except UnicodeError:
        return False

def check_metadata(req, target_type):
    """
    Check metadata sent in the request headers.

    :param req: request object
    :param target_type: str: one of: object, container, or account: indicates
                        which type the target storage for the metadata is
    :returns: HTTPBadRequest with bad metadata otherwise None
    """
    prefix = 'x-%s-meta-' % target_type.lower()
    meta_count = 0
    meta_size = 0
    for key, value in req.headers.iteritems():
        if isinstance(value, basestring) and len(value) > MAX_HEADER_SIZE:
            return HTTPBadRequest('Header Line Too Long')
        if not key.lower().startswith(prefix):
            continue
        key = key[len(prefix):]
        if not key:
            return HTTPBadRequest(body='Metadata name cannot be empty',
                                  request=req, content_type='text/plain')
        meta_count += 1
        meta_size += len(key) + len(value)
        if len(key) > MAX_META_NAME_LENGTH:
            return HTTPBadRequest(
                body='Metadata name too long; max %d' % MAX_META_NAME_LENGTH,
                request=req, content_type='text/plain')
        elif len(value) > MAX_META_VALUE_LENGTH:
            return HTTPBadRequest(
                body='Metadata value too long; max %d' % MAX_META_VALUE_LENGTH,
                request=req, content_type='text/plain')
        elif meta_count > MAX_META_COUNT:
            return HTTPBadRequest(
                body='Too many metadata items; max %d' % MAX_META_COUNT,
                request=req, content_type='text/plain')
        elif meta_size > MAX_META_OVERALL_SIZE:
            return HTTPBadRequest(
                body='Total metadata too large; max %d'
                % MAX_META_OVERALL_SIZE,
                request=req, content_type='text/plain')
    return None

def split_path(
    path,
    minsegs=1,
    maxsegs=None,
    rest_with_last=False,
    ):
    """
    Validate and split the given HTTP request path.

    **Examples**::

        ['a'] = split_path('/a')
        ['a', None] = split_path('/a', 1, 2)
        ['a', 'c'] = split_path('/a/c', 1, 2)
        ['a', 'c', 'o/r'] = split_path('/a/c/o/r', 1, 3, True)

    :param path: HTTP Request path to be split
    :param minsegs: Minimum number of segments to be extracted
    :param maxsegs: Maximum number of segments to be extracted
    :param rest_with_last: If True, trailing data will be returned as part
                           of last segment.  If False, and there is
                           trailing data, raises ValueError.
    :returns: list of segments with a length of maxsegs (non-existant
              segments will return as None)
    :raises: ValueError if given an invalid path
    """

    if not maxsegs:
        maxsegs = minsegs
    if minsegs > maxsegs:
        raise ValueError('minsegs > maxsegs: %d > %d' % (minsegs,
                         maxsegs))
    if rest_with_last:
        segs = path.split('/', maxsegs)
        minsegs += 1
        maxsegs += 1
        count = len(segs)
        if segs[0] or count < minsegs or count > maxsegs or '' \
            in segs[1:minsegs]:
            raise ValueError('Invalid path: %s' % quote(path))
    else:
        minsegs += 1
        maxsegs += 1
        segs = path.split('/', maxsegs)
        count = len(segs)
        if segs[0] or count < minsegs or count > maxsegs + 1 or '' \
            in segs[1:minsegs] or count == maxsegs + 1 \
            and segs[maxsegs]:
            raise ValueError('Invalid path: %s' % quote(path))
    segs = segs[1:maxsegs]
    segs.extend([None] * (maxsegs - 1 - len(segs)))
    return segs

class NullLogger:

    """A no-op logger for eventlet wsgi."""

    def write(self, *args):

        # "Logs" the args to nowhere

        pass

class LoggerFileObject(object):

    def __init__(self, logger):
        self.logger = logger

    def write(self, value):
        value = value.strip()
        if value:
            if 'Connection reset by peer' in value:
                self.logger.error(_('STDOUT: Connection reset by peer'))
            else:
                self.logger.error(_('STDOUT: %s'), value)

    def writelines(self, values):
        self.logger.error(_('STDOUT: %s'), '#012'.join(values))

    def close(self):
        pass

    def flush(self):
        pass

    def __iter__(self):
        return self

    def next(self):
        raise IOError(errno.EBADF, 'Bad file descriptor')

    def read(self, size=-1):
        raise IOError(errno.EBADF, 'Bad file descriptor')

    def readline(self, size=-1):
        raise IOError(errno.EBADF, 'Bad file descriptor')

    def tell(self):
        return 0

    def xreadlines(self):
        return self

class StatsdClient(object):

    def __init__(
        self,
        host,
        port,
        base_prefix='',
        tail_prefix='',
        default_sample_rate=1,
        sample_rate_factor=1,
        ):
        self._host = host
        self._port = port
        self._base_prefix = base_prefix
        self.set_prefix(tail_prefix)
        self._default_sample_rate = default_sample_rate
        self._sample_rate_factor = sample_rate_factor
        self._target = (self._host, self._port)
        self.random = random

    def set_prefix(self, new_prefix):
        if new_prefix and self._base_prefix:
            self._prefix = '.'.join([self._base_prefix, new_prefix, ''])
        elif new_prefix:
            self._prefix = new_prefix + '.'
        elif self._base_prefix:
            self._prefix = self._base_prefix + '.'
        else:
            self._prefix = ''

    def _send(
        self,
        m_name,
        m_value,
        m_type,
        sample_rate,
        ):
        if sample_rate is None:
            sample_rate = self._default_sample_rate
        sample_rate = sample_rate * self._sample_rate_factor
        parts = ['%s%s:%s' % (self._prefix, m_name, m_value), m_type]
        if sample_rate < 1:
            if self.random() < sample_rate:
                parts.append('@%s' % (sample_rate, ))
            else:
                return

        # Ideally, we'd cache a sending socket in self, but that
        # results in a socket getting shared by multiple green threads.

        with closing(self._open_socket()) as sock:
            return sock.sendto('|'.join(parts), self._target)

    def _open_socket(self):
        return socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    def update_stats(
        self,
        m_name,
        m_value,
        sample_rate=None,
        ):
        return self._send(m_name, m_value, 'c', sample_rate)

    def increment(self, metric, sample_rate=None):
        return self.update_stats(metric, 1, sample_rate)

    def decrement(self, metric, sample_rate=None):
        return self.update_stats(metric, -1, sample_rate)

    def timing(
        self,
        metric,
        timing_ms,
        sample_rate=None,
        ):
        return self._send(metric, timing_ms, 'ms', sample_rate)

    def timing_since(
        self,
        metric,
        orig_time,
        sample_rate=None,
        ):
        return self.timing(metric, (time.time() - orig_time) * 1000,
                           sample_rate)

def timing_stats(**dec_kwargs):
    """
    Returns a decorator that logs timing events or errors for public methods in
    gate's wsgi server controllers, based on response code.
    """

    def decorating_func(func):
        method = func.func_name

        @functools.wraps(func)
        def _timing_stats(ctrl, *args, **kwargs):
            start_time = time.time()
            resp = func(ctrl, *args, **kwargs)
            if is_success(resp.status_int) \
                or is_redirection(resp.status_int) or resp.status_int \
                == HTTP_NOT_FOUND:
                ctrl.logger.timing_since(method + '.timing',
                        start_time, **dec_kwargs)
            else:
                ctrl.logger.timing_since(method + '.errors.timing',
                        start_time, **dec_kwargs)
            return resp

        return _timing_stats

    return decorating_func

# double inheritance to support property with setter

class LogAdapter(logging.LoggerAdapter, object):

    """
    A Logger like object which performs some reformatting on calls to
    :meth:`exception`.  Can be used to store a threadlocal transaction id and
    client ip.
    """

    _cls_thread_local = threading.local()

    def __init__(self, logger, server):
        logging.LoggerAdapter.__init__(self, logger, {})
        self.server = server
        setattr(self, 'warn', self.warning)

    @property
    def txn_id(self):
        if hasattr(self._cls_thread_local, 'txn_id'):
            return self._cls_thread_local.txn_id

    @txn_id.setter
    def txn_id(self, value):
        self._cls_thread_local.txn_id = value

    @property
    def client_ip(self):
        if hasattr(self._cls_thread_local, 'client_ip'):
            return self._cls_thread_local.client_ip

    @client_ip.setter
    def client_ip(self, value):
        self._cls_thread_local.client_ip = value

    @property
    def thread_locals(self):
        return (self.txn_id, self.client_ip)

    @thread_locals.setter
    def thread_locals(self, value):
        (self.txn_id, self.client_ip) = value

    def getEffectiveLevel(self):
        return self.logger.getEffectiveLevel()

    def process(self, msg, kwargs):
        """
        Add extra info to message
        """

        kwargs['extra'] = {'server': self.server,
                           'txn_id': self.txn_id,
                           'client_ip': self.client_ip}
        return (msg, kwargs)

    def notice(
        self,
        msg,
        *args,
        **kwargs
        ):
        """
        Convenience function for syslog priority LOG_NOTICE. The python
        logging lvl is set to 25, just above info.  SysLogHandler is
        monkey patched to map this log lvl to the LOG_NOTICE syslog
        priority.
        """

        self.log(NOTICE, msg, *args, **kwargs)

    def _exception(
        self,
        msg,
        *args,
        **kwargs
        ):
        logging.LoggerAdapter.exception(self, msg, *args, **kwargs)

    def exception(
        self,
        msg,
        *args,
        **kwargs
        ):
        (_junk, exc, _junk) = sys.exc_info()
        call = self.error
        emsg = ''
        if isinstance(exc, OSError):
            if exc.errno in (errno.EIO, errno.ENOSPC):
                emsg = str(exc)
            else:
                call = self._exception
        elif isinstance(exc, socket.error):
            if exc.errno == errno.ECONNREFUSED:
                emsg = _('Connection refused')
            elif exc.errno == errno.EHOSTUNREACH:
                emsg = _('Host unreachable')
            elif exc.errno == errno.ETIMEDOUT:
                emsg = _('Connection timeout')
            else:
                call = self._exception
        elif isinstance(exc, eventlet.Timeout):
            emsg = exc.__class__.__name__
            if hasattr(exc, 'seconds'):
                emsg += ' (%ss)' % exc.seconds
            if isinstance(exc, MessageTimeout):
                if exc.msg:
                    emsg += ' %s' % exc.msg
        else:
            call = self._exception
        call('%s: %s' % (msg, emsg), *args, **kwargs)

    def set_statsd_prefix(self, prefix):
        """
        The StatsD client prefix defaults to the "name" of the logger.  This
        method may override that default with a specific value.  Currently used
        in the proxy-server to differentiate the Account, Container, and Object
        controllers.
        """

        if self.logger.statsd_client:
            self.logger.statsd_client.set_prefix(prefix)

    def statsd_delegate(statsd_func_name):
        """
        Factory to create methods which delegate to methods on
        self.logger.statsd_client (an instance of StatsdClient).  The
        created methods conditionally delegate to a method whose name is given
        in 'statsd_func_name'.  The created delegate methods are a no-op when
        StatsD logging is not configured.

        :param statsd_func_name: the name of a method on StatsdClient.
        """

        func = getattr(StatsdClient, statsd_func_name)

        @functools.wraps(func)
        def wrapped(self, *a, **kw):
            if getattr(self.logger, 'statsd_client'):
                return func(self.logger.statsd_client, *a, **kw)
        return wrapped

    update_stats = statsd_delegate('update_stats')
    increment = statsd_delegate('increment')
    decrement = statsd_delegate('decrement')
    timing = statsd_delegate('timing')
    timing_since = statsd_delegate('timing_since')

class GateLogFormatter(logging.Formatter):

    """
    Custom logging.Formatter will append txn_id to a log message if the record
    has one and the message does not.
    """

    def format(self, record):
        if not hasattr(record, 'server'):

            # Catch log messages that were not initiated by gate
            # (for example, the keystone auth middleware)

            record.server = record.name
        msg = logging.Formatter.format(self, record)
        if hasattr(record, 'txn_id') and record.txn_id \
            and record.levelno != logging.INFO and record.txn_id \
            not in msg:
            msg = '%s (txn: %s)' % (msg, record.txn_id)
        if hasattr(record, 'client_ip') and record.client_ip \
            and record.levelno != logging.INFO and record.client_ip \
            not in msg:
            msg = '%s (client_ip: %s)' % (msg, record.client_ip)
        return msg

def get_logger(
    conf,
    name=None,
    log_to_console=False,
    log_route=None,
    fmt='%(server)s %(message)s',
    ):
    """
    Get the current system logger using config settings.

    **Log config and defaults**::

        log_facility = LOG_LOCAL0
        log_level = INFO
        log_name = gate
        log_udp_host = (disabled)
        log_udp_port = logging.handlers.SYSLOG_UDP_PORT
        log_address = /dev/log
        log_statsd_host = (disabled)
        log_statsd_port = 8125
        log_statsd_default_sample_rate = 1.0
        log_statsd_sample_rate_factor = 1.0
        log_statsd_metric_prefix = (empty-string)

    :param conf: Configuration dict to read settings from
    :param name: Name of the logger
    :param log_to_console: Add handler which writes to console on stderr
    :param log_route: Route for the logging, not emitted to the log, just used
                      to separate logging configurations
    :param fmt: Override log format
    """

    if not conf:
        conf = {}
    if name is None:
        name = conf.get('log_name', 'gate')
    if not log_route:
        log_route = name
    logger = logging.getLogger(log_route)
    logger.propagate = False

    # all new handlers will get the same formatter

    formatter = GateLogFormatter(fmt)

    # get_logger will only ever add one SysLog Handler to a logger

    if not hasattr(get_logger, 'handler4logger'):
        get_logger.handler4logger = {}
    if logger in get_logger.handler4logger:
        logger.removeHandler(get_logger.handler4logger[logger])

    # facility for this logger will be set by last call wins

    facility = getattr(SysLogHandler, conf.get('log_facility',
                       'LOG_LOCAL0'), SysLogHandler.LOG_LOCAL0)
    udp_host = conf.get('log_udp_host')
    if udp_host:
        udp_port = int(conf.get('log_udp_port',
                       logging.handlers.SYSLOG_UDP_PORT))
        handler = SysLogHandler(address=(udp_host, udp_port),
                                facility=facility)
    else:
        log_address = conf.get('log_address', '/dev/log')
        try:
            handler = SysLogHandler(address=log_address,
                                    facility=facility)
        except socket.error, e:

            # Either /dev/log isn't a UNIX socket or it does not exist at all

            if e.errno not in [errno.ENOTSOCK, errno.ENOENT]:
                raise e
            handler = SysLogHandler(facility=facility)
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    get_logger.handler4logger[logger] = handler

    # setup console logging

    if log_to_console or hasattr(get_logger, 'console_handler4logger'):

        # remove pre-existing console handler for this logger

        if not hasattr(get_logger, 'console_handler4logger'):
            get_logger.console_handler4logger = {}
        if logger in get_logger.console_handler4logger:
            logger.removeHandler(get_logger.console_handler4logger[logger])

        console_handler = logging.StreamHandler(sys.__stderr__)
        console_handler.setFormatter(formatter)
        logger.addHandler(console_handler)
        get_logger.console_handler4logger[logger] = console_handler

    # set the level for the logger

    logger.setLevel(getattr(logging, conf.get('log_level', 'INFO'
                    ).upper(), logging.INFO))

    # Setup logger with a StatsD client if so configured

    statsd_host = conf.get('log_statsd_host')
    if statsd_host:
        statsd_port = int(conf.get('log_statsd_port', 8125))
        base_prefix = conf.get('log_statsd_metric_prefix', '')
        default_sample_rate = \
            float(conf.get('log_statsd_default_sample_rate', 1))
        sample_rate_factor = \
            float(conf.get('log_statsd_sample_rate_factor', 1))
        statsd_client = StatsdClient(
            statsd_host,
            statsd_port,
            base_prefix,
            name,
            default_sample_rate,
            sample_rate_factor,
            )
        logger.statsd_client = statsd_client
    else:
        logger.statsd_client = None

    adapted_logger = LogAdapter(logger, name)
    other_handlers = conf.get('log_custom_handlers', None)
    if other_handlers:
        log_custom_handlers = [s.strip() for s in
                               other_handlers.split(',') if s.strip()]
        for hook in log_custom_handlers:
            try:
                (mod, fnc) = hook.rsplit('.', 1)
                logger_hook = getattr(__import__(mod, fromlist=[fnc]),
                        fnc)
                logger_hook(
                    conf,
                    name,
                    log_to_console,
                    log_route,
                    fmt,
                    logger,
                    adapted_logger,
                    )
            except (AttributeError, ImportError):
                print >> sys.stderr, \
                    'Error calling custom handler [%s]' % hook
            except ValueError:
                print >> sys.stderr, \
                    'Invalid custom handler format [%s]' % hook
    return adapted_logger

def get_hub():
    """
    Checks whether poll is available and falls back
    on select if it isn't.

    Note about epoll:

    Review: https://review.openstack.org/#/c/18806/

    There was a problem where once out of every 30 quadrillion
    connections, a coroutine wouldn't wake up when the client
    closed its end. Epoll was not reporting the event or it was
    getting swallowed somewhere. Then when that file descriptor
    was re-used, eventlet would freak right out because it still
    thought it was waiting for activity from it in some other coro.
    """

    try:
        import select
        if hasattr(select, 'poll'):
            return 'poll'
        return 'selects'
    except ImportError:
        return None

def drop_privileges(user):
    """
    Sets the userid/groupid of the current process, get session leader, etc.

    :param user: User name to change privileges to
    """

    user = pwd.getpwnam(user)
    if os.geteuid() == 0:
        os.setgroups([])
    os.setgid(user[3])
    os.setuid(user[2])
    os.environ['HOME'] = user[5]
    try:
        os.setsid()
    except OSError:
        pass
    os.chdir('/')  # in case you need to rmdir on where you started the daemon
    os.umask(022)  # ensure files are created with the correct privileges

def capture_stdio(logger, **kwargs):
    """
    Log unhandled exceptions, close stdio, capture stdout and stderr.

    param logger: Logger object to use
    """

    # log uncaught exceptions

    sys.excepthook = lambda *exc_info: \
        logger.critical(_('UNCAUGHT EXCEPTION'), exc_info=exc_info)

    # collect stdio file desc not in use for logging

    stdio_files = [sys.stdin, sys.stdout, sys.stderr]
    console_fds = [h.stream.fileno() for (_junk, h) in
                   getattr(get_logger, 'console_handler4logger',
                   {}).items()]
    stdio_files = [f for f in stdio_files if f.fileno()
                   not in console_fds]

    with open(os.devnull, 'r+b') as nullfile:

        # close stdio (excludes fds open for logging)

        for f in stdio_files:

            # some platforms throw an error when attempting an stdin flush

            try:
                f.flush()
            except IOError:
                pass

            try:
                os.dup2(nullfile.fileno(), f.fileno())
            except OSError:
                pass

    # redirect stdio

    if kwargs.pop('capture_stdout', True):
        sys.stdout = LoggerFileObject(logger)
    if kwargs.pop('capture_stderr', True):
        sys.stderr = LoggerFileObject(logger)

def parse_options(parser=None, once=False, test_args=None):
    """
    Parse standard gate server/daemon options with optparse.OptionParser.

    :param parser: OptionParser to use. If not sent one will be created.
    :param once: Boolean indicating the "once" option is available
    :param test_args: Override sys.argv; used in testing

    :returns : Tuple of (config, options); config is an absolute path to the
               config file, options is the parser options as a dictionary.

    :raises SystemExit: config option, file must exist
    """

    if not parser:
        parser = OptionParser(usage='%prog CONFIG [options]')
    parser.add_option('-v', '--verbose', default=False,
                      action='store_true', help='log to console')
    if once:
        parser.add_option('-o', '--once', default=False,
                          action='store_true',
                          help='only run one pass of daemon')

    # if test_args is None, optparse will use sys.argv[:1]
    options, args = parser.parse_args(args=test_args)

    if not args:
        parser.print_usage()
        print _("Error: missing config file argument")
        sys.exit(1)
    config = os.path.abspath(args.pop(0))
    if not os.path.exists(config):
        parser.print_usage()
        print _('Error: unable to locate %s') % config
        sys.exit(1)

    extra_args = []

    # if any named options appear in remaining args, set the option to True

    for arg in args:
        if arg in options.__dict__:
            setattr(options, arg, True)
        else:
            extra_args.append(arg)

    options = vars(options)
    if extra_args:
        options['extra_args'] = extra_args
    return (config, options)

def whataremyips():
    """
    Get the machine's ip addresses

    :returns: list of Strings of ip addresses
    """

    addresses = []
    for interface in netifaces.interfaces():
        try:
            iface_data = netifaces.ifaddresses(interface)
            for family in iface_data:
                if family not in (netifaces.AF_INET,
                                  netifaces.AF_INET6):
                    continue
                for address in iface_data[family]:
                    addresses.append(address['addr'])
        except ValueError:
            pass
    return addresses

def get_time_units(time_amount):
    """
    Get a nomralized length of time in the largest unit of time (hours,
    minutes, or seconds.)

    :param time_amount: length of time in seconds
    :returns: A touple of (length of time, unit of time) where unit of time is
              one of ('h', 'm', 's')
    """

    time_unit = 's'
    if time_amount > 60:
        time_amount /= 60
        time_unit = 'm'
        if time_amount > 60:
            time_amount /= 60
            time_unit = 'h'
    return (time_amount, time_unit)

def compute_eta(start_time, current_value, final_value):
    """
    Compute an ETA.  Now only if we could also have a progress bar...

    :param start_time: Unix timestamp when the operation began
    :param current_value: Current value
    :param final_value: Final value
    :returns: ETA as a tuple of (length of time, unit of time) where unit of
              time is one of ('h', 'm', 's')
    """

    elapsed = time.time() - start_time
    completion = float(current_value) / final_value or 0.00001
    return get_time_units(1.0 / completion * elapsed - elapsed)

def unlink_older_than(path, mtime):
    """
    Remove any file in a given path that that was last modified before mtime.

    :param path: path to remove file from
    :mtime: timestamp of oldest file to keep
    """

    if os.path.exists(path):
        for fname in listdir(path):
            fpath = os.path.join(path, fname)
            try:
                if os.path.getmtime(fpath) < mtime:
                    os.unlink(fpath)
            except OSError:
                pass

def item_from_env(env, item_name):
    """
    Get a value from the wsgi environment

    :param env: wsgi environment dict
    :param item_name: name of item to get

    :returns: the value from the environment
    """

    item = env.get(item_name, None)
    if item is None:
        logging.error('ERROR: %s could not be found in env!'
                      % item_name)
    return item

def readconf(
    conffile,
    section_name=None,
    log_name=None,
    defaults=None,
    raw=False,
    ):
    """
    Read config file and return config items as a dict

    :param conffile: path to config file, or a file-like object (hasattr
                     readline)
    :param section_name: config section to read (will return all sections if
                     not defined)
    :param log_name: name to be used with logging (will use section_name if
                     not defined)
    :param defaults: dict of default values to pre-populate the config with
    :returns: dict of config items
    """

    if defaults is None:
        defaults = {}
    if raw:
        c = RawConfigParser(defaults)
    else:
        c = ConfigParser(defaults)
    if hasattr(conffile, 'readline'):
        c.readfp(conffile)
    else:
        if not c.read(conffile):
            print _('Unable to read config file %s') % conffile
            sys.exit(1)
    if section_name:
        if c.has_section(section_name):
            conf = dict(c.items(section_name))
        else:
            print _('Unable to find %s config section in %s') \
                % (section_name, conffile)
            sys.exit(1)
        if 'log_name' not in conf:
            if log_name is not None:
                conf['log_name'] = log_name
            else:
                conf['log_name'] = section_name
    else:
        conf = {}
        for s in c.sections():
            conf.update({s: dict(c.items(s))})
        if 'log_name' not in conf:
            conf['log_name'] = log_name
    conf['__file__'] = conffile
    return conf

def write_pickle(
    obj,
    dest,
    tmp=None,
    pickle_protocol=0,
    ):
    """
    Ensure that a pickle file gets written to disk.  The file
    is first written to a tmp location, ensure it is synced to disk, then
    perform a move to its final location

    :param obj: python object to be pickled
    :param dest: path of final destination file
    :param tmp: path to tmp to use, defaults to None
    :param pickle_protocol: protocol to pickle the obj with, defaults to 0
    """

    if tmp is None:
        tmp = os.path.dirname(dest)
    (fd, tmppath) = mkstemp(dir=tmp, suffix='.tmp')
    with os.fdopen(fd, 'wb') as fo:
        pickle.dump(obj, fo, pickle_protocol)
        fo.flush()
        os.fsync(fd)
        renamer(tmppath, dest)

def search_tree(root, glob_match, ext):
    """Look in root, for any files/dirs matching glob, recurively traversing
    any found directories looking for files ending with ext

    :param root: start of search path
    :param glob_match: glob to match in root, matching dirs are traversed with
                       os.walk
    :param ext: only files that end in ext will be returned

    :returns: list of full paths to matching files, sorted

    """

    found_files = []
    for path in glob.glob(os.path.join(root, glob_match)):
        if path.endswith(ext):
            found_files.append(path)
        else:
            for (root, dirs, files) in os.walk(path):
                for file in files:
                    if file.endswith(ext):
                        found_files.append(os.path.join(root, file))
    return sorted(found_files)

def write_file(path, contents):
    """Write contents to file at path

    :param path: any path, subdirs will be created as needed
    :param contents: data to write to file, will be converted to string

    """

    (dirname, name) = os.path.split(path)
    if not os.path.exists(dirname):
        try:
            os.makedirs(dirname)
        except OSError, err:
            if err.errno == errno.EACCES:
                sys.exit('Unable to create %s.  Running as non-root?'
                         % dirname)
    with open(path, 'w') as f:
        f.write('%s' % contents)

def remove_file(path):
    """Quiet wrapper for os.unlink, OSErrors are suppressed

    :param path: first and only argument passed to os.unlink
    """

    try:
        os.unlink(path)
    except OSError:
        pass

def audit_location_generator(
    devices,
    datadir,
    mount_check=True,
    logger=None,
    ):
    '''
    Given a devices path and a data directory, yield (path, device,
    partition) for all files in that directory

    :param devices: parent directory of the devices to be audited
    :param datadir: a directory located under self.devices. This should be
                    one of the DATADIR constants defined in the account,
                    container, and object servers.
    :param mount_check: Flag to check if a mount check should be performed
                    on devices
    :param logger: a logger object
    '''

    device_dir = listdir(devices)

    # randomize devices in case of process restart before sweep completed

    shuffle(device_dir)
    for device in device_dir:
        if mount_check and not os.path.ismount(os.path.join(devices,
                device)):
            if logger:
                logger.debug(_('Skipping %s as it is not mounted'),
                             device)
            continue
        datadir_path = os.path.join(devices, device, datadir)
        if not os.path.exists(datadir_path):
            continue
        partitions = listdir(datadir_path)
        for partition in partitions:
            part_path = os.path.join(datadir_path, partition)
            if not os.path.isdir(part_path):
                continue
            suffixes = listdir(part_path)
            for suffix in suffixes:
                suff_path = os.path.join(part_path, suffix)
                if not os.path.isdir(suff_path):
                    continue
                hashes = listdir(suff_path)
                for hsh in hashes:
                    hash_path = os.path.join(suff_path, hsh)
                    if not os.path.isdir(hash_path):
                        continue
                    for fname in sorted(listdir(hash_path),
                            reverse=True):
                        path = os.path.join(hash_path, fname)
                        yield (path, device, partition)

def ratelimit_sleep(
    running_time,
    max_rate,
    incr_by=1,
    rate_buffer=5,
    ):
    '''
    Will eventlet.sleep() for the appropriate time so that the max_rate
    is never exceeded.  If max_rate is 0, will not ratelimit.  The
    maximum recommended rate should not exceed (1000 * incr_by) a second
    as eventlet.sleep() does involve some overhead.  Returns running_time
    that should be used for subsequent calls.

    :param running_time: the running time of the next allowable request. Best
                         to start at zero.
    :param max_rate: The maximum rate per second allowed for the process.
    :param incr_by: How much to increment the counter.  Useful if you want
                    to ratelimit 1024 bytes/sec and have differing sizes
                    of requests. Must be >= 0.
    :param rate_buffer: Number of seconds the rate counter can drop and be
                        allowed to catch up (at a faster than listed rate).
                        A larger number will result in larger spikes in rate
                        but better average accuracy.
    '''

    if not max_rate or incr_by <= 0:
        return running_time
    clock_accuracy = 1000.0
    now = time.time() * clock_accuracy
    time_per_request = clock_accuracy * (float(incr_by) / max_rate)
    if now - running_time > rate_buffer * clock_accuracy:
        running_time = now
    elif running_time - now > time_per_request:
        eventlet.sleep((running_time - now) / clock_accuracy)
    return running_time + time_per_request

class ContextPool(GreenPool):

    """GreenPool subclassed to kill its coros when it gets gc'ed"""

    def __enter__(self):
        return self

    def __exit__(
        self,
        type,
        value,
        traceback,
        ):
        for coro in list(self.coroutines_running):
            coro.kill()

class ModifiedParseResult(ParseResult):

    '''Parse results class for urlparse.'''

    @property
    def hostname(self):
        netloc = self.netloc.split('@', 1)[-1]
        if netloc.startswith('['):
            return netloc[1:].split(']')[0]
        elif ':' in netloc:
            return netloc.rsplit(':')[0]
        return netloc

    @property
    def port(self):
        netloc = self.netloc.split('@', 1)[-1]
        if netloc.startswith('['):
            netloc = netloc.rsplit(']')[1]
        if ':' in netloc:
            return int(netloc.rsplit(':')[1])
        return None

def urlparse(url):
    """
    urlparse augmentation.
    This is necessary because urlparse can't handle RFC 2732 URLs.

    :param url: URL to parse.
    """

    return ModifiedParseResult(*stdlib_urlparse(url))

def validate_sync_to(value, allowed_sync_hosts):
    if not value:
        return None
    p = urlparse(value)
    if p.scheme not in ('http', 'https'):
        return _('Invalid scheme %r in X-Container-Sync-To, must be "http" or "https".'
                 ) % p.scheme
    if not p.path:
        return _('Path required in X-Container-Sync-To')
    if p.params or p.query or p.fragment:
        return _('Params, queries, and fragments not allowed in X-Container-Sync-To'
                 )
    if p.hostname not in allowed_sync_hosts:
        return _('Invalid host %r in X-Container-Sync-To') % p.hostname
    return None

def get_remote_client(req):

    # remote host for zeus

    client = req.headers.get('x-cluster-client-ip')
    if not client and 'x-forwarded-for' in req.headers:

        # remote host for other lbs

        client = req.headers['x-forwarded-for'].split(',')[0].strip()
    if not client:
        client = req.remote_addr
    return client

def human_readable(value):
    """
    Returns the number in a human readable format; for example 1048576 = "1Mi".
    """

    value = float(value)
    index = -1
    suffixes = 'KMGTPEZY'
    while value >= 1024 and index + 1 < len(suffixes):
        index += 1
        value = round(value / 1024)
    if index == -1:
        return '%d' % value
    return '%d%si' % (round(value), suffixes[index])

def dump_recon_cache(
    cache_dict,
    cache_file,
    logger,
    lock_timeout=2,
    ):
    """Update recon cache values

    :param cache_dict: Dictionary of cache key/value pairs to write out
    :param cache_file: cache file to update
    :param logger: the logger to use to log an encountered error
    :param lock_timeout: timeout (in seconds)
    """

    try:
        with lock_file(cache_file, lock_timeout, unlink=False) as cf:
            cache_entry = {}
            try:
                existing_entry = cf.readline()
                if existing_entry:
                    cache_entry = json.loads(existing_entry)
            except ValueError:

                # file doesn't have a valid entry, we'll recreate it

                pass
            for (cache_key, cache_value) in cache_dict.items():
                cache_entry[cache_key] = cache_value
            try:
                with NamedTemporaryFile(dir=os.path.dirname(cache_file),
                        delete=False) as tf:
                    tf.write(json.dumps(cache_entry) + '\n')
                os.rename(tf.name, cache_file)
            finally:
                try:
                    os.unlink(tf.name)
                except OSError, err:
                    if err.errno != errno.ENOENT:
                        raise
    except (Exception, Timeout):
        logger.exception(_('Exception dumping recon cache'))

def listdir(path):
    try:
        return os.listdir(path)
    except OSError, err:
        if err.errno != errno.ENOENT:
            raise
    return []

def streq_const_time(s1, s2):
    """Constant-time string comparison.

    :params s1: the first string
    :params s2: the second string

    :return: True if the strings are equal.

    This function takes two strings and compares them.  It is intended to be
    used when doing a comparison for authentication purposes to help guard
    against timing attacks.
    """

    if len(s1) != len(s2):
        return False
    result = 0
    for (a, b) in zip(s1, s2):
        result |= ord(a) ^ ord(b)
    return result == 0

def public(func):
    """
    Decorator to declare which methods are publicly accessible as HTTP
    requests

    :param func: function to make public
    """

    func.publicly_accessible = True

    @functools.wraps(func)
    def wrapped(*a, **kw):
        return func(*a, **kw)
    return wrapped

def rsync_ip(ip):
    """
    Transform ip string to an rsync-compatible form

    Will return ipv4 addresses unchanged, but will nest ipv6 addresses
    inside square brackets.

    :param ip: an ip string (ipv4 or ipv6)

    :returns: a string ip address
    """

    try:
        socket.inet_pton(socket.AF_INET6, ip)
    except socket.error:

                          # it's IPv4

        return ip
    else:
        return '[%s]' % ip

def get_valid_utf8_str(str_or_unicode):
    """
    Get valid parts of utf-8 str from str, unicode and even invalid utf-8 str

    :param str_or_unicode: a string or an unicode which can be invalid utf-8
    """

    if isinstance(str_or_unicode, unicode):
        (str_or_unicode, _len) = utf8_encoder(str_or_unicode, 'replace')
    (valid_utf8_str, _len) = utf8_decoder(str_or_unicode, 'replace')
    return valid_utf8_str.encode('utf-8')

def list_from_csv(comma_separated_str):
    """
    Splits the str given and returns a properly stripped list of the comma
    separated values.
    """

    if comma_separated_str:
        return [v.strip() for v in comma_separated_str.split(',')
                if v.strip()]
    return []

def csv_append(csv_string, item):
    """
    Appends an item to a comma-separated string.

    If the comma-separated string is empty/None, just returns item.
    """

    if csv_string:
        return ','.join((csv_string, item))
    else:
        return item

def reiterate(iterable):
    """
    Consume the first item from an iterator, then re-chain it to the rest of
    the iterator.  This is useful when you want to make sure the prologue to
    downstream generators have been executed before continuing.

    :param iterable: an iterable object
    """

    if isinstance(iterable, (list, tuple)):
        return iterable
    else:
        iterator = iter(iterable)
        try:
            chunk = ''
            while not chunk:
                chunk = next(iterator)
            return itertools.chain([chunk], iterator)
        except StopIteration:
            return []

class InputProxy(object):

    """
    File-like object that counts bytes read.
    To be swapped in for wsgi.input for accounting purposes.
    """

    def __init__(self, wsgi_input):
        """
        :param wsgi_input: file-like object to wrap the functionality of
        """

        self.wsgi_input = wsgi_input
        self.bytes_received = 0
        self.client_disconnect = False

    def read(self, *args, **kwargs):
        """
        Pass read request to the underlying file-like object and
        add bytes read to total.
        """

        try:
            chunk = self.wsgi_input.read(*args, **kwargs)
        except Exception:
            self.client_disconnect = True
            raise
        self.bytes_received += len(chunk)
        return chunk

    def readline(self, *args, **kwargs):
        """
        Pass readline request to the underlying file-like object and
        add bytes read to total.
        """

        try:
            line = self.wsgi_input.readline(*args, **kwargs)
        except Exception:
            self.client_disconnect = True
            raise
        self.bytes_received += len(line)
        return line


text:
var chai = require('chai')
  , sinon = require('sinon')
  , _ = require('../lib/utils')
  , MixinSubscribable = require('../lib/MixinSubscribable')
  , assert = chai.assert;

var SubscribableClass = _.defineClass(MixinSubscribable);

describe("MixinSubscribable", function() {
  describe("when instantiated", function() {
    it("should have an emitter", function() {
      var subscribable = new SubscribableClass();
      assert(subscribable.emitter instanceof _.EventEmitter, "Subscribable did not have an emitter");
    });

    it("should look like MixinSubscribable", function() {
      var subscribable = new SubscribableClass();

      assert.typeOf(subscribable.subscribe, "function");
      assert.typeOf(subscribable.trigger, "function");
    });
  });

  describe("the subscribe method", function() {
    describe("when passed invalid arguments", function() {
      it("should properly throw an error", function() {
        var subscribable = new SubscribableClass();

        assert.throws(function(){
          subscribable.subscribe();
        }, TypeError, "expected string event");

        assert.throws(function(){
          subscribable.subscribe("foobar");
        }, TypeError, "expected callback but found");
      });
    });

    describe("when passed valid arguments", function() {
      it("should subscribe and return a dispose method", function() {
        var subscribable = new SubscribableClass();
        assert.equal(subscribable.emitter.listeners("foobar").length, 0);

        var disposer = subscribable.subscribe("foobar", function() {});
        assert.typeOf(disposer, "function");
        assert.equal(subscribable.emitter.listeners("foobar").length, 1);

        disposer();
        assert.equal(subscribable.emitter.listeners("foobar").length, 0);
      });
    });
  });

  describe("the trigger method", function() {
    describe("when passed invalid arguments", function() {
      it("should properly throw an error", function() {
        var subscribable = new SubscribableClass();

        assert.throws(function(){
          subscribable.trigger();
        }, TypeError, "expected string event but found");

        assert.throws(function(){
          subscribable.trigger(123);
        }, TypeError, "expected string event but found");
      });
    });

    describe("when passed valid arguments", function() {
      it("should trigger a subscribed event", function() {
        var subscribable = new SubscribableClass()
          , callback1 = sinon.spy()
          , callback2 = sinon.spy();

        subscribable.subscribe("foobar", callback1);
        subscribable.subscribe("barfoo", callback2);
        assert.equal(callback1.callCount, 0);
        assert.equal(callback2.callCount, 0);

        subscribable.trigger("foobar");
        assert.equal(callback1.callCount, 1);
        assert.equal(callback2.callCount, 0);
      });

      it("should not trigger a disposed event", function() {
        var subscribable = new SubscribableClass()
          , callback1 = sinon.spy()
          , callback2 = sinon.spy();

        var disposer = subscribable.subscribe("foobar", callback1);
        subscribable.subscribe("barfoo", callback2);
        assert.equal(callback1.callCount, 0);
        assert.equal(callback2.callCount, 0);

        disposer();
        assert.equal(callback1.callCount, 0);
        assert.equal(callback2.callCount, 0);
      });
    });
  });
});


text:
package org.kuali.kfs.module.purap.businessobject.options;

import java.util.ArrayList;
import java.util.List;

import org.kuali.rice.core.api.util.ConcreteKeyValue;
import org.kuali.rice.krad.keyvalues.KeyValuesBase;

/**
 * Value Finder for Purchase Order Headers.
 */
public class PurchaseOrderHeaderValuesFinder extends KeyValuesBase {

    /**
     * @see org.kuali.rice.kns.lookup.keyvalues.KeyValuesFinder#getKeyValues()
     */
    public List getKeyValues() {
        List keyValues = new ArrayList();
        keyValues.add(new ConcreteKeyValue("Retransmission of Purchase Order", "Retransmission of Purchase Order"));
        keyValues.add(new ConcreteKeyValue("Purchase Order", "Purchase Order"));
        return keyValues;
    }
}