text:
//
// Created by Arvid on 17/12/13.
// Copyright (c) 2013 Mike Walker. All rights reserved.
//

#import "INKHandler.h"

@class INKActivityPresenter;

@interface INKGPlusHandler : INKHandler

/** Whether it should use a browser fallback or not */
@property (nonatomic) BOOL fallback;

/**
* Shows a specific profile / page
* @param userId The id of the profile / page
* @return A `INKActivityPresenter` object to present
*/
- (INKActivityPresenter *)showGPlusProfileWithId:(NSString *)userId;

/**
* Shows a specific profile / page
* @param userName The name of the profile / page. Without the +!
* @return A `INKActivityPresenter` object to present
*/
- (INKActivityPresenter *)showGPlusProfileWithName:(NSString *)userName;

/**
* Shows a specific event
* @param eventId The id of the event
* @return A `INKActivityPresenter` object to present
*/
- (INKActivityPresenter *)showGPlusEventWithId:(NSString *)eventId;

/**
* Shows a specific community
* @param communityId The id of the community
* @return A `MWActivityPresenter` object to present
*/
- (INKActivityPresenter *)showGPlusCommunityWithId:(NSString *)communityId;

@end


text:
package com.amazonaws.services.iot.model.transform;

import javax.annotation.Generated;

import com.amazonaws.SdkClientException;
import com.amazonaws.services.iot.model.*;

import com.amazonaws.protocol.*;
import com.amazonaws.annotation.SdkInternalApi;

/**
 * GetIndexingConfigurationRequestMarshaller
 */
@Generated("com.amazonaws:aws-java-sdk-code-generator")
@SdkInternalApi
public class GetIndexingConfigurationRequestMarshaller {

    private static final GetIndexingConfigurationRequestMarshaller instance = new GetIndexingConfigurationRequestMarshaller();

    public static GetIndexingConfigurationRequestMarshaller getInstance() {
        return instance;
    }

    /**
     * Marshall the given parameter object.
     */
    public void marshall(GetIndexingConfigurationRequest getIndexingConfigurationRequest, ProtocolMarshaller protocolMarshaller) {

        if (getIndexingConfigurationRequest == null) {
            throw new SdkClientException("Invalid argument passed to marshall(...)");
        }

        try {
        } catch (Exception e) {
            throw new SdkClientException("Unable to marshall request to JSON: " + e.getMessage(), e);
        }
    }

}

redpajama_set_name: RedPajamaGithub


text:
import sys
from django.db import models

from mint.django_rest.deco import D
from mint.django_rest.rbuilder import modellib
from mint.django_rest.rbuilder.users import models as usersmodels
from mint.django_rest.rbuilder.projects import models as projectsmodels
from mint.django_rest.rbuilder.rbac import models as rbacmodels
from mint.django_rest.rbuilder.querysets import errors
from mint.django_rest.rbuilder.jobs import models as jobmodels
from django.db.utils import IntegrityError
from xobj import xobj

APIReadOnly = modellib.APIReadOnly
XObjHidden = modellib.XObjHidden

OPERATOR_CHOICES = [(k, v) for k, v in modellib.filterTermMap.items()]

class Universe(modellib.XObjIdModel):
    '''Reference to the absolute all of a Given Type parent of any queryset'''
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(tag = "universe")
    view_name = "QuerySetUniverseResult"

class AllMembers(modellib.XObjIdModel):
    '''Query set results matched regardless of match type (below)'''
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag = "all_members")
    view_name = "QuerySetAllResult"

class ChosenMembers(modellib.XObjIdModel):
    '''Query set results based on resources explicitly added to the QS'''
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag = "chosen_members")
    view_name = "QuerySetChosenResult"

class FilteredMembers(modellib.XObjIdModel):
    '''Query set results based on given match criteria'''
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag = "filtered_members")
    view_name = "QuerySetFilteredResult"

class ChildMembers(modellib.XObjIdModel):
    '''Query set results that are selected because of a child query set'''
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag = "child_members")
    view_name = "QuerySetChildResult"

class GrantMatrix(modellib.XObjIdModel):
    '''Permissions on queryset arranged in a UI-friendly way'''
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag = "grant_matrix")
    view_name = "QuerySetGrantMatrix"

class FavoriteQuerySets(modellib.Collection):
    '''A list of all query sets in the rBuilder'''
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag = "favorite_query_sets")
    list_fields = ["query_set"]
    query_set = []
    view_name = 'FavoriteQuerySets'

class QuerySets(modellib.Collection):
    '''A list of all query sets in the rBuilder'''
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag = "query_sets")
    list_fields = ["query_set"]
    query_set = []

class OperatorChoices(modellib.UnpaginatedCollection):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(tag='operator_choices')
    list_fields = [ 'operator_choice' ]

    def __eq__(self, other):
        return isinstance(other, self.__class__) and \
                self.operator_choice == other.operator_choice

class OperatorChoice(modellib.XObjModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(tag='operator_choice')
    key = models.TextField()
    label = models.TextField()

    def __eq__(self, other):
        return isinstance(other, self.__class__) and \
                self.key == other.key and \
                self.label == other.label

class ValueOptions(modellib.UnpaginatedCollection):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(tag='value_options')
    list_fields = [ 'options' ]

    def __eq__(self, other):
        return isinstance(other, self.__class__) and \
                self.options == other.options

class FieldDescriptor(modellib.XObjModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(tag='field_descriptor')
    field_label = models.TextField()
    field_key = models.TextField()
    value_type = models.TextField()
    operator_choices = modellib.SyntheticField(OperatorChoices)
    value_options = modellib.SyntheticField(ValueOptions)

    def __eq__(self, other):
        return isinstance(other, self.__class__) and \
            self.field_label == other.field_label and \
            self.field_key == other.field_key and \
            self.value_type == other.value_type and \
            self.operator_choices == other.operator_choices and \
            self.value_options == other.value_options

class FieldDescriptors(modellib.UnpaginatedCollection):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(tag='field_descriptors')
    list_fields = [ 'field_descriptor' ]

class FilterDescriptor(modellib.XObjIdModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(tag='filter_descriptor',
        elements=['field_descriptors'],
        attributes={'id':str})
    view_name = 'QuerySetFilterDescriptor'
    field_descriptors = modellib.SyntheticField(FieldDescriptors)
    _querySetId = modellib.XObjHidden(models.TextField())

    url_key = [ '_querySetId' ]

class CollectionId(modellib.XObjIdModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(tag='collection')

class QuerySet(modellib.XObjIdModel):
    '''An individual queryset, ex: "All Systems"'''

    objects = modellib.SaveableManyToManyManager()
    _xobj = xobj.XObjMetadata(
                tag = "query_set")
    _xobj_explicit_accessors = set([])
    _m2m_safe_to_create = [ 'filter_entries' ]
    summary_view = [ 'name', 'description' ]

    query_set_id = D(models.AutoField(primary_key=True),
        "The database id for the query set")
    name = D(models.TextField(unique=True),
        "Query set name, must be unique", short="Queryset name")
    description = D(models.TextField(null=True),
        "Query set description, is null by default", short="Queryset description")
    created_date = D(modellib.DateTimeUtcField(auto_now_add=True),
        "Date the query set was created")
    modified_date = D(modellib.DateTimeUtcField(auto_now_add=True),
        "Date the query set was modified")
    tagged_date = D(modellib.DateTimeUtcField(auto_now_add=False, null=True),
        "Date the query set was last tagged")
    children = D(models.ManyToManyField("self", symmetrical=False),
        "Query sets that are children of this query set")
    filter_entries = D(models.ManyToManyField("FilterEntry"),
        "Defined filter entries for this query set")
    resource_type = D(models.TextField(),
        "Name of the resource this query set operates on")
    presentation_type = D(models.TextField(null=True),
        "A classification for client to use when displaying the objects.  For example, stages can be on projects, branches, platforms, etc.")
    can_modify = D(models.BooleanField(default=True),
        "Whether this query set can be deleted through the API.  Boolean, defaults to False")
    # TODO: make this an href field and not inline
    config_environments = D(models.ManyToManyField("ConfigEnvironment", through='QuerySetConfigEnvironment',
        symmetrical=False, related_name='querysets'), "Query sets that are children of this query set")

    actions = D(modellib.SyntheticField(jobmodels.Actions), 'Available actions on this query set')
    # public querysets are querysets like "All Systems" and do not require rbac ReadSet permissions
    # to be visible, but will be empty unless ReadMember(ship) is conveyed on some of their contents.
    is_public = APIReadOnly(models.BooleanField(default=False))
    is_static = APIReadOnly(models.BooleanField(default=False))
    created_by = APIReadOnly(modellib.ForeignKey('users.User', related_name='+', null=True, db_column='created_by'))
    modified_by = APIReadOnly(modellib.ForeignKey('users.User', related_name='+', null=True, db_column='modified_by'))
    personal_for = APIReadOnly(modellib.ForeignKey('users.User', related_name='+', db_column='personal_for', null=True))

    load_fields = [name]

    # the name of the most important key (default) in the filter descriptor
    _queryset_search_key_map = {
        'system'               : 'System name' ,
        'user'                 : 'User name'   ,
        'project'              : 'Project name',
        'project_branch_stage' : 'Project name',
        'grant'                : 'Grant name'  ,
        'role'                 : 'Role name'   ,
        'target'               : 'Target name' ,
        'image'                : 'Image name'  ,
    }

    def searchKey(self):
        '''The name of the most important item in the filter descriptor'''
        return self._queryset_search_key_map[self.resource_type]

    def computeSyntheticFields(self, sender, **kwargs):
        ''' Compute non-database fields.'''
        self._computeActions()

    def _computeActions(self):
        '''What actions are available on the system?'''

        self.actions = jobmodels.Actions()
        self.actions.action = []

        if self.tagged_date is not None:
            # refreshes the queryset next time it is accessed
            # regardless of resource tag (cache) age
            invalidate = jobmodels.EventType.makeAction(
                jobmodels.EventType.QUERYSET_INVALIDATE
            )
            self.actions.action.append(invalidate)

    def serialize(self, request=None, **kwargs):
        etreeModel = modellib.XObjIdModel.serialize(self, request, **kwargs)

        # whether the user can create any resource of this type or not
        # determines whether the UI should show the "+" button as enabled.
        # in RBAC 1.5, this logic will likely want to change.

        userCreatePermission = False
        user = getattr(request, '_authUser', None)
        if getattr(request, '_is_admin', False):
            userCreatePermission = True
        elif user is not None:
            matching_grants = rbacmodels.RbacPermission.objects.filter(
                queryset__resource_type=self.resource_type,
                role__rbacuserrole__user=user,
                permission__name='CreateResource'
            )
            if matching_grants.count() > 0:
                userCreatePermission = True

        modellib.Etree.Node('user_create_permission', parent=etreeModel,
                text=str(userCreatePermission).lower())

        am = AllMembers()
        am._parents = [self]
        etreeModel.append(am.serialize(request, tag='all_members'))
        cm = ChosenMembers()
        cm._parents = [self]
        etreeModel.append(cm.serialize(request, tag='chosen_members'))
        fm = FilteredMembers()
        fm._parents = [self]
        etreeModel.append(fm.serialize(request, tag='filtered_members'))
        childM = ChildMembers()
        childM._parents = [self]
        etreeModel.append(childM.serialize(request, tag='child_members'))
        universe = Universe()
        universe._parents = [self]
        etreeModel.append(universe.serialize(request, tag='universe'))
        grant_matrix = GrantMatrix()
        grant_matrix._parents = [self]
        etreeModel.append(grant_matrix.serialize(request, tag='grant_matrix'))

        fd = FilterDescriptor(_querySetId=self.query_set_id)
        etreeModel.append(fd.serialize(request, tag='filter_descriptor'))

        modellib.Etree.Node('is_top_level', text=str(self.isTopLevel()).lower(),
                parent=etreeModel)

        return etreeModel

    def getFilterBy(self):
        filterBy = []
        for filterEntry in self.filter_entries.all():
            filterStr = '[%s,%s,%s]' % (filterEntry.field, filterEntry.operator,
                filterEntry.value)
            filterBy.append(filterStr)
        return ','.join(filterBy)

    def isTopLevel(self):
        parents = self.__class__.children.through.objects.filter(to_queryset=self)
        if parents:
            return False
        else:
            return True

    def save(self, *args, **kwargs):
        """
        Validate that the query set does not have any circular relationships in
        it's children before saving it.  E.g., a query set can not be a child
        of one of it's children.
        """
        # If we don't have a query_set_id (pk), then we've never even been saved
        # and Django prevents us from using the Many to Many manager on
        # children.  Skip validating children in this case.
        if self.query_set_id is not None:
            self._validateChildren()
        return modellib.XObjIdModel.save(self, *args, **kwargs)

    def _validateChildren(self, validatedChildren=[]):
        """
        Validate the query set does not have any circular relationships in
        it's children.
        """
        # Save the current query set as validated
        validatedChildren.append(self)

        children = self.children.all()

        # A query set can not be in it's own children
        if self in children:
            raise errors.InvalidChildQuerySet(parent=self.name,
                child=self.name)

        # A query set can not be a child of any of it's children
        for childQuerySet in children:
            grandChildren = childQuerySet.children.all()
            if self in grandChildren:
                raise errors.InvalidChildQuerySet(parent=childQuerySet.name,
                    child=self.name)

            # Recurse, validating each child
            childQuerySet._validateChildren(validatedChildren)

    def parents(self):
        '''querysets that directly include this queryset'''
        results = QuerySet.objects.filter(
            children = self
        )
        return results

    def ancestors(self):
        '''querysets that directly or indirectly include this queryset'''
        return self._ancestors([], 0)

    def _ancestors(self, results, depth):
        self._depth = depth
        my_parents = self.parents()
        results.extend(my_parents)
        for parent in my_parents:
            results.extend(parent._ancestors(results, depth-1))
        return results

# TODO: collection, ConfigEnvironments
# RBAC based on union of querysets, etc, or just public?

class ConfigEnvironment(modellib.XObjIdModel):
    '''An individual queryset, ex: "All Systems"'''

    class Meta:
        db_table = 'config_environments'

    _xobj = xobj.XObjMetadata(tag = "config_environment")
    _xobj_explicit_accessors = set(['config_environment_settings'])

    config_environment_id = D(models.AutoField(primary_key=True, db_column='id'),
        "The database id for the config environment")
    name = D(models.TextField(unique=True),
        "Config environment name", short="Config environment name")
    description = D(models.TextField(unique=False),
        "Config environment description", short="Config environment description")
    descriptor = models.TextField(db_column='config_descriptor')
    created_by = D(models.ForeignKey('users.User', db_column='created_by', related_name='+'),
        "User who created the config environment", short="Config environment created by")
    modified_by = D(models.ForeignKey('users.User', db_column='modified_by', related_name='+'),
        "User who last modified the config environment", short="Config environment modified by")
    created_date = D(modellib.DateTimeUtcField(auto_now_add=True),
        "Date the query set was created")
    modified_date = D(modellib.DateTimeUtcField(auto_now_add=True),
        "Date the query set was modified")

class QuerySetConfigEnvironment(modellib.XObjIdModel):
    ''' M2M mapping table '''
    class Meta:
        db_table = 'querysets_queryset_config_environments'

    queryset_config_environment_id = models.AutoField(primary_key=True, db_column='id')
    config_environment = models.ForeignKey(ConfigEnvironment, db_column='config_environment_id')
    queryset = models.ForeignKey('querysets.QuerySet', db_column='queryset_id')

class ConfigEnvironmentSetting(modellib.XObjIdModel):

    class Meta:
        db_table = 'config_environment_config_values'

    _xobj = xobj.XObjMetadata(tag='config_environment_setting')

    config_setting_id = models.AutoField(primary_key=True, db_column='id')
    config_environment = modellib.ForeignKey(ConfigEnvironment, related_name='config_environment_settings')
    key = models.TextField(null=True)
    value = models.TextField(null=True)

class FilterEntry(modellib.XObjIdModel):

    class Meta:
        unique_together = ('field', 'operator', 'value')

    _xobj = xobj.XObjMetadata(
                tag = "filter_entry")
    _xobj_hidden_accessors = set(['links'])

    filter_entry_id = models.AutoField(primary_key=True)
    field = D(models.TextField(),
        "Field this filter operates on.  '.' in field names indicate resource relationships")
    operator = D(models.TextField(choices=OPERATOR_CHOICES),
        "Operator for this filter")
    value = D(models.TextField(null=True),
        "Value for this filter, defaults to null")

    load_fields = [field, operator, value]

class FilterEntryLink(modellib.XObjIdModel):

    class Meta:
        db_table = 'querysets_queryset_filter_entries'

    id = models.AutoField(primary_key=True)
    queryset = models.ForeignKey(QuerySet, related_name='+')
    filter_entry = models.ForeignKey(FilterEntry,
        db_column='filterentry_id', related_name='links')

class InclusionMethod(modellib.XObjIdModel):
    '''
    Explains how the system came to be in the query set.  This is probably
    not needed and could be removed for a performance boost.  [MPD]
    '''
    _xobj = xobj.XObjMetadata(
                tag = 'inclusion_method')
    _xobj_explicit_accessors = set([])

    METHOD_CHOICES = [
        ('chosen', 'Chosen'),
        ('filtered', 'Filtered'),
    ]

    inclusion_method_id = models.AutoField(primary_key=True)
    name = models.TextField(choices=METHOD_CHOICES)

    load_fields = [name]

class SystemTag(modellib.XObjIdModel):
    '''
    Indicates what systems were matched by a query set
    '''

    class Meta:
        unique_together = (("system", "query_set", "inclusion_method"),)

    _xobj = xobj.XObjMetadata(
                tag = 'system_tag')

    system_tag_id = D(models.AutoField(primary_key=True), 'The ID of the system tag')
    system = XObjHidden(modellib.ForeignKey('inventory.System',
        related_name="tags"))
    query_set = XObjHidden(modellib.ForeignKey(QuerySet, related_name="system_tags",
        text_field="name"))
    inclusion_method = XObjHidden(modellib.ForeignKey(InclusionMethod,
        related_name="+"))

    load_fields = [system, query_set, inclusion_method]

    def get_absolute_url(self, *args, **kwargs):
        self._parents = [self.system, self]
        return modellib.XObjIdModel.get_absolute_url(self, *args, **kwargs)

class ImageTag(modellib.XObjIdModel):
    '''
    Indicates what systems were matched by a query set
    '''

    class Meta:
        unique_together = (("image", "query_set", "inclusion_method"),)

    _xobj = xobj.XObjMetadata(
                tag = 'image_tag')

    image_tag_id = D(models.AutoField(primary_key=True), 'The ID of the image tag')
    image = XObjHidden(modellib.ForeignKey('images.Image',
        related_name="tags"))
    query_set = XObjHidden(modellib.ForeignKey(QuerySet, related_name="image_tags",
        text_field="name"))
    inclusion_method = XObjHidden(modellib.ForeignKey(InclusionMethod,
        related_name="+"))

    load_fields = [image, query_set, inclusion_method] # TODO: determine if needed?

class TargetTag(modellib.XObjIdModel):
    '''
    Indicates what targets were matched by a query set
    '''

    class Meta:
        unique_together = (("target", "query_set", "inclusion_method"),)

    _xobj = xobj.XObjMetadata(
                tag = 'target_tag')

    target_tag_id = D(models.AutoField(primary_key=True), 'The ID of the target tag')
    target = XObjHidden(modellib.ForeignKey('targets.Target',
        related_name="tags"))
    query_set = XObjHidden(modellib.ForeignKey(QuerySet, related_name="target_tags",
        text_field="name"))
    inclusion_method = XObjHidden(modellib.ForeignKey(InclusionMethod,
        related_name="+"))

    load_fields = [target, query_set, inclusion_method]

# TODO: reduce duplication here, add a common tag base class or factory?
class RoleTag(modellib.XObjIdModel):
    '''
    Indicates what roles were matched by a query set
    '''

    class Meta:
        unique_together = (("role", "query_set", "inclusion_method"),)

    _xobj = xobj.XObjMetadata(
                tag = 'role_tag')

    role_tag_id = D(models.AutoField(primary_key=True), 'The ID of the role tag')
    role = XObjHidden(modellib.ForeignKey(rbacmodels.RbacRole,
        related_name="tags"))
    query_set = XObjHidden(modellib.ForeignKey(QuerySet, related_name="role_tags",
        text_field="name"))
    inclusion_method = XObjHidden(modellib.ForeignKey(InclusionMethod,
        related_name="+"))

    load_fields = [role, query_set, inclusion_method]

# TODO: reduce duplication here, add a common tag base class or factory?
class PermissionTag(modellib.XObjIdModel):
    '''
    Indicates what roles were matched by a query set
    '''

    class Meta:
        unique_together = (("permission", "query_set", "inclusion_method"),)

    _xobj = xobj.XObjMetadata(
                tag = 'grant_tag')

    # NOTE: we call permissions "GRANTS" in the XML api, though these
    # tags will largely be hidden here, and we can call fields by their
    # internal names, knowing this data need not be surfaced.
    permission_tag_id = D(models.AutoField(primary_key=True), 'The ID of a permission tag')
    permission = XObjHidden(modellib.ForeignKey(rbacmodels.RbacPermission,
        related_name="tags"))
    query_set = XObjHidden(modellib.ForeignKey(QuerySet, related_name="permission_tags",
        text_field="name"))
    inclusion_method = XObjHidden(modellib.ForeignKey(InclusionMethod,
        related_name="+"))

    load_fields = [permission, query_set, inclusion_method]

class UserTag(modellib.XObjIdModel):
    '''Indicates what users were matched by a query set'''

    class Meta:
        unique_together = (('user', 'query_set', 'inclusion_method'),)

    _xobj = xobj.XObjMetadata(tag='user_tag')

    user_tag_id = D(models.AutoField(primary_key=True), 'The ID of a user tag')
    user = D(modellib.ForeignKey(usersmodels.User, related_name='tags'), 'User having tag')
    query_set = XObjHidden(
        modellib.ForeignKey(QuerySet, related_name='user_tags', text_field='name')
    )
    inclusion_method = XObjHidden(
        modellib.ForeignKey(InclusionMethod, related_name='+')
    )

    load_fields = [user, query_set, inclusion_method]

class ProjectTag(modellib.XObjIdModel):
    '''Indicates what projects were matched by a query set'''

    class Meta:
        unique_together = (('project', 'query_set', 'inclusion_method'),)

    _xobj = xobj.XObjMetadata(tag='project_tag')

    project_tag_id = D(models.AutoField(primary_key=True), 'ID of the project tag')
    project = D(modellib.ForeignKey(projectsmodels.Project, related_name='tags'),
        'Project attached to the tag')
    query_set = XObjHidden(
        modellib.ForeignKey(QuerySet, related_name='project_tags', text_field='name')
    )
    inclusion_method = XObjHidden(
       modellib.ForeignKey(InclusionMethod, related_name='+')
    )

    load_fields = [project, query_set, inclusion_method]

class StageTag(modellib.XObjIdModel):
    '''Indicates which stages were matched by a query set'''

    class Meta:
        unique_together = (('stage', 'query_set', 'inclusion_method'),)

    _xobj = xobj.XObjMetadata(tag='stage_tag')

    stage_tag_id = D(models.AutoField(primary_key=True), 'The id of a stage tag')
    stage = D(modellib.ForeignKey(projectsmodels.Stage, related_name='tags'),
        'Associated stage')
    query_set = XObjHidden(
        modellib.ForeignKey(QuerySet, related_name='stage_tags', text_field='name')
    )
    inclusion_method = XObjHidden(
        modellib.ForeignKey(InclusionMethod, related_name='+')
    )

    load_fields = [stage, query_set, inclusion_method]
    # Queryset resource_type is 'project_branch_stage' but the field here is
    # just called 'stage', so a special override is needed.
    tagged_field = 'stage'

# register xobj metadata
for mod_obj in sys.modules[__name__].__dict__.values():
    if hasattr(mod_obj, '_xobj'):
        if mod_obj._xobj.tag:
            modellib.type_map[mod_obj._xobj.tag] = mod_obj


text:
package io.crnk.core.module.discovery;

import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.List;

public class EmptyServiceDiscovery implements ServiceDiscovery {

	@Override
	public <T> List<T> getInstancesByType(Class<T> clazz) {
		return Collections.emptyList();
	}

	@Override
	public <A extends Annotation> List<Object> getInstancesByAnnotation(Class<A> annotation) {
		return Collections.emptyList();
	}
}


text:
package eu.smartenit.unada.db.dao.impl;

import java.util.Iterator;
import java.util.Properties;
import java.util.List;

import org.skife.jdbi.v2.DBI;

import eu.smartenit.unada.db.dao.AbstractVideoInfoDAO;
import eu.smartenit.unada.db.dao.util.Constants;
import eu.smartenit.unada.db.dto.VideoInfo;

public class VideoInfoDAO {
	final AbstractVideoInfoDAO dao;

	public VideoInfoDAO() {
		Properties connectionProperties = new Properties();
		DBI dbi = new DBI(Constants.DBI_URL, connectionProperties);
		dao = dbi.onDemand(AbstractVideoInfoDAO.class);
	}

	public void createTable() {
		dao.createTable();
	}

	public void deleteTable() {
		dao.deleteTable();
	}

	public void insert(VideoInfo v) {
		dao.insert(v);
	}

	public synchronized void insertAll(Iterator<VideoInfo> vList) {
		dao.insertAll(vList);
	}

	public void update(VideoInfo v) {
		dao.update(v);
	}

	public List<VideoInfo> findAll() {
		return dao.findAll();
	}

	public VideoInfo findById(long contentID) {
		return dao.findById(contentID);
	}

	public void deleteAll() {
		dao.deleteAll();
	}

	public void delete(long contentID) {
		dao.delete(contentID);
	}

}


text:
'use strict';

// Setting up route
angular.module('products').config(['$stateProvider','RouteHelpersProvider',
	function($stateProvider,helper) {
		// Products state routing
		$stateProvider.
		state('app.listProducts', {
			url: '/products',
			title: '商品列表',
			templateUrl: 'modules/products/views/list.html',
			resolve:helper.resolveFor('datatables')
		}).
		state('app.createProduct', {
			url: '/products/create',
			title: '新增商品',
			templateUrl: 'modules/products/views/create.html'
		}).
		state('app.viewProduct', {
			url: '/products/:productId',
			title: '查看商品',
			templateUrl: 'modules/products/views/view.html',
			controller: 'ProductsViewController',
			controllerAs: 'vm'
		}).
		state('app.editProduct', {
			title: '编辑商品',
			url: '/products/:productId/edit',
			templateUrl: 'modules/products/views/edit.html'
		});
	}
]);


text:
package com.jayantkrish.jklol.training;

import com.jayantkrish.jklol.models.parametric.SufficientStatistics;

/**
 * Oracle for the steps of the {@link ExpectationMaximization} algorithm.
 *
 * @author jayantk
 * @param <M> instantiated model type
 * @param <E> example type
 * @param <O> expectation type
 * @param  accumulator type for expectations
 */
public interface EmOracle<M, E, O, A> {

  /**
   * Instantiates any sort of intermediate data structure that may assist in
   * performing inference. For example, this could construct a
   * {@code FactorGraph} for {@code parameters}.
   *

   * If no such initialization is necessary, simply return {@code parameters}.
   *
   * @param parameters
   */
  public M instantiateModel(SufficientStatistics parameters);

  /**
   * Gets a data structure used to aggregate expectations of type O.
   *
   * @return
   */
  public A getInitialExpectationAccumulator();

  /**
   * Combines two accumulators into a single accumulator. May
   * mutate both accumulators.
   *
   * @param accumulator1
   * @param accumulator2
   * @return
   */
  public A combineAccumulators(A accumulator1, A accumulator2);

  /**
   * E-step of the Expectation-Maximization algorithm. Computes
   * expectations for {@code example} under {@code model} and
   * adds the expectations to {@code accumulator}. Returns the
   * accumulated expectations.
   *
   * @param model
   * @param currentParameters
   * @param example
   * @param accumulator
   * @param log
   * @return
   */
  public A computeExpectations(M model, SufficientStatistics currentParameters,
      E example, A accumulator, LogFunction log);

  /**
   * M-step of the Expectation-Maximization algorithm. Re-estimates parameters
   * from the model expectations computed in the E-step.
   *

   * This method may mutate and return {@code currentParameters} to avoid
   * unnecessary copying.
   *
   * @param expectationAccumulator
   * @param currentParameters
   * @return
   */
  public SufficientStatistics maximizeParameters(A expectationAccumulator,
      SufficientStatistics currentParameters, LogFunction log);
}


text:
package com.ibm.streamsx.topology.internal.context;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;

import com.ibm.json.java.JSONArray;
import com.ibm.json.java.JSONObject;
import com.ibm.streamsx.topology.Topology;
import com.ibm.streamsx.topology.context.ContextProperties;
import com.ibm.streamsx.topology.generator.spl.SPLGenerator;
import com.ibm.streamsx.topology.internal.process.CompletedFuture;

public class ToolkitStreamsContext extends StreamsContextImpl<File> {

    Map<String, Object> graphItems;

    @Override
    public Type getType() {
        return Type.TOOLKIT;
    }

    @Override
    public Future<File> submit(Topology app, Map<String, Object> config)
            throws Exception {

        if (config == null)
            config = new HashMap<>();

        // If the toolkit path is not given, then create one in the
        // currrent directory.
        if (!config.containsKey(ContextProperties.TOOLKIT_DIR)) {
            config.put(ContextProperties.TOOLKIT_DIR, Files
                    .createTempDirectory(Paths.get(""), "tk").toAbsolutePath().toString());
        }

        File toolkitRoot = new File((String) config.get(ContextProperties.TOOLKIT_DIR));

        makeDirectoryStructure(toolkitRoot,
                (String) app.builder().json().get("namespace"));

        graphItems = app.finalizeGraph(getType(), config);

        addConfigToJSON(app.builder().getConfig(), config);

        generateSPL(app, config);
        return new CompletedFuture<File>(toolkitRoot);
    }

    protected void addConfigToJSON(JSONObject graphConfig, Map<String,Object> config) {

        for (String key : config.keySet()) {
            Object value = config.get(key);

            if (key.equals(ContextProperties.SUBMISSION_PARAMS)) {
                // value causes issues below and no need to add this to json
                continue;
            }
            if (JSONObject.isValidObject(value)) {
                graphConfig.put(key, value);
                continue;
            }
            if (value instanceof Collection) {
                JSONArray ja = new JSONArray();
                @SuppressWarnings("unchecked")
                Collection<Object> coll = (Collection<Object>) value;
                ja.addAll(coll);
                graphConfig.put(key, ja);
            }
        }
    }

    public static void main(String[] args) {
        JSONArray ja = new JSONArray();
        ja.add(new Integer(22));
    }

    private void generateSPL(Topology app, Map<String, Object> config)
            throws IOException {

        JSONObject json = app.builder().complete();

        // Create the SPL file, and save a copy of the JSON file.
        SPLGenerator generator = new  SPLGenerator();
        createNamespaceFile(json, config, "spl", generator.generateSPL(json));
        createNamespaceFile(json, config, "json", json.serialize());
    }

    private void createNamespaceFile(JSONObject json,
            Map<String, Object> config, String suffix, String content)
            throws IOException {

        String namespace = (String) json.get("namespace");
        String name = (String) json.get("name");

        File f = new File((String) config.get(ContextProperties.TOOLKIT_DIR),
                namespace + "/" + name + "." + suffix);
        PrintWriter splFile = new PrintWriter(f, "UTF-8");
        // splFile.print(app.splgraph().toSPLString());
        splFile.print(content);
        splFile.flush();
        splFile.close();
    }

    private void makeDirectoryStructure(File toolkitRoot, String namespace)
            throws Exception {

        File tkNamespace = new File(toolkitRoot, namespace);
        File tkImplLib = new File(toolkitRoot, "impl/lib");
        File tkEtc = new File(toolkitRoot, "etc");
        File tkOpt = new File(toolkitRoot, "opt");

        tkImplLib.mkdirs();
        tkNamespace.mkdirs();
        tkEtc.mkdir();
        tkOpt.mkdir();
    }

}


text:
const disableHandlersList = new WeakSet();

const mapIterator = f => iterator => ({
  next() {
    const next = iterator.next();
    return {
      value: next.done ? undefined : f(next.value),
      done: next.done,
    };
  },
  [Symbol.iterator]() {
    return this;
  },
});

const convertMethod = (method, target) => {
  const decrementIfPositive = number => (number > 0 ? number - 1 : number);
  const transformArgsMap = {
    concat: (...args) => args.map(x => (x instanceof target.constructor ? Array.from(x) : x)),
    includes: (searchElement, fromIndex = 1) => [searchElement, decrementIfPositive(fromIndex)],
    slice: (begin, end) => [decrementIfPositive(begin), decrementIfPositive(end)],
    indexOf: (searchElement, fromIndex = 1) => [searchElement, decrementIfPositive(fromIndex)],
    lastIndexOf: (searchElement, fromIndex = Infinity) =>
      [searchElement, decrementIfPositive(fromIndex)],
    copyWithin: (...args) => args.map(decrementIfPositive),
    fill: (value, start = 1, end = target.length + 1) =>
      [value, decrementIfPositive(start), decrementIfPositive(end)],
    splice: (start, ...rest) => [decrementIfPositive(start), ...rest],
    reduce: (callback, ...rest) => [(a, b, i, array) =>
      callback(a, b, i + 1, Reflect.construct(target.constructor, array)),
    ...rest],
  };
  transformArgsMap.reduceRight = transformArgsMap.reduce;
  const callbackList = ['forEach', 'every', 'some', 'find', 'filter', 'findIndex', 'map'];
  const convertReturnedValueList = ['concat', 'slice', 'copyWithin', 'fill', 'reverse', 'sort', 'splice', 'filter', 'map'];
  const indexOfList = ['indexOf', 'lastIndexOf', 'findIndex'];
  const generators = {
    keys: mapIterator(i => i + 1),
    entries: mapIterator(([i, x]) => [i + 1, x]),
  };
  return (...args) => {
    disableHandlersList.add(target);

    let transformedArgs = args;
    if (Object.prototype.hasOwnProperty.call(transformArgsMap, method)) {
      transformedArgs = transformArgsMap[method](...args);
    } else if (callbackList.includes(method)) {
      transformedArgs = [(currentValue, index, array) => Reflect.apply(
        args[0], args[1],
        [currentValue, index + 1, Reflect.construct(target.constructor, array)],
      )];
    }

    let result = Reflect.apply(target[method], target, transformedArgs);
    if (convertReturnedValueList.includes(method)) {
      result = Reflect.construct(target.constructor, Array.from(result));
    } else if (indexOfList.includes(method)) {
      result = result === -1 ? result : result + 1;
    } else if (Object.prototype.hasOwnProperty.call(generators, method)) {
      result = generators[method](result);
    }

    disableHandlersList.delete(target);

    return result;
  };
};
export default class OneBasedArray extends Array {
  constructor(...args) {
    super(...args);

    const convertHelper = (property, back = false) => {
      if (typeof property === 'symbol') return property;
      const parsed = parseInt(property, 10);
      const maxArrayLength = 4294967295;
      if (!Number.isNaN(parsed) && Number.isFinite(parsed)
      && parsed >= 0 && parsed <= maxArrayLength) {
        return back ? String(parsed + 1) : parsed - 1;
      }
      return property;
    };
    const convertProperty = property => convertHelper(property);
    const convertBack = property => convertHelper(property, true);

    return new Proxy(this, {
      get(target, property, receiver) {
        const methodList = ['concat', 'includes', 'join', 'slice', 'toString', 'toLocaleString', 'indexOf', 'lastIndexOf',
          'copyWithin', 'fill', 'pop', 'push', 'reverse', 'shift', 'unshift', 'sort', 'splice', 'forEach', 'every', 'some',
          'find', 'filter', 'findIndex', 'map', 'reduce', 'reduceRight', 'keys', 'values', 'entries'];
        return methodList.includes(property)
          ? convertMethod(property, target)
          : Reflect.get(target, convertProperty(property), receiver);
      },
      set(target, property, value, receiver) {
        disableHandlersList.add(target);
        const result = Reflect.set(target, convertProperty(property), value, receiver);
        disableHandlersList.delete(target);
        return result;
      },
      has(target, property) {
        return Reflect.has(target, convertProperty(property));
      },
      deleteProperty(target, property) {
        return Reflect.deleteProperty(target, convertProperty(property));
      },
      ownKeys(target) {
        return Reflect.ownKeys(target).map(convertBack);
      },
      getOwnPropertyDescriptor(target, property) {
        const prop = disableHandlersList.has(target) ? property : convertProperty(property);
        return Reflect.getOwnPropertyDescriptor(target, prop);
      },
    });
  }
  values() {
    return mapIterator((([, x]) => x))(this.entries());
  }
  [Symbol.iterator]() {
    return this.values();
  }
}


text:
namespace fiber_bundle
{

  using namespace sheaf;

///
/// Nodal connectivity for a block containing zones of type triangle.
///
class SHEAF_DLL_SPEC triangle_connectivity : public block_connectivity
{

public:
  // ===========================================================
  /// @name TRIANGLE_CONNECTIVITY FACET
  // ===========================================================
  //@{

public:

  ///
  /// Creates an empty instance.
  ///
  triangle_connectivity();

  ///
  /// Copy constructor
  ///
  triangle_connectivity(const triangle_connectivity& xother);

  ///
  /// Creates an instance corresponding to the general arrangement
  /// of vertices given by node_ids() == xnode_ids,
  /// and node_id_ct() == xnode_id_ct. If xnode_ct == 0,
  /// node_ct() will be computed from node_ids(), otherwise,
  /// node_ct() == xnode_ct.
  ///
  triangle_connectivity(const pod_index_type* xnode_ids,
			size_type xnode_id_ct,
			size_type xnode_ct);

  ///
  /// Creates an instance corresponding to a rectangular array of
  /// vertices xi_size + 1 by  x_jsize + 1, that is, xi_size and xj_size
  /// are the number of edges in the i and j direction, respectively.
  /// Xstart_id is the id of the first vertex, that is, i=0, j=0.
  /// If xlower_left_to_upper_right_diagonals == true (default), the triangles
  /// are created by dividing a quad with the diagonal from the lower left to
  /// upper right corner; otherwise the other diagonal is used.
  ///
  triangle_connectivity(size_type xi_size,
                        size_type xj_size,
                        pod_index_type xstart_id = 0,
                        bool xlower_left_to_upper_right_diagonals = true);

  ///
  /// Destructor.
  ///
  virtual ~triangle_connectivity();

  ///
  /// Static const integral data members.
  /// @hack Inline initialization of static const integral data
  /// members causes link error in MSC++; use enum instead.
  ///
  enum static_const_int {NODES_PER_ELEMENT = 3};

protected:

  ///
  /// Allocates and initializes the connectivity array.
  ///
  void create_connectivity(size_type xi_size,
			   size_type xj_size,
			   pod_index_type xstart_id,
			   bool xlower_left_to_upper_right_diagonals);

private:

  //@}

  // ===========================================================
  /// @name BLOCK_CONNECTIVITY FACET
  // ===========================================================
  //@{

public:

protected:

private:

  //@}

  // ===========================================================
  /// @name BLOCK_RELATION FACET
  // ===========================================================
  //@{

public:

  ///
  /// The element type.
  ///
  virtual cell_type element_type() const;

protected:

private:

  //@}

  // ===========================================================
  /// @name ANY FACET
  // ===========================================================
  //@{

public:

  ///
  /// Virtual constructor, makes a new
  /// instance of the same type as this
  ///
  virtual triangle_connectivity* clone() const;

  ///
  /// Class invariant.
  ///
  virtual bool invariant() const;

  ///
  /// Conformance test; true if other conforms to this
  ///
  virtual bool is_ancestor_of(const any* other) const;

protected:

private:

  //@}

};

} // namespace fiber_bundle

#endif // ifndef TRIANGLE_CONNECTIVITY_H


text:
"""
Servers to assist in testing
"""
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import logging
import tempfile
import shlex
import subprocess
import socket

import requests

import tests.utils as utils

ga4ghPort = 8001
remotePort = 8002
oidcOpPort = 8443

class ServerForTesting(object):
    """
    The base class of a test server
    """
    def __init__(self, port, protocol='http',
                 subdirectory=None, pingStatusCode=200):
        # suppress requests package log messages
        logging.getLogger("requests").setLevel(logging.CRITICAL)
        self.port = port
        self.subdirectory = subdirectory
        self.pingStatusCode = pingStatusCode
        self.outFile = None
        self.errFile = None
        self.server = None
        self.serverUrl = "{}://{}:{}".format(protocol,
                                             socket.gethostname(),
                                             self.port)

    def getUrl(self):
        """
        Return the url at which the server is configured to run
        """
        return self.serverUrl

    def getCmdLine(self):
        """
        Return the command line string used to launch the server.
        Subclasses must override this method.
        """
        raise NotImplementedError()

    def start(self):
        """
        Start the server
        """
        assert not self.isRunning(), "Another server is running at {}".format(
            self.serverUrl)
        self.outFile = tempfile.TemporaryFile()
        self.errFile = tempfile.TemporaryFile()
        splits = shlex.split(self.getCmdLine())
        self.server = subprocess.Popen(
            splits, stdout=self.outFile,
            stderr=self.errFile,
            cwd=self.subdirectory)
        self._waitForServerStartup()

    def shutdown(self):
        """
        Shut down the server
        """
        if self.isRunning():
            self.server.kill()
        if self.server is not None:
            self.server.wait()
            self._assertServerShutdown()
        if self.outFile is not None:
            self.outFile.close()
        if self.errFile is not None:
            self.errFile.close()

    def restart(self):
        """
        Restart the server
        """
        self.shutdown()
        self.start()

    def isRunning(self):
        """
        Returns true if the server is running, false otherwise
        """
        try:
            response = self.ping()
            if response.status_code != self.pingStatusCode:
                msg = ("Ping of server returned unexpected status code "
                       "({})").format(response.status_code)
                assert False, msg
            return True
        except requests.ConnectionError:
            return False

    def ping(self):
        """
        Pings the server by doing a GET request to /
        """
        response = requests.get(self.serverUrl, verify=False)
        return response

    def getOutLines(self):
        """
        Return the lines of the server stdout file
        """
        return utils.getLinesFromLogFile(self.outFile)

    def getErrLines(self):
        """
        Return the lines of the server stderr file
        """
        return utils.getLinesFromLogFile(self.errFile)

    def printDebugInfo(self):
        """
        Print debugging information about the server
        """
        className = self.__class__.__name__
        print('\n')
        print('*** {} CMD ***'.format(className))
        print(self.getCmdLine())
        print('*** {} STDOUT ***'.format(className))
        print(''.join(self.getOutLines()))
        print('*** {} STDERR ***'.format(className))
        print(''.join(self.getErrLines()))

    @utils.Timeout()
    @utils.Repeat()
    def _waitForServerStartup(self):
        self.server.poll()
        if self.server.returncode is not None:
            self._waitForErrLines()
            message = "Server process unexpectedly died; stderr: {0}"
            failMessage = message.format(''.join(self.getErrLines()))
            assert False, failMessage
        return not self.isRunning()

    @utils.Timeout()
    @utils.Repeat()
    def _waitForErrLines(self):
        # not sure why there's some delay in getting the server
        # process' stderr (at least for the ga4gh server)...
        return self.getErrLines() == []

    def _assertServerShutdown(self):
        shutdownString = "Server did not shut down correctly"
        assert self.server.returncode is not None, shutdownString
        assert not self.isRunning(), shutdownString

class Ga4ghServerForTesting(ServerForTesting):
    """
    A ga4gh test server
    """
    def __init__(self, useOidc=False):
        protocol = 'https' if useOidc else 'http'
        super(Ga4ghServerForTesting, self).__init__(ga4ghPort, protocol)
        self.configFile = None
        self.useOidc = useOidc

    def getConfig(self):
        config = """
SIMULATED_BACKEND_NUM_VARIANT_SETS = 10
SIMULATED_BACKEND_VARIANT_DENSITY = 1
DATA_SOURCE = "simulated://"
DEBUG = True
"""
        if self.useOidc:
            config += """
TESTING = True
OIDC_PROVIDER = "https://localhost:{0}"
""".format(oidcOpPort)
        return config

    def getCmdLine(self):
        if self.configFile is None:
            self.configFile = tempfile.NamedTemporaryFile()
        config = self.getConfig()
        self.configFile.write(config)
        self.configFile.flush()
        configFilePath = self.configFile.name
        cmdLine = """
python server_dev.py
--dont-use-reloader
--disable-urllib-warnings
--host 0.0.0.0
--config TestConfig
--config-file {}
--port {} """.format(configFilePath, self.port)
        return cmdLine

    def shutdown(self):
        super(Ga4ghServerForTesting, self).shutdown()
        if self.configFile is not None:
            self.configFile.close()

    def printDebugInfo(self):
        super(Ga4ghServerForTesting, self).printDebugInfo()
        className = self.__class__.__name__
        print('*** {} CONFIG ***'.format(className))
        print(self.getConfig())

class Ga4ghServerForTestingDataSource(Ga4ghServerForTesting):
    """
    A test server that reads data from a data source
    """
    def __init__(self, dataDir):
        super(Ga4ghServerForTestingDataSource, self).__init__()
        self.dataDir = dataDir

    def getConfig(self):
        config = """
DATA_SOURCE = "{}"
DEBUG = True""".format(self.dataDir)
        return config

class OidcOpServerForTesting(ServerForTesting):
    """
    Runs a test OP server on localhost
    """
    def __init__(self):
        super(OidcOpServerForTesting, self).__init__(
            oidcOpPort, protocol="https",
            subdirectory="oidc-provider/simple_op",
            pingStatusCode=404)

    def getCmdLine(self):
        return ("python src/run.py --base https://localhost:{}" +
                " -p {} -d settings.yaml").format(oidcOpPort, oidcOpPort)


text:
from time import sleep
import mplayer as mp

bg = mp.Player()

bg.osdlevel = 0
bg.loadfile('/home/debian/videos/test.mpg')
bg.fullscreen=True

bg.loop=20

sleep(5)

while True:
    userInput = raw_input('when?')
    if userInput == '':
	userInput = 0
    else:
	userInput = float(userInput)

    bg.time_pos=userInput


text:
import logging
import mock
import os
import shutil
import tempfile
from future.tests.base import unittest

from mobly.controllers import android_device

from tests.lib import mock_android_device

# Mock log path for a test run.
MOCK_LOG_PATH = "/tmp/logs/MockTest/xx-xx-xx_xx-xx-xx/"
# The expected result of the cat adb operation.
MOCK_ADB_LOGCAT_CAT_RESULT = [
    "02-29 14:02:21.456  4454  Something\n",
    "02-29 14:02:21.789  4454  Something again\n"
]
# A mockd piece of adb logcat output.
MOCK_ADB_LOGCAT = ("02-29 14:02:19.123  4454  Nothing\n"
                   "%s"
                   "02-29 14:02:22.123  4454  Something again and again\n"
                   ) % ''.join(MOCK_ADB_LOGCAT_CAT_RESULT)
# Mock start and end time of the adb cat.
MOCK_ADB_LOGCAT_BEGIN_TIME = "02-29 14:02:20.123"
MOCK_ADB_LOGCAT_END_TIME = "02-29 14:02:22.000"
MOCK_SNIPPET_PACKAGE_NAME = "com.my.snippet"

# A mock SnippetClient used for testing snippet management logic.
MockSnippetClient = mock.MagicMock()
MockSnippetClient.package = MOCK_SNIPPET_PACKAGE_NAME

class AndroidDeviceTest(unittest.TestCase):
    """This test class has unit tests for the implementation of everything
    under mobly.controllers.android_device.
    """
    def setUp(self):
        # Set log_path to logging since mobly logger setup is not called.
        if not hasattr(logging, "log_path"):
            setattr(logging, "log_path", "/tmp/logs")
        # Creates a temp dir to be used by tests in this test class.
        self.tmp_dir = tempfile.mkdtemp()

    def tearDown(self):
        """Removes the temp dir.
        """
        shutil.rmtree(self.tmp_dir)

    # Tests for android_device module functions.
    # These tests use mock AndroidDevice instances.

    @mock.patch.object(
        android_device,
        "get_all_instances",
        new=mock_android_device.get_all_instances)
    @mock.patch.object(
        android_device,
        "list_adb_devices",
        new=mock_android_device.list_adb_devices)
    @mock.patch.object(
        android_device,
        "list_adb_devices_by_usb_id",
        new=mock_android_device.list_adb_devices)
    def test_create_with_pickup_all(self):
        pick_all_token = android_device.ANDROID_DEVICE_PICK_ALL_TOKEN
        actual_ads = android_device.create(pick_all_token)
        for actual, expected in zip(actual_ads,
                                    mock_android_device.get_mock_ads(5)):
            self.assertEqual(actual.serial, expected.serial)

    @mock.patch.object(
        android_device, "get_instances", new=mock_android_device.get_instances)
    @mock.patch.object(
        android_device,
        "list_adb_devices",
        new=mock_android_device.list_adb_devices)
    @mock.patch.object(
        android_device,
        "list_adb_devices_by_usb_id",
        new=mock_android_device.list_adb_devices)
    def test_create_with_string_list(self):
        string_list = [u'1', '2']
        actual_ads = android_device.create(string_list)
        for actual_ad, expected_serial in zip(actual_ads, ['1', '2']):
            self.assertEqual(actual_ad.serial, expected_serial)

    @mock.patch.object(
        android_device,
        "get_instances_with_configs",
        new=mock_android_device.get_instances_with_configs)
    @mock.patch.object(
        android_device,
        "list_adb_devices",
        new=mock_android_device.list_adb_devices)
    @mock.patch.object(
        android_device,
        "list_adb_devices_by_usb_id",
        new=mock_android_device.list_adb_devices)
    def test_create_with_dict_list(self):
        string_list = [{'serial': '1'}, {'serial': '2'}]
        actual_ads = android_device.create(string_list)
        for actual_ad, expected_serial in zip(actual_ads, ['1', '2']):
            self.assertEqual(actual_ad.serial, expected_serial)

    @mock.patch.object(
        android_device,
        "get_instances_with_configs",
        new=mock_android_device.get_instances_with_configs)
    @mock.patch.object(
        android_device,
        "list_adb_devices",
        new=mock_android_device.list_adb_devices)
    @mock.patch.object(
        android_device, "list_adb_devices_by_usb_id", return_value=['usb:1'])
    def test_create_with_usb_id(self, mock_list_adb_devices_by_usb_id):
        string_list = [{'serial': '1'}, {'serial': '2'}, {'serial': 'usb:1'}]
        actual_ads = android_device.create(string_list)
        for actual_ad, expected_serial in zip(actual_ads, ['1', '2', 'usb:1']):
            self.assertEqual(actual_ad.serial, expected_serial)

    def test_create_with_empty_config(self):
        expected_msg = android_device.ANDROID_DEVICE_EMPTY_CONFIG_MSG
        with self.assertRaisesRegex(android_device.Error, expected_msg):
            android_device.create([])

    def test_create_with_not_list_config(self):
        expected_msg = android_device.ANDROID_DEVICE_NOT_LIST_CONFIG_MSG
        with self.assertRaisesRegex(android_device.Error, expected_msg):
            android_device.create("HAHA")

    def test_create_with_no_valid_config(self):
        expected_msg = "No valid config found in: .*"
        with self.assertRaisesRegex(android_device.Error, expected_msg):
            android_device.create([1])

    def test_get_device_success_with_serial(self):
        ads = mock_android_device.get_mock_ads(5)
        expected_serial = '0'
        ad = android_device.get_device(ads, serial=expected_serial)
        self.assertEqual(ad.serial, expected_serial)

    def test_get_device_success_with_serial_and_extra_field(self):
        ads = mock_android_device.get_mock_ads(5)
        expected_serial = '1'
        expected_h_port = 5555
        ads[1].h_port = expected_h_port
        ad = android_device.get_device(
            ads, serial=expected_serial, h_port=expected_h_port)
        self.assertEqual(ad.serial, expected_serial)
        self.assertEqual(ad.h_port, expected_h_port)

    def test_get_device_no_match(self):
        ads = mock_android_device.get_mock_ads(5)
        expected_msg = ("Could not find a target device that matches condition"
                        ": {'serial': 5}.")
        with self.assertRaisesRegex(android_device.Error, expected_msg):
            ad = android_device.get_device(ads, serial=len(ads))

    def test_get_device_too_many_matches(self):
        ads = mock_android_device.get_mock_ads(5)
        target_serial = ads[1].serial = ads[0].serial
        expected_msg = "More than one device matched: \['0', '0'\]"
        with self.assertRaisesRegex(android_device.Error, expected_msg):
            android_device.get_device(ads, serial=target_serial)

    def test_start_services_on_ads(self):
        """Makes sure when an AndroidDevice fails to start some services, all
        AndroidDevice objects get cleaned up.
        """
        msg = "Some error happened."
        ads = mock_android_device.get_mock_ads(3)
        ads[0].start_services = mock.MagicMock()
        ads[0].stop_services = mock.MagicMock()
        ads[1].start_services = mock.MagicMock()
        ads[1].stop_services = mock.MagicMock()
        ads[2].start_services = mock.MagicMock(
            side_effect=android_device.Error(msg))
        ads[2].stop_services = mock.MagicMock()
        with self.assertRaisesRegex(android_device.Error, msg):
            android_device._start_services_on_ads(ads)
        ads[0].stop_services.assert_called_once_with()
        ads[1].stop_services.assert_called_once_with()
        ads[2].stop_services.assert_called_once_with()

    # Tests for android_device.AndroidDevice class.
    # These tests mock out any interaction with the OS and real android device
    # in AndroidDeivce.

    @mock.patch(
        'mobly.controllers.android_device_lib.adb.AdbProxy',
        return_value=mock_android_device.MockAdbProxy(1))
    @mock.patch(
        'mobly.controllers.android_device_lib.fastboot.FastbootProxy',
        return_value=mock_android_device.MockFastbootProxy(1))
    def test_AndroidDevice_instantiation(self, MockFastboot, MockAdbProxy):
        """Verifies the AndroidDevice object's basic attributes are correctly
        set after instantiation.
        """
        mock_serial = 1
        ad = android_device.AndroidDevice(serial=mock_serial)
        self.assertEqual(ad.serial, 1)
        self.assertEqual(ad.model, "fakemodel")
        self.assertIsNone(ad._adb_logcat_process)
        self.assertIsNone(ad.adb_logcat_file_path)
        expected_lp = os.path.join(logging.log_path,
                                   "AndroidDevice%s" % mock_serial)
        self.assertEqual(ad.log_path, expected_lp)

    @mock.patch(
        'mobly.controllers.android_device_lib.adb.AdbProxy',
        return_value=mock_android_device.MockAdbProxy(1))
    @mock.patch(
        'mobly.controllers.android_device_lib.fastboot.FastbootProxy',
        return_value=mock_android_device.MockFastbootProxy(1))
    def test_AndroidDevice_build_info(self, MockFastboot, MockAdbProxy):
        """Verifies the AndroidDevice object's basic attributes are correctly
        set after instantiation.
        """
        ad = android_device.AndroidDevice(serial=1)
        build_info = ad.build_info
        self.assertEqual(build_info["build_id"], "AB42")
        self.assertEqual(build_info["build_type"], "userdebug")

    @mock.patch(
        'mobly.controllers.android_device_lib.adb.AdbProxy',
        return_value=mock_android_device.MockAdbProxy(1))
    @mock.patch(
        'mobly.controllers.android_device_lib.fastboot.FastbootProxy',
        return_value=mock_android_device.MockFastbootProxy(1))
    @mock.patch('mobly.utils.create_dir')
    def test_AndroidDevice_take_bug_report(self, create_dir_mock,
                                           FastbootProxy, MockAdbProxy):
        """Verifies AndroidDevice.take_bug_report calls the correct adb command
        and writes the bugreport file to the correct path.
        """
        mock_serial = 1
        ad = android_device.AndroidDevice(serial=mock_serial)
        ad.take_bug_report("test_something", "sometime")
        expected_path = os.path.join(logging.log_path, "AndroidDevice%s" %
                                     ad.serial, "BugReports")
        create_dir_mock.assert_called_with(expected_path)

    @mock.patch(
        'mobly.controllers.android_device_lib.adb.AdbProxy',
        return_value=mock_android_device.MockAdbProxy(
            1, fail_br=True))
    @mock.patch(
        'mobly.controllers.android_device_lib.fastboot.FastbootProxy',
        return_value=mock_android_device.MockFastbootProxy(1))
    @mock.patch('mobly.utils.create_dir')
    def test_AndroidDevice_take_bug_report_fail(self, create_dir_mock,
                                                FastbootProxy, MockAdbProxy):
        """Verifies AndroidDevice.take_bug_report writes out the correct message
        when taking bugreport fails.
        """
        mock_serial = 1
        ad = android_device.AndroidDevice(serial=mock_serial)
        expected_msg = ".* Failed to take bugreport."
        with self.assertRaisesRegex(android_device.Error, expected_msg):
            ad.take_bug_report("test_something", "sometime")

    @mock.patch(
        'mobly.controllers.android_device_lib.adb.AdbProxy',
        return_value=mock_android_device.MockAdbProxy(
            1, fail_br_before_N=True))
    @mock.patch(
        'mobly.controllers.android_device_lib.fastboot.FastbootProxy',
        return_value=mock_android_device.MockFastbootProxy(1))
    @mock.patch('mobly.utils.create_dir')
    def test_AndroidDevice_take_bug_report_fallback(
            self, create_dir_mock, FastbootProxy, MockAdbProxy):
        """Verifies AndroidDevice.take_bug_report falls back to traditional
        bugreport on builds that do not have bugreportz.
        """
        mock_serial = 1
        ad = android_device.AndroidDevice(serial=mock_serial)
        ad.take_bug_report("test_something", "sometime")
        expected_path = os.path.join(logging.log_path, "AndroidDevice%s" %
                                     ad.serial, "BugReports")
        create_dir_mock.assert_called_with(expected_path)

    @mock.patch(
        'mobly.controllers.android_device_lib.adb.AdbProxy',
        return_value=mock_android_device.MockAdbProxy(1))
    @mock.patch(
        'mobly.controllers.android_device_lib.fastboot.FastbootProxy',
        return_value=mock_android_device.MockFastbootProxy(1))
    @mock.patch('mobly.utils.create_dir')
    @mock.patch(
        'mobly.utils.start_standing_subprocess', return_value="process")
    @mock.patch('mobly.utils.stop_standing_subprocess')
    def test_AndroidDevice_take_logcat(self, stop_proc_mock, start_proc_mock,
                                       creat_dir_mock, FastbootProxy,
                                       MockAdbProxy):
        """Verifies the steps of collecting adb logcat on an AndroidDevice
        object, including various function calls and the expected behaviors of
        the calls.
        """
        mock_serial = 1
        ad = android_device.AndroidDevice(serial=mock_serial)
        expected_msg = ".* No ongoing adb logcat collection found."
        # Expect error if stop is called before start.
        with self.assertRaisesRegex(android_device.Error, expected_msg):
            ad.stop_adb_logcat()
        ad.start_adb_logcat()
        # Verify start did the correct operations.
        self.assertTrue(ad._adb_logcat_process)
        expected_log_path = os.path.join(logging.log_path,
                                         "AndroidDevice%s" % ad.serial,
                                         "adblog,fakemodel,%s.txt" % ad.serial)
        creat_dir_mock.assert_called_with(os.path.dirname(expected_log_path))
        adb_cmd = '"adb" -s %s logcat -v threadtime  >> %s'
        start_proc_mock.assert_called_with(
            adb_cmd % (ad.serial, expected_log_path), shell=True)
        self.assertEqual(ad.adb_logcat_file_path, expected_log_path)
        expected_msg = (
            'Logcat thread is already running, cannot start another'
            ' one.')
        # Expect error if start is called back to back.
        with self.assertRaisesRegex(android_device.Error, expected_msg):
            ad.start_adb_logcat()
        # Verify stop did the correct operations.
        ad.stop_adb_logcat()
        stop_proc_mock.assert_called_with("process")
        self.assertIsNone(ad._adb_logcat_process)
        self.assertEqual(ad.adb_logcat_file_path, expected_log_path)

    @mock.patch(
        'mobly.controllers.android_device_lib.adb.AdbProxy',
        return_value=mock_android_device.MockAdbProxy(1))
    @mock.patch(
        'mobly.controllers.android_device_lib.fastboot.FastbootProxy',
        return_value=mock_android_device.MockFastbootProxy(1))
    @mock.patch('mobly.utils.create_dir')
    @mock.patch(
        'mobly.utils.start_standing_subprocess', return_value="process")
    @mock.patch('mobly.utils.stop_standing_subprocess')
    def test_AndroidDevice_take_logcat_with_user_param(
            self, stop_proc_mock, start_proc_mock, creat_dir_mock,
            FastbootProxy, MockAdbProxy):
        """Verifies the steps of collecting adb logcat on an AndroidDevice
        object, including various function calls and the expected behaviors of
        the calls.
        """
        mock_serial = 1
        ad = android_device.AndroidDevice(serial=mock_serial)
        ad.adb_logcat_param = "-b radio"
        expected_msg = '.* No ongoing adb logcat collection found.'
        # Expect error if stop is called before start.
        with self.assertRaisesRegex(android_device.Error, expected_msg):
            ad.stop_adb_logcat()
        ad.start_adb_logcat()
        # Verify start did the correct operations.
        self.assertTrue(ad._adb_logcat_process)
        expected_log_path = os.path.join(logging.log_path,
                                         "AndroidDevice%s" % ad.serial,
                                         "adblog,fakemodel,%s.txt" % ad.serial)
        creat_dir_mock.assert_called_with(os.path.dirname(expected_log_path))
        adb_cmd = '"adb" -s %s logcat -v threadtime -b radio >> %s'
        start_proc_mock.assert_called_with(
            adb_cmd % (ad.serial, expected_log_path), shell=True)
        self.assertEqual(ad.adb_logcat_file_path, expected_log_path)

    @mock.patch(
        'mobly.controllers.android_device_lib.adb.AdbProxy',
        return_value=mock_android_device.MockAdbProxy(1))
    @mock.patch(
        'mobly.controllers.android_device_lib.fastboot.FastbootProxy',
        return_value=mock_android_device.MockFastbootProxy(1))
    @mock.patch(
        'mobly.utils.start_standing_subprocess', return_value="process")
    @mock.patch('mobly.utils.stop_standing_subprocess')
    @mock.patch(
        'mobly.logger.get_log_line_timestamp',
        return_value=MOCK_ADB_LOGCAT_END_TIME)
    def test_AndroidDevice_cat_adb_log(self, mock_timestamp_getter,
                                       stop_proc_mock, start_proc_mock,
                                       FastbootProxy, MockAdbProxy):
        """Verifies that AndroidDevice.cat_adb_log loads the correct adb log
        file, locates the correct adb log lines within the given time range,
        and writes the lines to the correct output file.
        """
        mock_serial = 1
        ad = android_device.AndroidDevice(serial=mock_serial)
        # Expect error if attempted to cat adb log before starting adb logcat.
        expected_msg = (".* Attempting to cat adb log when none"
                        " has been collected.")
        with self.assertRaisesRegex(android_device.Error, expected_msg):
            ad.cat_adb_log("some_test", MOCK_ADB_LOGCAT_BEGIN_TIME)
        ad.start_adb_logcat()
        # Direct the log path of the ad to a temp dir to avoid racing.
        ad.log_path = os.path.join(self.tmp_dir, ad.log_path)
        mock_adb_log_path = os.path.join(ad.log_path, "adblog,%s,%s.txt" %
                                         (ad.model, ad.serial))
        with open(mock_adb_log_path, 'w') as f:
            f.write(MOCK_ADB_LOGCAT)
        ad.cat_adb_log("some_test", MOCK_ADB_LOGCAT_BEGIN_TIME)
        cat_file_path = os.path.join(ad.log_path, "AdbLogExcerpts", (
            "some_test,02-29 14:02:20.123,%s,%s.txt") % (ad.model, ad.serial))
        with open(cat_file_path, 'r') as f:
            actual_cat = f.read()
        self.assertEqual(actual_cat, ''.join(MOCK_ADB_LOGCAT_CAT_RESULT))
        # Stops adb logcat.
        ad.stop_adb_logcat()

    @mock.patch(
        'mobly.controllers.android_device_lib.adb.AdbProxy',
        return_value=mock_android_device.MockAdbProxy(1))
    @mock.patch(
        'mobly.controllers.android_device_lib.fastboot.FastbootProxy',
        return_value=mock_android_device.MockFastbootProxy(1))
    @mock.patch(
        'mobly.controllers.android_device_lib.snippet_client.SnippetClient')
    @mock.patch('mobly.utils.get_available_host_port')
    def test_AndroidDevice_load_snippet(self, MockGetPort, MockSnippetClient,
                                        MockFastboot, MockAdbProxy):
        ad = android_device.AndroidDevice(serial=1)
        ad.load_snippet('snippet', MOCK_SNIPPET_PACKAGE_NAME)
        self.assertTrue(hasattr(ad, 'snippet'))

    @mock.patch(
        'mobly.controllers.android_device_lib.adb.AdbProxy',
        return_value=mock_android_device.MockAdbProxy(1))
    @mock.patch(
        'mobly.controllers.android_device_lib.fastboot.FastbootProxy',
        return_value=mock_android_device.MockFastbootProxy(1))
    @mock.patch(
        'mobly.controllers.android_device_lib.snippet_client.SnippetClient',
        return_value=MockSnippetClient)
    @mock.patch('mobly.utils.get_available_host_port')
    def test_AndroidDevice_load_snippet_dup_package(
            self, MockGetPort, MockSnippetClient, MockFastboot, MockAdbProxy):
        ad = android_device.AndroidDevice(serial=1)
        ad.load_snippet('snippet', MOCK_SNIPPET_PACKAGE_NAME)
        expected_msg = ('Snippet package "%s" has already been loaded under '
                        'name "snippet".') % MOCK_SNIPPET_PACKAGE_NAME
        with self.assertRaisesRegex(android_device.Error, expected_msg):
            ad.load_snippet('snippet2', MOCK_SNIPPET_PACKAGE_NAME)

    @mock.patch(
        'mobly.controllers.android_device_lib.adb.AdbProxy',
        return_value=mock_android_device.MockAdbProxy(1))
    @mock.patch(
        'mobly.controllers.android_device_lib.fastboot.FastbootProxy',
        return_value=mock_android_device.MockFastbootProxy(1))
    @mock.patch(
        'mobly.controllers.android_device_lib.snippet_client.SnippetClient',
        return_value=MockSnippetClient)
    @mock.patch('mobly.utils.get_available_host_port')
    def test_AndroidDevice_load_snippet_dup_snippet_name(
            self, MockGetPort, MockSnippetClient, MockFastboot, MockAdbProxy):
        ad = android_device.AndroidDevice(serial=1)
        ad.load_snippet('snippet', MOCK_SNIPPET_PACKAGE_NAME)
        expected_msg = ('Attribute "%s" is already registered with package '
                        '"%s", it cannot be used again.') % (
                            'snippet', MOCK_SNIPPET_PACKAGE_NAME)
        with self.assertRaisesRegex(android_device.Error, expected_msg):
            ad.load_snippet('snippet', MOCK_SNIPPET_PACKAGE_NAME + 'haha')

    @mock.patch(
        'mobly.controllers.android_device_lib.adb.AdbProxy',
        return_value=mock_android_device.MockAdbProxy(1))
    @mock.patch(
        'mobly.controllers.android_device_lib.fastboot.FastbootProxy',
        return_value=mock_android_device.MockFastbootProxy(1))
    @mock.patch(
        'mobly.controllers.android_device_lib.snippet_client.SnippetClient')
    @mock.patch('mobly.utils.get_available_host_port')
    def test_AndroidDevice_load_snippet_dup_attribute_name(
            self, MockGetPort, MockSnippetClient, MockFastboot, MockAdbProxy):
        ad = android_device.AndroidDevice(serial=1)
        expected_msg = ('Attribute "%s" already exists, please use a different'
                        ' name') % 'adb'
        with self.assertRaisesRegex(android_device.Error, expected_msg):
            ad.load_snippet('adb', MOCK_SNIPPET_PACKAGE_NAME)

    @mock.patch(
        'mobly.controllers.android_device_lib.adb.AdbProxy',
        return_value=mock_android_device.MockAdbProxy(1))
    @mock.patch(
        'mobly.controllers.android_device_lib.fastboot.FastbootProxy',
        return_value=mock_android_device.MockFastbootProxy(1))
    @mock.patch(
        'mobly.controllers.android_device_lib.snippet_client.SnippetClient')
    @mock.patch('mobly.utils.get_available_host_port')
    def test_AndroidDevice_snippet_cleanup(
            self, MockGetPort, MockSnippetClient, MockFastboot, MockAdbProxy):
        ad = android_device.AndroidDevice(serial=1)
        ad.load_snippet('snippet', MOCK_SNIPPET_PACKAGE_NAME)
        ad.stop_services()
        self.assertFalse(hasattr(ad, 'snippet'))

    @mock.patch(
        'mobly.controllers.android_device_lib.adb.AdbProxy',
        return_value=mock_android_device.MockAdbProxy(1))
    @mock.patch(
        'mobly.controllers.android_device_lib.fastboot.FastbootProxy',
        return_value=mock_android_device.MockFastbootProxy(1))
    def test_AndroidDevice_debug_tag(self, MockFastboot, MockAdbProxy):
        mock_serial = 1
        ad = android_device.AndroidDevice(serial=mock_serial)
        self.assertEqual(ad.debug_tag, 1)
        try:
            raise android_device.DeviceError(ad, 'Something')
        except android_device.DeviceError as e:
            self.assertEqual('<AndroidDevice|1> Something', str(e))
        # Verify that debug tag's setter updates the debug prefix correctly.
        ad.debug_tag = 'Mememe'
        try:
            raise android_device.DeviceError(ad, 'Something')
        except android_device.DeviceError as e:
            self.assertEqual('<AndroidDevice|Mememe> Something', str(e))
        # Verify that repr is changed correctly.
        try:
            raise Exception(ad, 'Something')
        except Exception as e:
            self.assertEqual("(<AndroidDevice|Mememe>, 'Something')", str(e))

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